Actual source code: dmplexsnes.c

petsc-master 2020-05-29
Report Typos and Errors
  1:  #include <petsc/private/dmpleximpl.h>
  2:  #include <petsc/private/snesimpl.h>
  3:  #include <petscds.h>
  4:  #include <petscblaslapack.h>
  5:  #include <petsc/private/petscimpl.h>
  6:  #include <petsc/private/petscfeimpl.h>

  8: /************************** Interpolation *******************************/

 10: static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy)
 11: {
 12:   PetscBool      isPlex;

 16:   PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);
 17:   if (isPlex) {
 18:     *plex = dm;
 19:     PetscObjectReference((PetscObject) dm);
 20:   } else {
 21:     PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);
 22:     if (!*plex) {
 23:       DMConvert(dm,DMPLEX,plex);
 24:       PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);
 25:       if (copy) {
 26:         PetscInt    i;
 27:         PetscObject obj;
 28:         const char *comps[3] = {"A","dmAux","dmCh"};

 30:         DMCopyDMSNES(dm, *plex);
 31:         for (i = 0; i < 3; i++) {
 32:           PetscObjectQuery((PetscObject) dm, comps[i], &obj);
 33:           PetscObjectCompose((PetscObject) *plex, comps[i], obj);
 34:         }
 35:       }
 36:     } else {
 37:       PetscObjectReference((PetscObject) *plex);
 38:     }
 39:   }
 40:   return(0);
 41: }

 43: /*@C
 44:   DMInterpolationCreate - Creates a DMInterpolationInfo context

 46:   Collective

 48:   Input Parameter:
 49: . comm - the communicator

 51:   Output Parameter:
 52: . ctx - the context

 54:   Level: beginner

 56: .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationDestroy()
 57: @*/
 58: PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
 59: {

 64:   PetscNew(ctx);

 66:   (*ctx)->comm   = comm;
 67:   (*ctx)->dim    = -1;
 68:   (*ctx)->nInput = 0;
 69:   (*ctx)->points = NULL;
 70:   (*ctx)->cells  = NULL;
 71:   (*ctx)->n      = -1;
 72:   (*ctx)->coords = NULL;
 73:   return(0);
 74: }

 76: /*@C
 77:   DMInterpolationSetDim - Sets the spatial dimension for the interpolation context

 79:   Not collective

 81:   Input Parameters:
 82: + ctx - the context
 83: - dim - the spatial dimension

 85:   Level: intermediate

 87: .seealso: DMInterpolationGetDim(), DMInterpolationEvaluate(), DMInterpolationAddPoints()
 88: @*/
 89: PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
 90: {
 92:   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %D", dim);
 93:   ctx->dim = dim;
 94:   return(0);
 95: }

 97: /*@C
 98:   DMInterpolationGetDim - Gets the spatial dimension for the interpolation context

100:   Not collective

102:   Input Parameter:
103: . ctx - the context

105:   Output Parameter:
106: . dim - the spatial dimension

108:   Level: intermediate

110: .seealso: DMInterpolationSetDim(), DMInterpolationEvaluate(), DMInterpolationAddPoints()
111: @*/
112: PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
113: {
116:   *dim = ctx->dim;
117:   return(0);
118: }

120: /*@C
121:   DMInterpolationSetDof - Sets the number of fields interpolated at a point for the interpolation context

123:   Not collective

125:   Input Parameters:
126: + ctx - the context
127: - dof - the number of fields

129:   Level: intermediate

131: .seealso: DMInterpolationGetDof(), DMInterpolationEvaluate(), DMInterpolationAddPoints()
132: @*/
133: PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
134: {
136:   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %D", dof);
137:   ctx->dof = dof;
138:   return(0);
139: }

141: /*@C
142:   DMInterpolationGetDof - Gets the number of fields interpolated at a point for the interpolation context

144:   Not collective

146:   Input Parameter:
147: . ctx - the context

149:   Output Parameter:
150: . dof - the number of fields

152:   Level: intermediate

154: .seealso: DMInterpolationSetDof(), DMInterpolationEvaluate(), DMInterpolationAddPoints()
155: @*/
156: PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
157: {
160:   *dof = ctx->dof;
161:   return(0);
162: }

164: /*@C
165:   DMInterpolationAddPoints - Add points at which we will interpolate the fields

167:   Not collective

169:   Input Parameters:
170: + ctx    - the context
171: . n      - the number of points
172: - points - the coordinates for each point, an array of size n * dim

174:   Note: The coordinate information is copied.

176:   Level: intermediate

178: .seealso: DMInterpolationSetDim(), DMInterpolationEvaluate(), DMInterpolationCreate()
179: @*/
180: PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
181: {

185:   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
186:   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
187:   ctx->nInput = n;

189:   PetscMalloc1(n*ctx->dim, &ctx->points);
190:   PetscArraycpy(ctx->points, points, n*ctx->dim);
191:   return(0);
192: }

194: /*@C
195:   DMInterpolationSetUp - Computea spatial indices that add in point location during interpolation

197:   Collective on ctx

199:   Input Parameters:
200: + ctx - the context
201: . dm  - the DM for the function space used for interpolation
202: - redundantPoints - If PETSC_TRUE, all processes are passing in the same array of points. Otherwise, points need to be communicated among processes.

204:   Level: intermediate

206: .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate()
207: @*/
208: PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
209: {
210:   MPI_Comm          comm = ctx->comm;
211:   PetscScalar       *a;
212:   PetscInt          p, q, i;
213:   PetscMPIInt       rank, size;
214:   PetscErrorCode    ierr;
215:   Vec               pointVec;
216:   PetscSF           cellSF;
217:   PetscLayout       layout;
218:   PetscReal         *globalPoints;
219:   PetscScalar       *globalPointsScalar;
220:   const PetscInt    *ranges;
221:   PetscMPIInt       *counts, *displs;
222:   const PetscSFNode *foundCells;
223:   const PetscInt    *foundPoints;
224:   PetscMPIInt       *foundProcs, *globalProcs;
225:   PetscInt          n, N, numFound;

229:   MPI_Comm_size(comm, &size);
230:   MPI_Comm_rank(comm, &rank);
231:   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
232:   /* Locate points */
233:   n = ctx->nInput;
234:   if (!redundantPoints) {
235:     PetscLayoutCreate(comm, &layout);
236:     PetscLayoutSetBlockSize(layout, 1);
237:     PetscLayoutSetLocalSize(layout, n);
238:     PetscLayoutSetUp(layout);
239:     PetscLayoutGetSize(layout, &N);
240:     /* Communicate all points to all processes */
241:     PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);
242:     PetscLayoutGetRanges(layout, &ranges);
243:     for (p = 0; p < size; ++p) {
244:       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
245:       displs[p] = ranges[p]*ctx->dim;
246:     }
247:     MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);
248:   } else {
249:     N = n;
250:     globalPoints = ctx->points;
251:     counts = displs = NULL;
252:     layout = NULL;
253:   }
254: #if 0
255:   PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);
256:   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
257: #else
258: #if defined(PETSC_USE_COMPLEX)
259:   PetscMalloc1(N*ctx->dim,&globalPointsScalar);
260:   for (i=0; i<N*ctx->dim; i++) globalPointsScalar[i] = globalPoints[i];
261: #else
262:   globalPointsScalar = globalPoints;
263: #endif
264:   VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);
265:   PetscMalloc2(N,&foundProcs,N,&globalProcs);
266:   for (p = 0; p < N; ++p) {foundProcs[p] = size;}
267:   cellSF = NULL;
268:   DMLocatePoints(dm, pointVec, DM_POINTLOCATION_REMOVE, &cellSF);
269:   PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);
270: #endif
271:   for (p = 0; p < numFound; ++p) {
272:     if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank;
273:   }
274:   /* Let the lowest rank process own each point */
275:   MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);
276:   ctx->n = 0;
277:   for (p = 0; p < N; ++p) {
278:     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, (double)globalPoints[p*ctx->dim+0], (double)(ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0), (double)(ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0));
279:     else if (globalProcs[p] == rank) ctx->n++;
280:   }
281:   /* Create coordinates vector and array of owned cells */
282:   PetscMalloc1(ctx->n, &ctx->cells);
283:   VecCreate(comm, &ctx->coords);
284:   VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);
285:   VecSetBlockSize(ctx->coords, ctx->dim);
286:   VecSetType(ctx->coords,VECSTANDARD);
287:   VecGetArray(ctx->coords, &a);
288:   for (p = 0, q = 0, i = 0; p < N; ++p) {
289:     if (globalProcs[p] == rank) {
290:       PetscInt d;

292:       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
293:       ctx->cells[q] = foundCells[q].index;
294:       ++q;
295:     }
296:   }
297:   VecRestoreArray(ctx->coords, &a);
298: #if 0
299:   PetscFree3(foundCells,foundProcs,globalProcs);
300: #else
301:   PetscFree2(foundProcs,globalProcs);
302:   PetscSFDestroy(&cellSF);
303:   VecDestroy(&pointVec);
304: #endif
305:   if ((void*)globalPointsScalar != (void*)globalPoints) {PetscFree(globalPointsScalar);}
306:   if (!redundantPoints) {PetscFree3(globalPoints,counts,displs);}
307:   PetscLayoutDestroy(&layout);
308:   return(0);
309: }

311: /*@C
312:   DMInterpolationGetCoordinates - Gets a Vec with the coordinates of each interpolation point

314:   Collective on ctx

316:   Input Parameter:
317: . ctx - the context

319:   Output Parameter:
320: . coordinates  - the coordinates of interpolation points

322:   Note: The local vector entries correspond to interpolation points lying on this process, according to the associated DM. This is a borrowed vector that the user should not destroy.

324:   Level: intermediate

326: .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate()
327: @*/
328: PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
329: {
332:   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
333:   *coordinates = ctx->coords;
334:   return(0);
335: }

337: /*@C
338:   DMInterpolationGetVector - Gets a Vec which can hold all the interpolated field values

340:   Collective on ctx

342:   Input Parameter:
343: . ctx - the context

345:   Output Parameter:
346: . v  - a vector capable of holding the interpolated field values

348:   Note: This vector should be returned using DMInterpolationRestoreVector().

350:   Level: intermediate

352: .seealso: DMInterpolationRestoreVector(), DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate()
353: @*/
354: PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
355: {

360:   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
361:   VecCreate(ctx->comm, v);
362:   VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);
363:   VecSetBlockSize(*v, ctx->dof);
364:   VecSetType(*v,VECSTANDARD);
365:   return(0);
366: }

368: /*@C
369:   DMInterpolationRestoreVector - Returns a Vec which can hold all the interpolated field values

371:   Collective on ctx

373:   Input Parameters:
374: + ctx - the context
375: - v  - a vector capable of holding the interpolated field values

377:   Level: intermediate

379: .seealso: DMInterpolationGetVector(), DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate()
380: @*/
381: PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
382: {

387:   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
388:   VecDestroy(v);
389:   return(0);
390: }

392: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
393: {
394:   PetscReal      *v0, *J, *invJ, detJ;
395:   const PetscScalar *coords;
396:   PetscScalar    *a;
397:   PetscInt       p;

401:   PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);
402:   VecGetArrayRead(ctx->coords, &coords);
403:   VecGetArray(v, &a);
404:   for (p = 0; p < ctx->n; ++p) {
405:     PetscInt     c = ctx->cells[p];
406:     PetscScalar *x = NULL;
407:     PetscReal    xi[4];
408:     PetscInt     d, f, comp;

410:     DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);
411:     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D", (double)detJ, c);
412:     DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);
413:     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];

415:     for (d = 0; d < ctx->dim; ++d) {
416:       xi[d] = 0.0;
417:       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
418:       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
419:     }
420:     DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);
421:   }
422:   VecRestoreArray(v, &a);
423:   VecRestoreArrayRead(ctx->coords, &coords);
424:   PetscFree3(v0, J, invJ);
425:   return(0);
426: }

428: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
429: {
430:   PetscReal      *v0, *J, *invJ, detJ;
431:   const PetscScalar *coords;
432:   PetscScalar    *a;
433:   PetscInt       p;

437:   PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);
438:   VecGetArrayRead(ctx->coords, &coords);
439:   VecGetArray(v, &a);
440:   for (p = 0; p < ctx->n; ++p) {
441:     PetscInt       c = ctx->cells[p];
442:     const PetscInt order[3] = {2, 1, 3};
443:     PetscScalar   *x = NULL;
444:     PetscReal      xi[4];
445:     PetscInt       d, f, comp;

447:     DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);
448:     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D", (double)detJ, c);
449:     DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);
450:     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];

452:     for (d = 0; d < ctx->dim; ++d) {
453:       xi[d] = 0.0;
454:       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
455:       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
456:     }
457:     DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);
458:   }
459:   VecRestoreArray(v, &a);
460:   VecRestoreArrayRead(ctx->coords, &coords);
461:   PetscFree3(v0, J, invJ);
462:   return(0);
463: }

465: PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
466: {
467:   const PetscScalar *vertices = (const PetscScalar*) ctx;
468:   const PetscScalar x0        = vertices[0];
469:   const PetscScalar y0        = vertices[1];
470:   const PetscScalar x1        = vertices[2];
471:   const PetscScalar y1        = vertices[3];
472:   const PetscScalar x2        = vertices[4];
473:   const PetscScalar y2        = vertices[5];
474:   const PetscScalar x3        = vertices[6];
475:   const PetscScalar y3        = vertices[7];
476:   const PetscScalar f_1       = x1 - x0;
477:   const PetscScalar g_1       = y1 - y0;
478:   const PetscScalar f_3       = x3 - x0;
479:   const PetscScalar g_3       = y3 - y0;
480:   const PetscScalar f_01      = x2 - x1 - x3 + x0;
481:   const PetscScalar g_01      = y2 - y1 - y3 + y0;
482:   const PetscScalar *ref;
483:   PetscScalar       *real;
484:   PetscErrorCode    ierr;

487:   VecGetArrayRead(Xref,  &ref);
488:   VecGetArray(Xreal, &real);
489:   {
490:     const PetscScalar p0 = ref[0];
491:     const PetscScalar p1 = ref[1];

493:     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
494:     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
495:   }
496:   PetscLogFlops(28);
497:   VecRestoreArrayRead(Xref,  &ref);
498:   VecRestoreArray(Xreal, &real);
499:   return(0);
500: }

