xref: /petsc/src/dm/impls/plex/plexfem.c (revision b29cfa1c0f7944571ef0a263c2bd25d4db888f1b)
1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2cb1e1211SMatthew G Knepley 
30f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h>
4f62f30faSMatthew G. Knepley #include <petscfv.h>
5a0845e3aSMatthew G. Knepley 
6cb1e1211SMatthew G Knepley #undef __FUNCT__
7cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale"
8cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
9cb1e1211SMatthew G Knepley {
10cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
11cb1e1211SMatthew G Knepley 
12cb1e1211SMatthew G Knepley   PetscFunctionBegin;
13cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
15cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
16cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
17cb1e1211SMatthew G Knepley }
18cb1e1211SMatthew G Knepley 
19cb1e1211SMatthew G Knepley #undef __FUNCT__
20cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale"
21cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
22cb1e1211SMatthew G Knepley {
23cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
24cb1e1211SMatthew G Knepley 
25cb1e1211SMatthew G Knepley   PetscFunctionBegin;
26cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
28cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
29cb1e1211SMatthew G Knepley }
30cb1e1211SMatthew G Knepley 
31cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k)
32cb1e1211SMatthew G Knepley {
33cb1e1211SMatthew G Knepley   switch (i) {
34cb1e1211SMatthew G Knepley   case 0:
35cb1e1211SMatthew G Knepley     switch (j) {
36cb1e1211SMatthew G Knepley     case 0: return 0;
37cb1e1211SMatthew G Knepley     case 1:
38cb1e1211SMatthew G Knepley       switch (k) {
39cb1e1211SMatthew G Knepley       case 0: return 0;
40cb1e1211SMatthew G Knepley       case 1: return 0;
41cb1e1211SMatthew G Knepley       case 2: return 1;
42cb1e1211SMatthew G Knepley       }
43cb1e1211SMatthew G Knepley     case 2:
44cb1e1211SMatthew G Knepley       switch (k) {
45cb1e1211SMatthew G Knepley       case 0: return 0;
46cb1e1211SMatthew G Knepley       case 1: return -1;
47cb1e1211SMatthew G Knepley       case 2: return 0;
48cb1e1211SMatthew G Knepley       }
49cb1e1211SMatthew G Knepley     }
50cb1e1211SMatthew G Knepley   case 1:
51cb1e1211SMatthew G Knepley     switch (j) {
52cb1e1211SMatthew G Knepley     case 0:
53cb1e1211SMatthew G Knepley       switch (k) {
54cb1e1211SMatthew G Knepley       case 0: return 0;
55cb1e1211SMatthew G Knepley       case 1: return 0;
56cb1e1211SMatthew G Knepley       case 2: return -1;
57cb1e1211SMatthew G Knepley       }
58cb1e1211SMatthew G Knepley     case 1: return 0;
59cb1e1211SMatthew G Knepley     case 2:
60cb1e1211SMatthew G Knepley       switch (k) {
61cb1e1211SMatthew G Knepley       case 0: return 1;
62cb1e1211SMatthew G Knepley       case 1: return 0;
63cb1e1211SMatthew G Knepley       case 2: return 0;
64cb1e1211SMatthew G Knepley       }
65cb1e1211SMatthew G Knepley     }
66cb1e1211SMatthew G Knepley   case 2:
67cb1e1211SMatthew G Knepley     switch (j) {
68cb1e1211SMatthew G Knepley     case 0:
69cb1e1211SMatthew G Knepley       switch (k) {
70cb1e1211SMatthew G Knepley       case 0: return 0;
71cb1e1211SMatthew G Knepley       case 1: return 1;
72cb1e1211SMatthew G Knepley       case 2: return 0;
73cb1e1211SMatthew G Knepley       }
74cb1e1211SMatthew G Knepley     case 1:
75cb1e1211SMatthew G Knepley       switch (k) {
76cb1e1211SMatthew G Knepley       case 0: return -1;
77cb1e1211SMatthew G Knepley       case 1: return 0;
78cb1e1211SMatthew G Knepley       case 2: return 0;
79cb1e1211SMatthew G Knepley       }
80cb1e1211SMatthew G Knepley     case 2: return 0;
81cb1e1211SMatthew G Knepley     }
82cb1e1211SMatthew G Knepley   }
83cb1e1211SMatthew G Knepley   return 0;
84cb1e1211SMatthew G Knepley }
85cb1e1211SMatthew G Knepley 
86cb1e1211SMatthew G Knepley #undef __FUNCT__
87cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody"
88cb1e1211SMatthew G Knepley /*@C
89cb1e1211SMatthew G Knepley   DMPlexCreateRigidBody - create rigid body modes from coordinates
90cb1e1211SMatthew G Knepley 
91cb1e1211SMatthew G Knepley   Collective on DM
92cb1e1211SMatthew G Knepley 
93cb1e1211SMatthew G Knepley   Input Arguments:
94cb1e1211SMatthew G Knepley + dm - the DM
95cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section
96cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section
97cb1e1211SMatthew G Knepley 
98cb1e1211SMatthew G Knepley   Output Argument:
99cb1e1211SMatthew G Knepley . sp - the null space
100cb1e1211SMatthew G Knepley 
101cb1e1211SMatthew G Knepley   Note: This is necessary to take account of Dirichlet conditions on the displacements
102cb1e1211SMatthew G Knepley 
103cb1e1211SMatthew G Knepley   Level: advanced
104cb1e1211SMatthew G Knepley 
105cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate()
106cb1e1211SMatthew G Knepley @*/
107cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp)
108cb1e1211SMatthew G Knepley {
109cb1e1211SMatthew G Knepley   MPI_Comm       comm;
110cb1e1211SMatthew G Knepley   Vec            coordinates, localMode, mode[6];
111cb1e1211SMatthew G Knepley   PetscSection   coordSection;
112cb1e1211SMatthew G Knepley   PetscScalar   *coords;
113cb1e1211SMatthew G Knepley   PetscInt       dim, vStart, vEnd, v, n, m, d, i, j;
114cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
115cb1e1211SMatthew G Knepley 
116cb1e1211SMatthew G Knepley   PetscFunctionBegin;
117cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
118c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
119cb1e1211SMatthew G Knepley   if (dim == 1) {
120cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
121cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
122cb1e1211SMatthew G Knepley   }
123cb1e1211SMatthew G Knepley   if (!section)       {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
124cb1e1211SMatthew G Knepley   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
125cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
126cb1e1211SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
12769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
128cb1e1211SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
129cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
130cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
131cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
132cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
133cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
134cb1e1211SMatthew G Knepley   /* Assume P1 */
135cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr);
136cb1e1211SMatthew G Knepley   for (d = 0; d < dim; ++d) {
137cb1e1211SMatthew G Knepley     PetscScalar values[3] = {0.0, 0.0, 0.0};
138cb1e1211SMatthew G Knepley 
139cb1e1211SMatthew G Knepley     values[d] = 1.0;
140cb1e1211SMatthew G Knepley     ierr      = VecSet(localMode, 0.0);CHKERRQ(ierr);
141cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
142cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
143cb1e1211SMatthew G Knepley     }
144cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
145cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
146cb1e1211SMatthew G Knepley   }
147cb1e1211SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
148cb1e1211SMatthew G Knepley   for (d = dim; d < dim*(dim+1)/2; ++d) {
149cb1e1211SMatthew G Knepley     PetscInt i, j, k = dim > 2 ? d - dim : d;
150cb1e1211SMatthew G Knepley 
151cb1e1211SMatthew G Knepley     ierr = VecSet(localMode, 0.0);CHKERRQ(ierr);
152cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
153cb1e1211SMatthew G Knepley       PetscScalar values[3] = {0.0, 0.0, 0.0};
154cb1e1211SMatthew G Knepley       PetscInt    off;
155cb1e1211SMatthew G Knepley 
156cb1e1211SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
157cb1e1211SMatthew G Knepley       for (i = 0; i < dim; ++i) {
158cb1e1211SMatthew G Knepley         for (j = 0; j < dim; ++j) {
159cb1e1211SMatthew G Knepley           values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]);
160cb1e1211SMatthew G Knepley         }
161cb1e1211SMatthew G Knepley       }
162cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
163cb1e1211SMatthew G Knepley     }
164cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
165cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
166cb1e1211SMatthew G Knepley   }
167cb1e1211SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
168cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr);
169cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
170cb1e1211SMatthew G Knepley   /* Orthonormalize system */
171cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
172cb1e1211SMatthew G Knepley     PetscScalar dots[6];
173cb1e1211SMatthew G Knepley 
174cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
175cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
176cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
177cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
178cb1e1211SMatthew G Knepley   }
179cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
180cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
181cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
182cb1e1211SMatthew G Knepley }
183cb1e1211SMatthew G Knepley 
184cb1e1211SMatthew G Knepley #undef __FUNCT__
185*b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight"
186*b29cfa1cSToby Isaac /*@
187*b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
188*b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
189*b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
190*b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
191*b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
192*b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
193*b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
194*b29cfa1cSToby Isaac 
195*b29cfa1cSToby Isaac   Input Parameters:
196*b29cfa1cSToby Isaac + dm - the DMPlex object
197*b29cfa1cSToby Isaac - height - the maximum projection height >= 0
198*b29cfa1cSToby Isaac 
199*b29cfa1cSToby Isaac   Level: advanced
200*b29cfa1cSToby Isaac 
201*b29cfa1cSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFieldLocal(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
202*b29cfa1cSToby Isaac @*/
203*b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
204*b29cfa1cSToby Isaac {
205*b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
206*b29cfa1cSToby Isaac 
207*b29cfa1cSToby Isaac   PetscFunctionBegin;
208*b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
209*b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
210*b29cfa1cSToby Isaac   PetscFunctionReturn(0);
211*b29cfa1cSToby Isaac }
212*b29cfa1cSToby Isaac 
213*b29cfa1cSToby Isaac #undef __FUNCT__
214*b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight"
215*b29cfa1cSToby Isaac /*@
216*b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
217*b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
218*b29cfa1cSToby Isaac 
219*b29cfa1cSToby Isaac   Input Parameters:
220*b29cfa1cSToby Isaac . dm - the DMPlex object
221*b29cfa1cSToby Isaac 
222*b29cfa1cSToby Isaac   Output Parameters:
223*b29cfa1cSToby Isaac . height - the maximum projection height
224*b29cfa1cSToby Isaac 
225*b29cfa1cSToby Isaac   Level: intermediate
226*b29cfa1cSToby Isaac 
227*b29cfa1cSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFieldLocal(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal()
228*b29cfa1cSToby Isaac @*/
229*b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
230*b29cfa1cSToby Isaac {
231*b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
232*b29cfa1cSToby Isaac 
233*b29cfa1cSToby Isaac   PetscFunctionBegin;
234*b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
235*b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
236*b29cfa1cSToby Isaac   PetscFunctionReturn(0);
237*b29cfa1cSToby Isaac }
238*b29cfa1cSToby Isaac 
239*b29cfa1cSToby Isaac #undef __FUNCT__
240a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
241a18a7fb9SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
242a18a7fb9SMatthew G. Knepley {
243a18a7fb9SMatthew G. Knepley   PetscDualSpace *sp;
244a18a7fb9SMatthew G. Knepley   PetscSection    section;
245a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
246a18a7fb9SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
247ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
248a18a7fb9SMatthew G. Knepley   PetscInt        numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, f, d, v, i, comp;
249a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
250a18a7fb9SMatthew G. Knepley 
251a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
252c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
253a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
254a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
255a18a7fb9SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&sp,dim,&v0,dim*dim,&J);CHKERRQ(ierr);
256a18a7fb9SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
257a18a7fb9SMatthew G. Knepley     ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr);
258a18a7fb9SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
259a18a7fb9SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
260a18a7fb9SMatthew G. Knepley     totDim += spDim*numComp;
261a18a7fb9SMatthew G. Knepley   }
262a18a7fb9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
263a18a7fb9SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
264a18a7fb9SMatthew 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);
265a18a7fb9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
266ad96f515SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
267ad96f515SMatthew G. Knepley   for (f = 0; f < numFields; ++f) fieldActive[f] = funcs[f] ? PETSC_TRUE : PETSC_FALSE;
268a18a7fb9SMatthew G. Knepley   for (i = 0; i < numIds; ++i) {
269a18a7fb9SMatthew G. Knepley     IS              pointIS;
270a18a7fb9SMatthew G. Knepley     const PetscInt *points;
271a18a7fb9SMatthew G. Knepley     PetscInt        n, p;
272a18a7fb9SMatthew G. Knepley 
273a18a7fb9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
274a18a7fb9SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
275a18a7fb9SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
276a18a7fb9SMatthew G. Knepley     for (p = 0; p < n; ++p) {
277a18a7fb9SMatthew G. Knepley       const PetscInt    point = points[p];
278a18a7fb9SMatthew G. Knepley       PetscCellGeometry geom;
279a18a7fb9SMatthew G. Knepley 
280a18a7fb9SMatthew G. Knepley       if ((point < cStart) || (point >= cEnd)) continue;
2818e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
282a18a7fb9SMatthew G. Knepley       geom.v0   = v0;
283a18a7fb9SMatthew G. Knepley       geom.J    = J;
284a18a7fb9SMatthew G. Knepley       geom.detJ = &detJ;
285a18a7fb9SMatthew G. Knepley       for (f = 0, v = 0; f < numFields; ++f) {
286a18a7fb9SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[f] : NULL;
287a18a7fb9SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
288a18a7fb9SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
289a18a7fb9SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
290a18a7fb9SMatthew G. Knepley           if (funcs[f]) {
291a18a7fb9SMatthew G. Knepley             ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
292a18a7fb9SMatthew G. Knepley           } else {
293a18a7fb9SMatthew G. Knepley             for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
294a18a7fb9SMatthew G. Knepley           }
295a18a7fb9SMatthew G. Knepley           v += numComp;
296a18a7fb9SMatthew G. Knepley         }
297a18a7fb9SMatthew G. Knepley       }
298ad96f515SMatthew G. Knepley       ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
299a18a7fb9SMatthew G. Knepley     }
300a18a7fb9SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
301a18a7fb9SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
302a18a7fb9SMatthew G. Knepley   }
303a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
304ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
305a18a7fb9SMatthew G. Knepley   ierr = PetscFree3(sp,v0,J);CHKERRQ(ierr);
306a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
307a18a7fb9SMatthew G. Knepley }
308a18a7fb9SMatthew G. Knepley 
309a18a7fb9SMatthew G. Knepley #undef __FUNCT__
310cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
3110f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
312cb1e1211SMatthew G Knepley {
31372f94c41SMatthew G. Knepley   PetscDualSpace *sp;
31472f94c41SMatthew G. Knepley   PetscSection    section;
31572f94c41SMatthew G. Knepley   PetscScalar    *values;
31672f94c41SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
317120386c5SMatthew G. Knepley   PetscInt        numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp;
318cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
319cb1e1211SMatthew G Knepley 
320cb1e1211SMatthew G Knepley   PetscFunctionBegin;
321cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
32272f94c41SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
323785e854fSJed Brown   ierr = PetscMalloc1(numFields, &sp);CHKERRQ(ierr);
32472f94c41SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3250f2d7e86SMatthew G. Knepley     PetscFE fe;
3260f2d7e86SMatthew G. Knepley 
3270f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
3280f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
3290f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
33072f94c41SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
33172f94c41SMatthew G. Knepley     totDim += spDim*numComp;
332cb1e1211SMatthew G Knepley   }
333c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33472f94c41SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
33572f94c41SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
33672f94c41SMatthew 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);
33772f94c41SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
338dcca6d9dSJed Brown   ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr);
33972f94c41SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
34072f94c41SMatthew G. Knepley     PetscCellGeometry geom;
341cb1e1211SMatthew G Knepley 
3428e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
34372f94c41SMatthew G. Knepley     geom.v0   = v0;
34472f94c41SMatthew G. Knepley     geom.J    = J;
34572f94c41SMatthew G. Knepley     geom.detJ = &detJ;
34672f94c41SMatthew G. Knepley     for (f = 0, v = 0; f < numFields; ++f) {
3470f2d7e86SMatthew G. Knepley       PetscFE      fe;
348c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[f] : NULL;
3490f2d7e86SMatthew G. Knepley 
3500f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
3510f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
35272f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
35372f94c41SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
354120386c5SMatthew G. Knepley         if (funcs[f]) {
355c110b1eeSGeoffrey Irving           ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
356120386c5SMatthew G. Knepley         } else {
357120386c5SMatthew G. Knepley           for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
358120386c5SMatthew G. Knepley         }
35972f94c41SMatthew G. Knepley         v += numComp;
360cb1e1211SMatthew G Knepley       }
361cb1e1211SMatthew G Knepley     }
36272f94c41SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
363cb1e1211SMatthew G Knepley   }
36472f94c41SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
3651f2da991SMatthew G. Knepley   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
36672f94c41SMatthew G. Knepley   ierr = PetscFree(sp);CHKERRQ(ierr);
367cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
368cb1e1211SMatthew G Knepley }
369cb1e1211SMatthew G Knepley 
370cb1e1211SMatthew G Knepley #undef __FUNCT__
371cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction"
372cb1e1211SMatthew G Knepley /*@C
373cb1e1211SMatthew G Knepley   DMPlexProjectFunction - This projects the given function into the function space provided.
374cb1e1211SMatthew G Knepley 
375cb1e1211SMatthew G Knepley   Input Parameters:
376cb1e1211SMatthew G Knepley + dm      - The DM
37772f94c41SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
378c110b1eeSGeoffrey Irving . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
379cb1e1211SMatthew G Knepley - mode    - The insertion mode for values
380cb1e1211SMatthew G Knepley 
381cb1e1211SMatthew G Knepley   Output Parameter:
382cb1e1211SMatthew G Knepley . X - vector
383cb1e1211SMatthew G Knepley 
384cb1e1211SMatthew G Knepley   Level: developer
385cb1e1211SMatthew G Knepley 
386878cb397SSatish Balay .seealso: DMPlexComputeL2Diff()
387878cb397SSatish Balay @*/
3880f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
389cb1e1211SMatthew G Knepley {
390cb1e1211SMatthew G Knepley   Vec            localX;
391cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
392cb1e1211SMatthew G Knepley 
393cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3949a800dd8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
3960f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
397cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
398cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
399cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
400cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
401cb1e1211SMatthew G Knepley }
402cb1e1211SMatthew G Knepley 
40355f2e967SMatthew G. Knepley #undef __FUNCT__
4040f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
4053bc3b0a0SMatthew 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)
4060f2d7e86SMatthew G. Knepley {
4070f2d7e86SMatthew G. Knepley   DM                dmAux;
4082764a2aaSMatthew G. Knepley   PetscDS           prob, probAux;
4090f2d7e86SMatthew G. Knepley   Vec               A;
410326413afSMatthew G. Knepley   PetscSection      section, sectionAux;
411326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
4120f2d7e86SMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux;
413326413afSMatthew G. Knepley   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, c, f, d, v, comp;
4140f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
4150f2d7e86SMatthew G. Knepley 
4160f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
4172764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
418c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4190f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4200f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
4210f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
4222764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
4232764a2aaSMatthew G. Knepley   ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr);
4242764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
4252764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
4260f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
4270f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
4280f2d7e86SMatthew G. Knepley   if (dmAux) {
4292764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
430326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
4312764a2aaSMatthew G. Knepley     ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr);
4322764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
4330f2d7e86SMatthew G. Knepley   }
4340f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localU);CHKERRQ(ierr);
4350f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
4360f2d7e86SMatthew 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);
4370f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4380f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
4390f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
440326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
441326413afSMatthew G. Knepley 
4428e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
443326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
444326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
4450f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
4463113607cSMatthew G. Knepley       PetscFE        fe;
4473113607cSMatthew G. Knepley       PetscDualSpace sp;
4483113607cSMatthew G. Knepley       PetscInt       Ncf;
4493113607cSMatthew G. Knepley 
4502764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
4513113607cSMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
4523113607cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr);
4533113607cSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr);
4540f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
4550f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
4560f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
4570f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
4580f2d7e86SMatthew G. Knepley 
4590f2d7e86SMatthew G. Knepley         if (funcs[f]) {
4603113607cSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr);
4610f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
4623113607cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
4630f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
4640f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
465326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
466326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
4673bc3b0a0SMatthew G. Knepley             (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]);
4680f2d7e86SMatthew G. Knepley           }
4693113607cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
4700f2d7e86SMatthew G. Knepley         } else {
4710f2d7e86SMatthew G. Knepley           for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0;
4720f2d7e86SMatthew G. Knepley         }
4730f2d7e86SMatthew G. Knepley         v += Ncf;
4740f2d7e86SMatthew G. Knepley       }
4750f2d7e86SMatthew G. Knepley     }
476326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
477326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
4780f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
4790f2d7e86SMatthew G. Knepley   }
4800f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4810f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
4820f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
4830f2d7e86SMatthew G. Knepley }
4840f2d7e86SMatthew G. Knepley 
4850f2d7e86SMatthew G. Knepley #undef __FUNCT__
4860f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectField"
4870f2d7e86SMatthew G. Knepley /*@C
4880f2d7e86SMatthew G. Knepley   DMPlexProjectField - This projects the given function of the fields into the function space provided.
4890f2d7e86SMatthew G. Knepley 
4900f2d7e86SMatthew G. Knepley   Input Parameters:
4910f2d7e86SMatthew G. Knepley + dm      - The DM
4920f2d7e86SMatthew G. Knepley . U       - The input field vector
4930f2d7e86SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
4940f2d7e86SMatthew G. Knepley - mode    - The insertion mode for values
4950f2d7e86SMatthew G. Knepley 
4960f2d7e86SMatthew G. Knepley   Output Parameter:
4970f2d7e86SMatthew G. Knepley . X       - The output vector
4980f2d7e86SMatthew G. Knepley 
4990f2d7e86SMatthew G. Knepley   Level: developer
5000f2d7e86SMatthew G. Knepley 
5010f2d7e86SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
5020f2d7e86SMatthew G. Knepley @*/
5033bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectField(DM dm, Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X)
5040f2d7e86SMatthew G. Knepley {
5050f2d7e86SMatthew G. Knepley   Vec            localX, localU;
5060f2d7e86SMatthew G. Knepley   PetscErrorCode ierr;
5070f2d7e86SMatthew G. Knepley 
5080f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
5090f2d7e86SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5100f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
5110f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr);
5120f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
5130f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
5143113607cSMatthew G. Knepley   ierr = DMPlexProjectFieldLocal(dm, localU, funcs, mode, localX);CHKERRQ(ierr);
5150f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
5160f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
5170f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
5180f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr);
5190f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
5200f2d7e86SMatthew G. Knepley }
5210f2d7e86SMatthew G. Knepley 
5220f2d7e86SMatthew G. Knepley #undef __FUNCT__
5233351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM"
5243351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX)
52555f2e967SMatthew G. Knepley {
52655f2e967SMatthew G. Knepley   void        (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx);
52755f2e967SMatthew G. Knepley   void         **ctxs;
52855f2e967SMatthew G. Knepley   PetscFE       *fe;
52955f2e967SMatthew G. Knepley   PetscInt       numFields, f, numBd, b;
53055f2e967SMatthew G. Knepley   PetscErrorCode ierr;
53155f2e967SMatthew G. Knepley 
53255f2e967SMatthew G. Knepley   PetscFunctionBegin;
53355f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53455f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 2);
53555f2e967SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
53655f2e967SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
53755f2e967SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);}
53855f2e967SMatthew G. Knepley   /* OPT: Could attempt to do multiple BCs at once */
53955f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
54055f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
541a18a7fb9SMatthew G. Knepley     DMLabel         label;
54255f2e967SMatthew G. Knepley     const PetscInt *ids;
54363d5297fSMatthew G. Knepley     const char     *labelname;
54455f2e967SMatthew G. Knepley     PetscInt        numids, field;
54555f2e967SMatthew G. Knepley     PetscBool       isEssential;
54655f2e967SMatthew G. Knepley     void          (*func)();
54755f2e967SMatthew G. Knepley     void           *ctx;
54855f2e967SMatthew G. Knepley 
54963d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
55063d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
55155f2e967SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
55255f2e967SMatthew G. Knepley       funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL;
55355f2e967SMatthew G. Knepley       ctxs[f]  = field == f ? ctx : NULL;
55455f2e967SMatthew G. Knepley     }
555a18a7fb9SMatthew G. Knepley     ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
55655f2e967SMatthew G. Knepley   }
55755f2e967SMatthew G. Knepley   ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr);
55855f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
55955f2e967SMatthew G. Knepley }
56055f2e967SMatthew G. Knepley 
561cb1e1211SMatthew G Knepley #undef __FUNCT__
562cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
563cb1e1211SMatthew G Knepley /*@C
564cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
565cb1e1211SMatthew G Knepley 
566cb1e1211SMatthew G Knepley   Input Parameters:
567cb1e1211SMatthew G Knepley + dm    - The DM
568cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
56951259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
570cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
571cb1e1211SMatthew G Knepley 
572cb1e1211SMatthew G Knepley   Output Parameter:
573cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
574cb1e1211SMatthew G Knepley 
575cb1e1211SMatthew G Knepley   Level: developer
576cb1e1211SMatthew G Knepley 
57723d86601SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2GradientDiff()
578878cb397SSatish Balay @*/
5790f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
580cb1e1211SMatthew G Knepley {
581cb1e1211SMatthew G Knepley   const PetscInt  debug = 0;
582cb1e1211SMatthew G Knepley   PetscSection    section;
583c5bbbd5bSMatthew G. Knepley   PetscQuadrature quad;
584cb1e1211SMatthew G Knepley   Vec             localX;
58572f94c41SMatthew G. Knepley   PetscScalar    *funcVal;
586cb1e1211SMatthew G Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
587cb1e1211SMatthew G Knepley   PetscReal       localDiff = 0.0;
588cb1e1211SMatthew G Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
589cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
590cb1e1211SMatthew G Knepley 
591cb1e1211SMatthew G Knepley   PetscFunctionBegin;
592c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
593cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
594cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
595cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
596cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
597cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
598cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
5990f2d7e86SMatthew G. Knepley     PetscFE  fe;
600c5bbbd5bSMatthew G. Knepley     PetscInt Nc;
601c5bbbd5bSMatthew G. Knepley 
6020f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
6030f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
6040f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
605c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
606cb1e1211SMatthew G Knepley   }
6070f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
608dcca6d9dSJed Brown   ierr = PetscMalloc5(numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
609cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
610cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
611a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
612cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
613cb1e1211SMatthew G Knepley 
6148e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
615cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
616cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
617cb1e1211SMatthew G Knepley 
618cb1e1211SMatthew G Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
6190f2d7e86SMatthew G. Knepley       PetscFE          fe;
620c110b1eeSGeoffrey Irving       void * const     ctx = ctxs ? ctxs[field] : NULL;
62121454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
622c5bbbd5bSMatthew G. Knepley       PetscReal       *basis;
62321454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
624cb1e1211SMatthew G Knepley 
6250f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
62621454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
6270f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
6280f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
6290f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
630cb1e1211SMatthew G Knepley       if (debug) {
631cb1e1211SMatthew G Knepley         char title[1024];
632cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
633cb1e1211SMatthew G Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
634cb1e1211SMatthew G Knepley       }
635cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
636cb1e1211SMatthew G Knepley         for (d = 0; d < dim; d++) {
637cb1e1211SMatthew G Knepley           coords[d] = v0[d];
638cb1e1211SMatthew G Knepley           for (e = 0; e < dim; e++) {
639cb1e1211SMatthew G Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
640cb1e1211SMatthew G Knepley           }
641cb1e1211SMatthew G Knepley         }
642c110b1eeSGeoffrey Irving         (*funcs[field])(coords, funcVal, ctx);
643cb1e1211SMatthew G Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
644a1d24da5SMatthew G. Knepley           PetscScalar interpolant = 0.0;
645a1d24da5SMatthew G. Knepley 
646cb1e1211SMatthew G Knepley           for (f = 0; f < numBasisFuncs; ++f) {
647cb1e1211SMatthew G Knepley             const PetscInt fidx = f*numBasisComps+fc;
648a1d24da5SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
649cb1e1211SMatthew G Knepley           }
65072f94c41SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
65172f94c41SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
652cb1e1211SMatthew G Knepley         }
653cb1e1211SMatthew G Knepley       }
654cb1e1211SMatthew G Knepley       comp        += numBasisComps;
655cb1e1211SMatthew G Knepley       fieldOffset += numBasisFuncs*numBasisComps;
656cb1e1211SMatthew G Knepley     }
657cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
658cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
659cb1e1211SMatthew G Knepley     localDiff += elemDiff;
660cb1e1211SMatthew G Knepley   }
66172f94c41SMatthew G. Knepley   ierr  = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
662cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
66386a74ee0SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
664cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
665cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
666cb1e1211SMatthew G Knepley }
667cb1e1211SMatthew G Knepley 
668cb1e1211SMatthew G Knepley #undef __FUNCT__
66940e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
67040e14135SMatthew G. Knepley /*@C
67140e14135SMatthew 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.
67240e14135SMatthew G. Knepley 
67340e14135SMatthew G. Knepley   Input Parameters:
67440e14135SMatthew G. Knepley + dm    - The DM
67540e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
67651259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
67740e14135SMatthew G. Knepley . X     - The coefficient vector u_h
67840e14135SMatthew G. Knepley - n     - The vector to project along
67940e14135SMatthew G. Knepley 
68040e14135SMatthew G. Knepley   Output Parameter:
68140e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
68240e14135SMatthew G. Knepley 
68340e14135SMatthew G. Knepley   Level: developer
68440e14135SMatthew G. Knepley 
68540e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
68640e14135SMatthew G. Knepley @*/
6870f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
688cb1e1211SMatthew G Knepley {
68940e14135SMatthew G. Knepley   const PetscInt  debug = 0;
690cb1e1211SMatthew G Knepley   PetscSection    section;
69140e14135SMatthew G. Knepley   PetscQuadrature quad;
69240e14135SMatthew G. Knepley   Vec             localX;
69340e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
69440e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
69540e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
69640e14135SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
697cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
698cb1e1211SMatthew G Knepley 
699cb1e1211SMatthew G Knepley   PetscFunctionBegin;
700c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
70140e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
70240e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
70340e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
70440e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
70540e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
706652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
7070f2d7e86SMatthew G. Knepley     PetscFE  fe;
70840e14135SMatthew G. Knepley     PetscInt Nc;
709652b88e8SMatthew G. Knepley 
7100f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
7110f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7120f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
71340e14135SMatthew G. Knepley     numComponents += Nc;
714652b88e8SMatthew G. Knepley   }
71540e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
71640e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
71740e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
71840e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
71940e14135SMatthew G. Knepley     PetscScalar *x = NULL;
72040e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
721652b88e8SMatthew G. Knepley 
7228e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
72340e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
72440e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
72540e14135SMatthew G. Knepley 
72640e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
7270f2d7e86SMatthew G. Knepley       PetscFE          fe;
72851259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
72921454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
73040e14135SMatthew G. Knepley       PetscReal       *basisDer;
73121454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
73240e14135SMatthew G. Knepley 
7330f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
73421454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
7350f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
7360f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
7370f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
73840e14135SMatthew G. Knepley       if (debug) {
73940e14135SMatthew G. Knepley         char title[1024];
74040e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
74140e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
742652b88e8SMatthew G. Knepley       }
74340e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
74440e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
74540e14135SMatthew G. Knepley           coords[d] = v0[d];
74640e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
74740e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
748652b88e8SMatthew G. Knepley           }
74940e14135SMatthew G. Knepley         }
75051259fa3SMatthew G. Knepley         (*funcs[field])(coords, n, funcVal, ctx);
75140e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
75240e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
75340e14135SMatthew G. Knepley 
75440e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
75540e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
75640e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
75740e14135SMatthew G. Knepley 
75840e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
75940e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
76040e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
76140e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
76240e14135SMatthew G. Knepley               }
76340e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
76440e14135SMatthew G. Knepley             }
76540e14135SMatthew G. Knepley           }
76640e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
76740e14135SMatthew 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);}
76840e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
76940e14135SMatthew G. Knepley         }
77040e14135SMatthew G. Knepley       }
77140e14135SMatthew G. Knepley       comp        += Ncomp;
77240e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
77340e14135SMatthew G. Knepley     }
77440e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
77540e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
77640e14135SMatthew G. Knepley     localDiff += elemDiff;
77740e14135SMatthew G. Knepley   }
77840e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
77940e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
78040e14135SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
78140e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
782cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
783cb1e1211SMatthew G Knepley }
784cb1e1211SMatthew G Knepley 
785a0845e3aSMatthew G. Knepley #undef __FUNCT__
78673d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
7870f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
78873d901b8SMatthew G. Knepley {
78973d901b8SMatthew G. Knepley   const PetscInt  debug = 0;
79073d901b8SMatthew G. Knepley   PetscSection    section;
79173d901b8SMatthew G. Knepley   PetscQuadrature quad;
79273d901b8SMatthew G. Knepley   Vec             localX;
79373d901b8SMatthew G. Knepley   PetscScalar    *funcVal;
79473d901b8SMatthew G. Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
79573d901b8SMatthew G. Knepley   PetscReal      *localDiff;
79673d901b8SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
79773d901b8SMatthew G. Knepley   PetscErrorCode  ierr;
79873d901b8SMatthew G. Knepley 
79973d901b8SMatthew G. Knepley   PetscFunctionBegin;
800c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
80173d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
80273d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
80373d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80473d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
80573d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
80673d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8070f2d7e86SMatthew G. Knepley     PetscFE  fe;
80873d901b8SMatthew G. Knepley     PetscInt Nc;
80973d901b8SMatthew G. Knepley 
8100f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
8110f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8120f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
81373d901b8SMatthew G. Knepley     numComponents += Nc;
81473d901b8SMatthew G. Knepley   }
8150f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
81673d901b8SMatthew G. Knepley   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
81773d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
81873d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
81973d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
82073d901b8SMatthew G. Knepley 
8218e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
82273d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
82373d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
82473d901b8SMatthew G. Knepley 
82573d901b8SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
8260f2d7e86SMatthew G. Knepley       PetscFE          fe;
82773d901b8SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
82873d901b8SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
82973d901b8SMatthew G. Knepley       PetscReal       *basis, elemDiff = 0.0;
83073d901b8SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
83173d901b8SMatthew G. Knepley 
8320f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
83373d901b8SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8340f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
8350f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
8360f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
83773d901b8SMatthew G. Knepley       if (debug) {
83873d901b8SMatthew G. Knepley         char title[1024];
83973d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
84073d901b8SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
84173d901b8SMatthew G. Knepley       }
84273d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
84373d901b8SMatthew G. Knepley         for (d = 0; d < dim; d++) {
84473d901b8SMatthew G. Knepley           coords[d] = v0[d];
84573d901b8SMatthew G. Knepley           for (e = 0; e < dim; e++) {
84673d901b8SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
84773d901b8SMatthew G. Knepley           }
84873d901b8SMatthew G. Knepley         }
84973d901b8SMatthew G. Knepley         (*funcs[field])(coords, funcVal, ctx);
85073d901b8SMatthew G. Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
85173d901b8SMatthew G. Knepley           PetscScalar interpolant = 0.0;
85273d901b8SMatthew G. Knepley 
85373d901b8SMatthew G. Knepley           for (f = 0; f < numBasisFuncs; ++f) {
85473d901b8SMatthew G. Knepley             const PetscInt fidx = f*numBasisComps+fc;
85573d901b8SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
85673d901b8SMatthew G. Knepley           }
85773d901b8SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
85873d901b8SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
85973d901b8SMatthew G. Knepley         }
86073d901b8SMatthew G. Knepley       }
86173d901b8SMatthew G. Knepley       comp        += numBasisComps;
86273d901b8SMatthew G. Knepley       fieldOffset += numBasisFuncs*numBasisComps;
86373d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
86473d901b8SMatthew G. Knepley     }
86573d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
86673d901b8SMatthew G. Knepley   }
86773d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
86873d901b8SMatthew G. Knepley   ierr  = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
86973d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
87073d901b8SMatthew G. Knepley   ierr  = PetscFree6(localDiff,funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
87173d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
87273d901b8SMatthew G. Knepley }
87373d901b8SMatthew G. Knepley 
87473d901b8SMatthew G. Knepley #undef __FUNCT__
87573d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
87673d901b8SMatthew G. Knepley /*@
87773d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
87873d901b8SMatthew G. Knepley 
87973d901b8SMatthew G. Knepley   Input Parameters:
88073d901b8SMatthew G. Knepley + dm - The mesh
88173d901b8SMatthew G. Knepley . X  - Local input vector
88273d901b8SMatthew G. Knepley - user - The user context
88373d901b8SMatthew G. Knepley 
88473d901b8SMatthew G. Knepley   Output Parameter:
88573d901b8SMatthew G. Knepley . integral - Local integral for each field
88673d901b8SMatthew G. Knepley 
88773d901b8SMatthew G. Knepley   Level: developer
88873d901b8SMatthew G. Knepley 
88973d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
89073d901b8SMatthew G. Knepley @*/
8910f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
89273d901b8SMatthew G. Knepley {
89373d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
89473d901b8SMatthew G. Knepley   DM                dmAux;
89573d901b8SMatthew G. Knepley   Vec               localX, A;
8962764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
89773d901b8SMatthew G. Knepley   PetscQuadrature   q;
89873d901b8SMatthew G. Knepley   PetscCellGeometry geom;
89973d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
90073d901b8SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
90173d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
9020f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
9030f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
90473d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
90573d901b8SMatthew G. Knepley 
90673d901b8SMatthew G. Knepley   PetscFunctionBegin;
90773d901b8SMatthew G. Knepley   /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
90873d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
90973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
91073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
911c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
91273d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
9132764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
9142764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
91573d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
91673d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
91773d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
9180f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {integral[f]    = 0.0;}
91973d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
92073d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
92173d901b8SMatthew G. Knepley   if (dmAux) {
92273d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
9232764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
9242764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
92573d901b8SMatthew G. Knepley   }
92673d901b8SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localX);CHKERRQ(ierr);
9270f2d7e86SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ);CHKERRQ(ierr);
9280f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
92973d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
93073d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
93173d901b8SMatthew G. Knepley     PetscInt     i;
93273d901b8SMatthew G. Knepley 
9338e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
93473d901b8SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
93573d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
9360f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
93773d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
93873d901b8SMatthew G. Knepley     if (dmAux) {
93973d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
9400f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
94173d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
94273d901b8SMatthew G. Knepley     }
94373d901b8SMatthew G. Knepley   }
94473d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
9450f2d7e86SMatthew G. Knepley     PetscFE  fe;
94673d901b8SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
94773d901b8SMatthew G. Knepley     /* Conforming batches */
94873d901b8SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
94973d901b8SMatthew G. Knepley     /* Remainder */
95073d901b8SMatthew G. Knepley     PetscInt Nr, offset;
95173d901b8SMatthew G. Knepley 
9522764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
9530f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
9540f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
9550f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
95673d901b8SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
95773d901b8SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
95873d901b8SMatthew G. Knepley     batchSize = numBlocks * blockSize;
9590f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
96073d901b8SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
96173d901b8SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
96273d901b8SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
96373d901b8SMatthew G. Knepley     offset    = numCells - Nr;
96473d901b8SMatthew G. Knepley     geom.v0   = v0;
96573d901b8SMatthew G. Knepley     geom.J    = J;
96673d901b8SMatthew G. Knepley     geom.invJ = invJ;
96773d901b8SMatthew G. Knepley     geom.detJ = detJ;
9680f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Ne, geom, u, probAux, a, integral);CHKERRQ(ierr);
96973d901b8SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
97073d901b8SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
97173d901b8SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
97273d901b8SMatthew G. Knepley     geom.detJ = &detJ[offset];
9730f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Nr, geom, &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr);
97473d901b8SMatthew G. Knepley   }
97573d901b8SMatthew G. Knepley   ierr = PetscFree5(u,v0,J,invJ,detJ);CHKERRQ(ierr);
97673d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
97773d901b8SMatthew G. Knepley   if (mesh->printFEM) {
97873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
97973d901b8SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);}
98073d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
98173d901b8SMatthew G. Knepley   }
98273d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
98373d901b8SMatthew G. Knepley   /* TODO: Allreduce for integral */
98473d901b8SMatthew G. Knepley   /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
98573d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
98673d901b8SMatthew G. Knepley }
98773d901b8SMatthew G. Knepley 
98873d901b8SMatthew G. Knepley #undef __FUNCT__
9890f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal"
9900f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
9910f2d7e86SMatthew G. Knepley {
9920f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
9930f2d7e86SMatthew G. Knepley   const char       *name  = "Residual";
9940f2d7e86SMatthew G. Knepley   DM                dmAux;
9950f2d7e86SMatthew G. Knepley   DMLabel           depth;
9960f2d7e86SMatthew G. Knepley   Vec               A;
9972764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
9980f2d7e86SMatthew G. Knepley   PetscQuadrature   q;
9990f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
10000f2d7e86SMatthew G. Knepley   PetscSection      section, sectionAux;
10010f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
10020f2d7e86SMatthew G. Knepley   PetscScalar      *elemVec, *u, *u_t, *a = NULL;
10030f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd;
10040f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux;
10050f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
10060f2d7e86SMatthew G. Knepley 
10070f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
10080f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1009c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10100f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
10112764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
10122764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
10132764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
10140f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
10150f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10160f2d7e86SMatthew G. Knepley   numCells = cEnd - cStart;
10170f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
10180f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
10190f2d7e86SMatthew G. Knepley   if (dmAux) {
10200f2d7e86SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
10212764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
10222764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
10230f2d7e86SMatthew G. Knepley   }
10240f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
10250f2d7e86SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
10260f2d7e86SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
10270f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
10280f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10290f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
10300f2d7e86SMatthew G. Knepley     PetscInt     i;
10310f2d7e86SMatthew G. Knepley 
10328e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
10330f2d7e86SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
10340f2d7e86SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10350f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
10360f2d7e86SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10370f2d7e86SMatthew G. Knepley     if (X_t) {
10380f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10390f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
10400f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10410f2d7e86SMatthew G. Knepley     }
10420f2d7e86SMatthew G. Knepley     if (dmAux) {
10430f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
10440f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
10450f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
10460f2d7e86SMatthew G. Knepley     }
10470f2d7e86SMatthew G. Knepley   }
10480f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
10490f2d7e86SMatthew G. Knepley     PetscFE  fe;
10500f2d7e86SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
10510f2d7e86SMatthew G. Knepley     /* Conforming batches */
10520f2d7e86SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
10530f2d7e86SMatthew G. Knepley     /* Remainder */
10540f2d7e86SMatthew G. Knepley     PetscInt Nr, offset;
10550f2d7e86SMatthew G. Knepley 
10562764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
10570f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
10580f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
10590f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
10600f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
10610f2d7e86SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
10620f2d7e86SMatthew G. Knepley     batchSize = numBlocks * blockSize;
10630f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
10640f2d7e86SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
10650f2d7e86SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
10660f2d7e86SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
10670f2d7e86SMatthew G. Knepley     offset    = numCells - Nr;
10680f2d7e86SMatthew G. Knepley     geom.v0   = v0;
10690f2d7e86SMatthew G. Knepley     geom.J    = J;
10700f2d7e86SMatthew G. Knepley     geom.invJ = invJ;
10710f2d7e86SMatthew G. Knepley     geom.detJ = detJ;
10720f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
10730f2d7e86SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
10740f2d7e86SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
10750f2d7e86SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
10760f2d7e86SMatthew G. Knepley     geom.detJ = &detJ[offset];
10770f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
10780f2d7e86SMatthew G. Knepley   }
10790f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10800f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);}
10810f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
10820f2d7e86SMatthew G. Knepley   }
10830f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
10840f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
10850f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
10860f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
10870f2d7e86SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
10880f2d7e86SMatthew G. Knepley     const char     *bdLabel;
10890f2d7e86SMatthew G. Knepley     DMLabel         label;
10900f2d7e86SMatthew G. Knepley     IS              pointIS;
10910f2d7e86SMatthew G. Knepley     const PetscInt *points;
10920f2d7e86SMatthew G. Knepley     const PetscInt *values;
10930f2d7e86SMatthew G. Knepley     PetscReal      *n;
10940f2d7e86SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
10950f2d7e86SMatthew G. Knepley     PetscBool       isEssential;
10960f2d7e86SMatthew G. Knepley 
10970f2d7e86SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
10980f2d7e86SMatthew G. Knepley     if (isEssential) continue;
10990f2d7e86SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
11000f2d7e86SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
11010f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
11020f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
11030f2d7e86SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
11040f2d7e86SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
11050f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
11060f2d7e86SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
11070f2d7e86SMatthew G. Knepley     }
11080f2d7e86SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
11090f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
11100f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
11110f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
11120f2d7e86SMatthew G. Knepley       PetscScalar   *x     = NULL;
11130f2d7e86SMatthew G. Knepley       PetscInt       i;
11140f2d7e86SMatthew G. Knepley 
11150f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
11160f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
11178e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
11180f2d7e86SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
11190f2d7e86SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
11200f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11210f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
11220f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11230f2d7e86SMatthew G. Knepley       if (X_t) {
11240f2d7e86SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11250f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
11260f2d7e86SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11270f2d7e86SMatthew G. Knepley       }
11280f2d7e86SMatthew G. Knepley       ++f;
11290f2d7e86SMatthew G. Knepley     }
11300f2d7e86SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
11310f2d7e86SMatthew G. Knepley       PetscFE  fe;
11320f2d7e86SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
11330f2d7e86SMatthew G. Knepley       /* Conforming batches */
11340f2d7e86SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
11350f2d7e86SMatthew G. Knepley       /* Remainder */
11360f2d7e86SMatthew G. Knepley       PetscInt Nr, offset;
11370f2d7e86SMatthew G. Knepley 
11382764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
11390f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
11400f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11410f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
11420f2d7e86SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
11430f2d7e86SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
11440f2d7e86SMatthew G. Knepley       batchSize = numBlocks * blockSize;
11450f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
11460f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
11470f2d7e86SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
11480f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
11490f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
11500f2d7e86SMatthew G. Knepley       geom.v0   = v0;
11510f2d7e86SMatthew G. Knepley       geom.n    = n;
11520f2d7e86SMatthew G. Knepley       geom.J    = J;
11530f2d7e86SMatthew G. Knepley       geom.invJ = invJ;
11540f2d7e86SMatthew G. Knepley       geom.detJ = detJ;
11550f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
11560f2d7e86SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
11570f2d7e86SMatthew G. Knepley       geom.n    = &n[offset*dim];
11580f2d7e86SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
11590f2d7e86SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
11600f2d7e86SMatthew G. Knepley       geom.detJ = &detJ[offset];
11610f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr);
11620f2d7e86SMatthew G. Knepley     }
11630f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
11640f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
11650f2d7e86SMatthew G. Knepley 
11660f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
11670f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
11680f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
11690f2d7e86SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
11700f2d7e86SMatthew G. Knepley       ++f;
11710f2d7e86SMatthew G. Knepley     }
11720f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
11730f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
11740f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
11750f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
11760f2d7e86SMatthew G. Knepley   }
11770f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
11780f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
11790f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
11800f2d7e86SMatthew G. Knepley }
11810f2d7e86SMatthew G. Knepley 
11820f2d7e86SMatthew G. Knepley #undef __FUNCT__
118308f11eefSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check"
118408f11eefSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check(DM dm, Vec X, Vec X_t, Vec F, void *user)
118508f11eefSMatthew G. Knepley {
118608f11eefSMatthew G. Knepley   DM                dmCh, dmAux;
118708f11eefSMatthew G. Knepley   Vec               A;
11886f4eac71SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
118908f11eefSMatthew G. Knepley   PetscQuadrature   q;
119008f11eefSMatthew G. Knepley   PetscCellGeometry geom;
119108f11eefSMatthew G. Knepley   PetscSection      section, sectionAux;
119208f11eefSMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
119308f11eefSMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
119408f11eefSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
119508f11eefSMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
119608f11eefSMatthew G. Knepley   PetscErrorCode    ierr;
119708f11eefSMatthew G. Knepley 
119808f11eefSMatthew G. Knepley   PetscFunctionBegin;
1199c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
120008f11eefSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
12016f4eac71SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
12026f4eac71SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
120308f11eefSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
120408f11eefSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
120508f11eefSMatthew G. Knepley   numCells = cEnd - cStart;
120608f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
12076f4eac71SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
120808f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
120908f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
121008f11eefSMatthew G. Knepley   if (dmAux) {
121108f11eefSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
12126f4eac71SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
12136f4eac71SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
121408f11eefSMatthew G. Knepley   }
121508f11eefSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
121608f11eefSMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
121708f11eefSMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
121808f11eefSMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
121908f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
122008f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
122108f11eefSMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
122208f11eefSMatthew G. Knepley     PetscInt     i;
122308f11eefSMatthew G. Knepley 
12248e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
122508f11eefSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
122608f11eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
122708f11eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
122808f11eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
122908f11eefSMatthew G. Knepley     if (X_t) {
123008f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
123108f11eefSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
123208f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
123308f11eefSMatthew G. Knepley     }
123408f11eefSMatthew G. Knepley     if (dmAux) {
123508f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
123608f11eefSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
123708f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
123808f11eefSMatthew G. Knepley     }
123908f11eefSMatthew G. Knepley   }
124008f11eefSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
124108f11eefSMatthew G. Knepley     PetscFE  fe, feCh;
124208f11eefSMatthew G. Knepley     PetscInt numQuadPoints, Nb;
124308f11eefSMatthew G. Knepley     /* Conforming batches */
124408f11eefSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
124508f11eefSMatthew G. Knepley     /* Remainder */
124608f11eefSMatthew G. Knepley     PetscInt Nr, offset;
124708f11eefSMatthew G. Knepley 
12486f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
12496f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
125008f11eefSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
125108f11eefSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
125208f11eefSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
125308f11eefSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
125408f11eefSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
125508f11eefSMatthew G. Knepley     batchSize = numBlocks * blockSize;
125608f11eefSMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
125708f11eefSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
125808f11eefSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
125908f11eefSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
126008f11eefSMatthew G. Knepley     offset    = numCells - Nr;
126108f11eefSMatthew G. Knepley     geom.v0   = v0;
126208f11eefSMatthew G. Knepley     geom.J    = J;
126308f11eefSMatthew G. Knepley     geom.invJ = invJ;
126408f11eefSMatthew G. Knepley     geom.detJ = detJ;
126508f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
126608f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, geom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
126708f11eefSMatthew G. Knepley     geom.v0   = &v0[offset*dim];
126808f11eefSMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
126908f11eefSMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
127008f11eefSMatthew G. Knepley     geom.detJ = &detJ[offset];
127108f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
127208f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr);
127308f11eefSMatthew G. Knepley   }
127408f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
127508f11eefSMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
127608f11eefSMatthew G. Knepley     PetscInt  d;
127708f11eefSMatthew G. Knepley 
127808f11eefSMatthew G. Knepley     for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;}
127908f11eefSMatthew G. Knepley     if (diff) {
128008f11eefSMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
128108f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
128208f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
128308f11eefSMatthew G. Knepley       ++diffCell;
128408f11eefSMatthew G. Knepley     }
128508f11eefSMatthew G. Knepley     if (diffCell > 9) break;
128608f11eefSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
128708f11eefSMatthew G. Knepley   }
128808f11eefSMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
128908f11eefSMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
129008f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
129108f11eefSMatthew G. Knepley   PetscFunctionReturn(0);
129208f11eefSMatthew G. Knepley }
129308f11eefSMatthew G. Knepley 
129408f11eefSMatthew G. Knepley #undef __FUNCT__
12950f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
1296a0845e3aSMatthew G. Knepley /*@
12970f2d7e86SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1298a0845e3aSMatthew G. Knepley 
1299a0845e3aSMatthew G. Knepley   Input Parameters:
1300a0845e3aSMatthew G. Knepley + dm - The mesh
13010f2d7e86SMatthew G. Knepley . X  - Local solution
1302a0845e3aSMatthew G. Knepley - user - The user context
1303a0845e3aSMatthew G. Knepley 
1304a0845e3aSMatthew G. Knepley   Output Parameter:
1305a0845e3aSMatthew G. Knepley . F  - Local output vector
1306a0845e3aSMatthew G. Knepley 
1307a0845e3aSMatthew G. Knepley   Level: developer
1308a0845e3aSMatthew G. Knepley 
1309a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1310a0845e3aSMatthew G. Knepley @*/
13110f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1312a0845e3aSMatthew G. Knepley {
131308f11eefSMatthew G. Knepley   PetscObject    check;
1314a0845e3aSMatthew G. Knepley   PetscErrorCode ierr;
1315a0845e3aSMatthew G. Knepley 
1316a0845e3aSMatthew G. Knepley   PetscFunctionBegin;
131708f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
131808f11eefSMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check(dm, X, NULL, F, user);CHKERRQ(ierr);}
131908f11eefSMatthew G. Knepley   else       {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
13200f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
13210f2d7e86SMatthew G. Knepley }
13220f2d7e86SMatthew G. Knepley 
13230f2d7e86SMatthew G. Knepley #undef __FUNCT__
1324adbe6fbbSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
1325adbe6fbbSMatthew G. Knepley /*@
1326adbe6fbbSMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1327adbe6fbbSMatthew G. Knepley 
1328adbe6fbbSMatthew G. Knepley   Input Parameters:
1329adbe6fbbSMatthew G. Knepley + dm - The mesh
1330adbe6fbbSMatthew G. Knepley . t - The time
1331adbe6fbbSMatthew G. Knepley . X  - Local solution
1332adbe6fbbSMatthew G. Knepley . X_t - Local solution time derivative, or NULL
1333adbe6fbbSMatthew G. Knepley - user - The user context
1334adbe6fbbSMatthew G. Knepley 
1335adbe6fbbSMatthew G. Knepley   Output Parameter:
1336adbe6fbbSMatthew G. Knepley . F  - Local output vector
1337adbe6fbbSMatthew G. Knepley 
1338adbe6fbbSMatthew G. Knepley   Level: developer
1339adbe6fbbSMatthew G. Knepley 
1340adbe6fbbSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1341adbe6fbbSMatthew G. Knepley @*/
1342adbe6fbbSMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec X, Vec X_t, Vec F, void *user)
1343adbe6fbbSMatthew G. Knepley {
1344adbe6fbbSMatthew G. Knepley   PetscErrorCode ierr;
1345adbe6fbbSMatthew G. Knepley 
1346adbe6fbbSMatthew G. Knepley   PetscFunctionBegin;
1347adbe6fbbSMatthew G. Knepley   ierr = DMPlexComputeResidualFEM_Internal(dm, X, X_t, F, user);CHKERRQ(ierr);
1348adbe6fbbSMatthew G. Knepley   PetscFunctionReturn(0);
1349adbe6fbbSMatthew G. Knepley }
1350adbe6fbbSMatthew G. Knepley 
1351adbe6fbbSMatthew G. Knepley #undef __FUNCT__
13520f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
13530f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
13540f2d7e86SMatthew G. Knepley {
13550f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
13560f2d7e86SMatthew G. Knepley   const char       *name  = "Jacobian";
13570f2d7e86SMatthew G. Knepley   DM                dmAux;
13580f2d7e86SMatthew G. Knepley   DMLabel           depth;
13590f2d7e86SMatthew G. Knepley   Vec               A;
13602764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
13610f2d7e86SMatthew G. Knepley   PetscQuadrature   quad;
13620f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
13630f2d7e86SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
13640f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
13650f2d7e86SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
13660f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
13670f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
13680f2d7e86SMatthew G. Knepley   PetscBool         isShell;
13690f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
13700f2d7e86SMatthew G. Knepley 
13710f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
13720f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1373c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1374a0845e3aSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
13750f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
13762764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
13772764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
13782764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
13799a559087SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1380a0845e3aSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1381a0845e3aSMatthew G. Knepley   numCells = cEnd - cStart;
13829a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
13839a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
13849a559087SMatthew G. Knepley   if (dmAux) {
13859a559087SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
13862764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
13872764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
13889a559087SMatthew G. Knepley   }
13893351dd3dSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
13900f2d7e86SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
13910f2d7e86SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
13920f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1393a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
13940f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
1395a0845e3aSMatthew G. Knepley     PetscInt     i;
1396a0845e3aSMatthew G. Knepley 
13978e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1398a0845e3aSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1399a0845e3aSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
14000f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1401a0845e3aSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
14020f2d7e86SMatthew G. Knepley     if (X_t) {
14030f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
14040f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
14050f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
14060f2d7e86SMatthew G. Knepley     }
14079a559087SMatthew G. Knepley     if (dmAux) {
14089a559087SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
14090f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
14109a559087SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1411a0845e3aSMatthew G. Knepley     }
14129a559087SMatthew G. Knepley   }
14130f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
14140f2d7e86SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
14150f2d7e86SMatthew G. Knepley     PetscFE  fe;
141621454ff5SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1417a0845e3aSMatthew G. Knepley     /* Conforming batches */
1418f30c5766SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1419a0845e3aSMatthew G. Knepley     /* Remainder */
1420a0845e3aSMatthew G. Knepley     PetscInt Nr, offset;
1421a0845e3aSMatthew G. Knepley 
14222764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
14230f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
14240f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
14250f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
14260f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
142721454ff5SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1428a0845e3aSMatthew G. Knepley     batchSize = numBlocks * blockSize;
14290f2d7e86SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1430a0845e3aSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1431a0845e3aSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1432a0845e3aSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1433a0845e3aSMatthew G. Knepley     offset    = numCells - Nr;
14340f2d7e86SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1435a0845e3aSMatthew G. Knepley       geom.v0   = v0;
1436a0845e3aSMatthew G. Knepley       geom.J    = J;
1437a0845e3aSMatthew G. Knepley       geom.invJ = invJ;
1438a0845e3aSMatthew G. Knepley       geom.detJ = detJ;
14390f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1440a0845e3aSMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1441a0845e3aSMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1442a0845e3aSMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1443a0845e3aSMatthew G. Knepley       geom.detJ = &detJ[offset];
14440f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
14450f2d7e86SMatthew G. Knepley     }
1446a0845e3aSMatthew G. Knepley   }
1447a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
14480f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
14490f2d7e86SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
1450a0845e3aSMatthew G. Knepley   }
14510f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
14529a559087SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
14530f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
14540f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
14551c093863SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
14561c093863SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
14571c093863SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
14581c093863SMatthew G. Knepley     const char     *bdLabel;
14591c093863SMatthew G. Knepley     DMLabel         label;
14601c093863SMatthew G. Knepley     IS              pointIS;
14611c093863SMatthew G. Knepley     const PetscInt *points;
14621c093863SMatthew G. Knepley     const PetscInt *values;
14631c093863SMatthew G. Knepley     PetscReal      *n;
14641c093863SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
14651c093863SMatthew G. Knepley     PetscBool       isEssential;
14661c093863SMatthew G. Knepley 
146763d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
14680f2d7e86SMatthew G. Knepley     if (isEssential) continue;
14691c093863SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
14701c093863SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
14711c093863SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
14721c093863SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
14731c093863SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1474075da914SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1475075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1476075da914SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1477075da914SMatthew G. Knepley     }
14780f2d7e86SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
14790f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1480075da914SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1481f1ea0e2fSMatthew G. Knepley       const PetscInt point = points[p];
1482f1ea0e2fSMatthew G. Knepley       PetscScalar   *x     = NULL;
1483f1ea0e2fSMatthew G. Knepley       PetscInt       i;
1484f1ea0e2fSMatthew G. Knepley 
1485075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1486075da914SMatthew G. Knepley       if (dep != dim-1) continue;
14878e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1488a8007bbfSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1489075da914SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1490f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
14910f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1492f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
14930f2d7e86SMatthew G. Knepley       if (X_t) {
1494af1eca97SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
14950f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1496af1eca97SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
14970f2d7e86SMatthew G. Knepley       }
1498af1eca97SMatthew G. Knepley       ++f;
1499af1eca97SMatthew G. Knepley     }
15000f2d7e86SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
15010f2d7e86SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
15020f2d7e86SMatthew G. Knepley       PetscFE  fe;
1503af1eca97SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1504af1eca97SMatthew G. Knepley       /* Conforming batches */
1505af1eca97SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1506af1eca97SMatthew G. Knepley       /* Remainder */
1507af1eca97SMatthew G. Knepley       PetscInt Nr, offset;
1508af1eca97SMatthew G. Knepley 
15092764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
15100f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
15110f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
15120f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
151321454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
151421454ff5SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
15150483ade4SMatthew G. Knepley       batchSize = numBlocks * blockSize;
15160f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
15170f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
15180483ade4SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
15190f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
15200f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
15210f2d7e86SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
15220483ade4SMatthew G. Knepley         geom.v0   = v0;
15230f2d7e86SMatthew G. Knepley         geom.n    = n;
15240483ade4SMatthew G. Knepley         geom.J    = J;
15250483ade4SMatthew G. Knepley         geom.invJ = invJ;
15260483ade4SMatthew G. Knepley         geom.detJ = detJ;
15270f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
15280483ade4SMatthew G. Knepley         geom.v0   = &v0[offset*dim];
15290f2d7e86SMatthew G. Knepley         geom.n    = &n[offset*dim];
15300483ade4SMatthew G. Knepley         geom.J    = &J[offset*dim*dim];
15310483ade4SMatthew G. Knepley         geom.invJ = &invJ[offset*dim*dim];
15320483ade4SMatthew G. Knepley         geom.detJ = &detJ[offset];
15330f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr);
1534cb1e1211SMatthew G Knepley       }
1535cb1e1211SMatthew G Knepley     }
15360f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
15370f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1538cb1e1211SMatthew G Knepley 
15390f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
15400f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
15410f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
15420f2d7e86SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
15430f2d7e86SMatthew G. Knepley       ++f;
1544cb1e1211SMatthew G Knepley     }
15450f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
15460f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
15470f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr);
15480f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1549cb1e1211SMatthew G Knepley   }
15500f2d7e86SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15510f2d7e86SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15520f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {
15530f2d7e86SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
15540f2d7e86SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
15550f2d7e86SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
15560f2d7e86SMatthew G. Knepley   }
15570f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
15580f2d7e86SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
15590f2d7e86SMatthew G. Knepley   if (isShell) {
15600f2d7e86SMatthew G. Knepley     JacActionCtx *jctx;
15610f2d7e86SMatthew G. Knepley 
15620f2d7e86SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
15630f2d7e86SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
15640f2d7e86SMatthew G. Knepley   }
1565cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1566cb1e1211SMatthew G Knepley }
1567cb1e1211SMatthew G Knepley 
1568cb1e1211SMatthew G Knepley #undef __FUNCT__
15690f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
1570cb1e1211SMatthew G Knepley /*@
15710f2d7e86SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
1572cb1e1211SMatthew G Knepley 
1573cb1e1211SMatthew G Knepley   Input Parameters:
1574cb1e1211SMatthew G Knepley + dm - The mesh
1575cb1e1211SMatthew G Knepley . X  - Local input vector
1576cb1e1211SMatthew G Knepley - user - The user context
1577cb1e1211SMatthew G Knepley 
1578cb1e1211SMatthew G Knepley   Output Parameter:
1579cb1e1211SMatthew G Knepley . Jac  - Jacobian matrix
1580cb1e1211SMatthew G Knepley 
1581cb1e1211SMatthew G Knepley   Note:
15828026896cSMatthew G. Knepley   The first member of the user context must be an FEMContext.
1583cb1e1211SMatthew G Knepley 
1584cb1e1211SMatthew G Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1585cb1e1211SMatthew G Knepley   like a GPU, or vectorize on a multicore machine.
1586cb1e1211SMatthew G Knepley 
15870059ad2aSSatish Balay   Level: developer
15880059ad2aSSatish Balay 
1589cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal()
1590878cb397SSatish Balay @*/
15910f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
1592cb1e1211SMatthew G Knepley {
1593cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1594cb1e1211SMatthew G Knepley 
1595cb1e1211SMatthew G Knepley   PetscFunctionBegin;
15960f2d7e86SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
1597cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1598cb1e1211SMatthew G Knepley }
1599bceba477SMatthew G. Knepley 
1600d69c5d34SMatthew G. Knepley #undef __FUNCT__
1601d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1602d69c5d34SMatthew G. Knepley /*@
1603d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1604d69c5d34SMatthew G. Knepley 
1605d69c5d34SMatthew G. Knepley   Input Parameters:
1606d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1607d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1608d69c5d34SMatthew G. Knepley - user - The user context
1609d69c5d34SMatthew G. Knepley 
1610d69c5d34SMatthew G. Knepley   Output Parameter:
1611934789fcSMatthew G. Knepley . In  - The interpolation matrix
1612d69c5d34SMatthew G. Knepley 
1613d69c5d34SMatthew G. Knepley   Note:
1614d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1615d69c5d34SMatthew G. Knepley 
1616d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1617d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1618d69c5d34SMatthew G. Knepley 
1619d69c5d34SMatthew G. Knepley   Level: developer
1620d69c5d34SMatthew G. Knepley 
1621d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1622d69c5d34SMatthew G. Knepley @*/
1623934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1624d69c5d34SMatthew G. Knepley {
1625d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1626d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
16272764a2aaSMatthew G. Knepley   PetscDS           prob;
1628d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
1629d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1630d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1631d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
1632942a7a06SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
16330f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1634d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1635d69c5d34SMatthew G. Knepley 
1636d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1637d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1638c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1639d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1640d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1641d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1642d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1643d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1644d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
16452764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
1646d69c5d34SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
1647d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
16480f2d7e86SMatthew G. Knepley     PetscFE  fe;
16490f2d7e86SMatthew G. Knepley     PetscInt rNb, Nc;
1650d69c5d34SMatthew G. Knepley 
16512764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
16520f2d7e86SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1653d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
16540f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
16550f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1656d69c5d34SMatthew G. Knepley   }
16572764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
16580f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
16590f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1660d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1661d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1662d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1663d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1664d69c5d34SMatthew G. Knepley     PetscReal       *points;
1665d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1666d69c5d34SMatthew G. Knepley 
1667d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
1668d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
16690f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
1670d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1671d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1672d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1673d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1674d69c5d34SMatthew G. Knepley       npoints += Np;
1675d69c5d34SMatthew G. Knepley     }
1676d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1677d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1678d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1679d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1680d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1681d69c5d34SMatthew G. Knepley     }
1682d69c5d34SMatthew G. Knepley 
1683d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
16840f2d7e86SMatthew G. Knepley       PetscFE    fe;
1685d69c5d34SMatthew G. Knepley       PetscReal *B;
168636a6d9c0SMatthew G. Knepley       PetscInt   NcJ, cpdim, j;
1687d69c5d34SMatthew G. Knepley 
1688d69c5d34SMatthew G. Knepley       /* Evaluate basis at points */
16892764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr);
16900f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
16910f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1692ffe73a53SMatthew G. Knepley       /* For now, fields only interpolate themselves */
1693ffe73a53SMatthew G. Knepley       if (fieldI == fieldJ) {
16947c927364SMatthew 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);
16950f2d7e86SMatthew G. Knepley         ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1696d69c5d34SMatthew G. Knepley         for (i = 0, k = 0; i < fpdim; ++i) {
1697d69c5d34SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1698d69c5d34SMatthew G. Knepley           ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1699d69c5d34SMatthew G. Knepley           for (p = 0; p < Np; ++p, ++k) {
170036a6d9c0SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
17010f2d7e86SMatthew 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];
170236a6d9c0SMatthew G. Knepley             }
1703d69c5d34SMatthew G. Knepley           }
1704d69c5d34SMatthew G. Knepley         }
17050f2d7e86SMatthew G. Knepley         ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1706ffe73a53SMatthew G. Knepley       }
170736a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1708d69c5d34SMatthew G. Knepley     }
1709d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1710549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1711d69c5d34SMatthew G. Knepley   }
17120f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
17137f5b169aSMatthew G. Knepley   /* Preallocate matrix */
17147f5b169aSMatthew G. Knepley   {
17157f5b169aSMatthew G. Knepley     PetscHashJK ht;
17167f5b169aSMatthew G. Knepley     PetscLayout rLayout;
17177f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
17187f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
17197f5b169aSMatthew G. Knepley 
17207f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
17217f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
17227f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
17237f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
17247f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
17257f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
17267f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
17277f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
17287f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
17297f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
17307f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
17317f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
17327f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
17337f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
17347f5b169aSMatthew G. Knepley 
17357f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
17367f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
17377f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
17387f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
17397f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
17407f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
17417f5b169aSMatthew G. Knepley           if (missing) {
17427f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
17437f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
17447f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
17457f5b169aSMatthew G. Knepley           }
17467f5b169aSMatthew G. Knepley         }
17477f5b169aSMatthew G. Knepley       }
17487f5b169aSMatthew G. Knepley     }
17497f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
17507f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
17517f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
17527f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
17537f5b169aSMatthew G. Knepley   }
17547f5b169aSMatthew G. Knepley   /* Fill matrix */
17557f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1756d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1757934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1758d69c5d34SMatthew G. Knepley   }
1759549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
1760d69c5d34SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
1761549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1762934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1763934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1764d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1765d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1766934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1767934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1768d69c5d34SMatthew G. Knepley   }
1769d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1770d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1771d69c5d34SMatthew G. Knepley }
17726c73c22cSMatthew G. Knepley 
17736c73c22cSMatthew G. Knepley #undef __FUNCT__
17747c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
17757c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
17767c927364SMatthew G. Knepley {
1777e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
17787c927364SMatthew G. Knepley   PetscFE       *feRef;
17797c927364SMatthew G. Knepley   Vec            fv, cv;
17807c927364SMatthew G. Knepley   IS             fis, cis;
17817c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
17827c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
17837c927364SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, offsetC, offsetF, m;
17847c927364SMatthew G. Knepley   PetscErrorCode ierr;
17857c927364SMatthew G. Knepley 
17867c927364SMatthew G. Knepley   PetscFunctionBegin;
178775a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1788c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
17897c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
17907c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
17917c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
17927c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
17937c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
17947c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
1795e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
17967c927364SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
17977c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
17987c927364SMatthew G. Knepley     PetscFE  fe;
17997c927364SMatthew G. Knepley     PetscInt fNb, Nc;
18007c927364SMatthew G. Knepley 
1801e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
18027c927364SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
18037c927364SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
18047c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
18057c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
18067c927364SMatthew G. Knepley   }
1807e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
18087c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
18097c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
18107c927364SMatthew G. Knepley     PetscFE        feC;
18117c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
18127c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
18137c927364SMatthew G. Knepley 
1814e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
18157c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
18167c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
18177c927364SMatthew 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);
18187c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
18197c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
18207c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
18217c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
18227c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
18237c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
18247c927364SMatthew G. Knepley       const PetscReal *cqpoints;
18257c927364SMatthew G. Knepley       PetscInt         NpC;
18267c927364SMatthew G. Knepley 
18277c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
18287c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
18297c927364SMatthew G. Knepley       if (NpC != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
18307c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
18317c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
18327c927364SMatthew G. Knepley         const PetscReal *fqpoints;
18337c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
18347c927364SMatthew G. Knepley         PetscInt         NpF, comp;
18357c927364SMatthew G. Knepley 
18367c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
18377c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
18387c927364SMatthew G. Knepley         if (NpC != NpF) continue;
18397c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
18407c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
18417c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
18427c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
18437c927364SMatthew G. Knepley         }
18447c927364SMatthew G. Knepley         break;
18457c927364SMatthew G. Knepley       }
18467c927364SMatthew G. Knepley     }
18477c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
18487c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
18497c927364SMatthew G. Knepley   }
18507c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
18517c927364SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
18527c927364SMatthew G. Knepley 
18537c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
18547c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
18557c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
18567c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
18577c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1858aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1859aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
18607c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
18617c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
18627c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
18637c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
18647c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
18657c927364SMatthew 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]]);
18667c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
18677c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
18687c927364SMatthew G. Knepley     }
18697c927364SMatthew G. Knepley   }
18707c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
18717c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
18727c927364SMatthew G. Knepley 
18737c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
18747c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
18757c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
18767c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
18777c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
18787c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
18797c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
188075a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1881cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1882cb1e1211SMatthew G Knepley }
1883cb1e1211SMatthew G Knepley 
1884cb1e1211SMatthew G Knepley #undef __FUNCT__
18858e136ac0SMatthew G. Knepley #define __FUNCT__ "BoundaryDuplicate"
18868e136ac0SMatthew G. Knepley static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary)
18878e136ac0SMatthew G. Knepley {
18888e136ac0SMatthew G. Knepley   DMBoundary     b = bd, b2, bold = NULL;
18898e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
18908e136ac0SMatthew G. Knepley 
18918e136ac0SMatthew G. Knepley   PetscFunctionBegin;
18928e136ac0SMatthew G. Knepley   *boundary = NULL;
18938e136ac0SMatthew G. Knepley   for (; b; b = b->next, bold = b2) {
18948e136ac0SMatthew G. Knepley     ierr = PetscNew(&b2);CHKERRQ(ierr);
18958e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
18968e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
18978e136ac0SMatthew G. Knepley     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
18988e136ac0SMatthew G. Knepley     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
18998e136ac0SMatthew G. Knepley     b2->label     = NULL;
19008e136ac0SMatthew G. Knepley     b2->essential = b->essential;
19018e136ac0SMatthew G. Knepley     b2->field     = b->field;
19028e136ac0SMatthew G. Knepley     b2->func      = b->func;
19038e136ac0SMatthew G. Knepley     b2->numids    = b->numids;
19048e136ac0SMatthew G. Knepley     b2->ctx       = b->ctx;
19058e136ac0SMatthew G. Knepley     b2->next      = NULL;
19068e136ac0SMatthew G. Knepley     if (!*boundary) *boundary   = b2;
19078e136ac0SMatthew G. Knepley     if (bold)        bold->next = b2;
19088e136ac0SMatthew G. Knepley   }
19098e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
19108e136ac0SMatthew G. Knepley }
19118e136ac0SMatthew G. Knepley 
19128e136ac0SMatthew G. Knepley #undef __FUNCT__
19138e136ac0SMatthew G. Knepley #define __FUNCT__ "DMPlexCopyBoundary"
19148e136ac0SMatthew G. Knepley PetscErrorCode DMPlexCopyBoundary(DM dm, DM dmNew)
19158e136ac0SMatthew G. Knepley {
19168e136ac0SMatthew G. Knepley   DM_Plex       *mesh    = (DM_Plex *) dm->data;
19178e136ac0SMatthew G. Knepley   DM_Plex       *meshNew = (DM_Plex *) dmNew->data;
19188e136ac0SMatthew G. Knepley   DMBoundary     b;
19198e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
19208e136ac0SMatthew G. Knepley 
19218e136ac0SMatthew G. Knepley   PetscFunctionBegin;
19228e136ac0SMatthew G. Knepley   ierr = BoundaryDuplicate(mesh->boundary, &meshNew->boundary);CHKERRQ(ierr);
19238e136ac0SMatthew G. Knepley   for (b = meshNew->boundary; b; b = b->next) {
19248e136ac0SMatthew G. Knepley     if (b->labelname) {
19258e136ac0SMatthew G. Knepley       ierr = DMPlexGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
19268e136ac0SMatthew G. Knepley       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
19278e136ac0SMatthew G. Knepley     }
19288e136ac0SMatthew G. Knepley   }
19298e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
19308e136ac0SMatthew G. Knepley }
19318e136ac0SMatthew G. Knepley 
19328e136ac0SMatthew G. Knepley #undef __FUNCT__
1933cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexAddBoundary"
1934cb1e1211SMatthew G Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */
193563d5297fSMatthew G. Knepley PetscErrorCode DMPlexAddBoundary(DM dm, PetscBool isEssential, const char name[], const char labelname[], PetscInt field, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx)
1936cb1e1211SMatthew G Knepley {
1937cb1e1211SMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1938cb1e1211SMatthew G Knepley   DMBoundary     b;
1939cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1940cb1e1211SMatthew G Knepley 
1941cb1e1211SMatthew G Knepley   PetscFunctionBegin;
194263d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1943cb1e1211SMatthew G Knepley   ierr = PetscNew(&b);CHKERRQ(ierr);
1944cb1e1211SMatthew G Knepley   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
194563d5297fSMatthew G. Knepley   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
1946cb1e1211SMatthew G Knepley   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
1947cb1e1211SMatthew G Knepley   ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);
194863d5297fSMatthew G. Knepley   if (b->labelname) {
194963d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
195063d5297fSMatthew G. Knepley     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
195163d5297fSMatthew G. Knepley   }
1952cb1e1211SMatthew G Knepley   b->essential   = isEssential;
1953cb1e1211SMatthew G Knepley   b->field       = field;
1954cb1e1211SMatthew G Knepley   b->func        = bcFunc;
1955cb1e1211SMatthew G Knepley   b->numids      = numids;
1956cb1e1211SMatthew G Knepley   b->ctx         = ctx;
1957cb1e1211SMatthew G Knepley   b->next        = mesh->boundary;
1958cb1e1211SMatthew G Knepley   mesh->boundary = b;
1959cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1960cb1e1211SMatthew G Knepley }
1961cb1e1211SMatthew G Knepley 
1962cb1e1211SMatthew G Knepley #undef __FUNCT__
1963cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetNumBoundary"
1964cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd)
1965cb1e1211SMatthew G Knepley {
1966cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
1967cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
1968cb1e1211SMatthew G Knepley 
1969cb1e1211SMatthew G Knepley   PetscFunctionBegin;
197063d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
197163d5297fSMatthew G. Knepley   PetscValidPointer(numBd, 2);
1972cb1e1211SMatthew G Knepley   *numBd = 0;
1973cb1e1211SMatthew G Knepley   while (b) {++(*numBd); b = b->next;}
1974cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1975cb1e1211SMatthew G Knepley }
1976cb1e1211SMatthew G Knepley 
1977cb1e1211SMatthew G Knepley #undef __FUNCT__
1978cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetBoundary"
197963d5297fSMatthew G. Knepley PetscErrorCode DMPlexGetBoundary(DM dm, PetscInt bd, PetscBool *isEssential, const char **name, const char **labelname, PetscInt *field, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx)
1980cb1e1211SMatthew G Knepley {
1981cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
1982cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
1983cb1e1211SMatthew G Knepley   PetscInt   n    = 0;
1984cb1e1211SMatthew G Knepley 
1985cb1e1211SMatthew G Knepley   PetscFunctionBegin;
198663d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1987cb1e1211SMatthew G Knepley   while (b) {
1988cb1e1211SMatthew G Knepley     if (n == bd) break;
1989cb1e1211SMatthew G Knepley     b = b->next;
1990cb1e1211SMatthew G Knepley     ++n;
1991cb1e1211SMatthew G Knepley   }
1992cb1e1211SMatthew G Knepley   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
1993cb1e1211SMatthew G Knepley   if (isEssential) {
1994cb1e1211SMatthew G Knepley     PetscValidPointer(isEssential, 3);
1995cb1e1211SMatthew G Knepley     *isEssential = b->essential;
1996cb1e1211SMatthew G Knepley   }
1997cb1e1211SMatthew G Knepley   if (name) {
1998cb1e1211SMatthew G Knepley     PetscValidPointer(name, 4);
1999cb1e1211SMatthew G Knepley     *name = b->name;
2000cb1e1211SMatthew G Knepley   }
200163d5297fSMatthew G. Knepley   if (labelname) {
200263d5297fSMatthew G. Knepley     PetscValidPointer(labelname, 5);
200363d5297fSMatthew G. Knepley     *labelname = b->labelname;
200463d5297fSMatthew G. Knepley   }
2005cb1e1211SMatthew G Knepley   if (field) {
200663d5297fSMatthew G. Knepley     PetscValidPointer(field, 6);
2007cb1e1211SMatthew G Knepley     *field = b->field;
2008cb1e1211SMatthew G Knepley   }
2009cb1e1211SMatthew G Knepley   if (func) {
201063d5297fSMatthew G. Knepley     PetscValidPointer(func, 7);
2011cb1e1211SMatthew G Knepley     *func = b->func;
2012cb1e1211SMatthew G Knepley   }
2013cb1e1211SMatthew G Knepley   if (numids) {
201463d5297fSMatthew G. Knepley     PetscValidPointer(numids, 8);
2015cb1e1211SMatthew G Knepley     *numids = b->numids;
2016cb1e1211SMatthew G Knepley   }
2017cb1e1211SMatthew G Knepley   if (ids) {
201863d5297fSMatthew G. Knepley     PetscValidPointer(ids, 9);
2019cb1e1211SMatthew G Knepley     *ids = b->ids;
2020cb1e1211SMatthew G Knepley   }
2021cb1e1211SMatthew G Knepley   if (ctx) {
202263d5297fSMatthew G. Knepley     PetscValidPointer(ctx, 10);
2023cb1e1211SMatthew G Knepley     *ctx = b->ctx;
2024cb1e1211SMatthew G Knepley   }
2025cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2026cb1e1211SMatthew G Knepley }
20270225b034SMatthew G. Knepley 
20280225b034SMatthew G. Knepley #undef __FUNCT__
20290225b034SMatthew G. Knepley #define __FUNCT__ "DMPlexIsBoundaryPoint"
20300225b034SMatthew G. Knepley PetscErrorCode DMPlexIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
20310225b034SMatthew G. Knepley {
20320225b034SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
20330225b034SMatthew G. Knepley   DMBoundary     b    = mesh->boundary;
20340225b034SMatthew G. Knepley   PetscErrorCode ierr;
20350225b034SMatthew G. Knepley 
20360225b034SMatthew G. Knepley   PetscFunctionBegin;
20370225b034SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20380225b034SMatthew G. Knepley   PetscValidPointer(isBd, 3);
20390225b034SMatthew G. Knepley   *isBd = PETSC_FALSE;
20400225b034SMatthew G. Knepley   while (b && !(*isBd)) {
20410225b034SMatthew G. Knepley     if (b->label) {
20420225b034SMatthew G. Knepley       PetscInt i;
20430225b034SMatthew G. Knepley 
20440225b034SMatthew G. Knepley       for (i = 0; i < b->numids && !(*isBd); ++i) {
20450225b034SMatthew G. Knepley         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
20460225b034SMatthew G. Knepley       }
20470225b034SMatthew G. Knepley     }
20480225b034SMatthew G. Knepley     b = b->next;
20490225b034SMatthew G. Knepley   }
20500225b034SMatthew G. Knepley   PetscFunctionReturn(0);
20510225b034SMatthew G. Knepley }
2052