Actual source code: fe.c

  1: /* Basis Jet Tabulation

  3: We would like to tabulate the nodal basis functions and derivatives at a set of points, usually quadrature points. We
  4: follow here the derviation in http://www.math.ttu.edu/~kirby/papers/fiat-toms-2004.pdf. The nodal basis $\psi_i$ can
  5: be expressed in terms of a prime basis $\phi_i$ which can be stably evaluated. In PETSc, we will use the Legendre basis
  6: as a prime basis.

  8:   \psi_i = \sum_k \alpha_{ki} \phi_k

 10: Our nodal basis is defined in terms of the dual basis $n_j$

 12:   n_j \cdot \psi_i = \delta_{ji}

 14: and we may act on the first equation to obtain

 16:   n_j \cdot \psi_i = \sum_k \alpha_{ki} n_j \cdot \phi_k
 17:        \delta_{ji} = \sum_k \alpha_{ki} V_{jk}
 18:                  I = V \alpha

 20: so the coefficients of the nodal basis in the prime basis are

 22:    \alpha = V^{-1}

 24: We will define the dual basis vectors $n_j$ using a quadrature rule.

 26: Right now, we will just use the polynomial spaces P^k. I know some elements use the space of symmetric polynomials
 27: (I think Nedelec), but we will neglect this for now. Constraints in the space, e.g. Arnold-Winther elements, can
 28: be implemented exactly as in FIAT using functionals $L_j$.

 30: I will have to count the degrees correctly for the Legendre product when we are on simplices.

 32: We will have three objects:
 33:  - Space, P: this just need point evaluation I think
 34:  - Dual Space, P'+K: This looks like a set of functionals that can act on members of P, each n is defined by a Q
 35:  - FEM: This keeps {P, P', Q}
 36: */
 37: #include <petsc/private/petscfeimpl.h>
 38: #include <petscdmplex.h>

 40: PetscBool  FEcite       = PETSC_FALSE;
 41: const char FECitation[] = "@article{kirby2004,\n"
 42:                           "  title   = {Algorithm 839: FIAT, a New Paradigm for Computing Finite Element Basis Functions},\n"
 43:                           "  journal = {ACM Transactions on Mathematical Software},\n"
 44:                           "  author  = {Robert C. Kirby},\n"
 45:                           "  volume  = {30},\n"
 46:                           "  number  = {4},\n"
 47:                           "  pages   = {502--516},\n"
 48:                           "  doi     = {10.1145/1039813.1039820},\n"
 49:                           "  year    = {2004}\n}\n";

 51: PetscClassId PETSCFE_CLASSID = 0;

 53: PetscLogEvent PETSCFE_SetUp;

 55: PetscFunctionList PetscFEList              = NULL;
 56: PetscBool         PetscFERegisterAllCalled = PETSC_FALSE;

 58: /*@C
 59:   PetscFERegister - Adds a new `PetscFEType`

 61:   Not Collective, No Fortran Support

 63:   Input Parameters:
 64: + sname    - The name of a new user-defined creation routine
 65: - function - The creation routine

 67:   Example Usage:
 68: .vb
 69:     PetscFERegister("my_fe", MyPetscFECreate);
 70: .ve

 72:   Then, your PetscFE type can be chosen with the procedural interface via
 73: .vb
 74:     PetscFECreate(MPI_Comm, PetscFE *);
 75:     PetscFESetType(PetscFE, "my_fe");
 76: .ve
 77:   or at runtime via the option
 78: .vb
 79:     -petscfe_type my_fe
 80: .ve

 82:   Level: advanced

 84:   Note:
 85:   `PetscFERegister()` may be called multiple times to add several user-defined `PetscFE`s

 87: .seealso: `PetscFE`, `PetscFEType`, `PetscFERegisterAll()`, `PetscFERegisterDestroy()`
 88: @*/
 89: PetscErrorCode PetscFERegister(const char sname[], PetscErrorCode (*function)(PetscFE))
 90: {
 91:   PetscFunctionBegin;
 92:   PetscCall(PetscFunctionListAdd(&PetscFEList, sname, function));
 93:   PetscFunctionReturn(PETSC_SUCCESS);
 94: }

 96: /*@
 97:   PetscFESetType - Builds a particular `PetscFE`

 99:   Collective

101:   Input Parameters:
102: + fem  - The `PetscFE` object
103: - name - The kind of FEM space

105:   Options Database Key:
106: . -petscfe_type type - Sets the `PetscFE` type; use -help for a list of available types

108:   Level: intermediate

110: .seealso: `PetscFEType`, `PetscFE`, `PetscFEGetType()`, `PetscFECreate()`
111: @*/
112: PetscErrorCode PetscFESetType(PetscFE fem, PetscFEType name)
113: {
114:   PetscErrorCode (*r)(PetscFE);
115:   PetscBool match;

117:   PetscFunctionBegin;
119:   PetscCall(PetscObjectTypeCompare((PetscObject)fem, name, &match));
120:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

122:   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());
123:   PetscCall(PetscFunctionListFind(PetscFEList, name, &r));
124:   PetscCheck(r, PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscFE type: %s", name);

126:   PetscTryTypeMethod(fem, destroy);
127:   fem->ops->destroy = NULL;

129:   PetscCall((*r)(fem));
130:   PetscCall(PetscObjectChangeTypeName((PetscObject)fem, name));
131:   PetscFunctionReturn(PETSC_SUCCESS);
132: }

134: /*@
135:   PetscFEGetType - Gets the `PetscFEType` (as a string) from the `PetscFE` object.

137:   Not Collective

139:   Input Parameter:
140: . fem - The `PetscFE`

142:   Output Parameter:
143: . name - The `PetscFEType` name

145:   Level: intermediate

147: .seealso: `PetscFEType`, `PetscFE`, `PetscFESetType()`, `PetscFECreate()`
148: @*/
149: PetscErrorCode PetscFEGetType(PetscFE fem, PetscFEType *name)
150: {
151:   PetscFunctionBegin;
153:   PetscAssertPointer(name, 2);
154:   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());
155:   *name = ((PetscObject)fem)->type_name;
156:   PetscFunctionReturn(PETSC_SUCCESS);
157: }

159: /*@
160:   PetscFEViewFromOptions - View from a `PetscFE` based on values in the options database

162:   Collective

164:   Input Parameters:
165: + A    - the `PetscFE` object
166: . obj  - Optional object that provides the options prefix, pass `NULL` to use the options prefix of `A`
167: - name - command line option name

169:   Options Database Key:
170: . -name [viewertype][:...] - option name and values. See `PetscObjectViewFromOptions()` for the possible arguments

172:   Level: intermediate

174: .seealso: `PetscFE`, `PetscFEView()`, `PetscObjectViewFromOptions()`, `PetscFECreate()`
175: @*/
176: PetscErrorCode PetscFEViewFromOptions(PetscFE A, PeOp PetscObject obj, const char name[])
177: {
178:   PetscFunctionBegin;
180:   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
181:   PetscFunctionReturn(PETSC_SUCCESS);
182: }

184: /*@
185:   PetscFEView - Views a `PetscFE`

187:   Collective

189:   Input Parameters:
190: + fem    - the `PetscFE` object to view
191: - viewer - the viewer

193:   Level: beginner

195: .seealso: `PetscFE`, `PetscViewer`, `PetscFEDestroy()`, `PetscFEViewFromOptions()`
196: @*/
197: PetscErrorCode PetscFEView(PetscFE fem, PetscViewer viewer)
198: {
199:   PetscBool isascii;

201:   PetscFunctionBegin;
204:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)fem), &viewer));
205:   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)fem, viewer));
206:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
207:   PetscTryTypeMethod(fem, view, viewer);
208:   PetscFunctionReturn(PETSC_SUCCESS);
209: }

211: /*@
212:   PetscFESetFromOptions - sets parameters in a `PetscFE` from the options database

214:   Collective

216:   Input Parameter:
217: . fem - the `PetscFE` object to set options for

219:   Options Database Keys:
220: + -petscfe_num_blocks  - the number of cell blocks to integrate concurrently
221: - -petscfe_num_batches - the number of cell batches to integrate serially

223:   Level: intermediate

225: .seealso: `PetscFE`, `PetscFEView()`
226: @*/
227: PetscErrorCode PetscFESetFromOptions(PetscFE fem)
228: {
229:   const char *defaultType;
230:   char        name[256];
231:   PetscBool   flg;

233:   PetscFunctionBegin;
235:   if (!((PetscObject)fem)->type_name) defaultType = PETSCFEBASIC;
236:   else defaultType = ((PetscObject)fem)->type_name;
237:   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());

239:   PetscObjectOptionsBegin((PetscObject)fem);
240:   PetscCall(PetscOptionsFList("-petscfe_type", "Finite element space", "PetscFESetType", PetscFEList, defaultType, name, 256, &flg));
241:   if (flg) PetscCall(PetscFESetType(fem, name));
242:   else if (!((PetscObject)fem)->type_name) PetscCall(PetscFESetType(fem, defaultType));
243:   PetscCall(PetscOptionsBoundedInt("-petscfe_num_blocks", "The number of cell blocks to integrate concurrently", "PetscSpaceSetTileSizes", fem->numBlocks, &fem->numBlocks, NULL, 1));
244:   PetscCall(PetscOptionsBoundedInt("-petscfe_num_batches", "The number of cell batches to integrate serially", "PetscSpaceSetTileSizes", fem->numBatches, &fem->numBatches, NULL, 1));
245:   PetscTryTypeMethod(fem, setfromoptions, PetscOptionsObject);
246:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
247:   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)fem, PetscOptionsObject));
248:   PetscOptionsEnd();
249:   PetscCall(PetscFEViewFromOptions(fem, NULL, "-petscfe_view"));
250:   PetscFunctionReturn(PETSC_SUCCESS);
251: }