502:  #include <petsc/private/dmimpl.h>
503: PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
504: {
505:   const PetscScalar *vertices = (const PetscScalar*) ctx;
506:   const PetscScalar x0        = vertices[0];
507:   const PetscScalar y0        = vertices[1];
508:   const PetscScalar x1        = vertices[2];
509:   const PetscScalar y1        = vertices[3];
510:   const PetscScalar x2        = vertices[4];
511:   const PetscScalar y2        = vertices[5];
512:   const PetscScalar x3        = vertices[6];
513:   const PetscScalar y3        = vertices[7];
514:   const PetscScalar f_01      = x2 - x1 - x3 + x0;
515:   const PetscScalar g_01      = y2 - y1 - y3 + y0;
516:   const PetscScalar *ref;
517:   PetscErrorCode    ierr;

520:   VecGetArrayRead(Xref,  &ref);
521:   {
522:     const PetscScalar x       = ref[0];
523:     const PetscScalar y       = ref[1];
524:     const PetscInt    rows[2] = {0, 1};
525:     PetscScalar       values[4];

527:     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
528:     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
529:     MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);
530:   }
531:   PetscLogFlops(30);
532:   VecRestoreArrayRead(Xref,  &ref);
533:   MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);
534:   MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);
535:   return(0);
536: }

538: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
539: {
540:   DM             dmCoord;
541:   PetscFE        fem = NULL;
542:   SNES           snes;
543:   KSP            ksp;
544:   PC             pc;
545:   Vec            coordsLocal, r, ref, real;
546:   Mat            J;
547:   PetscTabulation    T;
548:   const PetscScalar *coords;
549:   PetscScalar    *a;
550:   PetscReal      xir[2];
551:   PetscInt       Nf, p;
552:   const PetscInt dof = ctx->dof;

556:   DMGetNumFields(dm, &Nf);
557:   if (Nf) {DMGetField(dm, 0, NULL, (PetscObject *) &fem);}
558:   DMGetCoordinatesLocal(dm, &coordsLocal);
559:   DMGetCoordinateDM(dm, &dmCoord);
560:   SNESCreate(PETSC_COMM_SELF, &snes);
561:   SNESSetOptionsPrefix(snes, "quad_interp_");
562:   VecCreate(PETSC_COMM_SELF, &r);
563:   VecSetSizes(r, 2, 2);
564:   VecSetType(r,dm->vectype);
565:   VecDuplicate(r, &ref);
566:   VecDuplicate(r, &real);
567:   MatCreate(PETSC_COMM_SELF, &J);
568:   MatSetSizes(J, 2, 2, 2, 2);
569:   MatSetType(J, MATSEQDENSE);
570:   MatSetUp(J);
571:   SNESSetFunction(snes, r, QuadMap_Private, NULL);
572:   SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);
573:   SNESGetKSP(snes, &ksp);
574:   KSPGetPC(ksp, &pc);
575:   PCSetType(pc, PCLU);
576:   SNESSetFromOptions(snes);

578:   VecGetArrayRead(ctx->coords, &coords);
579:   VecGetArray(v, &a);
580:   PetscFECreateTabulation(fem, 1, 1, xir, 0, &T);
581:   for (p = 0; p < ctx->n; ++p) {
582:     PetscScalar *x = NULL, *vertices = NULL;
583:     PetscScalar *xi;
584:     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;

586:     /* Can make this do all points at once */
587:     DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
588:     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %d", coordSize, 4*2);
589:     DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);
590:     SNESSetFunction(snes, NULL, NULL, (void*) vertices);
591:     SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);
592:     VecGetArray(real, &xi);
593:     xi[0]  = coords[p*ctx->dim+0];
594:     xi[1]  = coords[p*ctx->dim+1];
595:     VecRestoreArray(real, &xi);
596:     SNESSolve(snes, real, ref);
597:     VecGetArray(ref, &xi);
598:     xir[0] = PetscRealPart(xi[0]);
599:     xir[1] = PetscRealPart(xi[1]);
600:     if (4*dof != xSize) {
601:       PetscInt d;

603:       xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0;
604:       PetscFEComputeTabulation(fem, 1, xir, 0, T);
605:       for (comp = 0; comp < dof; ++comp) {
606:         a[p*dof+comp] = 0.0;
607:         for (d = 0; d < xSize/dof; ++d) {
608:           a[p*dof+comp] += x[d*dof+comp]*T->T[0][d*dof+comp];
609:         }
610:       }
611:     } else {
612:       for (comp = 0; comp < dof; ++comp)
613:         a[p*dof+comp] = x[0*dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*dof+comp]*xir[0]*(1 - xir[1]) + x[2*dof+comp]*xir[0]*xir[1] + x[3*dof+comp]*(1 - xir[0])*xir[1];
614:     }
615:     VecRestoreArray(ref, &xi);
616:     DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
617:     DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);
618:   }
619:   PetscTabulationDestroy(&T);
620:   VecRestoreArray(v, &a);
621:   VecRestoreArrayRead(ctx->coords, &coords);

623:   SNESDestroy(&snes);
624:   VecDestroy(&r);
625:   VecDestroy(&ref);
626:   VecDestroy(&real);
627:   MatDestroy(&J);
628:   return(0);
629: }

631: PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
632: {
633:   const PetscScalar *vertices = (const PetscScalar*) ctx;
634:   const PetscScalar x0        = vertices[0];
635:   const PetscScalar y0        = vertices[1];
636:   const PetscScalar z0        = vertices[2];
637:   const PetscScalar x1        = vertices[9];
638:   const PetscScalar y1        = vertices[10];
639:   const PetscScalar z1        = vertices[11];
640:   const PetscScalar x2        = vertices[6];
641:   const PetscScalar y2        = vertices[7];
642:   const PetscScalar z2        = vertices[8];
643:   const PetscScalar x3        = vertices[3];
644:   const PetscScalar y3        = vertices[4];
645:   const PetscScalar z3        = vertices[5];
646:   const PetscScalar x4        = vertices[12];
647:   const PetscScalar y4        = vertices[13];
648:   const PetscScalar z4        = vertices[14];
649:   const PetscScalar x5        = vertices[15];
650:   const PetscScalar y5        = vertices[16];
651:   const PetscScalar z5        = vertices[17];
652:   const PetscScalar x6        = vertices[18];
653:   const PetscScalar y6        = vertices[19];
654:   const PetscScalar z6        = vertices[20];
655:   const PetscScalar x7        = vertices[21];
656:   const PetscScalar y7        = vertices[22];
657:   const PetscScalar z7        = vertices[23];
658:   const PetscScalar f_1       = x1 - x0;
659:   const PetscScalar g_1       = y1 - y0;
660:   const PetscScalar h_1       = z1 - z0;
661:   const PetscScalar f_3       = x3 - x0;
662:   const PetscScalar g_3       = y3 - y0;
663:   const PetscScalar h_3       = z3 - z0;
664:   const PetscScalar f_4       = x4 - x0;
665:   const PetscScalar g_4       = y4 - y0;
666:   const PetscScalar h_4       = z4 - z0;
667:   const PetscScalar f_01      = x2 - x1 - x3 + x0;
668:   const PetscScalar g_01      = y2 - y1 - y3 + y0;
669:   const PetscScalar h_01      = z2 - z1 - z3 + z0;
670:   const PetscScalar f_12      = x7 - x3 - x4 + x0;
671:   const PetscScalar g_12      = y7 - y3 - y4 + y0;
672:   const PetscScalar h_12      = z7 - z3 - z4 + z0;
673:   const PetscScalar f_02      = x5 - x1 - x4 + x0;
674:   const PetscScalar g_02      = y5 - y1 - y4 + y0;
675:   const PetscScalar h_02      = z5 - z1 - z4 + z0;
676:   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
677:   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
678:   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
679:   const PetscScalar *ref;
680:   PetscScalar       *real;
681:   PetscErrorCode    ierr;

684:   VecGetArrayRead(Xref,  &ref);
685:   VecGetArray(Xreal, &real);
686:   {
687:     const PetscScalar p0 = ref[0];
688:     const PetscScalar p1 = ref[1];
689:     const PetscScalar p2 = ref[2];

691:     real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2;
692:     real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2;
693:     real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2;
694:   }
695:   PetscLogFlops(114);
696:   VecRestoreArrayRead(Xref,  &ref);
697:   VecRestoreArray(Xreal, &real);
698:   return(0);
699: }

701: PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
702: {
703:   const PetscScalar *vertices = (const PetscScalar*) ctx;
704:   const PetscScalar x0        = vertices[0];
705:   const PetscScalar y0        = vertices[1];
706:   const PetscScalar z0        = vertices[2];
707:   const PetscScalar x1        = vertices[9];
708:   const PetscScalar y1        = vertices[10];
709:   const PetscScalar z1        = vertices[11];
710:   const PetscScalar x2        = vertices[6];
711:   const PetscScalar y2        = vertices[7];
712:   const PetscScalar z2        = vertices[8];
713:   const PetscScalar x3        = vertices[3];
714:   const PetscScalar y3        = vertices[4];
715:   const PetscScalar z3        = vertices[5];
716:   const PetscScalar x4        = vertices[12];
717:   const PetscScalar y4        = vertices[13];
718:   const PetscScalar z4        = vertices[14];
719:   const PetscScalar x5        = vertices[15];
720:   const PetscScalar y5        = vertices[16];
721:   const PetscScalar z5        = vertices[17];
722:   const PetscScalar x6        = vertices[18];
723:   const PetscScalar y6        = vertices[19];
724:   const PetscScalar z6        = vertices[20];
725:   const PetscScalar x7        = vertices[21];
726:   const PetscScalar y7        = vertices[22];
727:   const PetscScalar z7        = vertices[23];
728:   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
729:   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
730:   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
731:   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
732:   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
733:   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
734:   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
735:   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
736:   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
737:   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
738:   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
739:   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
740:   const PetscScalar *ref;
741:   PetscErrorCode    ierr;

744:   VecGetArrayRead(Xref,  &ref);
745:   {
746:     const PetscScalar x       = ref[0];
747:     const PetscScalar y       = ref[1];
748:     const PetscScalar z       = ref[2];
749:     const PetscInt    rows[3] = {0, 1, 2};
750:     PetscScalar       values[9];

752:     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
753:     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
754:     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
755:     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
756:     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
757:     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
758:     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
759:     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
760:     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;

762:     MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);
763:   }
764:   PetscLogFlops(152);
765:   VecRestoreArrayRead(Xref,  &ref);
766:   MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);
767:   MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);
768:   return(0);
769: }

771: PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
772: {
773:   DM             dmCoord;
774:   SNES           snes;
775:   KSP            ksp;
776:   PC             pc;
777:   Vec            coordsLocal, r, ref, real;
778:   Mat            J;
779:   const PetscScalar *coords;
780:   PetscScalar    *a;
781:   PetscInt       p;

785:   DMGetCoordinatesLocal(dm, &coordsLocal);
786:   DMGetCoordinateDM(dm, &dmCoord);
787:   SNESCreate(PETSC_COMM_SELF, &snes);
788:   SNESSetOptionsPrefix(snes, "hex_interp_");
789:   VecCreate(PETSC_COMM_SELF, &r);
790:   VecSetSizes(r, 3, 3);
791:   VecSetType(r,dm->vectype);
792:   VecDuplicate(r, &ref);
793:   VecDuplicate(r, &real);
794:   MatCreate(PETSC_COMM_SELF, &J);
795:   MatSetSizes(J, 3, 3, 3, 3);
796:   MatSetType(J, MATSEQDENSE);
797:   MatSetUp(J);
798:   SNESSetFunction(snes, r, HexMap_Private, NULL);
799:   SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);
800:   SNESGetKSP(snes, &ksp);
801:   KSPGetPC(ksp, &pc);
802:   PCSetType(pc, PCLU);
803:   SNESSetFromOptions(snes);

805:   VecGetArrayRead(ctx->coords, &coords);
806:   VecGetArray(v, &a);
807:   for (p = 0; p < ctx->n; ++p) {
808:     PetscScalar *x = NULL, *vertices = NULL;
809:     PetscScalar *xi;
810:     PetscReal    xir[3];
811:     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;

813:     /* Can make this do all points at once */
814:     DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
815:     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %d", coordSize, 8*3);
816:     DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);
817:     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %D", xSize, 8*ctx->dof);
818:     SNESSetFunction(snes, NULL, NULL, (void*) vertices);
819:     SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);
820:     VecGetArray(real, &xi);
821:     xi[0]  = coords[p*ctx->dim+0];
822:     xi[1]  = coords[p*ctx->dim+1];
823:     xi[2]  = coords[p*ctx->dim+2];
824:     VecRestoreArray(real, &xi);
825:     SNESSolve(snes, real, ref);
826:     VecGetArray(ref, &xi);
827:     xir[0] = PetscRealPart(xi[0]);
828:     xir[1] = PetscRealPart(xi[1]);
829:     xir[2] = PetscRealPart(xi[2]);
830:     for (comp = 0; comp < ctx->dof; ++comp) {
831:       a[p*ctx->dof+comp] =
832:         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
833:         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
834:         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
835:         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
836:         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
837:         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
838:         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
839:         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
840:     }
841:     VecRestoreArray(ref, &xi);
842:     DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);
843:     DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);
844:   }
845:   VecRestoreArray(v, &a);
846:   VecRestoreArrayRead(ctx->coords, &coords);

848:   SNESDestroy(&snes);
849:   VecDestroy(&r);
850:   VecDestroy(&ref);
851:   VecDestroy(&real);
852:   MatDestroy(&J);
853:   return(0);
854: }

