xref: /petsc/src/dm/impls/plex/plexfem.c (revision fa5a00099b38c39f4124740e4a9eded9bb6777df)
1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3cb1e1211SMatthew G Knepley 
40f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h>
515496722SMatthew G. Knepley #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__
88cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody"
89cb1e1211SMatthew G Knepley /*@C
90cb1e1211SMatthew G Knepley   DMPlexCreateRigidBody - create rigid body modes from coordinates
91cb1e1211SMatthew G Knepley 
92cb1e1211SMatthew G Knepley   Collective on DM
93cb1e1211SMatthew G Knepley 
94cb1e1211SMatthew G Knepley   Input Arguments:
95cb1e1211SMatthew G Knepley + dm - the DM
96cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section
97cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section
98cb1e1211SMatthew G Knepley 
99cb1e1211SMatthew G Knepley   Output Argument:
100cb1e1211SMatthew G Knepley . sp - the null space
101cb1e1211SMatthew G Knepley 
102cb1e1211SMatthew G Knepley   Note: This is necessary to take account of Dirichlet conditions on the displacements
103cb1e1211SMatthew G Knepley 
104cb1e1211SMatthew G Knepley   Level: advanced
105cb1e1211SMatthew G Knepley 
106cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate()
107cb1e1211SMatthew G Knepley @*/
108cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp)
109cb1e1211SMatthew G Knepley {
110cb1e1211SMatthew G Knepley   MPI_Comm       comm;
111cb1e1211SMatthew G Knepley   Vec            coordinates, localMode, mode[6];
112cb1e1211SMatthew G Knepley   PetscSection   coordSection;
113cb1e1211SMatthew G Knepley   PetscScalar   *coords;
114cb1e1211SMatthew G Knepley   PetscInt       dim, vStart, vEnd, v, n, m, d, i, j;
115cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
116cb1e1211SMatthew G Knepley 
117cb1e1211SMatthew G Knepley   PetscFunctionBegin;
118cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
119c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
120cb1e1211SMatthew G Knepley   if (dim == 1) {
121cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
122cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
123cb1e1211SMatthew G Knepley   }
124cb1e1211SMatthew G Knepley   if (!section)       {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
125cb1e1211SMatthew G Knepley   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
126cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
127cb1e1211SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
12869d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
129cb1e1211SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
130cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
131cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
132cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
133cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
134cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
135cb1e1211SMatthew G Knepley   /* Assume P1 */
136cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr);
137cb1e1211SMatthew G Knepley   for (d = 0; d < dim; ++d) {
138cb1e1211SMatthew G Knepley     PetscScalar values[3] = {0.0, 0.0, 0.0};
139cb1e1211SMatthew G Knepley 
140cb1e1211SMatthew G Knepley     values[d] = 1.0;
141cb1e1211SMatthew G Knepley     ierr      = VecSet(localMode, 0.0);CHKERRQ(ierr);
142cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
143cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
144cb1e1211SMatthew G Knepley     }
145cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
146cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
147cb1e1211SMatthew G Knepley   }
148cb1e1211SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
149cb1e1211SMatthew G Knepley   for (d = dim; d < dim*(dim+1)/2; ++d) {
150cb1e1211SMatthew G Knepley     PetscInt i, j, k = dim > 2 ? d - dim : d;
151cb1e1211SMatthew G Knepley 
152cb1e1211SMatthew G Knepley     ierr = VecSet(localMode, 0.0);CHKERRQ(ierr);
153cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
154cb1e1211SMatthew G Knepley       PetscScalar values[3] = {0.0, 0.0, 0.0};
155cb1e1211SMatthew G Knepley       PetscInt    off;
156cb1e1211SMatthew G Knepley 
157cb1e1211SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
158cb1e1211SMatthew G Knepley       for (i = 0; i < dim; ++i) {
159cb1e1211SMatthew G Knepley         for (j = 0; j < dim; ++j) {
160cb1e1211SMatthew G Knepley           values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]);
161cb1e1211SMatthew G Knepley         }
162cb1e1211SMatthew G Knepley       }
163cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
164cb1e1211SMatthew G Knepley     }
165cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
166cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
167cb1e1211SMatthew G Knepley   }
168cb1e1211SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
169cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr);
170cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
171cb1e1211SMatthew G Knepley   /* Orthonormalize system */
172cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
173cb1e1211SMatthew G Knepley     PetscScalar dots[6];
174cb1e1211SMatthew G Knepley 
175cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
176cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
177cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
178cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
179cb1e1211SMatthew G Knepley   }
180cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
181cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
182cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
183cb1e1211SMatthew G Knepley }
184cb1e1211SMatthew G Knepley 
185cb1e1211SMatthew G Knepley #undef __FUNCT__
186b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight"
187b29cfa1cSToby Isaac /*@
188b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
189b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
190b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
191b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
192b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
193b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
194b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
195b29cfa1cSToby Isaac 
196b29cfa1cSToby Isaac   Input Parameters:
197b29cfa1cSToby Isaac + dm - the DMPlex object
198b29cfa1cSToby Isaac - height - the maximum projection height >= 0
199b29cfa1cSToby Isaac 
200b29cfa1cSToby Isaac   Level: advanced
201b29cfa1cSToby Isaac 
202048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
203b29cfa1cSToby Isaac @*/
204b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
205b29cfa1cSToby Isaac {
206b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
207b29cfa1cSToby Isaac 
208b29cfa1cSToby Isaac   PetscFunctionBegin;
209b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
210b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
211b29cfa1cSToby Isaac   PetscFunctionReturn(0);
212b29cfa1cSToby Isaac }
213b29cfa1cSToby Isaac 
214b29cfa1cSToby Isaac #undef __FUNCT__
215b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight"
216b29cfa1cSToby Isaac /*@
217b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
218b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
219b29cfa1cSToby Isaac 
220b29cfa1cSToby Isaac   Input Parameters:
221b29cfa1cSToby Isaac . dm - the DMPlex object
222b29cfa1cSToby Isaac 
223b29cfa1cSToby Isaac   Output Parameters:
224b29cfa1cSToby Isaac . height - the maximum projection height
225b29cfa1cSToby Isaac 
226b29cfa1cSToby Isaac   Level: intermediate
227b29cfa1cSToby Isaac 
228048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
229b29cfa1cSToby Isaac @*/
230b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
231b29cfa1cSToby Isaac {
232b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
233b29cfa1cSToby Isaac 
234b29cfa1cSToby Isaac   PetscFunctionBegin;
235b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
236b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
237b29cfa1cSToby Isaac   PetscFunctionReturn(0);
238b29cfa1cSToby Isaac }
239b29cfa1cSToby Isaac 
240b29cfa1cSToby Isaac #undef __FUNCT__
241a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
242bf3434eeSMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
243a18a7fb9SMatthew G. Knepley {
244bf3434eeSMatthew G. Knepley   PetscFE         fe;
2457d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
246a18a7fb9SMatthew G. Knepley   PetscSection    section;
247a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
248ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
2499ac3fadcSMatthew G. Knepley   PetscInt        numFields, numComp, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h;
250a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
251a18a7fb9SMatthew G. Knepley 
252a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
2539ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2549ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2559ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2569ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
257c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2587a1a1bd4SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
259a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
260a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
261e1d0b1adSMatthew G. Knepley   ierr = PetscMalloc1(numFields,&sp);CHKERRQ(ierr);
2627d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
2637d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
2649ac3fadcSMatthew G. Knepley   if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);}
2659ac3fadcSMatthew G. Knepley   else               {cellsp = sp;}
2667d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
2677d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
268*fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
2697d1dd11eSToby Isaac     if (pEnd <= pStart) continue;
2707d1dd11eSToby Isaac     totDim = 0;
271a18a7fb9SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
2729ac3fadcSMatthew G. Knepley       PetscObject  obj;
2739ac3fadcSMatthew G. Knepley       PetscClassId id;
2749ac3fadcSMatthew G. Knepley 
2759ac3fadcSMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2769ac3fadcSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2779ac3fadcSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
2789ac3fadcSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
2799ac3fadcSMatthew G. Knepley 
2809ac3fadcSMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
2817d1dd11eSToby Isaac         if (!h) {
282ee2838f6SToby Isaac           ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
2837d1dd11eSToby Isaac           sp[f] = cellsp[f];
2849ac3fadcSMatthew G. Knepley           ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
2859ac3fadcSMatthew G. Knepley         } else {
2867d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
2877d1dd11eSToby Isaac           if (!sp[f]) continue;
2887d1dd11eSToby Isaac         }
2899ac3fadcSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
2909ac3fadcSMatthew G. Knepley         PetscFV         fv = (PetscFV) obj;
2919ac3fadcSMatthew G. Knepley         PetscQuadrature q;
2929ac3fadcSMatthew G. Knepley 
2939ac3fadcSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
2949ac3fadcSMatthew G. Knepley         ierr = PetscFVGetQuadrature(fv, &q);CHKERRQ(ierr);
2959ac3fadcSMatthew G. Knepley         ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject) fv), &sp[f]);CHKERRQ(ierr);
2969ac3fadcSMatthew G. Knepley         ierr = PetscDualSpaceSetDM(sp[f], dm);CHKERRQ(ierr);
2979ac3fadcSMatthew G. Knepley         ierr = PetscDualSpaceSetType(sp[f], PETSCDUALSPACESIMPLE);CHKERRQ(ierr);
2989ac3fadcSMatthew G. Knepley         ierr = PetscDualSpaceSimpleSetDimension(sp[f], 1);CHKERRQ(ierr);
2999ac3fadcSMatthew G. Knepley         ierr = PetscDualSpaceSimpleSetFunctional(sp[f], 0, q);CHKERRQ(ierr);
3009ac3fadcSMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
301a18a7fb9SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
302a18a7fb9SMatthew G. Knepley       totDim += spDim*numComp;
303a18a7fb9SMatthew G. Knepley     }
3047d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
3057d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
306d374af2bSToby Isaac     if (!totDim) continue;
307a18a7fb9SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
308ad96f515SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
309ee2838f6SToby Isaac     for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE;
310a18a7fb9SMatthew G. Knepley     for (i = 0; i < numIds; ++i) {
311a18a7fb9SMatthew G. Knepley       IS              pointIS;
312a18a7fb9SMatthew G. Knepley       const PetscInt *points;
313a18a7fb9SMatthew G. Knepley       PetscInt        n, p;
314a18a7fb9SMatthew G. Knepley 
315a18a7fb9SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
316a18a7fb9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
317a18a7fb9SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
318a18a7fb9SMatthew G. Knepley       for (p = 0; p < n; ++p) {
319a18a7fb9SMatthew G. Knepley         const PetscInt    point = points[p];
320e1d0b1adSMatthew G. Knepley         PetscFECellGeom   geom;
321a18a7fb9SMatthew G. Knepley 
3227d1dd11eSToby Isaac         if ((point < pStart) || (point >= pEnd)) continue;
323e1d0b1adSMatthew G. Knepley         ierr          = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
324ee2838f6SToby Isaac         geom.dim      = dim - h;
3257a1a1bd4SToby Isaac         geom.dimEmbed = dimEmbed;
326a18a7fb9SMatthew G. Knepley         for (f = 0, v = 0; f < numFields; ++f) {
327a18a7fb9SMatthew G. Knepley           void * const ctx = ctxs ? ctxs[f] : NULL;
328bf3434eeSMatthew G. Knepley 
3297d1dd11eSToby Isaac           if (!sp[f]) continue;
330bf3434eeSMatthew G. Knepley           ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
331bf3434eeSMatthew G. Knepley           ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
332a18a7fb9SMatthew G. Knepley           ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
333a18a7fb9SMatthew G. Knepley           for (d = 0; d < spDim; ++d) {
334a18a7fb9SMatthew G. Knepley             if (funcs[f]) {
335e1d0b1adSMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
336a18a7fb9SMatthew G. Knepley             } else {
337a18a7fb9SMatthew G. Knepley               for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
338a18a7fb9SMatthew G. Knepley             }
339a18a7fb9SMatthew G. Knepley             v += numComp;
340a18a7fb9SMatthew G. Knepley           }
341a18a7fb9SMatthew G. Knepley         }
342ad96f515SMatthew G. Knepley         ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
343a18a7fb9SMatthew G. Knepley       }
344a18a7fb9SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
345a18a7fb9SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
346a18a7fb9SMatthew G. Knepley     }
3477d1dd11eSToby Isaac     if (h) {
3487d1dd11eSToby Isaac       for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
3497d1dd11eSToby Isaac     }
3507d1dd11eSToby Isaac   }
351a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
352ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
3539ac3fadcSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
354e1d0b1adSMatthew G. Knepley   ierr = PetscFree(sp);CHKERRQ(ierr);
3557d1dd11eSToby Isaac   if (maxHeight > 0) {
3567d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
3577d1dd11eSToby Isaac   }
358a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
359a18a7fb9SMatthew G. Knepley }
360a18a7fb9SMatthew G. Knepley 
361a18a7fb9SMatthew G. Knepley #undef __FUNCT__
362cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
3630f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
364cb1e1211SMatthew G Knepley {
3657d1dd11eSToby Isaac   PetscDualSpace *sp, *cellsp;
36615496722SMatthew G. Knepley   PetscInt       *numComp;
36772f94c41SMatthew G. Knepley   PetscSection    section;
36872f94c41SMatthew G. Knepley   PetscScalar    *values;
3699ac3fadcSMatthew G. Knepley   PetscInt        numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, c, f, d, v, comp, h, maxHeight;
370cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
371cb1e1211SMatthew G Knepley 
372cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3739ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3749ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
3759ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
3769ac3fadcSMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
377cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
37872f94c41SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
37915496722SMatthew G. Knepley   ierr = PetscMalloc2(numFields, &sp, numFields, &numComp);CHKERRQ(ierr);
3807d1dd11eSToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
381ee2838f6SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
3827d1dd11eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
3837d1dd11eSToby Isaac   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
3847d1dd11eSToby Isaac   if (maxHeight > 0) {
3857d1dd11eSToby Isaac     ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);
3867d1dd11eSToby Isaac   }
387048b7b1eSToby Isaac   else {
388048b7b1eSToby Isaac     cellsp = sp;
389048b7b1eSToby Isaac   }
3907d1dd11eSToby Isaac   for (h = 0; h <= maxHeight; h++) {
3917d1dd11eSToby Isaac     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
392*fa5a0009SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
393048b7b1eSToby Isaac     if (pEnd <= pStart) continue;
3947d1dd11eSToby Isaac     totDim = 0;
39572f94c41SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
39615496722SMatthew G. Knepley       PetscObject  obj;
39715496722SMatthew G. Knepley       PetscClassId id;
3980f2d7e86SMatthew G. Knepley 
39915496722SMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
40015496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
40115496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
40215496722SMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
40315496722SMatthew G. Knepley 
40415496722SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
4057d1dd11eSToby Isaac         if (!h) {
4067d1dd11eSToby Isaac           ierr  = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
4077d1dd11eSToby Isaac           sp[f] = cellsp[f];
40815496722SMatthew G. Knepley           ierr  = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
4097d1dd11eSToby Isaac         }
4107d1dd11eSToby Isaac         else {
4117d1dd11eSToby Isaac           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
4127d1dd11eSToby Isaac           if (!sp[f]) {
4137d1dd11eSToby Isaac             continue;
4147d1dd11eSToby Isaac           }
4157d1dd11eSToby Isaac         }
41615496722SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
41715496722SMatthew G. Knepley         PetscFV         fv = (PetscFV) obj;
41815496722SMatthew G. Knepley         PetscQuadrature q;
41915496722SMatthew G. Knepley 
420ee2838f6SToby Isaac         if (h) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP, "Projection height > 0 not supported for finite volume");
42115496722SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
42215496722SMatthew G. Knepley         ierr = PetscFVGetQuadrature(fv, &q);CHKERRQ(ierr);
42315496722SMatthew G. Knepley         ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject) fv), &sp[f]);CHKERRQ(ierr);
42415496722SMatthew G. Knepley         ierr = PetscDualSpaceSetDM(sp[f], dm);CHKERRQ(ierr);
42515496722SMatthew G. Knepley         ierr = PetscDualSpaceSetType(sp[f], PETSCDUALSPACESIMPLE);CHKERRQ(ierr);
42615496722SMatthew G. Knepley         ierr = PetscDualSpaceSimpleSetDimension(sp[f], 1);CHKERRQ(ierr);
42715496722SMatthew G. Knepley         ierr = PetscDualSpaceSimpleSetFunctional(sp[f], 0, q);CHKERRQ(ierr);
42815496722SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
42972f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
43015496722SMatthew G. Knepley       totDim += spDim*numComp[f];
431cb1e1211SMatthew G Knepley     }
4327d1dd11eSToby Isaac     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
4337d1dd11eSToby Isaac     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
434d374af2bSToby Isaac     if (!totDim) continue;
43572f94c41SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4367d1dd11eSToby Isaac     for (p = pStart; p < pEnd; ++p) {
437e1d0b1adSMatthew G. Knepley       PetscFECellGeom geom;
438cb1e1211SMatthew G Knepley 
43931383a9bSToby Isaac       ierr          = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
440910c25dcSToby Isaac       geom.dim      = dim - h;
4417a1a1bd4SToby Isaac       geom.dimEmbed = dimEmbed;
44272f94c41SMatthew G. Knepley       for (f = 0, v = 0; f < numFields; ++f) {
443c110b1eeSGeoffrey Irving         void * const ctx = ctxs ? ctxs[f] : NULL;
4440f2d7e86SMatthew G. Knepley 
4457d1dd11eSToby Isaac         if (!sp[f]) continue;
44672f94c41SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
44772f94c41SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
448120386c5SMatthew G. Knepley           if (funcs[f]) {
449e1d0b1adSMatthew G. Knepley             ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
450120386c5SMatthew G. Knepley           } else {
45115496722SMatthew G. Knepley             for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
452120386c5SMatthew G. Knepley           }
45315496722SMatthew G. Knepley           v += numComp[f];
454cb1e1211SMatthew G Knepley         }
455cb1e1211SMatthew G Knepley       }
4567d1dd11eSToby Isaac       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
457cb1e1211SMatthew G Knepley     }
45872f94c41SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
459ee2838f6SToby Isaac     if (h || !maxHeight) {
4607d1dd11eSToby Isaac       for (f = 0; f < numFields; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
4617d1dd11eSToby Isaac     }
4627d1dd11eSToby Isaac   }
46315496722SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
4647d1dd11eSToby Isaac   if (maxHeight > 0) {
465ee2838f6SToby Isaac     for (f = 0; f < numFields; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);}
4667d1dd11eSToby Isaac     ierr = PetscFree(cellsp);CHKERRQ(ierr);
4677d1dd11eSToby Isaac   }
468cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
469cb1e1211SMatthew G Knepley }
470cb1e1211SMatthew G Knepley 
471cb1e1211SMatthew G Knepley #undef __FUNCT__
4728040c1f3SToby Isaac #define __FUNCT__ "DMPlexProjectFunction"
4738040c1f3SToby Isaac /*@C
4748040c1f3SToby Isaac   DMPlexProjectFunction - This projects the given function into the function space provided.
4758040c1f3SToby Isaac 
4768040c1f3SToby Isaac   Input Parameters:
4778040c1f3SToby Isaac + dm      - The DM
4788040c1f3SToby Isaac . funcs   - The coordinate functions to evaluate, one per field
4798040c1f3SToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
4808040c1f3SToby Isaac - mode    - The insertion mode for values
4818040c1f3SToby Isaac 
4828040c1f3SToby Isaac   Output Parameter:
4838040c1f3SToby Isaac . X - vector
4848040c1f3SToby Isaac 
4858040c1f3SToby Isaac   Level: developer
4868040c1f3SToby Isaac 
4878040c1f3SToby Isaac .seealso: DMPlexComputeL2Diff()
4888040c1f3SToby Isaac @*/
4898040c1f3SToby Isaac PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
4908040c1f3SToby Isaac {
4918040c1f3SToby Isaac   Vec            localX;
4928040c1f3SToby Isaac   PetscErrorCode ierr;
4938040c1f3SToby Isaac 
4948040c1f3SToby Isaac   PetscFunctionBegin;
4958040c1f3SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4968040c1f3SToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
4978040c1f3SToby Isaac   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
4988040c1f3SToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
4998040c1f3SToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
5008040c1f3SToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
501cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
502cb1e1211SMatthew G Knepley }
503cb1e1211SMatthew G Knepley 
504cb1e1211SMatthew G Knepley #undef __FUNCT__
5050f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
5063bc3b0a0SMatthew 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)
5070f2d7e86SMatthew G. Knepley {
5080f2d7e86SMatthew G. Knepley   DM                dmAux;
5092764a2aaSMatthew G. Knepley   PetscDS           prob, probAux;
5100f2d7e86SMatthew G. Knepley   Vec               A;
511326413afSMatthew G. Knepley   PetscSection      section, sectionAux;
512326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
5130f2d7e86SMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux;
5149ac3fadcSMatthew G. Knepley   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight;
5150f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
5160f2d7e86SMatthew G. Knepley 
5170f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
518048b7b1eSToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
519048b7b1eSToby Isaac   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
5202764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
521c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5220f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5230f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
5240f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5252764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
5262764a2aaSMatthew G. Knepley   ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr);
5272764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
5282764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
5290f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
5300f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
5310f2d7e86SMatthew G. Knepley   if (dmAux) {
5322764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
533326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
5342764a2aaSMatthew G. Knepley     ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr);
5352764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
5360f2d7e86SMatthew G. Knepley   }
537d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
5380f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
5390f2d7e86SMatthew 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);
5400f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5410f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
5429ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5439ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
5440f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
545326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
546326413afSMatthew G. Knepley 
5478e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
548326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
549326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5500f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
5513113607cSMatthew G. Knepley       PetscFE        fe;
5523113607cSMatthew G. Knepley       PetscDualSpace sp;
5533113607cSMatthew G. Knepley       PetscInt       Ncf;
5543113607cSMatthew G. Knepley 
5552764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
5563113607cSMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
5573113607cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr);
5583113607cSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr);
5590f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
5600f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
5610f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
5620f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
5630f2d7e86SMatthew G. Knepley 
5640f2d7e86SMatthew G. Knepley         if (funcs[f]) {
5653113607cSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr);
5660f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
5673113607cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);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           }
5743113607cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
5750f2d7e86SMatthew G. Knepley         } else {
5760f2d7e86SMatthew G. Knepley           for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0;
5770f2d7e86SMatthew G. Knepley         }
5780f2d7e86SMatthew G. Knepley         v += Ncf;
5790f2d7e86SMatthew G. Knepley       }
5800f2d7e86SMatthew G. Knepley     }
581326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
582326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
5830f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
5840f2d7e86SMatthew G. Knepley   }
5850f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
5860f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
5870f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5880f2d7e86SMatthew G. Knepley }
5890f2d7e86SMatthew G. Knepley 
5900f2d7e86SMatthew G. Knepley #undef __FUNCT__
591d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal"
592d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], void (*func)(const PetscReal[], PetscScalar *, void *), void *ctx, Vec locX)
59355f2e967SMatthew G. Knepley {
59455f2e967SMatthew G. Knepley   void        (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx);
59555f2e967SMatthew G. Knepley   void         **ctxs;
596d7ddef95SMatthew G. Knepley   PetscInt       numFields;
597d7ddef95SMatthew G. Knepley   PetscErrorCode ierr;
598d7ddef95SMatthew G. Knepley 
599d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
600d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
601d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
602d7ddef95SMatthew G. Knepley   funcs[field] = func;
603d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
604d7ddef95SMatthew G. Knepley   ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
605d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
606d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
607d7ddef95SMatthew G. Knepley }
608d7ddef95SMatthew G. Knepley 
609d7ddef95SMatthew G. Knepley #undef __FUNCT__
610d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal"
611d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad,
612d7ddef95SMatthew 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)
613d7ddef95SMatthew G. Knepley {
61461f58d28SMatthew G. Knepley   PetscDS            prob;
615da97024aSMatthew G. Knepley   PetscSF            sf;
616d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
617d7ddef95SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *grad;
618da97024aSMatthew G. Knepley   const PetscInt    *leaves;
619d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
620520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
621d7ddef95SMatthew G. Knepley   PetscErrorCode     ierr;
622d7ddef95SMatthew G. Knepley 
623d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
624da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
625da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
626da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
627d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
628d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
62961f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
630d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
631d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
632d7ddef95SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
633d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
634d7ddef95SMatthew G. Knepley   if (Grad) {
635c0a6632aSMatthew G. Knepley     PetscFV fv;
636c0a6632aSMatthew G. Knepley 
637c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
638d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
639d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
640d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
641d7ddef95SMatthew G. Knepley     ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
642d7ddef95SMatthew G. Knepley   }
643d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
644d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
645d7ddef95SMatthew G. Knepley     IS              faceIS;
646d7ddef95SMatthew G. Knepley     const PetscInt *faces;
647d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
648d7ddef95SMatthew G. Knepley 
649d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
650d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
651d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
652d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
653d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
654d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
655d7ddef95SMatthew G. Knepley       const PetscFVFaceGeom *fg;
656d7ddef95SMatthew G. Knepley 
657d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
658da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
659da97024aSMatthew G. Knepley       if (loc >= 0) continue;
660d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
661d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
662d7ddef95SMatthew G. Knepley       if (Grad) {
663d7ddef95SMatthew G. Knepley         const PetscFVCellGeom *cg;
664d7ddef95SMatthew G. Knepley         const PetscScalar     *cx, *cgrad;
665d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
666d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
667d7ddef95SMatthew G. Knepley         PetscInt               d;
668d7ddef95SMatthew G. Knepley 
669d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
670d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
671d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
672520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
673d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
674d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
675520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
676d7ddef95SMatthew G. Knepley       } else {
677d7ddef95SMatthew G. Knepley         const PetscScalar *xI;
678d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
679d7ddef95SMatthew G. Knepley 
680d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
681520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
682520b3818SMatthew G. Knepley         ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr);
683d7ddef95SMatthew G. Knepley       }
684d7ddef95SMatthew G. Knepley     }
685d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
686d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
687d7ddef95SMatthew G. Knepley   }
688d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
689d7ddef95SMatthew G. Knepley   if (Grad) {
690d7ddef95SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr);
691d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
692d7ddef95SMatthew G. Knepley   }
693d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
694d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
695d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
696d7ddef95SMatthew G. Knepley }
697d7ddef95SMatthew G. Knepley 
698d7ddef95SMatthew G. Knepley #undef __FUNCT__
699d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues"
700d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
701d7ddef95SMatthew G. Knepley {
702d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
70355f2e967SMatthew G. Knepley   PetscErrorCode ierr;
70455f2e967SMatthew G. Knepley 
70555f2e967SMatthew G. Knepley   PetscFunctionBegin;
70655f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
707d7ddef95SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
708d7ddef95SMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
709d7ddef95SMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
710d7ddef95SMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
71155f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
71255f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
71355f2e967SMatthew G. Knepley     PetscBool       isEssential;
714d7ddef95SMatthew G. Knepley     const char     *labelname;
715d7ddef95SMatthew G. Knepley     DMLabel         label;
716d7ddef95SMatthew G. Knepley     PetscInt        field;
717d7ddef95SMatthew G. Knepley     PetscObject     obj;
718d7ddef95SMatthew G. Knepley     PetscClassId    id;
71955f2e967SMatthew G. Knepley     void          (*func)();
720d7ddef95SMatthew G. Knepley     PetscInt        numids;
721d7ddef95SMatthew G. Knepley     const PetscInt *ids;
72255f2e967SMatthew G. Knepley     void           *ctx;
72355f2e967SMatthew G. Knepley 
72463d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
725d7ddef95SMatthew G. Knepley     if (!isEssential) continue;
72663d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
727d7ddef95SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
728d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
729d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
730d7ddef95SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (void (*)(const PetscReal[], PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
731d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
73243ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
733d7ddef95SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM,
734d7ddef95SMatthew G. Knepley                                                      field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
735d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
73655f2e967SMatthew G. Knepley   }
73755f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
73855f2e967SMatthew G. Knepley }
73955f2e967SMatthew G. Knepley 
740cb1e1211SMatthew G Knepley #undef __FUNCT__
741cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
742cb1e1211SMatthew G Knepley /*@C
743cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
744cb1e1211SMatthew G Knepley 
745cb1e1211SMatthew G Knepley   Input Parameters:
746cb1e1211SMatthew G Knepley + dm    - The DM
747cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
74851259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
749cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
750cb1e1211SMatthew G Knepley 
751cb1e1211SMatthew G Knepley   Output Parameter:
752cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
753cb1e1211SMatthew G Knepley 
754cb1e1211SMatthew G Knepley   Level: developer
755cb1e1211SMatthew G Knepley 
75615496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff()
757878cb397SSatish Balay @*/
7580f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
759cb1e1211SMatthew G Knepley {
760cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
761cb1e1211SMatthew G Knepley   PetscSection     section;
762c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
763cb1e1211SMatthew G Knepley   Vec              localX;
76415496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
765cb1e1211SMatthew G Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
766cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
76715496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7689ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
769cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
770cb1e1211SMatthew G Knepley 
771cb1e1211SMatthew G Knepley   PetscFunctionBegin;
772c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
773cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
774cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
775cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
776cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
777cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
778cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
77915496722SMatthew G. Knepley     PetscObject  obj;
78015496722SMatthew G. Knepley     PetscClassId id;
781c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
782c5bbbd5bSMatthew G. Knepley 
78315496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
78415496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
78515496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
78615496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
78715496722SMatthew G. Knepley 
7880f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7890f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
79015496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
79115496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
79215496722SMatthew G. Knepley 
79315496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
79415496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
79515496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
796c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
797cb1e1211SMatthew G Knepley   }
79815496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
7990f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
80015496722SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
801cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8029ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8039ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
804cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
805a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
806cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
807cb1e1211SMatthew G Knepley 
8088e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
809cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
810cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
811cb1e1211SMatthew G Knepley 
81215496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
81315496722SMatthew G. Knepley       PetscObject  obj;
81415496722SMatthew G. Knepley       PetscClassId id;
815c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
81615496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
817cb1e1211SMatthew G Knepley 
81815496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
81915496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
82015496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
82115496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
82215496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
823cb1e1211SMatthew G Knepley       if (debug) {
824cb1e1211SMatthew G Knepley         char title[1024];
825cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
82615496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
827cb1e1211SMatthew G Knepley       }
828cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
82915496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
830c110b1eeSGeoffrey Irving         (*funcs[field])(coords, funcVal, ctx);
83115496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83215496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
83315496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
83415496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
83515496722SMatthew 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);}
83615496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
837cb1e1211SMatthew G Knepley         }
838cb1e1211SMatthew G Knepley       }
83915496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
840cb1e1211SMatthew G Knepley     }
841cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
842cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
843cb1e1211SMatthew G Knepley     localDiff += elemDiff;
844cb1e1211SMatthew G Knepley   }
84515496722SMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
846cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
84786a74ee0SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
848cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
849cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
850cb1e1211SMatthew G Knepley }
851cb1e1211SMatthew G Knepley 
852cb1e1211SMatthew G Knepley #undef __FUNCT__
85340e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
85440e14135SMatthew G. Knepley /*@C
85540e14135SMatthew 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.
85640e14135SMatthew G. Knepley 
85740e14135SMatthew G. Knepley   Input Parameters:
85840e14135SMatthew G. Knepley + dm    - The DM
85940e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
86051259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
86140e14135SMatthew G. Knepley . X     - The coefficient vector u_h
86240e14135SMatthew G. Knepley - n     - The vector to project along
86340e14135SMatthew G. Knepley 
86440e14135SMatthew G. Knepley   Output Parameter:
86540e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
86640e14135SMatthew G. Knepley 
86740e14135SMatthew G. Knepley   Level: developer
86840e14135SMatthew G. Knepley 
86940e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
87040e14135SMatthew G. Knepley @*/
8710f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
872cb1e1211SMatthew G Knepley {
87340e14135SMatthew G. Knepley   const PetscInt  debug = 0;
874cb1e1211SMatthew G Knepley   PetscSection    section;
87540e14135SMatthew G. Knepley   PetscQuadrature quad;
87640e14135SMatthew G. Knepley   Vec             localX;
87740e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
87840e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
87940e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
8809ac3fadcSMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp;
881cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
882cb1e1211SMatthew G Knepley 
883cb1e1211SMatthew G Knepley   PetscFunctionBegin;
884c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
88540e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
88640e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
88740e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
88840e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
88940e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
890652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8910f2d7e86SMatthew G. Knepley     PetscFE  fe;
89240e14135SMatthew G. Knepley     PetscInt Nc;
893652b88e8SMatthew G. Knepley 
8940f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8950f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8960f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
89740e14135SMatthew G. Knepley     numComponents += Nc;
898652b88e8SMatthew G. Knepley   }
89940e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
90040e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
90140e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9029ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
9039ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
90440e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
90540e14135SMatthew G. Knepley     PetscScalar *x = NULL;
90640e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
907652b88e8SMatthew G. Knepley 
9088e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
90940e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
91040e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
91140e14135SMatthew G. Knepley 
91240e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
9130f2d7e86SMatthew G. Knepley       PetscFE          fe;
91451259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
91521454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
91640e14135SMatthew G. Knepley       PetscReal       *basisDer;
91721454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
91840e14135SMatthew G. Knepley 
9190f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
92021454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
9210f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
9220f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
9230f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
92440e14135SMatthew G. Knepley       if (debug) {
92540e14135SMatthew G. Knepley         char title[1024];
92640e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
92740e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
928652b88e8SMatthew G. Knepley       }
92940e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
93040e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
93140e14135SMatthew G. Knepley           coords[d] = v0[d];
93240e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
93340e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
934652b88e8SMatthew G. Knepley           }
93540e14135SMatthew G. Knepley         }
93651259fa3SMatthew G. Knepley         (*funcs[field])(coords, n, funcVal, ctx);
93740e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
93840e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
93940e14135SMatthew G. Knepley 
94040e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
94140e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
94240e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
94340e14135SMatthew G. Knepley 
94440e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
94540e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
94640e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
94740e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
94840e14135SMatthew G. Knepley               }
94940e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
95040e14135SMatthew G. Knepley             }
95140e14135SMatthew G. Knepley           }
95240e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
95340e14135SMatthew 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);}
95440e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
95540e14135SMatthew G. Knepley         }
95640e14135SMatthew G. Knepley       }
95740e14135SMatthew G. Knepley       comp        += Ncomp;
95840e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
95940e14135SMatthew G. Knepley     }
96040e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
96140e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
96240e14135SMatthew G. Knepley     localDiff += elemDiff;
96340e14135SMatthew G. Knepley   }
96440e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
96540e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
96640e14135SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
96740e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
968cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
969cb1e1211SMatthew G Knepley }
970cb1e1211SMatthew G Knepley 
971a0845e3aSMatthew G. Knepley #undef __FUNCT__
97273d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
97315496722SMatthew G. Knepley /*@C
97415496722SMatthew 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.
97515496722SMatthew G. Knepley 
97615496722SMatthew G. Knepley   Input Parameters:
97715496722SMatthew G. Knepley + dm    - The DM
97815496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component
97915496722SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
98015496722SMatthew G. Knepley - X     - The coefficient vector u_h
98115496722SMatthew G. Knepley 
98215496722SMatthew G. Knepley   Output Parameter:
98315496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2
98415496722SMatthew G. Knepley 
98515496722SMatthew G. Knepley   Level: developer
98615496722SMatthew G. Knepley 
98715496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff()
98815496722SMatthew G. Knepley @*/
9890f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
99073d901b8SMatthew G. Knepley {
99173d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
99273d901b8SMatthew G. Knepley   PetscSection     section;
99373d901b8SMatthew G. Knepley   PetscQuadrature  quad;
99473d901b8SMatthew G. Knepley   Vec              localX;
99515496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
99673d901b8SMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
99773d901b8SMatthew G. Knepley   PetscReal       *localDiff;
99815496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
9999ac3fadcSMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset;
100073d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
100173d901b8SMatthew G. Knepley 
100273d901b8SMatthew G. Knepley   PetscFunctionBegin;
1003c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
100473d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
100573d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
100673d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
100773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
100873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
100973d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
101015496722SMatthew G. Knepley     PetscObject  obj;
101115496722SMatthew G. Knepley     PetscClassId id;
101273d901b8SMatthew G. Knepley     PetscInt     Nc;
101373d901b8SMatthew G. Knepley 
101415496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
101515496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
101615496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
101715496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
101815496722SMatthew G. Knepley 
10190f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10200f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
102115496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
102215496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
102315496722SMatthew G. Knepley 
102415496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
102515496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
102615496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
102773d901b8SMatthew G. Knepley     numComponents += Nc;
102873d901b8SMatthew G. Knepley   }
102915496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
10300f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
103115496722SMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
103273d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10339ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10349ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
103573d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
103673d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
103773d901b8SMatthew G. Knepley 
10388e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
103973d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
104073d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
104173d901b8SMatthew G. Knepley 
104215496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
104315496722SMatthew G. Knepley       PetscObject  obj;
104415496722SMatthew G. Knepley       PetscClassId id;
104573d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
104615496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
104773d901b8SMatthew G. Knepley 
104815496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
104915496722SMatthew G. Knepley 
105015496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
105115496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
105215496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
105315496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
105415496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
105573d901b8SMatthew G. Knepley       if (debug) {
105673d901b8SMatthew G. Knepley         char title[1024];
105773d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
105815496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
105973d901b8SMatthew G. Knepley       }
106073d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
106115496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
106273d901b8SMatthew G. Knepley         (*funcs[field])(coords, funcVal, ctx);
106315496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106415496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
106515496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
106615496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
106715496722SMatthew 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);}
106815496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
106973d901b8SMatthew G. Knepley         }
107073d901b8SMatthew G. Knepley       }
107115496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
107273d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
107373d901b8SMatthew G. Knepley     }
107473d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
107573d901b8SMatthew G. Knepley   }
107673d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
107773d901b8SMatthew G. Knepley   ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
107873d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
107915496722SMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
108073d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
108173d901b8SMatthew G. Knepley }
108273d901b8SMatthew G. Knepley 
108373d901b8SMatthew G. Knepley #undef __FUNCT__
108473d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
108573d901b8SMatthew G. Knepley /*@
108673d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
108773d901b8SMatthew G. Knepley 
108873d901b8SMatthew G. Knepley   Input Parameters:
108973d901b8SMatthew G. Knepley + dm - The mesh
109073d901b8SMatthew G. Knepley . X  - Local input vector
109173d901b8SMatthew G. Knepley - user - The user context
109273d901b8SMatthew G. Knepley 
109373d901b8SMatthew G. Knepley   Output Parameter:
109473d901b8SMatthew G. Knepley . integral - Local integral for each field
109573d901b8SMatthew G. Knepley 
109673d901b8SMatthew G. Knepley   Level: developer
109773d901b8SMatthew G. Knepley 
109873d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
109973d901b8SMatthew G. Knepley @*/
11000f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
110173d901b8SMatthew G. Knepley {
110273d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
110373d901b8SMatthew G. Knepley   DM                dmAux;
110473d901b8SMatthew G. Knepley   Vec               localX, A;
11052764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
110673d901b8SMatthew G. Knepley   PetscQuadrature   q;
110773d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
1108bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
110973d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
11109ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c;
11110f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
111273d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
111373d901b8SMatthew G. Knepley 
111473d901b8SMatthew G. Knepley   PetscFunctionBegin;
111573d901b8SMatthew G. Knepley   /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
111673d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
111773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
111873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1119c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
112073d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
11212764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
11222764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
112373d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
112473d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11259ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11269ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
112773d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
11280f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {integral[f]    = 0.0;}
112973d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
113073d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
113173d901b8SMatthew G. Knepley   if (dmAux) {
113273d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
11332764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
11342764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
113573d901b8SMatthew G. Knepley   }
1136d7ddef95SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1137bbce034cSMatthew G. Knepley   ierr = PetscMalloc2(numCells*totDim,&u,numCells,&cgeom);CHKERRQ(ierr);
11380f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
113973d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
114073d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
114173d901b8SMatthew G. Knepley     PetscInt     i;
114273d901b8SMatthew G. Knepley 
1143bbce034cSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr);
1144bbce034cSMatthew 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);
114573d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
11460f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
114773d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
114873d901b8SMatthew G. Knepley     if (dmAux) {
114973d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
11500f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
115173d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
115273d901b8SMatthew G. Knepley     }
115373d901b8SMatthew G. Knepley   }
115473d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
11550f2d7e86SMatthew G. Knepley     PetscFE  fe;
115673d901b8SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
115773d901b8SMatthew G. Knepley     /* Conforming batches */
115873d901b8SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
115973d901b8SMatthew G. Knepley     /* Remainder */
116073d901b8SMatthew G. Knepley     PetscInt Nr, offset;
116173d901b8SMatthew G. Knepley 
11622764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
11630f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
11640f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11650f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
116673d901b8SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
116773d901b8SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
116873d901b8SMatthew G. Knepley     batchSize = numBlocks * blockSize;
11690f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
117073d901b8SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
117173d901b8SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
117273d901b8SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
117373d901b8SMatthew G. Knepley     offset    = numCells - Nr;
1174bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, integral);CHKERRQ(ierr);
1175bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr);
117673d901b8SMatthew G. Knepley   }
1177bbce034cSMatthew G. Knepley   ierr = PetscFree2(u,cgeom);CHKERRQ(ierr);
117873d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
117973d901b8SMatthew G. Knepley   if (mesh->printFEM) {
118073d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
118173d901b8SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);}
118273d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
118373d901b8SMatthew G. Knepley   }
118473d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
118573d901b8SMatthew G. Knepley   /* TODO: Allreduce for integral */
118673d901b8SMatthew G. Knepley   /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
118773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
118873d901b8SMatthew G. Knepley }
118973d901b8SMatthew G. Knepley 
119073d901b8SMatthew G. Knepley #undef __FUNCT__
1191d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1192d69c5d34SMatthew G. Knepley /*@
1193d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1194d69c5d34SMatthew G. Knepley 
1195d69c5d34SMatthew G. Knepley   Input Parameters:
1196d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1197d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1198d69c5d34SMatthew G. Knepley - user - The user context
1199d69c5d34SMatthew G. Knepley 
1200d69c5d34SMatthew G. Knepley   Output Parameter:
1201934789fcSMatthew G. Knepley . In  - The interpolation matrix
1202d69c5d34SMatthew G. Knepley 
1203d69c5d34SMatthew G. Knepley   Note:
1204d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1205d69c5d34SMatthew G. Knepley 
1206d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1207d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1208d69c5d34SMatthew G. Knepley 
1209d69c5d34SMatthew G. Knepley   Level: developer
1210d69c5d34SMatthew G. Knepley 
1211d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1212d69c5d34SMatthew G. Knepley @*/
1213934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1214d69c5d34SMatthew G. Knepley {
1215d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1216d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
12172764a2aaSMatthew G. Knepley   PetscDS           prob;
1218d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
1219d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1220d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1221d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
12229ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
12230f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1224d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1225d69c5d34SMatthew G. Knepley 
1226d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1227d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1228c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1229d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1230d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1231d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1232d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1233d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1234d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
12359ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12369ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
12372764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
1238d69c5d34SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
1239d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
12400f2d7e86SMatthew G. Knepley     PetscFE  fe;
12410f2d7e86SMatthew G. Knepley     PetscInt rNb, Nc;
1242d69c5d34SMatthew G. Knepley 
12432764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
12440f2d7e86SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1245d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
12460f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
12470f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1248d69c5d34SMatthew G. Knepley   }
12492764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
12500f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
12510f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1252d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1253d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1254d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1255d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1256d69c5d34SMatthew G. Knepley     PetscReal       *points;
1257d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1258d69c5d34SMatthew G. Knepley 
1259d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
1260d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
12610f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
1262d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1263d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1264d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1265d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1266d69c5d34SMatthew G. Knepley       npoints += Np;
1267d69c5d34SMatthew G. Knepley     }
1268d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1269d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1270d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1271d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1272d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1273d69c5d34SMatthew G. Knepley     }
1274d69c5d34SMatthew G. Knepley 
1275d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
12760f2d7e86SMatthew G. Knepley       PetscFE    fe;
1277d69c5d34SMatthew G. Knepley       PetscReal *B;
127836a6d9c0SMatthew G. Knepley       PetscInt   NcJ, cpdim, j;
1279d69c5d34SMatthew G. Knepley 
1280d69c5d34SMatthew G. Knepley       /* Evaluate basis at points */
12812764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr);
12820f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
12830f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1284ffe73a53SMatthew G. Knepley       /* For now, fields only interpolate themselves */
1285ffe73a53SMatthew G. Knepley       if (fieldI == fieldJ) {
12867c927364SMatthew 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);
12870f2d7e86SMatthew G. Knepley         ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1288d69c5d34SMatthew G. Knepley         for (i = 0, k = 0; i < fpdim; ++i) {
1289d69c5d34SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1290d69c5d34SMatthew G. Knepley           ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1291d69c5d34SMatthew G. Knepley           for (p = 0; p < Np; ++p, ++k) {
129236a6d9c0SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
12930f2d7e86SMatthew 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];
129436a6d9c0SMatthew G. Knepley             }
1295d69c5d34SMatthew G. Knepley           }
1296d69c5d34SMatthew G. Knepley         }
12970f2d7e86SMatthew G. Knepley         ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1298ffe73a53SMatthew G. Knepley       }
129936a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1300d69c5d34SMatthew G. Knepley     }
1301d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1302549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1303d69c5d34SMatthew G. Knepley   }
13040f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
13057f5b169aSMatthew G. Knepley   /* Preallocate matrix */
13067f5b169aSMatthew G. Knepley   {
13077f5b169aSMatthew G. Knepley     PetscHashJK ht;
13087f5b169aSMatthew G. Knepley     PetscLayout rLayout;
13097f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
13107f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
13117f5b169aSMatthew G. Knepley 
13127f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
13137f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
13147f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
13157f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
13167f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
13177f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
13187f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
13197f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
13207f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
13217f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
13227f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
13237f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
13247f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
13257f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
13267f5b169aSMatthew G. Knepley 
13277f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
13287f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
13297f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
13307f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
13317f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
13327f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
13337f5b169aSMatthew G. Knepley           if (missing) {
13347f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
13357f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
13367f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
13377f5b169aSMatthew G. Knepley           }
13387f5b169aSMatthew G. Knepley         }
13397f5b169aSMatthew G. Knepley       }
13407f5b169aSMatthew G. Knepley     }
13417f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
13427f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
13437f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
13447f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
13457f5b169aSMatthew G. Knepley   }
13467f5b169aSMatthew G. Knepley   /* Fill matrix */
13477f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1348d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1349934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1350d69c5d34SMatthew G. Knepley   }
1351549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
1352d69c5d34SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
1353549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1354934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1355934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1356d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1357d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1358934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1359934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1360d69c5d34SMatthew G. Knepley   }
1361d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1362d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1363d69c5d34SMatthew G. Knepley }
13646c73c22cSMatthew G. Knepley 
13656c73c22cSMatthew G. Knepley #undef __FUNCT__
13667c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
13677c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
13687c927364SMatthew G. Knepley {
1369e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
13707c927364SMatthew G. Knepley   PetscFE       *feRef;
13717c927364SMatthew G. Knepley   Vec            fv, cv;
13727c927364SMatthew G. Knepley   IS             fis, cis;
13737c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
13747c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
13759ac3fadcSMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, offsetC, offsetF, m;
13767c927364SMatthew G. Knepley   PetscErrorCode ierr;
13777c927364SMatthew G. Knepley 
13787c927364SMatthew G. Knepley   PetscFunctionBegin;
137975a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1380c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
13817c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
13827c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
13837c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
13847c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
13857c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
13867c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
13879ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
13889ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1389e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
13907c927364SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
13917c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
13927c927364SMatthew G. Knepley     PetscFE  fe;
13937c927364SMatthew G. Knepley     PetscInt fNb, Nc;
13947c927364SMatthew G. Knepley 
1395e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
13967c927364SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
13977c927364SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
13987c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
13997c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
14007c927364SMatthew G. Knepley   }
1401e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
14027c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
14037c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
14047c927364SMatthew G. Knepley     PetscFE        feC;
14057c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
14067c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
14077c927364SMatthew G. Knepley 
1408e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
14097c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
14107c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
14117c927364SMatthew 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);
14127c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
14137c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
14147c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
14157c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
14167c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
14177c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
14187c927364SMatthew G. Knepley       const PetscReal *cqpoints;
14197c927364SMatthew G. Knepley       PetscInt         NpC;
14207c927364SMatthew G. Knepley 
14217c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
14227c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
14237c927364SMatthew G. Knepley       if (NpC != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
14247c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
14257c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
14267c927364SMatthew G. Knepley         const PetscReal *fqpoints;
14277c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
14287c927364SMatthew G. Knepley         PetscInt         NpF, comp;
14297c927364SMatthew G. Knepley 
14307c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
14317c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
14327c927364SMatthew G. Knepley         if (NpC != NpF) continue;
14337c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
14347c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
14357c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
14367c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
14377c927364SMatthew G. Knepley         }
14387c927364SMatthew G. Knepley         break;
14397c927364SMatthew G. Knepley       }
14407c927364SMatthew G. Knepley     }
14417c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
14427c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
14437c927364SMatthew G. Knepley   }
14447c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
14457c927364SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
14467c927364SMatthew G. Knepley 
14477c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
14487c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
14497c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
14507c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
14517c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1452aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1453aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
14547c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
14557c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
14567c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
14577c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
14587c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
14597c927364SMatthew 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]]);
14607c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
14617c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
14627c927364SMatthew G. Knepley     }
14637c927364SMatthew G. Knepley   }
14647c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
14657c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
14667c927364SMatthew G. Knepley 
14677c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
14687c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
14697c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
14707c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
14717c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
14727c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
14737c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
147475a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1475cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1476cb1e1211SMatthew G Knepley }
1477