xref: /petsc/src/dm/impls/plex/plexfem.c (revision 154967224734f0c541f45060bb3cc484689b19ac)
1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2cb1e1211SMatthew G Knepley 
30f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h>
4*15496722SMatthew G. Knepley #include <petsc-private/petscfvimpl.h>
5a0845e3aSMatthew G. Knepley 
6cb1e1211SMatthew G Knepley #undef __FUNCT__
7cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale"
8cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
9cb1e1211SMatthew G Knepley {
10cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
11cb1e1211SMatthew G Knepley 
12cb1e1211SMatthew G Knepley   PetscFunctionBegin;
13cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
15cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
16cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
17cb1e1211SMatthew G Knepley }
18cb1e1211SMatthew G Knepley 
19cb1e1211SMatthew G Knepley #undef __FUNCT__
20cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale"
21cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
22cb1e1211SMatthew G Knepley {
23cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
24cb1e1211SMatthew G Knepley 
25cb1e1211SMatthew G Knepley   PetscFunctionBegin;
26cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
28cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
29cb1e1211SMatthew G Knepley }
30cb1e1211SMatthew G Knepley 
31cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k)
32cb1e1211SMatthew G Knepley {
33cb1e1211SMatthew G Knepley   switch (i) {
34cb1e1211SMatthew G Knepley   case 0:
35cb1e1211SMatthew G Knepley     switch (j) {
36cb1e1211SMatthew G Knepley     case 0: return 0;
37cb1e1211SMatthew G Knepley     case 1:
38cb1e1211SMatthew G Knepley       switch (k) {
39cb1e1211SMatthew G Knepley       case 0: return 0;
40cb1e1211SMatthew G Knepley       case 1: return 0;
41cb1e1211SMatthew G Knepley       case 2: return 1;
42cb1e1211SMatthew G Knepley       }
43cb1e1211SMatthew G Knepley     case 2:
44cb1e1211SMatthew G Knepley       switch (k) {
45cb1e1211SMatthew G Knepley       case 0: return 0;
46cb1e1211SMatthew G Knepley       case 1: return -1;
47cb1e1211SMatthew G Knepley       case 2: return 0;
48cb1e1211SMatthew G Knepley       }
49cb1e1211SMatthew G Knepley     }
50cb1e1211SMatthew G Knepley   case 1:
51cb1e1211SMatthew G Knepley     switch (j) {
52cb1e1211SMatthew G Knepley     case 0:
53cb1e1211SMatthew G Knepley       switch (k) {
54cb1e1211SMatthew G Knepley       case 0: return 0;
55cb1e1211SMatthew G Knepley       case 1: return 0;
56cb1e1211SMatthew G Knepley       case 2: return -1;
57cb1e1211SMatthew G Knepley       }
58cb1e1211SMatthew G Knepley     case 1: return 0;
59cb1e1211SMatthew G Knepley     case 2:
60cb1e1211SMatthew G Knepley       switch (k) {
61cb1e1211SMatthew G Knepley       case 0: return 1;
62cb1e1211SMatthew G Knepley       case 1: return 0;
63cb1e1211SMatthew G Knepley       case 2: return 0;
64cb1e1211SMatthew G Knepley       }
65cb1e1211SMatthew G Knepley     }
66cb1e1211SMatthew G Knepley   case 2:
67cb1e1211SMatthew G Knepley     switch (j) {
68cb1e1211SMatthew G Knepley     case 0:
69cb1e1211SMatthew G Knepley       switch (k) {
70cb1e1211SMatthew G Knepley       case 0: return 0;
71cb1e1211SMatthew G Knepley       case 1: return 1;
72cb1e1211SMatthew G Knepley       case 2: return 0;
73cb1e1211SMatthew G Knepley       }
74cb1e1211SMatthew G Knepley     case 1:
75cb1e1211SMatthew G Knepley       switch (k) {
76cb1e1211SMatthew G Knepley       case 0: return -1;
77cb1e1211SMatthew G Knepley       case 1: return 0;
78cb1e1211SMatthew G Knepley       case 2: return 0;
79cb1e1211SMatthew G Knepley       }
80cb1e1211SMatthew G Knepley     case 2: return 0;
81cb1e1211SMatthew G Knepley     }
82cb1e1211SMatthew G Knepley   }
83cb1e1211SMatthew G Knepley   return 0;
84cb1e1211SMatthew G Knepley }
85cb1e1211SMatthew G Knepley 
86cb1e1211SMatthew G Knepley #undef __FUNCT__
87cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody"
88cb1e1211SMatthew G Knepley /*@C
89cb1e1211SMatthew G Knepley   DMPlexCreateRigidBody - create rigid body modes from coordinates
90cb1e1211SMatthew G Knepley 
91cb1e1211SMatthew G Knepley   Collective on DM
92cb1e1211SMatthew G Knepley 
93cb1e1211SMatthew G Knepley   Input Arguments:
94cb1e1211SMatthew G Knepley + dm - the DM
95cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section
96cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section
97cb1e1211SMatthew G Knepley 
98cb1e1211SMatthew G Knepley   Output Argument:
99cb1e1211SMatthew G Knepley . sp - the null space
100cb1e1211SMatthew G Knepley 
101cb1e1211SMatthew G Knepley   Note: This is necessary to take account of Dirichlet conditions on the displacements
102cb1e1211SMatthew G Knepley 
103cb1e1211SMatthew G Knepley   Level: advanced
104cb1e1211SMatthew G Knepley 
105cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate()
106cb1e1211SMatthew G Knepley @*/
107cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp)
108cb1e1211SMatthew G Knepley {
109cb1e1211SMatthew G Knepley   MPI_Comm       comm;
110cb1e1211SMatthew G Knepley   Vec            coordinates, localMode, mode[6];
111cb1e1211SMatthew G Knepley   PetscSection   coordSection;
112cb1e1211SMatthew G Knepley   PetscScalar   *coords;
113cb1e1211SMatthew G Knepley   PetscInt       dim, vStart, vEnd, v, n, m, d, i, j;
114cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
115cb1e1211SMatthew G Knepley 
116cb1e1211SMatthew G Knepley   PetscFunctionBegin;
117cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
118c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
119cb1e1211SMatthew G Knepley   if (dim == 1) {
120cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
121cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
122cb1e1211SMatthew G Knepley   }
123cb1e1211SMatthew G Knepley   if (!section)       {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
124cb1e1211SMatthew G Knepley   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
125cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
126cb1e1211SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
12769d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
128cb1e1211SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
129cb1e1211SMatthew G Knepley   m    = (dim*(dim+1))/2;
130cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
131cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
132cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
133cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
134cb1e1211SMatthew G Knepley   /* Assume P1 */
135cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr);
136cb1e1211SMatthew G Knepley   for (d = 0; d < dim; ++d) {
137cb1e1211SMatthew G Knepley     PetscScalar values[3] = {0.0, 0.0, 0.0};
138cb1e1211SMatthew G Knepley 
139cb1e1211SMatthew G Knepley     values[d] = 1.0;
140cb1e1211SMatthew G Knepley     ierr      = VecSet(localMode, 0.0);CHKERRQ(ierr);
141cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
142cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
143cb1e1211SMatthew G Knepley     }
144cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
145cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
146cb1e1211SMatthew G Knepley   }
147cb1e1211SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
148cb1e1211SMatthew G Knepley   for (d = dim; d < dim*(dim+1)/2; ++d) {
149cb1e1211SMatthew G Knepley     PetscInt i, j, k = dim > 2 ? d - dim : d;
150cb1e1211SMatthew G Knepley 
151cb1e1211SMatthew G Knepley     ierr = VecSet(localMode, 0.0);CHKERRQ(ierr);
152cb1e1211SMatthew G Knepley     for (v = vStart; v < vEnd; ++v) {
153cb1e1211SMatthew G Knepley       PetscScalar values[3] = {0.0, 0.0, 0.0};
154cb1e1211SMatthew G Knepley       PetscInt    off;
155cb1e1211SMatthew G Knepley 
156cb1e1211SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
157cb1e1211SMatthew G Knepley       for (i = 0; i < dim; ++i) {
158cb1e1211SMatthew G Knepley         for (j = 0; j < dim; ++j) {
159cb1e1211SMatthew G Knepley           values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]);
160cb1e1211SMatthew G Knepley         }
161cb1e1211SMatthew G Knepley       }
162cb1e1211SMatthew G Knepley       ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr);
163cb1e1211SMatthew G Knepley     }
164cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
165cb1e1211SMatthew G Knepley     ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
166cb1e1211SMatthew G Knepley   }
167cb1e1211SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
168cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr);
169cb1e1211SMatthew G Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
170cb1e1211SMatthew G Knepley   /* Orthonormalize system */
171cb1e1211SMatthew G Knepley   for (i = dim; i < m; ++i) {
172cb1e1211SMatthew G Knepley     PetscScalar dots[6];
173cb1e1211SMatthew G Knepley 
174cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
175cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
176cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
177cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
178cb1e1211SMatthew G Knepley   }
179cb1e1211SMatthew G Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
180cb1e1211SMatthew G Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
181cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
182cb1e1211SMatthew G Knepley }
183cb1e1211SMatthew G Knepley 
184cb1e1211SMatthew G Knepley #undef __FUNCT__
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;
197c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(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;
2268e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, 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"
2560f2d7e86SMatthew 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;
259*15496722SMatthew G. Knepley   PetscInt       *numComp;
26072f94c41SMatthew G. Knepley   PetscSection    section;
26172f94c41SMatthew G. Knepley   PetscScalar    *values;
26272f94c41SMatthew G. Knepley   PetscReal      *v0, *J, detJ;
263*15496722SMatthew G. Knepley   PetscInt        numFields, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp;
264cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
265cb1e1211SMatthew G Knepley 
266cb1e1211SMatthew G Knepley   PetscFunctionBegin;
267cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
26872f94c41SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
269*15496722SMatthew G. Knepley   ierr = PetscMalloc2(numFields, &sp, numFields, &numComp);CHKERRQ(ierr);
27072f94c41SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
271*15496722SMatthew G. Knepley     PetscObject  obj;
272*15496722SMatthew G. Knepley     PetscClassId id;
2730f2d7e86SMatthew G. Knepley 
274*15496722SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
275*15496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
276*15496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
277*15496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
278*15496722SMatthew G. Knepley 
279*15496722SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
2800f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
281*15496722SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr);
282*15496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
283*15496722SMatthew G. Knepley       PetscFV         fv = (PetscFV) obj;
284*15496722SMatthew G. Knepley       PetscQuadrature q;
285*15496722SMatthew G. Knepley 
286*15496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
287*15496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &q);CHKERRQ(ierr);
288*15496722SMatthew G. Knepley       ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject) fv), &sp[f]);CHKERRQ(ierr);
289*15496722SMatthew G. Knepley       ierr = PetscDualSpaceSetDM(sp[f], dm);CHKERRQ(ierr);
290*15496722SMatthew G. Knepley       ierr = PetscDualSpaceSetType(sp[f], PETSCDUALSPACESIMPLE);CHKERRQ(ierr);
291*15496722SMatthew G. Knepley       ierr = PetscDualSpaceSimpleSetDimension(sp[f], 1);CHKERRQ(ierr);
292*15496722SMatthew G. Knepley       ierr = PetscDualSpaceSimpleSetFunctional(sp[f], 0, q);CHKERRQ(ierr);
293*15496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
29472f94c41SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
295*15496722SMatthew G. Knepley     totDim += spDim*numComp[f];
296cb1e1211SMatthew G Knepley   }
297c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
29872f94c41SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
29972f94c41SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
30072f94c41SMatthew 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);
30172f94c41SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
302dcca6d9dSJed Brown   ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr);
30372f94c41SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
30472f94c41SMatthew G. Knepley     PetscCellGeometry geom;
305cb1e1211SMatthew G Knepley 
3068e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, NULL, &detJ);CHKERRQ(ierr);
30772f94c41SMatthew G. Knepley     geom.v0   = v0;
30872f94c41SMatthew G. Knepley     geom.J    = J;
30972f94c41SMatthew G. Knepley     geom.detJ = &detJ;
31072f94c41SMatthew G. Knepley     for (f = 0, v = 0; f < numFields; ++f) {
311c110b1eeSGeoffrey Irving       void *const ctx = ctxs ? ctxs[f] : NULL;
3120f2d7e86SMatthew G. Knepley 
31372f94c41SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
31472f94c41SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
315120386c5SMatthew G. Knepley         if (funcs[f]) {
316*15496722SMatthew G. Knepley           ierr = PetscDualSpaceApply(sp[f], d, geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr);
317120386c5SMatthew G. Knepley         } else {
318*15496722SMatthew G. Knepley           for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
319120386c5SMatthew G. Knepley         }
320*15496722SMatthew G. Knepley         v += numComp[f];
321cb1e1211SMatthew G Knepley       }
322cb1e1211SMatthew G Knepley     }
32372f94c41SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
324cb1e1211SMatthew G Knepley   }
32572f94c41SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
3261f2da991SMatthew G. Knepley   ierr = PetscFree2(v0,J);CHKERRQ(ierr);
327*15496722SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);}
328*15496722SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
329cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
330cb1e1211SMatthew G Knepley }
331cb1e1211SMatthew G Knepley 
332cb1e1211SMatthew G Knepley #undef __FUNCT__
333cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction"
334cb1e1211SMatthew G Knepley /*@C
335cb1e1211SMatthew G Knepley   DMPlexProjectFunction - This projects the given function into the function space provided.
336cb1e1211SMatthew G Knepley 
337cb1e1211SMatthew G Knepley   Input Parameters:
338cb1e1211SMatthew G Knepley + dm      - The DM
33972f94c41SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
340c110b1eeSGeoffrey Irving . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
341cb1e1211SMatthew G Knepley - mode    - The insertion mode for values
342cb1e1211SMatthew G Knepley 
343cb1e1211SMatthew G Knepley   Output Parameter:
344cb1e1211SMatthew G Knepley . X - vector
345cb1e1211SMatthew G Knepley 
346cb1e1211SMatthew G Knepley   Level: developer
347cb1e1211SMatthew G Knepley 
348878cb397SSatish Balay .seealso: DMPlexComputeL2Diff()
349878cb397SSatish Balay @*/
3500f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
351cb1e1211SMatthew G Knepley {
352cb1e1211SMatthew G Knepley   Vec            localX;
353cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
354cb1e1211SMatthew G Knepley 
355cb1e1211SMatthew G Knepley   PetscFunctionBegin;
3569a800dd8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
357cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
3580f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr);
359cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
360cb1e1211SMatthew G Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
361cb1e1211SMatthew G Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
362cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
363cb1e1211SMatthew G Knepley }
364cb1e1211SMatthew G Knepley 
36555f2e967SMatthew G. Knepley #undef __FUNCT__
3660f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal"
3673bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX)
3680f2d7e86SMatthew G. Knepley {
3690f2d7e86SMatthew G. Knepley   DM                dmAux;
3702764a2aaSMatthew G. Knepley   PetscDS           prob, probAux;
3710f2d7e86SMatthew G. Knepley   Vec               A;
372326413afSMatthew G. Knepley   PetscSection      section, sectionAux;
373326413afSMatthew G. Knepley   PetscScalar      *values, *u, *u_x, *a, *a_x;
3740f2d7e86SMatthew G. Knepley   PetscReal        *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux;
375326413afSMatthew G. Knepley   PetscInt          Nf, dim, spDim, totDim, numValues, cStart, cEnd, c, f, d, v, comp;
3760f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
3770f2d7e86SMatthew G. Knepley 
3780f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
3792764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
380c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3810f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
3820f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
3830f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
3842764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3852764a2aaSMatthew G. Knepley   ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr);
3862764a2aaSMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
3872764a2aaSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
3880f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3890f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3900f2d7e86SMatthew G. Knepley   if (dmAux) {
3912764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
392326413afSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
3932764a2aaSMatthew G. Knepley     ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr);
3942764a2aaSMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
3950f2d7e86SMatthew G. Knepley   }
3960f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localU);CHKERRQ(ierr);
3970f2d7e86SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
3980f2d7e86SMatthew 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);
3990f2d7e86SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4000f2d7e86SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
4010f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
402326413afSMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
403326413afSMatthew G. Knepley 
4048e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
405326413afSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
406326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
4070f2d7e86SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
4083113607cSMatthew G. Knepley       PetscFE        fe;
4093113607cSMatthew G. Knepley       PetscDualSpace sp;
4103113607cSMatthew G. Knepley       PetscInt       Ncf;
4113113607cSMatthew G. Knepley 
4122764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
4133113607cSMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr);
4143113607cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr);
4153113607cSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr);
4160f2d7e86SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
4170f2d7e86SMatthew G. Knepley         PetscQuadrature  quad;
4180f2d7e86SMatthew G. Knepley         const PetscReal *points, *weights;
4190f2d7e86SMatthew G. Knepley         PetscInt         numPoints, q;
4200f2d7e86SMatthew G. Knepley 
4210f2d7e86SMatthew G. Knepley         if (funcs[f]) {
4223113607cSMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr);
4230f2d7e86SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
4243113607cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
4250f2d7e86SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
4260f2d7e86SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
427326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
428326413afSMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
4293bc3b0a0SMatthew G. Knepley             (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]);
4300f2d7e86SMatthew G. Knepley           }
4313113607cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr);
4320f2d7e86SMatthew G. Knepley         } else {
4330f2d7e86SMatthew G. Knepley           for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0;
4340f2d7e86SMatthew G. Knepley         }
4350f2d7e86SMatthew G. Knepley         v += Ncf;
4360f2d7e86SMatthew G. Knepley       }
4370f2d7e86SMatthew G. Knepley     }
438326413afSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
439326413afSMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
4400f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
4410f2d7e86SMatthew G. Knepley   }
4420f2d7e86SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
4430f2d7e86SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
4440f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
4450f2d7e86SMatthew G. Knepley }
4460f2d7e86SMatthew G. Knepley 
4470f2d7e86SMatthew G. Knepley #undef __FUNCT__
4480f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectField"
4490f2d7e86SMatthew G. Knepley /*@C
4500f2d7e86SMatthew G. Knepley   DMPlexProjectField - This projects the given function of the fields into the function space provided.
4510f2d7e86SMatthew G. Knepley 
4520f2d7e86SMatthew G. Knepley   Input Parameters:
4530f2d7e86SMatthew G. Knepley + dm      - The DM
4540f2d7e86SMatthew G. Knepley . U       - The input field vector
4550f2d7e86SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
4560f2d7e86SMatthew G. Knepley - mode    - The insertion mode for values
4570f2d7e86SMatthew G. Knepley 
4580f2d7e86SMatthew G. Knepley   Output Parameter:
4590f2d7e86SMatthew G. Knepley . X       - The output vector
4600f2d7e86SMatthew G. Knepley 
4610f2d7e86SMatthew G. Knepley   Level: developer
4620f2d7e86SMatthew G. Knepley 
4630f2d7e86SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
4640f2d7e86SMatthew G. Knepley @*/
4653bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectField(DM dm, Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X)
4660f2d7e86SMatthew G. Knepley {
4670f2d7e86SMatthew G. Knepley   Vec            localX, localU;
4680f2d7e86SMatthew G. Knepley   PetscErrorCode ierr;
4690f2d7e86SMatthew G. Knepley 
4700f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
4710f2d7e86SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4720f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
4730f2d7e86SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr);
4740f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
4750f2d7e86SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
4763113607cSMatthew G. Knepley   ierr = DMPlexProjectFieldLocal(dm, localU, funcs, mode, localX);CHKERRQ(ierr);
4770f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
4780f2d7e86SMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
4790f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
4800f2d7e86SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr);
4810f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
4820f2d7e86SMatthew G. Knepley }
4830f2d7e86SMatthew G. Knepley 
4840f2d7e86SMatthew G. Knepley #undef __FUNCT__
4853351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM"
4863351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX)
48755f2e967SMatthew G. Knepley {
48855f2e967SMatthew G. Knepley   void        (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx);
48955f2e967SMatthew G. Knepley   void         **ctxs;
49055f2e967SMatthew G. Knepley   PetscFE       *fe;
49155f2e967SMatthew G. Knepley   PetscInt       numFields, f, numBd, b;
49255f2e967SMatthew G. Knepley   PetscErrorCode ierr;
49355f2e967SMatthew G. Knepley 
49455f2e967SMatthew G. Knepley   PetscFunctionBegin;
49555f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49655f2e967SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 2);
49755f2e967SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
49855f2e967SMatthew G. Knepley   ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
49955f2e967SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);}
50055f2e967SMatthew G. Knepley   /* OPT: Could attempt to do multiple BCs at once */
50155f2e967SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
50255f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
503a18a7fb9SMatthew G. Knepley     DMLabel         label;
50455f2e967SMatthew G. Knepley     const PetscInt *ids;
50563d5297fSMatthew G. Knepley     const char     *labelname;
50655f2e967SMatthew G. Knepley     PetscInt        numids, field;
50755f2e967SMatthew G. Knepley     PetscBool       isEssential;
50855f2e967SMatthew G. Knepley     void          (*func)();
50955f2e967SMatthew G. Knepley     void           *ctx;
51055f2e967SMatthew G. Knepley 
51163d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
51263d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);
51355f2e967SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
51455f2e967SMatthew G. Knepley       funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL;
51555f2e967SMatthew G. Knepley       ctxs[f]  = field == f ? ctx : NULL;
51655f2e967SMatthew G. Knepley     }
517a18a7fb9SMatthew G. Knepley     ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
51855f2e967SMatthew G. Knepley   }
51955f2e967SMatthew G. Knepley   ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr);
52055f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
52155f2e967SMatthew G. Knepley }
52255f2e967SMatthew G. Knepley 
523cb1e1211SMatthew G Knepley #undef __FUNCT__
524cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff"
525cb1e1211SMatthew G Knepley /*@C
526cb1e1211SMatthew G Knepley   DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
527cb1e1211SMatthew G Knepley 
528cb1e1211SMatthew G Knepley   Input Parameters:
529cb1e1211SMatthew G Knepley + dm    - The DM
530cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component
53151259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
532cb1e1211SMatthew G Knepley - X     - The coefficient vector u_h
533cb1e1211SMatthew G Knepley 
534cb1e1211SMatthew G Knepley   Output Parameter:
535cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2
536cb1e1211SMatthew G Knepley 
537cb1e1211SMatthew G Knepley   Level: developer
538cb1e1211SMatthew G Knepley 
539*15496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff()
540878cb397SSatish Balay @*/
5410f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
542cb1e1211SMatthew G Knepley {
543cb1e1211SMatthew G Knepley   const PetscInt   debug = 0;
544cb1e1211SMatthew G Knepley   PetscSection     section;
545c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
546cb1e1211SMatthew G Knepley   Vec              localX;
547*15496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
548cb1e1211SMatthew G Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
549cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
550*15496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
551*15496722SMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, c, field, fieldOffset;
552cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
553cb1e1211SMatthew G Knepley 
554cb1e1211SMatthew G Knepley   PetscFunctionBegin;
555c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
556cb1e1211SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
557cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
558cb1e1211SMatthew G Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
559cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
560cb1e1211SMatthew G Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
561cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
562*15496722SMatthew G. Knepley     PetscObject  obj;
563*15496722SMatthew G. Knepley     PetscClassId id;
564c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
565c5bbbd5bSMatthew G. Knepley 
566*15496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
567*15496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
568*15496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
569*15496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
570*15496722SMatthew G. Knepley 
5710f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
5720f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
573*15496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
574*15496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
575*15496722SMatthew G. Knepley 
576*15496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
577*15496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
578*15496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
579c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
580cb1e1211SMatthew G Knepley   }
581*15496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
5820f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
583*15496722SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
584cb1e1211SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
585cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
586a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
587cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
588cb1e1211SMatthew G Knepley 
5898e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
590cb1e1211SMatthew G Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
591cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
592cb1e1211SMatthew G Knepley 
593*15496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
594*15496722SMatthew G. Knepley       PetscObject  obj;
595*15496722SMatthew G. Knepley       PetscClassId id;
596c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
597*15496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
598cb1e1211SMatthew G Knepley 
599*15496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
600*15496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
601*15496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
602*15496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
603*15496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
604cb1e1211SMatthew G Knepley       if (debug) {
605cb1e1211SMatthew G Knepley         char title[1024];
606cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
607*15496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
608cb1e1211SMatthew G Knepley       }
609cb1e1211SMatthew G Knepley       for (q = 0; q < numQuadPoints; ++q) {
610*15496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
611c110b1eeSGeoffrey Irving         (*funcs[field])(coords, funcVal, ctx);
612*15496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
613*15496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
614*15496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
615*15496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
616*15496722SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
617*15496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
618cb1e1211SMatthew G Knepley         }
619cb1e1211SMatthew G Knepley       }
620*15496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
621cb1e1211SMatthew G Knepley     }
622cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
623cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
624cb1e1211SMatthew G Knepley     localDiff += elemDiff;
625cb1e1211SMatthew G Knepley   }
626*15496722SMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
627cb1e1211SMatthew G Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62886a74ee0SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
629cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
630cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
631cb1e1211SMatthew G Knepley }
632cb1e1211SMatthew G Knepley 
633cb1e1211SMatthew G Knepley #undef __FUNCT__
63440e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff"
63540e14135SMatthew G. Knepley /*@C
63640e14135SMatthew 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.
63740e14135SMatthew G. Knepley 
63840e14135SMatthew G. Knepley   Input Parameters:
63940e14135SMatthew G. Knepley + dm    - The DM
64040e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component
64151259fa3SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
64240e14135SMatthew G. Knepley . X     - The coefficient vector u_h
64340e14135SMatthew G. Knepley - n     - The vector to project along
64440e14135SMatthew G. Knepley 
64540e14135SMatthew G. Knepley   Output Parameter:
64640e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2
64740e14135SMatthew G. Knepley 
64840e14135SMatthew G. Knepley   Level: developer
64940e14135SMatthew G. Knepley 
65040e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
65140e14135SMatthew G. Knepley @*/
6520f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
653cb1e1211SMatthew G Knepley {
65440e14135SMatthew G. Knepley   const PetscInt  debug = 0;
655cb1e1211SMatthew G Knepley   PetscSection    section;
65640e14135SMatthew G. Knepley   PetscQuadrature quad;
65740e14135SMatthew G. Knepley   Vec             localX;
65840e14135SMatthew G. Knepley   PetscScalar    *funcVal, *interpolantVec;
65940e14135SMatthew G. Knepley   PetscReal      *coords, *realSpaceDer, *v0, *J, *invJ, detJ;
66040e14135SMatthew G. Knepley   PetscReal       localDiff = 0.0;
66140e14135SMatthew G. Knepley   PetscInt        dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp;
662cb1e1211SMatthew G Knepley   PetscErrorCode  ierr;
663cb1e1211SMatthew G Knepley 
664cb1e1211SMatthew G Knepley   PetscFunctionBegin;
665c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
66640e14135SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
66740e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
66840e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
66940e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
67040e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
671652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
6720f2d7e86SMatthew G. Knepley     PetscFE  fe;
67340e14135SMatthew G. Knepley     PetscInt Nc;
674652b88e8SMatthew G. Knepley 
6750f2d7e86SMatthew G. Knepley     ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
6760f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
6770f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
67840e14135SMatthew G. Knepley     numComponents += Nc;
679652b88e8SMatthew G. Knepley   }
68040e14135SMatthew G. Knepley   /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
68140e14135SMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr);
68240e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
68340e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
68440e14135SMatthew G. Knepley     PetscScalar *x = NULL;
68540e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
686652b88e8SMatthew G. Knepley 
6878e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
68840e14135SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
68940e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
69040e14135SMatthew G. Knepley 
69140e14135SMatthew G. Knepley     for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) {
6920f2d7e86SMatthew G. Knepley       PetscFE          fe;
69351259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
69421454ff5SMatthew G. Knepley       const PetscReal *quadPoints, *quadWeights;
69540e14135SMatthew G. Knepley       PetscReal       *basisDer;
69621454ff5SMatthew G. Knepley       PetscInt         numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g;
69740e14135SMatthew G. Knepley 
6980f2d7e86SMatthew G. Knepley       ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr);
69921454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
7000f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
7010f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr);
7020f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
70340e14135SMatthew G. Knepley       if (debug) {
70440e14135SMatthew G. Knepley         char title[1024];
70540e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
70640e14135SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr);
707652b88e8SMatthew G. Knepley       }
70840e14135SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
70940e14135SMatthew G. Knepley         for (d = 0; d < dim; d++) {
71040e14135SMatthew G. Knepley           coords[d] = v0[d];
71140e14135SMatthew G. Knepley           for (e = 0; e < dim; e++) {
71240e14135SMatthew G. Knepley             coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0);
713652b88e8SMatthew G. Knepley           }
71440e14135SMatthew G. Knepley         }
71551259fa3SMatthew G. Knepley         (*funcs[field])(coords, n, funcVal, ctx);
71640e14135SMatthew G. Knepley         for (fc = 0; fc < Ncomp; ++fc) {
71740e14135SMatthew G. Knepley           PetscScalar interpolant = 0.0;
71840e14135SMatthew G. Knepley 
71940e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0;
72040e14135SMatthew G. Knepley           for (f = 0; f < Nb; ++f) {
72140e14135SMatthew G. Knepley             const PetscInt fidx = f*Ncomp+fc;
72240e14135SMatthew G. Knepley 
72340e14135SMatthew G. Knepley             for (d = 0; d < dim; ++d) {
72440e14135SMatthew G. Knepley               realSpaceDer[d] = 0.0;
72540e14135SMatthew G. Knepley               for (g = 0; g < dim; ++g) {
72640e14135SMatthew G. Knepley                 realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g];
72740e14135SMatthew G. Knepley               }
72840e14135SMatthew G. Knepley               interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d];
72940e14135SMatthew G. Knepley             }
73040e14135SMatthew G. Knepley           }
73140e14135SMatthew G. Knepley           for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d];
73240e14135SMatthew 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);}
73340e14135SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ;
73440e14135SMatthew G. Knepley         }
73540e14135SMatthew G. Knepley       }
73640e14135SMatthew G. Knepley       comp        += Ncomp;
73740e14135SMatthew G. Knepley       fieldOffset += Nb*Ncomp;
73840e14135SMatthew G. Knepley     }
73940e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
74040e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
74140e14135SMatthew G. Knepley     localDiff += elemDiff;
74240e14135SMatthew G. Knepley   }
74340e14135SMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr);
74440e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
74540e14135SMatthew G. Knepley   ierr  = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
74640e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
747cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
748cb1e1211SMatthew G Knepley }
749cb1e1211SMatthew G Knepley 
750a0845e3aSMatthew G. Knepley #undef __FUNCT__
75173d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff"
752*15496722SMatthew G. Knepley /*@C
753*15496722SMatthew G. Knepley   DMPlexComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
754*15496722SMatthew G. Knepley 
755*15496722SMatthew G. Knepley   Input Parameters:
756*15496722SMatthew G. Knepley + dm    - The DM
757*15496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component
758*15496722SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
759*15496722SMatthew G. Knepley - X     - The coefficient vector u_h
760*15496722SMatthew G. Knepley 
761*15496722SMatthew G. Knepley   Output Parameter:
762*15496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2
763*15496722SMatthew G. Knepley 
764*15496722SMatthew G. Knepley   Level: developer
765*15496722SMatthew G. Knepley 
766*15496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff()
767*15496722SMatthew G. Knepley @*/
7680f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
76973d901b8SMatthew G. Knepley {
77073d901b8SMatthew G. Knepley   const PetscInt   debug = 0;
77173d901b8SMatthew G. Knepley   PetscSection     section;
77273d901b8SMatthew G. Knepley   PetscQuadrature  quad;
77373d901b8SMatthew G. Knepley   Vec              localX;
774*15496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
77573d901b8SMatthew G. Knepley   PetscReal       *coords, *v0, *J, *invJ, detJ;
77673d901b8SMatthew G. Knepley   PetscReal       *localDiff;
777*15496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
778*15496722SMatthew G. Knepley   PetscInt         dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, c, field, fieldOffset;
77973d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
78073d901b8SMatthew G. Knepley 
78173d901b8SMatthew G. Knepley   PetscFunctionBegin;
782c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
78373d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
78473d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
78573d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
78673d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
78773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
78873d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
789*15496722SMatthew G. Knepley     PetscObject  obj;
790*15496722SMatthew G. Knepley     PetscClassId id;
79173d901b8SMatthew G. Knepley     PetscInt     Nc;
79273d901b8SMatthew G. Knepley 
793*15496722SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
794*15496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
795*15496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
796*15496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
797*15496722SMatthew G. Knepley 
7980f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7990f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
800*15496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
801*15496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
802*15496722SMatthew G. Knepley 
803*15496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
804*15496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
805*15496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
80673d901b8SMatthew G. Knepley     numComponents += Nc;
80773d901b8SMatthew G. Knepley   }
808*15496722SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr);
8090f2d7e86SMatthew G. Knepley   ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
810*15496722SMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
81173d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
81273d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
81373d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
81473d901b8SMatthew G. Knepley 
8158e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
81673d901b8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
81773d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
81873d901b8SMatthew G. Knepley 
819*15496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
820*15496722SMatthew G. Knepley       PetscObject  obj;
821*15496722SMatthew G. Knepley       PetscClassId id;
82273d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
823*15496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
82473d901b8SMatthew G. Knepley 
825*15496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
826*15496722SMatthew G. Knepley 
827*15496722SMatthew G. Knepley       ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
828*15496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
829*15496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
830*15496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
831*15496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
83273d901b8SMatthew G. Knepley       if (debug) {
83373d901b8SMatthew G. Knepley         char title[1024];
83473d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
835*15496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
83673d901b8SMatthew G. Knepley       }
83773d901b8SMatthew G. Knepley       for (q = 0; q < numQuadPoints; ++q) {
838*15496722SMatthew G. Knepley         CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords);
83973d901b8SMatthew G. Knepley         (*funcs[field])(coords, funcVal, ctx);
840*15496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
841*15496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
842*15496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
843*15496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
844*15496722SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);}
845*15496722SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ;
84673d901b8SMatthew G. Knepley         }
84773d901b8SMatthew G. Knepley       }
848*15496722SMatthew G. Knepley       fieldOffset += Nb*Nc;
84973d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
85073d901b8SMatthew G. Knepley     }
85173d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
85273d901b8SMatthew G. Knepley   }
85373d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
85473d901b8SMatthew G. Knepley   ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
85573d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
856*15496722SMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr);
85773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
85873d901b8SMatthew G. Knepley }
85973d901b8SMatthew G. Knepley 
86073d901b8SMatthew G. Knepley #undef __FUNCT__
86173d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM"
86273d901b8SMatthew G. Knepley /*@
86373d901b8SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user
86473d901b8SMatthew G. Knepley 
86573d901b8SMatthew G. Knepley   Input Parameters:
86673d901b8SMatthew G. Knepley + dm - The mesh
86773d901b8SMatthew G. Knepley . X  - Local input vector
86873d901b8SMatthew G. Knepley - user - The user context
86973d901b8SMatthew G. Knepley 
87073d901b8SMatthew G. Knepley   Output Parameter:
87173d901b8SMatthew G. Knepley . integral - Local integral for each field
87273d901b8SMatthew G. Knepley 
87373d901b8SMatthew G. Knepley   Level: developer
87473d901b8SMatthew G. Knepley 
87573d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
87673d901b8SMatthew G. Knepley @*/
8770f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user)
87873d901b8SMatthew G. Knepley {
87973d901b8SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
88073d901b8SMatthew G. Knepley   DM                dmAux;
88173d901b8SMatthew G. Knepley   Vec               localX, A;
8822764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
88373d901b8SMatthew G. Knepley   PetscQuadrature   q;
88473d901b8SMatthew G. Knepley   PetscCellGeometry geom;
88573d901b8SMatthew G. Knepley   PetscSection      section, sectionAux;
88673d901b8SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
88773d901b8SMatthew G. Knepley   PetscScalar      *u, *a = NULL;
8880f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
8890f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimAux;
89073d901b8SMatthew G. Knepley   PetscErrorCode    ierr;
89173d901b8SMatthew G. Knepley 
89273d901b8SMatthew G. Knepley   PetscFunctionBegin;
89373d901b8SMatthew G. Knepley   /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
89473d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
89573d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
89673d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
897c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
89873d901b8SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
8992764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
9002764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
90173d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
90273d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
90373d901b8SMatthew G. Knepley   numCells = cEnd - cStart;
9040f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {integral[f]    = 0.0;}
90573d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
90673d901b8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
90773d901b8SMatthew G. Knepley   if (dmAux) {
90873d901b8SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
9092764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
9102764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
91173d901b8SMatthew G. Knepley   }
91273d901b8SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, localX);CHKERRQ(ierr);
9130f2d7e86SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ);CHKERRQ(ierr);
9140f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
91573d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
91673d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
91773d901b8SMatthew G. Knepley     PetscInt     i;
91873d901b8SMatthew G. Knepley 
9198e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
92073d901b8SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
92173d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
9220f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
92373d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr);
92473d901b8SMatthew G. Knepley     if (dmAux) {
92573d901b8SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
9260f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
92773d901b8SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
92873d901b8SMatthew G. Knepley     }
92973d901b8SMatthew G. Knepley   }
93073d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
9310f2d7e86SMatthew G. Knepley     PetscFE  fe;
93273d901b8SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
93373d901b8SMatthew G. Knepley     /* Conforming batches */
93473d901b8SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
93573d901b8SMatthew G. Knepley     /* Remainder */
93673d901b8SMatthew G. Knepley     PetscInt Nr, offset;
93773d901b8SMatthew G. Knepley 
9382764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
9390f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
9400f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
9410f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
94273d901b8SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
94373d901b8SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
94473d901b8SMatthew G. Knepley     batchSize = numBlocks * blockSize;
9450f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
94673d901b8SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
94773d901b8SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
94873d901b8SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
94973d901b8SMatthew G. Knepley     offset    = numCells - Nr;
95073d901b8SMatthew G. Knepley     geom.v0   = v0;
95173d901b8SMatthew G. Knepley     geom.J    = J;
95273d901b8SMatthew G. Knepley     geom.invJ = invJ;
95373d901b8SMatthew G. Knepley     geom.detJ = detJ;
9540f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Ne, geom, u, probAux, a, integral);CHKERRQ(ierr);
95573d901b8SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
95673d901b8SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
95773d901b8SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
95873d901b8SMatthew G. Knepley     geom.detJ = &detJ[offset];
9590f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrate(fe, prob, f, Nr, geom, &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr);
96073d901b8SMatthew G. Knepley   }
96173d901b8SMatthew G. Knepley   ierr = PetscFree5(u,v0,J,invJ,detJ);CHKERRQ(ierr);
96273d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
96373d901b8SMatthew G. Knepley   if (mesh->printFEM) {
96473d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr);
96573d901b8SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);}
96673d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
96773d901b8SMatthew G. Knepley   }
96873d901b8SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
96973d901b8SMatthew G. Knepley   /* TODO: Allreduce for integral */
97073d901b8SMatthew G. Knepley   /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/
97173d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
97273d901b8SMatthew G. Knepley }
97373d901b8SMatthew G. Knepley 
97473d901b8SMatthew G. Knepley #undef __FUNCT__
9750f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal"
9760f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
9770f2d7e86SMatthew G. Knepley {
9780f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
9790f2d7e86SMatthew G. Knepley   const char       *name  = "Residual";
9800f2d7e86SMatthew G. Knepley   DM                dmAux;
9810f2d7e86SMatthew G. Knepley   DMLabel           depth;
9820f2d7e86SMatthew G. Knepley   Vec               A;
9832764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
9840f2d7e86SMatthew G. Knepley   PetscQuadrature   q;
9850f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
9860f2d7e86SMatthew G. Knepley   PetscSection      section, sectionAux;
9870f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
9880f2d7e86SMatthew G. Knepley   PetscScalar      *elemVec, *u, *u_t, *a = NULL;
9890f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd;
9900f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux;
9910f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
9920f2d7e86SMatthew G. Knepley 
9930f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
9940f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
995c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9960f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
9972764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
9982764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
9992764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
10000f2d7e86SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
10010f2d7e86SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10020f2d7e86SMatthew G. Knepley   numCells = cEnd - cStart;
10030f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
10040f2d7e86SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
10050f2d7e86SMatthew G. Knepley   if (dmAux) {
10060f2d7e86SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
10072764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
10082764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
10090f2d7e86SMatthew G. Knepley   }
10100f2d7e86SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
10110f2d7e86SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
10120f2d7e86SMatthew 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);
10130f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
10140f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10150f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
10160f2d7e86SMatthew G. Knepley     PetscInt     i;
10170f2d7e86SMatthew G. Knepley 
10188e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
10190f2d7e86SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
10200f2d7e86SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10210f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
10220f2d7e86SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
10230f2d7e86SMatthew G. Knepley     if (X_t) {
10240f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10250f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
10260f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
10270f2d7e86SMatthew G. Knepley     }
10280f2d7e86SMatthew G. Knepley     if (dmAux) {
10290f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
10300f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
10310f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
10320f2d7e86SMatthew G. Knepley     }
10330f2d7e86SMatthew G. Knepley   }
10340f2d7e86SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
10350f2d7e86SMatthew G. Knepley     PetscFE  fe;
10360f2d7e86SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
10370f2d7e86SMatthew G. Knepley     /* Conforming batches */
10380f2d7e86SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
10390f2d7e86SMatthew G. Knepley     /* Remainder */
10400f2d7e86SMatthew G. Knepley     PetscInt Nr, offset;
10410f2d7e86SMatthew G. Knepley 
10422764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
10430f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
10440f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
10450f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
10460f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
10470f2d7e86SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
10480f2d7e86SMatthew G. Knepley     batchSize = numBlocks * blockSize;
10490f2d7e86SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
10500f2d7e86SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
10510f2d7e86SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
10520f2d7e86SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
10530f2d7e86SMatthew G. Knepley     offset    = numCells - Nr;
10540f2d7e86SMatthew G. Knepley     geom.v0   = v0;
10550f2d7e86SMatthew G. Knepley     geom.J    = J;
10560f2d7e86SMatthew G. Knepley     geom.invJ = invJ;
10570f2d7e86SMatthew G. Knepley     geom.detJ = detJ;
10580f2d7e86SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
10590f2d7e86SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
10600f2d7e86SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
10610f2d7e86SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
10620f2d7e86SMatthew G. Knepley     geom.detJ = &detJ[offset];
10630f2d7e86SMatthew 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);
10640f2d7e86SMatthew G. Knepley   }
10650f2d7e86SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10660f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);}
10670f2d7e86SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
10680f2d7e86SMatthew G. Knepley   }
10690f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
10700f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
10710f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
10720f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
10730f2d7e86SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
10740f2d7e86SMatthew G. Knepley     const char     *bdLabel;
10750f2d7e86SMatthew G. Knepley     DMLabel         label;
10760f2d7e86SMatthew G. Knepley     IS              pointIS;
10770f2d7e86SMatthew G. Knepley     const PetscInt *points;
10780f2d7e86SMatthew G. Knepley     const PetscInt *values;
10790f2d7e86SMatthew G. Knepley     PetscReal      *n;
10800f2d7e86SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
10810f2d7e86SMatthew G. Knepley     PetscBool       isEssential;
10820f2d7e86SMatthew G. Knepley 
10830f2d7e86SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
10840f2d7e86SMatthew G. Knepley     if (isEssential) continue;
10850f2d7e86SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
10860f2d7e86SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
10870f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
10880f2d7e86SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
10890f2d7e86SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
10900f2d7e86SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
10910f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
10920f2d7e86SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
10930f2d7e86SMatthew G. Knepley     }
10940f2d7e86SMatthew 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);
10950f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
10960f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
10970f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
10980f2d7e86SMatthew G. Knepley       PetscScalar   *x     = NULL;
10990f2d7e86SMatthew G. Knepley       PetscInt       i;
11000f2d7e86SMatthew G. Knepley 
11010f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
11020f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
11038e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
11040f2d7e86SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
11050f2d7e86SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
11060f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11070f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
11080f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
11090f2d7e86SMatthew G. Knepley       if (X_t) {
11100f2d7e86SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11110f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
11120f2d7e86SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
11130f2d7e86SMatthew G. Knepley       }
11140f2d7e86SMatthew G. Knepley       ++f;
11150f2d7e86SMatthew G. Knepley     }
11160f2d7e86SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
11170f2d7e86SMatthew G. Knepley       PetscFE  fe;
11180f2d7e86SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
11190f2d7e86SMatthew G. Knepley       /* Conforming batches */
11200f2d7e86SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
11210f2d7e86SMatthew G. Knepley       /* Remainder */
11220f2d7e86SMatthew G. Knepley       PetscInt Nr, offset;
11230f2d7e86SMatthew G. Knepley 
11242764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
11250f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
11260f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11270f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
11280f2d7e86SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
11290f2d7e86SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
11300f2d7e86SMatthew G. Knepley       batchSize = numBlocks * blockSize;
11310f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
11320f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
11330f2d7e86SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
11340f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
11350f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
11360f2d7e86SMatthew G. Knepley       geom.v0   = v0;
11370f2d7e86SMatthew G. Knepley       geom.n    = n;
11380f2d7e86SMatthew G. Knepley       geom.J    = J;
11390f2d7e86SMatthew G. Knepley       geom.invJ = invJ;
11400f2d7e86SMatthew G. Knepley       geom.detJ = detJ;
11410f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
11420f2d7e86SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
11430f2d7e86SMatthew G. Knepley       geom.n    = &n[offset*dim];
11440f2d7e86SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
11450f2d7e86SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
11460f2d7e86SMatthew G. Knepley       geom.detJ = &detJ[offset];
11470f2d7e86SMatthew 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);
11480f2d7e86SMatthew G. Knepley     }
11490f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
11500f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
11510f2d7e86SMatthew G. Knepley 
11520f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
11530f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
11540f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
11550f2d7e86SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
11560f2d7e86SMatthew G. Knepley       ++f;
11570f2d7e86SMatthew G. Knepley     }
11580f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
11590f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
11600f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
11610f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
11620f2d7e86SMatthew G. Knepley   }
11630f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
11640f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
11650f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
11660f2d7e86SMatthew G. Knepley }
11670f2d7e86SMatthew G. Knepley 
11680f2d7e86SMatthew G. Knepley #undef __FUNCT__
116908f11eefSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check"
117008f11eefSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check(DM dm, Vec X, Vec X_t, Vec F, void *user)
117108f11eefSMatthew G. Knepley {
117208f11eefSMatthew G. Knepley   DM                dmCh, dmAux;
117308f11eefSMatthew G. Knepley   Vec               A;
11746f4eac71SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
117508f11eefSMatthew G. Knepley   PetscQuadrature   q;
117608f11eefSMatthew G. Knepley   PetscCellGeometry geom;
117708f11eefSMatthew G. Knepley   PetscSection      section, sectionAux;
117808f11eefSMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
117908f11eefSMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
118008f11eefSMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
118108f11eefSMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
118208f11eefSMatthew G. Knepley   PetscErrorCode    ierr;
118308f11eefSMatthew G. Knepley 
118408f11eefSMatthew G. Knepley   PetscFunctionBegin;
1185c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
118608f11eefSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
11876f4eac71SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
11886f4eac71SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
118908f11eefSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
119008f11eefSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
119108f11eefSMatthew G. Knepley   numCells = cEnd - cStart;
119208f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
11936f4eac71SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
119408f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
119508f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
119608f11eefSMatthew G. Knepley   if (dmAux) {
119708f11eefSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
11986f4eac71SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
11996f4eac71SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
120008f11eefSMatthew G. Knepley   }
120108f11eefSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
120208f11eefSMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
120308f11eefSMatthew 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);
120408f11eefSMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
120508f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
120608f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
120708f11eefSMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
120808f11eefSMatthew G. Knepley     PetscInt     i;
120908f11eefSMatthew G. Knepley 
12108e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
121108f11eefSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
121208f11eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
121308f11eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
121408f11eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
121508f11eefSMatthew G. Knepley     if (X_t) {
121608f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
121708f11eefSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
121808f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
121908f11eefSMatthew G. Knepley     }
122008f11eefSMatthew G. Knepley     if (dmAux) {
122108f11eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
122208f11eefSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
122308f11eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
122408f11eefSMatthew G. Knepley     }
122508f11eefSMatthew G. Knepley   }
122608f11eefSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
122708f11eefSMatthew G. Knepley     PetscFE  fe, feCh;
122808f11eefSMatthew G. Knepley     PetscInt numQuadPoints, Nb;
122908f11eefSMatthew G. Knepley     /* Conforming batches */
123008f11eefSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
123108f11eefSMatthew G. Knepley     /* Remainder */
123208f11eefSMatthew G. Knepley     PetscInt Nr, offset;
123308f11eefSMatthew G. Knepley 
12346f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
12356f4eac71SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
123608f11eefSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
123708f11eefSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
123808f11eefSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
123908f11eefSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
124008f11eefSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
124108f11eefSMatthew G. Knepley     batchSize = numBlocks * blockSize;
124208f11eefSMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
124308f11eefSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
124408f11eefSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
124508f11eefSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
124608f11eefSMatthew G. Knepley     offset    = numCells - Nr;
124708f11eefSMatthew G. Knepley     geom.v0   = v0;
124808f11eefSMatthew G. Knepley     geom.J    = J;
124908f11eefSMatthew G. Knepley     geom.invJ = invJ;
125008f11eefSMatthew G. Knepley     geom.detJ = detJ;
125108f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
125208f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, geom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
125308f11eefSMatthew G. Knepley     geom.v0   = &v0[offset*dim];
125408f11eefSMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
125508f11eefSMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
125608f11eefSMatthew G. Knepley     geom.detJ = &detJ[offset];
125708f11eefSMatthew 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);
125808f11eefSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr);
125908f11eefSMatthew G. Knepley   }
126008f11eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
126108f11eefSMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
126208f11eefSMatthew G. Knepley     PetscInt  d;
126308f11eefSMatthew G. Knepley 
126408f11eefSMatthew G. Knepley     for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;}
126508f11eefSMatthew G. Knepley     if (diff) {
126608f11eefSMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
126708f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
126808f11eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
126908f11eefSMatthew G. Knepley       ++diffCell;
127008f11eefSMatthew G. Knepley     }
127108f11eefSMatthew G. Knepley     if (diffCell > 9) break;
127208f11eefSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
127308f11eefSMatthew G. Knepley   }
127408f11eefSMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
127508f11eefSMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
127608f11eefSMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
127708f11eefSMatthew G. Knepley   PetscFunctionReturn(0);
127808f11eefSMatthew G. Knepley }
127908f11eefSMatthew G. Knepley 
128008f11eefSMatthew G. Knepley #undef __FUNCT__
12810f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
1282a0845e3aSMatthew G. Knepley /*@
12830f2d7e86SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1284a0845e3aSMatthew G. Knepley 
1285a0845e3aSMatthew G. Knepley   Input Parameters:
1286a0845e3aSMatthew G. Knepley + dm - The mesh
12870f2d7e86SMatthew G. Knepley . X  - Local solution
1288a0845e3aSMatthew G. Knepley - user - The user context
1289a0845e3aSMatthew G. Knepley 
1290a0845e3aSMatthew G. Knepley   Output Parameter:
1291a0845e3aSMatthew G. Knepley . F  - Local output vector
1292a0845e3aSMatthew G. Knepley 
1293a0845e3aSMatthew G. Knepley   Level: developer
1294a0845e3aSMatthew G. Knepley 
1295a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1296a0845e3aSMatthew G. Knepley @*/
12970f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1298a0845e3aSMatthew G. Knepley {
129908f11eefSMatthew G. Knepley   PetscObject    check;
1300a0845e3aSMatthew G. Knepley   PetscErrorCode ierr;
1301a0845e3aSMatthew G. Knepley 
1302a0845e3aSMatthew G. Knepley   PetscFunctionBegin;
130308f11eefSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
130408f11eefSMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check(dm, X, NULL, F, user);CHKERRQ(ierr);}
130508f11eefSMatthew G. Knepley   else       {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
13060f2d7e86SMatthew G. Knepley   PetscFunctionReturn(0);
13070f2d7e86SMatthew G. Knepley }
13080f2d7e86SMatthew G. Knepley 
13090f2d7e86SMatthew G. Knepley #undef __FUNCT__
1310adbe6fbbSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
1311adbe6fbbSMatthew G. Knepley /*@
1312adbe6fbbSMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1313adbe6fbbSMatthew G. Knepley 
1314adbe6fbbSMatthew G. Knepley   Input Parameters:
1315adbe6fbbSMatthew G. Knepley + dm - The mesh
1316adbe6fbbSMatthew G. Knepley . t - The time
1317adbe6fbbSMatthew G. Knepley . X  - Local solution
1318adbe6fbbSMatthew G. Knepley . X_t - Local solution time derivative, or NULL
1319adbe6fbbSMatthew G. Knepley - user - The user context
1320adbe6fbbSMatthew G. Knepley 
1321adbe6fbbSMatthew G. Knepley   Output Parameter:
1322adbe6fbbSMatthew G. Knepley . F  - Local output vector
1323adbe6fbbSMatthew G. Knepley 
1324adbe6fbbSMatthew G. Knepley   Level: developer
1325adbe6fbbSMatthew G. Knepley 
1326adbe6fbbSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1327adbe6fbbSMatthew G. Knepley @*/
1328adbe6fbbSMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec X, Vec X_t, Vec F, void *user)
1329adbe6fbbSMatthew G. Knepley {
1330adbe6fbbSMatthew G. Knepley   PetscErrorCode ierr;
1331adbe6fbbSMatthew G. Knepley 
1332adbe6fbbSMatthew G. Knepley   PetscFunctionBegin;
1333adbe6fbbSMatthew G. Knepley   ierr = DMPlexComputeResidualFEM_Internal(dm, X, X_t, F, user);CHKERRQ(ierr);
1334adbe6fbbSMatthew G. Knepley   PetscFunctionReturn(0);
1335adbe6fbbSMatthew G. Knepley }
1336adbe6fbbSMatthew G. Knepley 
1337adbe6fbbSMatthew G. Knepley #undef __FUNCT__
13380f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
13390f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
13400f2d7e86SMatthew G. Knepley {
13410f2d7e86SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
13420f2d7e86SMatthew G. Knepley   const char       *name  = "Jacobian";
13430f2d7e86SMatthew G. Knepley   DM                dmAux;
13440f2d7e86SMatthew G. Knepley   DMLabel           depth;
13450f2d7e86SMatthew G. Knepley   Vec               A;
13462764a2aaSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
13470f2d7e86SMatthew G. Knepley   PetscQuadrature   quad;
13480f2d7e86SMatthew G. Knepley   PetscCellGeometry geom;
13490f2d7e86SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
13500f2d7e86SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
13510f2d7e86SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
13520f2d7e86SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
13530f2d7e86SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
13540f2d7e86SMatthew G. Knepley   PetscBool         isShell;
13550f2d7e86SMatthew G. Knepley   PetscErrorCode    ierr;
13560f2d7e86SMatthew G. Knepley 
13570f2d7e86SMatthew G. Knepley   PetscFunctionBegin;
13580f2d7e86SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1359c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1360a0845e3aSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
13610f2d7e86SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
13622764a2aaSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
13632764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
13642764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
13659a559087SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1366a0845e3aSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1367a0845e3aSMatthew G. Knepley   numCells = cEnd - cStart;
13689a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
13699a559087SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
13709a559087SMatthew G. Knepley   if (dmAux) {
13719a559087SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
13722764a2aaSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
13732764a2aaSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
13749a559087SMatthew G. Knepley   }
13753351dd3dSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
13760f2d7e86SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
13770f2d7e86SMatthew 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);
13780f2d7e86SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1379a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
13800f2d7e86SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
1381a0845e3aSMatthew G. Knepley     PetscInt     i;
1382a0845e3aSMatthew G. Knepley 
13838e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1384a0845e3aSMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1385a0845e3aSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
13860f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1387a0845e3aSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
13880f2d7e86SMatthew G. Knepley     if (X_t) {
13890f2d7e86SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
13900f2d7e86SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
13910f2d7e86SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
13920f2d7e86SMatthew G. Knepley     }
13939a559087SMatthew G. Knepley     if (dmAux) {
13949a559087SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
13950f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
13969a559087SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1397a0845e3aSMatthew G. Knepley     }
13989a559087SMatthew G. Knepley   }
13990f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
14000f2d7e86SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
14010f2d7e86SMatthew G. Knepley     PetscFE  fe;
140221454ff5SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1403a0845e3aSMatthew G. Knepley     /* Conforming batches */
1404f30c5766SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1405a0845e3aSMatthew G. Knepley     /* Remainder */
1406a0845e3aSMatthew G. Knepley     PetscInt Nr, offset;
1407a0845e3aSMatthew G. Knepley 
14082764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
14090f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
14100f2d7e86SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
14110f2d7e86SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
14120f2d7e86SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
141321454ff5SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1414a0845e3aSMatthew G. Knepley     batchSize = numBlocks * blockSize;
14150f2d7e86SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1416a0845e3aSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1417a0845e3aSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1418a0845e3aSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1419a0845e3aSMatthew G. Knepley     offset    = numCells - Nr;
14200f2d7e86SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1421a0845e3aSMatthew G. Knepley       geom.v0   = v0;
1422a0845e3aSMatthew G. Knepley       geom.J    = J;
1423a0845e3aSMatthew G. Knepley       geom.invJ = invJ;
1424a0845e3aSMatthew G. Knepley       geom.detJ = detJ;
14250f2d7e86SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1426a0845e3aSMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1427a0845e3aSMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1428a0845e3aSMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1429a0845e3aSMatthew G. Knepley       geom.detJ = &detJ[offset];
14300f2d7e86SMatthew 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);
14310f2d7e86SMatthew G. Knepley     }
1432a0845e3aSMatthew G. Knepley   }
1433a0845e3aSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
14340f2d7e86SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
14350f2d7e86SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
1436a0845e3aSMatthew G. Knepley   }
14370f2d7e86SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
14389a559087SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
14390f2d7e86SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
14400f2d7e86SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
14411c093863SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
14421c093863SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
14431c093863SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
14441c093863SMatthew G. Knepley     const char     *bdLabel;
14451c093863SMatthew G. Knepley     DMLabel         label;
14461c093863SMatthew G. Knepley     IS              pointIS;
14471c093863SMatthew G. Knepley     const PetscInt *points;
14481c093863SMatthew G. Knepley     const PetscInt *values;
14491c093863SMatthew G. Knepley     PetscReal      *n;
14501c093863SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
14511c093863SMatthew G. Knepley     PetscBool       isEssential;
14521c093863SMatthew G. Knepley 
145363d5297fSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
14540f2d7e86SMatthew G. Knepley     if (isEssential) continue;
14551c093863SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
14561c093863SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
14571c093863SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
14581c093863SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
14591c093863SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1460075da914SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1461075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1462075da914SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1463075da914SMatthew G. Knepley     }
14640f2d7e86SMatthew 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);
14650f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1466075da914SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1467f1ea0e2fSMatthew G. Knepley       const PetscInt point = points[p];
1468f1ea0e2fSMatthew G. Knepley       PetscScalar   *x     = NULL;
1469f1ea0e2fSMatthew G. Knepley       PetscInt       i;
1470f1ea0e2fSMatthew G. Knepley 
1471075da914SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1472075da914SMatthew G. Knepley       if (dep != dim-1) continue;
14738e0841e0SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1474a8007bbfSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1475075da914SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1476f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
14770f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1478f1ea0e2fSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
14790f2d7e86SMatthew G. Knepley       if (X_t) {
1480af1eca97SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
14810f2d7e86SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1482af1eca97SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
14830f2d7e86SMatthew G. Knepley       }
1484af1eca97SMatthew G. Knepley       ++f;
1485af1eca97SMatthew G. Knepley     }
14860f2d7e86SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
14870f2d7e86SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
14880f2d7e86SMatthew G. Knepley       PetscFE  fe;
1489af1eca97SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1490af1eca97SMatthew G. Knepley       /* Conforming batches */
1491af1eca97SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1492af1eca97SMatthew G. Knepley       /* Remainder */
1493af1eca97SMatthew G. Knepley       PetscInt Nr, offset;
1494af1eca97SMatthew G. Knepley 
14952764a2aaSMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
14960f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
14970f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
14980f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
149921454ff5SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
150021454ff5SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
15010483ade4SMatthew G. Knepley       batchSize = numBlocks * blockSize;
15020f2d7e86SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
15030f2d7e86SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
15040483ade4SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
15050f2d7e86SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
15060f2d7e86SMatthew G. Knepley       offset    = numFaces - Nr;
15070f2d7e86SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
15080483ade4SMatthew G. Knepley         geom.v0   = v0;
15090f2d7e86SMatthew G. Knepley         geom.n    = n;
15100483ade4SMatthew G. Knepley         geom.J    = J;
15110483ade4SMatthew G. Knepley         geom.invJ = invJ;
15120483ade4SMatthew G. Knepley         geom.detJ = detJ;
15130f2d7e86SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
15140483ade4SMatthew G. Knepley         geom.v0   = &v0[offset*dim];
15150f2d7e86SMatthew G. Knepley         geom.n    = &n[offset*dim];
15160483ade4SMatthew G. Knepley         geom.J    = &J[offset*dim*dim];
15170483ade4SMatthew G. Knepley         geom.invJ = &invJ[offset*dim*dim];
15180483ade4SMatthew G. Knepley         geom.detJ = &detJ[offset];
15190f2d7e86SMatthew 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);
1520cb1e1211SMatthew G Knepley       }
1521cb1e1211SMatthew G Knepley     }
15220f2d7e86SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
15230f2d7e86SMatthew G. Knepley       const PetscInt point = points[p];
1524cb1e1211SMatthew G Knepley 
15250f2d7e86SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
15260f2d7e86SMatthew G. Knepley       if (dep != dim-1) continue;
15270f2d7e86SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
15280f2d7e86SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
15290f2d7e86SMatthew G. Knepley       ++f;
1530cb1e1211SMatthew G Knepley     }
15310f2d7e86SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
15320f2d7e86SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
15330f2d7e86SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr);
15340f2d7e86SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1535cb1e1211SMatthew G Knepley   }
15360f2d7e86SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15370f2d7e86SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
15380f2d7e86SMatthew G. Knepley   if (mesh->printFEM) {
15390f2d7e86SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
15400f2d7e86SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
15410f2d7e86SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
15420f2d7e86SMatthew G. Knepley   }
15430f2d7e86SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
15440f2d7e86SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
15450f2d7e86SMatthew G. Knepley   if (isShell) {
15460f2d7e86SMatthew G. Knepley     JacActionCtx *jctx;
15470f2d7e86SMatthew G. Knepley 
15480f2d7e86SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
15490f2d7e86SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
15500f2d7e86SMatthew G. Knepley   }
1551cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1552cb1e1211SMatthew G Knepley }
1553cb1e1211SMatthew G Knepley 
1554cb1e1211SMatthew G Knepley #undef __FUNCT__
15550f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
1556cb1e1211SMatthew G Knepley /*@
15570f2d7e86SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
1558cb1e1211SMatthew G Knepley 
1559cb1e1211SMatthew G Knepley   Input Parameters:
1560cb1e1211SMatthew G Knepley + dm - The mesh
1561cb1e1211SMatthew G Knepley . X  - Local input vector
1562cb1e1211SMatthew G Knepley - user - The user context
1563cb1e1211SMatthew G Knepley 
1564cb1e1211SMatthew G Knepley   Output Parameter:
1565cb1e1211SMatthew G Knepley . Jac  - Jacobian matrix
1566cb1e1211SMatthew G Knepley 
1567cb1e1211SMatthew G Knepley   Note:
15688026896cSMatthew G. Knepley   The first member of the user context must be an FEMContext.
1569cb1e1211SMatthew G Knepley 
1570cb1e1211SMatthew G Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1571cb1e1211SMatthew G Knepley   like a GPU, or vectorize on a multicore machine.
1572cb1e1211SMatthew G Knepley 
15730059ad2aSSatish Balay   Level: developer
15740059ad2aSSatish Balay 
1575cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal()
1576878cb397SSatish Balay @*/
15770f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
1578cb1e1211SMatthew G Knepley {
1579cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1580cb1e1211SMatthew G Knepley 
1581cb1e1211SMatthew G Knepley   PetscFunctionBegin;
15820f2d7e86SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
1583cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1584cb1e1211SMatthew G Knepley }
1585bceba477SMatthew G. Knepley 
1586d69c5d34SMatthew G. Knepley #undef __FUNCT__
1587d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM"
1588d69c5d34SMatthew G. Knepley /*@
1589d69c5d34SMatthew G. Knepley   DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1590d69c5d34SMatthew G. Knepley 
1591d69c5d34SMatthew G. Knepley   Input Parameters:
1592d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1593d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1594d69c5d34SMatthew G. Knepley - user - The user context
1595d69c5d34SMatthew G. Knepley 
1596d69c5d34SMatthew G. Knepley   Output Parameter:
1597934789fcSMatthew G. Knepley . In  - The interpolation matrix
1598d69c5d34SMatthew G. Knepley 
1599d69c5d34SMatthew G. Knepley   Note:
1600d69c5d34SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1601d69c5d34SMatthew G. Knepley 
1602d69c5d34SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1603d69c5d34SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1604d69c5d34SMatthew G. Knepley 
1605d69c5d34SMatthew G. Knepley   Level: developer
1606d69c5d34SMatthew G. Knepley 
1607d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM()
1608d69c5d34SMatthew G. Knepley @*/
1609934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user)
1610d69c5d34SMatthew G. Knepley {
1611d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1612d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
16132764a2aaSMatthew G. Knepley   PetscDS           prob;
1614d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
1615d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1616d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1617d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
1618942a7a06SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
16190f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1620d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1621d69c5d34SMatthew G. Knepley 
1622d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1623d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1624c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1625d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
1626d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1627d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
1628d69c5d34SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1629d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1630d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
16312764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
1632d69c5d34SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
1633d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
16340f2d7e86SMatthew G. Knepley     PetscFE  fe;
16350f2d7e86SMatthew G. Knepley     PetscInt rNb, Nc;
1636d69c5d34SMatthew G. Knepley 
16372764a2aaSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
16380f2d7e86SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1639d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
16400f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
16410f2d7e86SMatthew G. Knepley     rTotDim += rNb*Nc;
1642d69c5d34SMatthew G. Knepley   }
16432764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
16440f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
16450f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1646d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1647d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1648d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1649d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1650d69c5d34SMatthew G. Knepley     PetscReal       *points;
1651d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1652d69c5d34SMatthew G. Knepley 
1653d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
1654d69c5d34SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
16550f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
1656d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1657d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1658d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1659d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1660d69c5d34SMatthew G. Knepley       npoints += Np;
1661d69c5d34SMatthew G. Knepley     }
1662d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1663d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1664d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1665d69c5d34SMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1666d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1667d69c5d34SMatthew G. Knepley     }
1668d69c5d34SMatthew G. Knepley 
1669d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
16700f2d7e86SMatthew G. Knepley       PetscFE    fe;
1671d69c5d34SMatthew G. Knepley       PetscReal *B;
167236a6d9c0SMatthew G. Knepley       PetscInt   NcJ, cpdim, j;
1673d69c5d34SMatthew G. Knepley 
1674d69c5d34SMatthew G. Knepley       /* Evaluate basis at points */
16752764a2aaSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr);
16760f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
16770f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1678ffe73a53SMatthew G. Knepley       /* For now, fields only interpolate themselves */
1679ffe73a53SMatthew G. Knepley       if (fieldI == fieldJ) {
16807c927364SMatthew 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);
16810f2d7e86SMatthew G. Knepley         ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1682d69c5d34SMatthew G. Knepley         for (i = 0, k = 0; i < fpdim; ++i) {
1683d69c5d34SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
1684d69c5d34SMatthew G. Knepley           ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr);
1685d69c5d34SMatthew G. Knepley           for (p = 0; p < Np; ++p, ++k) {
168636a6d9c0SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
16870f2d7e86SMatthew 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];
168836a6d9c0SMatthew G. Knepley             }
1689d69c5d34SMatthew G. Knepley           }
1690d69c5d34SMatthew G. Knepley         }
16910f2d7e86SMatthew G. Knepley         ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1692ffe73a53SMatthew G. Knepley       }
169336a6d9c0SMatthew G. Knepley       offsetJ += cpdim*NcJ;
1694d69c5d34SMatthew G. Knepley     }
1695d69c5d34SMatthew G. Knepley     offsetI += fpdim*Nc;
1696549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1697d69c5d34SMatthew G. Knepley   }
16980f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
16997f5b169aSMatthew G. Knepley   /* Preallocate matrix */
17007f5b169aSMatthew G. Knepley   {
17017f5b169aSMatthew G. Knepley     PetscHashJK ht;
17027f5b169aSMatthew G. Knepley     PetscLayout rLayout;
17037f5b169aSMatthew G. Knepley     PetscInt   *dnz, *onz, *cellCIndices, *cellFIndices;
17047f5b169aSMatthew G. Knepley     PetscInt    locRows, rStart, rEnd, cell, r;
17057f5b169aSMatthew G. Knepley 
17067f5b169aSMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
17077f5b169aSMatthew G. Knepley     ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
17087f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
17097f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
17107f5b169aSMatthew G. Knepley     ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
17117f5b169aSMatthew G. Knepley     ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
17127f5b169aSMatthew G. Knepley     ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
17137f5b169aSMatthew G. Knepley     ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
17147f5b169aSMatthew G. Knepley     ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr);
17157f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
17167f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
17177f5b169aSMatthew G. Knepley       for (r = 0; r < rTotDim; ++r) {
17187f5b169aSMatthew G. Knepley         PetscHashJKKey  key;
17197f5b169aSMatthew G. Knepley         PetscHashJKIter missing, iter;
17207f5b169aSMatthew G. Knepley 
17217f5b169aSMatthew G. Knepley         key.j = cellFIndices[r];
17227f5b169aSMatthew G. Knepley         if (key.j < 0) continue;
17237f5b169aSMatthew G. Knepley         for (c = 0; c < cTotDim; ++c) {
17247f5b169aSMatthew G. Knepley           key.k = cellCIndices[c];
17257f5b169aSMatthew G. Knepley           if (key.k < 0) continue;
17267f5b169aSMatthew G. Knepley           ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr);
17277f5b169aSMatthew G. Knepley           if (missing) {
17287f5b169aSMatthew G. Knepley             ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr);
17297f5b169aSMatthew G. Knepley             if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart];
17307f5b169aSMatthew G. Knepley             else                                     ++onz[key.j-rStart];
17317f5b169aSMatthew G. Knepley           }
17327f5b169aSMatthew G. Knepley         }
17337f5b169aSMatthew G. Knepley       }
17347f5b169aSMatthew G. Knepley     }
17357f5b169aSMatthew G. Knepley     ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr);
17367f5b169aSMatthew G. Knepley     ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
17377f5b169aSMatthew G. Knepley     ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
17387f5b169aSMatthew G. Knepley     ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr);
17397f5b169aSMatthew G. Knepley   }
17407f5b169aSMatthew G. Knepley   /* Fill matrix */
17417f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1742d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1743934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1744d69c5d34SMatthew G. Knepley   }
1745549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
1746d69c5d34SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
1747549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1748934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1749934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1750d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1751d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1752934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1753934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1754d69c5d34SMatthew G. Knepley   }
1755d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1756d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1757d69c5d34SMatthew G. Knepley }
17586c73c22cSMatthew G. Knepley 
17596c73c22cSMatthew G. Knepley #undef __FUNCT__
17607c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM"
17617c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
17627c927364SMatthew G. Knepley {
1763e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
17647c927364SMatthew G. Knepley   PetscFE       *feRef;
17657c927364SMatthew G. Knepley   Vec            fv, cv;
17667c927364SMatthew G. Knepley   IS             fis, cis;
17677c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
17687c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
17697c927364SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, offsetC, offsetF, m;
17707c927364SMatthew G. Knepley   PetscErrorCode ierr;
17717c927364SMatthew G. Knepley 
17727c927364SMatthew G. Knepley   PetscFunctionBegin;
177375a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1774c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
17757c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);
17767c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
17777c927364SMatthew G. Knepley   ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);
17787c927364SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
17797c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
17807c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
1781e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
17827c927364SMatthew G. Knepley   ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr);
17837c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
17847c927364SMatthew G. Knepley     PetscFE  fe;
17857c927364SMatthew G. Knepley     PetscInt fNb, Nc;
17867c927364SMatthew G. Knepley 
1787e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
17887c927364SMatthew G. Knepley     ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
17897c927364SMatthew G. Knepley     ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
17907c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
17917c927364SMatthew G. Knepley     fTotDim += fNb*Nc;
17927c927364SMatthew G. Knepley   }
1793e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17947c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
17957c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
17967c927364SMatthew G. Knepley     PetscFE        feC;
17977c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
17987c927364SMatthew G. Knepley     PetscInt       NcF, NcC, fpdim, cpdim;
17997c927364SMatthew G. Knepley 
1800e9d4ef1bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
18017c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
18027c927364SMatthew G. Knepley     ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
18037c927364SMatthew G. Knepley     if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC);
18047c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
18057c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
18067c927364SMatthew G. Knepley     ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
18077c927364SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
18087c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
18097c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
18107c927364SMatthew G. Knepley       const PetscReal *cqpoints;
18117c927364SMatthew G. Knepley       PetscInt         NpC;
18127c927364SMatthew G. Knepley 
18137c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
18147c927364SMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr);
18157c927364SMatthew G. Knepley       if (NpC != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
18167c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
18177c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
18187c927364SMatthew G. Knepley         const PetscReal *fqpoints;
18197c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
18207c927364SMatthew G. Knepley         PetscInt         NpF, comp;
18217c927364SMatthew G. Knepley 
18227c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
18237c927364SMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr);
18247c927364SMatthew G. Knepley         if (NpC != NpF) continue;
18257c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
18267c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
18277c927364SMatthew G. Knepley         for (comp = 0; comp < NcC; ++comp) {
18287c927364SMatthew G. Knepley           cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp;
18297c927364SMatthew G. Knepley         }
18307c927364SMatthew G. Knepley         break;
18317c927364SMatthew G. Knepley       }
18327c927364SMatthew G. Knepley     }
18337c927364SMatthew G. Knepley     offsetC += cpdim*NcC;
18347c927364SMatthew G. Knepley     offsetF += fpdim*NcF;
18357c927364SMatthew G. Knepley   }
18367c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
18377c927364SMatthew G. Knepley   ierr = PetscFree(feRef);CHKERRQ(ierr);
18387c927364SMatthew G. Knepley 
18397c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
18407c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
18417c927364SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr);
18427c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
18437c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
1844aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
1845aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
18467c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
18477c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
18487c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
18497c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
18507c927364SMatthew G. Knepley       if (cellCIndices[d] < 0) continue;
18517c927364SMatthew G. Knepley       if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]);
18527c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
18537c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
18547c927364SMatthew G. Knepley     }
18557c927364SMatthew G. Knepley   }
18567c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
18577c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
18587c927364SMatthew G. Knepley 
18597c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
18607c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
18617c927364SMatthew G. Knepley   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
18627c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
18637c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
18647c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
18657c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
186675a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1867cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1868cb1e1211SMatthew G Knepley }
1869cb1e1211SMatthew G Knepley 
1870cb1e1211SMatthew G Knepley #undef __FUNCT__
18718e136ac0SMatthew G. Knepley #define __FUNCT__ "BoundaryDuplicate"
18728e136ac0SMatthew G. Knepley static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary)
18738e136ac0SMatthew G. Knepley {
18748e136ac0SMatthew G. Knepley   DMBoundary     b = bd, b2, bold = NULL;
18758e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
18768e136ac0SMatthew G. Knepley 
18778e136ac0SMatthew G. Knepley   PetscFunctionBegin;
18788e136ac0SMatthew G. Knepley   *boundary = NULL;
18798e136ac0SMatthew G. Knepley   for (; b; b = b->next, bold = b2) {
18808e136ac0SMatthew G. Knepley     ierr = PetscNew(&b2);CHKERRQ(ierr);
18818e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr);
18828e136ac0SMatthew G. Knepley     ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr);
18838e136ac0SMatthew G. Knepley     ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr);
18848e136ac0SMatthew G. Knepley     ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr);
18858e136ac0SMatthew G. Knepley     b2->label     = NULL;
18868e136ac0SMatthew G. Knepley     b2->essential = b->essential;
18878e136ac0SMatthew G. Knepley     b2->field     = b->field;
18888e136ac0SMatthew G. Knepley     b2->func      = b->func;
18898e136ac0SMatthew G. Knepley     b2->numids    = b->numids;
18908e136ac0SMatthew G. Knepley     b2->ctx       = b->ctx;
18918e136ac0SMatthew G. Knepley     b2->next      = NULL;
18928e136ac0SMatthew G. Knepley     if (!*boundary) *boundary   = b2;
18938e136ac0SMatthew G. Knepley     if (bold)        bold->next = b2;
18948e136ac0SMatthew G. Knepley   }
18958e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
18968e136ac0SMatthew G. Knepley }
18978e136ac0SMatthew G. Knepley 
18988e136ac0SMatthew G. Knepley #undef __FUNCT__
18998e136ac0SMatthew G. Knepley #define __FUNCT__ "DMPlexCopyBoundary"
19008e136ac0SMatthew G. Knepley PetscErrorCode DMPlexCopyBoundary(DM dm, DM dmNew)
19018e136ac0SMatthew G. Knepley {
19028e136ac0SMatthew G. Knepley   DM_Plex       *mesh    = (DM_Plex *) dm->data;
19038e136ac0SMatthew G. Knepley   DM_Plex       *meshNew = (DM_Plex *) dmNew->data;
19048e136ac0SMatthew G. Knepley   DMBoundary     b;
19058e136ac0SMatthew G. Knepley   PetscErrorCode ierr;
19068e136ac0SMatthew G. Knepley 
19078e136ac0SMatthew G. Knepley   PetscFunctionBegin;
19088e136ac0SMatthew G. Knepley   ierr = BoundaryDuplicate(mesh->boundary, &meshNew->boundary);CHKERRQ(ierr);
19098e136ac0SMatthew G. Knepley   for (b = meshNew->boundary; b; b = b->next) {
19108e136ac0SMatthew G. Knepley     if (b->labelname) {
19118e136ac0SMatthew G. Knepley       ierr = DMPlexGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr);
19128e136ac0SMatthew G. Knepley       if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
19138e136ac0SMatthew G. Knepley     }
19148e136ac0SMatthew G. Knepley   }
19158e136ac0SMatthew G. Knepley   PetscFunctionReturn(0);
19168e136ac0SMatthew G. Knepley }
19178e136ac0SMatthew G. Knepley 
19188e136ac0SMatthew G. Knepley #undef __FUNCT__
1919cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexAddBoundary"
1920cb1e1211SMatthew G Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */
192163d5297fSMatthew 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)
1922cb1e1211SMatthew G Knepley {
1923cb1e1211SMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1924cb1e1211SMatthew G Knepley   DMBoundary     b;
1925cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
1926cb1e1211SMatthew G Knepley 
1927cb1e1211SMatthew G Knepley   PetscFunctionBegin;
192863d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1929cb1e1211SMatthew G Knepley   ierr = PetscNew(&b);CHKERRQ(ierr);
1930cb1e1211SMatthew G Knepley   ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr);
193163d5297fSMatthew G. Knepley   ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr);
1932cb1e1211SMatthew G Knepley   ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr);
1933cb1e1211SMatthew G Knepley   ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr);
193463d5297fSMatthew G. Knepley   if (b->labelname) {
193563d5297fSMatthew G. Knepley     ierr = DMPlexGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr);
193663d5297fSMatthew G. Knepley     if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname);
193763d5297fSMatthew G. Knepley   }
1938cb1e1211SMatthew G Knepley   b->essential   = isEssential;
1939cb1e1211SMatthew G Knepley   b->field       = field;
1940cb1e1211SMatthew G Knepley   b->func        = bcFunc;
1941cb1e1211SMatthew G Knepley   b->numids      = numids;
1942cb1e1211SMatthew G Knepley   b->ctx         = ctx;
1943cb1e1211SMatthew G Knepley   b->next        = mesh->boundary;
1944cb1e1211SMatthew G Knepley   mesh->boundary = b;
1945cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1946cb1e1211SMatthew G Knepley }
1947cb1e1211SMatthew G Knepley 
1948cb1e1211SMatthew G Knepley #undef __FUNCT__
1949cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetNumBoundary"
1950cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd)
1951cb1e1211SMatthew G Knepley {
1952cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
1953cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
1954cb1e1211SMatthew G Knepley 
1955cb1e1211SMatthew G Knepley   PetscFunctionBegin;
195663d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
195763d5297fSMatthew G. Knepley   PetscValidPointer(numBd, 2);
1958cb1e1211SMatthew G Knepley   *numBd = 0;
1959cb1e1211SMatthew G Knepley   while (b) {++(*numBd); b = b->next;}
1960cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1961cb1e1211SMatthew G Knepley }
1962cb1e1211SMatthew G Knepley 
1963cb1e1211SMatthew G Knepley #undef __FUNCT__
1964cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetBoundary"
196563d5297fSMatthew 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)
1966cb1e1211SMatthew G Knepley {
1967cb1e1211SMatthew G Knepley   DM_Plex   *mesh = (DM_Plex *) dm->data;
1968cb1e1211SMatthew G Knepley   DMBoundary b    = mesh->boundary;
1969cb1e1211SMatthew G Knepley   PetscInt   n    = 0;
1970cb1e1211SMatthew G Knepley 
1971cb1e1211SMatthew G Knepley   PetscFunctionBegin;
197263d5297fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1973cb1e1211SMatthew G Knepley   while (b) {
1974cb1e1211SMatthew G Knepley     if (n == bd) break;
1975cb1e1211SMatthew G Knepley     b = b->next;
1976cb1e1211SMatthew G Knepley     ++n;
1977cb1e1211SMatthew G Knepley   }
1978cb1e1211SMatthew G Knepley   if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n);
1979cb1e1211SMatthew G Knepley   if (isEssential) {
1980cb1e1211SMatthew G Knepley     PetscValidPointer(isEssential, 3);
1981cb1e1211SMatthew G Knepley     *isEssential = b->essential;
1982cb1e1211SMatthew G Knepley   }
1983cb1e1211SMatthew G Knepley   if (name) {
1984cb1e1211SMatthew G Knepley     PetscValidPointer(name, 4);
1985cb1e1211SMatthew G Knepley     *name = b->name;
1986cb1e1211SMatthew G Knepley   }
198763d5297fSMatthew G. Knepley   if (labelname) {
198863d5297fSMatthew G. Knepley     PetscValidPointer(labelname, 5);
198963d5297fSMatthew G. Knepley     *labelname = b->labelname;
199063d5297fSMatthew G. Knepley   }
1991cb1e1211SMatthew G Knepley   if (field) {
199263d5297fSMatthew G. Knepley     PetscValidPointer(field, 6);
1993cb1e1211SMatthew G Knepley     *field = b->field;
1994cb1e1211SMatthew G Knepley   }
1995cb1e1211SMatthew G Knepley   if (func) {
199663d5297fSMatthew G. Knepley     PetscValidPointer(func, 7);
1997cb1e1211SMatthew G Knepley     *func = b->func;
1998cb1e1211SMatthew G Knepley   }
1999cb1e1211SMatthew G Knepley   if (numids) {
200063d5297fSMatthew G. Knepley     PetscValidPointer(numids, 8);
2001cb1e1211SMatthew G Knepley     *numids = b->numids;
2002cb1e1211SMatthew G Knepley   }
2003cb1e1211SMatthew G Knepley   if (ids) {
200463d5297fSMatthew G. Knepley     PetscValidPointer(ids, 9);
2005cb1e1211SMatthew G Knepley     *ids = b->ids;
2006cb1e1211SMatthew G Knepley   }
2007cb1e1211SMatthew G Knepley   if (ctx) {
200863d5297fSMatthew G. Knepley     PetscValidPointer(ctx, 10);
2009cb1e1211SMatthew G Knepley     *ctx = b->ctx;
2010cb1e1211SMatthew G Knepley   }
2011cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2012cb1e1211SMatthew G Knepley }
20130225b034SMatthew G. Knepley 
20140225b034SMatthew G. Knepley #undef __FUNCT__
20150225b034SMatthew G. Knepley #define __FUNCT__ "DMPlexIsBoundaryPoint"
20160225b034SMatthew G. Knepley PetscErrorCode DMPlexIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
20170225b034SMatthew G. Knepley {
20180225b034SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
20190225b034SMatthew G. Knepley   DMBoundary     b    = mesh->boundary;
20200225b034SMatthew G. Knepley   PetscErrorCode ierr;
20210225b034SMatthew G. Knepley 
20220225b034SMatthew G. Knepley   PetscFunctionBegin;
20230225b034SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20240225b034SMatthew G. Knepley   PetscValidPointer(isBd, 3);
20250225b034SMatthew G. Knepley   *isBd = PETSC_FALSE;
20260225b034SMatthew G. Knepley   while (b && !(*isBd)) {
20270225b034SMatthew G. Knepley     if (b->label) {
20280225b034SMatthew G. Knepley       PetscInt i;
20290225b034SMatthew G. Knepley 
20300225b034SMatthew G. Knepley       for (i = 0; i < b->numids && !(*isBd); ++i) {
20310225b034SMatthew G. Knepley         ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr);
20320225b034SMatthew G. Knepley       }
20330225b034SMatthew G. Knepley     }
20340225b034SMatthew G. Knepley     b = b->next;
20350225b034SMatthew G. Knepley   }
20360225b034SMatthew G. Knepley   PetscFunctionReturn(0);
20370225b034SMatthew G. Knepley }
2038