856: /*@C
857:   DMInterpolationEvaluate - Using the input from dm and x, calculates interpolated field values at the interpolation points.

859:   Input Parameters:
860: + ctx - The DMInterpolationInfo context
861: . dm  - The DM
862: - x   - The local vector containing the field to be interpolated

864:   Output Parameters:
865: . v   - The vector containing the interpolated values

867:   Note: A suitable v can be obtained using DMInterpolationGetVector().

869:   Level: beginner

871: .seealso: DMInterpolationGetVector(), DMInterpolationAddPoints(), DMInterpolationCreate()
872: @*/
873: PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
874: {
875:   PetscInt       dim, coneSize, n;

882:   VecGetLocalSize(v, &n);
883:   if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %D should be %D", n, ctx->n*ctx->dof);
884:   if (n) {
885:     DMGetDimension(dm, &dim);
886:     DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);
887:     if (dim == 2) {
888:       if (coneSize == 3) {
889:         DMInterpolate_Triangle_Private(ctx, dm, x, v);
890:       } else if (coneSize == 4) {
891:         DMInterpolate_Quad_Private(ctx, dm, x, v);
892:       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %D for point interpolation", dim);
893:     } else if (dim == 3) {
894:       if (coneSize == 4) {
895:         DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);
896:       } else {
897:         DMInterpolate_Hex_Private(ctx, dm, x, v);
898:       }
899:     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %D for point interpolation", dim);
900:   }
901:   return(0);
902: }

904: /*@C
905:   DMInterpolationDestroy - Destroys a DMInterpolationInfo context

907:   Collective on ctx

909:   Input Parameter:
910: . ctx - the context

912:   Level: beginner

914: .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate()
915: @*/
916: PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
917: {

922:   VecDestroy(&(*ctx)->coords);
923:   PetscFree((*ctx)->points);
924:   PetscFree((*ctx)->cells);
925:   PetscFree(*ctx);
926:   *ctx = NULL;
927:   return(0);
928: }

930: /*@C
931:   SNESMonitorFields - Monitors the residual for each field separately

933:   Collective on SNES

935:   Input Parameters:
936: + snes   - the SNES context
937: . its    - iteration number
938: . fgnorm - 2-norm of residual
939: - vf  - PetscViewerAndFormat of type ASCII

941:   Notes:
942:   This routine prints the residual norm at each iteration.

944:   Level: intermediate

946: .seealso: SNESMonitorSet(), SNESMonitorDefault()
947: @*/
948: PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
949: {
950:   PetscViewer        viewer = vf->viewer;
951:   Vec                res;
952:   DM                 dm;
953:   PetscSection       s;
954:   const PetscScalar *r;
955:   PetscReal         *lnorms, *norms;
956:   PetscInt           numFields, f, pStart, pEnd, p;
957:   PetscErrorCode     ierr;

961:   SNESGetFunction(snes, &res, NULL, NULL);
962:   SNESGetDM(snes, &dm);
963:   DMGetLocalSection(dm, &s);
964:   PetscSectionGetNumFields(s, &numFields);
965:   PetscSectionGetChart(s, &pStart, &pEnd);
966:   PetscCalloc2(numFields, &lnorms, numFields, &norms);
967:   VecGetArrayRead(res, &r);
968:   for (p = pStart; p < pEnd; ++p) {
969:     for (f = 0; f < numFields; ++f) {
970:       PetscInt fdof, foff, d;

972:       PetscSectionGetFieldDof(s, p, f, &fdof);
973:       PetscSectionGetFieldOffset(s, p, f, &foff);
974:       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
975:     }
976:   }
977:   VecRestoreArrayRead(res, &r);
978:   MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));
979:   PetscViewerPushFormat(viewer,vf->format);
980:   PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);
981:   PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);
982:   for (f = 0; f < numFields; ++f) {
983:     if (f > 0) {PetscViewerASCIIPrintf(viewer, ", ");}
984:     PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));
985:   }
986:   PetscViewerASCIIPrintf(viewer, "]\n");
987:   PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);
988:   PetscViewerPopFormat(viewer);
989:   PetscFree2(lnorms, norms);
990:   return(0);
991: }

993: /********************* Residual Computation **************************/


996: /*@
997:   DMPlexSNESGetGeometryFVM - Return precomputed geometric data

999:   Input Parameter:
1000: . dm - The DM

1002:   Output Parameters:
1003: + facegeom - The values precomputed from face geometry
1004: . cellgeom - The values precomputed from cell geometry
1005: - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell

1007:   Level: developer

1009: .seealso: DMPlexTSSetRHSFunctionLocal()
1010: @*/
1011: PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
1012: {
1013:   DM             plex;

1018:   DMSNESConvertPlex(dm,&plex,PETSC_TRUE);
1019:   DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);
1020:   if (minRadius) {DMPlexGetMinRadius(plex, minRadius);}
1021:   DMDestroy(&plex);
1022:   return(0);
1023: }

1025: /*@
1026:   DMPlexSNESGetGradientDM - Return gradient data layout

1028:   Input Parameters:
1029: + dm - The DM
1030: - fv - The PetscFV

1032:   Output Parameter:
1033: . dmGrad - The layout for gradient values

1035:   Level: developer

1037: .seealso: DMPlexSNESGetGeometryFVM()
1038: @*/
1039: PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
1040: {
1041:   DM             plex;
1042:   PetscBool      computeGradients;

1049:   PetscFVGetComputeGradients(fv, &computeGradients);
1050:   if (!computeGradients) {*dmGrad = NULL; return(0);}
1051:   DMSNESConvertPlex(dm,&plex,PETSC_TRUE);
1052:   DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);
1053:   DMDestroy(&plex);
1054:   return(0);
1055: }

1057: static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
1058: {
1059:   DM_Plex         *mesh = (DM_Plex *) dm->data;
1060:   DM               plex = NULL, plexA = NULL;
1061:   DMEnclosureType  encAux;
1062:   PetscDS          prob, probAux = NULL;
1063:   PetscSection     section, sectionAux = NULL;
1064:   Vec              locA = NULL;
1065:   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
1066:   PetscInt         v;
1067:   PetscInt         totDim, totDimAux = 0;
1068:   PetscErrorCode   ierr;

1071:   DMConvert(dm, DMPLEX, &plex);
1072:   DMGetLocalSection(dm, &section);
1073:   DMGetDS(dm, &prob);
1074:   PetscDSGetTotalDimension(prob, &totDim);
1075:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);
1076:   if (locA) {
1077:     DM dmAux;

1079:     VecGetDM(locA, &dmAux);
1080:     DMGetEnclosureRelation(dmAux, dm, &encAux);
1081:     DMConvert(dmAux, DMPLEX, &plexA);
1082:     DMGetDS(plexA, &probAux);
1083:     PetscDSGetTotalDimension(probAux, &totDimAux);
1084:     DMGetLocalSection(plexA, &sectionAux);
1085:   }
1086:   for (v = 0; v < numValues; ++v) {
1087:     PetscFEGeom    *fgeom;
1088:     PetscInt        maxDegree;
1089:     PetscQuadrature qGeom = NULL;
1090:     IS              pointIS;
1091:     const PetscInt *points;
1092:     PetscInt        numFaces, face, Nq;

1094:     DMLabelGetStratumIS(label, values[v], &pointIS);
1095:     if (!pointIS) continue; /* No points with that id on this process */
1096:     {
1097:       IS isectIS;

1099:       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
1100:       ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);
1101:       ISDestroy(&pointIS);
1102:       pointIS = isectIS;
1103:     }
1104:     ISGetLocalSize(pointIS,&numFaces);
1105:     ISGetIndices(pointIS,&points);
1106:     PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);
1107:     DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);
1108:     if (maxDegree <= 1) {
1109:       DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);
1110:     }
1111:     if (!qGeom) {
1112:       PetscFE fe;

1114:       PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);
1115:       PetscFEGetFaceQuadrature(fe, &qGeom);
1116:       PetscObjectReference((PetscObject)qGeom);
1117:     }
1118:     PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);
1119:     DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);
1120:     for (face = 0; face < numFaces; ++face) {
1121:       const PetscInt point = points[face], *support, *cone;
1122:       PetscScalar   *x     = NULL;
1123:       PetscInt       i, coneSize, faceLoc;

1125:       DMPlexGetSupport(dm, point, &support);
1126:       DMPlexGetConeSize(dm, support[0], &coneSize);
1127:       DMPlexGetCone(dm, support[0], &cone);
1128:       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
1129:       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
1130:       fgeom->face[face][0] = faceLoc;
1131:       DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);
1132:       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
1133:       DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);
1134:       if (locX_t) {
1135:         DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);
1136:         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
1137:         DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);
1138:       }
1139:       if (locA) {
1140:         PetscInt subp;

1142:         DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);
1143:         DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);
1144:         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
1145:         DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);
1146:       }
1147:     }
1148:     PetscArrayzero(elemVec, numFaces*totDim);
1149:     {
1150:       PetscFE         fe;
1151:       PetscInt        Nb;
1152:       PetscFEGeom     *chunkGeom = NULL;
1153:       /* Conforming batches */
1154:       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1155:       /* Remainder */
1156:       PetscInt        Nr, offset;

1158:       PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);
1159:       PetscFEGetDimension(fe, &Nb);
1160:       PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
1161:       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
1162:       blockSize = Nb;
1163:       batchSize = numBlocks * blockSize;
1164:        PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
1165:       numChunks = numFaces / (numBatches*batchSize);
1166:       Ne        = numChunks*numBatches*batchSize;
1167:       Nr        = numFaces % (numBatches*batchSize);
1168:       offset    = numFaces - Nr;
1169:       PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);
1170:       PetscFEIntegrateBdResidual(prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);
1171:       PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);
1172:       PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);
1173:       PetscFEIntegrateBdResidual(prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);
1174:       PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);
1175:     }
1176:     for (face = 0; face < numFaces; ++face) {
1177:       const PetscInt point = points[face], *support;

1179:       if (mesh->printFEM > 1) {DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);}
1180:       DMPlexGetSupport(plex, point, &support);
1181:       DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);
1182:     }
1183:     DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);
1184:     PetscQuadratureDestroy(&qGeom);
1185:     ISRestoreIndices(pointIS, &points);
1186:     ISDestroy(&pointIS);
1187:     PetscFree4(u, u_t, elemVec, a);
1188:   }
1189:   if (plex)  {DMDestroy(&plex);}
1190:   if (plexA) {DMDestroy(&plexA);}
1191:   return(0);
1192: }

1194: PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF)
1195: {
1196:   DMField        coordField;
1197:   DMLabel        depthLabel;
1198:   IS             facetIS;
1199:   PetscInt       dim;

1203:   DMGetDimension(dm, &dim);
1204:   DMPlexGetDepthLabel(dm, &depthLabel);
1205:   DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);
1206:   DMGetCoordinateField(dm, &coordField);
1207:   DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);
1208:   ISDestroy(&facetIS);
1209:   return(0);
1210: }

1212: PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
1213: {
1214:   PetscDS        prob;
1215:   PetscInt       numBd, bd;
1216:   DMField        coordField = NULL;
1217:   IS             facetIS    = NULL;
1218:   DMLabel        depthLabel;
1219:   PetscInt       dim;

1223:   DMGetDS(dm, &prob);
1224:   DMPlexGetDepthLabel(dm, &depthLabel);
1225:   DMGetDimension(dm, &dim);
1226:   DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);
1227:   PetscDSGetNumBoundary(prob, &numBd);
1228:   for (bd = 0; bd < numBd; ++bd) {
1229:     DMBoundaryConditionType type;
1230:     const char             *bdLabel;
1231:     DMLabel                 label;
1232:     const PetscInt         *values;
1233:     PetscInt                field, numValues;
1234:     PetscObject             obj;
1235:     PetscClassId            id;

1237:     PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);
1238:     PetscDSGetDiscretization(prob, field, &obj);
1239:     PetscObjectGetClassId(obj, &id);
1240:     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
1241:     if (!facetIS) {
1242:       DMLabel  depthLabel;
1243:       PetscInt dim;

1245:       DMPlexGetDepthLabel(dm, &depthLabel);
1246:       DMGetDimension(dm, &dim);
1247:       DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);
1248:     }
1249:     DMGetCoordinateField(dm, &coordField);
1250:     DMGetLabel(dm, bdLabel, &label);
1251:     DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);
1252:   }
1253:   ISDestroy(&facetIS);
1254:   return(0);
1255: }