253: /*@
254:   PetscFESetUp - Construct data structures for the `PetscFE` after the `PetscFEType` has been set

256:   Collective

258:   Input Parameter:
259: . fem - the `PetscFE` object to setup

261:   Level: intermediate

263: .seealso: `PetscFE`, `PetscFEView()`, `PetscFEDestroy()`
264: @*/
265: PetscErrorCode PetscFESetUp(PetscFE fem)
266: {
267:   PetscFunctionBegin;
269:   if (fem->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
270:   PetscCall(PetscLogEventBegin(PETSCFE_SetUp, fem, 0, 0, 0));
271:   fem->setupcalled = PETSC_TRUE;
272:   PetscTryTypeMethod(fem, setup);
273:   PetscCall(PetscLogEventEnd(PETSCFE_SetUp, fem, 0, 0, 0));
274:   PetscFunctionReturn(PETSC_SUCCESS);
275: }

277: /*@
278:   PetscFEDestroy - Destroys a `PetscFE` object

280:   Collective

282:   Input Parameter:
283: . fem - the `PetscFE` object to destroy

285:   Level: beginner

287: .seealso: `PetscFE`, `PetscFEView()`
288: @*/
289: PetscErrorCode PetscFEDestroy(PetscFE *fem)
290: {
291:   PetscFunctionBegin;
292:   if (!*fem) PetscFunctionReturn(PETSC_SUCCESS);

295:   if (--((PetscObject)*fem)->refct > 0) {
296:     *fem = NULL;
297:     PetscFunctionReturn(PETSC_SUCCESS);
298:   }
299:   ((PetscObject)*fem)->refct = 0;

301:   if ((*fem)->subspaces) {
302:     PetscInt dim, d;

304:     PetscCall(PetscDualSpaceGetDimension((*fem)->dualSpace, &dim));
305:     for (d = 0; d < dim; ++d) PetscCall(PetscFEDestroy(&(*fem)->subspaces[d]));
306:   }
307:   PetscCall(PetscFree((*fem)->subspaces));
308:   PetscCall(PetscFree((*fem)->invV));
309:   PetscCall(PetscTabulationDestroy(&(*fem)->T));
310:   PetscCall(PetscTabulationDestroy(&(*fem)->Tf));
311:   PetscCall(PetscTabulationDestroy(&(*fem)->Tc));
312:   PetscCall(PetscSpaceDestroy(&(*fem)->basisSpace));
313:   PetscCall(PetscDualSpaceDestroy(&(*fem)->dualSpace));
314:   PetscCall(PetscQuadratureDestroy(&(*fem)->quadrature));
315:   PetscCall(PetscQuadratureDestroy(&(*fem)->faceQuadrature));
316: #ifdef PETSC_HAVE_LIBCEED
317:   PetscCallCEED(CeedBasisDestroy(&(*fem)->ceedBasis));
318:   PetscCallCEED(CeedDestroy(&(*fem)->ceed));
319: #endif

321:   PetscTryTypeMethod(*fem, destroy);
322:   PetscCall(PetscHeaderDestroy(fem));
323:   PetscFunctionReturn(PETSC_SUCCESS);
324: }

326: /*@
327:   PetscFECreate - Creates an empty `PetscFE` object. The type can then be set with `PetscFESetType()`.

329:   Collective

331:   Input Parameter:
332: . comm - The communicator for the `PetscFE` object

334:   Output Parameter:
335: . fem - The `PetscFE` object

337:   Level: beginner

339: .seealso: `PetscFE`, `PetscFEType`, `PetscFESetType()`, `PetscFECreateDefault()`, `PETSCFEGALERKIN`
340: @*/
341: PetscErrorCode PetscFECreate(MPI_Comm comm, PetscFE *fem)
342: {
343:   PetscFE f;

345:   PetscFunctionBegin;
346:   PetscAssertPointer(fem, 2);
347:   PetscCall(PetscCitationsRegister(FECitation, &FEcite));
348:   PetscCall(PetscFEInitializePackage());

350:   PetscCall(PetscHeaderCreate(f, PETSCFE_CLASSID, "PetscFE", "Finite Element", "PetscFE", comm, PetscFEDestroy, PetscFEView));

352:   f->basisSpace    = NULL;
353:   f->dualSpace     = NULL;
354:   f->numComponents = 1;
355:   f->subspaces     = NULL;
356:   f->invV          = NULL;
357:   f->T             = NULL;
358:   f->Tf            = NULL;
359:   f->Tc            = NULL;
360:   PetscCall(PetscArrayzero(&f->quadrature, 1));
361:   PetscCall(PetscArrayzero(&f->faceQuadrature, 1));
362:   f->blockSize  = 0;
363:   f->numBlocks  = 1;
364:   f->batchSize  = 0;
365:   f->numBatches = 1;

367:   *fem = f;
368:   PetscFunctionReturn(PETSC_SUCCESS);
369: }

371: /*@
372:   PetscFEGetSpatialDimension - Returns the spatial dimension of the element

374:   Not Collective

376:   Input Parameter:
377: . fem - The `PetscFE` object

379:   Output Parameter:
380: . dim - The spatial dimension

382:   Level: intermediate

384: .seealso: `PetscFE`, `PetscFECreate()`
385: @*/
386: PetscErrorCode PetscFEGetSpatialDimension(PetscFE fem, PetscInt *dim)
387: {
388:   DM dm;

390:   PetscFunctionBegin;
392:   PetscAssertPointer(dim, 2);
393:   PetscCall(PetscDualSpaceGetDM(fem->dualSpace, &dm));
394:   PetscCall(DMGetDimension(dm, dim));
395:   PetscFunctionReturn(PETSC_SUCCESS);
396: }

398: /*@
399:   PetscFESetNumComponents - Sets the number of field components in the element

401:   Not Collective

403:   Input Parameters:
404: + fem  - The `PetscFE` object
405: - comp - The number of field components

407:   Level: intermediate

409: .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetSpatialDimension()`, `PetscFEGetNumComponents()`
410: @*/
411: PetscErrorCode PetscFESetNumComponents(PetscFE fem, PetscInt comp)
412: {
413:   PetscFunctionBegin;
415:   fem->numComponents = comp;
416:   PetscFunctionReturn(PETSC_SUCCESS);
417: }

419: /*@
420:   PetscFEGetNumComponents - Returns the number of components in the element

422:   Not Collective

424:   Input Parameter:
425: . fem - The `PetscFE` object

427:   Output Parameter:
428: . comp - The number of field components

430:   Level: intermediate

432: .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetSpatialDimension()`
433: @*/
434: PetscErrorCode PetscFEGetNumComponents(PetscFE fem, PetscInt *comp)
435: {
436:   PetscFunctionBegin;
438:   PetscAssertPointer(comp, 2);
439:   *comp = fem->numComponents;
440:   PetscFunctionReturn(PETSC_SUCCESS);
441: }

443: /*@
444:   PetscFESetTileSizes - Sets the tile sizes for evaluation

446:   Not Collective

448:   Input Parameters:
449: + fem        - The `PetscFE` object
450: . blockSize  - The number of elements in a block
451: . numBlocks  - The number of blocks in a batch
452: . batchSize  - The number of elements in a batch
453: - numBatches - The number of batches in a chunk

455:   Level: intermediate

457: .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetTileSizes()`
458: @*/
459: PetscErrorCode PetscFESetTileSizes(PetscFE fem, PetscInt blockSize, PetscInt numBlocks, PetscInt batchSize, PetscInt numBatches)
460: {
461:   PetscFunctionBegin;
463:   fem->blockSize  = blockSize;
464:   fem->numBlocks  = numBlocks;
465:   fem->batchSize  = batchSize;
466:   fem->numBatches = numBatches;
467:   PetscFunctionReturn(PETSC_SUCCESS);
468: }

470: /*@
471:   PetscFEGetTileSizes - Returns the tile sizes for evaluation

473:   Not Collective

475:   Input Parameter:
476: . fem - The `PetscFE` object

478:   Output Parameters:
479: + blockSize  - The number of elements in a block, pass `NULL` if not needed
480: . numBlocks  - The number of blocks in a batch, pass `NULL` if not needed
481: . batchSize  - The number of elements in a batch, pass `NULL` if not needed
482: - numBatches - The number of batches in a chunk, pass `NULL` if not needed

484:   Level: intermediate

486: .seealso: `PetscFE`, `PetscFECreate()`, `PetscFESetTileSizes()`
487: @*/
488: PetscErrorCode PetscFEGetTileSizes(PetscFE fem, PeOp PetscInt *blockSize, PeOp PetscInt *numBlocks, PeOp PetscInt *batchSize, PeOp PetscInt *numBatches)
489: {
490:   PetscFunctionBegin;
492:   if (blockSize) PetscAssertPointer(blockSize, 2);
493:   if (numBlocks) PetscAssertPointer(numBlocks, 3);
494:   if (batchSize) PetscAssertPointer(batchSize, 4);
495:   if (numBatches) PetscAssertPointer(numBatches, 5);
496:   if (blockSize) *blockSize = fem->blockSize;
497:   if (numBlocks) *numBlocks = fem->numBlocks;
498:   if (batchSize) *batchSize = fem->batchSize;
499:   if (numBatches) *numBatches = fem->numBatches;
500:   PetscFunctionReturn(PETSC_SUCCESS);
501: }

503: /*@
504:   PetscFEGetBasisSpace - Returns the `PetscSpace` used for the approximation of the solution for the `PetscFE`

506:   Not Collective

508:   Input Parameter:
509: . fem - The `PetscFE` object

511:   Output Parameter:
512: . sp - The `PetscSpace` object

514:   Level: intermediate

516: .seealso: `PetscFE`, `PetscSpace`, `PetscFECreate()`
517: @*/
518: PetscErrorCode PetscFEGetBasisSpace(PetscFE fem, PetscSpace *sp)
519: {
520:   PetscFunctionBegin;
522:   PetscAssertPointer(sp, 2);
523:   *sp = fem->basisSpace;
524:   PetscFunctionReturn(PETSC_SUCCESS);
525: }

527: /*@
528:   PetscFESetBasisSpace - Sets the `PetscSpace` used for the approximation of the solution

530:   Not Collective

532:   Input Parameters:
533: + fem - The `PetscFE` object
534: - sp  - The `PetscSpace` object

536:   Level: intermediate

538:   Developer Notes:
539:   There is `PetscFESetBasisSpace()` but the `PetscFESetDualSpace()`, likely the Basis is unneeded in the function name

541: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`, `PetscFESetDualSpace()`
542: @*/
543: PetscErrorCode PetscFESetBasisSpace(PetscFE fem, PetscSpace sp)
544: {
545:   PetscFunctionBegin;
548:   PetscCall(PetscSpaceDestroy(&fem->basisSpace));
549:   fem->basisSpace = sp;
550:   PetscCall(PetscObjectReference((PetscObject)fem->basisSpace));
551:   PetscFunctionReturn(PETSC_SUCCESS);
552: }

554: /*@
555:   PetscFEGetDualSpace - Returns the `PetscDualSpace` used to define the inner product for a `PetscFE`

557:   Not Collective

559:   Input Parameter:
560: . fem - The `PetscFE` object

562:   Output Parameter:
563: . sp - The `PetscDualSpace` object

565:   Level: intermediate

567: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`
568: @*/
569: PetscErrorCode PetscFEGetDualSpace(PetscFE fem, PetscDualSpace *sp)
570: {
571:   PetscFunctionBegin;
573:   PetscAssertPointer(sp, 2);
574:   *sp = fem->dualSpace;
575:   PetscFunctionReturn(PETSC_SUCCESS);
576: }

578: /*@
579:   PetscFESetDualSpace - Sets the `PetscDualSpace` used to define the inner product

581:   Not Collective

583:   Input Parameters:
584: + fem - The `PetscFE` object
585: - sp  - The `PetscDualSpace` object

587:   Level: intermediate

589: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`, `PetscFESetBasisSpace()`
590: @*/
591: PetscErrorCode PetscFESetDualSpace(PetscFE fem, PetscDualSpace sp)
592: {
593:   PetscFunctionBegin;
596:   PetscCall(PetscDualSpaceDestroy(&fem->dualSpace));
597:   fem->dualSpace = sp;
598:   PetscCall(PetscObjectReference((PetscObject)fem->dualSpace));
599:   PetscFunctionReturn(PETSC_SUCCESS);
600: }

602: /*@
603:   PetscFEGetQuadrature - Returns the `PetscQuadrature` used to calculate inner products

605:   Not Collective

607:   Input Parameter:
608: . fem - The `PetscFE` object

610:   Output Parameter:
611: . q - The `PetscQuadrature` object

613:   Level: intermediate

615: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`
616: @*/
617: PetscErrorCode PetscFEGetQuadrature(PetscFE fem, PetscQuadrature *q)
618: {
619:   PetscFunctionBegin;
621:   PetscAssertPointer(q, 2);
622:   *q = fem->quadrature;
623:   PetscFunctionReturn(PETSC_SUCCESS);
624: }

626: /*@
627:   PetscFESetQuadrature - Sets the `PetscQuadrature` used to calculate inner products

629:   Not Collective

631:   Input Parameters:
632: + fem - The `PetscFE` object
633: - q   - The `PetscQuadrature` object

635:   Level: intermediate

637: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFEGetFaceQuadrature()`
638: @*/
639: PetscErrorCode PetscFESetQuadrature(PetscFE fem, PetscQuadrature q)
640: {
641:   PetscInt Nc, qNc;

643:   PetscFunctionBegin;
645:   if (q == fem->quadrature) PetscFunctionReturn(PETSC_SUCCESS);
646:   PetscCall(PetscFEGetNumComponents(fem, &Nc));
647:   PetscCall(PetscQuadratureGetNumComponents(q, &qNc));
648:   PetscCheck(!(qNc != 1) || !(Nc != qNc), PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_SIZ, "FE components %" PetscInt_FMT " != Quadrature components %" PetscInt_FMT " and non-scalar quadrature", Nc, qNc);
649:   PetscCall(PetscTabulationDestroy(&fem->T));
650:   PetscCall(PetscTabulationDestroy(&fem->Tc));
651:   PetscCall(PetscObjectReference((PetscObject)q));
652:   PetscCall(PetscQuadratureDestroy(&fem->quadrature));
653:   fem->quadrature = q;
654:   PetscFunctionReturn(PETSC_SUCCESS);
655: }

657: /*@
658:   PetscFEGetFaceQuadrature - Returns the `PetscQuadrature` used to calculate inner products on faces

660:   Not Collective

662:   Input Parameter:
663: . fem - The `PetscFE` object

665:   Output Parameter:
666: . q - The `PetscQuadrature` object

668:   Level: intermediate

670:   Developer Notes:
671:   There is a special face quadrature but not edge, likely this API would benefit from a refactorization

673: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`, `PetscFESetFaceQuadrature()`
674: @*/
675: PetscErrorCode PetscFEGetFaceQuadrature(PetscFE fem, PetscQuadrature *q)
676: {
677:   PetscFunctionBegin;
679:   PetscAssertPointer(q, 2);
680:   *q = fem->faceQuadrature;
681:   PetscFunctionReturn(PETSC_SUCCESS);
682: }

684: /*@
685:   PetscFESetFaceQuadrature - Sets the `PetscQuadrature` used to calculate inner products on faces

687:   Not Collective

689:   Input Parameters:
690: + fem - The `PetscFE` object
691: - q   - The `PetscQuadrature` object

693:   Level: intermediate

695: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`
696: @*/
697: PetscErrorCode PetscFESetFaceQuadrature(PetscFE fem, PetscQuadrature q)
698: {
699:   PetscInt Nc, qNc;

701:   PetscFunctionBegin;
703:   if (q == fem->faceQuadrature) PetscFunctionReturn(PETSC_SUCCESS);
704:   PetscCall(PetscFEGetNumComponents(fem, &Nc));
705:   PetscCall(PetscQuadratureGetNumComponents(q, &qNc));
706:   PetscCheck(!(qNc != 1) || !(Nc != qNc), PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_SIZ, "FE components %" PetscInt_FMT " != Quadrature components %" PetscInt_FMT " and non-scalar quadrature", Nc, qNc);
707:   PetscCall(PetscTabulationDestroy(&fem->Tf));
708:   PetscCall(PetscObjectReference((PetscObject)q));
709:   PetscCall(PetscQuadratureDestroy(&fem->faceQuadrature));
710:   fem->faceQuadrature = q;
711:   PetscFunctionReturn(PETSC_SUCCESS);
712: }

714: /*@
715:   PetscFECopyQuadrature - Copy both volumetric and surface quadrature to a new `PetscFE`

717:   Not Collective

719:   Input Parameters:
720: + sfe - The `PetscFE` source for the quadratures
721: - tfe - The `PetscFE` target for the quadratures

723:   Level: intermediate

725: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`, `PetscFESetFaceQuadrature()`
726: @*/
727: PetscErrorCode PetscFECopyQuadrature(PetscFE sfe, PetscFE tfe)
728: {
729:   PetscQuadrature q;

731:   PetscFunctionBegin;
734:   PetscCall(PetscFEGetQuadrature(sfe, &q));
735:   PetscCall(PetscFESetQuadrature(tfe, q));
736:   PetscCall(PetscFEGetFaceQuadrature(sfe, &q));
737:   PetscCall(PetscFESetFaceQuadrature(tfe, q));
738:   PetscFunctionReturn(PETSC_SUCCESS);
739: }

741: /*@C
742:   PetscFEGetNumDof - Returns the number of dofs (dual basis vectors) associated to mesh points on the reference cell of a given dimension

744:   Not Collective

746:   Input Parameter:
747: . fem - The `PetscFE` object

749:   Output Parameter:
750: . numDof - Array of length `dim` with the number of dofs in each dimension

752:   Level: intermediate

754: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`
755: @*/
756: PetscErrorCode PetscFEGetNumDof(PetscFE fem, const PetscInt *numDof[])
757: {
758:   PetscFunctionBegin;
760:   PetscAssertPointer(numDof, 2);
761:   PetscCall(PetscDualSpaceGetNumDof(fem->dualSpace, numDof));
762:   PetscFunctionReturn(PETSC_SUCCESS);
763: }

765: /*@C
766:   PetscFEGetCellTabulation - Returns the tabulation of the basis functions at the quadrature points on the reference cell

768:   Not Collective

770:   Input Parameters:
771: + fem - The `PetscFE` object
772: - k   - The highest derivative we need to tabulate, very often 1

774:   Output Parameter:
775: . T - The basis function values and derivatives at quadrature points

777:   Level: intermediate

779:   Note:
780: .vb
781:   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
782:   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
783:   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
784: .ve

786: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
787: @*/
788: PetscErrorCode PetscFEGetCellTabulation(PetscFE fem, PetscInt k, PetscTabulation *T)
789: {
790:   PetscInt         npoints;
791:   const PetscReal *points;

793:   PetscFunctionBegin;
795:   PetscAssertPointer(T, 3);
796:   PetscCall(PetscQuadratureGetData(fem->quadrature, NULL, NULL, &npoints, &points, NULL));
797:   if (!fem->T) PetscCall(PetscFECreateTabulation(fem, 1, npoints, points, k, &fem->T));
798:   PetscCheck(!fem->T || k <= fem->T->K || (!fem->T->cdim && !fem->T->K), PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_OUTOFRANGE, "Requested %" PetscInt_FMT " derivatives, but only tabulated %" PetscInt_FMT, k, fem->T->K);
799:   *T = fem->T;
800:   PetscFunctionReturn(PETSC_SUCCESS);
801: }

803: PetscErrorCode PetscFEExpandFaceQuadrature(PetscFE fe, PetscQuadrature fq, PetscQuadrature *efq)
804: {
805:   DM               dm;
806:   PetscDualSpace   sp;
807:   const PetscInt  *faces;
808:   const PetscReal *points, *weights;
809:   DMPolytopeType   ct;
810:   PetscReal       *facePoints, *faceWeights;
811:   PetscInt         dim, cStart, Nf, Nc, Np, order;

813:   PetscFunctionBegin;
814:   PetscCall(PetscFEGetDualSpace(fe, &sp));
815:   PetscCall(PetscDualSpaceGetDM(sp, &dm));
816:   PetscCall(DMGetDimension(dm, &dim));
817:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
818:   PetscCall(DMPlexGetConeSize(dm, cStart, &Nf));
819:   PetscCall(DMPlexGetCone(dm, cStart, &faces));
820:   PetscCall(PetscQuadratureGetData(fq, NULL, &Nc, &Np, &points, &weights));
821:   PetscCall(PetscMalloc1(Nf * Np * dim, &facePoints));
822:   PetscCall(PetscMalloc1(Nf * Np * Nc, &faceWeights));
823:   for (PetscInt f = 0; f < Nf; ++f) {
824:     const PetscReal xi0[3] = {-1., -1., -1.};
825:     PetscReal       v0[3], J[9], detJ;

827:     PetscCall(DMPlexComputeCellGeometryFEM(dm, faces[f], NULL, v0, J, NULL, &detJ));
828:     for (PetscInt q = 0; q < Np; ++q) {
829:       CoordinatesRefToReal(dim, dim - 1, xi0, v0, J, &points[q * (dim - 1)], &facePoints[(f * Np + q) * dim]);
830:       for (PetscInt c = 0; c < Nc; ++c) faceWeights[(f * Np + q) * Nc + c] = weights[q * Nc + c];
831:     }
832:   }
833:   PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)fq), efq));
834:   PetscCall(PetscQuadratureGetCellType(fq, &ct));
835:   PetscCall(PetscQuadratureSetCellType(*efq, ct));
836:   PetscCall(PetscQuadratureGetOrder(fq, &order));
837:   PetscCall(PetscQuadratureSetOrder(*efq, order));
838:   PetscCall(PetscQuadratureSetData(*efq, dim, Nc, Nf * Np, facePoints, faceWeights));
839:   PetscFunctionReturn(PETSC_SUCCESS);
840: }

842: /*@C
843:   PetscFEGetFaceTabulation - Returns the tabulation of the basis functions at the face quadrature points for each face of the reference cell

845:   Not Collective

847:   Input Parameters:
848: + fem - The `PetscFE` object
849: - k   - The highest derivative we need to tabulate, very often 1

851:   Output Parameter:
852: . Tf - The basis function values and derivatives at face quadrature points

854:   Level: intermediate

856:   Note:
857: .vb
858:   T->T[0] = Bf[((f*Nq + q)*pdim + i)*Nc + c] is the value at point f,q for basis function i and component c
859:   T->T[1] = Df[(((f*Nq + q)*pdim + i)*Nc + c)*dim + d] is the derivative value at point f,q for basis function i, component c, in direction d
860:   T->T[2] = Hf[((((f*Nq + q)*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point f,q for basis function i, component c, in directions d and e
861: .ve

863: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
864: @*/
865: PetscErrorCode PetscFEGetFaceTabulation(PetscFE fem, PetscInt k, PetscTabulation *Tf)
866: {
867:   PetscFunctionBegin;
869:   PetscAssertPointer(Tf, 3);
870:   if (!fem->Tf) {
871:     PetscQuadrature fq;

873:     PetscCall(PetscFEGetFaceQuadrature(fem, &fq));
874:     if (fq) {
875:       PetscQuadrature  efq;
876:       const PetscReal *facePoints;
877:       PetscInt         Np, eNp;

879:       PetscCall(PetscFEExpandFaceQuadrature(fem, fq, &efq));
880:       PetscCall(PetscQuadratureGetData(fq, NULL, NULL, &Np, NULL, NULL));
881:       PetscCall(PetscQuadratureGetData(efq, NULL, NULL, &eNp, &facePoints, NULL));
882:       if (PetscDefined(USE_DEBUG)) {
883:         PetscDualSpace sp;
884:         DM             dm;
885:         PetscInt       cStart, Nf;

887:         PetscCall(PetscFEGetDualSpace(fem, &sp));
888:         PetscCall(PetscDualSpaceGetDM(sp, &dm));
889:         PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
890:         PetscCall(DMPlexGetConeSize(dm, cStart, &Nf));
891:         PetscCheck(Nf == eNp / Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number of faces %" PetscInt_FMT " != %" PetscInt_FMT " number of quadrature replicas", Nf, eNp / Np);
892:       }
893:       PetscCall(PetscFECreateTabulation(fem, eNp / Np, Np, facePoints, k, &fem->Tf));
894:       PetscCall(PetscQuadratureDestroy(&efq));
895:     }
896:   }
897:   PetscCheck(!fem->Tf || k <= fem->Tf->K, PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_OUTOFRANGE, "Requested %" PetscInt_FMT " derivatives, but only tabulated %" PetscInt_FMT, k, fem->Tf->K);
898:   *Tf = fem->Tf;
899:   PetscFunctionReturn(PETSC_SUCCESS);
900: }

902: /*@C
903:   PetscFEGetFaceCentroidTabulation - Returns the tabulation of the basis functions at the face centroid points

905:   Not Collective

907:   Input Parameter:
908: . fem - The `PetscFE` object

910:   Output Parameter:
911: . Tc - The basis function values at face centroid points

913:   Level: intermediate

915:   Note:
916: .vb
917:   T->T[0] = Bf[(f*pdim + i)*Nc + c] is the value at point f for basis function i and component c
918: .ve

920: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFEGetFaceTabulation()`, `PetscFEGetCellTabulation()`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
921: @*/
922: PetscErrorCode PetscFEGetFaceCentroidTabulation(PetscFE fem, PetscTabulation *Tc)
923: {
924:   PetscFunctionBegin;
926:   PetscAssertPointer(Tc, 2);
927:   if (!fem->Tc) {
928:     PetscDualSpace  sp;
929:     DM              dm;
930:     const PetscInt *cone;
931:     PetscReal      *centroids;
932:     PetscInt        dim, numFaces, f;

934:     PetscCall(PetscFEGetDualSpace(fem, &sp));
935:     PetscCall(PetscDualSpaceGetDM(sp, &dm));
936:     PetscCall(DMGetDimension(dm, &dim));
937:     PetscCall(DMPlexGetConeSize(dm, 0, &numFaces));
938:     PetscCall(DMPlexGetCone(dm, 0, &cone));
939:     PetscCall(PetscMalloc1(numFaces * dim, &centroids));
940:     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexComputeCellGeometryFVM(dm, cone[f], NULL, &centroids[f * dim], NULL));
941:     PetscCall(PetscFECreateTabulation(fem, 1, numFaces, centroids, 0, &fem->Tc));
942:     PetscCall(PetscFree(centroids));
943:   }
944:   *Tc = fem->Tc;
945:   PetscFunctionReturn(PETSC_SUCCESS);
946: }

