xref: /petsc/src/dm/impls/plex/plexfem.c (revision 17f047d895ea9c297dd24b3df2aa1863c019c8ef)
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"
89ad917190SMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody(PetscInt dim, 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]);
157ad917190SMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody;
158cb1e1211SMatthew G Knepley 
1599d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
160330485fdSToby Isaac     ctx[1] = d;
161026175e5SToby Isaac     ierr = DMPlexProjectFunction(dm, &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 
195048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
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 
221048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
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__
234a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
235ad917190SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, 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]) {
321dda36da7SMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, &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__
348cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
349ad917190SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscErrorCode (**funcs)(PetscInt, 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;
356cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
357cb1e1211SMatthew G Knepley 
358cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3599ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3609ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
3619ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
3629ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
363cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
364b793a5ffSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
365b793a5ffSMatthew G. Knepley   ierr = PetscMalloc2(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   }
3767d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
3777d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
378fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
379048b7b1eSToby Isaac     if (pEnd <= pStart) continue;
3807d1dd11eSToby Isaac     totDim = 0;
381b793a5ffSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
38215496722SMatthew G. Knepley       PetscObject  obj;
38315496722SMatthew G. Knepley       PetscClassId id;
3840f2d7e86SMatthew G. Knepley 
38515496722SMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
38615496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
38715496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
38815496722SMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
38915496722SMatthew G. Knepley 
39015496722SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
3917d1dd11eSToby Isaac         if (!h) {
3927d1dd11eSToby Isaac           ierr  = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
3937d1dd11eSToby Isaac           sp[f] = cellsp[f];
39415496722SMatthew G. Knepley           ierr  = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
3957d1dd11eSToby Isaac         }
3967d1dd11eSToby Isaac         else {
3977d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
3987d1dd11eSToby Isaac           if (!sp[f]) {
3997d1dd11eSToby Isaac             continue;
4007d1dd11eSToby Isaac           }
4017d1dd11eSToby Isaac         }
40215496722SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
40315496722SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
40415496722SMatthew G. Knepley 
405ee2838f6SToby Isaac         if (h) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP, "Projection height > 0 not supported for finite volume");
40615496722SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
407302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
408e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
40915496722SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
41072f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
41115496722SMatthew G. Knepley       totDim += spDim*numComp[f];
412cb1e1211SMatthew G Knepley     }
4137d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
4147d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
415d374af2bSToby Isaac     if (!totDim) continue;
41672f94c41SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4177d1dd11eSToby Isaac     for (p = pStart; p < pEnd; ++p) {
418e1d0b1adSMatthew G. Knepley       PetscFECellGeom geom;
419cb1e1211SMatthew G Knepley 
42031383a9bSToby Isaac       ierr          = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
421910c25dcSToby Isaac       geom.dim      = dim - h;
4227a1a1bd4SToby Isaac       geom.dimEmbed = dimEmbed;
423b793a5ffSMatthew G. Knepley       for (f = 0, v = 0; f < Nf; ++f) {
424c110b1eeSGeoffrey Irving         void * const ctx = ctxs ? ctxs[f] : NULL;
4250f2d7e86SMatthew G. Knepley 
4267d1dd11eSToby Isaac         if (!sp[f]) continue;
42772f94c41SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
42872f94c41SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
429120386c5SMatthew G. Knepley           if (funcs[f]) {
430e1d0b1adSMatthew G. Knepley             ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
431120386c5SMatthew G. Knepley           } else {
43215496722SMatthew G. Knepley             for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
433120386c5SMatthew G. Knepley           }
43415496722SMatthew G. Knepley           v += numComp[f];
435cb1e1211SMatthew G Knepley         }
436cb1e1211SMatthew G Knepley       }
4377d1dd11eSToby Isaac       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
438cb1e1211SMatthew G Knepley     }
43972f94c41SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
440ee2838f6SToby Isaac     if (h || !maxHeight) {
441b793a5ffSMatthew G. Knepley       for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
4427d1dd11eSToby Isaac     }
4437d1dd11eSToby Isaac   }
44415496722SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
4457d1dd11eSToby Isaac   if (maxHeight > 0) {
446b793a5ffSMatthew G. Knepley     for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);}
4477d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
4487d1dd11eSToby Isaac   }
449cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
450cb1e1211SMatthew G Knepley }
451cb1e1211SMatthew G Knepley 
452cb1e1211SMatthew G Knepley #undef __FUNCT__
4538040c1f3SToby Isaac #define __FUNCT__ "DMPlexProjectFunction"
4548040c1f3SToby Isaac /*@C
4558040c1f3SToby Isaac   DMPlexProjectFunction - This projects the given function into the function space provided.
4568040c1f3SToby Isaac 
4578040c1f3SToby Isaac   Input Parameters:
4588040c1f3SToby Isaac + dm      - The DM
4598040c1f3SToby Isaac . funcs   - The coordinate functions to evaluate, one per field
4608040c1f3SToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
4618040c1f3SToby Isaac - mode    - The insertion mode for values
4628040c1f3SToby Isaac 
4638040c1f3SToby Isaac   Output Parameter:
4648040c1f3SToby Isaac . X - vector
4658040c1f3SToby Isaac 
466ad917190SMatthew G. Knepley    Calling sequence of func:
467ad917190SMatthew G. Knepley $    func(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
468ad917190SMatthew G. Knepley 
469ad917190SMatthew G. Knepley +  dim - The spatial dimension
470ad917190SMatthew G. Knepley .  x   - The coordinates
471ad917190SMatthew G. Knepley .  Nf  - The number of fields
472ad917190SMatthew G. Knepley .  u   - The output field values
473ad917190SMatthew G. Knepley -  ctx - optional user-defined function context
474ad917190SMatthew G. Knepley 
4758040c1f3SToby Isaac   Level: developer
4768040c1f3SToby Isaac 
4778040c1f3SToby Isaac .seealso: DMPlexComputeL2Diff()
4788040c1f3SToby Isaac @*/
479ad917190SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
4808040c1f3SToby Isaac {
4818040c1f3SToby Isaac   Vec            localX;
4828040c1f3SToby Isaac   PetscErrorCode ierr;
4838040c1f3SToby Isaac 
4848040c1f3SToby Isaac   PetscFunctionBegin;
4858040c1f3SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4868040c1f3SToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
4878040c1f3SToby Isaac   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
4888040c1f3SToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
4898040c1f3SToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
4908040c1f3SToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
491cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
492cb1e1211SMatthew G Knepley }
493cb1e1211SMatthew G Knepley 
494cb1e1211SMatthew G Knepley #undef __FUNCT__
4950f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
4963bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX)
4970f2d7e86SMatthew G. Knepley {
4980f2d7e86SMatthew G. Knepley   DM                dmAux;
499d5396abcSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
5000f2d7e86SMatthew G. Knepley   Vec               A;
501d5396abcSMatthew G. Knepley   PetscSection      section, sectionAux = NULL;
502b793a5ffSMatthew G. Knepley   PetscDualSpace   *sp;
503b793a5ffSMatthew G. Knepley   PetscInt         *Ncf;
504326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
505d5396abcSMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ;
5069ac3fadcSMatthew G. Knepley   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight;
5070f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
5080f2d7e86SMatthew G. Knepley 
5090f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
510048b7b1eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
511048b7b1eSToby Isaac   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
5122764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
513c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5140f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5150f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
516b793a5ffSMatthew G. Knepley   ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr);
5170f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5182764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
5192764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
5202764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
5210f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
5220f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
5230f2d7e86SMatthew G. Knepley   if (dmAux) {
5242764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
525326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
5262764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
5270f2d7e86SMatthew G. Knepley   }
528d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
5290f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
5300f2d7e86SMatthew 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);
5310f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5320f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
5339ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5349ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
5350f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
536326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
537326413afSMatthew G. Knepley 
5388e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
539326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
540326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5410f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
542d5396abcSMatthew G. Knepley       PetscObject  obj;
543d5396abcSMatthew G. Knepley       PetscClassId id;
5443113607cSMatthew G. Knepley 
545d5396abcSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
546d5396abcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
547d5396abcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
548d5396abcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
549d5396abcSMatthew G. Knepley 
550b793a5ffSMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
551b793a5ffSMatthew G. Knepley         ierr  = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
552b793a5ffSMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr);
553d5396abcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
554d5396abcSMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
555d5396abcSMatthew G. Knepley 
556b793a5ffSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr);
557302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
558e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
559d5396abcSMatthew G. Knepley       }
560b793a5ffSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
5610f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
5620f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
5630f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
5640f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
5650f2d7e86SMatthew G. Knepley 
5660f2d7e86SMatthew G. Knepley         if (funcs[f]) {
567b793a5ffSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr);
5680f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
5690f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
5700f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
571326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
572326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
5733bc3b0a0SMatthew G. Knepley             (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]);
5740f2d7e86SMatthew G. Knepley           }
5750f2d7e86SMatthew G. Knepley         } else {
576b793a5ffSMatthew G. Knepley           for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0;
5770f2d7e86SMatthew G. Knepley         }
578b793a5ffSMatthew G. Knepley         v += Ncf[f];
5790f2d7e86SMatthew G. Knepley       }
5800f2d7e86SMatthew G. Knepley     }
581326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
582326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5830f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
5840f2d7e86SMatthew G. Knepley   }
5850f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5860f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
587b793a5ffSMatthew G. Knepley   for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
588b793a5ffSMatthew G. Knepley   ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr);
5890f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5900f2d7e86SMatthew G. Knepley }
5910f2d7e86SMatthew G. Knepley 
5920f2d7e86SMatthew G. Knepley #undef __FUNCT__
593d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal"
594ad917190SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX)
59555f2e967SMatthew G. Knepley {
596ad917190SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59755f2e967SMatthew G. Knepley   void            **ctxs;
598d7ddef95SMatthew G. Knepley   PetscInt          numFields;
599d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
600d7ddef95SMatthew G. Knepley 
601d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
602d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
603d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
604d7ddef95SMatthew G. Knepley   funcs[field] = func;
605d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
606d7ddef95SMatthew G. Knepley   ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
607d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
608d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
609d7ddef95SMatthew G. Knepley }
610d7ddef95SMatthew G. Knepley 
611d7ddef95SMatthew G. Knepley #undef __FUNCT__
612d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal"
6130e0f25f2SMatthew G. Knepley /* This ignores numcomps/comps */
614d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad,
615d7ddef95SMatthew 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)
616d7ddef95SMatthew G. Knepley {
61761f58d28SMatthew G. Knepley   PetscDS            prob;
618da97024aSMatthew G. Knepley   PetscSF            sf;
619d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
620d7ddef95SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *grad;
621da97024aSMatthew G. Knepley   const PetscInt    *leaves;
622d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
623520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
624d7ddef95SMatthew G. Knepley   PetscErrorCode     ierr;
625d7ddef95SMatthew G. Knepley 
626d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
627da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
628da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
629da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
630d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
631d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
63261f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
633d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
634d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
635d7ddef95SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
636d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
637d7ddef95SMatthew G. Knepley   if (Grad) {
638c0a6632aSMatthew G. Knepley     PetscFV fv;
639c0a6632aSMatthew G. Knepley 
640c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
641d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
642d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
643d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
644d7ddef95SMatthew G. Knepley     ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
645d7ddef95SMatthew G. Knepley   }
646d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
647d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
648d7ddef95SMatthew G. Knepley     IS              faceIS;
649d7ddef95SMatthew G. Knepley     const PetscInt *faces;
650d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
651d7ddef95SMatthew G. Knepley 
652d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
653d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
654d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
655d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
656d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
657d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
658d7ddef95SMatthew G. Knepley       const PetscFVFaceGeom *fg;
659d7ddef95SMatthew G. Knepley 
660d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
661da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
662da97024aSMatthew G. Knepley       if (loc >= 0) continue;
663d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
664d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
665d7ddef95SMatthew G. Knepley       if (Grad) {
666d7ddef95SMatthew G. Knepley         const PetscFVCellGeom *cg;
667d7ddef95SMatthew G. Knepley         const PetscScalar     *cx, *cgrad;
668d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
669d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
670d7ddef95SMatthew G. Knepley         PetscInt               d;
671d7ddef95SMatthew G. Knepley 
672d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
673d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
674d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
675520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
676d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
677d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
678520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
679d7ddef95SMatthew G. Knepley       } else {
680d7ddef95SMatthew G. Knepley         const PetscScalar *xI;
681d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
682d7ddef95SMatthew G. Knepley 
683d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
684520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
685520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr);
686d7ddef95SMatthew G. Knepley       }
687d7ddef95SMatthew G. Knepley     }
688d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
689d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
690d7ddef95SMatthew G. Knepley   }
691d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
692d7ddef95SMatthew G. Knepley   if (Grad) {
693d7ddef95SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
694d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
695d7ddef95SMatthew G. Knepley   }
696d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
697d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
698d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
699d7ddef95SMatthew G. Knepley }
700d7ddef95SMatthew G. Knepley 
701d7ddef95SMatthew G. Knepley #undef __FUNCT__
702d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues"
703d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
704d7ddef95SMatthew G. Knepley {
705d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
70655f2e967SMatthew G. Knepley   PetscErrorCode ierr;
70755f2e967SMatthew G. Knepley 
70855f2e967SMatthew G. Knepley   PetscFunctionBegin;
70955f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
710d7ddef95SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
711d7ddef95SMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
712d7ddef95SMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
713d7ddef95SMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
71455f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
71555f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
71655f2e967SMatthew G. Knepley     PetscBool       isEssential;
717d7ddef95SMatthew G. Knepley     const char     *labelname;
718d7ddef95SMatthew G. Knepley     DMLabel         label;
719d7ddef95SMatthew G. Knepley     PetscInt        field;
720d7ddef95SMatthew G. Knepley     PetscObject     obj;
721d7ddef95SMatthew G. Knepley     PetscClassId    id;
72255f2e967SMatthew G. Knepley     void          (*func)();
723d7ddef95SMatthew G. Knepley     PetscInt        numids;
724d7ddef95SMatthew G. Knepley     const PetscInt *ids;
72555f2e967SMatthew G. Knepley     void           *ctx;
72655f2e967SMatthew G. Knepley 
7270e0f25f2SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, NULL, NULL, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
728d7ddef95SMatthew G. Knepley     if (!isEssential) continue;
72963d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
730d7ddef95SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
731d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
732d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
733ad917190SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (PetscErrorCode (*)(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
734d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
73543ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
736d7ddef95SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM,
737d7ddef95SMatthew G. Knepley                                                      field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
738d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
73955f2e967SMatthew G. Knepley   }
74055f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
74155f2e967SMatthew G. Knepley }
74255f2e967SMatthew G. Knepley 
743cb1e1211SMatthew G Knepley #undef __FUNCT__
744cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
745cb1e1211SMatthew G Knepley /*@C
746cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
747cb1e1211SMatthew G Knepley 
748cb1e1211SMatthew G Knepley   Input Parameters:
749cb1e1211SMatthew G Knepley + dm    - The DM
750cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
75151259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
752cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
753cb1e1211SMatthew G Knepley 
754cb1e1211SMatthew G Knepley   Output Parameter:
755cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
756cb1e1211SMatthew G Knepley 
757cb1e1211SMatthew G Knepley   Level: developer
758cb1e1211SMatthew G Knepley 
75915496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff()
760878cb397SSatish Balay @*/
761ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
762cb1e1211SMatthew G Knepley {
763cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
764cb1e1211SMatthew G Knepley   PetscSection     section;
765c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
766cb1e1211SMatthew G Knepley   Vec              localX;
76715496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
768cb1e1211SMatthew G Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
769cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
77015496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7719ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
772cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
773cb1e1211SMatthew G Knepley 
774cb1e1211SMatthew G Knepley   PetscFunctionBegin;
775c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
776cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
777cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
778cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
779cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
780cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
781cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
78215496722SMatthew G. Knepley     PetscObject  obj;
78315496722SMatthew G. Knepley     PetscClassId id;
784c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
785c5bbbd5bSMatthew G. Knepley 
78615496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
78715496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
78815496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
78915496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
79015496722SMatthew G. Knepley 
7910f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7920f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
79315496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
79415496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
79515496722SMatthew G. Knepley 
79615496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
79715496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
79815496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
799c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
800cb1e1211SMatthew G Knepley   }
80115496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8020f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
80315496722SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
804cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8059ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8069ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
807cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
808a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
809cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
810cb1e1211SMatthew G Knepley 
8118e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
812cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
813cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
814cb1e1211SMatthew G Knepley 
81515496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
81615496722SMatthew G. Knepley       PetscObject  obj;
81715496722SMatthew G. Knepley       PetscClassId id;
818c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
81915496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
820cb1e1211SMatthew G Knepley 
82115496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
82215496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
82315496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
82415496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
82515496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
826cb1e1211SMatthew G Knepley       if (debug) {
827cb1e1211SMatthew G Knepley         char title[1024];
828cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
82915496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
830cb1e1211SMatthew G Knepley       }
831cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
83215496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
833be7a662bSMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, Nc, funcVal, ctx);CHKERRQ(ierr);
83415496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83515496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83615496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
83715496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
83815496722SMatthew 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);}
83915496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
840cb1e1211SMatthew G Knepley         }
841cb1e1211SMatthew G Knepley       }
84215496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
843cb1e1211SMatthew G Knepley     }
844cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
845cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
846cb1e1211SMatthew G Knepley     localDiff += elemDiff;
847cb1e1211SMatthew G Knepley   }
84815496722SMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
849cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
850a529e722SBarry Smith   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
851cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
852cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
853cb1e1211SMatthew G Knepley }
854cb1e1211SMatthew G Knepley 
855cb1e1211SMatthew G Knepley #undef __FUNCT__
85640e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
85740e14135SMatthew G. Knepley /*@C
85840e14135SMatthew G. Knepley   DMPlexComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
85940e14135SMatthew G. Knepley 
86040e14135SMatthew G. Knepley   Input Parameters:
86140e14135SMatthew G. Knepley + dm    - The DM
86240e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
86351259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
86440e14135SMatthew G. Knepley . X     - The coefficient vector u_h
86540e14135SMatthew G. Knepley - n     - The vector to project along
86640e14135SMatthew G. Knepley 
86740e14135SMatthew G. Knepley   Output Parameter:
86840e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
86940e14135SMatthew G. Knepley 
87040e14135SMatthew G. Knepley   Level: developer
87140e14135SMatthew G. Knepley 
87240e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
87340e14135SMatthew G. Knepley @*/
874ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
875cb1e1211SMatthew G Knepley {
87640e14135SMatthew G. Knepley   const PetscInt  debug = 0;
877cb1e1211SMatthew G Knepley   PetscSection    section;
87840e14135SMatthew G. Knepley   PetscQuadrature quad;
87940e14135SMatthew G. Knepley   Vec             localX;
88040e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
88140e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
88240e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
8839ac3fadcSMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp;
884cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
885cb1e1211SMatthew G Knepley 
886cb1e1211SMatthew G Knepley   PetscFunctionBegin;
887c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
88840e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
88940e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
89040e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89140e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
89240e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
893652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8940f2d7e86SMatthew G. Knepley     PetscFE  fe;
89540e14135SMatthew G. Knepley     PetscInt Nc;
896652b88e8SMatthew G. Knepley 
8970f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8980f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8990f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
90040e14135SMatthew G. Knepley     numComponents += Nc;
901652b88e8SMatthew G. Knepley   }
90240e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
90340e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
90440e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9059ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
9069ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
90740e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
90840e14135SMatthew G. Knepley     PetscScalar *x = NULL;
90940e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
910652b88e8SMatthew G. Knepley 
9118e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
91240e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
91340e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
91440e14135SMatthew G. Knepley 
91540e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
9160f2d7e86SMatthew G. Knepley       PetscFE          fe;
91751259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
91821454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
91940e14135SMatthew G. Knepley       PetscReal       *basisDer;
92021454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
92140e14135SMatthew G. Knepley 
9220f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
92321454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
9240f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
9250f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
9260f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
92740e14135SMatthew G. Knepley       if (debug) {
92840e14135SMatthew G. Knepley         char title[1024];
92940e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
93040e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
931652b88e8SMatthew G. Knepley       }
93240e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
93340e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
93440e14135SMatthew G. Knepley           coords[d] = v0[d];
93540e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
93640e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
937652b88e8SMatthew G. Knepley           }
93840e14135SMatthew G. Knepley         }
939ad917190SMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr);
94040e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
94140e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
94240e14135SMatthew G. Knepley 
94340e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
94440e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
94540e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
94640e14135SMatthew G. Knepley 
94740e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
94840e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
94940e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
95040e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
95140e14135SMatthew G. Knepley               }
95240e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
95340e14135SMatthew G. Knepley             }
95440e14135SMatthew G. Knepley           }
95540e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
95640e14135SMatthew 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);}
95740e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
95840e14135SMatthew G. Knepley         }
95940e14135SMatthew G. Knepley       }
96040e14135SMatthew G. Knepley       comp        += Ncomp;
96140e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
96240e14135SMatthew G. Knepley     }
96340e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
96440e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
96540e14135SMatthew G. Knepley     localDiff += elemDiff;
96640e14135SMatthew G. Knepley   }
96740e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
96840e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
969a529e722SBarry Smith   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
97040e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
971cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
972cb1e1211SMatthew G Knepley }
973cb1e1211SMatthew G Knepley 
974a0845e3aSMatthew G. Knepley #undef __FUNCT__
97573d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
97615496722SMatthew G. Knepley /*@C
97715496722SMatthew G. Knepley   DMPlexComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
97815496722SMatthew G. Knepley 
97915496722SMatthew G. Knepley   Input Parameters:
98015496722SMatthew G. Knepley + dm    - The DM
98115496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component
98215496722SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
98315496722SMatthew G. Knepley - X     - The coefficient vector u_h
98415496722SMatthew G. Knepley 
98515496722SMatthew G. Knepley   Output Parameter:
98615496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2
98715496722SMatthew G. Knepley 
98815496722SMatthew G. Knepley   Level: developer
98915496722SMatthew G. Knepley 
99015496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff()
99115496722SMatthew G. Knepley @*/
992ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
99373d901b8SMatthew G. Knepley {
99473d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
99573d901b8SMatthew G. Knepley   PetscSection     section;
99673d901b8SMatthew G. Knepley   PetscQuadrature  quad;
99773d901b8SMatthew G. Knepley   Vec              localX;
99815496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
99973d901b8SMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
100073d901b8SMatthew G. Knepley   PetscReal       *localDiff;
100115496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
10029ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
100373d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
100473d901b8SMatthew G. Knepley 
100573d901b8SMatthew G. Knepley   PetscFunctionBegin;
1006c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
100773d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
100873d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
100973d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
101073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
101173d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
101273d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
101315496722SMatthew G. Knepley     PetscObject  obj;
101415496722SMatthew G. Knepley     PetscClassId id;
101573d901b8SMatthew G. Knepley     PetscInt     Nc;
101673d901b8SMatthew G. Knepley 
101715496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
101815496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
101915496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
102015496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
102115496722SMatthew G. Knepley 
10220f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10230f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
102415496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
102515496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
102615496722SMatthew G. Knepley 
102715496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
102815496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
102915496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
103073d901b8SMatthew G. Knepley     numComponents += Nc;
103173d901b8SMatthew G. Knepley   }
103215496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
10330f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
103415496722SMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
103573d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10369ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10379ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
103873d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
103973d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
104073d901b8SMatthew G. Knepley 
10418e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
104273d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
104373d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
104473d901b8SMatthew G. Knepley 
104515496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
104615496722SMatthew G. Knepley       PetscObject  obj;
104715496722SMatthew G. Knepley       PetscClassId id;
104873d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
104915496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
105073d901b8SMatthew G. Knepley 
105115496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
105215496722SMatthew G. Knepley 
105315496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
105415496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
105515496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
105615496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
105715496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
105873d901b8SMatthew G. Knepley       if (debug) {
105973d901b8SMatthew G. Knepley         char title[1024];
106073d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
106115496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
106273d901b8SMatthew G. Knepley       }
106373d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
106415496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
1065ad917190SMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, numFields, funcVal, ctx);CHKERRQ(ierr);
106615496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106715496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106815496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
106915496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
107015496722SMatthew 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);}
107115496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
107273d901b8SMatthew G. Knepley         }
107373d901b8SMatthew G. Knepley       }
107415496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
107573d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
107673d901b8SMatthew G. Knepley     }
107773d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
107873d901b8SMatthew G. Knepley   }
107973d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1080a529e722SBarry Smith   ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
108173d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
108215496722SMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
108373d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
108473d901b8SMatthew G. Knepley }
108573d901b8SMatthew G. Knepley 
108673d901b8SMatthew G. Knepley #undef __FUNCT__
108773d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
108873d901b8SMatthew G. Knepley /*@
108973d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
109073d901b8SMatthew G. Knepley 
109173d901b8SMatthew G. Knepley   Input Parameters:
109273d901b8SMatthew G. Knepley + dm - The mesh
109373d901b8SMatthew G. Knepley . X  - Local input vector
109473d901b8SMatthew G. Knepley - user - The user context
109573d901b8SMatthew G. Knepley 
109673d901b8SMatthew G. Knepley   Output Parameter:
109773d901b8SMatthew G. Knepley . integral - Local integral for each field
109873d901b8SMatthew G. Knepley 
109973d901b8SMatthew G. Knepley   Level: developer
110073d901b8SMatthew G. Knepley 
110173d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
110273d901b8SMatthew G. Knepley @*/
11030f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
110473d901b8SMatthew G. Knepley {
110573d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
110673d901b8SMatthew G. Knepley   DM                dmAux;
110773d901b8SMatthew G. Knepley   Vec               localX, A;
11082764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
110973d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
1110bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
111173d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
1112c1f031eeSMatthew G. Knepley   PetscReal        *lintegral, *vol;
11139ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c;
11140f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
111573d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
111673d901b8SMatthew G. Knepley 
111773d901b8SMatthew G. Knepley   PetscFunctionBegin;
1118c1f031eeSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
111973d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
112073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
112173d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1122c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
112373d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
11242764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
11252764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
112673d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
112773d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11289ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11299ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
113073d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
113173d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
113273d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
113373d901b8SMatthew G. Knepley   if (dmAux) {
113473d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
11352764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
11362764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
113773d901b8SMatthew G. Knepley   }
1138d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1139c1f031eeSMatthew G. Knepley   ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr);
11400f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1141c1f031eeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;}
114273d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
114373d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
114473d901b8SMatthew G. Knepley     PetscInt     i;
114573d901b8SMatthew G. Knepley 
1146bbce034cSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr);
1147c1f031eeSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr);
1148bbce034cSMatthew 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);
114973d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
11500f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
115173d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
115273d901b8SMatthew G. Knepley     if (dmAux) {
115373d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11540f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
115573d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
115673d901b8SMatthew G. Knepley     }
115773d901b8SMatthew G. Knepley   }
115873d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1159c1f031eeSMatthew G. Knepley     PetscObject  obj;
1160c1f031eeSMatthew G. Knepley     PetscClassId id;
1161c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
116273d901b8SMatthew G. Knepley 
1163c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1164c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1165c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1166c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1167c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1168c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1169c1f031eeSMatthew G. Knepley 
11700f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1171c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1172c1f031eeSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1173c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1174c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
117573d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
11760f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
117773d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
117873d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
117973d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
118073d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1181c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr);
1182c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr);
1183c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1184b69edc29SMatthew G. Knepley       /* PetscFV  fv = (PetscFV) obj; */
1185c1f031eeSMatthew G. Knepley       PetscInt       foff;
1186420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1187b69edc29SMatthew G. Knepley       PetscScalar    lint;
1188c1f031eeSMatthew G. Knepley 
1189c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1190c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1191c1f031eeSMatthew G. Knepley       if (obj_func) {
1192c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1193c1f031eeSMatthew G. Knepley           /* TODO: Need full pointwise interpolation and get centroid for x argument */
119430b9ff8bSMatthew G. Knepley           obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint);
1195b69edc29SMatthew G. Knepley           lintegral[f] = PetscRealPart(lint)*vol[c];
119673d901b8SMatthew G. Knepley         }
1197c1f031eeSMatthew G. Knepley       }
1198c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1199c1f031eeSMatthew G. Knepley   }
120073d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
120173d901b8SMatthew G. Knepley   if (mesh->printFEM) {
120273d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
1203c1f031eeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);}
120473d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
120573d901b8SMatthew G. Knepley   }
120673d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1207a529e722SBarry Smith   ierr = MPI_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
1208c1f031eeSMatthew G. Knepley   ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr);
1209c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
121073d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
121173d901b8SMatthew G. Knepley }
121273d901b8SMatthew G. Knepley 
121373d901b8SMatthew G. Knepley #undef __FUNCT__
121468132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorNested"
1215d69c5d34SMatthew G. Knepley /*@
121668132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1217d69c5d34SMatthew G. Knepley 
1218d69c5d34SMatthew G. Knepley   Input Parameters:
1219d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1220d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1221d69c5d34SMatthew G. Knepley - user - The user context
1222d69c5d34SMatthew G. Knepley 
1223d69c5d34SMatthew G. Knepley   Output Parameter:
1224934789fcSMatthew G. Knepley . In  - The interpolation matrix
1225d69c5d34SMatthew G. Knepley 
1226d69c5d34SMatthew G. Knepley   Level: developer
1227d69c5d34SMatthew G. Knepley 
122868132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1229d69c5d34SMatthew G. Knepley @*/
123068132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
1231d69c5d34SMatthew G. Knepley {
1232d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1233d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
12342764a2aaSMatthew G. Knepley   PetscDS           prob;
1235d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
123697c42addSMatthew G. Knepley   PetscFV          *fvRef;
1237d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1238d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1239d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
12409ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
12410f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1242d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1243d69c5d34SMatthew G. Knepley 
1244d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1245d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1246c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1247d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1248d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1249d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1250d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1251d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1252d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
12539ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12549ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
12552764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
125697c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1257d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
125897c42addSMatthew G. Knepley     PetscObject  obj;
125997c42addSMatthew G. Knepley     PetscClassId id;
1260aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1261d69c5d34SMatthew G. Knepley 
126297c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
126397c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
126497c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
126597c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
126697c42addSMatthew G. Knepley 
12670f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1268d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
12690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
127097c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
127197c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
127297c42addSMatthew G. Knepley       PetscDualSpace Q;
127397c42addSMatthew G. Knepley 
127497c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
127597c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
127697c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
127797c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
127897c42addSMatthew G. Knepley     }
12790f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1280d69c5d34SMatthew G. Knepley   }
12812764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
12820f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
12830f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1284d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1285d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1286d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1287d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1288d69c5d34SMatthew G. Knepley     PetscReal       *points;
1289d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1290d69c5d34SMatthew G. Knepley 
1291d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
129297c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1293d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
12940f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
129597c42addSMatthew G. Knepley     } else {
129697c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
129797c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
129897c42addSMatthew G. Knepley     }
1299d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1300d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1301d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1302d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1303d69c5d34SMatthew G. Knepley       npoints += Np;
1304d69c5d34SMatthew G. Knepley     }
1305d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1306d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1307d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1308d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1309d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1310d69c5d34SMatthew G. Knepley     }
1311d69c5d34SMatthew G. Knepley 
1312d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
131397c42addSMatthew G. Knepley       PetscObject  obj;
131497c42addSMatthew G. Knepley       PetscClassId id;
1315d69c5d34SMatthew G. Knepley       PetscReal   *B;
1316aa7890ccSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j;
1317d69c5d34SMatthew G. Knepley 
131897c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
131997c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
132097c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
132197c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1322d69c5d34SMatthew G. Knepley 
1323d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
13240f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
13250f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1326ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1327ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
13287c927364SMatthew 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);
13290f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1330d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1331d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1332d69c5d34SMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1333d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
133436a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
13350f2d7e86SMatthew 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];
133636a6d9c0SMatthew G. Knepley               }
1337d69c5d34SMatthew G. Knepley             }
1338d69c5d34SMatthew G. Knepley           }
13390f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1340ffe73a53SMatthew G. Knepley         }
134197c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
134297c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
134397c42addSMatthew G. Knepley 
134497c42addSMatthew G. Knepley         /* Evaluate constant function at points */
134597c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
134697c42addSMatthew G. Knepley         cpdim = 1;
134797c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
134897c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
134997c42addSMatthew 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);
135097c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
135197c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
135297c42addSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
135397c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
135497c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
135597c42addSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p];
135697c42addSMatthew G. Knepley               }
135797c42addSMatthew G. Knepley             }
135897c42addSMatthew G. Knepley           }
135997c42addSMatthew G. Knepley         }
136097c42addSMatthew G. Knepley       }
136136a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1362d69c5d34SMatthew G. Knepley     }
1363d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1364549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1365d69c5d34SMatthew G. Knepley   }
13660f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
13677f5b169aSMatthew G. Knepley   /* Preallocate matrix */
13687f5b169aSMatthew G. Knepley   {
13697f5b169aSMatthew G. Knepley     PetscHashJK ht;
13707f5b169aSMatthew G. Knepley     PetscLayout rLayout;
13717f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
13727f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
13737f5b169aSMatthew G. Knepley 
13747f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
13757f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
13767f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
13777f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
13787f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
13797f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
13807f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
13817f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
13827f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
13837f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
13847f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
13857f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
13867f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
13877f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
13887f5b169aSMatthew G. Knepley 
13897f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
13907f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
13917f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
13927f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
13937f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
13947f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
13957f5b169aSMatthew G. Knepley           if (missing) {
13967f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
13977f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
13987f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
13997f5b169aSMatthew G. Knepley           }
14007f5b169aSMatthew G. Knepley         }
14017f5b169aSMatthew G. Knepley       }
14027f5b169aSMatthew G. Knepley     }
14037f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
14047f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
14057f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
14067f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
14077f5b169aSMatthew G. Knepley   }
14087f5b169aSMatthew G. Knepley   /* Fill matrix */
14097f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1410d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1411934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1412d69c5d34SMatthew G. Knepley   }
1413549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
141497c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1415549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1416934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1417934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1418d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1419d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1420934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1421934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1422d69c5d34SMatthew G. Knepley   }
1423d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1424d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1425d69c5d34SMatthew G. Knepley }
14266c73c22cSMatthew G. Knepley 
14276c73c22cSMatthew G. Knepley #undef __FUNCT__
142868132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorGeneral"
142968132eb9SMatthew G. Knepley /*@
143068132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
143168132eb9SMatthew G. Knepley 
143268132eb9SMatthew G. Knepley   Input Parameters:
143368132eb9SMatthew G. Knepley + dmf  - The fine mesh
143468132eb9SMatthew G. Knepley . dmc  - The coarse mesh
143568132eb9SMatthew G. Knepley - user - The user context
143668132eb9SMatthew G. Knepley 
143768132eb9SMatthew G. Knepley   Output Parameter:
143868132eb9SMatthew G. Knepley . In  - The interpolation matrix
143968132eb9SMatthew G. Knepley 
144068132eb9SMatthew G. Knepley   Level: developer
144168132eb9SMatthew G. Knepley 
144268132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
144368132eb9SMatthew G. Knepley @*/
144468132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
14454ef9d792SMatthew G. Knepley {
14464ef9d792SMatthew G. Knepley   PetscDS        prob;
14474ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
14484ef9d792SMatthew G. Knepley   PetscHashJK    ht;
14494ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
14504ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
14514ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
14524ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
14534ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
14544ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
14554ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
14564ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
14574ef9d792SMatthew G. Knepley 
14584ef9d792SMatthew G. Knepley   PetscFunctionBegin;
14594ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
14604ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
14614ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
14624ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
14634ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
14644ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
14654ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
14664ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
14674ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
14684ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
14694ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
14704ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
14714ef9d792SMatthew G. Knepley   ierr = PetscMalloc1(totDim*totDim, &elemMat);CHKERRQ(ierr);
14724ef9d792SMatthew G. Knepley 
14734ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
14744ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
14754ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
14764ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
14774ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
14784ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
14794ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
14804ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
14814ef9d792SMatthew G. Knepley   ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
14824ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
14834ef9d792SMatthew G. Knepley     PetscObject      obj;
14844ef9d792SMatthew G. Knepley     PetscClassId     id;
14854ef9d792SMatthew G. Knepley     PetscDualSpace   Q;
14864ef9d792SMatthew G. Knepley     PetscQuadrature  f;
1487*17f047d8SMatthew G. Knepley     const PetscReal *qpoints;
1488*17f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
14894ef9d792SMatthew G. Knepley 
14904ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
14914ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
14924ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
14934ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
14944ef9d792SMatthew G. Knepley 
14954ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
14964ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
14974ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
14984ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
14994ef9d792SMatthew G. Knepley 
15004ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
15014ef9d792SMatthew G. Knepley       Nc   = 1;
15024ef9d792SMatthew G. Knepley     }
15034ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
15044ef9d792SMatthew G. Knepley     /* For each fine grid cell */
15054ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
15064ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
15074ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
15084ef9d792SMatthew G. Knepley 
15094ef9d792SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
15104ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
15114ef9d792SMatthew 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);
15124ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
15134ef9d792SMatthew G. Knepley         Vec             pointVec;
15144ef9d792SMatthew G. Knepley         PetscScalar    *pV;
15154ef9d792SMatthew G. Knepley         IS              coarseCellIS;
15164ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
15174ef9d792SMatthew G. Knepley         PetscInt        numCoarseCells, q, r, c;
15184ef9d792SMatthew G. Knepley 
15194ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
15204ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
15214ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
15224ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
15234ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
15244ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
15254ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
15264ef9d792SMatthew G. Knepley           /* Transform point to real space */
15274ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
15284ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
15294ef9d792SMatthew G. Knepley         }
15304ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
15314ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
15324ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
15334ef9d792SMatthew G. Knepley         ierr = ISViewFromOptions(coarseCellIS, NULL, "-interp_is_view");CHKERRQ(ierr);
15344ef9d792SMatthew G. Knepley         /* Update preallocation info */
15354ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
15364ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
15374ef9d792SMatthew G. Knepley         for (r = 0; r < Nc; ++r) {
15384ef9d792SMatthew G. Knepley           PetscHashJKKey  key;
15394ef9d792SMatthew G. Knepley           PetscHashJKIter missing, iter;
15404ef9d792SMatthew G. Knepley 
15414ef9d792SMatthew G. Knepley           key.j = findices[i*Nc+r];
15424ef9d792SMatthew G. Knepley           if (key.j < 0) continue;
15434ef9d792SMatthew G. Knepley           /* Get indices for coarse elements */
15444ef9d792SMatthew G. Knepley           for (ccell = 0; ccell < numCoarseCells; ++ccell) {
15454ef9d792SMatthew G. Knepley             ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
15464ef9d792SMatthew G. Knepley             for (c = 0; c < numCIndices; ++c) {
15474ef9d792SMatthew G. Knepley               key.k = cindices[c];
15484ef9d792SMatthew G. Knepley               if (key.k < 0) continue;
15494ef9d792SMatthew G. Knepley               ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
15504ef9d792SMatthew G. Knepley               if (missing) {
15514ef9d792SMatthew G. Knepley                 ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
15524ef9d792SMatthew G. Knepley                 if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
15534ef9d792SMatthew G. Knepley                 else                                     ++onz[key.j-rStart];
15544ef9d792SMatthew G. Knepley               }
15554ef9d792SMatthew G. Knepley             }
15564ef9d792SMatthew G. Knepley             ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
15574ef9d792SMatthew G. Knepley           }
15584ef9d792SMatthew G. Knepley         }
15594ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
15604ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
15614ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
15624ef9d792SMatthew G. Knepley       }
15634ef9d792SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
15644ef9d792SMatthew G. Knepley     }
15654ef9d792SMatthew G. Knepley   }
15664ef9d792SMatthew G. Knepley   ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
15674ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
15684ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
15694ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
15704ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
15714ef9d792SMatthew G. Knepley     PetscObject      obj;
15724ef9d792SMatthew G. Knepley     PetscClassId     id;
15734ef9d792SMatthew G. Knepley     PetscDualSpace   Q;
15744ef9d792SMatthew G. Knepley     PetscQuadrature  f;
15754ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1576*17f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
15774ef9d792SMatthew G. Knepley 
15784ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
15794ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15804ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
15814ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
15824ef9d792SMatthew G. Knepley 
15834ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
15844ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
15854ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
15864ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
15874ef9d792SMatthew G. Knepley 
15884ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
15894ef9d792SMatthew G. Knepley       Nc   = 1;
15904ef9d792SMatthew G. Knepley     }
15914ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
15924ef9d792SMatthew G. Knepley     /* For each fine grid cell */
15934ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
15944ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
15954ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
15964ef9d792SMatthew G. Knepley 
15974ef9d792SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
15984ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
15994ef9d792SMatthew 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);
16004ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
16014ef9d792SMatthew G. Knepley         Vec             pointVec;
16024ef9d792SMatthew G. Knepley         PetscScalar    *pV;
16034ef9d792SMatthew G. Knepley         IS              coarseCellIS;
16044ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
1605*17f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
16064ef9d792SMatthew G. Knepley 
16074ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
16084ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
16094ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, &qweights);CHKERRQ(ierr);
16104ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
16114ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
16124ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
16134ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
16144ef9d792SMatthew G. Knepley           /* Transform point to real space */
16154ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
16164ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
16174ef9d792SMatthew G. Knepley         }
16184ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
16194ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
16204ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
16214ef9d792SMatthew G. Knepley         /* Update preallocation info */
16224ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
16234ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
16244ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
16254ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
1626826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
1627826eb36dSMatthew G. Knepley 
16284ef9d792SMatthew G. Knepley           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
16294ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
1630f1ac73f2SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell], NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
1631826eb36dSMatthew G. Knepley           CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x);
1632826eb36dSMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[ccell*dim+d] = pVReal[d];
16334ef9d792SMatthew G. Knepley 
16344ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
16354ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
16364ef9d792SMatthew G. Knepley             PetscReal *B;
16374ef9d792SMatthew G. Knepley 
16384ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
16394ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
16404ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
16414ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
16424ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
16434ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = B[j*Nc + c]*qweights[ccell];
16444ef9d792SMatthew G. Knepley             }
16454ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
16464ef9d792SMatthew G. Knepley           } else {
16474ef9d792SMatthew G. Knepley             cpdim = 1;
16484ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
16494ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = 1.0*qweights[ccell];
16504ef9d792SMatthew G. Knepley             }
16514ef9d792SMatthew G. Knepley           }
16524ef9d792SMatthew G. Knepley           /* Update interpolator */
16534ef9d792SMatthew G. Knepley           ierr = MatSetValues(In, Nc, &findices[i*Nc], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
16544ef9d792SMatthew G. Knepley           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
16554ef9d792SMatthew G. Knepley         }
16564ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
16574ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
16584ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
16594ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
16604ef9d792SMatthew G. Knepley       }
16614ef9d792SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
16624ef9d792SMatthew G. Knepley     }
16634ef9d792SMatthew G. Knepley   }
16644ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
16654ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
16664ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
16674ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16684ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
16694ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
16704ef9d792SMatthew G. Knepley }
16714ef9d792SMatthew G. Knepley 
16724ef9d792SMatthew G. Knepley #undef __FUNCT__
16737c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
16747c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
16757c927364SMatthew G. Knepley {
1676e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
16777c927364SMatthew G. Knepley   PetscFE       *feRef;
167897c42addSMatthew G. Knepley   PetscFV       *fvRef;
16797c927364SMatthew G. Knepley   Vec            fv, cv;
16807c927364SMatthew G. Knepley   IS             fis, cis;
16817c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
16827c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
16839ac3fadcSMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, offsetC, offsetF, m;
16847c927364SMatthew G. Knepley   PetscErrorCode ierr;
16857c927364SMatthew G. Knepley 
16867c927364SMatthew G. Knepley   PetscFunctionBegin;
168775a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1688c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
16897c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
16907c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
16917c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
16927c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
16937c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
16947c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
16959ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
16969ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1697e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
169897c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
16997c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
170097c42addSMatthew G. Knepley     PetscObject  obj;
170197c42addSMatthew G. Knepley     PetscClassId id;
1702aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
17037c927364SMatthew G. Knepley 
170497c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
170597c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
170697c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
170797c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
170897c42addSMatthew G. Knepley 
17097c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
17107c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
17117c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
171297c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
171397c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
171497c42addSMatthew G. Knepley       PetscDualSpace Q;
171597c42addSMatthew G. Knepley 
171697c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
171797c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
171897c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
171997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
172097c42addSMatthew G. Knepley     }
17217c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
17227c927364SMatthew G. Knepley   }
1723e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17247c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
17257c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
17267c927364SMatthew G. Knepley     PetscFE        feC;
172797c42addSMatthew G. Knepley     PetscFV        fvC;
17287c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
17297c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
17307c927364SMatthew G. Knepley 
173197c42addSMatthew G. Knepley     if (feRef[field]) {
1732e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
17337c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
17347c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
17357c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
17367c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
17377c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
17387c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
173997c42addSMatthew G. Knepley     } else {
174097c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
174197c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
174297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
174397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
174497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
174597c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
174697c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
174797c42addSMatthew G. Knepley     }
174897c42addSMatthew 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);
17497c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
17507c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
17517c927364SMatthew G. Knepley       const PetscReal *cqpoints;
17527c927364SMatthew G. Knepley       PetscInt         NpC;
175397c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
17547c927364SMatthew G. Knepley 
17557c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
17567c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
175797c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
17587c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
17597c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
17607c927364SMatthew G. Knepley         const PetscReal *fqpoints;
17617c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
17627c927364SMatthew G. Knepley         PetscInt         NpF, comp;
17637c927364SMatthew G. Knepley 
17647c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
17657c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
17667c927364SMatthew G. Knepley         if (NpC != NpF) continue;
17677c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
17687c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
17697c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
17707c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
17717c927364SMatthew G. Knepley         }
177297c42addSMatthew G. Knepley         found = PETSC_TRUE;
17737c927364SMatthew G. Knepley         break;
17747c927364SMatthew G. Knepley       }
177597c42addSMatthew G. Knepley       if (!found) {
177697c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
177797c42addSMatthew G. Knepley         if (fvRef[field]) {
177897c42addSMatthew G. Knepley           PetscInt comp;
177997c42addSMatthew G. Knepley           for (comp = 0; comp < NcC; ++comp) {
178097c42addSMatthew G. Knepley             cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp;
178197c42addSMatthew G. Knepley           }
178297c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
178397c42addSMatthew G. Knepley       }
17847c927364SMatthew G. Knepley     }
17857c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
17867c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
17877c927364SMatthew G. Knepley   }
178897c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
178997c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
17907c927364SMatthew G. Knepley 
17917c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
17927c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
17937c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
17947c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
17957c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1796aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1797aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
17987c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
17997c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
18007c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
18017c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
18027c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
18037c927364SMatthew 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]]);
18047c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
18057c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
18067c927364SMatthew G. Knepley     }
18077c927364SMatthew G. Knepley   }
18087c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
18097c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
18107c927364SMatthew G. Knepley 
18117c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
18127c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
18137c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
18147c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
18157c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
18167c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
18177c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
181875a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1819cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1820cb1e1211SMatthew G Knepley }
1821