1257: PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
1258: {
1259:   DM_Plex         *mesh       = (DM_Plex *) dm->data;
1260:   const char      *name       = "Residual";
1261:   DM               dmAux      = NULL;
1262:   DM               dmGrad     = NULL;
1263:   DMLabel          ghostLabel = NULL;
1264:   PetscDS          prob       = NULL;
1265:   PetscDS          probAux    = NULL;
1266:   PetscSection     section    = NULL;
1267:   PetscBool        useFEM     = PETSC_FALSE;
1268:   PetscBool        useFVM     = PETSC_FALSE;
1269:   PetscBool        isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
1270:   PetscFV          fvm        = NULL;
1271:   PetscFVCellGeom *cgeomFVM   = NULL;
1272:   PetscFVFaceGeom *fgeomFVM   = NULL;
1273:   DMField          coordField = NULL;
1274:   Vec              locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
1275:   PetscScalar     *u = NULL, *u_t, *a, *uL, *uR;
1276:   IS               chunkIS;
1277:   const PetscInt  *cells;
1278:   PetscInt         cStart, cEnd, numCells;
1279:   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
1280:   PetscInt         maxDegree = PETSC_MAX_INT;
1281:   PetscQuadrature  affineQuad = NULL, *quads = NULL;
1282:   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
1283:   PetscErrorCode   ierr;

1286:   PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);
1287:   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1288:   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
1289:   /* FEM+FVM */
1290:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
1291:   DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
1292:   /* 1: Get sizes from dm and dmAux */
1293:   DMGetLocalSection(dm, &section);
1294:   DMGetLabel(dm, "ghost", &ghostLabel);
1295:   DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);
1296:   PetscDSGetNumFields(prob, &Nf);
1297:   PetscDSGetTotalDimension(prob, &totDim);
1298:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);
1299:   if (locA) {
1300:     PetscInt subcell;
1301:     VecGetDM(locA, &dmAux);
1302:     DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);
1303:     DMGetCellDS(dmAux, subcell, &probAux);
1304:     PetscDSGetTotalDimension(probAux, &totDimAux);
1305:   }
1306:   /* 2: Get geometric data */
1307:   for (f = 0; f < Nf; ++f) {
1308:     PetscObject  obj;
1309:     PetscClassId id;
1310:     PetscBool    fimp;

1312:     PetscDSGetImplicit(prob, f, &fimp);
1313:     if (isImplicit != fimp) continue;
1314:     PetscDSGetDiscretization(prob, f, &obj);
1315:     PetscObjectGetClassId(obj, &id);
1316:     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
1317:     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
1318:   }
1319:   if (useFEM) {
1320:     DMGetCoordinateField(dm, &coordField);
1321:     DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);
1322:     if (maxDegree <= 1) {
1323:       DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);
1324:       if (affineQuad) {
1325:         DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);
1326:       }
1327:     } else {
1328:       PetscCalloc2(Nf,&quads,Nf,&geoms);
1329:       for (f = 0; f < Nf; ++f) {
1330:         PetscObject  obj;
1331:         PetscClassId id;
1332:         PetscBool    fimp;

1334:         PetscDSGetImplicit(prob, f, &fimp);
1335:         if (isImplicit != fimp) continue;
1336:         PetscDSGetDiscretization(prob, f, &obj);
1337:         PetscObjectGetClassId(obj, &id);
1338:         if (id == PETSCFE_CLASSID) {
1339:           PetscFE fe = (PetscFE) obj;

1341:           PetscFEGetQuadrature(fe, &quads[f]);
1342:           PetscObjectReference((PetscObject)quads[f]);
1343:           DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);
1344:         }
1345:       }
1346:     }
1347:   }
1348:   if (useFVM) {
1349:     DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);
1350:     VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);
1351:     VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);
1352:     /* Reconstruct and limit cell gradients */
1353:     DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);
1354:     if (dmGrad) {
1355:       DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
1356:       DMGetGlobalVector(dmGrad, &grad);
1357:       DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);
1358:       /* Communicate gradient values */
1359:       DMGetLocalVector(dmGrad, &locGrad);
1360:       DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);
1361:       DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);
1362:       DMRestoreGlobalVector(dmGrad, &grad);
1363:     }
1364:     /* Handle non-essential (e.g. outflow) boundary values */
1365:     DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);
1366:   }
1367:   /* Loop over chunks */
1368:   if (useFEM) {ISCreate(PETSC_COMM_SELF, &chunkIS);}
1369:   numCells      = cEnd - cStart;
1370:   numChunks     = 1;
1371:   cellChunkSize = numCells/numChunks;
1372:   faceChunkSize = (fEnd - fStart)/numChunks;
1373:   numChunks     = PetscMin(1,numCells);
1374:   for (chunk = 0; chunk < numChunks; ++chunk) {
1375:     PetscScalar     *elemVec, *fluxL, *fluxR;
1376:     PetscReal       *vol;
1377:     PetscFVFaceGeom *fgeom;
1378:     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
1379:     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;

1381:     /* Extract field coefficients */
1382:     if (useFEM) {
1383:       ISGetPointSubrange(chunkIS, cS, cE, cells);
1384:       DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);
1385:       DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);
1386:       PetscArrayzero(elemVec, numCells*totDim);
1387:     }
1388:     if (useFVM) {
1389:       DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);
1390:       DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);
1391:       DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);
1392:       DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);
1393:       PetscArrayzero(fluxL, numFaces*totDim);
1394:       PetscArrayzero(fluxR, numFaces*totDim);
1395:     }
1396:     /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */
1397:     /* Loop over fields */
1398:     for (f = 0; f < Nf; ++f) {
1399:       PetscObject  obj;
1400:       PetscClassId id;
1401:       PetscBool    fimp;
1402:       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;

1404:       PetscDSGetImplicit(prob, f, &fimp);
1405:       if (isImplicit != fimp) continue;
1406:       PetscDSGetDiscretization(prob, f, &obj);
1407:       PetscObjectGetClassId(obj, &id);
1408:       if (id == PETSCFE_CLASSID) {
1409:         PetscFE         fe = (PetscFE) obj;
1410:         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
1411:         PetscFEGeom    *chunkGeom = NULL;
1412:         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
1413:         PetscInt        Nq, Nb;

1415:         PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
1416:         PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);
1417:         PetscFEGetDimension(fe, &Nb);
1418:         blockSize = Nb;
1419:         batchSize = numBlocks * blockSize;
1420:         PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
1421:         numChunks = numCells / (numBatches*batchSize);
1422:         Ne        = numChunks*numBatches*batchSize;
1423:         Nr        = numCells % (numBatches*batchSize);
1424:         offset    = numCells - Nr;
1425:         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
1426:         /*   For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */
1427:         PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);
1428:         PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);
1429:         PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);
1430:         PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);
1431:         PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);
1432:       } else if (id == PETSCFV_CLASSID) {
1433:         PetscFV fv = (PetscFV) obj;

1435:         Ne = numFaces;
1436:         /* Riemann solve over faces (need fields at face centroids) */
1437:         /*   We need to evaluate FE fields at those coordinates */
1438:         PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);
1439:       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1440:     }
1441:     /* Loop over domain */
1442:     if (useFEM) {
1443:       /* Add elemVec to locX */
1444:       for (c = cS; c < cE; ++c) {
1445:         const PetscInt cell = cells ? cells[c] : c;
1446:         const PetscInt cind = c - cStart;

1448:         if (mesh->printFEM > 1) {DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);}
1449:         if (ghostLabel) {
1450:           PetscInt ghostVal;

1452:           DMLabelGetValue(ghostLabel,cell,&ghostVal);
1453:           if (ghostVal > 0) continue;
1454:         }
1455:         DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);
1456:       }
1457:     }
1458:     if (useFVM) {
1459:       PetscScalar *fa;
1460:       PetscInt     iface;

1462:       VecGetArray(locF, &fa);
1463:       for (f = 0; f < Nf; ++f) {
1464:         PetscFV      fv;
1465:         PetscObject  obj;
1466:         PetscClassId id;
1467:         PetscInt     foff, pdim;

1469:         PetscDSGetDiscretization(prob, f, &obj);
1470:         PetscDSGetFieldOffset(prob, f, &foff);
1471:         PetscObjectGetClassId(obj, &id);
1472:         if (id != PETSCFV_CLASSID) continue;
1473:         fv   = (PetscFV) obj;
1474:         PetscFVGetNumComponents(fv, &pdim);
1475:         /* Accumulate fluxes to cells */
1476:         for (face = fS, iface = 0; face < fE; ++face) {
1477:           const PetscInt *scells;
1478:           PetscScalar    *fL = NULL, *fR = NULL;
1479:           PetscInt        ghost, d, nsupp, nchild;

1481:           DMLabelGetValue(ghostLabel, face, &ghost);
1482:           DMPlexGetSupportSize(dm, face, &nsupp);
1483:           DMPlexGetTreeChildren(dm, face, &nchild, NULL);
1484:           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
1485:           DMPlexGetSupport(dm, face, &scells);
1486:           DMLabelGetValue(ghostLabel,scells[0],&ghost);
1487:           if (ghost <= 0) {DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);}
1488:           DMLabelGetValue(ghostLabel,scells[1],&ghost);
1489:           if (ghost <= 0) {DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);}
1490:           for (d = 0; d < pdim; ++d) {
1491:             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1492:             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
1493:           }
1494:           ++iface;
1495:         }
1496:       }
1497:       VecRestoreArray(locF, &fa);
1498:     }
1499:     /* Handle time derivative */
1500:     if (locX_t) {
1501:       PetscScalar *x_t, *fa;

1503:       VecGetArray(locF, &fa);
1504:       VecGetArray(locX_t, &x_t);
1505:       for (f = 0; f < Nf; ++f) {
1506:         PetscFV      fv;
1507:         PetscObject  obj;
1508:         PetscClassId id;
1509:         PetscInt     pdim, d;

1511:         PetscDSGetDiscretization(prob, f, &obj);
1512:         PetscObjectGetClassId(obj, &id);
1513:         if (id != PETSCFV_CLASSID) continue;
1514:         fv   = (PetscFV) obj;
1515:         PetscFVGetNumComponents(fv, &pdim);
1516:         for (c = cS; c < cE; ++c) {
1517:           const PetscInt cell = cells ? cells[c] : c;
1518:           PetscScalar   *u_t, *r;

1520:           if (ghostLabel) {
1521:             PetscInt ghostVal;

1523:             DMLabelGetValue(ghostLabel, cell, &ghostVal);
1524:             if (ghostVal > 0) continue;
1525:           }
1526:           DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);
1527:           DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);
1528:           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1529:         }
1530:       }
1531:       VecRestoreArray(locX_t, &x_t);
1532:       VecRestoreArray(locF, &fa);
1533:     }
1534:     if (useFEM) {
1535:       DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);
1536:       DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);
1537:     }
1538:     if (useFVM) {
1539:       DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);
1540:       DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);
1541:       DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);
1542:       DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);
1543:       if (dmGrad) {DMRestoreLocalVector(dmGrad, &locGrad);}
1544:     }
1545:   }
1546:   if (useFEM) {ISDestroy(&chunkIS);}
1547:   ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);

1549:   if (useFEM) {
1550:     DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);

1552:     if (maxDegree <= 1) {
1553:       DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);
1554:       PetscQuadratureDestroy(&affineQuad);
1555:     } else {
1556:       for (f = 0; f < Nf; ++f) {
1557:         DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);
1558:         PetscQuadratureDestroy(&quads[f]);
1559:       }
1560:       PetscFree2(quads,geoms);
1561:     }
1562:   }

1564:   /* FEM */
1565:   /* 1: Get sizes from dm and dmAux */
1566:   /* 2: Get geometric data */
1567:   /* 3: Handle boundary values */
1568:   /* 4: Loop over domain */
1569:   /*   Extract coefficients */
1570:   /* Loop over fields */
1571:   /*   Set tiling for FE*/
1572:   /*   Integrate FE residual to get elemVec */
1573:   /*     Loop over subdomain */
1574:   /*       Loop over quad points */
1575:   /*         Transform coords to real space */
1576:   /*         Evaluate field and aux fields at point */
1577:   /*         Evaluate residual at point */
1578:   /*         Transform residual to real space */
1579:   /*       Add residual to elemVec */
1580:   /* Loop over domain */
1581:   /*   Add elemVec to locX */

1583:   /* FVM */
1584:   /* Get geometric data */
1585:   /* If using gradients */
1586:   /*   Compute gradient data */
1587:   /*   Loop over domain faces */
1588:   /*     Count computational faces */
1589:   /*     Reconstruct cell gradient */
1590:   /*   Loop over domain cells */
1591:   /*     Limit cell gradients */
1592:   /* Handle boundary values */
1593:   /* Loop over domain faces */
1594:   /*   Read out field, centroid, normal, volume for each side of face */
1595:   /* Riemann solve over faces */
1596:   /* Loop over domain faces */
1597:   /*   Accumulate fluxes to cells */
1598:   /* TODO Change printFEM to printDisc here */
1599:   if (mesh->printFEM) {
1600:     Vec         locFbc;
1601:     PetscInt    pStart, pEnd, p, maxDof;
1602:     PetscScalar *zeroes;

1604:     VecDuplicate(locF,&locFbc);
1605:     VecCopy(locF,locFbc);
1606:     PetscSectionGetChart(section,&pStart,&pEnd);
1607:     PetscSectionGetMaxDof(section,&maxDof);
1608:     PetscCalloc1(maxDof,&zeroes);
1609:     for (p = pStart; p < pEnd; p++) {
1610:       VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);
1611:     }
1612:     PetscFree(zeroes);
1613:     DMPrintLocalVec(dm, name, mesh->printTol, locFbc);
1614:     VecDestroy(&locFbc);
1615:   }
1616:   PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);
1617:   return(0);
1618: }

