xref: /petsc/src/dm/impls/plex/plexfem.c (revision ca3eba1ba7c328720091e5ba7905e32fa587b1d4)
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 
7cb1e1211SMatthew G Knepley #undef __FUNCT__
8cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale"
9cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
10cb1e1211SMatthew G Knepley {
11cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
12cb1e1211SMatthew G Knepley 
13cb1e1211SMatthew G Knepley   PetscFunctionBegin;
14cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
15cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
16cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
17cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
18cb1e1211SMatthew G Knepley }
19cb1e1211SMatthew G Knepley 
20cb1e1211SMatthew G Knepley #undef __FUNCT__
21cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale"
22cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
23cb1e1211SMatthew G Knepley {
24cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
25cb1e1211SMatthew G Knepley 
26cb1e1211SMatthew G Knepley   PetscFunctionBegin;
27cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
29cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
30cb1e1211SMatthew G Knepley }
31cb1e1211SMatthew G Knepley 
32cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k)
33cb1e1211SMatthew G Knepley {
34cb1e1211SMatthew G Knepley   switch (i) {
35cb1e1211SMatthew G Knepley   case 0:
36cb1e1211SMatthew G Knepley     switch (j) {
37cb1e1211SMatthew G Knepley     case 0: return 0;
38cb1e1211SMatthew G Knepley     case 1:
39cb1e1211SMatthew G Knepley       switch (k) {
40cb1e1211SMatthew G Knepley       case 0: return 0;
41cb1e1211SMatthew G Knepley       case 1: return 0;
42cb1e1211SMatthew G Knepley       case 2: return 1;
43cb1e1211SMatthew G Knepley       }
44cb1e1211SMatthew G Knepley     case 2:
45cb1e1211SMatthew G Knepley       switch (k) {
46cb1e1211SMatthew G Knepley       case 0: return 0;
47cb1e1211SMatthew G Knepley       case 1: return -1;
48cb1e1211SMatthew G Knepley       case 2: return 0;
49cb1e1211SMatthew G Knepley       }
50cb1e1211SMatthew G Knepley     }
51cb1e1211SMatthew G Knepley   case 1:
52cb1e1211SMatthew G Knepley     switch (j) {
53cb1e1211SMatthew G Knepley     case 0:
54cb1e1211SMatthew G Knepley       switch (k) {
55cb1e1211SMatthew G Knepley       case 0: return 0;
56cb1e1211SMatthew G Knepley       case 1: return 0;
57cb1e1211SMatthew G Knepley       case 2: return -1;
58cb1e1211SMatthew G Knepley       }
59cb1e1211SMatthew G Knepley     case 1: return 0;
60cb1e1211SMatthew G Knepley     case 2:
61cb1e1211SMatthew G Knepley       switch (k) {
62cb1e1211SMatthew G Knepley       case 0: return 1;
63cb1e1211SMatthew G Knepley       case 1: return 0;
64cb1e1211SMatthew G Knepley       case 2: return 0;
65cb1e1211SMatthew G Knepley       }
66cb1e1211SMatthew G Knepley     }
67cb1e1211SMatthew G Knepley   case 2:
68cb1e1211SMatthew G Knepley     switch (j) {
69cb1e1211SMatthew G Knepley     case 0:
70cb1e1211SMatthew G Knepley       switch (k) {
71cb1e1211SMatthew G Knepley       case 0: return 0;
72cb1e1211SMatthew G Knepley       case 1: return 1;
73cb1e1211SMatthew G Knepley       case 2: return 0;
74cb1e1211SMatthew G Knepley       }
75cb1e1211SMatthew G Knepley     case 1:
76cb1e1211SMatthew G Knepley       switch (k) {
77cb1e1211SMatthew G Knepley       case 0: return -1;
78cb1e1211SMatthew G Knepley       case 1: return 0;
79cb1e1211SMatthew G Knepley       case 2: return 0;
80cb1e1211SMatthew G Knepley       }
81cb1e1211SMatthew G Knepley     case 2: return 0;
82cb1e1211SMatthew G Knepley     }
83cb1e1211SMatthew G Knepley   }
84cb1e1211SMatthew G Knepley   return 0;
85cb1e1211SMatthew G Knepley }
86cb1e1211SMatthew G Knepley 
87cb1e1211SMatthew G Knepley #undef __FUNCT__
88026175e5SToby Isaac #define __FUNCT__ "DMPlexProjectRigidBody"
890163fd50SMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx)
90026175e5SToby Isaac {
91026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
92ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
93026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
94026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
95026175e5SToby Isaac 
96ad917190SMatthew G. Knepley   PetscFunctionBegin;
97ad917190SMatthew G. Knepley   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2);
98026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
99026175e5SToby Isaac   if (d < dim) {
100026175e5SToby Isaac     mode[d] = 1.;
101ad917190SMatthew G. Knepley   } else {
102026175e5SToby Isaac     for (i = 0; i < dim; i++) {
103026175e5SToby Isaac       for (j = 0; j < dim; j++) {
104026175e5SToby Isaac         mode[j] += epsilon(i, j, k)*X[i];
105026175e5SToby Isaac       }
106026175e5SToby Isaac     }
107026175e5SToby Isaac   }
108ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
109026175e5SToby Isaac }
110026175e5SToby Isaac 
111026175e5SToby Isaac #undef __FUNCT__
112cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody"
113cb1e1211SMatthew G Knepley /*@C
114026175e5SToby Isaac   DMPlexCreateRigidBody - for the default global section, create rigid body modes from coordinates
115cb1e1211SMatthew G Knepley 
116cb1e1211SMatthew G Knepley   Collective on DM
117cb1e1211SMatthew G Knepley 
118cb1e1211SMatthew G Knepley   Input Arguments:
119026175e5SToby Isaac . dm - the DM
120cb1e1211SMatthew G Knepley 
121cb1e1211SMatthew G Knepley   Output Argument:
122cb1e1211SMatthew G Knepley . sp - the null space
123cb1e1211SMatthew G Knepley 
124cb1e1211SMatthew G Knepley   Note: This is necessary to take account of Dirichlet conditions on the displacements
125cb1e1211SMatthew G Knepley 
126cb1e1211SMatthew G Knepley   Level: advanced
127cb1e1211SMatthew G Knepley 
128cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate()
129cb1e1211SMatthew G Knepley @*/
130026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp)
131cb1e1211SMatthew G Knepley {
132cb1e1211SMatthew G Knepley   MPI_Comm       comm;
133026175e5SToby Isaac   Vec            mode[6];
134026175e5SToby Isaac   PetscSection   section, globalSection;
1359d8fbdeaSMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, d, i, j;
136cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
137cb1e1211SMatthew G Knepley 
138cb1e1211SMatthew G Knepley   PetscFunctionBegin;
139cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
140c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1419d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
142cb1e1211SMatthew G Knepley   if (dim == 1) {
143cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
144cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
145cb1e1211SMatthew G Knepley   }
146026175e5SToby Isaac   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
147026175e5SToby Isaac   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
148cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
149cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
150cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
151cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
152cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
153cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
154026175e5SToby Isaac   for (d = 0; d < m; d++) {
155330485fdSToby Isaac     PetscInt ctx[2];
156026175e5SToby Isaac     void    *voidctx = (void *) (&ctx[0]);
1570163fd50SMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody;
158cb1e1211SMatthew G Knepley 
1599d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
160330485fdSToby Isaac     ctx[1] = d;
1610709b2feSToby Isaac     ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
162cb1e1211SMatthew G Knepley   }
163cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
164cb1e1211SMatthew G Knepley   /* Orthonormalize system */
165cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
166cb1e1211SMatthew G Knepley     PetscScalar dots[6];
167cb1e1211SMatthew G Knepley 
168cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
169cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
170cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
171cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
172cb1e1211SMatthew G Knepley   }
173cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
174cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
175cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
176cb1e1211SMatthew G Knepley }
177cb1e1211SMatthew G Knepley 
178cb1e1211SMatthew G Knepley #undef __FUNCT__
179b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight"
180b29cfa1cSToby Isaac /*@
181b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
182b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
183b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
184b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
185b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
186b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
187b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
188b29cfa1cSToby Isaac 
189b29cfa1cSToby Isaac   Input Parameters:
190b29cfa1cSToby Isaac + dm - the DMPlex object
191b29cfa1cSToby Isaac - height - the maximum projection height >= 0
192b29cfa1cSToby Isaac 
193b29cfa1cSToby Isaac   Level: advanced
194b29cfa1cSToby Isaac 
1954d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
196b29cfa1cSToby Isaac @*/
197b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
198b29cfa1cSToby Isaac {
199b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
200b29cfa1cSToby Isaac 
201b29cfa1cSToby Isaac   PetscFunctionBegin;
202b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
203b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
204b29cfa1cSToby Isaac   PetscFunctionReturn(0);
205b29cfa1cSToby Isaac }
206b29cfa1cSToby Isaac 
207b29cfa1cSToby Isaac #undef __FUNCT__
208b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight"
209b29cfa1cSToby Isaac /*@
210b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
211b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
212b29cfa1cSToby Isaac 
213b29cfa1cSToby Isaac   Input Parameters:
214b29cfa1cSToby Isaac . dm - the DMPlex object
215b29cfa1cSToby Isaac 
216b29cfa1cSToby Isaac   Output Parameters:
217b29cfa1cSToby Isaac . height - the maximum projection height
218b29cfa1cSToby Isaac 
219b29cfa1cSToby Isaac   Level: intermediate
220b29cfa1cSToby Isaac 
2214d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
222b29cfa1cSToby Isaac @*/
223b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
224b29cfa1cSToby Isaac {
225b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
226b29cfa1cSToby Isaac 
227b29cfa1cSToby Isaac   PetscFunctionBegin;
228b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
229b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
230b29cfa1cSToby Isaac   PetscFunctionReturn(0);
231b29cfa1cSToby Isaac }
232b29cfa1cSToby Isaac 
233b29cfa1cSToby Isaac #undef __FUNCT__
2340709b2feSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal_Plex"
2350709b2feSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
236a18a7fb9SMatthew G. Knepley {
2377d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
238dda36da7SMatthew G. Knepley   PetscInt       *numComp;
239a18a7fb9SMatthew G. Knepley   PetscSection    section;
240a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
241ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
242dda36da7SMatthew G. Knepley   PetscInt        numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h;
243a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
244a18a7fb9SMatthew G. Knepley 
245a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
2469ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2479ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2489ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2499ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
250c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2517a1a1bd4SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
252a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
253a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
254dda36da7SMatthew G. Knepley   ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr);
2557d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
2567d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
2579ac3fadcSMatthew G. Knepley   if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);}
2589ac3fadcSMatthew G. Knepley   else               {cellsp = sp;}
2597d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
2607d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
261fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
2627d1dd11eSToby Isaac     if (pEnd <= pStart) continue;
2637d1dd11eSToby Isaac     totDim = 0;
264a18a7fb9SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
2659ac3fadcSMatthew G. Knepley       PetscObject  obj;
2669ac3fadcSMatthew G. Knepley       PetscClassId id;
2679ac3fadcSMatthew G. Knepley 
2689ac3fadcSMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2699ac3fadcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2709ac3fadcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
2719ac3fadcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
2729ac3fadcSMatthew G. Knepley 
273dda36da7SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
2747d1dd11eSToby Isaac         if (!h) {
275ee2838f6SToby Isaac           ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
2767d1dd11eSToby Isaac           sp[f] = cellsp[f];
2779ac3fadcSMatthew G. Knepley           ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
2789ac3fadcSMatthew G. Knepley         } else {
2797d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
2807d1dd11eSToby Isaac           if (!sp[f]) continue;
2817d1dd11eSToby Isaac         }
2829ac3fadcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
2839ac3fadcSMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
2849ac3fadcSMatthew G. Knepley 
285dda36da7SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
286302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
287e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
2889ac3fadcSMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
289a18a7fb9SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
290dda36da7SMatthew G. Knepley       totDim += spDim*numComp[f];
291a18a7fb9SMatthew G. Knepley     }
2927d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
2937d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
294d374af2bSToby Isaac     if (!totDim) continue;
295a18a7fb9SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
296ad96f515SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
297ee2838f6SToby Isaac     for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE;
298a18a7fb9SMatthew G. Knepley     for (i = 0; i < numIds; ++i) {
299a18a7fb9SMatthew G. Knepley       IS              pointIS;
300a18a7fb9SMatthew G. Knepley       const PetscInt *points;
301a18a7fb9SMatthew G. Knepley       PetscInt        n, p;
302a18a7fb9SMatthew G. Knepley 
303a18a7fb9SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
304a18a7fb9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
305a18a7fb9SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
306a18a7fb9SMatthew G. Knepley       for (p = 0; p < n; ++p) {
307a18a7fb9SMatthew G. Knepley         const PetscInt    point = points[p];
308e1d0b1adSMatthew G. Knepley         PetscFECellGeom   geom;
309a18a7fb9SMatthew G. Knepley 
3107d1dd11eSToby Isaac         if ((point < pStart) || (point >= pEnd)) continue;
311e1d0b1adSMatthew G. Knepley         ierr          = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
312ee2838f6SToby Isaac         geom.dim      = dim - h;
3137a1a1bd4SToby Isaac         geom.dimEmbed = dimEmbed;
314a18a7fb9SMatthew G. Knepley         for (f = 0, v = 0; f < numFields; ++f) {
315a18a7fb9SMatthew G. Knepley           void * const ctx = ctxs ? ctxs[f] : NULL;
316bf3434eeSMatthew G. Knepley 
3177d1dd11eSToby Isaac           if (!sp[f]) continue;
318a18a7fb9SMatthew G. Knepley           ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
319a18a7fb9SMatthew G. Knepley           for (d = 0; d < spDim; ++d) {
320a18a7fb9SMatthew G. Knepley             if (funcs[f]) {
3210163fd50SMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, time, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
322a18a7fb9SMatthew G. Knepley             } else {
323dda36da7SMatthew G. Knepley               for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
324a18a7fb9SMatthew G. Knepley             }
325dda36da7SMatthew G. Knepley             v += numComp[f];
326a18a7fb9SMatthew G. Knepley           }
327a18a7fb9SMatthew G. Knepley         }
328ad96f515SMatthew G. Knepley         ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
329a18a7fb9SMatthew G. Knepley       }
330a18a7fb9SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
331a18a7fb9SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
332a18a7fb9SMatthew G. Knepley     }
3337d1dd11eSToby Isaac     if (h) {
3347d1dd11eSToby Isaac       for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
3357d1dd11eSToby Isaac     }
3367d1dd11eSToby Isaac   }
337a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
338ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
3399ac3fadcSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
340dda36da7SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
3417d1dd11eSToby Isaac   if (maxHeight > 0) {
3427d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
3437d1dd11eSToby Isaac   }
344a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
345a18a7fb9SMatthew G. Knepley }
346a18a7fb9SMatthew G. Knepley 
347a18a7fb9SMatthew G. Knepley #undef __FUNCT__
3480709b2feSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal_Plex"
3490709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
350cb1e1211SMatthew G Knepley {
3517d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
35215496722SMatthew G. Knepley   PetscInt       *numComp;
35372f94c41SMatthew G. Knepley   PetscSection    section;
35472f94c41SMatthew G. Knepley   PetscScalar    *values;
355b793a5ffSMatthew G. Knepley   PetscInt        Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight;
356d0ed5973SMatthew G. Knepley   PetscBool      *isFE, hasFE = PETSC_FALSE, hasFV = PETSC_FALSE;
357cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
358cb1e1211SMatthew G Knepley 
359cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3609ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3619ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
3629ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
363cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
364b793a5ffSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
365d0ed5973SMatthew G. Knepley   ierr = PetscMalloc3(Nf, &isFE, Nf, &sp, Nf, &numComp);CHKERRQ(ierr);
3667d1dd11eSToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
367ee2838f6SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3687d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
3697d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
3707d1dd11eSToby Isaac   if (maxHeight > 0) {
371b793a5ffSMatthew G. Knepley     ierr = PetscMalloc1(Nf,&cellsp);CHKERRQ(ierr);
3727d1dd11eSToby Isaac   }
373048b7b1eSToby Isaac   else {
374048b7b1eSToby Isaac     cellsp = sp;
375048b7b1eSToby Isaac   }
376b793a5ffSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
37715496722SMatthew G. Knepley     PetscObject  obj;
37815496722SMatthew G. Knepley     PetscClassId id;
3790f2d7e86SMatthew G. Knepley 
38015496722SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
38115496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
38215496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
38315496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
38415496722SMatthew G. Knepley 
385d0ed5973SMatthew G. Knepley       hasFE   = PETSC_TRUE;
386d0ed5973SMatthew G. Knepley       isFE[f] = PETSC_TRUE;
38715496722SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
3887d1dd11eSToby Isaac       ierr  = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
38915496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
39015496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
39115496722SMatthew G. Knepley 
392d0ed5973SMatthew G. Knepley       hasFV   = PETSC_TRUE;
393d0ed5973SMatthew G. Knepley       isFE[f] = PETSC_FALSE;
394976fa17dSToby Isaac       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
395976fa17dSToby Isaac       ierr = PetscFVGetDualSpace(fv, &cellsp[f]);CHKERRQ(ierr);
39615496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
397db4c39b1SToby Isaac   }
398db4c39b1SToby Isaac   for (h = 0; h <= maxHeight; h++) {
399db4c39b1SToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
400db4c39b1SToby Isaac     if (!h) {pStart = cStart; pEnd = cEnd;}
401db4c39b1SToby Isaac     if (pEnd <= pStart) continue;
402db4c39b1SToby Isaac     totDim = 0;
403db4c39b1SToby Isaac     for (f = 0; f < Nf; ++f) {
404db4c39b1SToby Isaac       if (!h) {
405db4c39b1SToby Isaac         sp[f] = cellsp[f];
406db4c39b1SToby Isaac       }
407db4c39b1SToby Isaac       else {
408db4c39b1SToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
409db4c39b1SToby Isaac         if (!sp[f]) {
410db4c39b1SToby Isaac           continue;
411db4c39b1SToby Isaac         }
412db4c39b1SToby Isaac       }
41372f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
41415496722SMatthew G. Knepley       totDim += spDim*numComp[f];
415cb1e1211SMatthew G Knepley     }
4167d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
4177d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
418d374af2bSToby Isaac     if (!totDim) continue;
41972f94c41SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4207d1dd11eSToby Isaac     for (p = pStart; p < pEnd; ++p) {
421d0ed5973SMatthew G. Knepley       PetscFECellGeom fegeom;
422d0ed5973SMatthew G. Knepley       PetscFVCellGeom fvgeom;
423cb1e1211SMatthew G Knepley 
424d0ed5973SMatthew G. Knepley       if (hasFE) {
425d0ed5973SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, fegeom.v0, fegeom.J, NULL, &fegeom.detJ);CHKERRQ(ierr);
426d0ed5973SMatthew G. Knepley         fegeom.dim      = dim - h;
427d0ed5973SMatthew G. Knepley         fegeom.dimEmbed = dimEmbed;
428d0ed5973SMatthew G. Knepley       }
429d0ed5973SMatthew G. Knepley       if (hasFV) {ierr = DMPlexComputeCellGeometryFVM(dm, p, &fvgeom.volume, fvgeom.centroid, NULL);CHKERRQ(ierr);}
430b793a5ffSMatthew G. Knepley       for (f = 0, v = 0; f < Nf; ++f) {
431c110b1eeSGeoffrey Irving         void * const ctx = ctxs ? ctxs[f] : NULL;
4320f2d7e86SMatthew G. Knepley 
4337d1dd11eSToby Isaac         if (!sp[f]) continue;
43472f94c41SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
43572f94c41SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
436120386c5SMatthew G. Knepley           if (funcs[f]) {
437d0ed5973SMatthew G. Knepley             if (isFE[f]) {ierr = PetscDualSpaceApply(sp[f], d, time, &fegeom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);}
438d0ed5973SMatthew G. Knepley             else         {ierr = PetscDualSpaceApplyFVM(sp[f], d, time, &fvgeom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);}
439120386c5SMatthew G. Knepley           } else {
44015496722SMatthew G. Knepley             for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
441120386c5SMatthew G. Knepley           }
44215496722SMatthew G. Knepley           v += numComp[f];
443cb1e1211SMatthew G Knepley         }
444cb1e1211SMatthew G Knepley       }
4457d1dd11eSToby Isaac       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
446cb1e1211SMatthew G Knepley     }
44772f94c41SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
448db4c39b1SToby Isaac     if (h) {
449b793a5ffSMatthew G. Knepley       for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
4507d1dd11eSToby Isaac     }
4517d1dd11eSToby Isaac   }
452d0ed5973SMatthew G. Knepley   ierr = PetscFree3(isFE, sp, numComp);CHKERRQ(ierr);
4537d1dd11eSToby Isaac   if (maxHeight > 0) {
4547d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
4557d1dd11eSToby Isaac   }
456cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
457cb1e1211SMatthew G Knepley }
458cb1e1211SMatthew G Knepley 
459cb1e1211SMatthew G Knepley #undef __FUNCT__
460bdd6f66aSToby Isaac #define __FUNCT__ "DMProjectFieldLocal_Plex"
461bfc4295aSToby Isaac PetscErrorCode DMProjectFieldLocal_Plex(DM dm, Vec localU,
462e29c0834SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
463e29c0834SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
464e29c0834SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
465e29c0834SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscScalar[]),
466e29c0834SMatthew G. Knepley                                         InsertMode mode, Vec localX)
4670f2d7e86SMatthew G. Knepley {
4680f2d7e86SMatthew G. Knepley   DM              dmAux;
469d5396abcSMatthew G. Knepley   PetscDS         prob, probAux = NULL;
4700f2d7e86SMatthew G. Knepley   Vec             A;
471d5396abcSMatthew G. Knepley   PetscSection    section, sectionAux = NULL;
472b793a5ffSMatthew G. Knepley   PetscDualSpace *sp;
473b793a5ffSMatthew G. Knepley   PetscInt       *Ncf;
474326413afSMatthew G. Knepley   PetscScalar    *values, *u, *u_x, *a, *a_x;
475d5396abcSMatthew G. Knepley   PetscReal      *x, *v0, *J, *invJ, detJ;
476e29c0834SMatthew G. Knepley   PetscInt       *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
477e29c0834SMatthew G. Knepley   PetscInt        Nf, NfAux = 0, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight;
4780f2d7e86SMatthew G. Knepley   PetscErrorCode  ierr;
4790f2d7e86SMatthew G. Knepley 
4800f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
481048b7b1eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
482048b7b1eSToby Isaac   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
4832764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
484c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4850f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4860f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
487b793a5ffSMatthew G. Knepley   ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr);
4880f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4892764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
490e29c0834SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
491e29c0834SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
4922764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
4932764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
4940f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
4950f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
4960f2d7e86SMatthew G. Knepley   if (dmAux) {
4972764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
498e29c0834SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
499326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
500e29c0834SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
501e29c0834SMatthew G. Knepley     ierr = PetscDSGetComponentDerivativeOffsets(probAux, &aOff_x);CHKERRQ(ierr);
5022764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
5030f2d7e86SMatthew G. Knepley   }
504bdd6f66aSToby Isaac   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
5050f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
5060f2d7e86SMatthew G. Knepley   if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim);
5070f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5080f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
5099ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5109ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
5110f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
512326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
513326413afSMatthew G. Knepley 
5148e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
515326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
516326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5170f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
518d5396abcSMatthew G. Knepley       PetscObject  obj;
519d5396abcSMatthew G. Knepley       PetscClassId id;
5203113607cSMatthew G. Knepley 
521d5396abcSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
522d5396abcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
523d5396abcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
524d5396abcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
525d5396abcSMatthew G. Knepley 
526b793a5ffSMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
527b793a5ffSMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr);
528d5396abcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
529d5396abcSMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
530d5396abcSMatthew G. Knepley 
531b793a5ffSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr);
532e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
533d5396abcSMatthew G. Knepley       }
534b793a5ffSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
5350f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
5360f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
5370f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
5380f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
5390f2d7e86SMatthew G. Knepley 
5400f2d7e86SMatthew G. Knepley         if (funcs[f]) {
541b793a5ffSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr);
5420f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
5430f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
5440f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
545326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
546326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
547e29c0834SMatthew G. Knepley             (*funcs[f])(dim, Nf, NfAux, uOff, uOff_x, u, NULL, u_x, aOff, aOff_x, a, NULL, a_x, 0.0, x, &values[v]);
5480f2d7e86SMatthew G. Knepley           }
5490f2d7e86SMatthew G. Knepley         } else {
550b793a5ffSMatthew G. Knepley           for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0;
5510f2d7e86SMatthew G. Knepley         }
552b793a5ffSMatthew G. Knepley         v += Ncf[f];
5530f2d7e86SMatthew G. Knepley       }
5540f2d7e86SMatthew G. Knepley     }
555326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
556326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5570f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
5580f2d7e86SMatthew G. Knepley   }
5590f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
560*ca3eba1bSToby Isaac   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
561b793a5ffSMatthew G. Knepley   ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr);
5620f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5630f2d7e86SMatthew G. Knepley }
5640f2d7e86SMatthew G. Knepley 
5650f2d7e86SMatthew G. Knepley #undef __FUNCT__
566d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal"
5670163fd50SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscReal time, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX)
56855f2e967SMatthew G. Knepley {
5690163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
57055f2e967SMatthew G. Knepley   void            **ctxs;
571d7ddef95SMatthew G. Knepley   PetscInt          numFields;
572d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
573d7ddef95SMatthew G. Knepley 
574d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
575d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
576d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
577d7ddef95SMatthew G. Knepley   funcs[field] = func;
578d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
5790709b2feSToby Isaac   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
580d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
581d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
582d7ddef95SMatthew G. Knepley }
583d7ddef95SMatthew G. Knepley 
584d7ddef95SMatthew G. Knepley #undef __FUNCT__
585d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal"
5860e0f25f2SMatthew G. Knepley /* This ignores numcomps/comps */
587d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad,
588d7ddef95SMatthew G. Knepley                                                               PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
589d7ddef95SMatthew G. Knepley {
59061f58d28SMatthew G. Knepley   PetscDS            prob;
591da97024aSMatthew G. Knepley   PetscSF            sf;
592d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
59320369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
594da97024aSMatthew G. Knepley   const PetscInt    *leaves;
595d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
596520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
597d7ddef95SMatthew G. Knepley   PetscErrorCode     ierr;
598d7ddef95SMatthew G. Knepley 
599d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
600da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
601da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
602da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
603d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
604d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
60561f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
606d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
607d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
60820369375SToby Isaac   if (cellGeometry) {
609d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
610d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
61120369375SToby Isaac   }
612d7ddef95SMatthew G. Knepley   if (Grad) {
613c0a6632aSMatthew G. Knepley     PetscFV fv;
614c0a6632aSMatthew G. Knepley 
615c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
616d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
617d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
618d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
619d7ddef95SMatthew G. Knepley     ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
620d7ddef95SMatthew G. Knepley   }
621d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
622d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
623d7ddef95SMatthew G. Knepley     IS              faceIS;
624d7ddef95SMatthew G. Knepley     const PetscInt *faces;
625d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
626d7ddef95SMatthew G. Knepley 
627d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
628d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
629d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
630d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
631d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
632d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
633d7ddef95SMatthew G. Knepley       const PetscFVFaceGeom *fg;
634d7ddef95SMatthew G. Knepley 
635d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
636da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
637da97024aSMatthew G. Knepley       if (loc >= 0) continue;
638d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
639d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
640d7ddef95SMatthew G. Knepley       if (Grad) {
641d7ddef95SMatthew G. Knepley         const PetscFVCellGeom *cg;
642d7ddef95SMatthew G. Knepley         const PetscScalar     *cx, *cgrad;
643d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
644d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
645d7ddef95SMatthew G. Knepley         PetscInt               d;
646d7ddef95SMatthew G. Knepley 
647d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
648d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
649d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
650520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
651d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
652d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
653520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
654d7ddef95SMatthew G. Knepley       } else {
655d7ddef95SMatthew G. Knepley         const PetscScalar *xI;
656d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
657d7ddef95SMatthew G. Knepley 
658d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
659520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
660520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr);
661d7ddef95SMatthew G. Knepley       }
662d7ddef95SMatthew G. Knepley     }
663d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
664d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
665d7ddef95SMatthew G. Knepley   }
666d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
667d7ddef95SMatthew G. Knepley   if (Grad) {
668d7ddef95SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
669d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
670d7ddef95SMatthew G. Knepley   }
67120369375SToby Isaac   if (cellGeometry) {
672d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
67320369375SToby Isaac   }
674d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
675d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
676d7ddef95SMatthew G. Knepley }
677d7ddef95SMatthew G. Knepley 
678d7ddef95SMatthew G. Knepley #undef __FUNCT__
679d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues"
680bdd6f66aSToby Isaac PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
681d7ddef95SMatthew G. Knepley {
682d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
68355f2e967SMatthew G. Knepley   PetscErrorCode ierr;
68455f2e967SMatthew G. Knepley 
68555f2e967SMatthew G. Knepley   PetscFunctionBegin;
68655f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
687d7ddef95SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
688d7ddef95SMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
689d7ddef95SMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
690d7ddef95SMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
691a6ba4734SToby Isaac   ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
69255f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
69355f2e967SMatthew G. Knepley     PetscBool       isEssential;
694d7ddef95SMatthew G. Knepley     const char     *labelname;
695d7ddef95SMatthew G. Knepley     DMLabel         label;
696d7ddef95SMatthew G. Knepley     PetscInt        field;
697d7ddef95SMatthew G. Knepley     PetscObject     obj;
698d7ddef95SMatthew G. Knepley     PetscClassId    id;
69955f2e967SMatthew G. Knepley     void          (*func)();
700d7ddef95SMatthew G. Knepley     PetscInt        numids;
701d7ddef95SMatthew G. Knepley     const PetscInt *ids;
70255f2e967SMatthew G. Knepley     void           *ctx;
70355f2e967SMatthew G. Knepley 
704a6ba4734SToby Isaac     ierr = DMGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, NULL, NULL, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
705bdd6f66aSToby Isaac     if (insertEssential != isEssential) continue;
706c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
707d7ddef95SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
708d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
709d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
710bdd6f66aSToby Isaac       if (!isEssential) continue; /* for FEM, there is no insertion to be done for non-essential boundary conditions */
7110163fd50SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, time, field, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
712d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
71343ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
714d7ddef95SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM,
715d7ddef95SMatthew G. Knepley                                                      field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
716d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
71755f2e967SMatthew G. Knepley   }
71855f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
71955f2e967SMatthew G. Knepley }
72055f2e967SMatthew G. Knepley 
721cb1e1211SMatthew G Knepley #undef __FUNCT__
7220709b2feSToby Isaac #define __FUNCT__ "DMComputeL2Diff_Plex"
7230709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
724cb1e1211SMatthew G Knepley {
725cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
726cb1e1211SMatthew G Knepley   PetscSection     section;
727c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
728cb1e1211SMatthew G Knepley   Vec              localX;
72915496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
730cb1e1211SMatthew G Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
731cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
73215496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7339ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
734cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
735cb1e1211SMatthew G Knepley 
736cb1e1211SMatthew G Knepley   PetscFunctionBegin;
737c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
738cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
739cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
740cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
741bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
742cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
743cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
744cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
74515496722SMatthew G. Knepley     PetscObject  obj;
74615496722SMatthew G. Knepley     PetscClassId id;
747c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
748c5bbbd5bSMatthew G. Knepley 
74915496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
75015496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
75115496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
75215496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
75315496722SMatthew G. Knepley 
7540f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7550f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
75615496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
75715496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
75815496722SMatthew G. Knepley 
75915496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
76015496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
76115496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
762c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
763cb1e1211SMatthew G Knepley   }
76415496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
76515496722SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
766cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7679ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
7689ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
769cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
770a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
771cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
772cb1e1211SMatthew G Knepley 
7738e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
774cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
775cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
776cb1e1211SMatthew G Knepley 
77715496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
77815496722SMatthew G. Knepley       PetscObject  obj;
77915496722SMatthew G. Knepley       PetscClassId id;
780c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
78115496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
782cb1e1211SMatthew G Knepley 
78315496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
78415496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
78515496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
78615496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
78715496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
788cb1e1211SMatthew G Knepley       if (debug) {
789cb1e1211SMatthew G Knepley         char title[1024];
790cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
79115496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
792cb1e1211SMatthew G Knepley       }
793cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
79415496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
7950163fd50SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, Nc, funcVal, ctx);CHKERRQ(ierr);
79615496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
79715496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
79815496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
79915496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
80015496722SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
80115496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
802cb1e1211SMatthew G Knepley         }
803cb1e1211SMatthew G Knepley       }
80415496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
805cb1e1211SMatthew G Knepley     }
806cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
807cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
808cb1e1211SMatthew G Knepley     localDiff += elemDiff;
809cb1e1211SMatthew G Knepley   }
81015496722SMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
811cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
812b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
813cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
814cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
815cb1e1211SMatthew G Knepley }
816cb1e1211SMatthew G Knepley 
817cb1e1211SMatthew G Knepley #undef __FUNCT__
818b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff_Plex"
819b698f381SToby 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)
820cb1e1211SMatthew G Knepley {
82140e14135SMatthew G. Knepley   const PetscInt  debug = 0;
822cb1e1211SMatthew G Knepley   PetscSection    section;
82340e14135SMatthew G. Knepley   PetscQuadrature quad;
82440e14135SMatthew G. Knepley   Vec             localX;
82540e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
82640e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
82740e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
8289ac3fadcSMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp;
829cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
830cb1e1211SMatthew G Knepley 
831cb1e1211SMatthew G Knepley   PetscFunctionBegin;
832c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
83340e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
83440e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
83540e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
83640e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
83740e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
838652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8390f2d7e86SMatthew G. Knepley     PetscFE  fe;
84040e14135SMatthew G. Knepley     PetscInt Nc;
841652b88e8SMatthew G. Knepley 
8420f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8430f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8440f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
84540e14135SMatthew G. Knepley     numComponents += Nc;
846652b88e8SMatthew G. Knepley   }
8474d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
84840e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
84940e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8509ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8519ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
85240e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
85340e14135SMatthew G. Knepley     PetscScalar *x = NULL;
85440e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
855652b88e8SMatthew G. Knepley 
8568e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
85740e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
85840e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
85940e14135SMatthew G. Knepley 
86040e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
8610f2d7e86SMatthew G. Knepley       PetscFE          fe;
86251259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
86321454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
86440e14135SMatthew G. Knepley       PetscReal       *basisDer;
86521454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
86640e14135SMatthew G. Knepley 
8670f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
86821454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
8700f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
8710f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
87240e14135SMatthew G. Knepley       if (debug) {
87340e14135SMatthew G. Knepley         char title[1024];
87440e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
87540e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
876652b88e8SMatthew G. Knepley       }
87740e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
87840e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
87940e14135SMatthew G. Knepley           coords[d] = v0[d];
88040e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
88140e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
882652b88e8SMatthew G. Knepley           }
88340e14135SMatthew G. Knepley         }
8840163fd50SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr);
88540e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
88640e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
88740e14135SMatthew G. Knepley 
88840e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
88940e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
89040e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
89140e14135SMatthew G. Knepley 
89240e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
89340e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
89440e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
89540e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
89640e14135SMatthew G. Knepley               }
89740e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
89840e14135SMatthew G. Knepley             }
89940e14135SMatthew G. Knepley           }
90040e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
90140e14135SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
90240e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
90340e14135SMatthew G. Knepley         }
90440e14135SMatthew G. Knepley       }
90540e14135SMatthew G. Knepley       comp        += Ncomp;
90640e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
90740e14135SMatthew G. Knepley     }
90840e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
90940e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
91040e14135SMatthew G. Knepley     localDiff += elemDiff;
91140e14135SMatthew G. Knepley   }
91240e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
91340e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
914b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
91540e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
916cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
917cb1e1211SMatthew G Knepley }
918cb1e1211SMatthew G Knepley 
919a0845e3aSMatthew G. Knepley #undef __FUNCT__
9202a16baeaSToby Isaac #define __FUNCT__ "DMComputeL2FieldDiff_Plex"
9211189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
92273d901b8SMatthew G. Knepley {
92373d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
92473d901b8SMatthew G. Knepley   PetscSection     section;
92573d901b8SMatthew G. Knepley   PetscQuadrature  quad;
92673d901b8SMatthew G. Knepley   Vec              localX;
92715496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
92873d901b8SMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
92973d901b8SMatthew G. Knepley   PetscReal       *localDiff;
93015496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
9319ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
93273d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
93373d901b8SMatthew G. Knepley 
93473d901b8SMatthew G. Knepley   PetscFunctionBegin;
935c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
93673d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
93773d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
93873d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
939bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
94073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
94173d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
94273d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
94315496722SMatthew G. Knepley     PetscObject  obj;
94415496722SMatthew G. Knepley     PetscClassId id;
94573d901b8SMatthew G. Knepley     PetscInt     Nc;
94673d901b8SMatthew G. Knepley 
94715496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
94815496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
94915496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
95015496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
95115496722SMatthew G. Knepley 
9520f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
9530f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
95415496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
95515496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
95615496722SMatthew G. Knepley 
95715496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
95815496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
95915496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
96073d901b8SMatthew G. Knepley     numComponents += Nc;
96173d901b8SMatthew G. Knepley   }
96215496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
96315496722SMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
96473d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9659ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
9669ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
96773d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
96873d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
96973d901b8SMatthew G. Knepley 
9708e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
97173d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
97273d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
97373d901b8SMatthew G. Knepley 
97415496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
97515496722SMatthew G. Knepley       PetscObject  obj;
97615496722SMatthew G. Knepley       PetscClassId id;
97773d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
97815496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
97973d901b8SMatthew G. Knepley 
98015496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
98115496722SMatthew G. Knepley 
98215496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
98315496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
98415496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
98515496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
98615496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
98773d901b8SMatthew G. Knepley       if (debug) {
98873d901b8SMatthew G. Knepley         char title[1024];
98973d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
99015496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
99173d901b8SMatthew G. Knepley       }
99273d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
99315496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
9940163fd50SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, numFields, funcVal, ctx);CHKERRQ(ierr);
99515496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
99615496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
99715496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
99815496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
99915496722SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
100015496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
100173d901b8SMatthew G. Knepley         }
100273d901b8SMatthew G. Knepley       }
100315496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
100473d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
100573d901b8SMatthew G. Knepley     }
100673d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
100773d901b8SMatthew G. Knepley   }
100873d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1009b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
101073d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
101115496722SMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
101273d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
101373d901b8SMatthew G. Knepley }
101473d901b8SMatthew G. Knepley 
101573d901b8SMatthew G. Knepley #undef __FUNCT__
1016e729f68cSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2DiffVec"
1017e729f68cSMatthew G. Knepley /*@C
1018e729f68cSMatthew 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.
1019e729f68cSMatthew G. Knepley 
1020e729f68cSMatthew G. Knepley   Input Parameters:
1021e729f68cSMatthew G. Knepley + dm    - The DM
10220163fd50SMatthew G. Knepley . time  - The time
1023*ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1024e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1025e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1026e729f68cSMatthew G. Knepley 
1027e729f68cSMatthew G. Knepley   Output Parameter:
1028e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1029e729f68cSMatthew G. Knepley 
1030e729f68cSMatthew G. Knepley   Level: developer
1031e729f68cSMatthew G. Knepley 
1032b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1033e729f68cSMatthew G. Knepley @*/
10340163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1035e729f68cSMatthew G. Knepley {
1036e729f68cSMatthew G. Knepley   PetscSection     section;
1037e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1038e729f68cSMatthew G. Knepley   Vec              localX;
1039e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
1040e729f68cSMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
1041e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1042e729f68cSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1043e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1044e729f68cSMatthew G. Knepley 
1045e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1046e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1047e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1048e729f68cSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1049e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1050e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1051bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1052e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1053e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1054e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1055e729f68cSMatthew G. Knepley     PetscObject  obj;
1056e729f68cSMatthew G. Knepley     PetscClassId id;
1057e729f68cSMatthew G. Knepley     PetscInt     Nc;
1058e729f68cSMatthew G. Knepley 
1059e729f68cSMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
1060e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1061e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1062e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1063e729f68cSMatthew G. Knepley 
1064e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1065e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1066e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1067e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1068e729f68cSMatthew G. Knepley 
1069e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1070e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1071e729f68cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1072e729f68cSMatthew G. Knepley     numComponents += Nc;
1073e729f68cSMatthew G. Knepley   }
1074e729f68cSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
1075e729f68cSMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
1076e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1077e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1078e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1079e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1080e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
10816f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
1082e729f68cSMatthew G. Knepley 
1083e729f68cSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
1084e729f68cSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
1085e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1086e729f68cSMatthew G. Knepley 
1087e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1088e729f68cSMatthew G. Knepley       PetscObject  obj;
1089e729f68cSMatthew G. Knepley       PetscClassId id;
1090e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1091e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1092e729f68cSMatthew G. Knepley 
1093*ca3eba1bSToby Isaac       if (!funcs[field]) continue;
1094e729f68cSMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
1095e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1096e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1097e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1098e729f68cSMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1099e729f68cSMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
1100e729f68cSMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
11010163fd50SMatthew G. Knepley         ierr = (*funcs[field])(dim, time, coords, Nc, funcVal, ctx);CHKERRQ(ierr);
1102e729f68cSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1103e729f68cSMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1104e729f68cSMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1105e729f68cSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1106e729f68cSMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
1107e729f68cSMatthew G. Knepley         }
1108e729f68cSMatthew G. Knepley       }
1109e729f68cSMatthew G. Knepley       fieldOffset += Nb*Nc;
1110e729f68cSMatthew G. Knepley     }
1111e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1112e729f68cSMatthew G. Knepley     ierr = VecSetValuesSection(D, section, c, &elemDiff, ADD_VALUES);CHKERRQ(ierr);
1113e729f68cSMatthew G. Knepley   }
1114e729f68cSMatthew G. Knepley   ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
1115e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1116e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1117e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1118e729f68cSMatthew G. Knepley }
1119e729f68cSMatthew G. Knepley 
1120e729f68cSMatthew G. Knepley #undef __FUNCT__
112173d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
112273d901b8SMatthew G. Knepley /*@
112373d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
112473d901b8SMatthew G. Knepley 
112573d901b8SMatthew G. Knepley   Input Parameters:
112673d901b8SMatthew G. Knepley + dm - The mesh
112773d901b8SMatthew G. Knepley . X  - Local input vector
112873d901b8SMatthew G. Knepley - user - The user context
112973d901b8SMatthew G. Knepley 
113073d901b8SMatthew G. Knepley   Output Parameter:
113173d901b8SMatthew G. Knepley . integral - Local integral for each field
113273d901b8SMatthew G. Knepley 
113373d901b8SMatthew G. Knepley   Level: developer
113473d901b8SMatthew G. Knepley 
113573d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
113673d901b8SMatthew G. Knepley @*/
11370f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
113873d901b8SMatthew G. Knepley {
113973d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
114073d901b8SMatthew G. Knepley   DM                dmAux;
114173d901b8SMatthew G. Knepley   Vec               localX, A;
11422764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
114373d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
1144bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
114573d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
1146c1f031eeSMatthew G. Knepley   PetscReal        *lintegral, *vol;
11479ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c;
11480f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
114973d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
115073d901b8SMatthew G. Knepley 
115173d901b8SMatthew G. Knepley   PetscFunctionBegin;
1152c1f031eeSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
115373d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1154bdd6f66aSToby Isaac   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
115573d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
115673d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1157c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
115873d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
11592764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
11602764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
116173d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
116273d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11639ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11649ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
116573d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
116673d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
116773d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
116873d901b8SMatthew G. Knepley   if (dmAux) {
116973d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
11702764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
11712764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
117273d901b8SMatthew G. Knepley   }
1173c1f031eeSMatthew G. Knepley   ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr);
11740f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1175c1f031eeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;}
117673d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
117773d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
117873d901b8SMatthew G. Knepley     PetscInt     i;
117973d901b8SMatthew G. Knepley 
1180bbce034cSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr);
1181c1f031eeSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr);
1182bbce034cSMatthew G. Knepley     if (cgeom[c].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cgeom[c].detJ, c);
118373d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
11840f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
118573d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
118673d901b8SMatthew G. Knepley     if (dmAux) {
118773d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11880f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
118973d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
119073d901b8SMatthew G. Knepley     }
119173d901b8SMatthew G. Knepley   }
119273d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1193c1f031eeSMatthew G. Knepley     PetscObject  obj;
1194c1f031eeSMatthew G. Knepley     PetscClassId id;
1195c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
119673d901b8SMatthew G. Knepley 
1197c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1198c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1199c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1200c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1201c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1202c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1203c1f031eeSMatthew G. Knepley 
12040f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1205c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1206c1f031eeSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1207c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1208c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
120973d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
12100f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
121173d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
121273d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
121373d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
121473d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1215c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr);
1216c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr);
1217c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1218b69edc29SMatthew G. Knepley       /* PetscFV  fv = (PetscFV) obj; */
1219c1f031eeSMatthew G. Knepley       PetscInt       foff;
1220420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1221b69edc29SMatthew G. Knepley       PetscScalar    lint;
1222c1f031eeSMatthew G. Knepley 
1223c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1224c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1225c1f031eeSMatthew G. Knepley       if (obj_func) {
1226c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1227c1f031eeSMatthew G. Knepley           /* TODO: Need full pointwise interpolation and get centroid for x argument */
122830b9ff8bSMatthew G. Knepley           obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint);
1229b69edc29SMatthew G. Knepley           lintegral[f] = PetscRealPart(lint)*vol[c];
123073d901b8SMatthew G. Knepley         }
1231c1f031eeSMatthew G. Knepley       }
1232c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1233c1f031eeSMatthew G. Knepley   }
123473d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
123573d901b8SMatthew G. Knepley   if (mesh->printFEM) {
123673d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
1237c1f031eeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);}
123873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
123973d901b8SMatthew G. Knepley   }
124073d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1241b2566f29SBarry Smith   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
1242c1f031eeSMatthew G. Knepley   ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr);
1243c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
124473d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
124573d901b8SMatthew G. Knepley }
124673d901b8SMatthew G. Knepley 
124773d901b8SMatthew G. Knepley #undef __FUNCT__
124868132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorNested"
1249d69c5d34SMatthew G. Knepley /*@
125068132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1251d69c5d34SMatthew G. Knepley 
1252d69c5d34SMatthew G. Knepley   Input Parameters:
1253d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1254d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1255d69c5d34SMatthew G. Knepley - user - The user context
1256d69c5d34SMatthew G. Knepley 
1257d69c5d34SMatthew G. Knepley   Output Parameter:
1258934789fcSMatthew G. Knepley . In  - The interpolation matrix
1259d69c5d34SMatthew G. Knepley 
1260d69c5d34SMatthew G. Knepley   Level: developer
1261d69c5d34SMatthew G. Knepley 
126268132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1263d69c5d34SMatthew G. Knepley @*/
126468132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
1265d69c5d34SMatthew G. Knepley {
1266d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1267d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
12682764a2aaSMatthew G. Knepley   PetscDS           prob;
1269d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
127097c42addSMatthew G. Knepley   PetscFV          *fvRef;
1271d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1272d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1273d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
12749ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
12750f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1276d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1277d69c5d34SMatthew G. Knepley 
1278d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1279d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1280c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1281d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1282d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1283d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1284d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1285d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1286d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
12879ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12889ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
12892764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
129097c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1291d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
129297c42addSMatthew G. Knepley     PetscObject  obj;
129397c42addSMatthew G. Knepley     PetscClassId id;
1294aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1295d69c5d34SMatthew G. Knepley 
129697c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
129797c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
129897c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
129997c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
130097c42addSMatthew G. Knepley 
13010f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1302d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
13030f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
130497c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
130597c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
130697c42addSMatthew G. Knepley       PetscDualSpace Q;
130797c42addSMatthew G. Knepley 
130897c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
130997c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
131097c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
131197c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
131297c42addSMatthew G. Knepley     }
13130f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1314d69c5d34SMatthew G. Knepley   }
13152764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
13160f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
13170f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1318d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1319d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1320d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1321d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1322d69c5d34SMatthew G. Knepley     PetscReal       *points;
1323d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1324d69c5d34SMatthew G. Knepley 
1325d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
132697c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1327d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
13280f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
132997c42addSMatthew G. Knepley     } else {
133097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
133197c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
133297c42addSMatthew G. Knepley     }
1333d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1334d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1335d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1336d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1337d69c5d34SMatthew G. Knepley       npoints += Np;
1338d69c5d34SMatthew G. Knepley     }
1339d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1340d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1341d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1342d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1343d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1344d69c5d34SMatthew G. Knepley     }
1345d69c5d34SMatthew G. Knepley 
1346d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
134797c42addSMatthew G. Knepley       PetscObject  obj;
134897c42addSMatthew G. Knepley       PetscClassId id;
1349d69c5d34SMatthew G. Knepley       PetscReal   *B;
1350aa7890ccSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j;
1351d69c5d34SMatthew G. Knepley 
135297c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
135397c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
135497c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
135597c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1356d69c5d34SMatthew G. Knepley 
1357d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
13580f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
13590f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1360ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1361ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
13627c927364SMatthew 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);
13630f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1364d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1365d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1366d69c5d34SMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1367d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
136836a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
13690f2d7e86SMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p];
137036a6d9c0SMatthew G. Knepley               }
1371d69c5d34SMatthew G. Knepley             }
1372d69c5d34SMatthew G. Knepley           }
13730f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1374ffe73a53SMatthew G. Knepley         }
137597c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
137697c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
137797c42addSMatthew G. Knepley 
137897c42addSMatthew G. Knepley         /* Evaluate constant function at points */
137997c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
138097c42addSMatthew G. Knepley         cpdim = 1;
138197c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
138297c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
138397c42addSMatthew 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);
138497c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
138597c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
138697c42addSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
138797c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
138897c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
138997c42addSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p];
139097c42addSMatthew G. Knepley               }
139197c42addSMatthew G. Knepley             }
139297c42addSMatthew G. Knepley           }
139397c42addSMatthew G. Knepley         }
139497c42addSMatthew G. Knepley       }
139536a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1396d69c5d34SMatthew G. Knepley     }
1397d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1398549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1399d69c5d34SMatthew G. Knepley   }
14000f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
14017f5b169aSMatthew G. Knepley   /* Preallocate matrix */
14027f5b169aSMatthew G. Knepley   {
1403c094ef40SMatthew G. Knepley     Mat          preallocator;
1404c094ef40SMatthew G. Knepley     PetscScalar *vals;
1405c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
1406c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
14077f5b169aSMatthew G. Knepley 
1408c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
1409c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
1410c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1411c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1412c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1413c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
14147f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
14157f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
1416c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
14177f5b169aSMatthew G. Knepley     }
1418c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
1419c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1420c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1421c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
1422c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
14237f5b169aSMatthew G. Knepley   }
14247f5b169aSMatthew G. Knepley   /* Fill matrix */
14257f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1426d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1427934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1428d69c5d34SMatthew G. Knepley   }
1429549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
143097c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1431549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1432934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1433934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1434d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1435d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1436934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1437934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1438d69c5d34SMatthew G. Knepley   }
1439d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1440d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1441d69c5d34SMatthew G. Knepley }
14426c73c22cSMatthew G. Knepley 
14436c73c22cSMatthew G. Knepley #undef __FUNCT__
144468132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorGeneral"
144568132eb9SMatthew G. Knepley /*@
144668132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
144768132eb9SMatthew G. Knepley 
144868132eb9SMatthew G. Knepley   Input Parameters:
144968132eb9SMatthew G. Knepley + dmf  - The fine mesh
145068132eb9SMatthew G. Knepley . dmc  - The coarse mesh
145168132eb9SMatthew G. Knepley - user - The user context
145268132eb9SMatthew G. Knepley 
145368132eb9SMatthew G. Knepley   Output Parameter:
145468132eb9SMatthew G. Knepley . In  - The interpolation matrix
145568132eb9SMatthew G. Knepley 
145668132eb9SMatthew G. Knepley   Level: developer
145768132eb9SMatthew G. Knepley 
145868132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
145968132eb9SMatthew G. Knepley @*/
146068132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
14614ef9d792SMatthew G. Knepley {
146264e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
146364e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
14644ef9d792SMatthew G. Knepley   PetscDS        prob;
14654ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
14664ef9d792SMatthew G. Knepley   PetscHashJK    ht;
14674ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
14684ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
14694ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
14704ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
14714ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
14724ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
14734ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
14744ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
14754ef9d792SMatthew G. Knepley 
14764ef9d792SMatthew G. Knepley   PetscFunctionBegin;
14774ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
14784ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
14794ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
14804ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
14814ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
14824ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
14834ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
14844ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
14854ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
14864ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
14874ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
14884ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
14894ef9d792SMatthew G. Knepley   ierr = PetscMalloc1(totDim*totDim, &elemMat);CHKERRQ(ierr);
14904ef9d792SMatthew G. Knepley 
14914ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
14924ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
14934ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
14944ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
14954ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
14964ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
14974ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
14984ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
14994ef9d792SMatthew G. Knepley   ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
15004ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
15014ef9d792SMatthew G. Knepley     PetscObject      obj;
15024ef9d792SMatthew G. Knepley     PetscClassId     id;
1503c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
15044ef9d792SMatthew G. Knepley     PetscQuadrature  f;
150517f047d8SMatthew G. Knepley     const PetscReal *qpoints;
150617f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
15074ef9d792SMatthew G. Knepley 
15084ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
15094ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15104ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
15114ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
15124ef9d792SMatthew G. Knepley 
15134ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
15144ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
15154ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
15164ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
15174ef9d792SMatthew G. Knepley 
15184ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
15194ef9d792SMatthew G. Knepley       Nc   = 1;
15204ef9d792SMatthew G. Knepley     }
15214ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
15224ef9d792SMatthew G. Knepley     /* For each fine grid cell */
15234ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
15244ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
15254ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
15264ef9d792SMatthew G. Knepley 
15276ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
15284ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
15294ef9d792SMatthew G. Knepley       if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc);
15304ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
15314ef9d792SMatthew G. Knepley         Vec             pointVec;
15324ef9d792SMatthew G. Knepley         PetscScalar    *pV;
15334ef9d792SMatthew G. Knepley         IS              coarseCellIS;
15344ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
15354ef9d792SMatthew G. Knepley         PetscInt        numCoarseCells, q, r, c;
15364ef9d792SMatthew G. Knepley 
15374ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
15384ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
15394ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
15404ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
15414ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
15424ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
15434ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
15444ef9d792SMatthew G. Knepley           /* Transform point to real space */
15454ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
15464ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
15474ef9d792SMatthew G. Knepley         }
15484ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
15494ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
15504ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
15514ef9d792SMatthew G. Knepley         ierr = ISViewFromOptions(coarseCellIS, NULL, "-interp_is_view");CHKERRQ(ierr);
15524ef9d792SMatthew G. Knepley         /* Update preallocation info */
15534ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
15544ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
15554ef9d792SMatthew G. Knepley         for (r = 0; r < Nc; ++r) {
15564ef9d792SMatthew G. Knepley           PetscHashJKKey  key;
15574ef9d792SMatthew G. Knepley           PetscHashJKIter missing, iter;
15584ef9d792SMatthew G. Knepley 
15594ef9d792SMatthew G. Knepley           key.j = findices[i*Nc+r];
15604ef9d792SMatthew G. Knepley           if (key.j < 0) continue;
15614ef9d792SMatthew G. Knepley           /* Get indices for coarse elements */
15624ef9d792SMatthew G. Knepley           for (ccell = 0; ccell < numCoarseCells; ++ccell) {
156346bdb399SToby Isaac             ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices, NULL);CHKERRQ(ierr);
15644ef9d792SMatthew G. Knepley             for (c = 0; c < numCIndices; ++c) {
15654ef9d792SMatthew G. Knepley               key.k = cindices[c];
15664ef9d792SMatthew G. Knepley               if (key.k < 0) continue;
15674ef9d792SMatthew G. Knepley               ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
15684ef9d792SMatthew G. Knepley               if (missing) {
15694ef9d792SMatthew G. Knepley                 ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
15704ef9d792SMatthew G. Knepley                 if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
15714ef9d792SMatthew G. Knepley                 else                                     ++onz[key.j-rStart];
15724ef9d792SMatthew G. Knepley               }
15734ef9d792SMatthew G. Knepley             }
157446bdb399SToby Isaac             ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices, NULL);CHKERRQ(ierr);
15754ef9d792SMatthew G. Knepley           }
15764ef9d792SMatthew G. Knepley         }
15774ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
15784ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
15794ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
15804ef9d792SMatthew G. Knepley       }
158146bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
15824ef9d792SMatthew G. Knepley     }
15834ef9d792SMatthew G. Knepley   }
15844ef9d792SMatthew G. Knepley   ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
15854ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
15864ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
15874ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
15884ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
15894ef9d792SMatthew G. Knepley     PetscObject      obj;
15904ef9d792SMatthew G. Knepley     PetscClassId     id;
1591c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
15924ef9d792SMatthew G. Knepley     PetscQuadrature  f;
15934ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
159417f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
15954ef9d792SMatthew G. Knepley 
15964ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
15974ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15984ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
15994ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
16004ef9d792SMatthew G. Knepley 
16014ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
16024ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
16034ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
16044ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
16054ef9d792SMatthew G. Knepley 
16064ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
16074ef9d792SMatthew G. Knepley       Nc   = 1;
16084ef9d792SMatthew G. Knepley     }
16094ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
16104ef9d792SMatthew G. Knepley     /* For each fine grid cell */
16114ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
16124ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
16134ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
16144ef9d792SMatthew G. Knepley 
16156ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
16164ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
16174ef9d792SMatthew G. Knepley       if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc);
16184ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
16194ef9d792SMatthew G. Knepley         Vec             pointVec;
16204ef9d792SMatthew G. Knepley         PetscScalar    *pV;
16214ef9d792SMatthew G. Knepley         IS              coarseCellIS;
16224ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
162317f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
16244ef9d792SMatthew G. Knepley 
16254ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
16264ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
16274ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, &qweights);CHKERRQ(ierr);
16284ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
16294ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
16304ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
16314ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
16324ef9d792SMatthew G. Knepley           /* Transform point to real space */
16334ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
16344ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
16354ef9d792SMatthew G. Knepley         }
16364ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
16374ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
16384ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
16394ef9d792SMatthew G. Knepley         /* Update preallocation info */
16404ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
16414ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
16424ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
16434ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
1644826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
1645826eb36dSMatthew G. Knepley 
16466ecaa68aSToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices, NULL);CHKERRQ(ierr);
16474ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
1648f1ac73f2SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell], NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
1649e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
1650826eb36dSMatthew G. Knepley           CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x);
16514ef9d792SMatthew G. Knepley 
16524ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
16534ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
16544ef9d792SMatthew G. Knepley             PetscReal *B;
16554ef9d792SMatthew G. Knepley 
16564ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
16574ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
16584ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
16594ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
16604ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
16614ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = B[j*Nc + c]*qweights[ccell];
16624ef9d792SMatthew G. Knepley             }
16634ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
16644ef9d792SMatthew G. Knepley           } else {
16654ef9d792SMatthew G. Knepley             cpdim = 1;
16664ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
16674ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = 1.0*qweights[ccell];
16684ef9d792SMatthew G. Knepley             }
16694ef9d792SMatthew G. Knepley           }
16704ef9d792SMatthew G. Knepley           /* Update interpolator */
167164e98e1dSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, Nc, numCIndices, elemMat);CHKERRQ(ierr);}
16724ef9d792SMatthew G. Knepley           ierr = MatSetValues(In, Nc, &findices[i*Nc], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
167346bdb399SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices, NULL);CHKERRQ(ierr);
16744ef9d792SMatthew G. Knepley         }
16754ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
16764ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
16774ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
16784ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
16794ef9d792SMatthew G. Knepley       }
168046bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
16814ef9d792SMatthew G. Knepley     }
16824ef9d792SMatthew G. Knepley   }
16834ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
16844ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
16854ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
16864ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16874ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16884ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
16894ef9d792SMatthew G. Knepley }
16904ef9d792SMatthew G. Knepley 
16914ef9d792SMatthew G. Knepley #undef __FUNCT__
16927c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
16937c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
16947c927364SMatthew G. Knepley {
1695e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
16967c927364SMatthew G. Knepley   PetscFE       *feRef;
169797c42addSMatthew G. Knepley   PetscFV       *fvRef;
16987c927364SMatthew G. Knepley   Vec            fv, cv;
16997c927364SMatthew G. Knepley   IS             fis, cis;
17007c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
17017c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
17020bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
17037c927364SMatthew G. Knepley   PetscErrorCode ierr;
17047c927364SMatthew G. Knepley 
17057c927364SMatthew G. Knepley   PetscFunctionBegin;
170675a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1707c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
17087c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
17097c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
17107c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
17117c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
17127c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
17137c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
17149ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
17159ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1716e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
171797c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
17187c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
171997c42addSMatthew G. Knepley     PetscObject  obj;
172097c42addSMatthew G. Knepley     PetscClassId id;
1721aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
17227c927364SMatthew G. Knepley 
172397c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
172497c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
172597c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
172697c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
172797c42addSMatthew G. Knepley 
17287c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
17297c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
17307c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
173197c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
173297c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
173397c42addSMatthew G. Knepley       PetscDualSpace Q;
173497c42addSMatthew G. Knepley 
173597c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
173697c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
173797c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
173897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
173997c42addSMatthew G. Knepley     }
17407c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
17417c927364SMatthew G. Knepley   }
1742e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17437c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
17447c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
17457c927364SMatthew G. Knepley     PetscFE        feC;
174697c42addSMatthew G. Knepley     PetscFV        fvC;
17477c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
17487c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
17497c927364SMatthew G. Knepley 
175097c42addSMatthew G. Knepley     if (feRef[field]) {
1751e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
17527c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
17537c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
17547c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
17557c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
17567c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
17577c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
175897c42addSMatthew G. Knepley     } else {
175997c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
176097c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
176197c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
176297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
176397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
176497c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
176597c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
176697c42addSMatthew G. Knepley     }
176797c42addSMatthew 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);
17687c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
17697c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
17707c927364SMatthew G. Knepley       const PetscReal *cqpoints;
17717c927364SMatthew G. Knepley       PetscInt         NpC;
177297c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
17737c927364SMatthew G. Knepley 
17747c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
17757c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
177697c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
17777c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
17787c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
17797c927364SMatthew G. Knepley         const PetscReal *fqpoints;
17807c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
17817c927364SMatthew G. Knepley         PetscInt         NpF, comp;
17827c927364SMatthew G. Knepley 
17837c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
17847c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
17857c927364SMatthew G. Knepley         if (NpC != NpF) continue;
17867c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
17877c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
17887c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
17897c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
17907c927364SMatthew G. Knepley         }
179197c42addSMatthew G. Knepley         found = PETSC_TRUE;
17927c927364SMatthew G. Knepley         break;
17937c927364SMatthew G. Knepley       }
179497c42addSMatthew G. Knepley       if (!found) {
179597c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
179697c42addSMatthew G. Knepley         if (fvRef[field]) {
179797c42addSMatthew G. Knepley           PetscInt comp;
179897c42addSMatthew G. Knepley           for (comp = 0; comp < NcC; ++comp) {
179997c42addSMatthew G. Knepley             cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp;
180097c42addSMatthew G. Knepley           }
180197c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
180297c42addSMatthew G. Knepley       }
18037c927364SMatthew G. Knepley     }
18047c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
18057c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
18067c927364SMatthew G. Knepley   }
180797c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
180897c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
18097c927364SMatthew G. Knepley 
18107c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
18117c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
18120bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
18137c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
18147c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1815aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1816aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
18177c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
18187c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
18197c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
18207c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
18210bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
18227c927364SMatthew 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]]);
18237c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
18247c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
18257c927364SMatthew G. Knepley     }
18267c927364SMatthew G. Knepley   }
18277c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
18287c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
18297c927364SMatthew G. Knepley 
18307c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
18317c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
18327c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
18337c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
18347c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
18357c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
18367c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
183775a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1838cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1839cb1e1211SMatthew G Knepley }
1840