xref: /petsc/src/dm/impls/plex/plexfem.c (revision 4ef9d792ac49bb6ab57ba1bee36bef6443e823aa)
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;
135026175e5SToby Isaac   PetscInt       dim, 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);
141cb1e1211SMatthew G Knepley   if (dim == 1) {
142cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
143cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
144cb1e1211SMatthew G Knepley   }
145026175e5SToby Isaac   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
146026175e5SToby Isaac   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
147cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
148cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
149cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
150cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
151cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
152cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
153026175e5SToby Isaac   for (d = 0; d < m; d++) {
154330485fdSToby Isaac     PetscInt ctx[2];
155026175e5SToby Isaac     void    *voidctx = (void *) (&ctx[0]);
156ad917190SMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody;
157cb1e1211SMatthew G Knepley 
158330485fdSToby Isaac     ctx[0] = dim;
159330485fdSToby Isaac     ctx[1] = d;
160026175e5SToby Isaac     ierr = DMPlexProjectFunction(dm, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
161cb1e1211SMatthew G Knepley   }
162cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
163cb1e1211SMatthew G Knepley   /* Orthonormalize system */
164cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
165cb1e1211SMatthew G Knepley     PetscScalar dots[6];
166cb1e1211SMatthew G Knepley 
167cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
168cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
169cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
170cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
171cb1e1211SMatthew G Knepley   }
172cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
173cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
174cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
175cb1e1211SMatthew G Knepley }
176cb1e1211SMatthew G Knepley 
177cb1e1211SMatthew G Knepley #undef __FUNCT__
178b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight"
179b29cfa1cSToby Isaac /*@
180b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
181b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
182b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
183b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
184b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
185b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
186b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
187b29cfa1cSToby Isaac 
188b29cfa1cSToby Isaac   Input Parameters:
189b29cfa1cSToby Isaac + dm - the DMPlex object
190b29cfa1cSToby Isaac - height - the maximum projection height >= 0
191b29cfa1cSToby Isaac 
192b29cfa1cSToby Isaac   Level: advanced
193b29cfa1cSToby Isaac 
194048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
195b29cfa1cSToby Isaac @*/
196b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
197b29cfa1cSToby Isaac {
198b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
199b29cfa1cSToby Isaac 
200b29cfa1cSToby Isaac   PetscFunctionBegin;
201b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
202b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
203b29cfa1cSToby Isaac   PetscFunctionReturn(0);
204b29cfa1cSToby Isaac }
205b29cfa1cSToby Isaac 
206b29cfa1cSToby Isaac #undef __FUNCT__
207b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight"
208b29cfa1cSToby Isaac /*@
209b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
210b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
211b29cfa1cSToby Isaac 
212b29cfa1cSToby Isaac   Input Parameters:
213b29cfa1cSToby Isaac . dm - the DMPlex object
214b29cfa1cSToby Isaac 
215b29cfa1cSToby Isaac   Output Parameters:
216b29cfa1cSToby Isaac . height - the maximum projection height
217b29cfa1cSToby Isaac 
218b29cfa1cSToby Isaac   Level: intermediate
219b29cfa1cSToby Isaac 
220048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
221b29cfa1cSToby Isaac @*/
222b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
223b29cfa1cSToby Isaac {
224b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
225b29cfa1cSToby Isaac 
226b29cfa1cSToby Isaac   PetscFunctionBegin;
227b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
228b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
229b29cfa1cSToby Isaac   PetscFunctionReturn(0);
230b29cfa1cSToby Isaac }
231b29cfa1cSToby Isaac 
232b29cfa1cSToby Isaac #undef __FUNCT__
233a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
234ad917190SMatthew 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)
235a18a7fb9SMatthew G. Knepley {
2367d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
237dda36da7SMatthew G. Knepley   PetscInt       *numComp;
238a18a7fb9SMatthew G. Knepley   PetscSection    section;
239a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
240ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
241dda36da7SMatthew G. Knepley   PetscInt        numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h;
242a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
243a18a7fb9SMatthew G. Knepley 
244a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
2459ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2469ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2479ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2489ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
249c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2507a1a1bd4SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
251a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
252a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
253dda36da7SMatthew G. Knepley   ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr);
2547d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
2557d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
2569ac3fadcSMatthew G. Knepley   if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);}
2579ac3fadcSMatthew G. Knepley   else               {cellsp = sp;}
2587d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
2597d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
260fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
2617d1dd11eSToby Isaac     if (pEnd <= pStart) continue;
2627d1dd11eSToby Isaac     totDim = 0;
263a18a7fb9SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
2649ac3fadcSMatthew G. Knepley       PetscObject  obj;
2659ac3fadcSMatthew G. Knepley       PetscClassId id;
2669ac3fadcSMatthew G. Knepley 
2679ac3fadcSMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2689ac3fadcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2699ac3fadcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
2709ac3fadcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
2719ac3fadcSMatthew G. Knepley 
272dda36da7SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
2737d1dd11eSToby Isaac         if (!h) {
274ee2838f6SToby Isaac           ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
2757d1dd11eSToby Isaac           sp[f] = cellsp[f];
2769ac3fadcSMatthew G. Knepley           ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
2779ac3fadcSMatthew G. Knepley         } else {
2787d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
2797d1dd11eSToby Isaac           if (!sp[f]) continue;
2807d1dd11eSToby Isaac         }
2819ac3fadcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
2829ac3fadcSMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
2839ac3fadcSMatthew G. Knepley 
284dda36da7SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
285302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
286e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
2879ac3fadcSMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
288a18a7fb9SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
289dda36da7SMatthew G. Knepley       totDim += spDim*numComp[f];
290a18a7fb9SMatthew G. Knepley     }
2917d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
2927d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
293d374af2bSToby Isaac     if (!totDim) continue;
294a18a7fb9SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
295ad96f515SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
296ee2838f6SToby Isaac     for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE;
297a18a7fb9SMatthew G. Knepley     for (i = 0; i < numIds; ++i) {
298a18a7fb9SMatthew G. Knepley       IS              pointIS;
299a18a7fb9SMatthew G. Knepley       const PetscInt *points;
300a18a7fb9SMatthew G. Knepley       PetscInt        n, p;
301a18a7fb9SMatthew G. Knepley 
302a18a7fb9SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
303a18a7fb9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
304a18a7fb9SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
305a18a7fb9SMatthew G. Knepley       for (p = 0; p < n; ++p) {
306a18a7fb9SMatthew G. Knepley         const PetscInt    point = points[p];
307e1d0b1adSMatthew G. Knepley         PetscFECellGeom   geom;
308a18a7fb9SMatthew G. Knepley 
3097d1dd11eSToby Isaac         if ((point < pStart) || (point >= pEnd)) continue;
310e1d0b1adSMatthew G. Knepley         ierr          = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
311ee2838f6SToby Isaac         geom.dim      = dim - h;
3127a1a1bd4SToby Isaac         geom.dimEmbed = dimEmbed;
313a18a7fb9SMatthew G. Knepley         for (f = 0, v = 0; f < numFields; ++f) {
314a18a7fb9SMatthew G. Knepley           void * const ctx = ctxs ? ctxs[f] : NULL;
315bf3434eeSMatthew G. Knepley 
3167d1dd11eSToby Isaac           if (!sp[f]) continue;
317a18a7fb9SMatthew G. Knepley           ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
318a18a7fb9SMatthew G. Knepley           for (d = 0; d < spDim; ++d) {
319a18a7fb9SMatthew G. Knepley             if (funcs[f]) {
320dda36da7SMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
321a18a7fb9SMatthew G. Knepley             } else {
322dda36da7SMatthew G. Knepley               for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
323a18a7fb9SMatthew G. Knepley             }
324dda36da7SMatthew G. Knepley             v += numComp[f];
325a18a7fb9SMatthew G. Knepley           }
326a18a7fb9SMatthew G. Knepley         }
327ad96f515SMatthew G. Knepley         ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
328a18a7fb9SMatthew G. Knepley       }
329a18a7fb9SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
330a18a7fb9SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
331a18a7fb9SMatthew G. Knepley     }
3327d1dd11eSToby Isaac     if (h) {
3337d1dd11eSToby Isaac       for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
3347d1dd11eSToby Isaac     }
3357d1dd11eSToby Isaac   }
336a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
337ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
3389ac3fadcSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
339dda36da7SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
3407d1dd11eSToby Isaac   if (maxHeight > 0) {
3417d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
3427d1dd11eSToby Isaac   }
343a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
344a18a7fb9SMatthew G. Knepley }
345a18a7fb9SMatthew G. Knepley 
346a18a7fb9SMatthew G. Knepley #undef __FUNCT__
347cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
348ad917190SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
349cb1e1211SMatthew G Knepley {
3507d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
35115496722SMatthew G. Knepley   PetscInt       *numComp;
35272f94c41SMatthew G. Knepley   PetscSection    section;
35372f94c41SMatthew G. Knepley   PetscScalar    *values;
354b793a5ffSMatthew G. Knepley   PetscInt        Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight;
355cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
356cb1e1211SMatthew G Knepley 
357cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3589ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3599ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
3609ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
3619ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
362cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
363b793a5ffSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
364b793a5ffSMatthew G. Knepley   ierr = PetscMalloc2(Nf, &sp, Nf, &numComp);CHKERRQ(ierr);
3657d1dd11eSToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
366ee2838f6SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3677d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
3687d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
3697d1dd11eSToby Isaac   if (maxHeight > 0) {
370b793a5ffSMatthew G. Knepley     ierr = PetscMalloc1(Nf,&cellsp);CHKERRQ(ierr);
3717d1dd11eSToby Isaac   }
372048b7b1eSToby Isaac   else {
373048b7b1eSToby Isaac     cellsp = sp;
374048b7b1eSToby Isaac   }
3757d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
3767d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
377fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
378048b7b1eSToby Isaac     if (pEnd <= pStart) continue;
3797d1dd11eSToby Isaac     totDim = 0;
380b793a5ffSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
38115496722SMatthew G. Knepley       PetscObject  obj;
38215496722SMatthew G. Knepley       PetscClassId id;
3830f2d7e86SMatthew G. Knepley 
38415496722SMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
38515496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
38615496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
38715496722SMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
38815496722SMatthew G. Knepley 
38915496722SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
3907d1dd11eSToby Isaac         if (!h) {
3917d1dd11eSToby Isaac           ierr  = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
3927d1dd11eSToby Isaac           sp[f] = cellsp[f];
39315496722SMatthew G. Knepley           ierr  = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
3947d1dd11eSToby Isaac         }
3957d1dd11eSToby Isaac         else {
3967d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
3977d1dd11eSToby Isaac           if (!sp[f]) {
3987d1dd11eSToby Isaac             continue;
3997d1dd11eSToby Isaac           }
4007d1dd11eSToby Isaac         }
40115496722SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
40215496722SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
40315496722SMatthew G. Knepley 
404ee2838f6SToby Isaac         if (h) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP, "Projection height > 0 not supported for finite volume");
40515496722SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
406302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
407e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
40815496722SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
40972f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
41015496722SMatthew G. Knepley       totDim += spDim*numComp[f];
411cb1e1211SMatthew G Knepley     }
4127d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
4137d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
414d374af2bSToby Isaac     if (!totDim) continue;
41572f94c41SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4167d1dd11eSToby Isaac     for (p = pStart; p < pEnd; ++p) {
417e1d0b1adSMatthew G. Knepley       PetscFECellGeom geom;
418cb1e1211SMatthew G Knepley 
41931383a9bSToby Isaac       ierr          = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
420910c25dcSToby Isaac       geom.dim      = dim - h;
4217a1a1bd4SToby Isaac       geom.dimEmbed = dimEmbed;
422b793a5ffSMatthew G. Knepley       for (f = 0, v = 0; f < Nf; ++f) {
423c110b1eeSGeoffrey Irving         void * const ctx = ctxs ? ctxs[f] : NULL;
4240f2d7e86SMatthew G. Knepley 
4257d1dd11eSToby Isaac         if (!sp[f]) continue;
42672f94c41SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
42772f94c41SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
428120386c5SMatthew G. Knepley           if (funcs[f]) {
429e1d0b1adSMatthew G. Knepley             ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
430120386c5SMatthew G. Knepley           } else {
43115496722SMatthew G. Knepley             for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
432120386c5SMatthew G. Knepley           }
43315496722SMatthew G. Knepley           v += numComp[f];
434cb1e1211SMatthew G Knepley         }
435cb1e1211SMatthew G Knepley       }
4367d1dd11eSToby Isaac       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
437cb1e1211SMatthew G Knepley     }
43872f94c41SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
439ee2838f6SToby Isaac     if (h || !maxHeight) {
440b793a5ffSMatthew G. Knepley       for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
4417d1dd11eSToby Isaac     }
4427d1dd11eSToby Isaac   }
44315496722SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
4447d1dd11eSToby Isaac   if (maxHeight > 0) {
445b793a5ffSMatthew G. Knepley     for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);}
4467d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
4477d1dd11eSToby Isaac   }
448cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
449cb1e1211SMatthew G Knepley }
450cb1e1211SMatthew G Knepley 
451cb1e1211SMatthew G Knepley #undef __FUNCT__
4528040c1f3SToby Isaac #define __FUNCT__ "DMPlexProjectFunction"
4538040c1f3SToby Isaac /*@C
4548040c1f3SToby Isaac   DMPlexProjectFunction - This projects the given function into the function space provided.
4558040c1f3SToby Isaac 
4568040c1f3SToby Isaac   Input Parameters:
4578040c1f3SToby Isaac + dm      - The DM
4588040c1f3SToby Isaac . funcs   - The coordinate functions to evaluate, one per field
4598040c1f3SToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
4608040c1f3SToby Isaac - mode    - The insertion mode for values
4618040c1f3SToby Isaac 
4628040c1f3SToby Isaac   Output Parameter:
4638040c1f3SToby Isaac . X - vector
4648040c1f3SToby Isaac 
465ad917190SMatthew G. Knepley    Calling sequence of func:
466ad917190SMatthew G. Knepley $    func(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
467ad917190SMatthew G. Knepley 
468ad917190SMatthew G. Knepley +  dim - The spatial dimension
469ad917190SMatthew G. Knepley .  x   - The coordinates
470ad917190SMatthew G. Knepley .  Nf  - The number of fields
471ad917190SMatthew G. Knepley .  u   - The output field values
472ad917190SMatthew G. Knepley -  ctx - optional user-defined function context
473ad917190SMatthew G. Knepley 
4748040c1f3SToby Isaac   Level: developer
4758040c1f3SToby Isaac 
4768040c1f3SToby Isaac .seealso: DMPlexComputeL2Diff()
4778040c1f3SToby Isaac @*/
478ad917190SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
4798040c1f3SToby Isaac {
4808040c1f3SToby Isaac   Vec            localX;
4818040c1f3SToby Isaac   PetscErrorCode ierr;
4828040c1f3SToby Isaac 
4838040c1f3SToby Isaac   PetscFunctionBegin;
4848040c1f3SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4858040c1f3SToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
4868040c1f3SToby Isaac   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
4878040c1f3SToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
4888040c1f3SToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
4898040c1f3SToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
490cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
491cb1e1211SMatthew G Knepley }
492cb1e1211SMatthew G Knepley 
493cb1e1211SMatthew G Knepley #undef __FUNCT__
4940f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
4953bc3b0a0SMatthew 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)
4960f2d7e86SMatthew G. Knepley {
4970f2d7e86SMatthew G. Knepley   DM                dmAux;
498d5396abcSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
4990f2d7e86SMatthew G. Knepley   Vec               A;
500d5396abcSMatthew G. Knepley   PetscSection      section, sectionAux = NULL;
501b793a5ffSMatthew G. Knepley   PetscDualSpace   *sp;
502b793a5ffSMatthew G. Knepley   PetscInt         *Ncf;
503326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
504d5396abcSMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ;
5059ac3fadcSMatthew G. Knepley   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight;
5060f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
5070f2d7e86SMatthew G. Knepley 
5080f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
509048b7b1eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
510048b7b1eSToby Isaac   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
5112764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
512c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5130f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5140f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
515b793a5ffSMatthew G. Knepley   ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr);
5160f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5172764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
5182764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
5192764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
5200f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
5210f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
5220f2d7e86SMatthew G. Knepley   if (dmAux) {
5232764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
524326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
5252764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
5260f2d7e86SMatthew G. Knepley   }
527d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
5280f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
5290f2d7e86SMatthew 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);
5300f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5310f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
5329ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5339ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
5340f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
535326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
536326413afSMatthew G. Knepley 
5378e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
538326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
539326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5400f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
541d5396abcSMatthew G. Knepley       PetscObject  obj;
542d5396abcSMatthew G. Knepley       PetscClassId id;
5433113607cSMatthew G. Knepley 
544d5396abcSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
545d5396abcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
546d5396abcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
547d5396abcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
548d5396abcSMatthew G. Knepley 
549b793a5ffSMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
550b793a5ffSMatthew G. Knepley         ierr  = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
551b793a5ffSMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr);
552d5396abcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
553d5396abcSMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
554d5396abcSMatthew G. Knepley 
555b793a5ffSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr);
556302440fdSBarry Smith         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
557e3e48f69SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
558d5396abcSMatthew G. Knepley       }
559b793a5ffSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
5600f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
5610f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
5620f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
5630f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
5640f2d7e86SMatthew G. Knepley 
5650f2d7e86SMatthew G. Knepley         if (funcs[f]) {
566b793a5ffSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr);
5670f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
5680f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
5690f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
570326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
571326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
5723bc3b0a0SMatthew G. Knepley             (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]);
5730f2d7e86SMatthew G. Knepley           }
5740f2d7e86SMatthew G. Knepley         } else {
575b793a5ffSMatthew G. Knepley           for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0;
5760f2d7e86SMatthew G. Knepley         }
577b793a5ffSMatthew G. Knepley         v += Ncf[f];
5780f2d7e86SMatthew G. Knepley       }
5790f2d7e86SMatthew G. Knepley     }
580326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
581326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5820f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
5830f2d7e86SMatthew G. Knepley   }
5840f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5850f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
586b793a5ffSMatthew G. Knepley   for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
587b793a5ffSMatthew G. Knepley   ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr);
5880f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5890f2d7e86SMatthew G. Knepley }
5900f2d7e86SMatthew G. Knepley 
5910f2d7e86SMatthew G. Knepley #undef __FUNCT__
592d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal"
593ad917190SMatthew 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)
59455f2e967SMatthew G. Knepley {
595ad917190SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59655f2e967SMatthew G. Knepley   void            **ctxs;
597d7ddef95SMatthew G. Knepley   PetscInt          numFields;
598d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
599d7ddef95SMatthew G. Knepley 
600d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
601d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
602d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
603d7ddef95SMatthew G. Knepley   funcs[field] = func;
604d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
605d7ddef95SMatthew G. Knepley   ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
606d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
607d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
608d7ddef95SMatthew G. Knepley }
609d7ddef95SMatthew G. Knepley 
610d7ddef95SMatthew G. Knepley #undef __FUNCT__
611d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal"
6120e0f25f2SMatthew G. Knepley /* This ignores numcomps/comps */
613d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad,
614d7ddef95SMatthew 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)
615d7ddef95SMatthew G. Knepley {
61661f58d28SMatthew G. Knepley   PetscDS            prob;
617da97024aSMatthew G. Knepley   PetscSF            sf;
618d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
619d7ddef95SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *grad;
620da97024aSMatthew G. Knepley   const PetscInt    *leaves;
621d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
622520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
623d7ddef95SMatthew G. Knepley   PetscErrorCode     ierr;
624d7ddef95SMatthew G. Knepley 
625d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
626da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
627da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
628da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
629d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
630d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
63161f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
632d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
633d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
634d7ddef95SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
635d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
636d7ddef95SMatthew G. Knepley   if (Grad) {
637c0a6632aSMatthew G. Knepley     PetscFV fv;
638c0a6632aSMatthew G. Knepley 
639c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
640d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
641d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
642d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
643d7ddef95SMatthew G. Knepley     ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
644d7ddef95SMatthew G. Knepley   }
645d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
646d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
647d7ddef95SMatthew G. Knepley     IS              faceIS;
648d7ddef95SMatthew G. Knepley     const PetscInt *faces;
649d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
650d7ddef95SMatthew G. Knepley 
651d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
652d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
653d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
654d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
655d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
656d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
657d7ddef95SMatthew G. Knepley       const PetscFVFaceGeom *fg;
658d7ddef95SMatthew G. Knepley 
659d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
660da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
661da97024aSMatthew G. Knepley       if (loc >= 0) continue;
662d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
663d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
664d7ddef95SMatthew G. Knepley       if (Grad) {
665d7ddef95SMatthew G. Knepley         const PetscFVCellGeom *cg;
666d7ddef95SMatthew G. Knepley         const PetscScalar     *cx, *cgrad;
667d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
668d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
669d7ddef95SMatthew G. Knepley         PetscInt               d;
670d7ddef95SMatthew G. Knepley 
671d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
672d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
673d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
674520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
675d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
676d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
677520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
678d7ddef95SMatthew G. Knepley       } else {
679d7ddef95SMatthew G. Knepley         const PetscScalar *xI;
680d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
681d7ddef95SMatthew G. Knepley 
682d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
683520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
684520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr);
685d7ddef95SMatthew G. Knepley       }
686d7ddef95SMatthew G. Knepley     }
687d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
688d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
689d7ddef95SMatthew G. Knepley   }
690d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
691d7ddef95SMatthew G. Knepley   if (Grad) {
692d7ddef95SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
693d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
694d7ddef95SMatthew G. Knepley   }
695d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
696d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
697d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
698d7ddef95SMatthew G. Knepley }
699d7ddef95SMatthew G. Knepley 
700d7ddef95SMatthew G. Knepley #undef __FUNCT__
701d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues"
702d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
703d7ddef95SMatthew G. Knepley {
704d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
70555f2e967SMatthew G. Knepley   PetscErrorCode ierr;
70655f2e967SMatthew G. Knepley 
70755f2e967SMatthew G. Knepley   PetscFunctionBegin;
70855f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
709d7ddef95SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
710d7ddef95SMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
711d7ddef95SMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
712d7ddef95SMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
71355f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
71455f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
71555f2e967SMatthew G. Knepley     PetscBool       isEssential;
716d7ddef95SMatthew G. Knepley     const char     *labelname;
717d7ddef95SMatthew G. Knepley     DMLabel         label;
718d7ddef95SMatthew G. Knepley     PetscInt        field;
719d7ddef95SMatthew G. Knepley     PetscObject     obj;
720d7ddef95SMatthew G. Knepley     PetscClassId    id;
72155f2e967SMatthew G. Knepley     void          (*func)();
722d7ddef95SMatthew G. Knepley     PetscInt        numids;
723d7ddef95SMatthew G. Knepley     const PetscInt *ids;
72455f2e967SMatthew G. Knepley     void           *ctx;
72555f2e967SMatthew G. Knepley 
7260e0f25f2SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, NULL, NULL, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
727d7ddef95SMatthew G. Knepley     if (!isEssential) continue;
72863d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
729d7ddef95SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
730d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
731d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
732ad917190SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (PetscErrorCode (*)(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
733d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
73443ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
735d7ddef95SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM,
736d7ddef95SMatthew G. Knepley                                                      field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
737d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
73855f2e967SMatthew G. Knepley   }
73955f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
74055f2e967SMatthew G. Knepley }
74155f2e967SMatthew G. Knepley 
742cb1e1211SMatthew G Knepley #undef __FUNCT__
743cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
744cb1e1211SMatthew G Knepley /*@C
745cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
746cb1e1211SMatthew G Knepley 
747cb1e1211SMatthew G Knepley   Input Parameters:
748cb1e1211SMatthew G Knepley + dm    - The DM
749cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
75051259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
751cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
752cb1e1211SMatthew G Knepley 
753cb1e1211SMatthew G Knepley   Output Parameter:
754cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
755cb1e1211SMatthew G Knepley 
756cb1e1211SMatthew G Knepley   Level: developer
757cb1e1211SMatthew G Knepley 
75815496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff()
759878cb397SSatish Balay @*/
760ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
761cb1e1211SMatthew G Knepley {
762cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
763cb1e1211SMatthew G Knepley   PetscSection     section;
764c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
765cb1e1211SMatthew G Knepley   Vec              localX;
76615496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
767cb1e1211SMatthew G Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
768cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
76915496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7709ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
771cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
772cb1e1211SMatthew G Knepley 
773cb1e1211SMatthew G Knepley   PetscFunctionBegin;
774c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
775cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
776cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
777cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
778cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
779cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
780cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
78115496722SMatthew G. Knepley     PetscObject  obj;
78215496722SMatthew G. Knepley     PetscClassId id;
783c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
784c5bbbd5bSMatthew G. Knepley 
78515496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
78615496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
78715496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
78815496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
78915496722SMatthew G. Knepley 
7900f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7910f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
79215496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
79315496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
79415496722SMatthew G. Knepley 
79515496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
79615496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
79715496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
798c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
799cb1e1211SMatthew G Knepley   }
80015496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8010f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
80215496722SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
803cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8049ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8059ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
806cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
807a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
808cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
809cb1e1211SMatthew G Knepley 
8108e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
811cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
812cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
813cb1e1211SMatthew G Knepley 
81415496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
81515496722SMatthew G. Knepley       PetscObject  obj;
81615496722SMatthew G. Knepley       PetscClassId id;
817c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
81815496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
819cb1e1211SMatthew G Knepley 
82015496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
82115496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
82215496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
82315496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
82415496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
825cb1e1211SMatthew G Knepley       if (debug) {
826cb1e1211SMatthew G Knepley         char title[1024];
827cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
82815496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
829cb1e1211SMatthew G Knepley       }
830cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
83115496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
832ad917190SMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, numFields, funcVal, ctx);CHKERRQ(ierr);
83315496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83415496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83515496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
83615496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
83715496722SMatthew 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);}
83815496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
839cb1e1211SMatthew G Knepley         }
840cb1e1211SMatthew G Knepley       }
84115496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
842cb1e1211SMatthew G Knepley     }
843cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
844cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
845cb1e1211SMatthew G Knepley     localDiff += elemDiff;
846cb1e1211SMatthew G Knepley   }
84715496722SMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
848cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
849a529e722SBarry Smith   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
850cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
851cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
852cb1e1211SMatthew G Knepley }
853cb1e1211SMatthew G Knepley 
854cb1e1211SMatthew G Knepley #undef __FUNCT__
85540e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
85640e14135SMatthew G. Knepley /*@C
85740e14135SMatthew 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.
85840e14135SMatthew G. Knepley 
85940e14135SMatthew G. Knepley   Input Parameters:
86040e14135SMatthew G. Knepley + dm    - The DM
86140e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
86251259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
86340e14135SMatthew G. Knepley . X     - The coefficient vector u_h
86440e14135SMatthew G. Knepley - n     - The vector to project along
86540e14135SMatthew G. Knepley 
86640e14135SMatthew G. Knepley   Output Parameter:
86740e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
86840e14135SMatthew G. Knepley 
86940e14135SMatthew G. Knepley   Level: developer
87040e14135SMatthew G. Knepley 
87140e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
87240e14135SMatthew G. Knepley @*/
873ad917190SMatthew 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)
874cb1e1211SMatthew G Knepley {
87540e14135SMatthew G. Knepley   const PetscInt  debug = 0;
876cb1e1211SMatthew G Knepley   PetscSection    section;
87740e14135SMatthew G. Knepley   PetscQuadrature quad;
87840e14135SMatthew G. Knepley   Vec             localX;
87940e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
88040e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
88140e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
8829ac3fadcSMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp;
883cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
884cb1e1211SMatthew G Knepley 
885cb1e1211SMatthew G Knepley   PetscFunctionBegin;
886c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
88740e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
88840e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
88940e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89040e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
89140e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
892652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8930f2d7e86SMatthew G. Knepley     PetscFE  fe;
89440e14135SMatthew G. Knepley     PetscInt Nc;
895652b88e8SMatthew G. Knepley 
8960f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8970f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8980f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
89940e14135SMatthew G. Knepley     numComponents += Nc;
900652b88e8SMatthew G. Knepley   }
90140e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
90240e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
90340e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9049ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
9059ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
90640e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
90740e14135SMatthew G. Knepley     PetscScalar *x = NULL;
90840e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
909652b88e8SMatthew G. Knepley 
9108e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
91140e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
91240e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
91340e14135SMatthew G. Knepley 
91440e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
9150f2d7e86SMatthew G. Knepley       PetscFE          fe;
91651259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
91721454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
91840e14135SMatthew G. Knepley       PetscReal       *basisDer;
91921454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
92040e14135SMatthew G. Knepley 
9210f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
92221454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
9230f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
9240f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
9250f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
92640e14135SMatthew G. Knepley       if (debug) {
92740e14135SMatthew G. Knepley         char title[1024];
92840e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
92940e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
930652b88e8SMatthew G. Knepley       }
93140e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
93240e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
93340e14135SMatthew G. Knepley           coords[d] = v0[d];
93440e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
93540e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
936652b88e8SMatthew G. Knepley           }
93740e14135SMatthew G. Knepley         }
938ad917190SMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr);
93940e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
94040e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
94140e14135SMatthew G. Knepley 
94240e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
94340e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
94440e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
94540e14135SMatthew G. Knepley 
94640e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
94740e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
94840e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
94940e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
95040e14135SMatthew G. Knepley               }
95140e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
95240e14135SMatthew G. Knepley             }
95340e14135SMatthew G. Knepley           }
95440e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
95540e14135SMatthew 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);}
95640e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
95740e14135SMatthew G. Knepley         }
95840e14135SMatthew G. Knepley       }
95940e14135SMatthew G. Knepley       comp        += Ncomp;
96040e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
96140e14135SMatthew G. Knepley     }
96240e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
96340e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
96440e14135SMatthew G. Knepley     localDiff += elemDiff;
96540e14135SMatthew G. Knepley   }
96640e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
96740e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
968a529e722SBarry Smith   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
96940e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
970cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
971cb1e1211SMatthew G Knepley }
972cb1e1211SMatthew G Knepley 
973a0845e3aSMatthew G. Knepley #undef __FUNCT__
97473d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
97515496722SMatthew G. Knepley /*@C
97615496722SMatthew 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.
97715496722SMatthew G. Knepley 
97815496722SMatthew G. Knepley   Input Parameters:
97915496722SMatthew G. Knepley + dm    - The DM
98015496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component
98115496722SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
98215496722SMatthew G. Knepley - X     - The coefficient vector u_h
98315496722SMatthew G. Knepley 
98415496722SMatthew G. Knepley   Output Parameter:
98515496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2
98615496722SMatthew G. Knepley 
98715496722SMatthew G. Knepley   Level: developer
98815496722SMatthew G. Knepley 
98915496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff()
99015496722SMatthew G. Knepley @*/
991ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
99273d901b8SMatthew G. Knepley {
99373d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
99473d901b8SMatthew G. Knepley   PetscSection     section;
99573d901b8SMatthew G. Knepley   PetscQuadrature  quad;
99673d901b8SMatthew G. Knepley   Vec              localX;
99715496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
99873d901b8SMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
99973d901b8SMatthew G. Knepley   PetscReal       *localDiff;
100015496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
10019ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
100273d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
100373d901b8SMatthew G. Knepley 
100473d901b8SMatthew G. Knepley   PetscFunctionBegin;
1005c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
100673d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
100773d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
100873d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
100973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
101073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
101173d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
101215496722SMatthew G. Knepley     PetscObject  obj;
101315496722SMatthew G. Knepley     PetscClassId id;
101473d901b8SMatthew G. Knepley     PetscInt     Nc;
101573d901b8SMatthew G. Knepley 
101615496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
101715496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
101815496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
101915496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
102015496722SMatthew G. Knepley 
10210f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10220f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
102315496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
102415496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
102515496722SMatthew G. Knepley 
102615496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
102715496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
102815496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
102973d901b8SMatthew G. Knepley     numComponents += Nc;
103073d901b8SMatthew G. Knepley   }
103115496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
10320f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
103315496722SMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
103473d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10359ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10369ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
103773d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
103873d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
103973d901b8SMatthew G. Knepley 
10408e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
104173d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
104273d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
104373d901b8SMatthew G. Knepley 
104415496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
104515496722SMatthew G. Knepley       PetscObject  obj;
104615496722SMatthew G. Knepley       PetscClassId id;
104773d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
104815496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
104973d901b8SMatthew G. Knepley 
105015496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
105115496722SMatthew G. Knepley 
105215496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
105315496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
105415496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
105515496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
105615496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
105773d901b8SMatthew G. Knepley       if (debug) {
105873d901b8SMatthew G. Knepley         char title[1024];
105973d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
106015496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
106173d901b8SMatthew G. Knepley       }
106273d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
106315496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
1064ad917190SMatthew G. Knepley         ierr = (*funcs[field])(dim, coords, numFields, funcVal, ctx);CHKERRQ(ierr);
106515496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106615496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106715496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
106815496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
106915496722SMatthew 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);}
107015496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
107173d901b8SMatthew G. Knepley         }
107273d901b8SMatthew G. Knepley       }
107315496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
107473d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
107573d901b8SMatthew G. Knepley     }
107673d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
107773d901b8SMatthew G. Knepley   }
107873d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1079a529e722SBarry Smith   ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
108073d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
108115496722SMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
108273d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
108373d901b8SMatthew G. Knepley }
108473d901b8SMatthew G. Knepley 
108573d901b8SMatthew G. Knepley #undef __FUNCT__
108673d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
108773d901b8SMatthew G. Knepley /*@
108873d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
108973d901b8SMatthew G. Knepley 
109073d901b8SMatthew G. Knepley   Input Parameters:
109173d901b8SMatthew G. Knepley + dm - The mesh
109273d901b8SMatthew G. Knepley . X  - Local input vector
109373d901b8SMatthew G. Knepley - user - The user context
109473d901b8SMatthew G. Knepley 
109573d901b8SMatthew G. Knepley   Output Parameter:
109673d901b8SMatthew G. Knepley . integral - Local integral for each field
109773d901b8SMatthew G. Knepley 
109873d901b8SMatthew G. Knepley   Level: developer
109973d901b8SMatthew G. Knepley 
110073d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
110173d901b8SMatthew G. Knepley @*/
11020f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
110373d901b8SMatthew G. Knepley {
110473d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
110573d901b8SMatthew G. Knepley   DM                dmAux;
110673d901b8SMatthew G. Knepley   Vec               localX, A;
11072764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
110873d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
1109bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
111073d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
1111c1f031eeSMatthew G. Knepley   PetscReal        *lintegral, *vol;
11129ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c;
11130f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
111473d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
111573d901b8SMatthew G. Knepley 
111673d901b8SMatthew G. Knepley   PetscFunctionBegin;
1117c1f031eeSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
111873d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
111973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
112073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1121c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
112273d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
11232764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
11242764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
112573d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
112673d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11279ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11289ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
112973d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
113073d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
113173d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
113273d901b8SMatthew G. Knepley   if (dmAux) {
113373d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
11342764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
11352764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
113673d901b8SMatthew G. Knepley   }
1137d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1138c1f031eeSMatthew G. Knepley   ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr);
11390f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1140c1f031eeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;}
114173d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
114273d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
114373d901b8SMatthew G. Knepley     PetscInt     i;
114473d901b8SMatthew G. Knepley 
1145bbce034cSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr);
1146c1f031eeSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr);
1147bbce034cSMatthew 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);
114873d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
11490f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
115073d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
115173d901b8SMatthew G. Knepley     if (dmAux) {
115273d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11530f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
115473d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
115573d901b8SMatthew G. Knepley     }
115673d901b8SMatthew G. Knepley   }
115773d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1158c1f031eeSMatthew G. Knepley     PetscObject  obj;
1159c1f031eeSMatthew G. Knepley     PetscClassId id;
1160c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
116173d901b8SMatthew G. Knepley 
1162c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1163c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1164c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1165c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1166c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1167c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1168c1f031eeSMatthew G. Knepley 
11690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1170c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1171c1f031eeSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1172c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1173c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
117473d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
11750f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
117673d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
117773d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
117873d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
117973d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1180c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr);
1181c1f031eeSMatthew G. Knepley       ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr);
1182c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1183b69edc29SMatthew G. Knepley       /* PetscFV  fv = (PetscFV) obj; */
1184c1f031eeSMatthew G. Knepley       PetscInt       foff;
1185420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1186b69edc29SMatthew G. Knepley       PetscScalar    lint;
1187c1f031eeSMatthew G. Knepley 
1188c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1189c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1190c1f031eeSMatthew G. Knepley       if (obj_func) {
1191c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1192c1f031eeSMatthew G. Knepley           /* TODO: Need full pointwise interpolation and get centroid for x argument */
119330b9ff8bSMatthew G. Knepley           obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint);
1194b69edc29SMatthew G. Knepley           lintegral[f] = PetscRealPart(lint)*vol[c];
119573d901b8SMatthew G. Knepley         }
1196c1f031eeSMatthew G. Knepley       }
1197c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1198c1f031eeSMatthew G. Knepley   }
119973d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
120073d901b8SMatthew G. Knepley   if (mesh->printFEM) {
120173d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
1202c1f031eeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);}
120373d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
120473d901b8SMatthew G. Knepley   }
120573d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1206a529e722SBarry Smith   ierr = MPI_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
1207c1f031eeSMatthew G. Knepley   ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr);
1208c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
120973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
121073d901b8SMatthew G. Knepley }
121173d901b8SMatthew G. Knepley 
121273d901b8SMatthew G. Knepley #undef __FUNCT__
1213d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1214d69c5d34SMatthew G. Knepley /*@
1215d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1216d69c5d34SMatthew G. Knepley 
1217d69c5d34SMatthew G. Knepley   Input Parameters:
1218d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1219d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1220d69c5d34SMatthew G. Knepley - user - The user context
1221d69c5d34SMatthew G. Knepley 
1222d69c5d34SMatthew G. Knepley   Output Parameter:
1223934789fcSMatthew G. Knepley . In  - The interpolation matrix
1224d69c5d34SMatthew G. Knepley 
1225d69c5d34SMatthew G. Knepley   Note:
1226d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1227d69c5d34SMatthew G. Knepley 
1228d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1229d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1230d69c5d34SMatthew G. Knepley 
1231d69c5d34SMatthew G. Knepley   Level: developer
1232d69c5d34SMatthew G. Knepley 
1233d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1234d69c5d34SMatthew G. Knepley @*/
1235934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1236d69c5d34SMatthew G. Knepley {
1237d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1238d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
12392764a2aaSMatthew G. Knepley   PetscDS           prob;
1240d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
124197c42addSMatthew G. Knepley   PetscFV          *fvRef;
1242d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1243d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1244d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
12459ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
12460f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1247d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1248d69c5d34SMatthew G. Knepley 
1249d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1250d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1251c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1252d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1253d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1254d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1255d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1256d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1257d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
12589ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12599ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
12602764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
126197c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1262d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
126397c42addSMatthew G. Knepley     PetscObject  obj;
126497c42addSMatthew G. Knepley     PetscClassId id;
1265aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1266d69c5d34SMatthew G. Knepley 
126797c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
126897c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
126997c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
127097c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
127197c42addSMatthew G. Knepley 
12720f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1273d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
12740f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
127597c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
127697c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
127797c42addSMatthew G. Knepley       PetscDualSpace Q;
127897c42addSMatthew G. Knepley 
127997c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
128097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
128197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
128297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
128397c42addSMatthew G. Knepley     }
12840f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1285d69c5d34SMatthew G. Knepley   }
12862764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
12870f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
12880f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1289d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1290d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1291d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1292d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1293d69c5d34SMatthew G. Knepley     PetscReal       *points;
1294d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1295d69c5d34SMatthew G. Knepley 
1296d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
129797c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1298d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
12990f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
130097c42addSMatthew G. Knepley     } else {
130197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
130297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
130397c42addSMatthew G. Knepley     }
1304d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1305d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1306d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1307d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1308d69c5d34SMatthew G. Knepley       npoints += Np;
1309d69c5d34SMatthew G. Knepley     }
1310d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1311d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1312d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1313d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1314d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1315d69c5d34SMatthew G. Knepley     }
1316d69c5d34SMatthew G. Knepley 
1317d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
131897c42addSMatthew G. Knepley       PetscObject  obj;
131997c42addSMatthew G. Knepley       PetscClassId id;
1320d69c5d34SMatthew G. Knepley       PetscReal   *B;
1321aa7890ccSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j;
1322d69c5d34SMatthew G. Knepley 
132397c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
132497c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
132597c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
132697c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1327d69c5d34SMatthew G. Knepley 
1328d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
13290f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
13300f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1331ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1332ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
13337c927364SMatthew 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);
13340f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1335d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1336d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1337d69c5d34SMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1338d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
133936a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
13400f2d7e86SMatthew 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];
134136a6d9c0SMatthew G. Knepley               }
1342d69c5d34SMatthew G. Knepley             }
1343d69c5d34SMatthew G. Knepley           }
13440f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1345ffe73a53SMatthew G. Knepley         }
134697c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
134797c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
134897c42addSMatthew G. Knepley 
134997c42addSMatthew G. Knepley         /* Evaluate constant function at points */
135097c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
135197c42addSMatthew G. Knepley         cpdim = 1;
135297c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
135397c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
135497c42addSMatthew 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);
135597c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
135697c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
135797c42addSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
135897c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
135997c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
136097c42addSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p];
136197c42addSMatthew G. Knepley               }
136297c42addSMatthew G. Knepley             }
136397c42addSMatthew G. Knepley           }
136497c42addSMatthew G. Knepley         }
136597c42addSMatthew G. Knepley       }
136636a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1367d69c5d34SMatthew G. Knepley     }
1368d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1369549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1370d69c5d34SMatthew G. Knepley   }
13710f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
13727f5b169aSMatthew G. Knepley   /* Preallocate matrix */
13737f5b169aSMatthew G. Knepley   {
13747f5b169aSMatthew G. Knepley     PetscHashJK ht;
13757f5b169aSMatthew G. Knepley     PetscLayout rLayout;
13767f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
13777f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
13787f5b169aSMatthew G. Knepley 
13797f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
13807f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
13817f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
13827f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
13837f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
13847f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
13857f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
13867f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
13877f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
13887f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
13897f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
13907f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
13917f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
13927f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
13937f5b169aSMatthew G. Knepley 
13947f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
13957f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
13967f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
13977f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
13987f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
13997f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
14007f5b169aSMatthew G. Knepley           if (missing) {
14017f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
14027f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
14037f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
14047f5b169aSMatthew G. Knepley           }
14057f5b169aSMatthew G. Knepley         }
14067f5b169aSMatthew G. Knepley       }
14077f5b169aSMatthew G. Knepley     }
14087f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
14097f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
14107f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
14117f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
14127f5b169aSMatthew G. Knepley   }
14137f5b169aSMatthew G. Knepley   /* Fill matrix */
14147f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1415d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1416934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1417d69c5d34SMatthew G. Knepley   }
1418549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
141997c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1420549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1421934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1422934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1423d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1424d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1425934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1426934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1427d69c5d34SMatthew G. Knepley   }
1428d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1429d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1430d69c5d34SMatthew G. Knepley }
14316c73c22cSMatthew G. Knepley 
14326c73c22cSMatthew G. Knepley #undef __FUNCT__
1433*4ef9d792SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolator_General"
1434*4ef9d792SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolator_General(DM dmc, DM dmf, Mat In, void *user)
1435*4ef9d792SMatthew G. Knepley {
1436*4ef9d792SMatthew G. Knepley   PetscDS        prob;
1437*4ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
1438*4ef9d792SMatthew G. Knepley   PetscHashJK    ht;
1439*4ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
1440*4ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
1441*4ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
1442*4ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
1443*4ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
1444*4ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
1445*4ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
1446*4ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
1447*4ef9d792SMatthew G. Knepley 
1448*4ef9d792SMatthew G. Knepley   PetscFunctionBegin;
1449*4ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
1450*4ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
1451*4ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
1452*4ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1453*4ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
1454*4ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
1455*4ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1456*4ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
1457*4ef9d792SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1458*4ef9d792SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
1459*4ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
1460*4ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1461*4ef9d792SMatthew G. Knepley   ierr = PetscMalloc1(totDim*totDim, &elemMat);CHKERRQ(ierr);
1462*4ef9d792SMatthew G. Knepley 
1463*4ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
1464*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
1465*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
1466*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
1467*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
1468*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
1469*4ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
1470*4ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
1471*4ef9d792SMatthew G. Knepley   ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
1472*4ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
1473*4ef9d792SMatthew G. Knepley     PetscObject      obj;
1474*4ef9d792SMatthew G. Knepley     PetscClassId     id;
1475*4ef9d792SMatthew G. Knepley     PetscDualSpace   Q;
1476*4ef9d792SMatthew G. Knepley     PetscQuadrature  f;
1477*4ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1478*4ef9d792SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, p, d;
1479*4ef9d792SMatthew G. Knepley 
1480*4ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
1481*4ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1482*4ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1483*4ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1484*4ef9d792SMatthew G. Knepley 
1485*4ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
1486*4ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1487*4ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1488*4ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1489*4ef9d792SMatthew G. Knepley 
1490*4ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
1491*4ef9d792SMatthew G. Knepley       Nc   = 1;
1492*4ef9d792SMatthew G. Knepley     }
1493*4ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
1494*4ef9d792SMatthew G. Knepley     /* For each fine grid cell */
1495*4ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
1496*4ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
1497*4ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
1498*4ef9d792SMatthew G. Knepley 
1499*4ef9d792SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
1500*4ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
1501*4ef9d792SMatthew 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);
1502*4ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
1503*4ef9d792SMatthew G. Knepley         Vec             pointVec;
1504*4ef9d792SMatthew G. Knepley         PetscScalar    *pV;
1505*4ef9d792SMatthew G. Knepley         IS              coarseCellIS;
1506*4ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
1507*4ef9d792SMatthew G. Knepley         PetscInt        numCoarseCells, q, r, c;
1508*4ef9d792SMatthew G. Knepley 
1509*4ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
1510*4ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
1511*4ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1512*4ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
1513*4ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
1514*4ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
1515*4ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
1516*4ef9d792SMatthew G. Knepley           /* Transform point to real space */
1517*4ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
1518*4ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
1519*4ef9d792SMatthew G. Knepley         }
1520*4ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
1521*4ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
1522*4ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
1523*4ef9d792SMatthew G. Knepley         ierr = ISViewFromOptions(coarseCellIS, NULL, "-interp_is_view");CHKERRQ(ierr);
1524*4ef9d792SMatthew G. Knepley         /* Update preallocation info */
1525*4ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
1526*4ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
1527*4ef9d792SMatthew G. Knepley         for (r = 0; r < Nc; ++r) {
1528*4ef9d792SMatthew G. Knepley           PetscHashJKKey  key;
1529*4ef9d792SMatthew G. Knepley           PetscHashJKIter missing, iter;
1530*4ef9d792SMatthew G. Knepley 
1531*4ef9d792SMatthew G. Knepley           key.j = findices[i*Nc+r];
1532*4ef9d792SMatthew G. Knepley           if (key.j < 0) continue;
1533*4ef9d792SMatthew G. Knepley           /* Get indices for coarse elements */
1534*4ef9d792SMatthew G. Knepley           for (ccell = 0; ccell < numCoarseCells; ++ccell) {
1535*4ef9d792SMatthew G. Knepley             ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
1536*4ef9d792SMatthew G. Knepley             for (c = 0; c < numCIndices; ++c) {
1537*4ef9d792SMatthew G. Knepley               key.k = cindices[c];
1538*4ef9d792SMatthew G. Knepley               if (key.k < 0) continue;
1539*4ef9d792SMatthew G. Knepley               ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
1540*4ef9d792SMatthew G. Knepley               if (missing) {
1541*4ef9d792SMatthew G. Knepley                 ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
1542*4ef9d792SMatthew G. Knepley                 if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
1543*4ef9d792SMatthew G. Knepley                 else                                     ++onz[key.j-rStart];
1544*4ef9d792SMatthew G. Knepley               }
1545*4ef9d792SMatthew G. Knepley             }
1546*4ef9d792SMatthew G. Knepley             ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
1547*4ef9d792SMatthew G. Knepley           }
1548*4ef9d792SMatthew G. Knepley         }
1549*4ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
1550*4ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
1551*4ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
1552*4ef9d792SMatthew G. Knepley       }
1553*4ef9d792SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
1554*4ef9d792SMatthew G. Knepley     }
1555*4ef9d792SMatthew G. Knepley   }
1556*4ef9d792SMatthew G. Knepley   ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
1557*4ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
1558*4ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
1559*4ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
1560*4ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
1561*4ef9d792SMatthew G. Knepley     PetscObject      obj;
1562*4ef9d792SMatthew G. Knepley     PetscClassId     id;
1563*4ef9d792SMatthew G. Knepley     PetscDualSpace   Q;
1564*4ef9d792SMatthew G. Knepley     PetscQuadrature  f;
1565*4ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1566*4ef9d792SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, p, d;
1567*4ef9d792SMatthew G. Knepley 
1568*4ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
1569*4ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1570*4ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1571*4ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1572*4ef9d792SMatthew G. Knepley 
1573*4ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
1574*4ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1575*4ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1576*4ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1577*4ef9d792SMatthew G. Knepley 
1578*4ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
1579*4ef9d792SMatthew G. Knepley       Nc   = 1;
1580*4ef9d792SMatthew G. Knepley     }
1581*4ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
1582*4ef9d792SMatthew G. Knepley     /* For each fine grid cell */
1583*4ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
1584*4ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
1585*4ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
1586*4ef9d792SMatthew G. Knepley 
1587*4ef9d792SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
1588*4ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
1589*4ef9d792SMatthew 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);
1590*4ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
1591*4ef9d792SMatthew G. Knepley         Vec             pointVec;
1592*4ef9d792SMatthew G. Knepley         PetscScalar    *pV;
1593*4ef9d792SMatthew G. Knepley         IS              coarseCellIS;
1594*4ef9d792SMatthew G. Knepley         const PetscInt *coarseCells;
1595*4ef9d792SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, r, c, j;
1596*4ef9d792SMatthew G. Knepley 
1597*4ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
1598*4ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
1599*4ef9d792SMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, &qweights);CHKERRQ(ierr);
1600*4ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
1601*4ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
1602*4ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
1603*4ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
1604*4ef9d792SMatthew G. Knepley           /* Transform point to real space */
1605*4ef9d792SMatthew G. Knepley           CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x);
1606*4ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
1607*4ef9d792SMatthew G. Knepley         }
1608*4ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
1609*4ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
1610*4ef9d792SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr);
1611*4ef9d792SMatthew G. Knepley         /* Update preallocation info */
1612*4ef9d792SMatthew G. Knepley         ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr);
1613*4ef9d792SMatthew G. Knepley         ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
1614*4ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
1615*4ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
1616*4ef9d792SMatthew G. Knepley           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
1617*4ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
1618*4ef9d792SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFEM(dmc, ccell, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
1619*4ef9d792SMatthew G. Knepley           CoordinatesRealToRef(dim, dim, v0c, invJc, &pV[ccell*dim], x);
1620*4ef9d792SMatthew G. Knepley 
1621*4ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
1622*4ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
1623*4ef9d792SMatthew G. Knepley             PetscReal *B;
1624*4ef9d792SMatthew G. Knepley 
1625*4ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
1626*4ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1627*4ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
1628*4ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
1629*4ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
1630*4ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = B[j*Nc + c]*qweights[ccell];
1631*4ef9d792SMatthew G. Knepley             }
1632*4ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1633*4ef9d792SMatthew G. Knepley           } else {
1634*4ef9d792SMatthew G. Knepley             cpdim = 1;
1635*4ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
1636*4ef9d792SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = 1.0*qweights[ccell];
1637*4ef9d792SMatthew G. Knepley             }
1638*4ef9d792SMatthew G. Knepley           }
1639*4ef9d792SMatthew G. Knepley           /* Update interpolator */
1640*4ef9d792SMatthew G. Knepley           ierr = MatSetValues(In, Nc, &findices[i*Nc], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1641*4ef9d792SMatthew G. Knepley           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr);
1642*4ef9d792SMatthew G. Knepley         }
1643*4ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
1644*4ef9d792SMatthew G. Knepley         ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr);
1645*4ef9d792SMatthew G. Knepley         ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr);
1646*4ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
1647*4ef9d792SMatthew G. Knepley       }
1648*4ef9d792SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr);
1649*4ef9d792SMatthew G. Knepley     }
1650*4ef9d792SMatthew G. Knepley   }
1651*4ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
1652*4ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
1653*4ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1654*4ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1655*4ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1656*4ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
1657*4ef9d792SMatthew G. Knepley }
1658*4ef9d792SMatthew G. Knepley 
1659*4ef9d792SMatthew G. Knepley #undef __FUNCT__
16607c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
16617c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
16627c927364SMatthew G. Knepley {
1663e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
16647c927364SMatthew G. Knepley   PetscFE       *feRef;
166597c42addSMatthew G. Knepley   PetscFV       *fvRef;
16667c927364SMatthew G. Knepley   Vec            fv, cv;
16677c927364SMatthew G. Knepley   IS             fis, cis;
16687c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
16697c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
16709ac3fadcSMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, offsetC, offsetF, m;
16717c927364SMatthew G. Knepley   PetscErrorCode ierr;
16727c927364SMatthew G. Knepley 
16737c927364SMatthew G. Knepley   PetscFunctionBegin;
167475a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1675c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
16767c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
16777c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
16787c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
16797c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
16807c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
16817c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
16829ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
16839ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1684e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
168597c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
16867c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
168797c42addSMatthew G. Knepley     PetscObject  obj;
168897c42addSMatthew G. Knepley     PetscClassId id;
1689aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
16907c927364SMatthew G. Knepley 
169197c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
169297c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
169397c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
169497c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
169597c42addSMatthew G. Knepley 
16967c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
16977c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
16987c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
169997c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
170097c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
170197c42addSMatthew G. Knepley       PetscDualSpace Q;
170297c42addSMatthew G. Knepley 
170397c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
170497c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
170597c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
170697c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
170797c42addSMatthew G. Knepley     }
17087c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
17097c927364SMatthew G. Knepley   }
1710e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17117c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
17127c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
17137c927364SMatthew G. Knepley     PetscFE        feC;
171497c42addSMatthew G. Knepley     PetscFV        fvC;
17157c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
17167c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
17177c927364SMatthew G. Knepley 
171897c42addSMatthew G. Knepley     if (feRef[field]) {
1719e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
17207c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
17217c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
17227c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
17237c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
17247c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
17257c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
172697c42addSMatthew G. Knepley     } else {
172797c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
172897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
172997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
173097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
173197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
173297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
173397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
173497c42addSMatthew G. Knepley     }
173597c42addSMatthew 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);
17367c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
17377c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
17387c927364SMatthew G. Knepley       const PetscReal *cqpoints;
17397c927364SMatthew G. Knepley       PetscInt         NpC;
174097c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
17417c927364SMatthew G. Knepley 
17427c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
17437c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
174497c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
17457c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
17467c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
17477c927364SMatthew G. Knepley         const PetscReal *fqpoints;
17487c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
17497c927364SMatthew G. Knepley         PetscInt         NpF, comp;
17507c927364SMatthew G. Knepley 
17517c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
17527c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
17537c927364SMatthew G. Knepley         if (NpC != NpF) continue;
17547c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
17557c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
17567c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
17577c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
17587c927364SMatthew G. Knepley         }
175997c42addSMatthew G. Knepley         found = PETSC_TRUE;
17607c927364SMatthew G. Knepley         break;
17617c927364SMatthew G. Knepley       }
176297c42addSMatthew G. Knepley       if (!found) {
176397c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
176497c42addSMatthew G. Knepley         if (fvRef[field]) {
176597c42addSMatthew G. Knepley           PetscInt comp;
176697c42addSMatthew G. Knepley           for (comp = 0; comp < NcC; ++comp) {
176797c42addSMatthew G. Knepley             cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp;
176897c42addSMatthew G. Knepley           }
176997c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
177097c42addSMatthew G. Knepley       }
17717c927364SMatthew G. Knepley     }
17727c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
17737c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
17747c927364SMatthew G. Knepley   }
177597c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
177697c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
17777c927364SMatthew G. Knepley 
17787c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
17797c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
17807c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
17817c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
17827c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1783aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1784aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
17857c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
17867c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
17877c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
17887c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
17897c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
17907c927364SMatthew 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]]);
17917c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
17927c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
17937c927364SMatthew G. Knepley     }
17947c927364SMatthew G. Knepley   }
17957c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
17967c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
17977c927364SMatthew G. Knepley 
17987c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
17997c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
18007c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
18017c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
18027c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
18037c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
18047c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
180575a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1806cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1807cb1e1211SMatthew G Knepley }
1808