1620: PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
1621: {
1622:   DM_Plex         *mesh       = (DM_Plex *) dm->data;
1623:   const char      *name       = "Hybrid Residual";
1624:   DM               dmAux      = NULL;
1625:   DMLabel          ghostLabel = NULL;
1626:   PetscDS          prob       = NULL;
1627:   PetscDS          probAux    = NULL;
1628:   PetscSection     section    = NULL;
1629:   DMField          coordField = NULL;
1630:   Vec              locA;
1631:   PetscScalar     *u = NULL, *u_t, *a;
1632:   PetscScalar     *elemVec;
1633:   IS               chunkIS;
1634:   const PetscInt  *cells;
1635:   PetscInt        *faces;
1636:   PetscInt         cStart, cEnd, numCells;
1637:   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk;
1638:   PetscInt         maxDegree = PETSC_MAX_INT;
1639:   PetscQuadrature  affineQuad = NULL, *quads = NULL;
1640:   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
1641:   PetscErrorCode   ierr;

1644:   PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);
1645:   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1646:   /* FEM */
1647:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
1648:   /* 1: Get sizes from dm and dmAux */
1649:   DMGetSection(dm, &section);
1650:   DMGetLabel(dm, "ghost", &ghostLabel);
1651:   DMGetCellDS(dm, cStart, &prob);
1652:   PetscDSGetNumFields(prob, &Nf);
1653:   PetscDSGetTotalDimension(prob, &totDim);
1654:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);
1655:   if (locA) {
1656:     VecGetDM(locA, &dmAux);
1657:     DMGetCellDS(dmAux, cStart, &probAux);
1658:     PetscDSGetTotalDimension(probAux, &totDimAux);
1659:   }
1660:   /* 2: Setup geometric data */
1661:   DMGetCoordinateField(dm, &coordField);
1662:   DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);
1663:   if (maxDegree > 1) {
1664:     PetscCalloc2(Nf,&quads,Nf,&geoms);
1665:     for (f = 0; f < Nf; ++f) {
1666:       PetscFE fe;

1668:       PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);
1669:       if (fe) {
1670:         PetscFEGetQuadrature(fe, &quads[f]);
1671:         PetscObjectReference((PetscObject) quads[f]);
1672:       }
1673:     }
1674:   }
1675:   /* Loop over chunks */
1676:   numCells      = cEnd - cStart;
1677:   cellChunkSize = numCells;
1678:   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
1679:   PetscCalloc1(2*cellChunkSize, &faces);
1680:   ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);
1681:   /* Extract field coefficients */
1682:   /* NOTE This needs the end cap faces to have identical orientations */
1683:   DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);
1684:   DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);
1685:   for (chunk = 0; chunk < numChunks; ++chunk) {
1686:     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;

1688:     PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));
1689:     /* Get faces */
1690:     for (c = cS; c < cE; ++c) {
1691:       const PetscInt  cell = cells ? cells[c] : c;
1692:       const PetscInt *cone;
1693:       DMPlexGetCone(dm, cell, &cone);
1694:       faces[(c-cS)*2+0] = cone[0];
1695:       faces[(c-cS)*2+1] = cone[1];
1696:     }
1697:     ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);
1698:     /* Get geometric data */
1699:     if (maxDegree <= 1) {
1700:       if (!affineQuad) {DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);}
1701:       if (affineQuad)  {DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);}
1702:     } else {
1703:       for (f = 0; f < Nf; ++f) {
1704:         if (quads[f]) {DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);}
1705:       }
1706:     }
1707:     /* Loop over fields */
1708:     for (f = 0; f < Nf; ++f) {
1709:       PetscFE         fe;
1710:       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
1711:       PetscFEGeom    *chunkGeom = NULL;
1712:       PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
1713:       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;

1715:       PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);
1716:       if (!fe) continue;
1717:       PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
1718:       PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);
1719:       PetscFEGetDimension(fe, &Nb);
1720:       blockSize = Nb;
1721:       batchSize = numBlocks * blockSize;
1722:       PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
1723:       numChunks = numCells / (numBatches*batchSize);
1724:       Ne        = numChunks*numBatches*batchSize;
1725:       Nr        = numCells % (numBatches*batchSize);
1726:       offset    = numCells - Nr;
1727:       PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);
1728:       PetscFEIntegrateHybridResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);
1729:       PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);
1730:       PetscFEIntegrateHybridResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);
1731:       PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);
1732:     }
1733:     /* Add elemVec to locX */
1734:     for (c = cS; c < cE; ++c) {
1735:       const PetscInt cell = cells ? cells[c] : c;
1736:       const PetscInt cind = c - cStart;

1738:       if (mesh->printFEM > 1) {DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);}
1739:       if (ghostLabel) {
1740:         PetscInt ghostVal;

1742:         DMLabelGetValue(ghostLabel,cell,&ghostVal);
1743:         if (ghostVal > 0) continue;
1744:       }
1745:       DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);
1746:     }
1747:   }
1748:   DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);
1749:   DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);
1750:   PetscFree(faces);
1751:   ISDestroy(&chunkIS);
1752:   ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);
1753:   if (maxDegree <= 1) {
1754:     DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);
1755:     PetscQuadratureDestroy(&affineQuad);
1756:   } else {
1757:     for (f = 0; f < Nf; ++f) {
1758:       if (geoms) {DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);}
1759:       if (quads) {PetscQuadratureDestroy(&quads[f]);}
1760:     }
1761:     PetscFree2(quads,geoms);
1762:   }
1763:   PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);
1764:   return(0);
1765: }

1767: /*@
1768:   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user

1770:   Input Parameters:
1771: + dm - The mesh
1772: . X  - Local solution
1773: - user - The user context

1775:   Output Parameter:
1776: . F  - Local output vector

1778:   Level: developer

1780: .seealso: DMPlexComputeJacobianAction()
1781: @*/
1782: PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1783: {
1784:   DM             plex;
1785:   IS             allcellIS;
1786:   PetscInt       Nds, s, depth;

1790:   DMGetNumDS(dm, &Nds);
1791:   DMSNESConvertPlex(dm, &plex, PETSC_TRUE);
1792:   DMPlexGetDepth(plex, &depth);
1793:   DMGetStratumIS(plex, "dim", depth, &allcellIS);
1794:   if (!allcellIS) {DMGetStratumIS(plex, "depth", depth, &allcellIS);}
1795:   for (s = 0; s < Nds; ++s) {
1796:     PetscDS ds;
1797:     DMLabel label;
1798:     IS      cellIS;

1800:     DMGetRegionNumDS(dm, s, &label, NULL, &ds);
1801:     if (!label) {
1802:       PetscObjectReference((PetscObject) allcellIS);
1803:       cellIS = allcellIS;
1804:     } else {
1805:       IS pointIS;

1807:       DMLabelGetStratumIS(label, 1, &pointIS);
1808:       ISIntersect_Caching_Internal(allcellIS, pointIS, &cellIS);
1809:       ISDestroy(&pointIS);
1810:     }
1811:     DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);
1812:     ISDestroy(&cellIS);
1813:   }
1814:   ISDestroy(&allcellIS);
1815:   DMDestroy(&plex);
1816:   return(0);
1817: }

1819: /*@
1820:   DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X

1822:   Input Parameters:
1823: + dm - The mesh
1824: - user - The user context

1826:   Output Parameter:
1827: . X  - Local solution

1829:   Level: developer

1831: .seealso: DMPlexComputeJacobianAction()
1832: @*/
1833: PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user)
1834: {
1835:   DM             plex;

1839:   DMSNESConvertPlex(dm,&plex,PETSC_TRUE);
1840:   DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);
1841:   DMDestroy(&plex);
1842:   return(0);
1843: }

1845: PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS)
1846: {
1847:   DM_Plex        *mesh = (DM_Plex *) dm->data;
1848:   DM              plex = NULL, plexA = NULL, tdm;
1849:   DMEnclosureType encAux;
1850:   PetscDS         prob, probAux = NULL;
1851:   PetscSection    section, sectionAux = NULL;
1852:   PetscSection    globalSection, subSection = NULL;
1853:   Vec             locA = NULL, tv;
1854:   PetscScalar    *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
1855:   PetscInt        v;
1856:   PetscInt        Nf, totDim, totDimAux = 0;
1857:   PetscBool       isMatISP, transform;
1858:   PetscErrorCode  ierr;

1861:   DMConvert(dm, DMPLEX, &plex);
1862:   DMHasBasisTransform(dm, &transform);
1863:   DMGetBasisTransformDM_Internal(dm, &tdm);
1864:   DMGetBasisTransformVec_Internal(dm, &tv);
1865:   DMGetLocalSection(dm, &section);
1866:   DMGetDS(dm, &prob);
1867:   PetscDSGetNumFields(prob, &Nf);
1868:   PetscDSGetTotalDimension(prob, &totDim);
1869:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);
1870:   if (locA) {
1871:     DM dmAux;

1873:     VecGetDM(locA, &dmAux);
1874:     DMGetEnclosureRelation(dmAux, dm, &encAux);
1875:     DMConvert(dmAux, DMPLEX, &plexA);
1876:     DMGetDS(plexA, &probAux);
1877:     PetscDSGetTotalDimension(probAux, &totDimAux);
1878:     DMGetLocalSection(plexA, &sectionAux);
1879:   }

1881:   PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);
1882:   DMGetGlobalSection(dm, &globalSection);
1883:   if (isMatISP) {DMPlexGetSubdomainSection(dm, &subSection);}
1884:   for (v = 0; v < numValues; ++v) {
1885:     PetscFEGeom    *fgeom;
1886:     PetscInt        maxDegree;
1887:     PetscQuadrature qGeom = NULL;
1888:     IS              pointIS;
1889:     const PetscInt *points;
1890:     PetscInt        numFaces, face, Nq;

1892:     DMLabelGetStratumIS(label, values[v], &pointIS);
1893:     if (!pointIS) continue; /* No points with that id on this process */
1894:     {
1895:       IS isectIS;

1897:       /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
1898:       ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);
1899:       ISDestroy(&pointIS);
1900:       pointIS = isectIS;
1901:     }
1902:     ISGetLocalSize(pointIS, &numFaces);
1903:     ISGetIndices(pointIS, &points);
1904:     PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);
1905:     DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);
1906:     if (maxDegree <= 1) {
1907:       DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);
1908:     }
1909:     if (!qGeom) {
1910:       PetscFE fe;

1912:       PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);
1913:       PetscFEGetFaceQuadrature(fe, &qGeom);
1914:       PetscObjectReference((PetscObject)qGeom);
1915:     }
1916:     PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);
1917:     DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);
1918:     for (face = 0; face < numFaces; ++face) {
1919:       const PetscInt point = points[face], *support, *cone;
1920:       PetscScalar   *x     = NULL;
1921:       PetscInt       i, coneSize, faceLoc;

1923:       DMPlexGetSupport(dm, point, &support);
1924:       DMPlexGetConeSize(dm, support[0], &coneSize);
1925:       DMPlexGetCone(dm, support[0], &cone);
1926:       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
1927:       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
1928:       fgeom->face[face][0] = faceLoc;
1929:       DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);
1930:       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
1931:       DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);
1932:       if (locX_t) {
1933:         DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);
1934:         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
1935:         DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);
1936:       }
1937:       if (locA) {
1938:         PetscInt subp;
1939:         DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);
1940:         DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);
1941:         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
1942:         DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);
1943:       }
1944:     }
1945:     PetscArrayzero(elemMat, numFaces*totDim*totDim);
1946:     {
1947:       PetscFE         fe;
1948:       PetscInt        Nb;
1949:       /* Conforming batches */
1950:       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1951:       /* Remainder */
1952:       PetscFEGeom    *chunkGeom = NULL;
1953:       PetscInt        fieldJ, Nr, offset;

1955:       PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);
1956:       PetscFEGetDimension(fe, &Nb);
1957:       PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
1958:       blockSize = Nb;
1959:       batchSize = numBlocks * blockSize;
1960:       PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
1961:       numChunks = numFaces / (numBatches*batchSize);
1962:       Ne        = numChunks*numBatches*batchSize;
1963:       Nr        = numFaces % (numBatches*batchSize);
1964:       offset    = numFaces - Nr;
1965:       PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);
1966:       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1967:         PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);
1968:       }
1969:       PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);
1970:       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1971:         PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);
1972:       }
1973:       PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);
1974:     }
1975:     for (face = 0; face < numFaces; ++face) {
1976:       const PetscInt point = points[face], *support;

1978:       /* Transform to global basis before insertion in Jacobian */
1979:       DMPlexGetSupport(plex, point, &support);
1980:       if (transform) {DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);}
1981:       if (mesh->printFEM > 1) {DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);}
1982:       if (!isMatISP) {
1983:         DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);
1984:       } else {
1985:         Mat lJ;

1987:         MatISGetLocalMat(JacP, &lJ);
1988:         DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);
1989:       }
1990:     }
1991:     DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);
1992:     PetscQuadratureDestroy(&qGeom);
1993:     ISRestoreIndices(pointIS, &points);
1994:     ISDestroy(&pointIS);
1995:     PetscFree4(u, u_t, elemMat, a);
1996:   }
1997:   if (plex)  {DMDestroy(&plex);}
1998:   if (plexA) {DMDestroy(&plexA);}
1999:   return(0);
2000: }

2002: PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP)
2003: {
2004:   DMField        coordField;
2005:   DMLabel        depthLabel;
2006:   IS             facetIS;
2007:   PetscInt       dim;

2011:   DMGetDimension(dm, &dim);
2012:   DMPlexGetDepthLabel(dm, &depthLabel);
2013:   DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);
2014:   DMGetCoordinateField(dm, &coordField);
2015:   DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);
2016:   ISDestroy(&facetIS);
2017:   return(0);
2018: }

2020: PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
2021: {
2022:   PetscDS          prob;
2023:   PetscInt         dim, numBd, bd;
2024:   DMLabel          depthLabel;
2025:   DMField          coordField = NULL;
2026:   IS               facetIS;
2027:   PetscErrorCode   ierr;

2030:   DMGetDS(dm, &prob);
2031:   DMPlexGetDepthLabel(dm, &depthLabel);
2032:   DMGetDimension(dm, &dim);
2033:   DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);
2034:   PetscDSGetNumBoundary(prob, &numBd);
2035:   DMGetCoordinateField(dm, &coordField);
2036:   for (bd = 0; bd < numBd; ++bd) {
2037:     DMBoundaryConditionType type;
2038:     const char             *bdLabel;
2039:     DMLabel                 label;
2040:     const PetscInt         *values;
2041:     PetscInt                fieldI, numValues;
2042:     PetscObject             obj;
2043:     PetscClassId            id;

2045:     PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);
2046:     PetscDSGetDiscretization(prob, fieldI, &obj);
2047:     PetscObjectGetClassId(obj, &id);
2048:     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
2049:     DMGetLabel(dm, bdLabel, &label);
2050:     DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);
2051:   }
2052:   ISDestroy(&facetIS);
2053:   return(0);
2054: }