948: /*@C
949:   PetscFECreateTabulation - Tabulates the basis functions, and perhaps derivatives, at the points provided.

951:   Not Collective

953:   Input Parameters:
954: + fem     - The `PetscFE` object
955: . nrepl   - The number of replicas
956: . npoints - The number of tabulation points in a replica
957: . points  - The tabulation point coordinates
958: - K       - The number of derivatives calculated

960:   Output Parameter:
961: . T - The basis function values and derivatives at tabulation points

963:   Level: intermediate

965:   Note:
966: .vb
967:   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
968:   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
969:   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis
970:   T->function i, component c, in directions d and e
971: .ve

973: .seealso: `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscTabulationDestroy()`
974: @*/
975: PetscErrorCode PetscFECreateTabulation(PetscFE fem, PetscInt nrepl, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation *T)
976: {
977:   DM             dm;
978:   PetscDualSpace Q;
979:   PetscInt       Nb;   /* Dimension of FE space P */
980:   PetscInt       Nc;   /* Field components */
981:   PetscInt       cdim; /* Reference coordinate dimension */
982:   PetscInt       k;

984:   PetscFunctionBegin;
985:   if (!npoints || !fem->dualSpace || K < 0) {
986:     *T = NULL;
987:     PetscFunctionReturn(PETSC_SUCCESS);
988:   }
990:   PetscAssertPointer(points, 4);
991:   PetscAssertPointer(T, 6);
992:   PetscCall(PetscFEGetDualSpace(fem, &Q));
993:   PetscCall(PetscDualSpaceGetDM(Q, &dm));
994:   PetscCall(DMGetDimension(dm, &cdim));
995:   PetscCall(PetscDualSpaceGetDimension(Q, &Nb));
996:   PetscCall(PetscFEGetNumComponents(fem, &Nc));
997:   PetscCall(PetscMalloc1(1, T));
998:   (*T)->K    = !cdim ? 0 : K;
999:   (*T)->Nr   = nrepl;
1000:   (*T)->Np   = npoints;
1001:   (*T)->Nb   = Nb;
1002:   (*T)->Nc   = Nc;
1003:   (*T)->cdim = cdim;
1004:   PetscCall(PetscMalloc1((*T)->K + 1, &(*T)->T));
1005:   for (k = 0; k <= (*T)->K; ++k) PetscCall(PetscCalloc1(nrepl * npoints * Nb * Nc * PetscPowInt(cdim, k), &(*T)->T[k]));
1006:   PetscUseTypeMethod(fem, computetabulation, nrepl * npoints, points, K, *T);
1007:   PetscFunctionReturn(PETSC_SUCCESS);
1008: }

