xref: /petsc/src/dm/impls/plex/plexfem.c (revision 0f2d7e86914df288f8416ad81376576f5327bff0)
1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2cb1e1211SMatthew G Knepley 
3*0f2d7e86SMatthew 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);
118cb1e1211SMatthew G Knepley   ierr = DMPlexGetDimension(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__
185a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal"
186a18a7fb9SMatthew 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)
187a18a7fb9SMatthew G. Knepley {
188a18a7fb9SMatthew G. Knepley   PetscDualSpace *sp;
189a18a7fb9SMatthew G. Knepley   PetscSection    section;
190a18a7fb9SMatthew G. Knepley   PetscScalar    *values;
191a18a7fb9SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
192ad96f515SMatthew G. Knepley   PetscBool      *fieldActive;
193a18a7fb9SMatthew G. Knepley   PetscInt        numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, f, d, v, i, comp;
194a18a7fb9SMatthew G. Knepley   PetscErrorCode  ierr;
195a18a7fb9SMatthew G. Knepley 
196a18a7fb9SMatthew G. Knepley   PetscFunctionBegin;
197a18a7fb9SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
198a18a7fb9SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
199a18a7fb9SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
200a18a7fb9SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&sp,dim,&v0,dim*dim,&J);CHKERRQ(ierr);
201a18a7fb9SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
202a18a7fb9SMatthew G. Knepley     ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr);
203a18a7fb9SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
204a18a7fb9SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
205a18a7fb9SMatthew G. Knepley     totDim += spDim*numComp;
206a18a7fb9SMatthew G. Knepley   }
207a18a7fb9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
208a18a7fb9SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
209a18a7fb9SMatthew 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);
210a18a7fb9SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
211ad96f515SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
212ad96f515SMatthew G. Knepley   for (f = 0; f < numFields; ++f) fieldActive[f] = funcs[f] ? PETSC_TRUE : PETSC_FALSE;
213a18a7fb9SMatthew G. Knepley   for (i = 0; i < numIds; ++i) {
214a18a7fb9SMatthew G. Knepley     IS              pointIS;
215a18a7fb9SMatthew G. Knepley     const PetscInt *points;
216a18a7fb9SMatthew G. Knepley     PetscInt        n, p;
217a18a7fb9SMatthew G. Knepley 
218a18a7fb9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
219a18a7fb9SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
220a18a7fb9SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
221a18a7fb9SMatthew G. Knepley     for (p = 0; p < n; ++p) {
222a18a7fb9SMatthew G. Knepley       const PetscInt    point = points[p];
223a18a7fb9SMatthew G. Knepley       PetscCellGeometry geom;
224a18a7fb9SMatthew G. Knepley 
225a18a7fb9SMatthew G. Knepley       if ((point < cStart) || (point >= cEnd)) continue;
226a18a7fb9SMatthew G. Knepley       ierr = DMPlexComputeCellGeometry(dm, point, v0, J, NULL, &detJ);CHKERRQ(ierr);
227a18a7fb9SMatthew G. Knepley       geom.v0   = v0;
228a18a7fb9SMatthew G. Knepley       geom.J    = J;
229a18a7fb9SMatthew G. Knepley       geom.detJ = &detJ;
230a18a7fb9SMatthew G. Knepley       for (f = 0, v = 0; f < numFields; ++f) {
231a18a7fb9SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[f] : NULL;
232a18a7fb9SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr);
233a18a7fb9SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
234a18a7fb9SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
235a18a7fb9SMatthew G. Knepley           if (funcs[f]) {
236a18a7fb9SMatthew G. Knepley             ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
237a18a7fb9SMatthew G. Knepley           } else {
238a18a7fb9SMatthew G. Knepley             for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
239a18a7fb9SMatthew G. Knepley           }
240a18a7fb9SMatthew G. Knepley           v += numComp;
241a18a7fb9SMatthew G. Knepley         }
242a18a7fb9SMatthew G. Knepley       }
243ad96f515SMatthew G. Knepley       ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
244a18a7fb9SMatthew G. Knepley     }
245a18a7fb9SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
246a18a7fb9SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
247a18a7fb9SMatthew G. Knepley   }
248a18a7fb9SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
249ad96f515SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
250a18a7fb9SMatthew G. Knepley   ierr = PetscFree3(sp,v0,J);CHKERRQ(ierr);
251a18a7fb9SMatthew G. Knepley   PetscFunctionReturn(0);
252a18a7fb9SMatthew G. Knepley }
253a18a7fb9SMatthew G. Knepley 
254a18a7fb9SMatthew G. Knepley #undef __FUNCT__
255cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal"
256*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
257cb1e1211SMatthew G Knepley {
25872f94c41SMatthew G. Knepley   PetscDualSpace *sp;
25972f94c41SMatthew G. Knepley   PetscSection    section;
26072f94c41SMatthew G. Knepley   PetscScalar    *values;
26172f94c41SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
262120386c5SMatthew G. Knepley   PetscInt        numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp;
263cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
264cb1e1211SMatthew G Knepley 
265cb1e1211SMatthew G Knepley   PetscFunctionBegin;
266cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
26772f94c41SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
268785e854fSJed Brown   ierr = PetscMalloc1(numFields, &sp);CHKERRQ(ierr);
26972f94c41SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
270*0f2d7e86SMatthew G. Knepley     PetscFE fe;
271*0f2d7e86SMatthew G. Knepley 
272*0f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
273*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
274*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
27572f94c41SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
27672f94c41SMatthew G. Knepley     totDim += spDim*numComp;
277cb1e1211SMatthew G Knepley   }
27872f94c41SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
27972f94c41SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
28072f94c41SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
28172f94c41SMatthew 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);
28272f94c41SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
283dcca6d9dSJed Brown   ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr);
28472f94c41SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
28572f94c41SMatthew G. Knepley     PetscCellGeometry geom;
286cb1e1211SMatthew G Knepley 
287cb1e1211SMatthew G Knepley     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr);
28872f94c41SMatthew G. Knepley     geom.v0   = v0;
28972f94c41SMatthew G. Knepley     geom.J    = J;
29072f94c41SMatthew G. Knepley     geom.detJ = &detJ;
29172f94c41SMatthew G. Knepley     for (f = 0, v = 0; f < numFields; ++f) {
292*0f2d7e86SMatthew G. Knepley       PetscFE      fe;
293c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[f] : NULL;
294*0f2d7e86SMatthew G. Knepley 
295*0f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
296*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr);
29772f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
29872f94c41SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
299120386c5SMatthew G. Knepley         if (funcs[f]) {
300c110b1eeSGeoffrey Irving           ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr);
301120386c5SMatthew G. Knepley         } else {
302120386c5SMatthew G. Knepley           for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0;
303120386c5SMatthew G. Knepley         }
30472f94c41SMatthew G. Knepley         v += numComp;
305cb1e1211SMatthew G Knepley       }
306cb1e1211SMatthew G Knepley     }
30772f94c41SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
308cb1e1211SMatthew G Knepley   }
30972f94c41SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
3101f2da991SMatthew G. Knepley   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
31172f94c41SMatthew G. Knepley   ierr = PetscFree(sp);CHKERRQ(ierr);
312cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
313cb1e1211SMatthew G Knepley }
314cb1e1211SMatthew G Knepley 
315cb1e1211SMatthew G Knepley #undef __FUNCT__
316cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction"
317cb1e1211SMatthew G Knepley /*@C
318cb1e1211SMatthew G Knepley   DMPlexProjectFunction - This projects the given function into the function space provided.
319cb1e1211SMatthew G Knepley 
320cb1e1211SMatthew G Knepley   Input Parameters:
321cb1e1211SMatthew G Knepley + dm      - The DM
32272f94c41SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
323c110b1eeSGeoffrey Irving . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
324cb1e1211SMatthew G Knepley - mode    - The insertion mode for values
325cb1e1211SMatthew G Knepley 
326cb1e1211SMatthew G Knepley   Output Parameter:
327cb1e1211SMatthew G Knepley . X - vector
328cb1e1211SMatthew G Knepley 
329cb1e1211SMatthew G Knepley   Level: developer
330cb1e1211SMatthew G Knepley 
331878cb397SSatish Balay .seealso: DMPlexComputeL2Diff()
332878cb397SSatish Balay @*/
333*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
334cb1e1211SMatthew G Knepley {
335cb1e1211SMatthew G Knepley   Vec            localX;
336cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
337cb1e1211SMatthew G Knepley 
338cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3399a800dd8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
341*0f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
342cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
343cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
344cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
345cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
346cb1e1211SMatthew G Knepley }
347cb1e1211SMatthew G Knepley 
34855f2e967SMatthew G. Knepley #undef __FUNCT__
349*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
350*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, PetscFE fe[], PetscFE feAux[], Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX)
351*0f2d7e86SMatthew G. Knepley {
352*0f2d7e86SMatthew G. Knepley   DM                dmAux;
353*0f2d7e86SMatthew G. Knepley   PetscProblem      prob, probAux;
354*0f2d7e86SMatthew G. Knepley   Vec               A;
355*0f2d7e86SMatthew G. Knepley   PetscDualSpace   *sp;
356*0f2d7e86SMatthew G. Knepley   PetscSection      section, sectionAux;
357*0f2d7e86SMatthew G. Knepley   PetscScalar      *values, *u, *gradU, *a, *gradA, *coefficients, *coefficientsAux;
358*0f2d7e86SMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux;
359*0f2d7e86SMatthew G. Knepley   PetscInt          Nf, NfAux, Nc = 0, NcAux = 0, numCells, cOffset, cOffsetAux, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp;
360*0f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
361*0f2d7e86SMatthew G. Knepley 
362*0f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
363*0f2d7e86SMatthew G. Knepley   ierr = DMGetProblem(dm, &prob);CHKERRQ(ierr);
364*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
365*0f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
366*0f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
367*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
368*0f2d7e86SMatthew G. Knepley   numCells = cEnd - cStart;
369*0f2d7e86SMatthew G. Knepley   ierr = PetscMalloc5(Nf,&sp,Nf,&basisField,Nf,&basisFieldDer,Nf,&basisFieldAux,Nf,&basisFieldDerAux);CHKERRQ(ierr);
370*0f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
371*0f2d7e86SMatthew G. Knepley     PetscInt Nb, Ncf;
372*0f2d7e86SMatthew G. Knepley 
373*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr);
374*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr);
375*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe[f], &Ncf);CHKERRQ(ierr);
376*0f2d7e86SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
377*0f2d7e86SMatthew G. Knepley     totDim += spDim*Ncf;
378*0f2d7e86SMatthew G. Knepley     Nc     += Ncf;
379*0f2d7e86SMatthew G. Knepley   }
380*0f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
381*0f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
382*0f2d7e86SMatthew G. Knepley   if (dmAux) {
383*0f2d7e86SMatthew G. Knepley     ierr = DMGetProblem(dmAux, &probAux);CHKERRQ(ierr);
384*0f2d7e86SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
385*0f2d7e86SMatthew G. Knepley     ierr = PetscSectionGetNumFields(sectionAux, &NfAux);CHKERRQ(ierr);
386*0f2d7e86SMatthew G. Knepley     for (f = 0; f < NfAux; ++f) {
387*0f2d7e86SMatthew G. Knepley       PetscInt Nb, Ncf;
388*0f2d7e86SMatthew G. Knepley 
389*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(feAux[f], &Nb);CHKERRQ(ierr);
390*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feAux[f], &Ncf);CHKERRQ(ierr);
391*0f2d7e86SMatthew G. Knepley       NcAux += Ncf;
392*0f2d7e86SMatthew G. Knepley     }
393*0f2d7e86SMatthew G. Knepley   }
394*0f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localU);CHKERRQ(ierr);
395*0f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
396*0f2d7e86SMatthew 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);
397*0f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
398*0f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
399*0f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
400*0f2d7e86SMatthew G. Knepley     PetscInt Ncf;
401*0f2d7e86SMatthew G. Knepley 
402*0f2d7e86SMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
403*0f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
404*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe[f], &Ncf);CHKERRQ(ierr);
405*0f2d7e86SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
406*0f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
407*0f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
408*0f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
409*0f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
410*0f2d7e86SMatthew G. Knepley 
411*0f2d7e86SMatthew G. Knepley         if (funcs[f]) {
412*0f2d7e86SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr);
413*0f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
414*0f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe[f], numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
415*0f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
416*0f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
417*0f2d7e86SMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, &coefficients[cOffset],       NULL, u, gradU, NULL);CHKERRQ(ierr);
418*0f2d7e86SMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, &coefficientsAux[cOffsetAux], NULL, a, gradA, NULL);CHKERRQ(ierr);
419*0f2d7e86SMatthew G. Knepley             (*funcs[f])(u, gradU, a, gradA, x, &values[v]);
420*0f2d7e86SMatthew G. Knepley           }
421*0f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe[f], numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
422*0f2d7e86SMatthew G. Knepley         } else {
423*0f2d7e86SMatthew G. Knepley           for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0;
424*0f2d7e86SMatthew G. Knepley         }
425*0f2d7e86SMatthew G. Knepley         v += Ncf;
426*0f2d7e86SMatthew G. Knepley       }
427*0f2d7e86SMatthew G. Knepley     }
428*0f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
429*0f2d7e86SMatthew G. Knepley   }
430*0f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
431*0f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
432*0f2d7e86SMatthew G. Knepley   ierr = PetscFree5(sp,basisField,basisFieldDer,basisFieldAux,basisFieldDerAux);CHKERRQ(ierr);
433*0f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
434*0f2d7e86SMatthew G. Knepley }
435*0f2d7e86SMatthew G. Knepley 
436*0f2d7e86SMatthew G. Knepley #undef __FUNCT__
437*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectField"
438*0f2d7e86SMatthew G. Knepley /*@C
439*0f2d7e86SMatthew G. Knepley   DMPlexProjectField - This projects the given function of the fields into the function space provided.
440*0f2d7e86SMatthew G. Knepley 
441*0f2d7e86SMatthew G. Knepley   Input Parameters:
442*0f2d7e86SMatthew G. Knepley + dm      - The DM
443*0f2d7e86SMatthew G. Knepley . fe      - The PetscFE associated with the field
444*0f2d7e86SMatthew G. Knepley . U       - The input field vector
445*0f2d7e86SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
446*0f2d7e86SMatthew G. Knepley - mode    - The insertion mode for values
447*0f2d7e86SMatthew G. Knepley 
448*0f2d7e86SMatthew G. Knepley   Output Parameter:
449*0f2d7e86SMatthew G. Knepley . X       - The output vector
450*0f2d7e86SMatthew G. Knepley 
451*0f2d7e86SMatthew G. Knepley   Level: developer
452*0f2d7e86SMatthew G. Knepley 
453*0f2d7e86SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
454*0f2d7e86SMatthew G. Knepley @*/
455*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectField(DM dm, PetscFE fe[], PetscFE feAux[], Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X)
456*0f2d7e86SMatthew G. Knepley {
457*0f2d7e86SMatthew G. Knepley   Vec            localX, localU;
458*0f2d7e86SMatthew G. Knepley   PetscErrorCode ierr;
459*0f2d7e86SMatthew G. Knepley 
460*0f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
461*0f2d7e86SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
462*0f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
463*0f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr);
464*0f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
465*0f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
466*0f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFieldLocal(dm, fe, feAux, localU, funcs, mode, localX);CHKERRQ(ierr);
467*0f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
468*0f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
469*0f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
470*0f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr);
471*0f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
472*0f2d7e86SMatthew G. Knepley }
473*0f2d7e86SMatthew G. Knepley 
474*0f2d7e86SMatthew G. Knepley #undef __FUNCT__
4753351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM"
4763351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX)
47755f2e967SMatthew G. Knepley {
47855f2e967SMatthew G. Knepley   void        (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx);
47955f2e967SMatthew G. Knepley   void         **ctxs;
48055f2e967SMatthew G. Knepley   PetscFE       *fe;
48155f2e967SMatthew G. Knepley   PetscInt       numFields, f, numBd, b;
48255f2e967SMatthew G. Knepley   PetscErrorCode ierr;
48355f2e967SMatthew G. Knepley 
48455f2e967SMatthew G. Knepley   PetscFunctionBegin;
48555f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48655f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 2);
48755f2e967SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
48855f2e967SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
48955f2e967SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);}
49055f2e967SMatthew G. Knepley   /* OPT: Could attempt to do multiple BCs at once */
49155f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
49255f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
493a18a7fb9SMatthew G. Knepley     DMLabel         label;
49455f2e967SMatthew G. Knepley     const PetscInt *ids;
49563d5297fSMatthew G. Knepley     const char     *labelname;
49655f2e967SMatthew G. Knepley     PetscInt        numids, field;
49755f2e967SMatthew G. Knepley     PetscBool       isEssential;
49855f2e967SMatthew G. Knepley     void          (*func)();
49955f2e967SMatthew G. Knepley     void           *ctx;
50055f2e967SMatthew G. Knepley 
50163d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
50263d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
50355f2e967SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
50455f2e967SMatthew G. Knepley       funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL;
50555f2e967SMatthew G. Knepley       ctxs[f]  = field == f ? ctx : NULL;
50655f2e967SMatthew G. Knepley     }
507a18a7fb9SMatthew G. Knepley     ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
50855f2e967SMatthew G. Knepley   }
50955f2e967SMatthew G. Knepley   ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr);
51055f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
51155f2e967SMatthew G. Knepley }
51255f2e967SMatthew G. Knepley 
513cb1e1211SMatthew G Knepley #undef __FUNCT__
514cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
515cb1e1211SMatthew G Knepley /*@C
516cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
517cb1e1211SMatthew G Knepley 
518cb1e1211SMatthew G Knepley   Input Parameters:
519cb1e1211SMatthew G Knepley + dm    - The DM
520cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
52151259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
522cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
523cb1e1211SMatthew G Knepley 
524cb1e1211SMatthew G Knepley   Output Parameter:
525cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
526cb1e1211SMatthew G Knepley 
527cb1e1211SMatthew G Knepley   Level: developer
528cb1e1211SMatthew G Knepley 
52923d86601SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2GradientDiff()
530878cb397SSatish Balay @*/
531*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
532cb1e1211SMatthew G Knepley {
533cb1e1211SMatthew G Knepley   const PetscInt  debug = 0;
534cb1e1211SMatthew G Knepley   PetscSection    section;
535c5bbbd5bSMatthew G. Knepley   PetscQuadrature quad;
536cb1e1211SMatthew G Knepley   Vec             localX;
53772f94c41SMatthew G. Knepley   PetscScalar    *funcVal;
538cb1e1211SMatthew G Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
539cb1e1211SMatthew G Knepley   PetscReal       localDiff = 0.0;
540cb1e1211SMatthew G Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
541cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
542cb1e1211SMatthew G Knepley 
543cb1e1211SMatthew G Knepley   PetscFunctionBegin;
544cb1e1211SMatthew G Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
545cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
546cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
547cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
548cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
549cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
550cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
551*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
552c5bbbd5bSMatthew G. Knepley     PetscInt Nc;
553c5bbbd5bSMatthew G. Knepley 
554*0f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
555*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
556*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
557c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
558cb1e1211SMatthew G Knepley   }
559*0f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
560dcca6d9dSJed Brown   ierr = PetscMalloc5(numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
561cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
562cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
563a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
564cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
565cb1e1211SMatthew G Knepley 
566cb1e1211SMatthew G Knepley     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
567cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
568cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
569cb1e1211SMatthew G Knepley 
570cb1e1211SMatthew G Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
571*0f2d7e86SMatthew G. Knepley       PetscFE          fe;
572c110b1eeSGeoffrey Irving       void * const     ctx = ctxs ? ctxs[field] : NULL;
57321454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
574c5bbbd5bSMatthew G. Knepley       PetscReal       *basis;
57521454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
576cb1e1211SMatthew G Knepley 
577*0f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
57821454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
579*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
580*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
581*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
582cb1e1211SMatthew G Knepley       if (debug) {
583cb1e1211SMatthew G Knepley         char title[1024];
584cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
585cb1e1211SMatthew G Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
586cb1e1211SMatthew G Knepley       }
587cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
588cb1e1211SMatthew G Knepley         for (d = 0; d < dim; d++) {
589cb1e1211SMatthew G Knepley           coords[d] = v0[d];
590cb1e1211SMatthew G Knepley           for (e = 0; e < dim; e++) {
591cb1e1211SMatthew G Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
592cb1e1211SMatthew G Knepley           }
593cb1e1211SMatthew G Knepley         }
594c110b1eeSGeoffrey Irving         (*funcs[field])(coords, funcVal, ctx);
595cb1e1211SMatthew G Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
596a1d24da5SMatthew G. Knepley           PetscScalar interpolant = 0.0;
597a1d24da5SMatthew G. Knepley 
598cb1e1211SMatthew G Knepley           for (f = 0; f < numBasisFuncs; ++f) {
599cb1e1211SMatthew G Knepley             const PetscInt fidx = f*numBasisComps+fc;
600a1d24da5SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
601cb1e1211SMatthew G Knepley           }
60272f94c41SMatthew 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);}
60372f94c41SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
604cb1e1211SMatthew G Knepley         }
605cb1e1211SMatthew G Knepley       }
606cb1e1211SMatthew G Knepley       comp        += numBasisComps;
607cb1e1211SMatthew G Knepley       fieldOffset += numBasisFuncs*numBasisComps;
608cb1e1211SMatthew G Knepley     }
609cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
610cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
611cb1e1211SMatthew G Knepley     localDiff += elemDiff;
612cb1e1211SMatthew G Knepley   }
61372f94c41SMatthew G. Knepley   ierr  = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
614cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
61586a74ee0SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
616cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
617cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
618cb1e1211SMatthew G Knepley }
619cb1e1211SMatthew G Knepley 
620cb1e1211SMatthew G Knepley #undef __FUNCT__
62140e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
62240e14135SMatthew G. Knepley /*@C
62340e14135SMatthew 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.
62440e14135SMatthew G. Knepley 
62540e14135SMatthew G. Knepley   Input Parameters:
62640e14135SMatthew G. Knepley + dm    - The DM
62740e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
62851259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
62940e14135SMatthew G. Knepley . X     - The coefficient vector u_h
63040e14135SMatthew G. Knepley - n     - The vector to project along
63140e14135SMatthew G. Knepley 
63240e14135SMatthew G. Knepley   Output Parameter:
63340e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
63440e14135SMatthew G. Knepley 
63540e14135SMatthew G. Knepley   Level: developer
63640e14135SMatthew G. Knepley 
63740e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
63840e14135SMatthew G. Knepley @*/
639*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
640cb1e1211SMatthew G Knepley {
64140e14135SMatthew G. Knepley   const PetscInt  debug = 0;
642cb1e1211SMatthew G Knepley   PetscSection    section;
64340e14135SMatthew G. Knepley   PetscQuadrature quad;
64440e14135SMatthew G. Knepley   Vec             localX;
64540e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
64640e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
64740e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
64840e14135SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
649cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
650cb1e1211SMatthew G Knepley 
651cb1e1211SMatthew G Knepley   PetscFunctionBegin;
65240e14135SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
65340e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
65440e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
65540e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
65640e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
65740e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
658652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
659*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
66040e14135SMatthew G. Knepley     PetscInt Nc;
661652b88e8SMatthew G. Knepley 
662*0f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
663*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
664*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
66540e14135SMatthew G. Knepley     numComponents += Nc;
666652b88e8SMatthew G. Knepley   }
66740e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
66840e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
66940e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
67040e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
67140e14135SMatthew G. Knepley     PetscScalar *x = NULL;
67240e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
673652b88e8SMatthew G. Knepley 
67440e14135SMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
67540e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
67640e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
67740e14135SMatthew G. Knepley 
67840e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
679*0f2d7e86SMatthew G. Knepley       PetscFE          fe;
68051259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
68121454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
68240e14135SMatthew G. Knepley       PetscReal       *basisDer;
68321454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
68440e14135SMatthew G. Knepley 
685*0f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
68621454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
687*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
688*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
689*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
69040e14135SMatthew G. Knepley       if (debug) {
69140e14135SMatthew G. Knepley         char title[1024];
69240e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
69340e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
694652b88e8SMatthew G. Knepley       }
69540e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
69640e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
69740e14135SMatthew G. Knepley           coords[d] = v0[d];
69840e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
69940e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
700652b88e8SMatthew G. Knepley           }
70140e14135SMatthew G. Knepley         }
70251259fa3SMatthew G. Knepley         (*funcs[field])(coords, n, funcVal, ctx);
70340e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
70440e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
70540e14135SMatthew G. Knepley 
70640e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
70740e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
70840e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
70940e14135SMatthew G. Knepley 
71040e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
71140e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
71240e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
71340e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
71440e14135SMatthew G. Knepley               }
71540e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
71640e14135SMatthew G. Knepley             }
71740e14135SMatthew G. Knepley           }
71840e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
71940e14135SMatthew 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);}
72040e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
72140e14135SMatthew G. Knepley         }
72240e14135SMatthew G. Knepley       }
72340e14135SMatthew G. Knepley       comp        += Ncomp;
72440e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
72540e14135SMatthew G. Knepley     }
72640e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
72740e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
72840e14135SMatthew G. Knepley     localDiff += elemDiff;
72940e14135SMatthew G. Knepley   }
73040e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
73140e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
73240e14135SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
73340e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
734cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
735cb1e1211SMatthew G Knepley }
736cb1e1211SMatthew G Knepley 
737a0845e3aSMatthew G. Knepley #undef __FUNCT__
73873d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
739*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
74073d901b8SMatthew G. Knepley {
74173d901b8SMatthew G. Knepley   const PetscInt  debug = 0;
74273d901b8SMatthew G. Knepley   PetscSection    section;
74373d901b8SMatthew G. Knepley   PetscQuadrature quad;
74473d901b8SMatthew G. Knepley   Vec             localX;
74573d901b8SMatthew G. Knepley   PetscScalar    *funcVal;
74673d901b8SMatthew G. Knepley   PetscReal      *coords, *v0, *J, *invJ, detJ;
74773d901b8SMatthew G. Knepley   PetscReal      *localDiff;
74873d901b8SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
74973d901b8SMatthew G. Knepley   PetscErrorCode  ierr;
75073d901b8SMatthew G. Knepley 
75173d901b8SMatthew G. Knepley   PetscFunctionBegin;
75273d901b8SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
75373d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
75473d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
75573d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
75673d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
75773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
75873d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
759*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
76073d901b8SMatthew G. Knepley     PetscInt Nc;
76173d901b8SMatthew G. Knepley 
762*0f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
763*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
764*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
76573d901b8SMatthew G. Knepley     numComponents += Nc;
76673d901b8SMatthew G. Knepley   }
767*0f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
76873d901b8SMatthew G. Knepley   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
76973d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
77073d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
77173d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
77273d901b8SMatthew G. Knepley 
77373d901b8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr);
77473d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
77573d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
77673d901b8SMatthew G. Knepley 
77773d901b8SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
778*0f2d7e86SMatthew G. Knepley       PetscFE          fe;
77973d901b8SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
78073d901b8SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
78173d901b8SMatthew G. Knepley       PetscReal       *basis, elemDiff = 0.0;
78273d901b8SMatthew G. Knepley       PetscInt         numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f;
78373d901b8SMatthew G. Knepley 
784*0f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
78573d901b8SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
786*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr);
787*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr);
788*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr);
78973d901b8SMatthew G. Knepley       if (debug) {
79073d901b8SMatthew G. Knepley         char title[1024];
79173d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
79273d901b8SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr);
79373d901b8SMatthew G. Knepley       }
79473d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
79573d901b8SMatthew G. Knepley         for (d = 0; d < dim; d++) {
79673d901b8SMatthew G. Knepley           coords[d] = v0[d];
79773d901b8SMatthew G. Knepley           for (e = 0; e < dim; e++) {
79873d901b8SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
79973d901b8SMatthew G. Knepley           }
80073d901b8SMatthew G. Knepley         }
80173d901b8SMatthew G. Knepley         (*funcs[field])(coords, funcVal, ctx);
80273d901b8SMatthew G. Knepley         for (fc = 0; fc < numBasisComps; ++fc) {
80373d901b8SMatthew G. Knepley           PetscScalar interpolant = 0.0;
80473d901b8SMatthew G. Knepley 
80573d901b8SMatthew G. Knepley           for (f = 0; f < numBasisFuncs; ++f) {
80673d901b8SMatthew G. Knepley             const PetscInt fidx = f*numBasisComps+fc;
80773d901b8SMatthew G. Knepley             interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx];
80873d901b8SMatthew G. Knepley           }
80973d901b8SMatthew 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);}
81073d901b8SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
81173d901b8SMatthew G. Knepley         }
81273d901b8SMatthew G. Knepley       }
81373d901b8SMatthew G. Knepley       comp        += numBasisComps;
81473d901b8SMatthew G. Knepley       fieldOffset += numBasisFuncs*numBasisComps;
81573d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
81673d901b8SMatthew G. Knepley     }
81773d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
81873d901b8SMatthew G. Knepley   }
81973d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82073d901b8SMatthew G. Knepley   ierr  = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
82173d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
82273d901b8SMatthew G. Knepley   ierr  = PetscFree6(localDiff,funcVal,coords,v0,J,invJ);CHKERRQ(ierr);
82373d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
82473d901b8SMatthew G. Knepley }
82573d901b8SMatthew G. Knepley 
82673d901b8SMatthew G. Knepley #undef __FUNCT__
82773d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
82873d901b8SMatthew G. Knepley /*@
82973d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
83073d901b8SMatthew G. Knepley 
83173d901b8SMatthew G. Knepley   Input Parameters:
83273d901b8SMatthew G. Knepley + dm - The mesh
83373d901b8SMatthew G. Knepley . X  - Local input vector
83473d901b8SMatthew G. Knepley - user - The user context
83573d901b8SMatthew G. Knepley 
83673d901b8SMatthew G. Knepley   Output Parameter:
83773d901b8SMatthew G. Knepley . integral - Local integral for each field
83873d901b8SMatthew G. Knepley 
83973d901b8SMatthew G. Knepley   Level: developer
84073d901b8SMatthew G. Knepley 
84173d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
84273d901b8SMatthew G. Knepley @*/
843*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
84473d901b8SMatthew G. Knepley {
84573d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
84673d901b8SMatthew G. Knepley   DM                dmAux;
84773d901b8SMatthew G. Knepley   Vec               localX, A;
848*0f2d7e86SMatthew G. Knepley   PetscProblem      prob, probAux = NULL;
84973d901b8SMatthew G. Knepley   PetscQuadrature   q;
85073d901b8SMatthew G. Knepley   PetscCellGeometry geom;
85173d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
85273d901b8SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
85373d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
854*0f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
855*0f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
85673d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
85773d901b8SMatthew G. Knepley 
85873d901b8SMatthew G. Knepley   PetscFunctionBegin;
85973d901b8SMatthew G. Knepley   /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
86073d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
86173d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
86273d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
86373d901b8SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
86473d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
865*0f2d7e86SMatthew G. Knepley   ierr = DMGetProblem(dm, &prob);CHKERRQ(ierr);
866*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
86773d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
86873d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
86973d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
870*0f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {integral[f]    = 0.0;}
87173d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
87273d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
87373d901b8SMatthew G. Knepley   if (dmAux) {
87473d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
875*0f2d7e86SMatthew G. Knepley     ierr = DMGetProblem(dmAux, &probAux);CHKERRQ(ierr);
876*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
87773d901b8SMatthew G. Knepley   }
87873d901b8SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localX);CHKERRQ(ierr);
879*0f2d7e86SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ);CHKERRQ(ierr);
880*0f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
88173d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
88273d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
88373d901b8SMatthew G. Knepley     PetscInt     i;
88473d901b8SMatthew G. Knepley 
88573d901b8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
88673d901b8SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
88773d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
888*0f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
88973d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
89073d901b8SMatthew G. Knepley     if (dmAux) {
89173d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
892*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
89373d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
89473d901b8SMatthew G. Knepley     }
89573d901b8SMatthew G. Knepley   }
89673d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
897*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
89873d901b8SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
89973d901b8SMatthew G. Knepley     /* Conforming batches */
90073d901b8SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
90173d901b8SMatthew G. Knepley     /* Remainder */
90273d901b8SMatthew G. Knepley     PetscInt Nr, offset;
90373d901b8SMatthew G. Knepley 
904*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
905*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
906*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
907*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
90873d901b8SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
90973d901b8SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
91073d901b8SMatthew G. Knepley     batchSize = numBlocks * blockSize;
911*0f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
91273d901b8SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
91373d901b8SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
91473d901b8SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
91573d901b8SMatthew G. Knepley     offset    = numCells - Nr;
91673d901b8SMatthew G. Knepley     geom.v0   = v0;
91773d901b8SMatthew G. Knepley     geom.J    = J;
91873d901b8SMatthew G. Knepley     geom.invJ = invJ;
91973d901b8SMatthew G. Knepley     geom.detJ = detJ;
920*0f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Ne, geom, u, probAux, a, integral);CHKERRQ(ierr);
92173d901b8SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
92273d901b8SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
92373d901b8SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
92473d901b8SMatthew G. Knepley     geom.detJ = &detJ[offset];
925*0f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Nr, geom, &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr);
92673d901b8SMatthew G. Knepley   }
92773d901b8SMatthew G. Knepley   ierr = PetscFree5(u,v0,J,invJ,detJ);CHKERRQ(ierr);
92873d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
92973d901b8SMatthew G. Knepley   if (mesh->printFEM) {
93073d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
93173d901b8SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);}
93273d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
93373d901b8SMatthew G. Knepley   }
93473d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
93573d901b8SMatthew G. Knepley   /* TODO: Allreduce for integral */
93673d901b8SMatthew G. Knepley   /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
93773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
93873d901b8SMatthew G. Knepley }
93973d901b8SMatthew G. Knepley 
94073d901b8SMatthew G. Knepley #undef __FUNCT__
941*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal"
942*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
943*0f2d7e86SMatthew G. Knepley {
944*0f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
945*0f2d7e86SMatthew G. Knepley   const char       *name  = "Residual";
946*0f2d7e86SMatthew G. Knepley   DM                dmAux;
947*0f2d7e86SMatthew G. Knepley   DMLabel           depth;
948*0f2d7e86SMatthew G. Knepley   Vec               A;
949*0f2d7e86SMatthew G. Knepley   PetscProblem      prob, probAux = NULL;
950*0f2d7e86SMatthew G. Knepley   PetscQuadrature   q;
951*0f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
952*0f2d7e86SMatthew G. Knepley   PetscSection      section, sectionAux;
953*0f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
954*0f2d7e86SMatthew G. Knepley   PetscScalar      *elemVec, *u, *u_t, *a = NULL;
955*0f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd;
956*0f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux;
957*0f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
958*0f2d7e86SMatthew G. Knepley 
959*0f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
960*0f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
961*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
962*0f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
963*0f2d7e86SMatthew G. Knepley   ierr = DMGetProblem(dm, &prob);CHKERRQ(ierr);
964*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
965*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
966*0f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
967*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
968*0f2d7e86SMatthew G. Knepley   numCells = cEnd - cStart;
969*0f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
970*0f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
971*0f2d7e86SMatthew G. Knepley   if (dmAux) {
972*0f2d7e86SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
973*0f2d7e86SMatthew G. Knepley     ierr = DMGetProblem(dmAux, &probAux);CHKERRQ(ierr);
974*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
975*0f2d7e86SMatthew G. Knepley   }
976*0f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
977*0f2d7e86SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
978*0f2d7e86SMatthew 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);
979*0f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
980*0f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
981*0f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
982*0f2d7e86SMatthew G. Knepley     PetscInt     i;
983*0f2d7e86SMatthew G. Knepley 
984*0f2d7e86SMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
985*0f2d7e86SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
986*0f2d7e86SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
987*0f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
988*0f2d7e86SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
989*0f2d7e86SMatthew G. Knepley     if (X_t) {
990*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
991*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
992*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
993*0f2d7e86SMatthew G. Knepley     }
994*0f2d7e86SMatthew G. Knepley     if (dmAux) {
995*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
996*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
997*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
998*0f2d7e86SMatthew G. Knepley     }
999*0f2d7e86SMatthew G. Knepley   }
1000*0f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1001*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
1002*0f2d7e86SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1003*0f2d7e86SMatthew G. Knepley     /* Conforming batches */
1004*0f2d7e86SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1005*0f2d7e86SMatthew G. Knepley     /* Remainder */
1006*0f2d7e86SMatthew G. Knepley     PetscInt Nr, offset;
1007*0f2d7e86SMatthew G. Knepley 
1008*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1009*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1010*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1011*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1012*0f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1013*0f2d7e86SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1014*0f2d7e86SMatthew G. Knepley     batchSize = numBlocks * blockSize;
1015*0f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1016*0f2d7e86SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1017*0f2d7e86SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1018*0f2d7e86SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1019*0f2d7e86SMatthew G. Knepley     offset    = numCells - Nr;
1020*0f2d7e86SMatthew G. Knepley     geom.v0   = v0;
1021*0f2d7e86SMatthew G. Knepley     geom.J    = J;
1022*0f2d7e86SMatthew G. Knepley     geom.invJ = invJ;
1023*0f2d7e86SMatthew G. Knepley     geom.detJ = detJ;
1024*0f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
1025*0f2d7e86SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
1026*0f2d7e86SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
1027*0f2d7e86SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
1028*0f2d7e86SMatthew G. Knepley     geom.detJ = &detJ[offset];
1029*0f2d7e86SMatthew 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);
1030*0f2d7e86SMatthew G. Knepley   }
1031*0f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1032*0f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);}
1033*0f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
1034*0f2d7e86SMatthew G. Knepley   }
1035*0f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
1036*0f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1037*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1038*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
1039*0f2d7e86SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
1040*0f2d7e86SMatthew G. Knepley     const char     *bdLabel;
1041*0f2d7e86SMatthew G. Knepley     DMLabel         label;
1042*0f2d7e86SMatthew G. Knepley     IS              pointIS;
1043*0f2d7e86SMatthew G. Knepley     const PetscInt *points;
1044*0f2d7e86SMatthew G. Knepley     const PetscInt *values;
1045*0f2d7e86SMatthew G. Knepley     PetscReal      *n;
1046*0f2d7e86SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
1047*0f2d7e86SMatthew G. Knepley     PetscBool       isEssential;
1048*0f2d7e86SMatthew G. Knepley 
1049*0f2d7e86SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
1050*0f2d7e86SMatthew G. Knepley     if (isEssential) continue;
1051*0f2d7e86SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
1052*0f2d7e86SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1053*0f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
1054*0f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
1055*0f2d7e86SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1056*0f2d7e86SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1057*0f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1058*0f2d7e86SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1059*0f2d7e86SMatthew G. Knepley     }
1060*0f2d7e86SMatthew 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);
1061*0f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1062*0f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1063*0f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1064*0f2d7e86SMatthew G. Knepley       PetscScalar   *x     = NULL;
1065*0f2d7e86SMatthew G. Knepley       PetscInt       i;
1066*0f2d7e86SMatthew G. Knepley 
1067*0f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1068*0f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
1069*0f2d7e86SMatthew G. Knepley       ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1070*0f2d7e86SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1071*0f2d7e86SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1072*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1073*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1074*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1075*0f2d7e86SMatthew G. Knepley       if (X_t) {
1076*0f2d7e86SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1077*0f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1078*0f2d7e86SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1079*0f2d7e86SMatthew G. Knepley       }
1080*0f2d7e86SMatthew G. Knepley       ++f;
1081*0f2d7e86SMatthew G. Knepley     }
1082*0f2d7e86SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1083*0f2d7e86SMatthew G. Knepley       PetscFE  fe;
1084*0f2d7e86SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1085*0f2d7e86SMatthew G. Knepley       /* Conforming batches */
1086*0f2d7e86SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1087*0f2d7e86SMatthew G. Knepley       /* Remainder */
1088*0f2d7e86SMatthew G. Knepley       PetscInt Nr, offset;
1089*0f2d7e86SMatthew G. Knepley 
1090*0f2d7e86SMatthew G. Knepley       ierr = PetscProblemGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1091*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1092*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1093*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1094*0f2d7e86SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1095*0f2d7e86SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
1096*0f2d7e86SMatthew G. Knepley       batchSize = numBlocks * blockSize;
1097*0f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1098*0f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
1099*0f2d7e86SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
1100*0f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
1101*0f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
1102*0f2d7e86SMatthew G. Knepley       geom.v0   = v0;
1103*0f2d7e86SMatthew G. Knepley       geom.n    = n;
1104*0f2d7e86SMatthew G. Knepley       geom.J    = J;
1105*0f2d7e86SMatthew G. Knepley       geom.invJ = invJ;
1106*0f2d7e86SMatthew G. Knepley       geom.detJ = detJ;
1107*0f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
1108*0f2d7e86SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1109*0f2d7e86SMatthew G. Knepley       geom.n    = &n[offset*dim];
1110*0f2d7e86SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1111*0f2d7e86SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1112*0f2d7e86SMatthew G. Knepley       geom.detJ = &detJ[offset];
1113*0f2d7e86SMatthew 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);
1114*0f2d7e86SMatthew G. Knepley     }
1115*0f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1116*0f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1117*0f2d7e86SMatthew G. Knepley 
1118*0f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
1119*0f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
1120*0f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
1121*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
1122*0f2d7e86SMatthew G. Knepley       ++f;
1123*0f2d7e86SMatthew G. Knepley     }
1124*0f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1125*0f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1126*0f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
1127*0f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1128*0f2d7e86SMatthew G. Knepley   }
1129*0f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
1130*0f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1131*0f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
1132*0f2d7e86SMatthew G. Knepley }
1133*0f2d7e86SMatthew G. Knepley 
1134*0f2d7e86SMatthew G. Knepley #undef __FUNCT__
1135*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
1136a0845e3aSMatthew G. Knepley /*@
1137*0f2d7e86SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1138a0845e3aSMatthew G. Knepley 
1139a0845e3aSMatthew G. Knepley   Input Parameters:
1140a0845e3aSMatthew G. Knepley + dm - The mesh
1141*0f2d7e86SMatthew G. Knepley . X  - Local solution
1142*0f2d7e86SMatthew G. Knepley . X_t - Local solution time derivative, or NULL
1143a0845e3aSMatthew G. Knepley - user - The user context
1144a0845e3aSMatthew G. Knepley 
1145a0845e3aSMatthew G. Knepley   Output Parameter:
1146a0845e3aSMatthew G. Knepley . F  - Local output vector
1147a0845e3aSMatthew G. Knepley 
1148a0845e3aSMatthew G. Knepley   Note:
11498026896cSMatthew G. Knepley   The first member of the user context must be an FEMContext.
1150a0845e3aSMatthew G. Knepley 
1151a0845e3aSMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1152a0845e3aSMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1153a0845e3aSMatthew G. Knepley 
1154a0845e3aSMatthew G. Knepley   Level: developer
1155a0845e3aSMatthew G. Knepley 
1156a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1157a0845e3aSMatthew G. Knepley @*/
1158*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1159a0845e3aSMatthew G. Knepley {
1160a0845e3aSMatthew G. Knepley   PetscErrorCode ierr;
1161a0845e3aSMatthew G. Knepley 
1162a0845e3aSMatthew G. Knepley   PetscFunctionBegin;
1163*0f2d7e86SMatthew G. Knepley   ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);
1164*0f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
1165*0f2d7e86SMatthew G. Knepley }
1166*0f2d7e86SMatthew G. Knepley 
1167*0f2d7e86SMatthew G. Knepley #undef __FUNCT__
1168*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
1169*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
1170*0f2d7e86SMatthew G. Knepley {
1171*0f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
1172*0f2d7e86SMatthew G. Knepley   const char       *name  = "Jacobian";
1173*0f2d7e86SMatthew G. Knepley   DM                dmAux;
1174*0f2d7e86SMatthew G. Knepley   DMLabel           depth;
1175*0f2d7e86SMatthew G. Knepley   Vec               A;
1176*0f2d7e86SMatthew G. Knepley   PetscProblem      prob, probAux = NULL;
1177*0f2d7e86SMatthew G. Knepley   PetscQuadrature   quad;
1178*0f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
1179*0f2d7e86SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
1180*0f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
1181*0f2d7e86SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
1182*0f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
1183*0f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
1184*0f2d7e86SMatthew G. Knepley   PetscBool         isShell;
1185*0f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
1186*0f2d7e86SMatthew G. Knepley 
1187*0f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
1188*0f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1189a0845e3aSMatthew G. Knepley   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1190a0845e3aSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1191*0f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
1192*0f2d7e86SMatthew G. Knepley   ierr = DMGetProblem(dm, &prob);CHKERRQ(ierr);
1193*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1194*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
11959a559087SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1196a0845e3aSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1197a0845e3aSMatthew G. Knepley   numCells = cEnd - cStart;
11989a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
11999a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
12009a559087SMatthew G. Knepley   if (dmAux) {
12019a559087SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
1202*0f2d7e86SMatthew G. Knepley     ierr = DMGetProblem(dmAux, &probAux);CHKERRQ(ierr);
1203*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
12049a559087SMatthew G. Knepley   }
12053351dd3dSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
1206*0f2d7e86SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
1207*0f2d7e86SMatthew 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);
1208*0f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1209a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1210*0f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
1211a0845e3aSMatthew G. Knepley     PetscInt     i;
1212a0845e3aSMatthew G. Knepley 
1213a0845e3aSMatthew G. Knepley     ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1214a0845e3aSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1215a0845e3aSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1216*0f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1217a0845e3aSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1218*0f2d7e86SMatthew G. Knepley     if (X_t) {
1219*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1220*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
1221*0f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1222*0f2d7e86SMatthew G. Knepley     }
12239a559087SMatthew G. Knepley     if (dmAux) {
12249a559087SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1225*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
12269a559087SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1227a0845e3aSMatthew G. Knepley     }
12289a559087SMatthew G. Knepley   }
1229*0f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1230*0f2d7e86SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
1231*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
123221454ff5SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1233a0845e3aSMatthew G. Knepley     /* Conforming batches */
1234f30c5766SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1235a0845e3aSMatthew G. Knepley     /* Remainder */
1236a0845e3aSMatthew G. Knepley     PetscInt Nr, offset;
1237a0845e3aSMatthew G. Knepley 
1238*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
1239*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1240*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1241*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1242*0f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
124321454ff5SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1244a0845e3aSMatthew G. Knepley     batchSize = numBlocks * blockSize;
1245*0f2d7e86SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1246a0845e3aSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1247a0845e3aSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1248a0845e3aSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1249a0845e3aSMatthew G. Knepley     offset    = numCells - Nr;
1250*0f2d7e86SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1251a0845e3aSMatthew G. Knepley       geom.v0   = v0;
1252a0845e3aSMatthew G. Knepley       geom.J    = J;
1253a0845e3aSMatthew G. Knepley       geom.invJ = invJ;
1254a0845e3aSMatthew G. Knepley       geom.detJ = detJ;
1255*0f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1256a0845e3aSMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1257a0845e3aSMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1258a0845e3aSMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1259a0845e3aSMatthew G. Knepley       geom.detJ = &detJ[offset];
1260*0f2d7e86SMatthew 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);
1261*0f2d7e86SMatthew G. Knepley     }
1262a0845e3aSMatthew G. Knepley   }
1263a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1264*0f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
1265*0f2d7e86SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
1266a0845e3aSMatthew G. Knepley   }
1267*0f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
12689a559087SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1269*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1270*0f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
12711c093863SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
12721c093863SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
12731c093863SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
12741c093863SMatthew G. Knepley     const char     *bdLabel;
12751c093863SMatthew G. Knepley     DMLabel         label;
12761c093863SMatthew G. Knepley     IS              pointIS;
12771c093863SMatthew G. Knepley     const PetscInt *points;
12781c093863SMatthew G. Knepley     const PetscInt *values;
12791c093863SMatthew G. Knepley     PetscReal      *n;
12801c093863SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
12811c093863SMatthew G. Knepley     PetscBool       isEssential;
12821c093863SMatthew G. Knepley 
128363d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
1284*0f2d7e86SMatthew G. Knepley     if (isEssential) continue;
12851c093863SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
12861c093863SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
12871c093863SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
12881c093863SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
12891c093863SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1290075da914SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1291075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1292075da914SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1293075da914SMatthew G. Knepley     }
1294*0f2d7e86SMatthew 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);
1295*0f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1296075da914SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1297f1ea0e2fSMatthew G. Knepley       const PetscInt point = points[p];
1298f1ea0e2fSMatthew G. Knepley       PetscScalar   *x     = NULL;
1299f1ea0e2fSMatthew G. Knepley       PetscInt       i;
1300f1ea0e2fSMatthew G. Knepley 
1301075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1302075da914SMatthew G. Knepley       if (dep != dim-1) continue;
1303075da914SMatthew G. Knepley       ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1304a8007bbfSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1305075da914SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1306f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1307*0f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1308f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1309*0f2d7e86SMatthew G. Knepley       if (X_t) {
1310af1eca97SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1311*0f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1312af1eca97SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1313*0f2d7e86SMatthew G. Knepley       }
1314af1eca97SMatthew G. Knepley       ++f;
1315af1eca97SMatthew G. Knepley     }
1316*0f2d7e86SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
1317*0f2d7e86SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
1318*0f2d7e86SMatthew G. Knepley       PetscFE  fe;
1319af1eca97SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1320af1eca97SMatthew G. Knepley       /* Conforming batches */
1321af1eca97SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1322af1eca97SMatthew G. Knepley       /* Remainder */
1323af1eca97SMatthew G. Knepley       PetscInt Nr, offset;
1324af1eca97SMatthew G. Knepley 
1325*0f2d7e86SMatthew G. Knepley       ierr = PetscProblemGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
1326*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1327*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1328*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
132921454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
133021454ff5SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
13310483ade4SMatthew G. Knepley       batchSize = numBlocks * blockSize;
1332*0f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1333*0f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
13340483ade4SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
1335*0f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
1336*0f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
1337*0f2d7e86SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
13380483ade4SMatthew G. Knepley         geom.v0   = v0;
1339*0f2d7e86SMatthew G. Knepley         geom.n    = n;
13400483ade4SMatthew G. Knepley         geom.J    = J;
13410483ade4SMatthew G. Knepley         geom.invJ = invJ;
13420483ade4SMatthew G. Knepley         geom.detJ = detJ;
1343*0f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
13440483ade4SMatthew G. Knepley         geom.v0   = &v0[offset*dim];
1345*0f2d7e86SMatthew G. Knepley         geom.n    = &n[offset*dim];
13460483ade4SMatthew G. Knepley         geom.J    = &J[offset*dim*dim];
13470483ade4SMatthew G. Knepley         geom.invJ = &invJ[offset*dim*dim];
13480483ade4SMatthew G. Knepley         geom.detJ = &detJ[offset];
1349*0f2d7e86SMatthew 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);
1350cb1e1211SMatthew G Knepley       }
1351cb1e1211SMatthew G Knepley     }
1352*0f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1353*0f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1354cb1e1211SMatthew G Knepley 
1355*0f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
1356*0f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
1357*0f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
1358*0f2d7e86SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
1359*0f2d7e86SMatthew G. Knepley       ++f;
1360cb1e1211SMatthew G Knepley     }
1361*0f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1362*0f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1363*0f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr);
1364*0f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1365cb1e1211SMatthew G Knepley   }
1366*0f2d7e86SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1367*0f2d7e86SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1368*0f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {
1369*0f2d7e86SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1370*0f2d7e86SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
1371*0f2d7e86SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1372*0f2d7e86SMatthew G. Knepley   }
1373*0f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1374*0f2d7e86SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
1375*0f2d7e86SMatthew G. Knepley   if (isShell) {
1376*0f2d7e86SMatthew G. Knepley     JacActionCtx *jctx;
1377*0f2d7e86SMatthew G. Knepley 
1378*0f2d7e86SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
1379*0f2d7e86SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
1380*0f2d7e86SMatthew G. Knepley   }
1381cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1382cb1e1211SMatthew G Knepley }
1383cb1e1211SMatthew G Knepley 
1384cb1e1211SMatthew G Knepley #undef __FUNCT__
1385*0f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
1386cb1e1211SMatthew G Knepley /*@
1387*0f2d7e86SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
1388cb1e1211SMatthew G Knepley 
1389cb1e1211SMatthew G Knepley   Input Parameters:
1390cb1e1211SMatthew G Knepley + dm - The mesh
1391cb1e1211SMatthew G Knepley . X  - Local input vector
1392cb1e1211SMatthew G Knepley - user - The user context
1393cb1e1211SMatthew G Knepley 
1394cb1e1211SMatthew G Knepley   Output Parameter:
1395cb1e1211SMatthew G Knepley . Jac  - Jacobian matrix
1396cb1e1211SMatthew G Knepley 
1397cb1e1211SMatthew G Knepley   Note:
13988026896cSMatthew G. Knepley   The first member of the user context must be an FEMContext.
1399cb1e1211SMatthew G Knepley 
1400cb1e1211SMatthew G Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1401cb1e1211SMatthew G Knepley   like a GPU, or vectorize on a multicore machine.
1402cb1e1211SMatthew G Knepley 
14030059ad2aSSatish Balay   Level: developer
14040059ad2aSSatish Balay 
1405cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal()
1406878cb397SSatish Balay @*/
1407*0f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
1408cb1e1211SMatthew G Knepley {
1409cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1410cb1e1211SMatthew G Knepley 
1411cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1412*0f2d7e86SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
1413cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1414cb1e1211SMatthew G Knepley }
1415bceba477SMatthew G. Knepley 
1416d69c5d34SMatthew G. Knepley #undef __FUNCT__
1417d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1418d69c5d34SMatthew G. Knepley /*@
1419d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1420d69c5d34SMatthew G. Knepley 
1421d69c5d34SMatthew G. Knepley   Input Parameters:
1422d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1423d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1424d69c5d34SMatthew G. Knepley - user - The user context
1425d69c5d34SMatthew G. Knepley 
1426d69c5d34SMatthew G. Knepley   Output Parameter:
1427934789fcSMatthew G. Knepley . In  - The interpolation matrix
1428d69c5d34SMatthew G. Knepley 
1429d69c5d34SMatthew G. Knepley   Note:
1430d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1431d69c5d34SMatthew G. Knepley 
1432d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1433d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1434d69c5d34SMatthew G. Knepley 
1435d69c5d34SMatthew G. Knepley   Level: developer
1436d69c5d34SMatthew G. Knepley 
1437d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1438d69c5d34SMatthew G. Knepley @*/
1439934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1440d69c5d34SMatthew G. Knepley {
1441d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1442d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
1443*0f2d7e86SMatthew G. Knepley   PetscProblem      prob;
1444d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
1445d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1446d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1447d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
1448942a7a06SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
1449*0f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1450d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1451d69c5d34SMatthew G. Knepley 
1452d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1453d69c5d34SMatthew G. Knepley #if 0
1454d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1455d69c5d34SMatthew G. Knepley #endif
1456d69c5d34SMatthew G. Knepley   ierr = DMPlexGetDimension(dmf, &dim);CHKERRQ(ierr);
1457d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1458d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1459d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1460d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1461d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1462d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
1463*0f2d7e86SMatthew G. Knepley   ierr = DMGetProblem(dmf, &prob);CHKERRQ(ierr);
1464d69c5d34SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
1465d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1466*0f2d7e86SMatthew G. Knepley     PetscFE  fe;
1467*0f2d7e86SMatthew G. Knepley     PetscInt rNb, Nc;
1468d69c5d34SMatthew G. Knepley 
1469*0f2d7e86SMatthew G. Knepley     ierr = PetscProblemGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1470*0f2d7e86SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1471d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
1472*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1473*0f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1474d69c5d34SMatthew G. Knepley   }
1475*0f2d7e86SMatthew G. Knepley   ierr = PetscProblemGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
1476934789fcSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1477*0f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
1478*0f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1479d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1480d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1481d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1482d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1483d69c5d34SMatthew G. Knepley     PetscReal       *points;
1484d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1485d69c5d34SMatthew G. Knepley 
1486d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
1487d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
1488*0f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
1489d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1490d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1491d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1492d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1493d69c5d34SMatthew G. Knepley       npoints += Np;
1494d69c5d34SMatthew G. Knepley     }
1495d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1496d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1497d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1498d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1499d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1500d69c5d34SMatthew G. Knepley     }
1501d69c5d34SMatthew G. Knepley 
1502d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
1503*0f2d7e86SMatthew G. Knepley       PetscFE    fe;
1504d69c5d34SMatthew G. Knepley       PetscReal *B;
150536a6d9c0SMatthew G. Knepley       PetscInt   NcJ, cpdim, j;
1506d69c5d34SMatthew G. Knepley 
1507d69c5d34SMatthew G. Knepley       /* Evaluate basis at points */
1508*0f2d7e86SMatthew G. Knepley       ierr = PetscProblemGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr);
1509*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
151036a6d9c0SMatthew 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);
1511*0f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1512ffe73a53SMatthew G. Knepley       /* For now, fields only interpolate themselves */
1513ffe73a53SMatthew G. Knepley       if (fieldI == fieldJ) {
1514*0f2d7e86SMatthew G. Knepley         ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1515d69c5d34SMatthew G. Knepley         for (i = 0, k = 0; i < fpdim; ++i) {
1516d69c5d34SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1517d69c5d34SMatthew G. Knepley           ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1518d69c5d34SMatthew G. Knepley           for (p = 0; p < Np; ++p, ++k) {
151936a6d9c0SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
1520*0f2d7e86SMatthew 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];
152136a6d9c0SMatthew G. Knepley             }
1522d69c5d34SMatthew G. Knepley           }
1523d69c5d34SMatthew G. Knepley         }
1524*0f2d7e86SMatthew G. Knepley         ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1525ffe73a53SMatthew G. Knepley       }
152636a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1527d69c5d34SMatthew G. Knepley     }
1528d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1529549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1530d69c5d34SMatthew G. Knepley   }
1531*0f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
1532d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1533934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1534d69c5d34SMatthew G. Knepley   }
1535549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
1536d69c5d34SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
1537549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1538934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1539934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1540d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1541d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1542934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1543934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1544d69c5d34SMatthew G. Knepley   }
1545d69c5d34SMatthew G. Knepley #if 0
1546d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1547d69c5d34SMatthew G. Knepley #endif
1548d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1549d69c5d34SMatthew G. Knepley }
15506c73c22cSMatthew G. Knepley 
15516c73c22cSMatthew G. Knepley #undef __FUNCT__
15528e136ac0SMatthew G. Knepley #define __FUNCT__ "BoundaryDuplicate"
15538e136ac0SMatthew G. Knepley static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary)
15548e136ac0SMatthew G. Knepley {
15558e136ac0SMatthew G. Knepley   DMBoundary     b = bd, b2, bold = NULL;
15568e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
15578e136ac0SMatthew G. Knepley 
15588e136ac0SMatthew G. Knepley   PetscFunctionBegin;
15598e136ac0SMatthew G. Knepley   *boundary = NULL;
15608e136ac0SMatthew G. Knepley   for (; b; b = b->next, bold = b2) {
15618e136ac0SMatthew G. Knepley     ierr = PetscNew(&b2);CHKERRQ(ierr);
15628e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
15638e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
15648e136ac0SMatthew G. Knepley     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
15658e136ac0SMatthew G. Knepley     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
15668e136ac0SMatthew G. Knepley     b2->label     = NULL;
15678e136ac0SMatthew G. Knepley     b2->essential = b->essential;
15688e136ac0SMatthew G. Knepley     b2->field     = b->field;
15698e136ac0SMatthew G. Knepley     b2->func      = b->func;
15708e136ac0SMatthew G. Knepley     b2->numids    = b->numids;
15718e136ac0SMatthew G. Knepley     b2->ctx       = b->ctx;
15728e136ac0SMatthew G. Knepley     b2->next      = NULL;
15738e136ac0SMatthew G. Knepley     if (!*boundary) *boundary   = b2;
15748e136ac0SMatthew G. Knepley     if (bold)        bold->next = b2;
15758e136ac0SMatthew G. Knepley   }
15768e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
15778e136ac0SMatthew G. Knepley }
15788e136ac0SMatthew G. Knepley 
15798e136ac0SMatthew G. Knepley #undef __FUNCT__
15808e136ac0SMatthew G. Knepley #define __FUNCT__ "DMPlexCopyBoundary"
15818e136ac0SMatthew G. Knepley PetscErrorCode DMPlexCopyBoundary(DM dm, DM dmNew)
15828e136ac0SMatthew G. Knepley {
15838e136ac0SMatthew G. Knepley   DM_Plex       *mesh    = (DM_Plex *) dm->data;
15848e136ac0SMatthew G. Knepley   DM_Plex       *meshNew = (DM_Plex *) dmNew->data;
15858e136ac0SMatthew G. Knepley   DMBoundary     b;
15868e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
15878e136ac0SMatthew G. Knepley 
15888e136ac0SMatthew G. Knepley   PetscFunctionBegin;
15898e136ac0SMatthew G. Knepley   ierr = BoundaryDuplicate(mesh->boundary, &meshNew->boundary);CHKERRQ(ierr);
15908e136ac0SMatthew G. Knepley   for (b = meshNew->boundary; b; b = b->next) {
15918e136ac0SMatthew G. Knepley     if (b->labelname) {
15928e136ac0SMatthew G. Knepley       ierr = DMPlexGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
15938e136ac0SMatthew G. Knepley       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
15948e136ac0SMatthew G. Knepley     }
15958e136ac0SMatthew G. Knepley   }
15968e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
15978e136ac0SMatthew G. Knepley }
15988e136ac0SMatthew G. Knepley 
15998e136ac0SMatthew G. Knepley #undef __FUNCT__
16006c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexAddBoundary"
16016c73c22cSMatthew G. Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */
160263d5297fSMatthew 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)
16036c73c22cSMatthew G. Knepley {
16046c73c22cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
16056c73c22cSMatthew G. Knepley   DMBoundary     b;
16066c73c22cSMatthew G. Knepley   PetscErrorCode ierr;
16076c73c22cSMatthew G. Knepley 
16086c73c22cSMatthew G. Knepley   PetscFunctionBegin;
160963d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16106c73c22cSMatthew G. Knepley   ierr = PetscNew(&b);CHKERRQ(ierr);
16116c73c22cSMatthew G. Knepley   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
161263d5297fSMatthew G. Knepley   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
16136c73c22cSMatthew G. Knepley   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
16146c73c22cSMatthew G. Knepley   ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);
161563d5297fSMatthew G. Knepley   if (b->labelname) {
161663d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
161763d5297fSMatthew G. Knepley     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
161863d5297fSMatthew G. Knepley   }
16196c73c22cSMatthew G. Knepley   b->essential   = isEssential;
16206c73c22cSMatthew G. Knepley   b->field       = field;
16216c73c22cSMatthew G. Knepley   b->func        = bcFunc;
16226c73c22cSMatthew G. Knepley   b->numids      = numids;
16236c73c22cSMatthew G. Knepley   b->ctx         = ctx;
16246c73c22cSMatthew G. Knepley   b->next        = mesh->boundary;
16256c73c22cSMatthew G. Knepley   mesh->boundary = b;
16266c73c22cSMatthew G. Knepley   PetscFunctionReturn(0);
16276c73c22cSMatthew G. Knepley }
16286c73c22cSMatthew G. Knepley 
16296c73c22cSMatthew G. Knepley #undef __FUNCT__
16306c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumBoundary"
16316c73c22cSMatthew G. Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd)
16326c73c22cSMatthew G. Knepley {
16336c73c22cSMatthew G. Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
16346c73c22cSMatthew G. Knepley   DMBoundary b    = mesh->boundary;
16356c73c22cSMatthew G. Knepley 
16366c73c22cSMatthew G. Knepley   PetscFunctionBegin;
163763d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
163863d5297fSMatthew G. Knepley   PetscValidPointer(numBd, 2);
16396c73c22cSMatthew G. Knepley   *numBd = 0;
16406c73c22cSMatthew G. Knepley   while (b) {++(*numBd); b = b->next;}
16416c73c22cSMatthew G. Knepley   PetscFunctionReturn(0);
16426c73c22cSMatthew G. Knepley }
16436c73c22cSMatthew G. Knepley 
16446c73c22cSMatthew G. Knepley #undef __FUNCT__
16456c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetBoundary"
164663d5297fSMatthew 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)
16476c73c22cSMatthew G. Knepley {
16486c73c22cSMatthew G. Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
16496c73c22cSMatthew G. Knepley   DMBoundary b    = mesh->boundary;
16506c73c22cSMatthew G. Knepley   PetscInt   n    = 0;
16516c73c22cSMatthew G. Knepley 
16526c73c22cSMatthew G. Knepley   PetscFunctionBegin;
165363d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16546c73c22cSMatthew G. Knepley   while (b) {
16556c73c22cSMatthew G. Knepley     if (n == bd) break;
16566c73c22cSMatthew G. Knepley     b = b->next;
16576c73c22cSMatthew G. Knepley     ++n;
16586c73c22cSMatthew G. Knepley   }
16596c73c22cSMatthew G. Knepley   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
16606c73c22cSMatthew G. Knepley   if (isEssential) {
16616c73c22cSMatthew G. Knepley     PetscValidPointer(isEssential, 3);
16626c73c22cSMatthew G. Knepley     *isEssential = b->essential;
16636c73c22cSMatthew G. Knepley   }
16646c73c22cSMatthew G. Knepley   if (name) {
16656c73c22cSMatthew G. Knepley     PetscValidPointer(name, 4);
16666c73c22cSMatthew G. Knepley     *name = b->name;
16676c73c22cSMatthew G. Knepley   }
166863d5297fSMatthew G. Knepley   if (labelname) {
166963d5297fSMatthew G. Knepley     PetscValidPointer(labelname, 5);
167063d5297fSMatthew G. Knepley     *labelname = b->labelname;
167163d5297fSMatthew G. Knepley   }
16726c73c22cSMatthew G. Knepley   if (field) {
167363d5297fSMatthew G. Knepley     PetscValidPointer(field, 6);
16746c73c22cSMatthew G. Knepley     *field = b->field;
16756c73c22cSMatthew G. Knepley   }
16766c73c22cSMatthew G. Knepley   if (func) {
167763d5297fSMatthew G. Knepley     PetscValidPointer(func, 7);
16786c73c22cSMatthew G. Knepley     *func = b->func;
16796c73c22cSMatthew G. Knepley   }
16806c73c22cSMatthew G. Knepley   if (numids) {
168163d5297fSMatthew G. Knepley     PetscValidPointer(numids, 8);
16826c73c22cSMatthew G. Knepley     *numids = b->numids;
16836c73c22cSMatthew G. Knepley   }
16846c73c22cSMatthew G. Knepley   if (ids) {
168563d5297fSMatthew G. Knepley     PetscValidPointer(ids, 9);
16866c73c22cSMatthew G. Knepley     *ids = b->ids;
16876c73c22cSMatthew G. Knepley   }
16886c73c22cSMatthew G. Knepley   if (ctx) {
168963d5297fSMatthew G. Knepley     PetscValidPointer(ctx, 10);
16906c73c22cSMatthew G. Knepley     *ctx = b->ctx;
16916c73c22cSMatthew G. Knepley   }
16926c73c22cSMatthew G. Knepley   PetscFunctionReturn(0);
16936c73c22cSMatthew G. Knepley }
16940225b034SMatthew G. Knepley 
16950225b034SMatthew G. Knepley #undef __FUNCT__
16960225b034SMatthew G. Knepley #define __FUNCT__ "DMPlexIsBoundaryPoint"
16970225b034SMatthew G. Knepley PetscErrorCode DMPlexIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
16980225b034SMatthew G. Knepley {
16990225b034SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
17000225b034SMatthew G. Knepley   DMBoundary     b    = mesh->boundary;
17010225b034SMatthew G. Knepley   PetscErrorCode ierr;
17020225b034SMatthew G. Knepley 
17030225b034SMatthew G. Knepley   PetscFunctionBegin;
17040225b034SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17050225b034SMatthew G. Knepley   PetscValidPointer(isBd, 3);
17060225b034SMatthew G. Knepley   *isBd = PETSC_FALSE;
17070225b034SMatthew G. Knepley   while (b && !(*isBd)) {
17080225b034SMatthew G. Knepley     if (b->label) {
17090225b034SMatthew G. Knepley       PetscInt i;
17100225b034SMatthew G. Knepley 
17110225b034SMatthew G. Knepley       for (i = 0; i < b->numids && !(*isBd); ++i) {
17120225b034SMatthew G. Knepley         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
17130225b034SMatthew G. Knepley       }
17140225b034SMatthew G. Knepley     }
17150225b034SMatthew G. Knepley     b = b->next;
17160225b034SMatthew G. Knepley   }
17170225b034SMatthew G. Knepley   PetscFunctionReturn(0);
17180225b034SMatthew G. Knepley }
1719