2056: PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
2057: {
2058:   DM_Plex        *mesh  = (DM_Plex *) dm->data;
2059:   const char     *name  = "Jacobian";
2060:   DM              dmAux, plex, tdm;
2061:   DMEnclosureType encAux;
2062:   Vec             A, tv;
2063:   DMField         coordField;
2064:   PetscDS         prob, probAux = NULL;
2065:   PetscSection    section, globalSection, subSection, sectionAux;
2066:   PetscScalar    *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
2067:   const PetscInt *cells;
2068:   PetscInt        Nf, fieldI, fieldJ;
2069:   PetscInt        totDim, totDimAux, cStart, cEnd, numCells, c;
2070:   PetscBool       isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform;
2071:   PetscErrorCode  ierr;

2074:   PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);
2075:   ISGetLocalSize(cellIS, &numCells);
2076:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
2077:   DMHasBasisTransform(dm, &transform);
2078:   DMGetBasisTransformDM_Internal(dm, &tdm);
2079:   DMGetBasisTransformVec_Internal(dm, &tv);
2080:   DMGetLocalSection(dm, &section);
2081:   PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);
2082:   DMGetGlobalSection(dm, &globalSection);
2083:   if (isMatISP) {DMPlexGetSubdomainSection(dm, &subSection);}
2084:   ISGetLocalSize(cellIS, &numCells);
2085:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
2086:   DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);
2087:   PetscDSGetNumFields(prob, &Nf);
2088:   PetscDSGetTotalDimension(prob, &totDim);
2089:   PetscDSHasJacobian(prob, &hasJac);
2090:   PetscDSHasJacobianPreconditioner(prob, &hasPrec);
2091:   /* user passed in the same matrix, avoid double contributions and
2092:      only assemble the Jacobian */
2093:   if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE;
2094:   PetscDSHasDynamicJacobian(prob, &hasDyn);
2095:   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2096:   PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);
2097:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);
2098:   if (dmAux) {
2099:     DMGetEnclosureRelation(dmAux, dm, &encAux);
2100:     DMConvert(dmAux, DMPLEX, &plex);
2101:     DMGetLocalSection(plex, &sectionAux);
2102:     DMGetDS(dmAux, &probAux);
2103:     PetscDSGetTotalDimension(probAux, &totDimAux);
2104:   }
2105:   PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);
2106:   if (dmAux) {PetscMalloc1(numCells*totDimAux, &a);}
2107:   DMGetCoordinateField(dm, &coordField);
2108:   for (c = cStart; c < cEnd; ++c) {
2109:     const PetscInt cell = cells ? cells[c] : c;
2110:     const PetscInt cind = c - cStart;
2111:     PetscScalar   *x = NULL,  *x_t = NULL;
2112:     PetscInt       i;

2114:     DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);
2115:     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
2116:     DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);
2117:     if (X_t) {
2118:       DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);
2119:       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
2120:       DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);
2121:     }
2122:     if (dmAux) {
2123:       PetscInt subcell;
2124:       DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);
2125:       DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);
2126:       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
2127:       DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);
2128:     }
2129:   }
2130:   if (hasJac)  {PetscArrayzero(elemMat,  numCells*totDim*totDim);}
2131:   if (hasPrec) {PetscArrayzero(elemMatP, numCells*totDim*totDim);}
2132:   if (hasDyn)  {PetscArrayzero(elemMatD, numCells*totDim*totDim);}
2133:   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2134:     PetscClassId    id;
2135:     PetscFE         fe;
2136:     PetscQuadrature qGeom = NULL;
2137:     PetscInt        Nb;
2138:     /* Conforming batches */
2139:     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2140:     /* Remainder */
2141:     PetscInt        Nr, offset, Nq;
2142:     PetscInt        maxDegree;
2143:     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;

2145:     PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);
2146:     PetscObjectGetClassId((PetscObject) fe, &id);
2147:     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
2148:     PetscFEGetDimension(fe, &Nb);
2149:     PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
2150:     DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);
2151:     if (maxDegree <= 1) {
2152:       DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);
2153:     }
2154:     if (!qGeom) {
2155:       PetscFEGetQuadrature(fe,&qGeom);
2156:       PetscObjectReference((PetscObject)qGeom);
2157:     }
2158:     PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);
2159:     DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);
2160:     blockSize = Nb;
2161:     batchSize = numBlocks * blockSize;
2162:     PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
2163:     numChunks = numCells / (numBatches*batchSize);
2164:     Ne        = numChunks*numBatches*batchSize;
2165:     Nr        = numCells % (numBatches*batchSize);
2166:     offset    = numCells - Nr;
2167:     PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);
2168:     PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);
2169:     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2170:       if (hasJac) {
2171:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);
2172:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);
2173:       }
2174:       if (hasPrec) {
2175:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);
2176:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);
2177:       }
2178:       if (hasDyn) {
2179:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);
2180:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);
2181:       }
2182:     }
2183:     PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);
2184:     PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);
2185:     DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);
2186:     PetscQuadratureDestroy(&qGeom);
2187:   }
2188:   /*   Add contribution from X_t */
2189:   if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
2190:   if (hasFV) {
2191:     PetscClassId id;
2192:     PetscFV      fv;
2193:     PetscInt     offsetI, NcI, NbI = 1, fc, f;

2195:     for (fieldI = 0; fieldI < Nf; ++fieldI) {
2196:       PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);
2197:       PetscDSGetFieldOffset(prob, fieldI, &offsetI);
2198:       PetscObjectGetClassId((PetscObject) fv, &id);
2199:       if (id != PETSCFV_CLASSID) continue;
2200:       /* Put in the identity */
2201:       PetscFVGetNumComponents(fv, &NcI);
2202:       for (c = cStart; c < cEnd; ++c) {
2203:         const PetscInt cind    = c - cStart;
2204:         const PetscInt eOffset = cind*totDim*totDim;
2205:         for (fc = 0; fc < NcI; ++fc) {
2206:           for (f = 0; f < NbI; ++f) {
2207:             const PetscInt i = offsetI + f*NcI+fc;
2208:             if (hasPrec) {
2209:               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
2210:               elemMatP[eOffset+i*totDim+i] = 1.0;
2211:             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
2212:           }
2213:         }
2214:       }
2215:     }
2216:     /* No allocated space for FV stuff, so ignore the zero entries */
2217:     MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
2218:   }
2219:   /* Insert values into matrix */
2220:   isMatIS = PETSC_FALSE;
2221:   if (hasPrec && hasJac) {
2222:     PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);
2223:   }
2224:   if (isMatIS && !subSection) {
2225:     DMPlexGetSubdomainSection(dm, &subSection);
2226:   }
2227:   for (c = cStart; c < cEnd; ++c) {
2228:     const PetscInt cell = cells ? cells[c] : c;
2229:     const PetscInt cind = c - cStart;

2231:     /* Transform to global basis before insertion in Jacobian */
2232:     if (transform) {DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);}
2233:     if (hasPrec) {
2234:       if (hasJac) {
2235:         if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);}
2236:         if (!isMatIS) {
2237:           DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2238:         } else {
2239:           Mat lJ;

2241:           MatISGetLocalMat(Jac,&lJ);
2242:           DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2243:         }
2244:       }
2245:       if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);}
2246:       if (!isMatISP) {
2247:         DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);
2248:       } else {
2249:         Mat lJ;

2251:         MatISGetLocalMat(JacP,&lJ);
2252:         DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);
2253:       }
2254:     } else {
2255:       if (hasJac) {
2256:         if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);}
2257:         if (!isMatISP) {
2258:           DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2259:         } else {
2260:           Mat lJ;

2262:           MatISGetLocalMat(JacP,&lJ);
2263:           DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2264:         }
2265:       }
2266:     }
2267:   }
2268:   ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);
2269:   if (hasFV) {MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);}
2270:   PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);
2271:   if (dmAux) {
2272:     PetscFree(a);
2273:     DMDestroy(&plex);
2274:   }
2275:   /* Compute boundary integrals */
2276:   DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);
2277:   /* Assemble matrix */
2278:   if (hasJac && hasPrec) {
2279:     MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);
2280:     MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);
2281:   }
2282:   MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);
2283:   MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);
2284:   PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);
2285:   return(0);
2286: }

2288: PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user)
2289: {
2290:   DM_Plex         *mesh       = (DM_Plex *) dm->data;
2291:   const char      *name       = "Hybrid Jacobian";
2292:   DM               dmAux      = NULL;
2293:   DM               plex       = NULL;
2294:   DM               plexA      = NULL;
2295:   DMLabel          ghostLabel = NULL;
2296:   PetscDS          prob       = NULL;
2297:   PetscDS          probAux    = NULL;
2298:   PetscSection     section    = NULL;
2299:   DMField          coordField = NULL;
2300:   Vec              locA;
2301:   PetscScalar     *u = NULL, *u_t, *a = NULL;
2302:   PetscScalar     *elemMat, *elemMatP;
2303:   PetscSection     globalSection, subSection, sectionAux;
2304:   IS               chunkIS;
2305:   const PetscInt  *cells;
2306:   PetscInt        *faces;
2307:   PetscInt         cStart, cEnd, numCells;
2308:   PetscInt         Nf, fieldI, fieldJ, totDim, totDimAux, numChunks, cellChunkSize, chunk;
2309:   PetscInt         maxDegree = PETSC_MAX_INT;
2310:   PetscQuadrature  affineQuad = NULL, *quads = NULL;
2311:   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
2312:   PetscBool        isMatIS = PETSC_FALSE, isMatISP = PETSC_FALSE, hasBdJac, hasBdPrec;
2313:   PetscErrorCode   ierr;

2316:   PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);
2317:   ISGetLocalSize(cellIS, &numCells);
2318:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
2319:   DMConvert(dm, DMPLEX, &plex);
2320:   DMGetSection(dm, &section);
2321:   DMGetGlobalSection(dm, &globalSection);
2322:   DMGetLabel(dm, "ghost", &ghostLabel);
2323:   DMGetCellDS(dm, cStart, &prob);
2324:   PetscDSGetNumFields(prob, &Nf);
2325:   PetscDSGetTotalDimension(prob, &totDim);
2326:   PetscDSHasBdJacobian(prob, &hasBdJac);
2327:   PetscDSHasBdJacobianPreconditioner(prob, &hasBdPrec);
2328:   PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);
2329:   if (isMatISP) {DMPlexGetSubdomainSection(plex, &subSection);}
2330:   if (hasBdPrec && hasBdJac) {PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);}
2331:   if (isMatIS && !subSection) {DMPlexGetSubdomainSection(plex, &subSection);}
2332:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);
2333:   if (locA) {
2334:     VecGetDM(locA, &dmAux);
2335:     DMConvert(dmAux, DMPLEX, &plexA);
2336:     DMGetSection(dmAux, &sectionAux);
2337:     DMGetCellDS(dmAux, cStart, &probAux);
2338:     PetscDSGetTotalDimension(probAux, &totDimAux);
2339:   }
2340:   DMGetCoordinateField(dm, &coordField);
2341:   DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);
2342:   if (maxDegree > 1) {
2343:     PetscInt f;
2344:     PetscCalloc2(Nf,&quads,Nf,&geoms);
2345:     for (f = 0; f < Nf; ++f) {
2346:       PetscFE fe;

2348:       PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);
2349:       if (fe) {
2350:         PetscFEGetQuadrature(fe, &quads[f]);
2351:         PetscObjectReference((PetscObject) quads[f]);
2352:       }
2353:     }
2354:   }
2355:   cellChunkSize = numCells;
2356:   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
2357:   PetscCalloc1(2*cellChunkSize, &faces);
2358:   ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);
2359:   DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);
2360:   DMGetWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);
2361:   DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);
2362:   for (chunk = 0; chunk < numChunks; ++chunk) {
2363:     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;

2365:     if (hasBdJac)  {PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));}
2366:     if (hasBdPrec) {PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));}
2367:     /* Get faces */
2368:     for (c = cS; c < cE; ++c) {
2369:       const PetscInt  cell = cells ? cells[c] : c;
2370:       const PetscInt *cone;
2371:       DMPlexGetCone(plex, cell, &cone);
2372:       faces[(c-cS)*2+0] = cone[0];
2373:       faces[(c-cS)*2+1] = cone[1];
2374:     }
2375:     ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);
2376:     if (maxDegree <= 1) {
2377:       if (!affineQuad) {DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);}
2378:       if (affineQuad)  {DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);}
2379:     } else {
2380:       PetscInt f;
2381:       for (f = 0; f < Nf; ++f) {
2382:         if (quads[f]) {DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);}
2383:       }
2384:     }

2386:     for (fieldI = 0; fieldI < Nf; ++fieldI) {
2387:       PetscFE         feI;
2388:       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[fieldI];
2389:       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
2390:       PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI];
2391:       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;

2393:       PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &feI);
2394:       if (!feI) continue;
2395:       PetscFEGetTileSizes(feI, NULL, &numBlocks, NULL, &numBatches);
2396:       PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);
2397:       PetscFEGetDimension(feI, &Nb);
2398:       blockSize = Nb;
2399:       batchSize = numBlocks * blockSize;
2400:       PetscFESetTileSizes(feI, blockSize, numBlocks, batchSize, numBatches);
2401:       numChunks = numCells / (numBatches*batchSize);
2402:       Ne        = numChunks*numBatches*batchSize;
2403:       Nr        = numCells % (numBatches*batchSize);
2404:       offset    = numCells - Nr;
2405:       PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);
2406:       PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);
2407:       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2408:         PetscFE feJ;

2410:         PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &feJ);
2411:         if (!feJ) continue;
2412:         if (hasBdJac) {
2413:           PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);
2414:           PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);
2415:         }
2416:         if (hasBdPrec) {
2417:           PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);
2418:           PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);
2419:         }
2420:       }
2421:       PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);
2422:       PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);
2423:     }
2424:     /* Insert values into matrix */
2425:     for (c = cS; c < cE; ++c) {
2426:       const PetscInt cell = cells ? cells[c] : c;
2427:       const PetscInt cind = c - cS;

2429:       if (hasBdPrec) {
2430:         if (hasBdJac) {
2431:           if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);}
2432:           if (!isMatIS) {
2433:             DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2434:           } else {
2435:             Mat lJ;

2437:             MatISGetLocalMat(Jac,&lJ);
2438:             DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2439:           }
2440:         }
2441:         if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);}
2442:         if (!isMatISP) {
2443:           DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);
2444:         } else {
2445:           Mat lJ;

2447:           MatISGetLocalMat(JacP,&lJ);
2448:           DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);
2449:         }
2450:       } else if (hasBdJac) {
2451:         if (mesh->printFEM > 1) {DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);}
2452:         if (!isMatISP) {
2453:           DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2454:         } else {
2455:           Mat lJ;

2457:           MatISGetLocalMat(JacP,&lJ);
2458:           DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);
2459:         }
2460:       }
2461:     }
2462:   }
2463:   DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);
2464:   DMRestoreWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);
2465:   DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);
2466:   PetscFree(faces);
2467:   ISDestroy(&chunkIS);
2468:   ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);
2469:   if (maxDegree <= 1) {
2470:     DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);
2471:     PetscQuadratureDestroy(&affineQuad);
2472:   } else {
2473:     PetscInt f;
2474:     for (f = 0; f < Nf; ++f) {
2475:       if (geoms) {DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);}
2476:       if (quads) {PetscQuadratureDestroy(&quads[f]);}
2477:     }
2478:     PetscFree2(quads,geoms);
2479:   }
2480:   if (dmAux) {DMDestroy(&plexA);}
2481:   DMDestroy(&plex);
2482:   /* Assemble matrix */
2483:   if (hasBdJac && hasBdPrec) {
2484:     MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);
2485:     MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);
2486:   }
2487:   MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);
2488:   MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);
2489:   PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);
2490:   return(0);
2491: }