1010: /*@C
1011:   PetscFEComputeTabulation - Tabulates the basis functions, and perhaps derivatives, at the points provided.

1013:   Not Collective

1015:   Input Parameters:
1016: + fem     - The `PetscFE` object
1017: . npoints - The number of tabulation points
1018: . points  - The tabulation point coordinates
1019: . K       - The number of derivatives calculated
1020: - T       - An existing tabulation object with enough allocated space

1022:   Output Parameter:
1023: . T - The basis function values and derivatives at tabulation points

1025:   Level: intermediate

1027:   Note:
1028: .vb
1029:   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
1030:   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
1031:   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
1032: .ve

1034: .seealso: `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscTabulationDestroy()`
1035: @*/
1036: PetscErrorCode PetscFEComputeTabulation(PetscFE fem, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation T)
1037: {
1038:   PetscFunctionBeginHot;
1039:   if (!npoints || !fem->dualSpace || K < 0) PetscFunctionReturn(PETSC_SUCCESS);
1041:   PetscAssertPointer(points, 3);
1042:   PetscAssertPointer(T, 5);
1043:   if (PetscDefined(USE_DEBUG)) {
1044:     DM             dm;
1045:     PetscDualSpace Q;
1046:     PetscInt       Nb;   /* Dimension of FE space P */
1047:     PetscInt       Nc;   /* Field components */
1048:     PetscInt       cdim; /* Reference coordinate dimension */

1050:     PetscCall(PetscFEGetDualSpace(fem, &Q));
1051:     PetscCall(PetscDualSpaceGetDM(Q, &dm));
1052:     PetscCall(DMGetDimension(dm, &cdim));
1053:     PetscCall(PetscDualSpaceGetDimension(Q, &Nb));
1054:     PetscCall(PetscFEGetNumComponents(fem, &Nc));
1055:     PetscCheck(T->K == (!cdim ? 0 : K), PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation K %" PetscInt_FMT " must match requested K %" PetscInt_FMT, T->K, !cdim ? 0 : K);
1056:     PetscCheck(T->Nb == Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation Nb %" PetscInt_FMT " must match requested Nb %" PetscInt_FMT, T->Nb, Nb);
1057:     PetscCheck(T->Nc == Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation Nc %" PetscInt_FMT " must match requested Nc %" PetscInt_FMT, T->Nc, Nc);
1058:     PetscCheck(T->cdim == cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation cdim %" PetscInt_FMT " must match requested cdim %" PetscInt_FMT, T->cdim, cdim);
1059:   }
1060:   T->Nr = 1;
1061:   T->Np = npoints;
1062:   PetscUseTypeMethod(fem, computetabulation, npoints, points, K, T);
1063:   PetscFunctionReturn(PETSC_SUCCESS);
1064: }

1066: /*@
1067:   PetscTabulationDestroy - Frees memory from the associated tabulation.

1069:   Not Collective

1071:   Input Parameter:
1072: . T - The tabulation

1074:   Level: intermediate

1076: .seealso: `PetscTabulation`, `PetscFECreateTabulation()`, `PetscFEGetCellTabulation()`
1077: @*/
1078: PetscErrorCode PetscTabulationDestroy(PetscTabulation *T)
1079: {
1080:   PetscInt k;

1082:   PetscFunctionBegin;
1083:   PetscAssertPointer(T, 1);
1084:   if (!T || !*T) PetscFunctionReturn(PETSC_SUCCESS);
1085:   for (k = 0; k <= (*T)->K; ++k) PetscCall(PetscFree((*T)->T[k]));
1086:   PetscCall(PetscFree((*T)->T));
1087:   PetscCall(PetscFree(*T));
1088:   *T = NULL;
1089:   PetscFunctionReturn(PETSC_SUCCESS);
1090: }

1092: static PetscErrorCode PetscFECreatePointTraceDefault_Internal(PetscFE fe, PetscInt refPoint, PetscFE *trFE)
1093: {
1094:   PetscSpace      bsp, bsubsp;
1095:   PetscDualSpace  dsp, dsubsp;
1096:   PetscInt        dim, depth, numComp, i, j, coneSize, order;
1097:   DM              dm;
1098:   DMLabel         label;
1099:   PetscReal      *xi, *v, *J, detJ;
1100:   const char     *name;
1101:   PetscQuadrature origin, fullQuad, subQuad;

1103:   PetscFunctionBegin;
1104:   PetscCall(PetscFEGetBasisSpace(fe, &bsp));
1105:   PetscCall(PetscFEGetDualSpace(fe, &dsp));
1106:   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
1107:   PetscCall(DMGetDimension(dm, &dim));
1108:   PetscCall(DMPlexGetDepthLabel(dm, &label));
1109:   PetscCall(DMLabelGetValue(label, refPoint, &depth));
1110:   PetscCall(PetscCalloc1(depth, &xi));
1111:   PetscCall(PetscMalloc1(dim, &v));
1112:   PetscCall(PetscMalloc1(dim * dim, &J));
1113:   for (i = 0; i < depth; i++) xi[i] = 0.;
1114:   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &origin));
1115:   PetscCall(PetscQuadratureSetData(origin, depth, 0, 1, xi, NULL));
1116:   PetscCall(DMPlexComputeCellGeometryFEM(dm, refPoint, origin, v, J, NULL, &detJ));
1117:   /* CellGeometryFEM computes the expanded Jacobian, we want the true jacobian */
1118:   for (i = 1; i < dim; i++) {
1119:     for (j = 0; j < depth; j++) J[i * depth + j] = J[i * dim + j];
1120:   }
1121:   PetscCall(PetscQuadratureDestroy(&origin));
1122:   PetscCall(PetscDualSpaceGetPointSubspace(dsp, refPoint, &dsubsp));
1123:   PetscCall(PetscSpaceCreateSubspace(bsp, dsubsp, v, J, NULL, NULL, PETSC_OWN_POINTER, &bsubsp));
1124:   PetscCall(PetscSpaceSetUp(bsubsp));
1125:   PetscCall(PetscFECreate(PetscObjectComm((PetscObject)fe), trFE));
1126:   PetscCall(PetscFESetType(*trFE, PETSCFEBASIC));
1127:   PetscCall(PetscFEGetNumComponents(fe, &numComp));
1128:   PetscCall(PetscFESetNumComponents(*trFE, numComp));
1129:   PetscCall(PetscFESetBasisSpace(*trFE, bsubsp));
1130:   PetscCall(PetscFESetDualSpace(*trFE, dsubsp));
1131:   PetscCall(PetscObjectGetName((PetscObject)fe, &name));
1132:   if (name) PetscCall(PetscFESetName(*trFE, name));
1133:   PetscCall(PetscFEGetQuadrature(fe, &fullQuad));
1134:   PetscCall(PetscQuadratureGetOrder(fullQuad, &order));
1135:   PetscCall(DMPlexGetConeSize(dm, refPoint, &coneSize));
1136:   if (coneSize == 2 * depth) PetscCall(PetscDTGaussTensorQuadrature(depth, 1, (order + 2) / 2, -1., 1., &subQuad));
1137:   else PetscCall(PetscDTSimplexQuadrature(depth, order, PETSCDTSIMPLEXQUAD_DEFAULT, &subQuad));
1138:   PetscCall(PetscFESetQuadrature(*trFE, subQuad));
1139:   PetscCall(PetscFESetUp(*trFE));
1140:   PetscCall(PetscQuadratureDestroy(&subQuad));
1141:   PetscCall(PetscSpaceDestroy(&bsubsp));
1142:   PetscFunctionReturn(PETSC_SUCCESS);
1143: }

1145: PETSC_EXTERN PetscErrorCode PetscFECreatePointTrace(PetscFE fe, PetscInt refPoint, PetscFE *trFE)
1146: {
1147:   PetscFunctionBegin;
1149:   PetscAssertPointer(trFE, 3);
1150:   if (fe->ops->createpointtrace) PetscUseTypeMethod(fe, createpointtrace, refPoint, trFE);
1151:   else PetscCall(PetscFECreatePointTraceDefault_Internal(fe, refPoint, trFE));
1152:   PetscFunctionReturn(PETSC_SUCCESS);
1153: }

1155: PetscErrorCode PetscFECreateHeightTrace(PetscFE fe, PetscInt height, PetscFE *trFE)
1156: {
1157:   PetscInt       hStart, hEnd;
1158:   PetscDualSpace dsp;
1159:   DM             dm;

1161:   PetscFunctionBegin;
1163:   PetscAssertPointer(trFE, 3);
1164:   *trFE = NULL;
1165:   PetscCall(PetscFEGetDualSpace(fe, &dsp));
1166:   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
1167:   PetscCall(DMPlexGetHeightStratum(dm, height, &hStart, &hEnd));
1168:   if (hEnd <= hStart) PetscFunctionReturn(PETSC_SUCCESS);
1169:   PetscCall(PetscFECreatePointTrace(fe, hStart, trFE));
1170:   PetscFunctionReturn(PETSC_SUCCESS);
1171: }

1173: /*@
1174:   PetscFEGetDimension - Get the dimension of the finite element space on a cell

1176:   Not Collective

1178:   Input Parameter:
1179: . fem - The `PetscFE`

1181:   Output Parameter:
1182: . dim - The dimension

1184:   Level: intermediate

1186: .seealso: `PetscFE`, `PetscFECreate()`, `PetscSpaceGetDimension()`, `PetscDualSpaceGetDimension()`
1187: @*/
1188: PetscErrorCode PetscFEGetDimension(PetscFE fem, PetscInt *dim)
1189: {
1190:   PetscFunctionBegin;
1192:   PetscAssertPointer(dim, 2);
1193:   PetscTryTypeMethod(fem, getdimension, dim);
1194:   PetscFunctionReturn(PETSC_SUCCESS);
1195: }

1197: /*@
1198:   PetscFEPushforward - Map the reference element function to real space

1200:   Input Parameters:
1201: + fe     - The `PetscFE`
1202: . fegeom - The cell geometry
1203: . Nv     - The number of function values
1204: - vals   - The function values

1206:   Output Parameter:
1207: . vals - The transformed function values

1209:   Level: advanced

1211:   Notes:
1212:   This just forwards the call onto `PetscDualSpacePushforward()`.

1214:   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.

1216: .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscDualSpacePushforward()`
1217: @*/
1218: PetscErrorCode PetscFEPushforward(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1219: {
1220:   PetscFunctionBeginHot;
1221:   PetscCall(PetscDualSpacePushforward(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
1222:   PetscFunctionReturn(PETSC_SUCCESS);
1223: }

1225: /*@
1226:   PetscFEPushforwardGradient - Map the reference element function gradient to real space

1228:   Input Parameters:
1229: + fe     - The `PetscFE`
1230: . fegeom - The cell geometry
1231: . Nv     - The number of function gradient values
1232: - vals   - The function gradient values

1234:   Output Parameter:
1235: . vals - The transformed function gradient values

1237:   Level: advanced

1239:   Notes:
1240:   This just forwards the call onto `PetscDualSpacePushforwardGradient()`.

1242:   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.

1244: .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscFEPushforward()`, `PetscDualSpacePushforwardGradient()`, `PetscDualSpacePushforward()`
1245: @*/
1246: PetscErrorCode PetscFEPushforwardGradient(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1247: {
1248:   PetscFunctionBeginHot;
1249:   PetscCall(PetscDualSpacePushforwardGradient(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
1250:   PetscFunctionReturn(PETSC_SUCCESS);
1251: }

1253: /*@
1254:   PetscFEPushforwardHessian - Map the reference element function Hessian to real space

1256:   Input Parameters:
1257: + fe     - The `PetscFE`
1258: . fegeom - The cell geometry
1259: . Nv     - The number of function Hessian values
1260: - vals   - The function Hessian values

1262:   Output Parameter:
1263: . vals - The transformed function Hessian values

1265:   Level: advanced

1267:   Notes:
1268:   This just forwards the call onto `PetscDualSpacePushforwardHessian()`.

1270:   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.

1272:   Developer Notes:
1273:   It is unclear why all these one line convenience routines are desirable

1275: .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscFEPushforward()`, `PetscDualSpacePushforwardHessian()`, `PetscDualSpacePushforward()`
1276: @*/
1277: PetscErrorCode PetscFEPushforwardHessian(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1278: {
1279:   PetscFunctionBeginHot;
1280:   PetscCall(PetscDualSpacePushforwardHessian(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
1281:   PetscFunctionReturn(PETSC_SUCCESS);
1282: }

1284: /*
1285: Purpose: Compute element vector for chunk of elements

1287: Input:
1288:   Sizes:
1289:      Ne:  number of elements
1290:      Nf:  number of fields
1291:      PetscFE
1292:        dim: spatial dimension
1293:        Nb:  number of basis functions
1294:        Nc:  number of field components
1295:        PetscQuadrature
1296:          Nq:  number of quadrature points

1298:   Geometry:
1299:      PetscFEGeom[Ne] possibly *Nq
1300:        PetscReal v0s[dim]
1301:        PetscReal n[dim]
1302:        PetscReal jacobians[dim*dim]
1303:        PetscReal jacobianInverses[dim*dim]
1304:        PetscReal jacobianDeterminants
1305:   FEM:
1306:      PetscFE
1307:        PetscQuadrature
1308:          PetscReal   quadPoints[Nq*dim]
1309:          PetscReal   quadWeights[Nq]
1310:        PetscReal   basis[Nq*Nb*Nc]
1311:        PetscReal   basisDer[Nq*Nb*Nc*dim]
1312:      PetscScalar coefficients[Ne*Nb*Nc]
1313:      PetscScalar elemVec[Ne*Nb*Nc]

1315:   Problem:
1316:      PetscInt f: the active field
1317:      f0, f1

1319:   Work Space:
1320:      PetscFE
1321:        PetscScalar f0[Nq*dim];
1322:        PetscScalar f1[Nq*dim*dim];
1323:        PetscScalar u[Nc];
1324:        PetscScalar gradU[Nc*dim];
1325:        PetscReal   x[dim];
1326:        PetscScalar realSpaceDer[dim];

1328: Purpose: Compute element vector for N_cb batches of elements

1330: Input:
1331:   Sizes:
1332:      N_cb: Number of serial cell batches

1334:   Geometry:
1335:      PetscReal v0s[Ne*dim]
1336:      PetscReal jacobians[Ne*dim*dim]        possibly *Nq
1337:      PetscReal jacobianInverses[Ne*dim*dim] possibly *Nq
1338:      PetscReal jacobianDeterminants[Ne]     possibly *Nq
1339:   FEM:
1340:      static PetscReal   quadPoints[Nq*dim]
1341:      static PetscReal   quadWeights[Nq]
1342:      static PetscReal   basis[Nq*Nb*Nc]
1343:      static PetscReal   basisDer[Nq*Nb*Nc*dim]
1344:      PetscScalar coefficients[Ne*Nb*Nc]
1345:      PetscScalar elemVec[Ne*Nb*Nc]

1347: ex62.c:
1348:   PetscErrorCode PetscFEIntegrateResidualBatch(PetscInt Ne, PetscInt numFields, PetscInt field, PetscQuadrature quad[], const PetscScalar coefficients[],
1349:                                                const PetscReal v0s[], const PetscReal jacobians[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[],
1350:                                                void (*f0_func)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f0[]),
1351:                                                void (*f1_func)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f1[]), PetscScalar elemVec[])

1353: ex52.c:
1354:   PetscErrorCode IntegrateLaplacianBatchCPU(PetscInt Ne, PetscInt Nb, const PetscScalar coefficients[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscInt Nq, const PetscReal quadPoints[], const PetscReal quadWeights[], const PetscReal basisTabulation[], const PetscReal basisDerTabulation[], PetscScalar elemVec[], AppCtx *user)
1355:   PetscErrorCode IntegrateElasticityBatchCPU(PetscInt Ne, PetscInt Nb, PetscInt Ncomp, const PetscScalar coefficients[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscInt Nq, const PetscReal quadPoints[], const PetscReal quadWeights[], const PetscReal basisTabulation[], const PetscReal basisDerTabulation[], PetscScalar elemVec[], AppCtx *user)

1357: ex52_integrateElement.cu
1358: __global__ void integrateElementQuadrature(int N_cb, realType *coefficients, realType *jacobianInverses, realType *jacobianDeterminants, realType *elemVec)

1360: PETSC_EXTERN PetscErrorCode IntegrateElementBatchGPU(PetscInt spatial_dim, PetscInt Ne, PetscInt Ncb, PetscInt Nbc, PetscInt Nbl, const PetscScalar coefficients[],
1361:                                                      const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscScalar elemVec[],
1362:                                                      PetscLogEvent event, PetscInt debug, PetscInt pde_op)

1364: ex52_integrateElementOpenCL.c:
1365: PETSC_EXTERN PetscErrorCode IntegrateElementBatchGPU(PetscInt spatial_dim, PetscInt Ne, PetscInt Ncb, PetscInt Nbc, PetscInt N_bl, const PetscScalar coefficients[],
1366:                                                      const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscScalar elemVec[],
1367:                                                      PetscLogEvent event, PetscInt debug, PetscInt pde_op)

1369: __kernel void integrateElementQuadrature(int N_cb, __global float *coefficients, __global float *jacobianInverses, __global float *jacobianDeterminants, __global float *elemVec)
1370: */

1372: /*@
1373:   PetscFEIntegrate - Produce the integral for the given field for a chunk of elements by quadrature integration

1375:   Not Collective

1377:   Input Parameters:
1378: + prob            - The `PetscDS` specifying the discretizations and continuum functions
1379: . field           - The field being integrated
1380: . Ne              - The number of elements in the chunk
1381: . cgeom           - The cell geometry for each cell in the chunk
1382: . coefficients    - The array of FEM basis coefficients for the elements
1383: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1384: - coefficientsAux - The array of FEM auxiliary basis coefficients for the elements

1386:   Output Parameter:
1387: . integral - the integral for this field

1389:   Level: intermediate

1391:   Developer Notes:
1392:   The function name begins with `PetscFE` and yet the first argument is `PetscDS` and it has no `PetscFE` arguments.

1394: .seealso: `PetscFE`, `PetscDS`, `PetscFEIntegrateResidual()`, `PetscFEIntegrateBd()`
1395: @*/
1396: PetscErrorCode PetscFEIntegrate(PetscDS prob, PetscInt field, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
1397: {
1398:   PetscFE fe;

1400:   PetscFunctionBegin;
1402:   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
1403:   if (fe->ops->integrate) PetscCall((*fe->ops->integrate)(prob, field, Ne, cgeom, coefficients, probAux, coefficientsAux, integral));
1404:   PetscFunctionReturn(PETSC_SUCCESS);
1405: }

1407: /*@C
1408:   PetscFEIntegrateBd - Produce the integral for the given field for a chunk of elements by quadrature integration

1410:   Not Collective

1412:   Input Parameters:
1413: + prob            - The `PetscDS` specifying the discretizations and continuum functions
1414: . field           - The field being integrated
1415: . obj_func        - The function to be integrated
1416: . Ne              - The number of elements in the chunk
1417: . geom            - The face geometry for each face in the chunk
1418: . coefficients    - The array of FEM basis coefficients for the elements
1419: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1420: - coefficientsAux - The array of FEM auxiliary basis coefficients for the elements

1422:   Output Parameter:
1423: . integral - the integral for this field

1425:   Level: intermediate

1427:   Developer Notes:
1428:   The function name begins with `PetscFE` and yet the first argument is `PetscDS` and it has no `PetscFE` arguments.

1430: .seealso: `PetscFE`, `PetscDS`, `PetscFEIntegrateResidual()`, `PetscFEIntegrate()`
1431: @*/
1432: PetscErrorCode PetscFEIntegrateBd(PetscDS prob, PetscInt field, void (*obj_func)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), PetscInt Ne, PetscFEGeom *geom, const PetscScalar coefficients[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
1433: {
1434:   PetscFE fe;

1436:   PetscFunctionBegin;
1438:   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
1439:   if (fe->ops->integratebd) PetscCall((*fe->ops->integratebd)(prob, field, obj_func, Ne, geom, coefficients, probAux, coefficientsAux, integral));
1440:   PetscFunctionReturn(PETSC_SUCCESS);
1441: }

1443: /*@
1444:   PetscFEIntegrateResidual - Produce the element residual vector for a chunk of elements by quadrature integration

1446:   Not Collective

1448:   Input Parameters:
1449: + ds              - The `PetscDS` specifying the discretizations and continuum functions
1450: . key             - The (label+value, field) being integrated
1451: . Ne              - The number of elements in the chunk
1452: . cgeom           - The cell geometry for each cell in the chunk
1453: . coefficients    - The array of FEM basis coefficients for the elements
1454: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1455: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1456: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1457: - t               - The time

1459:   Output Parameter:
1460: . elemVec - the element residual vectors from each element

1462:   Level: intermediate

1464:   Note:
1465: .vb
1466:   Loop over batch of elements (e):
1467:     Loop over quadrature points (q):
1468:       Make u_q and gradU_q (loops over fields,Nb,Ncomp) and x_q
1469:       Call f_0 and f_1
1470:     Loop over element vector entries (f,fc --> i):
1471:       elemVec[i] += \psi^{fc}_f(q) f0_{fc}(u, \nabla u) + \nabla\psi^{fc}_f(q) \cdot f1_{fc,df}(u, \nabla u)
1472: .ve

1474: .seealso: `PetscFEIntegrateBdResidual()`
1475: @*/
1476: PetscErrorCode PetscFEIntegrateResidual(PetscDS ds, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1477: {
1478:   PetscFE fe;

1480:   PetscFunctionBeginHot;
1482:   PetscCall(PetscDSGetDiscretization(ds, key.field, (PetscObject *)&fe));
1483:   if (fe->ops->integrateresidual) PetscCall((*fe->ops->integrateresidual)(ds, key, Ne, cgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
1484:   PetscFunctionReturn(PETSC_SUCCESS);
1485: }

1487: /*@
1488:   PetscFEIntegrateBdResidual - Produce the element residual vector for a chunk of elements by quadrature integration over a boundary

1490:   Not Collective

1492:   Input Parameters:
1493: + ds              - The `PetscDS` specifying the discretizations and continuum functions
1494: . wf              - The PetscWeakForm object holding the pointwise functions
1495: . key             - The (label+value, field) being integrated
1496: . Ne              - The number of elements in the chunk
1497: . fgeom           - The face geometry for each cell in the chunk
1498: . coefficients    - The array of FEM basis coefficients for the elements
1499: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1500: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1501: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1502: - t               - The time

1504:   Output Parameter:
1505: . elemVec - the element residual vectors from each element

1507:   Level: intermediate

1509: .seealso: `PetscFEIntegrateResidual()`
1510: @*/
1511: PetscErrorCode PetscFEIntegrateBdResidual(PetscDS ds, PetscWeakForm wf, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1512: {
1513:   PetscFE fe;

1515:   PetscFunctionBegin;
1517:   PetscCall(PetscDSGetDiscretization(ds, key.field, (PetscObject *)&fe));
1518:   if (fe->ops->integratebdresidual) PetscCall((*fe->ops->integratebdresidual)(ds, wf, key, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
1519:   PetscFunctionReturn(PETSC_SUCCESS);
1520: }

1522: /*@
1523:   PetscFEIntegrateHybridResidual - Produce the element residual vector for a chunk of hybrid element faces by quadrature integration

1525:   Not Collective

1527:   Input Parameters:
1528: + ds              - The `PetscDS` specifying the discretizations and continuum functions
1529: . dsIn            - The `PetscDS` specifying the discretizations and continuum functions for input
1530: . key             - The (label+value, field) being integrated
1531: . s               - The side of the cell being integrated, 0 for negative and 1 for positive
1532: . Ne              - The number of elements in the chunk
1533: . fgeom           - The face geometry for each cell in the chunk
1534: . cgeom           - The cell geometry for each neighbor cell in the chunk
1535: . coefficients    - The array of FEM basis coefficients for the elements
1536: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1537: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1538: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1539: - t               - The time

1541:   Output Parameter:
1542: . elemVec - the element residual vectors from each element

1544:   Level: developer

1546: .seealso: `PetscFEIntegrateResidual()`
1547: @*/
1548: PetscErrorCode PetscFEIntegrateHybridResidual(PetscDS ds, PetscDS dsIn, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1549: {
1550:   PetscFE fe;

1552:   PetscFunctionBegin;
1555:   PetscCall(PetscDSGetDiscretization(ds, key.field, (PetscObject *)&fe));
1556:   if (fe->ops->integratehybridresidual) PetscCall((*fe->ops->integratehybridresidual)(ds, dsIn, key, s, Ne, fgeom, cgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
1557:   PetscFunctionReturn(PETSC_SUCCESS);
1558: }

1560: /*@
1561:   PetscFEIntegrateJacobian - Produce the element Jacobian for a chunk of elements by quadrature integration

1563:   Not Collective

1565:   Input Parameters:
1566: + rds             - The `PetscDS` specifying the row discretizations and continuum functions
1567: . cds             - The `PetscDS` specifying the column discretizations
1568: . jtype           - The type of matrix pointwise functions that should be used
1569: . key             - The (label+value, fieldI*Nf + fieldJ) being integrated
1570: . Ne              - The number of elements in the chunk
1571: . cgeom           - The cell geometry for each cell in the chunk
1572: . coefficients    - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
1573: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1574: . dsAux           - The `PetscDS` specifying the auxiliary discretizations
1575: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1576: . t               - The time
1577: - u_tshift        - A multiplier for the dF/du_t term (as opposed to the dF/du term)

1579:   Output Parameter:
1580: . elemMat - the element matrices for the Jacobian from each element

1582:   Level: intermediate

1584:   Note:
1585: .vb
1586:   Loop over batch of elements (e):
1587:     Loop over element matrix entries (f,fc,g,gc --> i,j):
1588:       Loop over quadrature points (q):
1589:         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1590:           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1591:                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1592:                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1593:                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1594: .ve

1596: .seealso: `PetscFEIntegrateResidual()`
1597: @*/
1598: PetscErrorCode PetscFEIntegrateJacobian(PetscDS rds, PetscDS cds, PetscFEJacobianType jtype, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS dsAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1599: {
1600:   PetscFE  fe;
1601:   PetscInt Nf;

1603:   PetscFunctionBegin;
1606:   PetscCall(PetscDSGetNumFields(rds, &Nf));
1607:   PetscCall(PetscDSGetDiscretization(rds, key.field / Nf, (PetscObject *)&fe));
1608:   if (fe->ops->integratejacobian) PetscCall((*fe->ops->integratejacobian)(rds, cds, jtype, key, Ne, cgeom, coefficients, coefficients_t, dsAux, coefficientsAux, t, u_tshift, elemMat));
1609:   PetscFunctionReturn(PETSC_SUCCESS);
1610: }

1612: /*@
1613:   PetscFEIntegrateBdJacobian - Produce the boundary element Jacobian for a chunk of elements by quadrature integration

1615:   Not Collective

1617:   Input Parameters:
1618: + ds              - The `PetscDS` specifying the discretizations and continuum functions
1619: . wf              - The PetscWeakForm holding the pointwise functions
1620: . jtype           - The type of matrix pointwise functions that should be used
1621: . key             - The (label+value, fieldI*Nf + fieldJ) being integrated
1622: . Ne              - The number of elements in the chunk
1623: . fgeom           - The face geometry for each cell in the chunk
1624: . coefficients    - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
1625: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1626: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1627: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1628: . t               - The time
1629: - u_tshift        - A multiplier for the dF/du_t term (as opposed to the dF/du term)

1631:   Output Parameter:
1632: . elemMat - the element matrices for the Jacobian from each element

1634:   Level: intermediate

1636:   Note:
1637: .vb
1638:   Loop over batch of elements (e):
1639:     Loop over element matrix entries (f,fc,g,gc --> i,j):
1640:       Loop over quadrature points (q):
1641:         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1642:           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1643:                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1644:                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1645:                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1646: .ve

1648: .seealso: `PetscFEIntegrateJacobian()`, `PetscFEIntegrateResidual()`
1649: @*/
1650: PetscErrorCode PetscFEIntegrateBdJacobian(PetscDS ds, PetscWeakForm wf, PetscFEJacobianType jtype, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1651: {
1652:   PetscFE  fe;
1653:   PetscInt Nf;

1655:   PetscFunctionBegin;
1657:   PetscCall(PetscDSGetNumFields(ds, &Nf));
1658:   PetscCall(PetscDSGetDiscretization(ds, key.field / Nf, (PetscObject *)&fe));
1659:   if (fe->ops->integratebdjacobian) PetscCall((*fe->ops->integratebdjacobian)(ds, wf, jtype, key, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, u_tshift, elemMat));
1660:   PetscFunctionReturn(PETSC_SUCCESS);
1661: }

1663: /*@
1664:   PetscFEIntegrateHybridJacobian - Produce the boundary element Jacobian for a chunk of hybrid elements by quadrature integration

1666:   Not Collective

1668:   Input Parameters:
1669: + ds              - The `PetscDS` specifying the discretizations and continuum functions for the output
1670: . dsIn            - The `PetscDS` specifying the discretizations and continuum functions for the input
1671: . jtype           - The type of matrix pointwise functions that should be used
1672: . key             - The (label+value, fieldI*Nf + fieldJ) being integrated
1673: . s               - The side of the cell being integrated, 0 for negative and 1 for positive
1674: . Ne              - The number of elements in the chunk
1675: . fgeom           - The face geometry for each cell in the chunk
1676: . cgeom           - The cell geometry for each neighbor cell in the chunk
1677: . coefficients    - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
1678: . coefficients_t  - The array of FEM basis time derivative coefficients for the elements
1679: . probAux         - The `PetscDS` specifying the auxiliary discretizations
1680: . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1681: . t               - The time
1682: - u_tshift        - A multiplier for the dF/du_t term (as opposed to the dF/du term)

1684:   Output Parameter:
1685: . elemMat - the element matrices for the Jacobian from each element

1687:   Level: developer

1689:   Note:
1690: .vb
1691:   Loop over batch of elements (e):
1692:     Loop over element matrix entries (f,fc,g,gc --> i,j):
1693:       Loop over quadrature points (q):
1694:         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1695:           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1696:                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1697:                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1698:                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1699: .ve

1701: .seealso: `PetscFEIntegrateJacobian()`, `PetscFEIntegrateResidual()`
1702: @*/
1703: PetscErrorCode PetscFEIntegrateHybridJacobian(PetscDS ds, PetscDS dsIn, PetscFEJacobianType jtype, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1704: {
1705:   PetscFE  fe;
1706:   PetscInt Nf;

1708:   PetscFunctionBegin;
1710:   PetscCall(PetscDSGetNumFields(ds, &Nf));
1711:   PetscCall(PetscDSGetDiscretization(ds, key.field / Nf, (PetscObject *)&fe));
1712:   if (fe->ops->integratehybridjacobian) PetscCall((*fe->ops->integratehybridjacobian)(ds, dsIn, jtype, key, s, Ne, fgeom, cgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, u_tshift, elemMat));
1713:   PetscFunctionReturn(PETSC_SUCCESS);
1714: }

1716: /*@
1717:   PetscFEGetHeightSubspace - Get the subspace of this space for a mesh point of a given height

1719:   Input Parameters:
1720: + fe     - The finite element space
1721: - height - The height of the `DMPLEX` point

1723:   Output Parameter:
1724: . subfe - The subspace of this `PetscFE` space

1726:   Level: advanced

1728:   Note:
1729:   For example, if we want the subspace of this space for a face, we would choose height = 1.

1731: .seealso: `PetscFECreateDefault()`
1732: @*/
1733: PetscErrorCode PetscFEGetHeightSubspace(PetscFE fe, PetscInt height, PetscFE *subfe)
1734: {
1735:   PetscSpace      P, subP;
1736:   PetscDualSpace  Q, subQ;
1737:   PetscQuadrature subq;
1738:   PetscInt        dim, Nc;

1740:   PetscFunctionBegin;
1742:   PetscAssertPointer(subfe, 3);
1743:   if (height == 0) {
1744:     *subfe = fe;
1745:     PetscFunctionReturn(PETSC_SUCCESS);
1746:   }
1747:   PetscCall(PetscFEGetBasisSpace(fe, &P));
1748:   PetscCall(PetscFEGetDualSpace(fe, &Q));
1749:   PetscCall(PetscFEGetNumComponents(fe, &Nc));
1750:   PetscCall(PetscFEGetFaceQuadrature(fe, &subq));
1751:   PetscCall(PetscDualSpaceGetDimension(Q, &dim));
1752:   PetscCheck(height <= dim && height >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Asked for space at height %" PetscInt_FMT " for dimension %" PetscInt_FMT " space", height, dim);
1753:   if (!fe->subspaces) PetscCall(PetscCalloc1(dim, &fe->subspaces));
1754:   if (height <= dim) {
1755:     if (!fe->subspaces[height - 1]) {
1756:       PetscFE     sub = NULL;
1757:       const char *name;

1759:       PetscCall(PetscSpaceGetHeightSubspace(P, height, &subP));
1760:       PetscCall(PetscDualSpaceGetHeightSubspace(Q, height, &subQ));
1761:       if (subQ) {
1762:         PetscCall(PetscObjectReference((PetscObject)subP));
1763:         PetscCall(PetscObjectReference((PetscObject)subQ));
1764:         PetscCall(PetscObjectReference((PetscObject)subq));
1765:         PetscCall(PetscFECreateFromSpaces(subP, subQ, subq, NULL, &sub));
1766:       }
1767:       if (sub) {
1768:         PetscCall(PetscObjectGetName((PetscObject)fe, &name));
1769:         if (name) PetscCall(PetscFESetName(sub, name));
1770:       }
1771:       fe->subspaces[height - 1] = sub;
1772:     }
1773:     *subfe = fe->subspaces[height - 1];
1774:   } else {
1775:     *subfe = NULL;
1776:   }
1777:   PetscFunctionReturn(PETSC_SUCCESS);
1778: }

1780: /*@
1781:   PetscFERefine - Create a "refined" `PetscFE` object that refines the reference cell into
1782:   smaller copies.

1784:   Collective

1786:   Input Parameter:
1787: . fe - The initial `PetscFE`

1789:   Output Parameter:
1790: . feRef - The refined `PetscFE`

1792:   Level: advanced

1794:   Notes:
1795:   This is typically used to generate a preconditioner for a higher order method from a lower order method on a
1796:   refined mesh having the same number of dofs (but more sparsity). It is also used to create an
1797:   interpolation between regularly refined meshes.

1799: .seealso: `PetscFEType`, `PetscFECreate()`, `PetscFESetType()`
1800: @*/
1801: PetscErrorCode PetscFERefine(PetscFE fe, PetscFE *feRef)
1802: {
1803:   PetscSpace       P, Pref;
1804:   PetscDualSpace   Q, Qref;
1805:   DM               K, Kref;
1806:   PetscQuadrature  q, qref;
1807:   const PetscReal *v0, *jac;
1808:   PetscInt         numComp, numSubelements;
1809:   PetscInt         cStart, cEnd, c;
1810:   PetscDualSpace  *cellSpaces;

1812:   PetscFunctionBegin;
1813:   PetscCall(PetscFEGetBasisSpace(fe, &P));
1814:   PetscCall(PetscFEGetDualSpace(fe, &Q));
1815:   PetscCall(PetscFEGetQuadrature(fe, &q));
1816:   PetscCall(PetscDualSpaceGetDM(Q, &K));
1817:   /* Create space */
1818:   PetscCall(PetscObjectReference((PetscObject)P));
1819:   Pref = P;
1820:   /* Create dual space */
1821:   PetscCall(PetscDualSpaceDuplicate(Q, &Qref));
1822:   PetscCall(PetscDualSpaceSetType(Qref, PETSCDUALSPACEREFINED));
1823:   PetscCall(DMRefine(K, PetscObjectComm((PetscObject)fe), &Kref));
1824:   PetscCall(DMGetCoordinatesLocalSetUp(Kref));
1825:   PetscCall(PetscDualSpaceSetDM(Qref, Kref));
1826:   PetscCall(DMPlexGetHeightStratum(Kref, 0, &cStart, &cEnd));
1827:   PetscCall(PetscMalloc1(cEnd - cStart, &cellSpaces));
1828:   /* TODO: fix for non-uniform refinement */
1829:   for (c = 0; c < cEnd - cStart; c++) cellSpaces[c] = Q;
1830:   PetscCall(PetscDualSpaceRefinedSetCellSpaces(Qref, cellSpaces));
1831:   PetscCall(PetscFree(cellSpaces));
1832:   PetscCall(DMDestroy(&Kref));
1833:   PetscCall(PetscDualSpaceSetUp(Qref));
1834:   /* Create element */
1835:   PetscCall(PetscFECreate(PetscObjectComm((PetscObject)fe), feRef));
1836:   PetscCall(PetscFESetType(*feRef, PETSCFECOMPOSITE));
1837:   PetscCall(PetscFESetBasisSpace(*feRef, Pref));
1838:   PetscCall(PetscFESetDualSpace(*feRef, Qref));
1839:   PetscCall(PetscFEGetNumComponents(fe, &numComp));
1840:   PetscCall(PetscFESetNumComponents(*feRef, numComp));
1841:   PetscCall(PetscFESetUp(*feRef));
1842:   PetscCall(PetscSpaceDestroy(&Pref));
1843:   PetscCall(PetscDualSpaceDestroy(&Qref));
1844:   /* Create quadrature */
1845:   PetscCall(PetscFECompositeGetMapping(*feRef, &numSubelements, &v0, &jac, NULL));
1846:   PetscCall(PetscQuadratureExpandComposite(q, numSubelements, v0, jac, &qref));
1847:   PetscCall(PetscFESetQuadrature(*feRef, qref));
1848:   PetscCall(PetscQuadratureDestroy(&qref));
1849:   PetscFunctionReturn(PETSC_SUCCESS);
1850: }

1852: static PetscErrorCode PetscFESetDefaultName_Private(PetscFE fe)
1853: {
1854:   PetscSpace     P;
1855:   PetscDualSpace Q;
1856:   DM             K;
1857:   DMPolytopeType ct;
1858:   PetscInt       degree;
1859:   char           name[64];

1861:   PetscFunctionBegin;
1862:   PetscCall(PetscFEGetBasisSpace(fe, &P));
1863:   PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
1864:   PetscCall(PetscFEGetDualSpace(fe, &Q));
1865:   PetscCall(PetscDualSpaceGetDM(Q, &K));
1866:   PetscCall(DMPlexGetCellType(K, 0, &ct));
1867:   switch (ct) {
1868:   case DM_POLYTOPE_SEGMENT:
1869:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1870:   case DM_POLYTOPE_QUADRILATERAL:
1871:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
1872:   case DM_POLYTOPE_HEXAHEDRON:
1873:   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1874:     PetscCall(PetscSNPrintf(name, sizeof(name), "Q%" PetscInt_FMT, degree));
1875:     break;
1876:   case DM_POLYTOPE_TRIANGLE:
1877:   case DM_POLYTOPE_TETRAHEDRON:
1878:     PetscCall(PetscSNPrintf(name, sizeof(name), "P%" PetscInt_FMT, degree));
1879:     break;
1880:   case DM_POLYTOPE_TRI_PRISM:
1881:   case DM_POLYTOPE_TRI_PRISM_TENSOR:
1882:     PetscCall(PetscSNPrintf(name, sizeof(name), "P%" PetscInt_FMT "xQ%" PetscInt_FMT, degree, degree));
1883:     break;
1884:   default:
1885:     PetscCall(PetscSNPrintf(name, sizeof(name), "FE"));
1886:   }
1887:   PetscCall(PetscFESetName(fe, name));
1888:   PetscFunctionReturn(PETSC_SUCCESS);
1889: }

1891: /*@
1892:   PetscFECreateFromSpaces - Create a `PetscFE` from the basis and dual spaces

1894:   Collective

1896:   Input Parameters:
1897: + P  - The basis space
1898: . Q  - The dual space
1899: . q  - The cell quadrature
1900: - fq - The face quadrature

1902:   Output Parameter:
1903: . fem - The `PetscFE` object

1905:   Level: beginner

1907:   Note:
1908:   The `PetscFE` takes ownership of these spaces by calling destroy on each. They should not be used after this call, and for borrowed references from `PetscFEGetSpace()` and the like, the caller must use `PetscObjectReference` before this call.

1910: .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`,
1911:           `PetscFECreateLagrangeByCell()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
1912: @*/
1913: PetscErrorCode PetscFECreateFromSpaces(PetscSpace P, PetscDualSpace Q, PetscQuadrature q, PetscQuadrature fq, PetscFE *fem)
1914: {
1915:   PetscInt    Nc;
1916:   PetscInt    p_Ns = -1, p_Nc = -1, q_Ns = -1, q_Nc = -1;
1917:   PetscBool   p_is_uniform_sum = PETSC_FALSE, p_interleave_basis = PETSC_FALSE, p_interleave_components = PETSC_FALSE;
1918:   PetscBool   q_is_uniform_sum = PETSC_FALSE, q_interleave_basis = PETSC_FALSE, q_interleave_components = PETSC_FALSE;
1919:   const char *prefix;

1921:   PetscFunctionBegin;
1922:   PetscCall(PetscObjectTypeCompare((PetscObject)P, PETSCSPACESUM, &p_is_uniform_sum));
1923:   if (p_is_uniform_sum) {
1924:     PetscSpace subsp_0 = NULL;
1925:     PetscCall(PetscSpaceSumGetNumSubspaces(P, &p_Ns));
1926:     PetscCall(PetscSpaceGetNumComponents(P, &p_Nc));
1927:     PetscCall(PetscSpaceSumGetConcatenate(P, &p_is_uniform_sum));
1928:     PetscCall(PetscSpaceSumGetInterleave(P, &p_interleave_basis, &p_interleave_components));
1929:     for (PetscInt s = 0; s < p_Ns; s++) {
1930:       PetscSpace subsp;

1932:       PetscCall(PetscSpaceSumGetSubspace(P, s, &subsp));
1933:       if (!s) {
1934:         subsp_0 = subsp;
1935:       } else if (subsp != subsp_0) {
1936:         p_is_uniform_sum = PETSC_FALSE;
1937:       }
1938:     }
1939:   }
1940:   PetscCall(PetscObjectTypeCompare((PetscObject)Q, PETSCDUALSPACESUM, &q_is_uniform_sum));
1941:   if (q_is_uniform_sum) {
1942:     PetscDualSpace subsp_0 = NULL;
1943:     PetscCall(PetscDualSpaceSumGetNumSubspaces(Q, &q_Ns));
1944:     PetscCall(PetscDualSpaceGetNumComponents(Q, &q_Nc));
1945:     PetscCall(PetscDualSpaceSumGetConcatenate(Q, &q_is_uniform_sum));
1946:     PetscCall(PetscDualSpaceSumGetInterleave(Q, &q_interleave_basis, &q_interleave_components));
1947:     for (PetscInt s = 0; s < q_Ns; s++) {
1948:       PetscDualSpace subsp;

1950:       PetscCall(PetscDualSpaceSumGetSubspace(Q, s, &subsp));
1951:       if (!s) {
1952:         subsp_0 = subsp;
1953:       } else if (subsp != subsp_0) {
1954:         q_is_uniform_sum = PETSC_FALSE;
1955:       }
1956:     }
1957:   }
1958:   if (p_is_uniform_sum && q_is_uniform_sum && (p_interleave_basis == q_interleave_basis) && (p_interleave_components == q_interleave_components) && (p_Ns == q_Ns) && (p_Nc == q_Nc)) {
1959:     PetscSpace     scalar_space;
1960:     PetscDualSpace scalar_dspace;
1961:     PetscFE        scalar_fe;

1963:     PetscCall(PetscSpaceSumGetSubspace(P, 0, &scalar_space));
1964:     PetscCall(PetscDualSpaceSumGetSubspace(Q, 0, &scalar_dspace));
1965:     PetscCall(PetscObjectReference((PetscObject)scalar_space));
1966:     PetscCall(PetscObjectReference((PetscObject)scalar_dspace));
1967:     PetscCall(PetscObjectReference((PetscObject)q));
1968:     PetscCall(PetscObjectReference((PetscObject)fq));
1969:     PetscCall(PetscFECreateFromSpaces(scalar_space, scalar_dspace, q, fq, &scalar_fe));
1970:     PetscCall(PetscFECreateVector(scalar_fe, p_Ns, p_interleave_basis, p_interleave_components, fem));
1971:     PetscCall(PetscFEDestroy(&scalar_fe));
1972:   } else {
1973:     PetscCall(PetscFECreate(PetscObjectComm((PetscObject)P), fem));
1974:     PetscCall(PetscFESetType(*fem, PETSCFEBASIC));
1975:   }
1976:   PetscCall(PetscSpaceGetNumComponents(P, &Nc));
1977:   PetscCall(PetscFESetNumComponents(*fem, Nc));
1978:   PetscCall(PetscFESetBasisSpace(*fem, P));
1979:   PetscCall(PetscFESetDualSpace(*fem, Q));
1980:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)P, &prefix));
1981:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*fem, prefix));
1982:   PetscCall(PetscFESetUp(*fem));
1983:   PetscCall(PetscSpaceDestroy(&P));
1984:   PetscCall(PetscDualSpaceDestroy(&Q));
1985:   PetscCall(PetscFESetQuadrature(*fem, q));
1986:   PetscCall(PetscFESetFaceQuadrature(*fem, fq));
1987:   PetscCall(PetscQuadratureDestroy(&q));
1988:   PetscCall(PetscQuadratureDestroy(&fq));
1989:   PetscCall(PetscFESetDefaultName_Private(*fem));
1990:   PetscFunctionReturn(PETSC_SUCCESS);
1991: }

1993: static PetscErrorCode PetscFECreate_Internal(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, const char prefix[], PetscInt degree, PetscInt qorder, PetscBool setFromOptions, PetscFE *fem)
1994: {
1995:   DM              K;
1996:   PetscSpace      P;
1997:   PetscDualSpace  Q;
1998:   PetscQuadrature q, fq;
1999:   PetscBool       tensor;

2001:   PetscFunctionBegin;
2002:   if (prefix) PetscAssertPointer(prefix, 5);
2003:   PetscAssertPointer(fem, 9);
2004:   switch (ct) {
2005:   case DM_POLYTOPE_SEGMENT:
2006:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
2007:   case DM_POLYTOPE_QUADRILATERAL:
2008:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
2009:   case DM_POLYTOPE_HEXAHEDRON:
2010:   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
2011:     tensor = PETSC_TRUE;
2012:     break;
2013:   default:
2014:     tensor = PETSC_FALSE;
2015:   }
2016:   /* Create space */
2017:   PetscCall(PetscSpaceCreate(comm, &P));
2018:   PetscCall(PetscSpaceSetType(P, PETSCSPACEPOLYNOMIAL));
2019:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)P, prefix));
2020:   PetscCall(PetscSpacePolynomialSetTensor(P, tensor));
2021:   PetscCall(PetscSpaceSetNumComponents(P, Nc));
2022:   PetscCall(PetscSpaceSetNumVariables(P, dim));
2023:   if (degree >= 0) {
2024:     PetscCall(PetscSpaceSetDegree(P, degree, PETSC_DETERMINE));
2025:     if (ct == DM_POLYTOPE_TRI_PRISM || ct == DM_POLYTOPE_TRI_PRISM_TENSOR) {
2026:       PetscSpace Pend, Pside;

2028:       PetscCall(PetscSpaceSetNumComponents(P, 1));
2029:       PetscCall(PetscSpaceCreate(comm, &Pend));
2030:       PetscCall(PetscSpaceSetType(Pend, PETSCSPACEPOLYNOMIAL));
2031:       PetscCall(PetscSpacePolynomialSetTensor(Pend, PETSC_FALSE));
2032:       PetscCall(PetscSpaceSetNumComponents(Pend, 1));
2033:       PetscCall(PetscSpaceSetNumVariables(Pend, dim - 1));
2034:       PetscCall(PetscSpaceSetDegree(Pend, degree, PETSC_DETERMINE));
2035:       PetscCall(PetscSpaceCreate(comm, &Pside));
2036:       PetscCall(PetscSpaceSetType(Pside, PETSCSPACEPOLYNOMIAL));
2037:       PetscCall(PetscSpacePolynomialSetTensor(Pside, PETSC_FALSE));
2038:       PetscCall(PetscSpaceSetNumComponents(Pside, 1));
2039:       PetscCall(PetscSpaceSetNumVariables(Pside, 1));
2040:       PetscCall(PetscSpaceSetDegree(Pside, degree, PETSC_DETERMINE));
2041:       PetscCall(PetscSpaceSetType(P, PETSCSPACETENSOR));
2042:       PetscCall(PetscSpaceTensorSetNumSubspaces(P, 2));
2043:       PetscCall(PetscSpaceTensorSetSubspace(P, 0, Pend));
2044:       PetscCall(PetscSpaceTensorSetSubspace(P, 1, Pside));
2045:       PetscCall(PetscSpaceDestroy(&Pend));
2046:       PetscCall(PetscSpaceDestroy(&Pside));

2048:       if (Nc > 1) {
2049:         PetscSpace scalar_P = P;

2051:         PetscCall(PetscSpaceCreate(comm, &P));
2052:         PetscCall(PetscSpaceSetNumVariables(P, dim));
2053:         PetscCall(PetscSpaceSetNumComponents(P, Nc));
2054:         PetscCall(PetscSpaceSetType(P, PETSCSPACESUM));
2055:         PetscCall(PetscSpaceSumSetNumSubspaces(P, Nc));
2056:         PetscCall(PetscSpaceSumSetConcatenate(P, PETSC_TRUE));
2057:         PetscCall(PetscSpaceSumSetInterleave(P, PETSC_TRUE, PETSC_FALSE));
2058:         for (PetscInt i = 0; i < Nc; i++) PetscCall(PetscSpaceSumSetSubspace(P, i, scalar_P));
2059:         PetscCall(PetscSpaceDestroy(&scalar_P));
2060:       }
2061:     }
2062:   }
2063:   if (setFromOptions) PetscCall(PetscSpaceSetFromOptions(P));
2064:   PetscCall(PetscSpaceSetUp(P));
2065:   PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
2066:   PetscCall(PetscSpacePolynomialGetTensor(P, &tensor));
2067:   PetscCall(PetscSpaceGetNumComponents(P, &Nc));
2068:   /* Create dual space */
2069:   PetscCall(PetscDualSpaceCreate(comm, &Q));
2070:   PetscCall(PetscDualSpaceSetType(Q, PETSCDUALSPACELAGRANGE));
2071:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Q, prefix));
2072:   PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &K));
2073:   PetscCall(PetscDualSpaceSetDM(Q, K));
2074:   PetscCall(DMDestroy(&K));
2075:   PetscCall(PetscDualSpaceSetNumComponents(Q, Nc));
2076:   PetscCall(PetscDualSpaceSetOrder(Q, degree));
2077:   PetscCall(PetscDualSpaceLagrangeSetTensor(Q, (tensor || (ct == DM_POLYTOPE_TRI_PRISM)) ? PETSC_TRUE : PETSC_FALSE));
2078:   if (setFromOptions) PetscCall(PetscDualSpaceSetFromOptions(Q));
2079:   PetscCall(PetscDualSpaceSetUp(Q));
2080:   /* Create quadrature */
2081:   PetscDTSimplexQuadratureType qtype = PETSCDTSIMPLEXQUAD_DEFAULT;

2083:   qorder = qorder >= 0 ? qorder : degree;
2084:   if (setFromOptions) {
2085:     PetscObjectOptionsBegin((PetscObject)P);
2086:     PetscCall(PetscOptionsBoundedInt("-petscfe_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "PetscFECreateDefault", qorder, &qorder, NULL, 0));
2087:     PetscCall(PetscOptionsEnum("-petscfe_default_quadrature_type", "Simplex quadrature type", "PetscDTSimplexQuadratureType", PetscDTSimplexQuadratureTypes, (PetscEnum)qtype, (PetscEnum *)&qtype, NULL));
2088:     PetscOptionsEnd();
2089:   }
2090:   PetscCall(PetscDTCreateQuadratureByCell(ct, qorder, qtype, &q, &fq));
2091:   /* Create finite element */
2092:   PetscCall(PetscFECreateFromSpaces(P, Q, q, fq, fem));
2093:   if (setFromOptions) PetscCall(PetscFESetFromOptions(*fem));
2094:   PetscFunctionReturn(PETSC_SUCCESS);
2095: }

2097: /*@
2098:   PetscFECreateDefault - Create a `PetscFE` for basic FEM computation

2100:   Collective

2102:   Input Parameters:
2103: + comm      - The MPI comm
2104: . dim       - The spatial dimension
2105: . Nc        - The number of components
2106: . isSimplex - Flag for simplex reference cell, otherwise its a tensor product
2107: . prefix    - The options prefix, or `NULL`
2108: - qorder    - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree

2110:   Output Parameter:
2111: . fem - The `PetscFE` object

2113:   Level: beginner

2115:   Note:
2116:   Each subobject is SetFromOption() during creation, so that the object may be customized from the command line, using the prefix specified above. See the links below for the particular options available.

2118: .seealso: `PetscFECreateLagrange()`, `PetscFECreateByCell()`, `PetscSpaceSetFromOptions()`, `PetscDualSpaceSetFromOptions()`, `PetscFESetFromOptions()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2119: @*/
2120: PetscErrorCode PetscFECreateDefault(MPI_Comm comm, PetscInt dim, PetscInt Nc, PetscBool isSimplex, const char prefix[], PetscInt qorder, PetscFE *fem)
2121: {
2122:   PetscFunctionBegin;
2123:   PetscCall(PetscFECreate_Internal(comm, dim, Nc, DMPolytopeTypeSimpleShape(dim, isSimplex), prefix, PETSC_DECIDE, qorder, PETSC_TRUE, fem));
2124:   PetscFunctionReturn(PETSC_SUCCESS);
2125: }

2127: /*@
2128:   PetscFECreateByCell - Create a `PetscFE` for basic FEM computation

2130:   Collective

2132:   Input Parameters:
2133: + comm   - The MPI comm
2134: . dim    - The spatial dimension
2135: . Nc     - The number of components
2136: . ct     - The celltype of the reference cell
2137: . prefix - The options prefix, or `NULL`
2138: - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree

2140:   Output Parameter:
2141: . fem - The `PetscFE` object

2143:   Level: beginner

2145:   Note:
2146:   Each subobject is SetFromOption() during creation, so that the object may be customized from the command line, using the prefix specified above. See the links below for the particular options available.

2148: .seealso: `PetscFECreateDefault()`, `PetscFECreateLagrange()`, `PetscSpaceSetFromOptions()`, `PetscDualSpaceSetFromOptions()`, `PetscFESetFromOptions()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2149: @*/
2150: PetscErrorCode PetscFECreateByCell(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, const char prefix[], PetscInt qorder, PetscFE *fem)
2151: {
2152:   PetscFunctionBegin;
2153:   PetscCall(PetscFECreate_Internal(comm, dim, Nc, ct, prefix, PETSC_DECIDE, qorder, PETSC_TRUE, fem));
2154:   PetscFunctionReturn(PETSC_SUCCESS);
2155: }

2157: /*@
2158:   PetscFECreateLagrange - Create a `PetscFE` for the basic Lagrange space of degree k

2160:   Collective

2162:   Input Parameters:
2163: + comm      - The MPI comm
2164: . dim       - The spatial dimension
2165: . Nc        - The number of components
2166: . isSimplex - Flag for simplex reference cell, otherwise its a tensor product
2167: . k         - The degree k of the space
2168: - qorder    - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree

2170:   Output Parameter:
2171: . fem - The `PetscFE` object

2173:   Level: beginner

2175:   Note:
2176:   For simplices, this element is the space of maximum polynomial degree k, otherwise it is a tensor product of 1D polynomials, each with maximal degree k.

2178: .seealso: `PetscFECreateLagrangeByCell()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2179: @*/
2180: PetscErrorCode PetscFECreateLagrange(MPI_Comm comm, PetscInt dim, PetscInt Nc, PetscBool isSimplex, PetscInt k, PetscInt qorder, PetscFE *fem)
2181: {
2182:   PetscFunctionBegin;
2183:   PetscCall(PetscFECreate_Internal(comm, dim, Nc, DMPolytopeTypeSimpleShape(dim, isSimplex), NULL, k, qorder, PETSC_FALSE, fem));
2184:   PetscFunctionReturn(PETSC_SUCCESS);
2185: }

2187: /*@
2188:   PetscFECreateLagrangeByCell - Create a `PetscFE` for the basic Lagrange space of degree k

2190:   Collective

2192:   Input Parameters:
2193: + comm   - The MPI comm
2194: . dim    - The spatial dimension
2195: . Nc     - The number of components
2196: . ct     - The celltype of the reference cell
2197: . k      - The degree k of the space
2198: - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree

2200:   Output Parameter:
2201: . fem - The `PetscFE` object

2203:   Level: beginner

2205:   Note:
2206:   For simplices, this element is the space of maximum polynomial degree k, otherwise it is a tensor product of 1D polynomials, each with maximal degree k.

2208: .seealso: `PetscFECreateLagrange()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2209: @*/
2210: PetscErrorCode PetscFECreateLagrangeByCell(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, PetscInt k, PetscInt qorder, PetscFE *fem)
2211: {
2212:   PetscFunctionBegin;
2213:   PetscCall(PetscFECreate_Internal(comm, dim, Nc, ct, NULL, k, qorder, PETSC_FALSE, fem));
2214:   PetscFunctionReturn(PETSC_SUCCESS);
2215: }

2217: /*@
2218:   PetscFELimitDegree - Copy a `PetscFE` but limit the degree to be in the given range

2220:   Collective

2222:   Input Parameters:
2223: + fe        - The `PetscFE`
2224: . minDegree - The minimum degree, or `PETSC_DETERMINE` for no limit
2225: - maxDegree - The maximum degree, or `PETSC_DETERMINE` for no limit

2227:   Output Parameter:
2228: . newfe - The `PetscFE` object

2230:   Level: advanced

2232:   Note:
2233:   This currently only works for Lagrange elements.

2235: .seealso: `PetscFECreateLagrange()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2236: @*/
2237: PetscErrorCode PetscFELimitDegree(PetscFE fe, PetscInt minDegree, PetscInt maxDegree, PetscFE *newfe)
2238: {
2239:   PetscDualSpace Q;
2240:   PetscBool      islag, issum;
2241:   PetscInt       oldk = 0, k;

2243:   PetscFunctionBegin;
2244:   PetscCall(PetscFEGetDualSpace(fe, &Q));
2245:   PetscCall(PetscObjectTypeCompare((PetscObject)Q, PETSCDUALSPACELAGRANGE, &islag));
2246:   PetscCall(PetscObjectTypeCompare((PetscObject)Q, PETSCDUALSPACESUM, &issum));
2247:   if (islag) {
2248:     PetscCall(PetscDualSpaceGetOrder(Q, &oldk));
2249:   } else if (issum) {
2250:     PetscDualSpace subQ;

2252:     PetscCall(PetscDualSpaceSumGetSubspace(Q, 0, &subQ));
2253:     PetscCall(PetscDualSpaceGetOrder(subQ, &oldk));
2254:   } else {
2255:     PetscCall(PetscObjectReference((PetscObject)fe));
2256:     *newfe = fe;
2257:     PetscFunctionReturn(PETSC_SUCCESS);
2258:   }
2259:   k = oldk;
2260:   if (minDegree >= 0) k = PetscMax(k, minDegree);
2261:   if (maxDegree >= 0) k = PetscMin(k, maxDegree);
2262:   if (k != oldk) {
2263:     DM              K;
2264:     PetscSpace      P;
2265:     PetscQuadrature q;
2266:     DMPolytopeType  ct;
2267:     PetscInt        dim, Nc;

2269:     PetscCall(PetscFEGetBasisSpace(fe, &P));
2270:     PetscCall(PetscSpaceGetNumVariables(P, &dim));
2271:     PetscCall(PetscSpaceGetNumComponents(P, &Nc));
2272:     PetscCall(PetscDualSpaceGetDM(Q, &K));
2273:     PetscCall(DMPlexGetCellType(K, 0, &ct));
2274:     PetscCall(PetscFECreateLagrangeByCell(PetscObjectComm((PetscObject)fe), dim, Nc, ct, k, PETSC_DETERMINE, newfe));
2275:     PetscCall(PetscFEGetQuadrature(fe, &q));
2276:     PetscCall(PetscFESetQuadrature(*newfe, q));
2277:   } else {
2278:     PetscCall(PetscObjectReference((PetscObject)fe));
2279:     *newfe = fe;
2280:   }
2281:   PetscFunctionReturn(PETSC_SUCCESS);
2282: }

2284: /*@
2285:   PetscFECreateBrokenElement - Create a discontinuous version of the input `PetscFE`

2287:   Collective

2289:   Input Parameters:
2290: . cgfe - The continuous `PetscFE` object

2292:   Output Parameter:
2293: . dgfe - The discontinuous `PetscFE` object

2295:   Level: advanced

2297:   Note:
2298:   This only works for Lagrange elements.

2300: .seealso: `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`, `PetscFECreateLagrange()`, `PetscFECreateLagrangeByCell()`, `PetscDualSpaceLagrangeSetContinuity()`
2301: @*/
2302: PetscErrorCode PetscFECreateBrokenElement(PetscFE cgfe, PetscFE *dgfe)
2303: {
2304:   PetscSpace      P;
2305:   PetscDualSpace  Q, dgQ;
2306:   PetscQuadrature q, fq;
2307:   PetscBool       is_lagrange, is_sum;

2309:   PetscFunctionBegin;
2310:   PetscCall(PetscFEGetBasisSpace(cgfe, &P));
2311:   PetscCall(PetscObjectReference((PetscObject)P));
2312:   PetscCall(PetscFEGetDualSpace(cgfe, &Q));
2313:   PetscCall(PetscObjectTypeCompare((PetscObject)Q, PETSCDUALSPACELAGRANGE, &is_lagrange));
2314:   PetscCall(PetscObjectTypeCompare((PetscObject)Q, PETSCDUALSPACESUM, &is_sum));
2315:   PetscCheck(is_lagrange || is_sum, PETSC_COMM_SELF, PETSC_ERR_SUP, "Can only create broken elements of Lagrange elements");
2316:   PetscCall(PetscDualSpaceDuplicate(Q, &dgQ));
2317:   PetscCall(PetscDualSpaceLagrangeSetContinuity(dgQ, PETSC_FALSE));
2318:   PetscCall(PetscDualSpaceSetUp(dgQ));
2319:   PetscCall(PetscFEGetQuadrature(cgfe, &q));
2320:   PetscCall(PetscObjectReference((PetscObject)q));
2321:   PetscCall(PetscFEGetFaceQuadrature(cgfe, &fq));
2322:   PetscCall(PetscObjectReference((PetscObject)fq));
2323:   PetscCall(PetscFECreateFromSpaces(P, dgQ, q, fq, dgfe));
2324:   PetscFunctionReturn(PETSC_SUCCESS);
2325: }

2327: /*@
2328:   PetscFESetName - Names the `PetscFE` and its subobjects

2330:   Not Collective

2332:   Input Parameters:
2333: + fe   - The `PetscFE`
2334: - name - The name

2336:   Level: intermediate

2338: .seealso: `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2339: @*/
2340: PetscErrorCode PetscFESetName(PetscFE fe, const char name[])
2341: {
2342:   PetscSpace     P;
2343:   PetscDualSpace Q;

2345:   PetscFunctionBegin;
2346:   PetscCall(PetscFEGetBasisSpace(fe, &P));
2347:   PetscCall(PetscFEGetDualSpace(fe, &Q));
2348:   PetscCall(PetscObjectSetName((PetscObject)fe, name));
2349:   PetscCall(PetscObjectSetName((PetscObject)P, name));
2350:   PetscCall(PetscObjectSetName((PetscObject)Q, name));
2351:   PetscFunctionReturn(PETSC_SUCCESS);
2352: }

2354: PetscErrorCode PetscFEEvaluateFieldJets_Internal(PetscDS ds, PetscInt Nf, PetscInt r, PetscInt q, PetscTabulation T[], PetscFEGeom *fegeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscScalar u[], PetscScalar u_x[], PetscScalar u_t[])
2355: {
2356:   PetscInt dOffset = 0, fOffset = 0, f, g;

2358:   for (f = 0; f < Nf; ++f) {
2359:     PetscCheck(r < T[f]->Nr, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Replica number %" PetscInt_FMT " should be in [0, %" PetscInt_FMT ")", r, T[f]->Nr);
2360:     PetscCheck(q < T[f]->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point number %" PetscInt_FMT " should be in [0, %" PetscInt_FMT ")", q, T[f]->Np);
2361:     PetscFE          fe;
2362:     const PetscInt   k       = ds->jetDegree[f];
2363:     const PetscInt   cdim    = T[f]->cdim;
2364:     const PetscInt   dE      = fegeom->dimEmbed;
2365:     const PetscInt   Nq      = T[f]->Np;
2366:     const PetscInt   Nbf     = T[f]->Nb;
2367:     const PetscInt   Ncf     = T[f]->Nc;
2368:     const PetscReal *Bq      = &T[f]->T[0][(r * Nq + q) * Nbf * Ncf];
2369:     const PetscReal *Dq      = &T[f]->T[1][(r * Nq + q) * Nbf * Ncf * cdim];
2370:     const PetscReal *Hq      = k > 1 ? &T[f]->T[2][(r * Nq + q) * Nbf * Ncf * cdim * cdim] : NULL;
2371:     PetscInt         hOffset = 0, b, c, d;

2373:     PetscCall(PetscDSGetDiscretization(ds, f, (PetscObject *)&fe));
2374:     for (c = 0; c < Ncf; ++c) u[fOffset + c] = 0.0;
2375:     for (d = 0; d < dE * Ncf; ++d) u_x[fOffset * dE + d] = 0.0;
2376:     for (b = 0; b < Nbf; ++b) {
2377:       for (c = 0; c < Ncf; ++c) {
2378:         const PetscInt cidx = b * Ncf + c;

2380:         u[fOffset + c] += Bq[cidx] * coefficients[dOffset + b];
2381:         for (d = 0; d < cdim; ++d) u_x[(fOffset + c) * dE + d] += Dq[cidx * cdim + d] * coefficients[dOffset + b];
2382:       }
2383:     }
2384:     if (k > 1) {
2385:       for (g = 0; g < Nf; ++g) hOffset += T[g]->Nc * dE;
2386:       for (d = 0; d < dE * dE * Ncf; ++d) u_x[hOffset + fOffset * dE * dE + d] = 0.0;
2387:       for (b = 0; b < Nbf; ++b) {
2388:         for (c = 0; c < Ncf; ++c) {
2389:           const PetscInt cidx = b * Ncf + c;

2391:           for (d = 0; d < cdim * cdim; ++d) u_x[hOffset + (fOffset + c) * dE * dE + d] += Hq[cidx * cdim * cdim + d] * coefficients[dOffset + b];
2392:         }
2393:       }
2394:       PetscCall(PetscFEPushforwardHessian(fe, fegeom, 1, &u_x[hOffset + fOffset * dE * dE]));
2395:     }
2396:     PetscCall(PetscFEPushforward(fe, fegeom, 1, &u[fOffset]));
2397:     PetscCall(PetscFEPushforwardGradient(fe, fegeom, 1, &u_x[fOffset * dE]));
2398:     if (u_t) {
2399:       for (c = 0; c < Ncf; ++c) u_t[fOffset + c] = 0.0;
2400:       for (b = 0; b < Nbf; ++b) {
2401:         for (c = 0; c < Ncf; ++c) {
2402:           const PetscInt cidx = b * Ncf + c;

2404:           u_t[fOffset + c] += Bq[cidx] * coefficients_t[dOffset + b];
2405:         }
2406:       }
2407:       PetscCall(PetscFEPushforward(fe, fegeom, 1, &u_t[fOffset]));
2408:     }
2409:     fOffset += Ncf;
2410:     dOffset += Nbf;
2411:   }
2412:   return PETSC_SUCCESS;
2413: }

2415: PetscErrorCode PetscFEEvaluateFieldJets_Hybrid_Internal(PetscDS ds, PetscInt Nf, PetscInt rc, PetscInt qc, PetscTabulation Tab[], const PetscInt rf[], const PetscInt qf[], PetscTabulation Tabf[], PetscFEGeom *fegeom, PetscFEGeom *fegeomNbr, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscScalar u[], PetscScalar u_x[], PetscScalar u_t[])
2416: {
2417:   PetscInt dOffset = 0, fOffset = 0, f;

2419:   /* f is the field number in the DS */
2420:   for (f = 0; f < Nf; ++f) {
2421:     PetscBool isCohesive;
2422:     PetscInt  Ns, s;

2424:     if (!Tab[f]) continue;
2425:     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
2426:     Ns = isCohesive ? 1 : 2;
2427:     {
2428:       PetscTabulation T   = isCohesive ? Tab[f] : Tabf[f];
2429:       PetscFE         fe  = (PetscFE)ds->disc[f];
2430:       const PetscInt  dEt = T->cdim;
2431:       const PetscInt  dE  = fegeom->dimEmbed;
2432:       const PetscInt  Nq  = T->Np;
2433:       const PetscInt  Nbf = T->Nb;
2434:       const PetscInt  Ncf = T->Nc;

2436:       for (s = 0; s < Ns; ++s) {
2437:         const PetscInt   r  = isCohesive ? rc : rf[s];
2438:         const PetscInt   q  = isCohesive ? qc : qf[s];
2439:         const PetscReal *Bq = &T->T[0][(r * Nq + q) * Nbf * Ncf];
2440:         const PetscReal *Dq = &T->T[1][(r * Nq + q) * Nbf * Ncf * dEt];
2441:         PetscInt         b, c, d;

2443:         PetscCheck(r < T->Nr, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " Side %" PetscInt_FMT " Replica number %" PetscInt_FMT " should be in [0, %" PetscInt_FMT ")", f, s, r, T->Nr);
2444:         PetscCheck(q < T->Np, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " Side %" PetscInt_FMT " Point number %" PetscInt_FMT " should be in [0, %" PetscInt_FMT ")", f, s, q, T->Np);
2445:         for (c = 0; c < Ncf; ++c) u[fOffset + c] = 0.0;
2446:         for (d = 0; d < dE * Ncf; ++d) u_x[fOffset * dE + d] = 0.0;
2447:         for (b = 0; b < Nbf; ++b) {
2448:           for (c = 0; c < Ncf; ++c) {
2449:             const PetscInt cidx = b * Ncf + c;

2451:             u[fOffset + c] += Bq[cidx] * coefficients[dOffset + b];
2452:             for (d = 0; d < dEt; ++d) u_x[(fOffset + c) * dE + d] += Dq[cidx * dEt + d] * coefficients[dOffset + b];
2453:           }
2454:         }
2455:         PetscCall(PetscFEPushforward(fe, isCohesive ? fegeom : &fegeomNbr[s], 1, &u[fOffset]));
2456:         PetscCall(PetscFEPushforwardGradient(fe, isCohesive ? fegeom : &fegeomNbr[s], 1, &u_x[fOffset * dE]));
2457:         if (u_t) {
2458:           for (c = 0; c < Ncf; ++c) u_t[fOffset + c] = 0.0;
2459:           for (b = 0; b < Nbf; ++b) {
2460:             for (c = 0; c < Ncf; ++c) {
2461:               const PetscInt cidx = b * Ncf + c;

2463:               u_t[fOffset + c] += Bq[cidx] * coefficients_t[dOffset + b];
2464:             }
2465:           }
2466:           PetscCall(PetscFEPushforward(fe, fegeom, 1, &u_t[fOffset]));
2467:         }
2468:         fOffset += Ncf;
2469:         dOffset += Nbf;
2470:       }
2471:     }
2472:   }
2473:   return PETSC_SUCCESS;
2474: }

2476: PetscErrorCode PetscFEEvaluateFaceFields_Internal(PetscDS prob, PetscInt field, PetscInt faceLoc, const PetscScalar coefficients[], PetscScalar u[])
2477: {
2478:   PetscFE         fe;
2479:   PetscTabulation Tc;
2480:   PetscInt        b, c;

2482:   if (!prob) return PETSC_SUCCESS;
2483:   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
2484:   PetscCall(PetscFEGetFaceCentroidTabulation(fe, &Tc));
2485:   {
2486:     const PetscReal *faceBasis = Tc->T[0];
2487:     const PetscInt   Nb        = Tc->Nb;
2488:     const PetscInt   Nc        = Tc->Nc;

2490:     for (c = 0; c < Nc; ++c) u[c] = 0.0;
2491:     for (b = 0; b < Nb; ++b) {
2492:       for (c = 0; c < Nc; ++c) u[c] += coefficients[b] * faceBasis[(faceLoc * Nb + b) * Nc + c];
2493:     }
2494:   }
2495:   return PETSC_SUCCESS;
2496: }

2498: PetscErrorCode PetscFEUpdateElementVec_Internal(PetscFE fe, PetscTabulation T, PetscInt r, PetscScalar tmpBasis[], PetscScalar tmpBasisDer[], PetscInt e, PetscFEGeom *fegeom, PetscScalar f0[], PetscScalar f1[], PetscScalar elemVec[])
2499: {
2500:   PetscFEGeom      pgeom;
2501:   const PetscInt   dEt      = T->cdim;
2502:   const PetscInt   dE       = fegeom->dimEmbed;
2503:   const PetscInt   Nq       = T->Np;
2504:   const PetscInt   Nb       = T->Nb;
2505:   const PetscInt   Nc       = T->Nc;
2506:   const PetscReal *basis    = &T->T[0][r * Nq * Nb * Nc];
2507:   const PetscReal *basisDer = &T->T[1][r * Nq * Nb * Nc * dEt];
2508:   PetscInt         q, b, c, d;

2510:   for (q = 0; q < Nq; ++q) {
2511:     for (b = 0; b < Nb; ++b) {
2512:       for (c = 0; c < Nc; ++c) {
2513:         const PetscInt bcidx = b * Nc + c;

2515:         tmpBasis[bcidx] = basis[q * Nb * Nc + bcidx];
2516:         for (d = 0; d < dEt; ++d) tmpBasisDer[bcidx * dE + d] = basisDer[q * Nb * Nc * dEt + bcidx * dEt + d];
2517:         for (d = dEt; d < dE; ++d) tmpBasisDer[bcidx * dE + d] = 0.0;
2518:       }
2519:     }
2520:     PetscCall(PetscFEGeomGetCellPoint(fegeom, e, q, &pgeom));
2521:     PetscCall(PetscFEPushforward(fe, &pgeom, Nb, tmpBasis));
2522:     PetscCall(PetscFEPushforwardGradient(fe, &pgeom, Nb, tmpBasisDer));
2523:     for (b = 0; b < Nb; ++b) {
2524:       for (c = 0; c < Nc; ++c) {
2525:         const PetscInt bcidx = b * Nc + c;
2526:         const PetscInt qcidx = q * Nc + c;

2528:         elemVec[b] += tmpBasis[bcidx] * f0[qcidx];
2529:         for (d = 0; d < dE; ++d) elemVec[b] += tmpBasisDer[bcidx * dE + d] * f1[qcidx * dE + d];
2530:       }
2531:     }
2532:   }
2533:   return PETSC_SUCCESS;
2534: }

2536: PetscErrorCode PetscFEUpdateElementVec_Hybrid_Internal(PetscFE fe, PetscTabulation T, PetscInt r, PetscInt side, PetscScalar tmpBasis[], PetscScalar tmpBasisDer[], PetscFEGeom *fegeom, PetscScalar f0[], PetscScalar f1[], PetscScalar elemVec[])
2537: {
2538:   const PetscInt   dE       = T->cdim;
2539:   const PetscInt   Nq       = T->Np;
2540:   const PetscInt   Nb       = T->Nb;
2541:   const PetscInt   Nc       = T->Nc;
2542:   const PetscReal *basis    = &T->T[0][r * Nq * Nb * Nc];
2543:   const PetscReal *basisDer = &T->T[1][r * Nq * Nb * Nc * dE];

2545:   for (PetscInt q = 0; q < Nq; ++q) {
2546:     for (PetscInt b = 0; b < Nb; ++b) {
2547:       for (PetscInt c = 0; c < Nc; ++c) {
2548:         const PetscInt bcidx = b * Nc + c;

2550:         tmpBasis[bcidx] = basis[q * Nb * Nc + bcidx];
2551:         for (PetscInt d = 0; d < dE; ++d) tmpBasisDer[bcidx * dE + d] = basisDer[q * Nb * Nc * dE + bcidx * dE + d];
2552:       }
2553:     }
2554:     PetscCall(PetscFEPushforward(fe, fegeom, Nb, tmpBasis));
2555:     // TODO This is currently broken since we do not pull the geometry down to the lower dimension
2556:     // PetscCall(PetscFEPushforwardGradient(fe, fegeom, Nb, tmpBasisDer));
2557:     if (side == 2) {
2558:       // Integrating over whole cohesive cell, so insert for both sides
2559:       for (PetscInt s = 0; s < 2; ++s) {
2560:         for (PetscInt b = 0; b < Nb; ++b) {
2561:           for (PetscInt c = 0; c < Nc; ++c) {
2562:             const PetscInt bcidx = b * Nc + c;
2563:             const PetscInt qcidx = (q * 2 + s) * Nc + c;

2565:             elemVec[Nb * s + b] += tmpBasis[bcidx] * f0[qcidx];
2566:             for (PetscInt d = 0; d < dE; ++d) elemVec[Nb * s + b] += tmpBasisDer[bcidx * dE + d] * f1[qcidx * dE + d];
2567:           }
2568:         }
2569:       }
2570:     } else {
2571:       // Integrating over endcaps of cohesive cell, so insert for correct side
2572:       for (PetscInt b = 0; b < Nb; ++b) {
2573:         for (PetscInt c = 0; c < Nc; ++c) {
2574:           const PetscInt bcidx = b * Nc + c;
2575:           const PetscInt qcidx = q * Nc + c;

2577:           elemVec[Nb * side + b] += tmpBasis[bcidx] * f0[qcidx];
2578:           for (PetscInt d = 0; d < dE; ++d) elemVec[Nb * side + b] += tmpBasisDer[bcidx * dE + d] * f1[qcidx * dE + d];
2579:         }
2580:       }
2581:     }
2582:   }
2583:   return PETSC_SUCCESS;
2584: }

2586: #define petsc_elemmat_kernel_g1(_NbI, _NcI, _NbJ, _NcJ, _dE) \
2587:   do { \
2588:     for (PetscInt fc = 0; fc < (_NcI); ++fc) { \
2589:       for (PetscInt gc = 0; gc < (_NcJ); ++gc) { \
2590:         const PetscScalar *G = g1 + (fc * (_NcJ) + gc) * _dE; \
2591:         for (PetscInt f = 0; f < (_NbI); ++f) { \
2592:           const PetscScalar tBIv = tmpBasisI[f * (_NcI) + fc]; \
2593:           for (PetscInt g = 0; g < (_NbJ); ++g) { \
2594:             const PetscScalar *tBDJ = tmpBasisDerJ + (g * (_NcJ) + gc) * (_dE); \
2595:             PetscScalar        s    = 0.0; \
2596:             for (PetscInt df = 0; df < _dE; ++df) s += G[df] * tBDJ[df]; \
2597:             elemMat[(offsetI + f) * totDim + (offsetJ + g)] += s * tBIv; \
2598:           } \
2599:         } \
2600:       } \
2601:     } \
2602:   } while (0)

2604: #define petsc_elemmat_kernel_g2(_NbI, _NcI, _NbJ, _NcJ, _dE) \
2605:   do { \
2606:     for (PetscInt gc = 0; gc < (_NcJ); ++gc) { \
2607:       for (PetscInt fc = 0; fc < (_NcI); ++fc) { \
2608:         const PetscScalar *G = g2 + (fc * (_NcJ) + gc) * _dE; \
2609:         for (PetscInt g = 0; g < (_NbJ); ++g) { \
2610:           const PetscScalar tBJv = tmpBasisJ[g * (_NcJ) + gc]; \
2611:           for (PetscInt f = 0; f < (_NbI); ++f) { \
2612:             const PetscScalar *tBDI = tmpBasisDerI + (f * (_NcI) + fc) * (_dE); \
2613:             PetscScalar        s    = 0.0; \
2614:             for (PetscInt df = 0; df < _dE; ++df) s += tBDI[df] * G[df]; \
2615:             elemMat[(offsetI + f) * totDim + (offsetJ + g)] += s * tBJv; \
2616:           } \
2617:         } \
2618:       } \
2619:     } \
2620:   } while (0)

2622: #define petsc_elemmat_kernel_g3(_NbI, _NcI, _NbJ, _NcJ, _dE) \
2623:   do { \
2624:     for (PetscInt fc = 0; fc < (_NcI); ++fc) { \
2625:       for (PetscInt gc = 0; gc < (_NcJ); ++gc) { \
2626:         const PetscScalar *G = g3 + (fc * (_NcJ) + gc) * (_dE) * (_dE); \
2627:         for (PetscInt f = 0; f < (_NbI); ++f) { \
2628:           const PetscScalar *tBDI = tmpBasisDerI + (f * (_NcI) + fc) * (_dE); \
2629:           for (PetscInt g = 0; g < (_NbJ); ++g) { \
2630:             PetscScalar        s    = 0.0; \
2631:             const PetscScalar *tBDJ = tmpBasisDerJ + (g * (_NcJ) + gc) * (_dE); \
2632:             for (PetscInt df = 0; df < (_dE); ++df) { \
2633:               for (PetscInt dg = 0; dg < (_dE); ++dg) s += tBDI[df] * G[df * (_dE) + dg] * tBDJ[dg]; \
2634:             } \
2635:             elemMat[(offsetI + f) * totDim + (offsetJ + g)] += s; \
2636:           } \
2637:         } \
2638:       } \
2639:     } \
2640:   } while (0)

2642: PetscErrorCode PetscFEUpdateElementMat_Internal(PetscFE feI, PetscFE feJ, PetscInt r, PetscInt q, PetscTabulation TI, PetscScalar tmpBasisI[], PetscScalar tmpBasisDerI[], PetscTabulation TJ, PetscScalar tmpBasisJ[], PetscScalar tmpBasisDerJ[], PetscFEGeom *fegeom, const PetscScalar g0[], const PetscScalar g1[], const PetscScalar g2[], const PetscScalar g3[], PetscInt totDim, PetscInt offsetI, PetscInt offsetJ, PetscScalar elemMat[])
2643: {
2644:   const PetscInt   cdim      = TI->cdim;
2645:   const PetscInt   dE        = fegeom->dimEmbed;
2646:   const PetscInt   NqI       = TI->Np;
2647:   const PetscInt   NbI       = TI->Nb;
2648:   const PetscInt   NcI       = TI->Nc;
2649:   const PetscReal *basisI    = &TI->T[0][(r * NqI + q) * NbI * NcI];
2650:   const PetscReal *basisDerI = &TI->T[1][(r * NqI + q) * NbI * NcI * cdim];
2651:   const PetscInt   NqJ       = TJ->Np;
2652:   const PetscInt   NbJ       = TJ->Nb;
2653:   const PetscInt   NcJ       = TJ->Nc;
2654:   const PetscReal *basisJ    = &TJ->T[0][(r * NqJ + q) * NbJ * NcJ];
2655:   const PetscReal *basisDerJ = &TJ->T[1][(r * NqJ + q) * NbJ * NcJ * cdim];

2657:   for (PetscInt f = 0; f < NbI; ++f) {
2658:     for (PetscInt fc = 0; fc < NcI; ++fc) {
2659:       const PetscInt fidx = f * NcI + fc; /* Test function basis index */

2661:       tmpBasisI[fidx] = basisI[fidx];
2662:       for (PetscInt df = 0; df < cdim; ++df) tmpBasisDerI[fidx * dE + df] = basisDerI[fidx * cdim + df];
2663:     }
2664:   }
2665:   PetscCall(PetscFEPushforward(feI, fegeom, NbI, tmpBasisI));
2666:   PetscCall(PetscFEPushforwardGradient(feI, fegeom, NbI, tmpBasisDerI));
2667:   if (feI != feJ) {
2668:     for (PetscInt g = 0; g < NbJ; ++g) {
2669:       for (PetscInt gc = 0; gc < NcJ; ++gc) {
2670:         const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */

2672:         tmpBasisJ[gidx] = basisJ[gidx];
2673:         for (PetscInt dg = 0; dg < cdim; ++dg) tmpBasisDerJ[gidx * dE + dg] = basisDerJ[gidx * cdim + dg];
2674:       }
2675:     }
2676:     PetscCall(PetscFEPushforward(feJ, fegeom, NbJ, tmpBasisJ));
2677:     PetscCall(PetscFEPushforwardGradient(feJ, fegeom, NbJ, tmpBasisDerJ));
2678:   } else {
2679:     tmpBasisJ    = tmpBasisI;
2680:     tmpBasisDerJ = tmpBasisDerI;
2681:   }
2682:   if (PetscUnlikely(g0)) {
2683:     for (PetscInt f = 0; f < NbI; ++f) {
2684:       const PetscInt i = offsetI + f; /* Element matrix row */

2686:       for (PetscInt fc = 0; fc < NcI; ++fc) {
2687:         const PetscScalar bI = tmpBasisI[f * NcI + fc]; /* Test function basis value */

2689:         for (PetscInt g = 0; g < NbJ; ++g) {
2690:           const PetscInt j    = offsetJ + g; /* Element matrix column */
2691:           const PetscInt fOff = i * totDim + j;

2693:           for (PetscInt gc = 0; gc < NcJ; ++gc) elemMat[fOff] += bI * g0[fc * NcJ + gc] * tmpBasisJ[g * NcJ + gc];
2694:         }
2695:       }
2696:     }
2697:   }
2698:   if (PetscUnlikely(g1)) {
2699: #if 1
2700:     if (dE == 2) {
2701:       petsc_elemmat_kernel_g1(NbI, NcI, NbJ, NcJ, 2);
2702:     } else if (dE == 3) {
2703:       petsc_elemmat_kernel_g1(NbI, NcI, NbJ, NcJ, 3);
2704:     } else {
2705:       petsc_elemmat_kernel_g1(NbI, NcI, NbJ, NcJ, dE);
2706:     }
2707: #else
2708:     for (PetscInt f = 0; f < NbI; ++f) {
2709:       const PetscInt i = offsetI + f; /* Element matrix row */

2711:       for (PetscInt fc = 0; fc < NcI; ++fc) {
2712:         const PetscScalar bI = tmpBasisI[f * NcI + fc]; /* Test function basis value */

2714:         for (PetscInt g = 0; g < NbJ; ++g) {
2715:           const PetscInt j    = offsetJ + g; /* Element matrix column */
2716:           const PetscInt fOff = i * totDim + j;

2718:           for (PetscInt gc = 0; gc < NcJ; ++gc) {
2719:             const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */

2721:             for (PetscInt df = 0; df < dE; ++df) elemMat[fOff] += bI * g1[(fc * NcJ + gc) * dE + df] * tmpBasisDerJ[gidx * dE + df];
2722:           }
2723:         }
2724:       }
2725:     }
2726: #endif
2727:   }
2728:   if (PetscUnlikely(g2)) {
2729: #if 1
2730:     if (dE == 2) {
2731:       petsc_elemmat_kernel_g2(NbI, NcI, NbJ, NcJ, 2);
2732:     } else if (dE == 3) {
2733:       petsc_elemmat_kernel_g2(NbI, NcI, NbJ, NcJ, 3);
2734:     } else {
2735:       petsc_elemmat_kernel_g2(NbI, NcI, NbJ, NcJ, dE);
2736:     }
2737: #else
2738:     for (PetscInt g = 0; g < NbJ; ++g) {
2739:       const PetscInt j = offsetJ + g; /* Element matrix column */

2741:       for (PetscInt gc = 0; gc < NcJ; ++gc) {
2742:         const PetscScalar bJ = tmpBasisJ[g * NcJ + gc]; /* Trial function basis value */

2744:         for (PetscInt f = 0; f < NbI; ++f) {
2745:           const PetscInt i    = offsetI + f; /* Element matrix row */
2746:           const PetscInt fOff = i * totDim + j;

2748:           for (PetscInt fc = 0; fc < NcI; ++fc) {
2749:             const PetscInt fidx = f * NcI + fc; /* Test function basis index */

2751:             for (PetscInt df = 0; df < dE; ++df) elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g2[(fc * NcJ + gc) * dE + df] * bJ;
2752:           }
2753:         }
2754:       }
2755:     }
2756: #endif
2757:   }
2758:   if (PetscUnlikely(g3)) {
2759: #if 1
2760:     if (dE == 2) {
2761:       petsc_elemmat_kernel_g3(NbI, NcI, NbJ, NcJ, 2);
2762:     } else if (dE == 3) {
2763:       petsc_elemmat_kernel_g3(NbI, NcI, NbJ, NcJ, 3);
2764:     } else {
2765:       petsc_elemmat_kernel_g3(NbI, NcI, NbJ, NcJ, dE);
2766:     }
2767: #else
2768:     for (PetscInt f = 0; f < NbI; ++f) {
2769:       const PetscInt i = offsetI + f; /* Element matrix row */

2771:       for (PetscInt fc = 0; fc < NcI; ++fc) {
2772:         const PetscInt fidx = f * NcI + fc; /* Test function basis index */

2774:         for (PetscInt g = 0; g < NbJ; ++g) {
2775:           const PetscInt j    = offsetJ + g; /* Element matrix column */
2776:           const PetscInt fOff = i * totDim + j;

2778:           for (PetscInt gc = 0; gc < NcJ; ++gc) {
2779:             const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */

2781:             for (PetscInt df = 0; df < dE; ++df) {
2782:               for (PetscInt dg = 0; dg < dE; ++dg) elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g3[((fc * NcJ + gc) * dE + df) * dE + dg] * tmpBasisDerJ[gidx * dE + dg];
2783:             }
2784:           }
2785:         }
2786:       }
2787:     }
2788: #endif
2789:   }
2790:   return PETSC_SUCCESS;
2791: }

2793: #undef petsc_elemmat_kernel_g1
2794: #undef petsc_elemmat_kernel_g2
2795: #undef petsc_elemmat_kernel_g3

2797: PetscErrorCode PetscFEUpdateElementMat_Hybrid_Internal(PetscFE feI, PetscBool isHybridI, PetscFE feJ, PetscBool isHybridJ, PetscInt r, PetscInt s, PetscInt t, PetscInt q, PetscTabulation TI, PetscScalar tmpBasisI[], PetscScalar tmpBasisDerI[], PetscTabulation TJ, PetscScalar tmpBasisJ[], PetscScalar tmpBasisDerJ[], PetscFEGeom *fegeom, const PetscScalar g0[], const PetscScalar g1[], const PetscScalar g2[], const PetscScalar g3[], PetscInt eOffset, PetscInt totDim, PetscInt offsetI, PetscInt offsetJ, PetscScalar elemMat[])
2798: {
2799:   const PetscInt   dE        = TI->cdim;
2800:   const PetscInt   NqI       = TI->Np;
2801:   const PetscInt   NbI       = TI->Nb;
2802:   const PetscInt   NcI       = TI->Nc;
2803:   const PetscReal *basisI    = &TI->T[0][(r * NqI + q) * NbI * NcI];
2804:   const PetscReal *basisDerI = &TI->T[1][(r * NqI + q) * NbI * NcI * dE];
2805:   const PetscInt   NqJ       = TJ->Np;
2806:   const PetscInt   NbJ       = TJ->Nb;
2807:   const PetscInt   NcJ       = TJ->Nc;
2808:   const PetscReal *basisJ    = &TJ->T[0][(r * NqJ + q) * NbJ * NcJ];
2809:   const PetscReal *basisDerJ = &TJ->T[1][(r * NqJ + q) * NbJ * NcJ * dE];
2810:   const PetscInt   so        = isHybridI ? 0 : s;
2811:   const PetscInt   to        = isHybridJ ? 0 : t;
2812:   PetscInt         f, fc, g, gc, df, dg;

2814:   for (f = 0; f < NbI; ++f) {
2815:     for (fc = 0; fc < NcI; ++fc) {
2816:       const PetscInt fidx = f * NcI + fc; /* Test function basis index */

2818:       tmpBasisI[fidx] = basisI[fidx];
2819:       for (df = 0; df < dE; ++df) tmpBasisDerI[fidx * dE + df] = basisDerI[fidx * dE + df];
2820:     }
2821:   }
2822:   PetscCall(PetscFEPushforward(feI, fegeom, NbI, tmpBasisI));
2823:   PetscCall(PetscFEPushforwardGradient(feI, fegeom, NbI, tmpBasisDerI));
2824:   for (g = 0; g < NbJ; ++g) {
2825:     for (gc = 0; gc < NcJ; ++gc) {
2826:       const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */

2828:       tmpBasisJ[gidx] = basisJ[gidx];
2829:       for (dg = 0; dg < dE; ++dg) tmpBasisDerJ[gidx * dE + dg] = basisDerJ[gidx * dE + dg];
2830:     }
2831:   }
2832:   PetscCall(PetscFEPushforward(feJ, fegeom, NbJ, tmpBasisJ));
2833:   // TODO This is currently broken since we do not pull the geometry down to the lower dimension
2834:   // PetscCall(PetscFEPushforwardGradient(feJ, fegeom, NbJ, tmpBasisDerJ));
2835:   for (f = 0; f < NbI; ++f) {
2836:     for (fc = 0; fc < NcI; ++fc) {
2837:       const PetscInt fidx = f * NcI + fc;           /* Test function basis index */
2838:       const PetscInt i    = offsetI + NbI * so + f; /* Element matrix row */
2839:       for (g = 0; g < NbJ; ++g) {
2840:         for (gc = 0; gc < NcJ; ++gc) {
2841:           const PetscInt gidx = g * NcJ + gc;           /* Trial function basis index */
2842:           const PetscInt j    = offsetJ + NbJ * to + g; /* Element matrix column */
2843:           const PetscInt fOff = eOffset + i * totDim + j;

2845:           elemMat[fOff] += tmpBasisI[fidx] * g0[fc * NcJ + gc] * tmpBasisJ[gidx];
2846:           for (df = 0; df < dE; ++df) {
2847:             elemMat[fOff] += tmpBasisI[fidx] * g1[(fc * NcJ + gc) * dE + df] * tmpBasisDerJ[gidx * dE + df];
2848:             elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g2[(fc * NcJ + gc) * dE + df] * tmpBasisJ[gidx];
2849:             for (dg = 0; dg < dE; ++dg) elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g3[((fc * NcJ + gc) * dE + df) * dE + dg] * tmpBasisDerJ[gidx * dE + dg];
2850:           }
2851:         }
2852:       }
2853:     }
2854:   }
2855:   return PETSC_SUCCESS;
2856: }

2858: PetscErrorCode PetscFECreateCellGeometry(PetscFE fe, PetscQuadrature quad, PetscFEGeom *cgeom)
2859: {
2860:   PetscDualSpace  dsp;
2861:   DM              dm;
2862:   PetscQuadrature quadDef;
2863:   PetscInt        dim, cdim, Nq;

2865:   PetscFunctionBegin;
2866:   PetscCall(PetscFEGetDualSpace(fe, &dsp));
2867:   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
2868:   PetscCall(DMGetDimension(dm, &dim));
2869:   PetscCall(DMGetCoordinateDim(dm, &cdim));
2870:   PetscCall(PetscFEGetQuadrature(fe, &quadDef));
2871:   quad = quad ? quad : quadDef;
2872:   PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL));
2873:   PetscCall(PetscMalloc1(Nq * cdim, &cgeom->v));
2874:   PetscCall(PetscMalloc1(Nq * cdim * cdim, &cgeom->J));
2875:   PetscCall(PetscMalloc1(Nq * cdim * cdim, &cgeom->invJ));
2876:   PetscCall(PetscMalloc1(Nq, &cgeom->detJ));
2877:   cgeom->dim       = dim;
2878:   cgeom->dimEmbed  = cdim;
2879:   cgeom->numCells  = 1;
2880:   cgeom->numPoints = Nq;
2881:   PetscCall(DMPlexComputeCellGeometryFEM(dm, 0, quad, cgeom->v, cgeom->J, cgeom->invJ, cgeom->detJ));
2882:   PetscFunctionReturn(PETSC_SUCCESS);
2883: }

2885: PetscErrorCode PetscFEDestroyCellGeometry(PetscFE fe, PetscFEGeom *cgeom)
2886: {
2887:   PetscFunctionBegin;
2888:   PetscCall(PetscFree(cgeom->v));
2889:   PetscCall(PetscFree(cgeom->J));
2890:   PetscCall(PetscFree(cgeom->invJ));
2891:   PetscCall(PetscFree(cgeom->detJ));
2892:   PetscFunctionReturn(PETSC_SUCCESS);
2893: }

2895: #if 0
2896: PetscErrorCode PetscFEUpdateElementMat_Internal_SparseIndices(PetscTabulation TI, PetscTabulation TJ, PetscInt dimEmbed, const PetscInt g0[], const PetscInt g1[], const PetscInt g2[], const PetscInt g3[], PetscInt totDim, PetscInt offsetI, PetscInt offsetJ, PetscInt *n_g0, PetscInt **g0_idxs_out, PetscInt *n_g1, PetscInt **g1_idxs_out, PetscInt *n_g2, PetscInt **g2_idxs_out, PetscInt *n_g3, PetscInt **g3_idxs_out)
2897: {
2898:   const PetscInt dE      = dimEmbed;
2899:   const PetscInt NbI     = TI->Nb;
2900:   const PetscInt NcI     = TI->Nc;
2901:   const PetscInt NbJ     = TJ->Nb;
2902:   const PetscInt NcJ     = TJ->Nc;
2903:   PetscBool      has_g0  = g0 ? PETSC_TRUE : PETSC_FALSE;
2904:   PetscBool      has_g1  = g1 ? PETSC_TRUE : PETSC_FALSE;
2905:   PetscBool      has_g2  = g2 ? PETSC_TRUE : PETSC_FALSE;
2906:   PetscBool      has_g3  = g3 ? PETSC_TRUE : PETSC_FALSE;
2907:   PetscInt      *g0_idxs = NULL, *g1_idxs = NULL, *g2_idxs = NULL, *g3_idxs = NULL;
2908:   PetscInt       g0_i, g1_i, g2_i, g3_i;

2910:   PetscFunctionBegin;
2911:   g0_i = g1_i = g2_i = g3_i = 0;
2912:   if (has_g0)
2913:     for (PetscInt i = 0; i < NcI * NcJ; i++)
2914:       if (g0[i]) g0_i += NbI * NbJ;
2915:   if (has_g1)
2916:     for (PetscInt i = 0; i < NcI * NcJ * dE; i++)
2917:       if (g1[i]) g1_i += NbI * NbJ;
2918:   if (has_g2)
2919:     for (PetscInt i = 0; i < NcI * NcJ * dE; i++)
2920:       if (g2[i]) g2_i += NbI * NbJ;
2921:   if (has_g3)
2922:     for (PetscInt i = 0; i < NcI * NcJ * dE * dE; i++)
2923:       if (g3[i]) g3_i += NbI * NbJ;
2924:   if (g0_i == NbI * NbJ * NcI * NcJ) g0_i = 0;
2925:   if (g1_i == NbI * NbJ * NcI * NcJ * dE) g1_i = 0;
2926:   if (g2_i == NbI * NbJ * NcI * NcJ * dE) g2_i = 0;
2927:   if (g3_i == NbI * NbJ * NcI * NcJ * dE * dE) g3_i = 0;
2928:   has_g0 = g0_i ? PETSC_TRUE : PETSC_FALSE;
2929:   has_g1 = g1_i ? PETSC_TRUE : PETSC_FALSE;
2930:   has_g2 = g2_i ? PETSC_TRUE : PETSC_FALSE;
2931:   has_g3 = g3_i ? PETSC_TRUE : PETSC_FALSE;
2932:   if (has_g0) PetscCall(PetscMalloc1(4 * g0_i, &g0_idxs));
2933:   if (has_g1) PetscCall(PetscMalloc1(4 * g1_i, &g1_idxs));
2934:   if (has_g2) PetscCall(PetscMalloc1(4 * g2_i, &g2_idxs));
2935:   if (has_g3) PetscCall(PetscMalloc1(4 * g3_i, &g3_idxs));
2936:   g0_i = g1_i = g2_i = g3_i = 0;

2938:   for (PetscInt f = 0; f < NbI; ++f) {
2939:     const PetscInt i = offsetI + f; /* Element matrix row */
2940:     for (PetscInt fc = 0; fc < NcI; ++fc) {
2941:       const PetscInt fidx = f * NcI + fc; /* Test function basis index */

2943:       for (PetscInt g = 0; g < NbJ; ++g) {
2944:         const PetscInt j    = offsetJ + g; /* Element matrix column */
2945:         const PetscInt fOff = i * totDim + j;
2946:         for (PetscInt gc = 0; gc < NcJ; ++gc) {
2947:           const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */

2949:           if (has_g0) {
2950:             if (g0[fc * NcJ + gc]) {
2951:               g0_idxs[4 * g0_i + 0] = fidx;
2952:               g0_idxs[4 * g0_i + 1] = fc * NcJ + gc;
2953:               g0_idxs[4 * g0_i + 2] = gidx;
2954:               g0_idxs[4 * g0_i + 3] = fOff;
2955:               g0_i++;
2956:             }
2957:           }

2959:           for (PetscInt df = 0; df < dE; ++df) {
2960:             if (has_g1) {
2961:               if (g1[(fc * NcJ + gc) * dE + df]) {
2962:                 g1_idxs[4 * g1_i + 0] = fidx;
2963:                 g1_idxs[4 * g1_i + 1] = (fc * NcJ + gc) * dE + df;
2964:                 g1_idxs[4 * g1_i + 2] = gidx * dE + df;
2965:                 g1_idxs[4 * g1_i + 3] = fOff;
2966:                 g1_i++;
2967:               }
2968:             }
2969:             if (has_g2) {
2970:               if (g2[(fc * NcJ + gc) * dE + df]) {
2971:                 g2_idxs[4 * g2_i + 0] = fidx * dE + df;
2972:                 g2_idxs[4 * g2_i + 1] = (fc * NcJ + gc) * dE + df;
2973:                 g2_idxs[4 * g2_i + 2] = gidx;
2974:                 g2_idxs[4 * g2_i + 3] = fOff;
2975:                 g2_i++;
2976:               }
2977:             }
2978:             if (has_g3) {
2979:               for (PetscInt dg = 0; dg < dE; ++dg) {
2980:                 if (g3[((fc * NcJ + gc) * dE + df) * dE + dg]) {
2981:                   g3_idxs[4 * g3_i + 0] = fidx * dE + df;
2982:                   g3_idxs[4 * g3_i + 1] = ((fc * NcJ + gc) * dE + df) * dE + dg;
2983:                   g3_idxs[4 * g3_i + 2] = gidx * dE + dg;
2984:                   g3_idxs[4 * g3_i + 3] = fOff;
2985:                   g3_i++;
2986:                 }
2987:               }
2988:             }
2989:           }
2990:         }
2991:       }
2992:     }
2993:   }
2994:   *n_g0 = g0_i;
2995:   *n_g1 = g1_i;
2996:   *n_g2 = g2_i;
2997:   *n_g3 = g3_i;

2999:   *g0_idxs_out = g0_idxs;
3000:   *g1_idxs_out = g1_idxs;
3001:   *g2_idxs_out = g2_idxs;
3002:   *g3_idxs_out = g3_idxs;
3003:   PetscFunctionReturn(PETSC_SUCCESS);
3004: }

3006: //example HOW TO USE
3007:       for (PetscInt i = 0; i < g0_sparse_n; i++) {
3008:         PetscInt bM = g0_sparse_idxs[4 * i + 0];
3009:         PetscInt bN = g0_sparse_idxs[4 * i + 1];
3010:         PetscInt bK = g0_sparse_idxs[4 * i + 2];
3011:         PetscInt bO = g0_sparse_idxs[4 * i + 3];
3012:         elemMat[bO] += tmpBasisI[bM] * g0[bN] * tmpBasisJ[bK];
3013:       }
3014: #endif