xref: /petsc/src/dm/impls/plex/plexfem.c (revision d172c84b7da0a47fed8c9ccbc9dfdecdae586ec8)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3cb1e1211SMatthew G Knepley 
4af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
5af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
6a0845e3aSMatthew G. Knepley 
746fa42a0SMatthew G. Knepley /*@
846fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
946fa42a0SMatthew G. Knepley 
1046fa42a0SMatthew G. Knepley   Not collective
1146fa42a0SMatthew G. Knepley 
1246fa42a0SMatthew G. Knepley   Input Arguments:
1346fa42a0SMatthew G. Knepley + dm   - the DM
1446fa42a0SMatthew G. Knepley - unit - The SI unit
1546fa42a0SMatthew G. Knepley 
1646fa42a0SMatthew G. Knepley   Output Argument:
1746fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
1846fa42a0SMatthew G. Knepley 
1946fa42a0SMatthew G. Knepley   Level: advanced
2046fa42a0SMatthew G. Knepley 
2146fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
2246fa42a0SMatthew G. Knepley @*/
23cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
24cb1e1211SMatthew G Knepley {
25cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
26cb1e1211SMatthew G Knepley 
27cb1e1211SMatthew G Knepley   PetscFunctionBegin;
28cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
30cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
31cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
32cb1e1211SMatthew G Knepley }
33cb1e1211SMatthew G Knepley 
3446fa42a0SMatthew G. Knepley /*@
3546fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
3646fa42a0SMatthew G. Knepley 
3746fa42a0SMatthew G. Knepley   Not collective
3846fa42a0SMatthew G. Knepley 
3946fa42a0SMatthew G. Knepley   Input Arguments:
4046fa42a0SMatthew G. Knepley + dm   - the DM
4146fa42a0SMatthew G. Knepley . unit - The SI unit
4246fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
4346fa42a0SMatthew G. Knepley 
4446fa42a0SMatthew G. Knepley   Level: advanced
4546fa42a0SMatthew G. Knepley 
4646fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
4746fa42a0SMatthew G. Knepley @*/
48cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
49cb1e1211SMatthew G Knepley {
50cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
51cb1e1211SMatthew G Knepley 
52cb1e1211SMatthew G Knepley   PetscFunctionBegin;
53cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
55cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
56cb1e1211SMatthew G Knepley }
57cb1e1211SMatthew G Knepley 
58d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx)
59026175e5SToby Isaac {
6012adca46SMatthew 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}}};
61026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
62ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
63026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
64026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
65026175e5SToby Isaac 
66ad917190SMatthew G. Knepley   PetscFunctionBegin;
67ad917190SMatthew G. Knepley   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2);
68026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
69026175e5SToby Isaac   if (d < dim) {
7012adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
71ad917190SMatthew G. Knepley   } else {
72026175e5SToby Isaac     for (i = 0; i < dim; i++) {
73026175e5SToby Isaac       for (j = 0; j < dim; j++) {
7412adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
75026175e5SToby Isaac       }
76026175e5SToby Isaac     }
77026175e5SToby Isaac   }
78ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
79026175e5SToby Isaac }
80026175e5SToby Isaac 
81cb1e1211SMatthew G Knepley /*@C
8212adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
83cb1e1211SMatthew G Knepley 
84cb1e1211SMatthew G Knepley   Collective on DM
85cb1e1211SMatthew G Knepley 
86cb1e1211SMatthew G Knepley   Input Arguments:
87026175e5SToby Isaac . dm - the DM
88cb1e1211SMatthew G Knepley 
89cb1e1211SMatthew G Knepley   Output Argument:
90cb1e1211SMatthew G Knepley . sp - the null space
91cb1e1211SMatthew G Knepley 
9212adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
93cb1e1211SMatthew G Knepley 
94cb1e1211SMatthew G Knepley   Level: advanced
95cb1e1211SMatthew G Knepley 
9612adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
97cb1e1211SMatthew G Knepley @*/
98026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp)
99cb1e1211SMatthew G Knepley {
100cb1e1211SMatthew G Knepley   MPI_Comm       comm;
101026175e5SToby Isaac   Vec            mode[6];
102026175e5SToby Isaac   PetscSection   section, globalSection;
1039d8fbdeaSMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, d, i, j;
104cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
105cb1e1211SMatthew G Knepley 
106cb1e1211SMatthew G Knepley   PetscFunctionBegin;
107cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
108c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1099d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
110cb1e1211SMatthew G Knepley   if (dim == 1) {
111cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
112cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
113cb1e1211SMatthew G Knepley   }
114026175e5SToby Isaac   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
115026175e5SToby Isaac   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
116cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
117cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
118cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
119cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
120cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
121cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
122026175e5SToby Isaac   for (d = 0; d < m; d++) {
123330485fdSToby Isaac     PetscInt         ctx[2];
124d1828a1cSMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
125026175e5SToby Isaac     void            *voidctx = (void *) (&ctx[0]);
126cb1e1211SMatthew G Knepley 
1279d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
128330485fdSToby Isaac     ctx[1] = d;
1290709b2feSToby Isaac     ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
130cb1e1211SMatthew G Knepley   }
131cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
132cb1e1211SMatthew G Knepley   /* Orthonormalize system */
133cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
134cb1e1211SMatthew G Knepley     PetscScalar dots[6];
135cb1e1211SMatthew G Knepley 
136cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
137cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
138cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
139cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
140cb1e1211SMatthew G Knepley   }
141cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
142cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
143cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
144cb1e1211SMatthew G Knepley }
145cb1e1211SMatthew G Knepley 
14612adca46SMatthew G. Knepley /*@C
14712adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
14812adca46SMatthew G. Knepley 
14912adca46SMatthew G. Knepley   Collective on DM
15012adca46SMatthew G. Knepley 
15112adca46SMatthew G. Knepley   Input Arguments:
15212adca46SMatthew G. Knepley + dm    - the DM
15312adca46SMatthew G. Knepley . nb    - The number of bodies
15412adca46SMatthew G. Knepley . label - The DMLabel marking each domain
15512adca46SMatthew G. Knepley . nids  - The number of ids per body
15612adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
15712adca46SMatthew G. Knepley 
15812adca46SMatthew G. Knepley   Output Argument:
15912adca46SMatthew G. Knepley . sp - the null space
16012adca46SMatthew G. Knepley 
16112adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
16212adca46SMatthew G. Knepley 
16312adca46SMatthew G. Knepley   Level: advanced
16412adca46SMatthew G. Knepley 
16512adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
16612adca46SMatthew G. Knepley @*/
16712adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
16812adca46SMatthew G. Knepley {
16912adca46SMatthew G. Knepley   MPI_Comm       comm;
17012adca46SMatthew G. Knepley   PetscSection   section, globalSection;
17112adca46SMatthew G. Knepley   Vec           *mode;
17212adca46SMatthew G. Knepley   PetscScalar   *dots;
17312adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
17412adca46SMatthew G. Knepley   PetscErrorCode ierr;
17512adca46SMatthew G. Knepley 
17612adca46SMatthew G. Knepley   PetscFunctionBegin;
17712adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
17812adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
17912adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
18012adca46SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
18112adca46SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
18212adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
18312adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
18412adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
18512adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
18612adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
18712adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
18812adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
18912adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
19012adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
19112adca46SMatthew G. Knepley       PetscInt         ctx[2];
19212adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
19312adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
19412adca46SMatthew G. Knepley 
19512adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
19612adca46SMatthew G. Knepley       ctx[1] = d;
19712adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
19812adca46SMatthew G. Knepley       off   += nids[b];
19912adca46SMatthew G. Knepley     }
20012adca46SMatthew G. Knepley   }
20112adca46SMatthew G. Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
20212adca46SMatthew G. Knepley   /* Orthonormalize system */
20312adca46SMatthew G. Knepley   for (i = 0; i < m; ++i) {
20412adca46SMatthew G. Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
20512adca46SMatthew G. Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
20612adca46SMatthew G. Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
20712adca46SMatthew G. Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
20812adca46SMatthew G. Knepley   }
20912adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
21012adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
21112adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
21212adca46SMatthew G. Knepley   PetscFunctionReturn(0);
21312adca46SMatthew G. Knepley }
21412adca46SMatthew G. Knepley 
215b29cfa1cSToby Isaac /*@
216b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
217b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
218b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
219b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
220b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
221b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
222b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
223b29cfa1cSToby Isaac 
224b29cfa1cSToby Isaac   Input Parameters:
225b29cfa1cSToby Isaac + dm - the DMPlex object
226b29cfa1cSToby Isaac - height - the maximum projection height >= 0
227b29cfa1cSToby Isaac 
228b29cfa1cSToby Isaac   Level: advanced
229b29cfa1cSToby Isaac 
2304d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
231b29cfa1cSToby Isaac @*/
232b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
233b29cfa1cSToby Isaac {
234b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
235b29cfa1cSToby Isaac 
236b29cfa1cSToby Isaac   PetscFunctionBegin;
237b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
238b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
239b29cfa1cSToby Isaac   PetscFunctionReturn(0);
240b29cfa1cSToby Isaac }
241b29cfa1cSToby Isaac 
242b29cfa1cSToby Isaac /*@
243b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
244b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
245b29cfa1cSToby Isaac 
246b29cfa1cSToby Isaac   Input Parameters:
247b29cfa1cSToby Isaac . dm - the DMPlex object
248b29cfa1cSToby Isaac 
249b29cfa1cSToby Isaac   Output Parameters:
250b29cfa1cSToby Isaac . height - the maximum projection height
251b29cfa1cSToby Isaac 
252b29cfa1cSToby Isaac   Level: intermediate
253b29cfa1cSToby Isaac 
2544d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
255b29cfa1cSToby Isaac @*/
256b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
257b29cfa1cSToby Isaac {
258b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
259b29cfa1cSToby Isaac 
260b29cfa1cSToby Isaac   PetscFunctionBegin;
261b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
262b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
263b29cfa1cSToby Isaac   PetscFunctionReturn(0);
264b29cfa1cSToby Isaac }
265b29cfa1cSToby Isaac 
266b278463cSMatthew G. Knepley /*@C
267b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector
268b278463cSMatthew G. Knepley 
269b278463cSMatthew G. Knepley   Input Parameters:
270b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
271b278463cSMatthew G. Knepley . time   - The time
272b278463cSMatthew G. Knepley . field  - The field to constrain
2731c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
2741c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
275b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
276b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
277b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
278b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
279b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
280b278463cSMatthew G. Knepley 
281b278463cSMatthew G. Knepley   Output Parameter:
282b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
283b278463cSMatthew G. Knepley 
284b278463cSMatthew G. Knepley   Level: developer
285b278463cSMatthew G. Knepley 
286b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
287b278463cSMatthew G. Knepley @*/
2881c531cf8SMatthew 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)
28955f2e967SMatthew G. Knepley {
2900163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
29155f2e967SMatthew G. Knepley   void            **ctxs;
292d7ddef95SMatthew G. Knepley   PetscInt          numFields;
293d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
294d7ddef95SMatthew G. Knepley 
295d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
296d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
297d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
298d7ddef95SMatthew G. Knepley   funcs[field] = func;
299d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
3001c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
301d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
302d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
303d7ddef95SMatthew G. Knepley }
304d7ddef95SMatthew G. Knepley 
305b278463cSMatthew G. Knepley /*@C
306b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector
307b278463cSMatthew G. Knepley 
308b278463cSMatthew G. Knepley   Input Parameters:
309b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
310b278463cSMatthew G. Knepley . time   - The time
311b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
312b278463cSMatthew G. Knepley . field  - The field to constrain
3131c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3141c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
315b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
316b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
317b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
318b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
319b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
320b278463cSMatthew G. Knepley 
321b278463cSMatthew G. Knepley   Output Parameter:
322b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
323b278463cSMatthew G. Knepley 
324b278463cSMatthew G. Knepley   Level: developer
325b278463cSMatthew G. Knepley 
326b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary()
327b278463cSMatthew G. Knepley @*/
3281c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
329c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
330c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
331c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
33297b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
333b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
334b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
335c60e475cSMatthew G. Knepley {
336c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
337c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
338c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
33997b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
340c60e475cSMatthew G. Knepley   void            **ctxs;
341c60e475cSMatthew G. Knepley   PetscInt          numFields;
342c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
343c60e475cSMatthew G. Knepley 
344c60e475cSMatthew G. Knepley   PetscFunctionBegin;
345c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
346c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
347c60e475cSMatthew G. Knepley   funcs[field] = func;
348c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
3491c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
350c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
351c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
352c60e475cSMatthew G. Knepley }
353c60e475cSMatthew G. Knepley 
354b278463cSMatthew G. Knepley /*@C
355b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
356b278463cSMatthew G. Knepley 
357b278463cSMatthew G. Knepley   Input Parameters:
358b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
359b278463cSMatthew G. Knepley . time   - The time
360b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
361b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
362b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
363b278463cSMatthew G. Knepley . field  - The field to constrain
3641c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3651c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
366b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
367b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
368b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
369b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
370b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
371b278463cSMatthew G. Knepley 
372b278463cSMatthew G. Knepley   Output Parameter:
373b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
374b278463cSMatthew G. Knepley 
375b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
376b278463cSMatthew G. Knepley 
377b278463cSMatthew G. Knepley   Level: developer
378b278463cSMatthew G. Knepley 
379b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
380b278463cSMatthew G. Knepley @*/
3811c531cf8SMatthew 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[],
382b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
383d7ddef95SMatthew G. Knepley {
38461f58d28SMatthew G. Knepley   PetscDS            prob;
385da97024aSMatthew G. Knepley   PetscSF            sf;
386d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
38720369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
388da97024aSMatthew G. Knepley   const PetscInt    *leaves;
389d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
390520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
391e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
392d7ddef95SMatthew G. Knepley 
393d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
394da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
395da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
396da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
397d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
398d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
39961f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
400d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
401d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
40220369375SToby Isaac   if (cellGeometry) {
403d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
404d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
40520369375SToby Isaac   }
406d7ddef95SMatthew G. Knepley   if (Grad) {
407c0a6632aSMatthew G. Knepley     PetscFV fv;
408c0a6632aSMatthew G. Knepley 
409c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
410d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
411d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
412d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
41369291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
414d7ddef95SMatthew G. Knepley   }
415d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
416d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
417d7ddef95SMatthew G. Knepley     IS              faceIS;
418d7ddef95SMatthew G. Knepley     const PetscInt *faces;
419d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
420d7ddef95SMatthew G. Knepley 
421d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
422d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
423d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
424d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
425d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
426d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
427640bce14SSatish Balay       PetscFVFaceGeom        *fg;
428d7ddef95SMatthew G. Knepley 
429d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
430da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
431da97024aSMatthew G. Knepley       if (loc >= 0) continue;
432d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
433d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
434d7ddef95SMatthew G. Knepley       if (Grad) {
435640bce14SSatish Balay         PetscFVCellGeom       *cg;
436640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
437d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
438d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
439d7ddef95SMatthew G. Knepley         PetscInt               d;
440d7ddef95SMatthew G. Knepley 
441d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
442d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
443d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
444520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
445d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
446d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
447e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
448e735a8a9SMatthew G. Knepley         if (ierru) {
449e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
450e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
451e735a8a9SMatthew G. Knepley           goto cleanup;
452e735a8a9SMatthew G. Knepley         }
453d7ddef95SMatthew G. Knepley       } else {
454640bce14SSatish Balay         PetscScalar       *xI;
455d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
456d7ddef95SMatthew G. Knepley 
457d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
458520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
459e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
460e735a8a9SMatthew G. Knepley         if (ierru) {
461e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
462e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
463e735a8a9SMatthew G. Knepley           goto cleanup;
464e735a8a9SMatthew G. Knepley         }
465d7ddef95SMatthew G. Knepley       }
466d7ddef95SMatthew G. Knepley     }
467d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
468d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
469d7ddef95SMatthew G. Knepley   }
470e735a8a9SMatthew G. Knepley   cleanup:
471d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
472d7ddef95SMatthew G. Knepley   if (Grad) {
47369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
474d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
475d7ddef95SMatthew G. Knepley   }
476e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
477d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
478e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
479d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
480d7ddef95SMatthew G. Knepley }
481d7ddef95SMatthew G. Knepley 
482f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
483d7ddef95SMatthew G. Knepley {
484d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
48555f2e967SMatthew G. Knepley   PetscErrorCode ierr;
48655f2e967SMatthew G. Knepley 
48755f2e967SMatthew G. Knepley   PetscFunctionBegin;
488dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
48955f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
490f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
491d7ddef95SMatthew G. Knepley     const char             *labelname;
492d7ddef95SMatthew G. Knepley     DMLabel                 label;
4931c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
4941c531cf8SMatthew G. Knepley     const PetscInt         *comps;
495d7ddef95SMatthew G. Knepley     PetscObject             obj;
496d7ddef95SMatthew G. Knepley     PetscClassId            id;
497a30ec4eaSSatish Balay     void                    (*func)(void);
498d7ddef95SMatthew G. Knepley     PetscInt                numids;
499d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
50055f2e967SMatthew G. Knepley     void                   *ctx;
50155f2e967SMatthew G. Knepley 
5021c531cf8SMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, NULL, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
503f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
504c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
505d7ddef95SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
506d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
507d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
508c60e475cSMatthew G. Knepley       switch (type) {
509c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
510c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
511092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
5121c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
513092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
514c60e475cSMatthew G. Knepley         break;
515c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
516c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
5171c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
518b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
519c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
52097b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
521c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
522c60e475cSMatthew G. Knepley         break;
523c60e475cSMatthew G. Knepley       default: break;
524c60e475cSMatthew G. Knepley       }
525d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
52643ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
5271c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
528b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
529d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
53055f2e967SMatthew G. Knepley   }
53155f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
53255f2e967SMatthew G. Knepley }
53355f2e967SMatthew G. Knepley 
534f1d73a7aSMatthew G. Knepley /*@
535f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
536f1d73a7aSMatthew G. Knepley 
537f1d73a7aSMatthew G. Knepley   Input Parameters:
538f1d73a7aSMatthew G. Knepley + dm - The DM
539f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
540f1d73a7aSMatthew G. Knepley . time - The time
541f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
542f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
543f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
544f1d73a7aSMatthew G. Knepley 
545f1d73a7aSMatthew G. Knepley   Output Parameters:
546f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
547f1d73a7aSMatthew G. Knepley 
548f1d73a7aSMatthew G. Knepley   Level: developer
549f1d73a7aSMatthew G. Knepley 
550f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
551f1d73a7aSMatthew G. Knepley @*/
552f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
553f1d73a7aSMatthew G. Knepley {
554f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
555f1d73a7aSMatthew G. Knepley 
556f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
557f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
558f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
559f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
560f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
561f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
562f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
563f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
564f1d73a7aSMatthew G. Knepley }
565f1d73a7aSMatthew G. Knepley 
5660709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
567cb1e1211SMatthew G Knepley {
568574a98acSMatthew G. Knepley   Vec              localX;
569574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
570574a98acSMatthew G. Knepley 
571574a98acSMatthew G. Knepley   PetscFunctionBegin;
572574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
5735d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
574574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
575574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
576574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
577574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
578574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
579574a98acSMatthew G. Knepley }
580574a98acSMatthew G. Knepley 
581574a98acSMatthew G. Knepley /*@C
582574a98acSMatthew G. Knepley   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
583574a98acSMatthew G. Knepley 
584574a98acSMatthew G. Knepley   Input Parameters:
585574a98acSMatthew G. Knepley + dm     - The DM
586574a98acSMatthew G. Knepley . time   - The time
587574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
588574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
589574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
590574a98acSMatthew G. Knepley 
591574a98acSMatthew G. Knepley   Output Parameter:
592574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
593574a98acSMatthew G. Knepley 
594574a98acSMatthew G. Knepley   Level: developer
595574a98acSMatthew G. Knepley 
596574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
597574a98acSMatthew G. Knepley @*/
598574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
599574a98acSMatthew G. Knepley {
600cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
601cb1e1211SMatthew G Knepley   PetscSection     section;
602c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
60315496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
6047318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
605cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
6067318780aSToby Isaac   const PetscReal *quadWeights;
607aed3cbd0SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset;
608cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
609cb1e1211SMatthew G Knepley 
610cb1e1211SMatthew G Knepley   PetscFunctionBegin;
611c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6127318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
613cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
614cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
615cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
61615496722SMatthew G. Knepley     PetscObject  obj;
61715496722SMatthew G. Knepley     PetscClassId id;
618c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
619c5bbbd5bSMatthew G. Knepley 
62015496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
62115496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
62215496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
62315496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
62415496722SMatthew G. Knepley 
6250f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
6260f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
62715496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
62815496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
62915496722SMatthew G. Knepley 
63015496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
63115496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
63215496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
633c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
634cb1e1211SMatthew G Knepley   }
6359c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
636beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
6377318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
638aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
639aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6409ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6419ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
642cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
643a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
644cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
6459c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
646cb1e1211SMatthew G Knepley 
6477318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
648cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
649cb1e1211SMatthew G Knepley 
65015496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
65115496722SMatthew G. Knepley       PetscObject  obj;
65215496722SMatthew G. Knepley       PetscClassId id;
653c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
65415496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
655cb1e1211SMatthew G Knepley 
65615496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
65715496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
65815496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
65915496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
66015496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
661cb1e1211SMatthew G Knepley       if (debug) {
662cb1e1211SMatthew G Knepley         char title[1024];
663cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
6644c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
665cb1e1211SMatthew G Knepley       }
6667318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
6677318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", detJ[q], c, q);
6687318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim * q], Nc, funcVal, ctx);
669e735a8a9SMatthew G. Knepley         if (ierr) {
670e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
671e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
672e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
6737318780aSToby Isaac           ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
674e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
675e735a8a9SMatthew G. Knepley         }
67615496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
67715496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
67815496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
67915496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
680beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
681beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
682beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
683cb1e1211SMatthew G Knepley         }
684cb1e1211SMatthew G Knepley       }
6859c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
686beaa55a6SMatthew G. Knepley       qc += Nc;
687cb1e1211SMatthew G Knepley     }
688cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
689cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
690cb1e1211SMatthew G Knepley     localDiff += elemDiff;
691cb1e1211SMatthew G Knepley   }
6927318780aSToby Isaac   ierr  = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
693b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
694cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
695cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
696cb1e1211SMatthew G Knepley }
697cb1e1211SMatthew G Knepley 
698b698f381SToby 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)
699cb1e1211SMatthew G Knepley {
70040e14135SMatthew G. Knepley   const PetscInt   debug = 0;
701cb1e1211SMatthew G Knepley   PetscSection     section;
70240e14135SMatthew G. Knepley   PetscQuadrature  quad;
70340e14135SMatthew G. Knepley   Vec              localX;
7049c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
7059c3cf19fSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7067318780aSToby Isaac   PetscReal       *coords, *realSpaceDer, *J, *invJ, *detJ;
70740e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
7089c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset;
709cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
710cb1e1211SMatthew G Knepley 
711cb1e1211SMatthew G Knepley   PetscFunctionBegin;
712c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7137318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
71440e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
71540e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
71640e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
71740e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
71840e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
719652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
7200f2d7e86SMatthew G. Knepley     PetscFE  fe;
72140e14135SMatthew G. Knepley     PetscInt Nc;
722652b88e8SMatthew G. Knepley 
7230f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
7240f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7250f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
72640e14135SMatthew G. Knepley     numComponents += Nc;
727652b88e8SMatthew G. Knepley   }
7289c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
729beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
7304d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
7319c3cf19fSMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*Nq,&realSpaceDer,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ,numComponents,&interpolant,Nq,&detJ);CHKERRQ(ierr);
73240e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7339ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
7349ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
73540e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
73640e14135SMatthew G. Knepley     PetscScalar *x = NULL;
73740e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
7389c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
739652b88e8SMatthew G. Knepley 
7407318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
74140e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
74240e14135SMatthew G. Knepley 
7439c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
7440f2d7e86SMatthew G. Knepley       PetscFE          fe;
74551259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
74640e14135SMatthew G. Knepley       PetscReal       *basisDer;
7479c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
74840e14135SMatthew G. Knepley 
7490f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
7500f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
7519c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
7520f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
75340e14135SMatthew G. Knepley       if (debug) {
75440e14135SMatthew G. Knepley         char title[1024];
75540e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
7569c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
757652b88e8SMatthew G. Knepley       }
7589c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
7597318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", detJ[q], c, q);
7607318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx);
761e735a8a9SMatthew G. Knepley         if (ierr) {
762e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
763e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
764e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
7659c3cf19fSMatthew G. Knepley           ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2);
766e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
767e735a8a9SMatthew G. Knepley         }
7689c3cf19fSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr);
7699c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
770beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
771beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
772beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
77340e14135SMatthew G. Knepley         }
77440e14135SMatthew G. Knepley       }
7759c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
7769c3cf19fSMatthew G. Knepley       qc          += Nc;
77740e14135SMatthew G. Knepley     }
77840e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
77940e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
78040e14135SMatthew G. Knepley     localDiff += elemDiff;
78140e14135SMatthew G. Knepley   }
7829c3cf19fSMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr);
78340e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
784b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
78540e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
786cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
787cb1e1211SMatthew G Knepley }
788cb1e1211SMatthew G Knepley 
789c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
79073d901b8SMatthew G. Knepley {
79173d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
79273d901b8SMatthew G. Knepley   PetscSection     section;
79373d901b8SMatthew G. Knepley   PetscQuadrature  quad;
79473d901b8SMatthew G. Knepley   Vec              localX;
79515496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
7967318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
79773d901b8SMatthew G. Knepley   PetscReal       *localDiff;
79815496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7999c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
80073d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
80173d901b8SMatthew G. Knepley 
80273d901b8SMatthew G. Knepley   PetscFunctionBegin;
803c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8047318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
80573d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
80673d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
80773d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
808bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
80973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
81073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
81173d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
81215496722SMatthew G. Knepley     PetscObject  obj;
81315496722SMatthew G. Knepley     PetscClassId id;
81473d901b8SMatthew G. Knepley     PetscInt     Nc;
81573d901b8SMatthew G. Knepley 
81615496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
81715496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
81815496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
81915496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
82015496722SMatthew G. Knepley 
8210f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8220f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
82315496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
82415496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
82515496722SMatthew G. Knepley 
82615496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
82715496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
82815496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
82973d901b8SMatthew G. Knepley     numComponents += Nc;
83073d901b8SMatthew G. Knepley   }
8319c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
832beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
8337318780aSToby Isaac   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
83473d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8359ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8369ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
83773d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
83873d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
8399c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
84073d901b8SMatthew G. Knepley 
8417318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
84273d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
84373d901b8SMatthew G. Knepley 
84415496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
84515496722SMatthew G. Knepley       PetscObject  obj;
84615496722SMatthew G. Knepley       PetscClassId id;
84773d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
84815496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
84973d901b8SMatthew G. Knepley 
85015496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
85115496722SMatthew G. Knepley 
85215496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
85315496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
85415496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
85515496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
85615496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
85773d901b8SMatthew G. Knepley       if (debug) {
85873d901b8SMatthew G. Knepley         char title[1024];
85973d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
86015496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
86173d901b8SMatthew G. Knepley       }
8627318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
8637318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature point %D", detJ, c, q);
8647318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim*q], numFields, funcVal, ctx);
865e735a8a9SMatthew G. Knepley         if (ierr) {
866e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
867e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
868e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
8697318780aSToby Isaac           ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
870e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
871e735a8a9SMatthew G. Knepley         }
87215496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
87315496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
87415496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
87515496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
876beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
877beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d point %g %g %g diff %g\n", c, field, coordDim > 0 ? coords[0] : 0., coordDim > 1 ? coords[1] : 0., coordDim > 2 ? coords[2] : 0., PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
878beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
87973d901b8SMatthew G. Knepley         }
88073d901b8SMatthew G. Knepley       }
881beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
8829c3cf19fSMatthew G. Knepley       qc          += Nc;
88373d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
88473d901b8SMatthew G. Knepley     }
88573d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
88673d901b8SMatthew G. Knepley   }
88773d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
888b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
88973d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
8907318780aSToby Isaac   ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
89173d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
89273d901b8SMatthew G. Knepley }
89373d901b8SMatthew G. Knepley 
894e729f68cSMatthew G. Knepley /*@C
895e729f68cSMatthew 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.
896e729f68cSMatthew G. Knepley 
897e729f68cSMatthew G. Knepley   Input Parameters:
898e729f68cSMatthew G. Knepley + dm    - The DM
8990163fd50SMatthew G. Knepley . time  - The time
900ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
901e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
902e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
903e729f68cSMatthew G. Knepley 
904e729f68cSMatthew G. Knepley   Output Parameter:
905e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
906e729f68cSMatthew G. Knepley 
907e729f68cSMatthew G. Knepley   Level: developer
908e729f68cSMatthew G. Knepley 
909b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
910e729f68cSMatthew G. Knepley @*/
9110163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
912e729f68cSMatthew G. Knepley {
913e729f68cSMatthew G. Knepley   PetscSection     section;
914e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
915e729f68cSMatthew G. Knepley   Vec              localX;
916e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
9177318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
918e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
9199c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
920e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
921e729f68cSMatthew G. Knepley 
922e729f68cSMatthew G. Knepley   PetscFunctionBegin;
923e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
924e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9257318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
926e729f68cSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
927e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
928e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
929bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
930e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
931e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
932e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
933e729f68cSMatthew G. Knepley     PetscObject  obj;
934e729f68cSMatthew G. Knepley     PetscClassId id;
935e729f68cSMatthew G. Knepley     PetscInt     Nc;
936e729f68cSMatthew G. Knepley 
937e729f68cSMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
938e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
939e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
940e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
941e729f68cSMatthew G. Knepley 
942e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
943e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
944e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
945e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
946e729f68cSMatthew G. Knepley 
947e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
948e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
949e729f68cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
950e729f68cSMatthew G. Knepley     numComponents += Nc;
951e729f68cSMatthew G. Knepley   }
9529c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
953beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
9547318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
955e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
956e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
957e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
958e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
959e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
9606f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
9619c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
962e729f68cSMatthew G. Knepley 
9637318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
964e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
965e729f68cSMatthew G. Knepley 
966e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
967e729f68cSMatthew G. Knepley       PetscObject  obj;
968e729f68cSMatthew G. Knepley       PetscClassId id;
969e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
970e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
971e729f68cSMatthew G. Knepley 
972e729f68cSMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
973e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
974e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
975e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
976e729f68cSMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
97723f34ed2SToby Isaac       if (funcs[field]) {
9787318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
9797f79407eSBarry Smith           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], c, q);
9807318780aSToby Isaac           ierr = (*funcs[field])(dim, time, &coords[q*coordDim], Nc, funcVal, ctx);
981e735a8a9SMatthew G. Knepley           if (ierr) {
982e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
983e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
9847f79407eSBarry Smith             ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
985e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
986e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
987e735a8a9SMatthew G. Knepley           }
988e729f68cSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
989e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
990e729f68cSMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
991e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
992beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
993beaa55a6SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
994e729f68cSMatthew G. Knepley           }
995e729f68cSMatthew G. Knepley         }
99623f34ed2SToby Isaac       }
997beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
9989c3cf19fSMatthew G. Knepley       qc          += Nc;
999e729f68cSMatthew G. Knepley     }
1000e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
100123f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1002e729f68cSMatthew G. Knepley   }
10037318780aSToby Isaac   ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
1004e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1005e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1006e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1007e729f68cSMatthew G. Knepley }
1008e729f68cSMatthew G. Knepley 
10091555c271SMatthew G. Knepley /*@C
10101555c271SMatthew 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.
10111555c271SMatthew G. Knepley 
10121555c271SMatthew G. Knepley   Input Parameters:
10131555c271SMatthew G. Knepley + dm - The DM
10141555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
10151555c271SMatthew G. Knepley 
10161555c271SMatthew G. Knepley   Output Parameter:
10171555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
10181555c271SMatthew G. Knepley 
10191555c271SMatthew G. Knepley   Notes: Add citation to (Clement, 1975) and definition of the interpolant
10201555c271SMatthew 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
10211555c271SMatthew G. Knepley 
10221555c271SMatthew G. Knepley   Level: developer
10231555c271SMatthew G. Knepley 
10241555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
10251555c271SMatthew G. Knepley @*/
10261555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
10271555c271SMatthew G. Knepley {
1028db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1029db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
10301555c271SMatthew G. Knepley   DM               dmC;
10311555c271SMatthew G. Knepley   PetscSection     section;
10321555c271SMatthew G. Knepley   PetscQuadrature  quad;
10331555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
10341555c271SMatthew G. Knepley   PetscReal       *coords, *detJ, *J, *invJ;
10351555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
10361555c271SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset;
10371555c271SMatthew G. Knepley   PetscErrorCode   ierr;
10381555c271SMatthew G. Knepley 
10391555c271SMatthew G. Knepley   PetscFunctionBegin;
10401555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
10411555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
10421555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10431555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
10441555c271SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
10451555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
10461555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
10471555c271SMatthew G. Knepley     PetscObject  obj;
10481555c271SMatthew G. Knepley     PetscClassId id;
10491555c271SMatthew G. Knepley     PetscInt     Nc;
10501555c271SMatthew G. Knepley 
10511555c271SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
10521555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
10531555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
10541555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
10551555c271SMatthew G. Knepley 
10561555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10571555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
10581555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
10591555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
10601555c271SMatthew G. Knepley 
10611555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
10621555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
10631555c271SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
10641555c271SMatthew G. Knepley     numComponents += Nc;
10651555c271SMatthew G. Knepley   }
10661555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
10671555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
10681555c271SMatthew G. Knepley   ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ);CHKERRQ(ierr);
10691555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
10701555c271SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10711555c271SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10721555c271SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
10731555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
10741555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
10751555c271SMatthew G. Knepley     PetscInt   *star = NULL;
10761555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
10771555c271SMatthew G. Knepley 
10781555c271SMatthew G. Knepley     ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
10791555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
10801555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
10811555c271SMatthew G. Knepley       const PetscInt cell = star[st];
10821555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
10831555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
10841555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
10851555c271SMatthew G. Knepley 
10861555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
10871555c271SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
10881555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
10891555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
10901555c271SMatthew G. Knepley         PetscObject  obj;
10911555c271SMatthew G. Knepley         PetscClassId id;
10921555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
10931555c271SMatthew G. Knepley 
10941555c271SMatthew G. Knepley         ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
10951555c271SMatthew G. Knepley         ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
10961555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
10971555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
10981555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
10991555c271SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11001555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
11011555c271SMatthew G. Knepley           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], cell, q);
11021555c271SMatthew G. Knepley           if (ierr) {
11031555c271SMatthew G. Knepley             PetscErrorCode ierr2;
11041555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
11051555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
11061555c271SMatthew G. Knepley             ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2);
11071555c271SMatthew G. Knepley             CHKERRQ(ierr);
11081555c271SMatthew G. Knepley           }
11091555c271SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);}
11101555c271SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11111555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
11121555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
11131555c271SMatthew G. Knepley 
11141555c271SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q];
11151555c271SMatthew G. Knepley           }
11161555c271SMatthew G. Knepley           vol += quadWeights[q*qNc]*detJ[q];
11171555c271SMatthew G. Knepley         }
11181555c271SMatthew G. Knepley         fieldOffset += Nb;
11191555c271SMatthew G. Knepley         qc          += Nc;
11201555c271SMatthew G. Knepley       }
11211555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1122f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1123f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1124f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1125f8527842SMatthew G. Knepley         }
1126f8527842SMatthew G. Knepley       }
1127f8527842SMatthew G. Knepley       volsum += vol;
1128db1066baSMatthew G. Knepley       if (debug) {
11296d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
11301555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
11311555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
11321555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
11336d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
11341555c271SMatthew G. Knepley           }
11351555c271SMatthew G. Knepley         }
11361555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1137db1066baSMatthew G. Knepley       }
11381555c271SMatthew G. Knepley     }
11391555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
11401555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
11411555c271SMatthew G. Knepley     }
11421555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
11431555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
11441555c271SMatthew G. Knepley   }
11451555c271SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr);
11461555c271SMatthew G. Knepley   PetscFunctionReturn(0);
11471555c271SMatthew G. Knepley }
11481555c271SMatthew G. Knepley 
1149338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
115073d901b8SMatthew G. Knepley {
1151338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
1152338f77d5SMatthew G. Knepley   PetscDS            prob,    probAux;
115373d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1154338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1155338f77d5SMatthew G. Knepley   PetscInt           dim, numCells = cEnd - cStart, cell, c, f;
1156338f77d5SMatthew G. Knepley   PetscBool          useFEM = PETSC_FALSE, useFVM = PETSC_FALSE;
1157338f77d5SMatthew G. Knepley   /* DS */
1158338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1159338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1160338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1161338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1162338f77d5SMatthew G. Knepley   /* Geometry */
1163b5a3613cSMatthew G. Knepley   PetscFECellGeom   *cgeomFEM;
1164338f77d5SMatthew G. Knepley   DM                 dmGrad;
1165338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1166b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1167338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
116873d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
116973d901b8SMatthew G. Knepley 
117073d901b8SMatthew G. Knepley   PetscFunctionBegin;
1171338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1172c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
117373d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
117473d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1175338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1176b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1177b5a3613cSMatthew G. Knepley     PetscObject  obj;
1178b5a3613cSMatthew G. Knepley     PetscClassId id;
1179b5a3613cSMatthew G. Knepley 
1180b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1181b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1182338f77d5SMatthew G. Knepley     if (id == PETSCFE_CLASSID) useFEM = PETSC_TRUE;
1183338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1184338f77d5SMatthew G. Knepley   }
1185338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1186338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1187338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1188338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1189338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1190338f77d5SMatthew G. Knepley   /* Read DS information */
1191338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1192338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1193338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1194338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1195338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1196338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1197338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1198338f77d5SMatthew G. Knepley   if (dmAux) {
1199338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1200338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
1201338f77d5SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
1202338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1203338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1204338f77d5SMatthew G. Knepley   }
1205338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1206338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1207338f77d5SMatthew G. Knepley   if (useFEM) {ierr = PetscMalloc1(numCells, &cgeomFEM);CHKERRQ(ierr);}
1208338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1209338f77d5SMatthew G. Knepley   /* Read out geometry */
1210338f77d5SMatthew G. Knepley   if (useFEM) {
1211338f77d5SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
1212338f77d5SMatthew G. Knepley       const PetscInt c = cell - cStart;
1213338f77d5SMatthew G. Knepley 
1214338f77d5SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, NULL, cgeomFEM[c].v0, cgeomFEM[c].J, cgeomFEM[c].invJ, &cgeomFEM[c].detJ);CHKERRQ(ierr);
1215338f77d5SMatthew G. Knepley       if (cgeomFEM[c].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cgeomFEM[c].detJ, c);
1216338f77d5SMatthew G. Knepley     }
1217b5a3613cSMatthew G. Knepley   }
1218b5a3613cSMatthew G. Knepley   if (useFVM) {
1219338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1220b5a3613cSMatthew G. Knepley     Vec       grad;
1221b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1222b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1223b5a3613cSMatthew G. Knepley 
1224338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1225338f77d5SMatthew G. Knepley       PetscObject  obj;
1226338f77d5SMatthew G. Knepley       PetscClassId id;
1227338f77d5SMatthew G. Knepley 
1228338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1229338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1230338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1231338f77d5SMatthew G. Knepley     }
1232338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1233338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1234b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1235338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1236338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1237b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1238b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1239b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1240b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1241338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1242b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1243b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1244b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1245b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1246b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1247b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1248338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1249b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1250b5a3613cSMatthew G. Knepley   }
1251338f77d5SMatthew G. Knepley   /* Read out data from inputs */
125273d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
125373d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
125473d901b8SMatthew G. Knepley     PetscInt     i;
125573d901b8SMatthew G. Knepley 
1256338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
12570f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1258338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
125973d901b8SMatthew G. Knepley     if (dmAux) {
1260338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
12610f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1262338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
126373d901b8SMatthew G. Knepley     }
126473d901b8SMatthew G. Knepley   }
1265338f77d5SMatthew G. Knepley   /* Do integration for each field */
126673d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1267c1f031eeSMatthew G. Knepley     PetscObject  obj;
1268c1f031eeSMatthew G. Knepley     PetscClassId id;
1269c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
127073d901b8SMatthew G. Knepley 
1271c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1272c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1273c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1274c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1275c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1276c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1277c1f031eeSMatthew G. Knepley 
12780f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1279c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
12809c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1281c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1282c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
128373d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
12840f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
128573d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
128673d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
128773d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
128873d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1289338f77d5SMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeomFEM, u, probAux, a, cintegral);CHKERRQ(ierr);
1290338f77d5SMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1291c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1292c1f031eeSMatthew G. Knepley       PetscInt       foff;
1293420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1294b69edc29SMatthew G. Knepley       PetscScalar    lint;
1295c1f031eeSMatthew G. Knepley 
1296c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1297c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1298c1f031eeSMatthew G. Knepley       if (obj_func) {
1299c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1300b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1301b5a3613cSMatthew G. Knepley 
1302b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1303338f77d5SMatthew 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);
1304338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
130573d901b8SMatthew G. Knepley         }
1306c1f031eeSMatthew G. Knepley       }
1307c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1308c1f031eeSMatthew G. Knepley   }
1309338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1310b5a3613cSMatthew G. Knepley   if (useFVM) {
1311b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1312b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1313b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1314b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1315b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1316b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1317b5a3613cSMatthew G. Knepley   }
1318338f77d5SMatthew G. Knepley   if (useFEM) {ierr = PetscFree(cgeomFEM);CHKERRQ(ierr);}
131973d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1320338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1321338f77d5SMatthew G. Knepley   /* Cleanup */
1322338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1323338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1324338f77d5SMatthew G. Knepley }
1325338f77d5SMatthew G. Knepley 
1326338f77d5SMatthew G. Knepley /*@
1327338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1328338f77d5SMatthew G. Knepley 
1329338f77d5SMatthew G. Knepley   Input Parameters:
1330338f77d5SMatthew G. Knepley + dm - The mesh
1331338f77d5SMatthew G. Knepley . X  - Global input vector
1332338f77d5SMatthew G. Knepley - user - The user context
1333338f77d5SMatthew G. Knepley 
1334338f77d5SMatthew G. Knepley   Output Parameter:
1335338f77d5SMatthew G. Knepley . integral - Integral for each field
1336338f77d5SMatthew G. Knepley 
1337338f77d5SMatthew G. Knepley   Level: developer
1338338f77d5SMatthew G. Knepley 
1339338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1340338f77d5SMatthew G. Knepley @*/
1341b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1342338f77d5SMatthew G. Knepley {
1343338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1344b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1345338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1346338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1347338f77d5SMatthew G. Knepley 
1348338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1349338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1350338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1351338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1352338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1353338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1354338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1355338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1356338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1357338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1358338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1359338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1360338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1361338f77d5SMatthew G. Knepley   /* Sum up values */
1362338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1363338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1364338f77d5SMatthew G. Knepley 
1365338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1366b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
1367338f77d5SMatthew G. Knepley   }
1368b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
136973d901b8SMatthew G. Knepley   if (mesh->printFEM) {
1370338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
1371b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
137273d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
137373d901b8SMatthew G. Knepley   }
1374338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
1375338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1376338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1377338f77d5SMatthew G. Knepley }
1378338f77d5SMatthew G. Knepley 
1379338f77d5SMatthew G. Knepley /*@
1380338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
1381338f77d5SMatthew G. Knepley 
1382338f77d5SMatthew G. Knepley   Input Parameters:
1383338f77d5SMatthew G. Knepley + dm - The mesh
1384338f77d5SMatthew G. Knepley . X  - Global input vector
1385338f77d5SMatthew G. Knepley - user - The user context
1386338f77d5SMatthew G. Knepley 
1387338f77d5SMatthew G. Knepley   Output Parameter:
1388338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
1389338f77d5SMatthew G. Knepley 
1390338f77d5SMatthew G. Knepley   Level: developer
1391338f77d5SMatthew G. Knepley 
1392338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1393338f77d5SMatthew G. Knepley @*/
1394338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
1395338f77d5SMatthew G. Knepley {
1396338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1397338f77d5SMatthew G. Knepley   DM             dmF;
1398338f77d5SMatthew G. Knepley   PetscSection   sectionF;
1399338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
1400338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1401338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1402338f77d5SMatthew G. Knepley 
1403338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1404338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1405338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1406338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
1407338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1408338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1409338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1410338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1411338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1412338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1413338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1414338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1415338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1416338f77d5SMatthew G. Knepley   /* Put values in F*/
1417338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
1418338f77d5SMatthew G. Knepley   ierr = DMGetDefaultSection(dmF, &sectionF);CHKERRQ(ierr);
1419338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
1420338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1421338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1422338f77d5SMatthew G. Knepley     PetscInt       dof, off;
1423338f77d5SMatthew G. Knepley 
1424338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1425338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
1426338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
1427338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
1428338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
1429338f77d5SMatthew G. Knepley   }
1430338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
1431338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
1432c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
143373d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
143473d901b8SMatthew G. Knepley }
143573d901b8SMatthew G. Knepley 
1436d69c5d34SMatthew G. Knepley /*@
143768132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1438d69c5d34SMatthew G. Knepley 
1439d69c5d34SMatthew G. Knepley   Input Parameters:
1440d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1441d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1442d69c5d34SMatthew G. Knepley - user - The user context
1443d69c5d34SMatthew G. Knepley 
1444d69c5d34SMatthew G. Knepley   Output Parameter:
1445934789fcSMatthew G. Knepley . In  - The interpolation matrix
1446d69c5d34SMatthew G. Knepley 
1447d69c5d34SMatthew G. Knepley   Level: developer
1448d69c5d34SMatthew G. Knepley 
144968132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1450d69c5d34SMatthew G. Knepley @*/
145168132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
1452d69c5d34SMatthew G. Knepley {
1453d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1454d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
14552764a2aaSMatthew G. Knepley   PetscDS           prob;
1456d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
145797c42addSMatthew G. Knepley   PetscFV          *fvRef;
1458d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1459d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1460d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
14619ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
14620f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1463d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1464d69c5d34SMatthew G. Knepley 
1465d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1466d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1467c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1468d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1469d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1470d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1471d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1472d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1473d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
14749ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
14759ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
14762764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
147797c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1478d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
147997c42addSMatthew G. Knepley     PetscObject  obj;
148097c42addSMatthew G. Knepley     PetscClassId id;
1481aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1482d69c5d34SMatthew G. Knepley 
148397c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
148497c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
148597c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
148697c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
148797c42addSMatthew G. Knepley 
14880f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1489d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
14900f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
149197c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
149297c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
149397c42addSMatthew G. Knepley       PetscDualSpace Q;
149497c42addSMatthew G. Knepley 
149597c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
149697c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
149797c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
149897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
149997c42addSMatthew G. Knepley     }
15009c3cf19fSMatthew G. Knepley     rTotDim += rNb;
1501d69c5d34SMatthew G. Knepley   }
15022764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
15030f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
15040f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1505d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1506d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1507d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1508d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1509d69c5d34SMatthew G. Knepley     PetscReal       *points;
1510d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1511d69c5d34SMatthew G. Knepley 
1512d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
151397c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1514d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
15150f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
151697c42addSMatthew G. Knepley     } else {
151797c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
151897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
151997c42addSMatthew G. Knepley     }
1520d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1521d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1522d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
15239c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1524d69c5d34SMatthew G. Knepley       npoints += Np;
1525d69c5d34SMatthew G. Knepley     }
1526d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1527d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1528d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
15299c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1530d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1531d69c5d34SMatthew G. Knepley     }
1532d69c5d34SMatthew G. Knepley 
1533d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
153497c42addSMatthew G. Knepley       PetscObject  obj;
153597c42addSMatthew G. Knepley       PetscClassId id;
1536d69c5d34SMatthew G. Knepley       PetscReal   *B;
15379c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
1538d69c5d34SMatthew G. Knepley 
153997c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
154097c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
154197c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
154297c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1543d69c5d34SMatthew G. Knepley 
1544d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
15450f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
15460f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1547ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1548ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
15499c3cf19fSMatthew G. Knepley           if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", Nc, NcJ);
15500f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1551d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1552d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
15539c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
15549c3cf19fSMatthew G. Knepley             if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
1555d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
155636a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
1557*d172c84bSMatthew G. Knepley                 /*
1558*d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
1559*d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
1560*d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
1561*d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
1562*d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
1563*d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
1564*d172c84bSMatthew G. Knepley                 */
15659c3cf19fSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c];
156636a6d9c0SMatthew G. Knepley               }
1567d69c5d34SMatthew G. Knepley             }
1568d69c5d34SMatthew G. Knepley           }
15690f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1570ffe73a53SMatthew G. Knepley         }
157197c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
157297c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
157397c42addSMatthew G. Knepley 
157497c42addSMatthew G. Knepley         /* Evaluate constant function at points */
157597c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
157697c42addSMatthew G. Knepley         cpdim = 1;
157797c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
157897c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
157997c42addSMatthew G. Knepley           if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", Nc, NcJ);
158097c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
158197c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
15829c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
15839c3cf19fSMatthew G. Knepley             if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
158497c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
158597c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
15869c3cf19fSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p*qNc+c];
158797c42addSMatthew G. Knepley               }
158897c42addSMatthew G. Knepley             }
158997c42addSMatthew G. Knepley           }
159097c42addSMatthew G. Knepley         }
159197c42addSMatthew G. Knepley       }
1592*d172c84bSMatthew G. Knepley       offsetJ += cpdim;
1593d69c5d34SMatthew G. Knepley     }
1594*d172c84bSMatthew G. Knepley     offsetI += fpdim;
1595549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1596d69c5d34SMatthew G. Knepley   }
15970f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
15987f5b169aSMatthew G. Knepley   /* Preallocate matrix */
15997f5b169aSMatthew G. Knepley   {
1600c094ef40SMatthew G. Knepley     Mat          preallocator;
1601c094ef40SMatthew G. Knepley     PetscScalar *vals;
1602c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
1603c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
16047f5b169aSMatthew G. Knepley 
1605c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
1606c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
1607c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1608c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1609c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1610c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
16117f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
16127f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
1613c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
16147f5b169aSMatthew G. Knepley     }
1615c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
1616c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1617c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1618c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
1619c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
16207f5b169aSMatthew G. Knepley   }
16217f5b169aSMatthew G. Knepley   /* Fill matrix */
16227f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1623d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1624934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1625d69c5d34SMatthew G. Knepley   }
1626549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
162797c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1628549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1629934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1630934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1631d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1632d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1633934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1634934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1635d69c5d34SMatthew G. Knepley   }
1636d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1637d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1638d69c5d34SMatthew G. Knepley }
16396c73c22cSMatthew G. Knepley 
1640bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
1641bd041c0cSMatthew G. Knepley {
1642bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
1643bd041c0cSMatthew G. Knepley }
1644bd041c0cSMatthew G. Knepley 
164568132eb9SMatthew G. Knepley /*@
164668132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
164768132eb9SMatthew G. Knepley 
164868132eb9SMatthew G. Knepley   Input Parameters:
164968132eb9SMatthew G. Knepley + dmf  - The fine mesh
165068132eb9SMatthew G. Knepley . dmc  - The coarse mesh
165168132eb9SMatthew G. Knepley - user - The user context
165268132eb9SMatthew G. Knepley 
165368132eb9SMatthew G. Knepley   Output Parameter:
165468132eb9SMatthew G. Knepley . In  - The interpolation matrix
165568132eb9SMatthew G. Knepley 
165668132eb9SMatthew G. Knepley   Level: developer
165768132eb9SMatthew G. Knepley 
165868132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
165968132eb9SMatthew G. Knepley @*/
166068132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
16614ef9d792SMatthew G. Knepley {
166264e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
166364e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
16644ef9d792SMatthew G. Knepley   PetscDS        prob;
16654ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
16664ef9d792SMatthew G. Knepley   PetscHashJK    ht;
16674ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
16684ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
16694ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
16704ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
16714ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
16724ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
16734ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
16744ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
16754ef9d792SMatthew G. Knepley 
16764ef9d792SMatthew G. Knepley   PetscFunctionBegin;
167777711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
16784ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
16794ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
16804ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
16814ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
16824ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
16834ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
16844ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
16854ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
16864ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
16874ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
16884ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
16894ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
16909c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
16914ef9d792SMatthew G. Knepley 
16924ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
16934ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
16944ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
16954ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
16964ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
16974ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
16984ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
16994ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
17004ef9d792SMatthew G. Knepley   ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
17014ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
17024ef9d792SMatthew G. Knepley     PetscObject      obj;
17034ef9d792SMatthew G. Knepley     PetscClassId     id;
1704c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
17054ef9d792SMatthew G. Knepley     PetscQuadrature  f;
170617f047d8SMatthew G. Knepley     const PetscReal *qpoints;
170717f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
17084ef9d792SMatthew G. Knepley 
17094ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
17104ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
17114ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
17124ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
17134ef9d792SMatthew G. Knepley 
17144ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
17154ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
17164ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
17174ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
17184ef9d792SMatthew G. Knepley 
17194ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
17204ef9d792SMatthew G. Knepley       Nc   = 1;
17214ef9d792SMatthew G. Knepley     }
17224ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
17234ef9d792SMatthew G. Knepley     /* For each fine grid cell */
17244ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
17254ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
17264ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
17274ef9d792SMatthew G. Knepley 
17286ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
17294ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
17309c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
17314ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
17324ef9d792SMatthew G. Knepley         Vec             pointVec;
17334ef9d792SMatthew G. Knepley         PetscScalar    *pV;
17343a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
17353a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
17369c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
17374ef9d792SMatthew G. Knepley 
17384ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
17394ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
17409c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
17414ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
17424ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
17434ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
17444ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
17454ef9d792SMatthew G. Knepley           /* Transform point to real space */
17464ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
17474ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
17484ef9d792SMatthew G. Knepley         }
17494ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
17504ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
17511555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
175262a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
17533a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
17544ef9d792SMatthew G. Knepley         /* Update preallocation info */
17553a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
17563a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
17579c3cf19fSMatthew G. Knepley         {
17584ef9d792SMatthew G. Knepley           PetscHashJKKey  key;
17594ef9d792SMatthew G. Knepley           PetscHashJKIter missing, iter;
17604ef9d792SMatthew G. Knepley 
17619c3cf19fSMatthew G. Knepley           key.j = findices[i];
17628c543595SMatthew G. Knepley           if (key.j >= 0) {
17634ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
17644ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
17653a93e3b7SToby Isaac               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
17664ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
17674ef9d792SMatthew G. Knepley                 key.k = cindices[c];
17684ef9d792SMatthew G. Knepley                 if (key.k < 0) continue;
17694ef9d792SMatthew G. Knepley                 ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
17704ef9d792SMatthew G. Knepley                 if (missing) {
17714ef9d792SMatthew G. Knepley                   ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
17724ef9d792SMatthew G. Knepley                   if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
17734ef9d792SMatthew G. Knepley                   else                                     ++onz[key.j-rStart];
17744ef9d792SMatthew G. Knepley                 }
17754ef9d792SMatthew G. Knepley               }
17763a93e3b7SToby Isaac               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
17774ef9d792SMatthew G. Knepley             }
17784ef9d792SMatthew G. Knepley           }
17798c543595SMatthew G. Knepley         }
17803a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
17814ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
17824ef9d792SMatthew G. Knepley       }
178346bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
17844ef9d792SMatthew G. Knepley     }
17854ef9d792SMatthew G. Knepley   }
17864ef9d792SMatthew G. Knepley   ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
17874ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
17884ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
17894ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
17904ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
17914ef9d792SMatthew G. Knepley     PetscObject      obj;
17924ef9d792SMatthew G. Knepley     PetscClassId     id;
1793c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
17944ef9d792SMatthew G. Knepley     PetscQuadrature  f;
17954ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
17969c3cf19fSMatthew G. Knepley     PetscInt         Nc, qNc, Np, fpdim, i, d;
17974ef9d792SMatthew G. Knepley 
17984ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
17994ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
18004ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
18014ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
18024ef9d792SMatthew G. Knepley 
18034ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
18044ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
18054ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
18064ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
18074ef9d792SMatthew G. Knepley 
18084ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
18094ef9d792SMatthew G. Knepley       Nc   = 1;
18104ef9d792SMatthew G. Knepley     }
18114ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
18124ef9d792SMatthew G. Knepley     /* For each fine grid cell */
18134ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
18144ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
18154ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
18164ef9d792SMatthew G. Knepley 
18176ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
18184ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
18199c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
18204ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
18214ef9d792SMatthew G. Knepley         Vec             pointVec;
18224ef9d792SMatthew G. Knepley         PetscScalar    *pV;
182312111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
18243a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
182517f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
18264ef9d792SMatthew G. Knepley 
18274ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
18284ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
18299c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
18309c3cf19fSMatthew G. Knepley         if (qNc != Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, Nc);
18314ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
18324ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
18334ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
18344ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
18354ef9d792SMatthew G. Knepley           /* Transform point to real space */
18364ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
18374ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
18384ef9d792SMatthew G. Knepley         }
18394ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
18404ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
18411555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
184262a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
18434ef9d792SMatthew G. Knepley         /* Update preallocation info */
18443a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
18453a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
18464ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
18474ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
1848826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
1849826eb36dSMatthew G. Knepley 
18503a93e3b7SToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
18514ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
18523a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
1853e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
1854826eb36dSMatthew G. Knepley           CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x);
18554ef9d792SMatthew G. Knepley 
18564ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
18574ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
18584ef9d792SMatthew G. Knepley             PetscReal *B;
18594ef9d792SMatthew G. Knepley 
18604ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
18614ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
18624ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
18639c3cf19fSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
18644ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
18654ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
18669c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c];
18674ef9d792SMatthew G. Knepley             }
18684ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
18694ef9d792SMatthew G. Knepley           } else {
18704ef9d792SMatthew G. Knepley             cpdim = 1;
18714ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
18729c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
18734ef9d792SMatthew G. Knepley             }
18744ef9d792SMatthew G. Knepley           }
18754ef9d792SMatthew G. Knepley           /* Update interpolator */
18769c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
18779c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
18789c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
18793a93e3b7SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
18804ef9d792SMatthew G. Knepley         }
18814ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
18823a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
18834ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
18844ef9d792SMatthew G. Knepley       }
188546bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
18864ef9d792SMatthew G. Knepley     }
18874ef9d792SMatthew G. Knepley   }
18884ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
18894ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
18904ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
18914ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18924ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
189377711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
18944ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
18954ef9d792SMatthew G. Knepley }
18964ef9d792SMatthew G. Knepley 
189746fa42a0SMatthew G. Knepley /*@
1898bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
1899bd041c0cSMatthew G. Knepley 
1900bd041c0cSMatthew G. Knepley   Input Parameters:
1901bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
1902bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
1903bd041c0cSMatthew G. Knepley - user - The user context
1904bd041c0cSMatthew G. Knepley 
1905bd041c0cSMatthew G. Knepley   Output Parameter:
1906bd041c0cSMatthew G. Knepley . mass  - The mass matrix
1907bd041c0cSMatthew G. Knepley 
1908bd041c0cSMatthew G. Knepley   Level: developer
1909bd041c0cSMatthew G. Knepley 
1910bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1911bd041c0cSMatthew G. Knepley @*/
1912bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
1913bd041c0cSMatthew G. Knepley {
1914bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
1915bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
1916bd041c0cSMatthew G. Knepley   PetscDS        prob;
1917bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
1918bd041c0cSMatthew G. Knepley   PetscHashJK    ht;
1919bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
1920bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
1921bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
1922bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
1923bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
1924bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
1925bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
1926bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1927bd041c0cSMatthew G. Knepley 
1928bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1929bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
1930bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
1931bd041c0cSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
1932bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1933bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
1934bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
1935bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1936bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
1937bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1938bd041c0cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
1939bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
1940bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1941bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
1942bd041c0cSMatthew G. Knepley 
1943bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
1944bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
1945bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
1946bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
1947bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
1948bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
1949bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
1950bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
1951bd041c0cSMatthew G. Knepley   ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
1952bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
1953bd041c0cSMatthew G. Knepley     PetscObject      obj;
1954bd041c0cSMatthew G. Knepley     PetscClassId     id;
1955bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
1956bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
1957bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
1958bd041c0cSMatthew G. Knepley 
1959bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
1960bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1961bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
1962bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
1963bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
1964bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
1965bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
1966bd041c0cSMatthew G. Knepley       Vec                pointVec;
1967bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
1968bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
1969bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
1970bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
1971bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
1972bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
1973bd041c0cSMatthew G. Knepley 
1974bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
1975bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
1976bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
1977bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
1978bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
1979bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
1980bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
1981bd041c0cSMatthew G. Knepley         /* Transform point to real space */
1982bd041c0cSMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
1983bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
1984bd041c0cSMatthew G. Knepley       }
1985bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
1986bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
1987bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
1988bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
1989bd041c0cSMatthew G. Knepley       /* Update preallocation info */
1990bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
1991bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
1992bd041c0cSMatthew G. Knepley       {
1993bd041c0cSMatthew G. Knepley         PetscHashJKKey  key;
1994bd041c0cSMatthew G. Knepley         PetscHashJKIter missing, iter;
1995bd041c0cSMatthew G. Knepley 
1996bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
1997bd041c0cSMatthew G. Knepley           key.j = findices[i];
1998bd041c0cSMatthew G. Knepley           if (key.j >= 0) {
1999bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2000bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2001bd041c0cSMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2002bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2003bd041c0cSMatthew G. Knepley                 key.k = cindices[c];
2004bd041c0cSMatthew G. Knepley                 if (key.k < 0) continue;
2005bd041c0cSMatthew G. Knepley                 ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
2006bd041c0cSMatthew G. Knepley                 if (missing) {
2007bd041c0cSMatthew G. Knepley                   ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
2008bd041c0cSMatthew G. Knepley                   if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
2009bd041c0cSMatthew G. Knepley                   else                                     ++onz[key.j-rStart];
2010bd041c0cSMatthew G. Knepley                 }
2011bd041c0cSMatthew G. Knepley               }
2012bd041c0cSMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2013bd041c0cSMatthew G. Knepley             }
2014bd041c0cSMatthew G. Knepley           }
2015bd041c0cSMatthew G. Knepley         }
2016bd041c0cSMatthew G. Knepley       }
2017bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2018bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2019bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2020bd041c0cSMatthew G. Knepley     }
2021bd041c0cSMatthew G. Knepley   }
2022bd041c0cSMatthew G. Knepley   ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
2023bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2024bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2025bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2026bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2027bd041c0cSMatthew G. Knepley     PetscObject      obj;
2028bd041c0cSMatthew G. Knepley     PetscClassId     id;
2029bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2030bd041c0cSMatthew G. Knepley     PetscReal       *Bfine;
2031bd041c0cSMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2032bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2033bd041c0cSMatthew G. Knepley 
2034bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2035bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2036bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);}
2037bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2038bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2039bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2040bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2041bd041c0cSMatthew G. Knepley       Vec                pointVec;
2042bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2043bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2044bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2045bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2046bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2047bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2048bd041c0cSMatthew G. Knepley 
2049bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2050bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2051bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2052bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2053bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2054bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2055bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2056bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2057bd041c0cSMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
2058bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2059bd041c0cSMatthew G. Knepley       }
2060bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2061bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2062bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2063bd041c0cSMatthew G. Knepley       /* Update matrix */
2064bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2065bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2066bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2067bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2068bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2069bd041c0cSMatthew G. Knepley 
2070bd041c0cSMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2071bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2072bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2073bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2074bd041c0cSMatthew G. Knepley         CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x);
2075bd041c0cSMatthew G. Knepley 
2076bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2077bd041c0cSMatthew G. Knepley           PetscFE    fe = (PetscFE) obj;
2078bd041c0cSMatthew G. Knepley           PetscReal *B;
2079bd041c0cSMatthew G. Knepley 
2080bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2081bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2082bd041c0cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2083bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2084bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2085bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2086bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2087bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*Bfine[(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ;
2088bd041c0cSMatthew G. Knepley             }
2089bd041c0cSMatthew G. Knepley             /* Update interpolator */
2090bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2091bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2092bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2093bd041c0cSMatthew G. Knepley           }
2094bd041c0cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2095bd041c0cSMatthew G. Knepley         } else {
2096bd041c0cSMatthew G. Knepley           cpdim = 1;
2097bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2098bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2099bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2100bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2101bd041c0cSMatthew G. Knepley             }
2102bd041c0cSMatthew G. Knepley             /* Update interpolator */
2103bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2104bd041c0cSMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %d %d Nf: %d %d Nc: %d %d\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
2105bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2106bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2107bd041c0cSMatthew G. Knepley           }
2108bd041c0cSMatthew G. Knepley         }
2109bd041c0cSMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2110bd041c0cSMatthew G. Knepley       }
2111bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2112bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2113bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2114bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2115bd041c0cSMatthew G. Knepley     }
2116bd041c0cSMatthew G. Knepley   }
2117bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
2118bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
2119bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2120bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2121bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2122bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
2123bd041c0cSMatthew G. Knepley }
2124bd041c0cSMatthew G. Knepley 
2125bd041c0cSMatthew G. Knepley /*@
212646fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
212746fa42a0SMatthew G. Knepley 
212846fa42a0SMatthew G. Knepley   Input Parameters:
212946fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
213046fa42a0SMatthew G. Knepley - dmf  - The fine mesh
213146fa42a0SMatthew G. Knepley - user - The user context
213246fa42a0SMatthew G. Knepley 
213346fa42a0SMatthew G. Knepley   Output Parameter:
213446fa42a0SMatthew G. Knepley . sc   - The mapping
213546fa42a0SMatthew G. Knepley 
213646fa42a0SMatthew G. Knepley   Level: developer
213746fa42a0SMatthew G. Knepley 
213846fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
213946fa42a0SMatthew G. Knepley @*/
21407c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
21417c927364SMatthew G. Knepley {
2142e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
21437c927364SMatthew G. Knepley   PetscFE       *feRef;
214497c42addSMatthew G. Knepley   PetscFV       *fvRef;
21457c927364SMatthew G. Knepley   Vec            fv, cv;
21467c927364SMatthew G. Knepley   IS             fis, cis;
21477c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
21487c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
21490bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
21507c927364SMatthew G. Knepley   PetscErrorCode ierr;
21517c927364SMatthew G. Knepley 
21527c927364SMatthew G. Knepley   PetscFunctionBegin;
215375a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2154c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
21557c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
21567c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
21577c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
21587c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
21597c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
21607c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
21619ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
21629ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2163e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
216497c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
21657c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
216697c42addSMatthew G. Knepley     PetscObject  obj;
216797c42addSMatthew G. Knepley     PetscClassId id;
2168aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
21697c927364SMatthew G. Knepley 
217097c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
217197c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
217297c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
217397c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
217497c42addSMatthew G. Knepley 
21757c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
21767c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
21777c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
217897c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
217997c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
218097c42addSMatthew G. Knepley       PetscDualSpace Q;
218197c42addSMatthew G. Knepley 
218297c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
218397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
218497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
218597c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
218697c42addSMatthew G. Knepley     }
2187*d172c84bSMatthew G. Knepley     fTotDim += fNb;
21887c927364SMatthew G. Knepley   }
2189e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
21907c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
21917c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
21927c927364SMatthew G. Knepley     PetscFE        feC;
219397c42addSMatthew G. Knepley     PetscFV        fvC;
21947c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
2195*d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
21967c927364SMatthew G. Knepley 
219797c42addSMatthew G. Knepley     if (feRef[field]) {
2198e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
21997c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
22007c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
22017c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
2202*d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
22037c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
22047c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
22057c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
220697c42addSMatthew G. Knepley     } else {
220797c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
220897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
220997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
221097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
221197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
221297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
221397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
221497c42addSMatthew G. Knepley     }
221597c42addSMatthew G. Knepley     if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC);
22167c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
22177c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
2218*d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
2219*d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
222097c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
22217c927364SMatthew G. Knepley 
22227c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
2223*d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
2224*d172c84bSMatthew G. Knepley       if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcC, NcC);
222597c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
22267c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
22277c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
2228*d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
22297c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
2230*d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
22317c927364SMatthew G. Knepley 
22327c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
2233*d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
2234*d172c84bSMatthew G. Knepley         if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcF, NcF);
22357c927364SMatthew G. Knepley         if (NpC != NpF) continue;
22367c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
22377c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
2238*d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
2239*d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
2240*d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
224197c42addSMatthew G. Knepley         found = PETSC_TRUE;
22427c927364SMatthew G. Knepley         break;
22437c927364SMatthew G. Knepley       }
224497c42addSMatthew G. Knepley       if (!found) {
224597c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
2246*d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
2247*d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
224897c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
224997c42addSMatthew G. Knepley       }
22507c927364SMatthew G. Knepley     }
2251*d172c84bSMatthew G. Knepley     offsetC += cpdim;
2252*d172c84bSMatthew G. Knepley     offsetF += fpdim;
22537c927364SMatthew G. Knepley   }
225497c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
225597c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
22567c927364SMatthew G. Knepley 
22577c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
22587c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
22590bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
22607c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
22617c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
2262aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
2263aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
22647c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
22657c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
22667c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
22677c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
22680bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
22697c927364SMatthew G. Knepley       if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]);
22707c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
22717c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
22727c927364SMatthew G. Knepley     }
22737c927364SMatthew G. Knepley   }
22747c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
22757c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
22767c927364SMatthew G. Knepley 
22777c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
22787c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
22797c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
22807c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
22817c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
22827c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
22837c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
228475a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2285cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2286cb1e1211SMatthew G Knepley }
2287