2493: /*@
2494:   DMPlexComputeJacobianAction - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user.

2496:   Input Parameters:
2497: + dm - The mesh
2498: . cellIS -
2499: . t  - The time
2500: . X_tShift - The multiplier for the Jacobian with repsect to X_t
2501: . X  - Local solution vector
2502: . X_t  - Time-derivative of the local solution vector
2503: . Y  - Local input vector
2504: - user - The user context

2506:   Output Parameter:
2507: . Z - Local output vector

2509:   Note:
2510:   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2511:   like a GPU, or vectorize on a multicore machine.

2513:   Level: developer

2515: .seealso: FormFunctionLocal()
2516: @*/
2517: PetscErrorCode DMPlexComputeJacobianAction(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
2518: {
2519:   DM_Plex          *mesh  = (DM_Plex *) dm->data;
2520:   const char       *name  = "Jacobian";
2521:   DM                dmAux, plex, plexAux = NULL;
2522:   DMEnclosureType   encAux;
2523:   Vec               A;
2524:   PetscDS           prob, probAux = NULL;
2525:   PetscQuadrature   quad;
2526:   PetscSection      section, globalSection, sectionAux;
2527:   PetscScalar      *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
2528:   PetscInt          Nf, fieldI, fieldJ;
2529:   PetscInt          totDim, totDimAux = 0;
2530:   const PetscInt   *cells;
2531:   PetscInt          cStart, cEnd, numCells, c;
2532:   PetscBool         hasDyn;
2533:   DMField           coordField;
2534:   PetscErrorCode    ierr;

2537:   PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);
2538:   DMSNESConvertPlex(dm, &plex, PETSC_TRUE);
2539:   if (!cellIS) {
2540:     PetscInt depth;

2542:     DMPlexGetDepth(plex, &depth);
2543:     DMGetStratumIS(plex, "dim", depth, &cellIS);
2544:     if (!cellIS) {DMGetStratumIS(plex, "depth", depth, &cellIS);}
2545:   } else {
2546:     PetscObjectReference((PetscObject) cellIS);
2547:   }
2548:   DMGetLocalSection(dm, &section);
2549:   DMGetGlobalSection(dm, &globalSection);
2550:   DMGetDS(dm, &prob);
2551:   PetscDSGetTotalDimension(prob, &totDim);
2552:   PetscDSHasDynamicJacobian(prob, &hasDyn);
2553:   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2554:   PetscSectionGetNumFields(section, &Nf);
2555:   ISGetLocalSize(cellIS, &numCells);
2556:   ISGetPointRange(cellIS, &cStart, &cEnd, &cells);
2557:   PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);
2558:   PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);
2559:   if (dmAux) {
2560:     DMGetEnclosureRelation(dmAux, dm, &encAux);
2561:     DMConvert(dmAux, DMPLEX, &plexAux);
2562:     DMGetLocalSection(plexAux, &sectionAux);
2563:     DMGetDS(dmAux, &probAux);
2564:     PetscDSGetTotalDimension(probAux, &totDimAux);
2565:   }
2566:   VecSet(Z, 0.0);
2567:   PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);
2568:   if (dmAux) {PetscMalloc1(numCells*totDimAux, &a);}
2569:   DMGetCoordinateField(dm, &coordField);
2570:   for (c = cStart; c < cEnd; ++c) {
2571:     const PetscInt cell = cells ? cells[c] : c;
2572:     const PetscInt cind = c - cStart;
2573:     PetscScalar   *x = NULL,  *x_t = NULL;
2574:     PetscInt       i;

2576:     DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);
2577:     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
2578:     DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);
2579:     if (X_t) {
2580:       DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);
2581:       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
2582:       DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);
2583:     }
2584:     if (dmAux) {
2585:       PetscInt subcell;
2586:       DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);
2587:       DMPlexVecGetClosure(plexAux, sectionAux, A, subcell, NULL, &x);
2588:       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
2589:       DMPlexVecRestoreClosure(plexAux, sectionAux, A, subcell, NULL, &x);
2590:     }
2591:     DMPlexVecGetClosure(dm, section, Y, cell, NULL, &x);
2592:     for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i];
2593:     DMPlexVecRestoreClosure(dm, section, Y, cell, NULL, &x);
2594:   }
2595:   PetscArrayzero(elemMat, numCells*totDim*totDim);
2596:   if (hasDyn)  {PetscArrayzero(elemMatD, numCells*totDim*totDim);}
2597:   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2598:     PetscFE  fe;
2599:     PetscInt Nb;
2600:     /* Conforming batches */
2601:     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2602:     /* Remainder */
2603:     PetscInt Nr, offset, Nq;
2604:     PetscQuadrature qGeom = NULL;
2605:     PetscInt    maxDegree;
2606:     PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;

2608:     PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);
2609:     PetscFEGetQuadrature(fe, &quad);
2610:     PetscFEGetDimension(fe, &Nb);
2611:     PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);
2612:     DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);
2613:     if (maxDegree <= 1) {DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);}
2614:     if (!qGeom) {
2615:       PetscFEGetQuadrature(fe,&qGeom);
2616:       PetscObjectReference((PetscObject)qGeom);
2617:     }
2618:     PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);
2619:     DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);
2620:     blockSize = Nb;
2621:     batchSize = numBlocks * blockSize;
2622:     PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);
2623:     numChunks = numCells / (numBatches*batchSize);
2624:     Ne        = numChunks*numBatches*batchSize;
2625:     Nr        = numCells % (numBatches*batchSize);
2626:     offset    = numCells - Nr;
2627:     PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);
2628:     PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);
2629:     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2630:       PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);
2631:       PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);
2632:       if (hasDyn) {
2633:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);
2634:         PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);
2635:       }
2636:     }
2637:     PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);
2638:     PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);
2639:     DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);
2640:     PetscQuadratureDestroy(&qGeom);
2641:   }
2642:   if (hasDyn) {
2643:     for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2644:   }
2645:   for (c = cStart; c < cEnd; ++c) {
2646:     const PetscInt     cell = cells ? cells[c] : c;
2647:     const PetscInt     cind = c - cStart;
2648:     const PetscBLASInt M = totDim, one = 1;
2649:     const PetscScalar  a = 1.0, b = 0.0;

2651:     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one));
2652:     if (mesh->printFEM > 1) {
2653:       DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);
2654:       DMPrintCellVector(c, "Y",  totDim, &y[cind*totDim]);
2655:       DMPrintCellVector(c, "Z",  totDim, z);
2656:     }
2657:     DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);
2658:   }
2659:   PetscFree6(u,u_t,elemMat,elemMatD,y,z);
2660:   if (mesh->printFEM) {
2661:     PetscPrintf(PetscObjectComm((PetscObject)Z), "Z:\n");
2662:     VecView(Z, NULL);
2663:   }
2664:   PetscFree(a);
2665:   ISDestroy(&cellIS);
2666:   DMDestroy(&plexAux);
2667:   DMDestroy(&plex);
2668:   PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);
2669:   return(0);
2670: }

2672: /*@
2673:   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.

2675:   Input Parameters:
2676: + dm - The mesh
2677: . X  - Local input vector
2678: - user - The user context

2680:   Output Parameter:
2681: . Jac  - Jacobian matrix

2683:   Note:
2684:   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2685:   like a GPU, or vectorize on a multicore machine.

2687:   Level: developer

2689: .seealso: FormFunctionLocal()
2690: @*/
2691: PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
2692: {
2693:   DM             plex;
2694:   IS             allcellIS;
2695:   PetscBool      hasJac, hasPrec;
2696:   PetscInt       Nds, s, depth;

2700:   DMGetNumDS(dm, &Nds);
2701:   DMSNESConvertPlex(dm, &plex, PETSC_TRUE);
2702:   DMPlexGetDepth(plex, &depth);
2703:   DMGetStratumIS(plex, "dim", depth, &allcellIS);
2704:   if (!allcellIS) {DMGetStratumIS(plex, "depth", depth, &allcellIS);}
2705:   for (s = 0; s < Nds; ++s) {
2706:     PetscDS ds;
2707:     DMLabel label;
2708:     IS      cellIS;

2710:     DMGetRegionNumDS(dm, s, &label, NULL, &ds);
2711:     if (!label) {
2712:       PetscObjectReference((PetscObject) allcellIS);
2713:       cellIS = allcellIS;
2714:     } else {
2715:       IS pointIS;

2717:       DMLabelGetStratumIS(label, 1, &pointIS);
2718:       ISIntersect_Caching_Internal(allcellIS, pointIS, &cellIS);
2719:       ISDestroy(&pointIS);
2720:     }
2721:     if (!s) {
2722:       PetscDSHasJacobian(ds, &hasJac);
2723:       PetscDSHasJacobianPreconditioner(ds, &hasPrec);
2724:       if (hasJac && hasPrec) {MatZeroEntries(Jac);}
2725:       MatZeroEntries(JacP);
2726:     }
2727:     DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);
2728:     ISDestroy(&cellIS);
2729:   }
2730:   ISDestroy(&allcellIS);
2731:   DMDestroy(&plex);
2732:   return(0);
2733: }

2735: /*
2736:      MatComputeNeumannOverlap - Computes an unassembled (Neumann) local overlapping Mat in nonlinear context.

2738:    Input Parameters:
2739: +     X - SNES linearization point
2740: .     ovl - index set of overlapping subdomains

2742:    Output Parameter:
2743: .     J - unassembled (Neumann) local matrix

2745:    Level: intermediate

2747: .seealso: DMCreateNeumannOverlap(), MATIS, PCHPDDMSetAuxiliaryMat()
2748: */
2749: static PetscErrorCode MatComputeNeumannOverlap_Plex(Mat J, PetscReal t, Vec X, Vec X_t, PetscReal s, IS ovl, void *ctx)
2750: {
2751:   SNES           snes;
2752:   Mat            pJ;
2753:   DM             ovldm,origdm;
2754:   DMSNES         sdm;
2755:   PetscErrorCode (*bfun)(DM,Vec,void*);
2756:   PetscErrorCode (*jfun)(DM,Vec,Mat,Mat,void*);
2757:   void           *bctx,*jctx;

2761:   PetscObjectQuery((PetscObject)ovl,"_DM_Overlap_HPDDM_MATIS",(PetscObject*)&pJ);
2762:   if (!pJ) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing overlapping Mat");
2763:   PetscObjectQuery((PetscObject)ovl,"_DM_Original_HPDDM",(PetscObject*)&origdm);
2764:   if (!origdm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing original DM");
2765:   MatGetDM(pJ,&ovldm);
2766:   DMSNESGetBoundaryLocal(origdm,&bfun,&bctx);
2767:   DMSNESSetBoundaryLocal(ovldm,bfun,bctx);
2768:   DMSNESGetJacobianLocal(origdm,&jfun,&jctx);
2769:   DMSNESSetJacobianLocal(ovldm,jfun,jctx);
2770:   PetscObjectQuery((PetscObject)ovl,"_DM_Overlap_HPDDM_SNES",(PetscObject*)&snes);
2771:   if (!snes) {
2772:     SNESCreate(PetscObjectComm((PetscObject)ovl),&snes);
2773:     SNESSetDM(snes,ovldm);
2774:     PetscObjectCompose((PetscObject)ovl,"_DM_Overlap_HPDDM_SNES",(PetscObject)snes);
2775:     PetscObjectDereference((PetscObject)snes);
2776:   }
2777:   DMGetDMSNES(ovldm,&sdm);
2778:   VecLockReadPush(X);
2779:   PetscStackPush("SNES user Jacobian function");
2780:   (*sdm->ops->computejacobian)(snes,X,pJ,pJ,sdm->jacobianctx);
2781:   PetscStackPop;
2782:   VecLockReadPop(X);
2783:   /* this is a no-hop, just in case we decide to change the placeholder for the local Neumann matrix */
2784:   {
2785:     Mat locpJ;

2787:     MatISGetLocalMat(pJ,&locpJ);
2788:     MatCopy(locpJ,J,SAME_NONZERO_PATTERN);
2789:   }
2790:   return(0);
2791: }

2793: /*@
2794:   DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian.

2796:   Input Parameters:
2797: + dm - The DM object
2798: . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary())
2799: . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual())
2800: - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian())

2802:   Level: developer
2803: @*/
2804: PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx)
2805: {

2809:   DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);
2810:   DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);
2811:   DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);
2812:   PetscObjectComposeFunction((PetscObject)dm,"MatComputeNeumannOverlap_C",MatComputeNeumannOverlap_Plex);
2813:   return(0);
2814: }

2816: /*@C
2817:   DMSNESCheckDiscretization - Check the discretization error of the exact solution

2819:   Input Parameters:
2820: + snes - the SNES object
2821: . dm   - the DM
2822: . u    - a DM vector
2823: . exactFuncs - pointwise functions of the exact solution for each field
2824: . ctxs - contexts for the functions
2825: - tol  - A tolerance for the check, or -1 to print the results instead

2827:   Output Parameters:
2828: . error - An array which holds the discretization error in each field, or NULL

2830:   Level: developer

2832: .seealso: DNSNESCheckFromOptions(), DMSNESCheckResidual(), DMSNESCheckJacobian()
2833: @*/
2834: PetscErrorCode DMSNESCheckDiscretization(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs, PetscReal tol, PetscReal error[])
2835: {
2836:   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
2837:   void            **ectxs;
2838:   MPI_Comm          comm;
2839:   PetscDS           ds;
2840:   PetscReal        *err;
2841:   PetscInt          Nf, f, Nds, s;
2842:   PetscErrorCode    ierr;

2849:   PetscObjectGetComm((PetscObject) snes, &comm);
2850:   DMGetNumFields(dm, &Nf);
2851:   PetscCalloc3(Nf, &exacts, Nf, &ectxs, PetscMax(1, Nf), &err);
2852:   DMGetNumDS(dm, &Nds);
2853:   for (s = 0; s < Nds; ++s) {
2854:     DMLabel         label;
2855:     IS              fieldIS;
2856:     const PetscInt *fields, id = 1;
2857:     PetscInt        dsNf;

2859:     DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);
2860:     PetscDSGetNumFields(ds, &dsNf);
2861:     ISGetIndices(fieldIS, &fields);
2862:     for (f = 0; f < dsNf; ++f) {
2863:       const PetscInt field = fields[f];
2864:       PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);
2865:     }
2866:     ISRestoreIndices(fieldIS, &fields);
2867:     if (label) {
2868:       DMProjectFunctionLabel(dm, 0.0, label, 1, &id, 0, NULL, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs, INSERT_ALL_VALUES, u);
2869:     } else {
2870:       DMProjectFunction(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs, INSERT_ALL_VALUES, u);
2871:     }
2872:   }
2873:   PetscObjectSetName((PetscObject) u, "Exact Solution");
2874:   PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");
2875:   VecViewFromOptions(u, NULL, "-vec_view");
2876:   if (Nf > 1) {
2877:     DMComputeL2FieldDiff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs, u, err);
2878:     if (tol >= 0.0) {
2879:       for (f = 0; f < Nf; ++f) {
2880:         if (err[f] > tol) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "L_2 Error %g for field %D exceeds tolerance %g", (double) err[f], f, (double) tol);
2881:       }
2882:     } else if (error) {
2883:       for (f = 0; f < Nf; ++f) error[f] = err[f];
2884:     } else {
2885:       PetscPrintf(comm, "L_2 Error: [");
2886:       for (f = 0; f < Nf; ++f) {
2887:         if (f) {PetscPrintf(comm, ", ");}
2888:         PetscPrintf(comm, "%g", (double)err[f]);
2889:       }
2890:       PetscPrintf(comm, "]\n");
2891:     }
2892:   } else {
2893:     DMComputeL2Diff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs , u, &err[0]);
2894:     if (tol >= 0.0) {
2895:       if (err[0] > tol) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "L_2 Error %g exceeds tolerance %g", (double) err[0], (double) tol);
2896:     } else if (error) {
2897:       error[0] = err[0];
2898:     } else {
2899:       PetscPrintf(comm, "L_2 Error: %g\n", (double)err[0]);
2900:     }
2901:   }
2902:   PetscFree3(exacts, ectxs, err);
2903:   return(0);
2904: }

2906: /*@C
2907:   DMSNESCheckResidual - Check the residual of the exact solution

2909:   Input Parameters:
2910: + snes - the SNES object
2911: . dm   - the DM
2912: . u    - a DM vector
2913: - tol  - A tolerance for the check, or -1 to print the results instead

2915:   Output Parameters:
2916: . residual - The residual norm of the exact solution, or NULL

2918:   Level: developer

2920: .seealso: DNSNESCheckFromOptions(), DMSNESCheckDiscretization(), DMSNESCheckJacobian()
2921: @*/
2922: PetscErrorCode DMSNESCheckResidual(SNES snes, DM dm, Vec u, PetscReal tol, PetscReal *residual)
2923: {
2924:   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
2925:   void            **ectxs;
2926:   MPI_Comm          comm;
2927:   PetscDS           ds;
2928:   Vec               r;
2929:   PetscReal         res;
2930:   PetscInt          Nf, f, Nds, s;
2931:   PetscBool         computeSol = PETSC_FALSE;
2932:   PetscErrorCode    ierr;

2939:   PetscObjectGetComm((PetscObject) snes, &comm);
2940:   DMGetDS(dm, &ds);
2941:   DMGetNumFields(dm, &Nf);
2942:   PetscCalloc2(Nf, &exacts, Nf, &ectxs);
2943:   DMGetNumDS(dm, &Nds);
2944:   for (s = 0; s < Nds; ++s) {
2945:     DMLabel         label;
2946:     IS              fieldIS;
2947:     const PetscInt *fields, id = 1;
2948:     PetscInt        dsNf;

2950:     DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);
2951:     PetscDSGetNumFields(ds, &dsNf);
2952:     ISGetIndices(fieldIS, &fields);
2953:     for (f = 0; f < dsNf; ++f) {
2954:       const PetscInt field = fields[f];
2955:       PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);
2956:       if (exacts[f]) computeSol = PETSC_TRUE;
2957:     }
2958:     ISRestoreIndices(fieldIS, &fields);
2959:     if (computeSol) {
2960:       if (label) {
2961:         DMProjectFunctionLabel(dm, 0.0, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);
2962:       } else {
2963:         DMProjectFunction(dm, 0.0, exacts, ectxs, INSERT_ALL_VALUES, u);
2964:       }
2965:     }
2966:   }
2967:   PetscFree2(exacts, ectxs);
2968:   VecDuplicate(u, &r);
2969:   SNESComputeFunction(snes, u, r);
2970:   VecNorm(r, NORM_2, &res);
2971:   if (tol >= 0.0) {
2972:     if (res > tol) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "L_2 Residual %g exceeds tolerance %g", (double) res, (double) tol);
2973:   } else if (residual) {
2974:     *residual = res;
2975:   } else {
2976:     PetscPrintf(comm, "L_2 Residual: %g\n", (double)res);
2977:     VecChop(r, 1.0e-10);
2978:     PetscObjectSetName((PetscObject) r, "Initial Residual");
2979:     PetscObjectSetOptionsPrefix((PetscObject)r,"res_");
2980:     VecViewFromOptions(r, NULL, "-vec_view");
2981:   }
2982:   VecDestroy(&r);
2983:   return(0);
2984: }

2986: /*@C
2987:   DMSNESCheckJacobian - Check the Jacobian of the exact solution against the residual using the Taylor Test

2989:   Input Parameters:
2990: + snes - the SNES object
2991: . dm   - the DM
2992: . u    - a DM vector
2993: - tol  - A tolerance for the check, or -1 to print the results instead

2995:   Output Parameters:
2996: + isLinear - Flag indicaing that the function looks linear, or NULL
2997: - convRate - The rate of convergence of the linear model, or NULL

2999:   Level: developer

3001: .seealso: DNSNESCheckFromOptions(), DMSNESCheckDiscretization(), DMSNESCheckResidual()
3002: @*/
3003: PetscErrorCode DMSNESCheckJacobian(SNES snes, DM dm, Vec u, PetscReal tol, PetscBool *isLinear, PetscReal *convRate)
3004: {
3005:   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
3006:   void            **ectxs;
3007:   MPI_Comm          comm;
3008:   PetscDS           ds;
3009:   Mat               J, M;
3010:   MatNullSpace      nullspace;
3011:   PetscReal         slope, intercept;
3012:   PetscInt          Nf, f, Nds, s;
3013:   PetscBool         hasJac, hasPrec, isLin = PETSC_FALSE, computeSol = PETSC_FALSE;
3014:   PetscErrorCode    ierr;

3022:   PetscObjectGetComm((PetscObject) snes, &comm);
3023:   DMGetDS(dm, &ds);
3024:   DMGetNumFields(dm, &Nf);
3025:   PetscMalloc2(Nf, &exacts, Nf, &ectxs);
3026:   DMGetNumDS(dm, &Nds);
3027:   for (s = 0; s < Nds; ++s) {
3028:     DMLabel         label;
3029:     IS              fieldIS;
3030:     const PetscInt *fields, id = 1;
3031:     PetscInt        dsNf;

3033:     DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);
3034:     PetscDSGetNumFields(ds, &dsNf);
3035:     ISGetIndices(fieldIS, &fields);
3036:     for (f = 0; f < dsNf; ++f) {
3037:       const PetscInt field = fields[f];
3038:       PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);
3039:       if (exacts[f]) computeSol = PETSC_TRUE;
3040:     }
3041:     ISRestoreIndices(fieldIS, &fields);
3042:     if (computeSol) {
3043:       if (label) {
3044:         DMProjectFunctionLabel(dm, 0.0, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);
3045:       } else {
3046:         DMProjectFunction(dm, 0.0, exacts, ectxs, INSERT_ALL_VALUES, u);
3047:       }
3048:     }
3049:   }
3050:   PetscFree2(exacts, ectxs);

3052:   /* Create and view matrices */
3053:   DMCreateMatrix(dm, &J);
3054:   PetscDSHasJacobian(ds, &hasJac);
3055:   PetscDSHasJacobianPreconditioner(ds, &hasPrec);
3056:   if (hasJac && hasPrec) {
3057:     DMCreateMatrix(dm, &M);
3058:     SNESComputeJacobian(snes, u, J, M);
3059:     PetscObjectSetName((PetscObject) M, "Preconditioning Matrix");
3060:     PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");
3061:     MatViewFromOptions(M, NULL, "-mat_view");
3062:     MatDestroy(&M);
3063:   } else {
3064:     SNESComputeJacobian(snes, u, J, J);
3065:   }
3066:   PetscObjectSetName((PetscObject) J, "Jacobian");
3067:   PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");
3068:   MatViewFromOptions(J, NULL, "-mat_view");
3069:   /* Check nullspace */
3070:   MatGetNullSpace(J, &nullspace);
3071:   if (nullspace) {
3072:     PetscBool isNull;
3073:     MatNullSpaceTest(nullspace, J, &isNull);
3074:     if (!isNull) SETERRQ(comm, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
3075:   }
3076:   MatNullSpaceDestroy(&nullspace);
3077:   /* Taylor test */
3078:   {
3079:     PetscRandom rand;
3080:     Vec         du, uhat, r, rhat, df;
3081:     PetscReal   h;
3082:     PetscReal  *es, *hs, *errors;
3083:     PetscReal   hMax = 1.0, hMin = 1e-6, hMult = 0.1;
3084:     PetscInt    Nv, v;

3086:     /* Choose a perturbation direction */
3087:     PetscRandomCreate(comm, &rand);
3088:     VecDuplicate(u, &du);
3089:     VecSetRandom(du, rand); 
3090:     PetscRandomDestroy(&rand);
3091:     VecDuplicate(u, &df);
3092:     MatMult(J, du, df);
3093:     /* Evaluate residual at u, F(u), save in vector r */
3094:     VecDuplicate(u, &r);
3095:     SNESComputeFunction(snes, u, r);
3096:     /* Look at the convergence of our Taylor approximation as we approach u */
3097:     for (h = hMax, Nv = 0; h >= hMin; h *= hMult, ++Nv);
3098:     PetscCalloc3(Nv, &es, Nv, &hs, Nv, &errors);
3099:     VecDuplicate(u, &uhat);
3100:     VecDuplicate(u, &rhat);
3101:     for (h = hMax, Nv = 0; h >= hMin; h *= hMult, ++Nv) {
3102:       VecWAXPY(uhat, h, du, u);
3103:       /* F(\hat u) \approx F(u) + J(u) (uhat - u) = F(u) + h * J(u) du */
3104:       SNESComputeFunction(snes, uhat, rhat);
3105:       VecAXPBYPCZ(rhat, -1.0, -h, 1.0, r, df);
3106:       VecNorm(rhat, NORM_2, &errors[Nv]);

3108:       es[Nv] = PetscLog10Real(errors[Nv]);
3109:       hs[Nv] = PetscLog10Real(h);
3110:     }
3111:     VecDestroy(&uhat);
3112:     VecDestroy(&rhat);
3113:     VecDestroy(&df);
3114:     VecDestroy(&r);
3115:     VecDestroy(&du);
3116:     for (v = 0; v < Nv; ++v) {
3117:       if ((tol >= 0) && (errors[v] > tol)) break;
3118:       else if (errors[v] > PETSC_SMALL)    break;
3119:     }
3120:     if (v == Nv) isLin = PETSC_TRUE;
3121:     PetscLinearRegression(Nv, hs, es, &slope, &intercept);
3122:     PetscFree3(es, hs, errors);
3123:     /* Slope should be about 2 */
3124:     if (tol >= 0) {
3125:       if (!isLin && PetscAbsReal(2 - slope) > tol) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Taylor approximation convergence rate should be 2, not %0.2f", (double) slope);
3126:     } else if (isLinear || convRate) {
3127:       if (isLinear) *isLinear = isLin;
3128:       if (convRate) *convRate = slope;
3129:     } else {
3130:       if (!isLin) {PetscPrintf(comm, "Taylor approximation converging at order %3.2f\n", (double) slope);}
3131:       else        {PetscPrintf(comm, "Function appears to be linear\n");}
3132:     }
3133:   }
3134:   MatDestroy(&J);
3135:   return(0);
3136: }

3138: PetscErrorCode DMSNESCheck_Internal(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs)
3139: {

3143:   DMSNESCheckDiscretization(snes, dm, u, exactFuncs, ctxs, -1.0, NULL);
3144:   DMSNESCheckResidual(snes, dm, u, -1.0, NULL);
3145:   DMSNESCheckJacobian(snes, dm, u, -1.0, NULL, NULL);
3146:   return(0);
3147: }

3149: /*@C
3150:   DMSNESCheckFromOptions - Check the residual and Jacobian functions using the exact solution by outputting some diagnostic information

3152:   Input Parameters:
3153: + snes - the SNES object
3154: . u    - representative SNES vector
3155: . exactFuncs - pointwise functions of the exact solution for each field
3156: - ctxs - contexts for the functions

3158:   Level: developer
3159: @*/
3160: PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
3161: {
3162:   DM             dm;
3163:   Vec            sol;
3164:   PetscBool      check;

3168:   PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);
3169:   if (!check) return(0);
3170:   SNESGetDM(snes, &dm);
3171:   VecDuplicate(u, &sol);
3172:   SNESSetSolution(snes, sol);
3173:   DMSNESCheck_Internal(snes, dm, sol, exactFuncs, ctxs);
3174:   VecDestroy(&sol);
3175:   return(0);
3176: }