124cdb843SMatthew G. Knepley #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 27d4028c8SMatthew G. Knepley #include <petsc-private/snesimpl.h> /*I "petscsnes.h" I*/ 324cdb843SMatthew G. Knepley #include <petscds.h> 4afcb2eb5SJed Brown #include <petsc-private/petscimpl.h> 5*477f7dfdSMatthew G. Knepley #include <petsc-private/petscfeimpl.h> 6552f7358SJed Brown 724cdb843SMatthew G. Knepley /************************** Interpolation *******************************/ 824cdb843SMatthew G. Knepley 9552f7358SJed Brown #undef __FUNCT__ 10552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate" 110adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 120adebc6cSBarry Smith { 13552f7358SJed Brown PetscErrorCode ierr; 14552f7358SJed Brown 15552f7358SJed Brown PetscFunctionBegin; 16552f7358SJed Brown PetscValidPointer(ctx, 2); 17552f7358SJed Brown ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr); 181aa26658SKarl Rupp 19552f7358SJed Brown (*ctx)->comm = comm; 20552f7358SJed Brown (*ctx)->dim = -1; 21552f7358SJed Brown (*ctx)->nInput = 0; 220298fd71SBarry Smith (*ctx)->points = NULL; 230298fd71SBarry Smith (*ctx)->cells = NULL; 24552f7358SJed Brown (*ctx)->n = -1; 250298fd71SBarry Smith (*ctx)->coords = NULL; 26552f7358SJed Brown PetscFunctionReturn(0); 27552f7358SJed Brown } 28552f7358SJed Brown 29552f7358SJed Brown #undef __FUNCT__ 30552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim" 310adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 320adebc6cSBarry Smith { 33552f7358SJed Brown PetscFunctionBegin; 340adebc6cSBarry Smith if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 35552f7358SJed Brown ctx->dim = dim; 36552f7358SJed Brown PetscFunctionReturn(0); 37552f7358SJed Brown } 38552f7358SJed Brown 39552f7358SJed Brown #undef __FUNCT__ 40552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim" 410adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 420adebc6cSBarry Smith { 43552f7358SJed Brown PetscFunctionBegin; 44552f7358SJed Brown PetscValidIntPointer(dim, 2); 45552f7358SJed Brown *dim = ctx->dim; 46552f7358SJed Brown PetscFunctionReturn(0); 47552f7358SJed Brown } 48552f7358SJed Brown 49552f7358SJed Brown #undef __FUNCT__ 50552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof" 510adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 520adebc6cSBarry Smith { 53552f7358SJed Brown PetscFunctionBegin; 540adebc6cSBarry Smith if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 55552f7358SJed Brown ctx->dof = dof; 56552f7358SJed Brown PetscFunctionReturn(0); 57552f7358SJed Brown } 58552f7358SJed Brown 59552f7358SJed Brown #undef __FUNCT__ 60552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof" 610adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 620adebc6cSBarry Smith { 63552f7358SJed Brown PetscFunctionBegin; 64552f7358SJed Brown PetscValidIntPointer(dof, 2); 65552f7358SJed Brown *dof = ctx->dof; 66552f7358SJed Brown PetscFunctionReturn(0); 67552f7358SJed Brown } 68552f7358SJed Brown 69552f7358SJed Brown #undef __FUNCT__ 70552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints" 710adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 720adebc6cSBarry Smith { 73552f7358SJed Brown PetscErrorCode ierr; 74552f7358SJed Brown 75552f7358SJed Brown PetscFunctionBegin; 760adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 770adebc6cSBarry Smith if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 78552f7358SJed Brown ctx->nInput = n; 791aa26658SKarl Rupp 80785e854fSJed Brown ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 81552f7358SJed Brown ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 82552f7358SJed Brown PetscFunctionReturn(0); 83552f7358SJed Brown } 84552f7358SJed Brown 85552f7358SJed Brown #undef __FUNCT__ 86552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp" 870adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 880adebc6cSBarry Smith { 89552f7358SJed Brown MPI_Comm comm = ctx->comm; 90552f7358SJed Brown PetscScalar *a; 91552f7358SJed Brown PetscInt p, q, i; 92552f7358SJed Brown PetscMPIInt rank, size; 93552f7358SJed Brown PetscErrorCode ierr; 94552f7358SJed Brown Vec pointVec; 95552f7358SJed Brown IS cellIS; 96552f7358SJed Brown PetscLayout layout; 97552f7358SJed Brown PetscReal *globalPoints; 98cb313848SJed Brown PetscScalar *globalPointsScalar; 99552f7358SJed Brown const PetscInt *ranges; 100552f7358SJed Brown PetscMPIInt *counts, *displs; 101552f7358SJed Brown const PetscInt *foundCells; 102552f7358SJed Brown PetscMPIInt *foundProcs, *globalProcs; 10319436ca2SJed Brown PetscInt n, N; 104552f7358SJed Brown 10519436ca2SJed Brown PetscFunctionBegin; 10619436ca2SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10719436ca2SJed Brown ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 10819436ca2SJed Brown ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 1090adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 11019436ca2SJed Brown /* Locate points */ 11119436ca2SJed Brown n = ctx->nInput; 112552f7358SJed Brown if (!redundantPoints) { 113552f7358SJed Brown ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 114552f7358SJed Brown ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 115552f7358SJed Brown ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 116552f7358SJed Brown ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 117552f7358SJed Brown ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 118552f7358SJed Brown /* Communicate all points to all processes */ 119dcca6d9dSJed Brown ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 120552f7358SJed Brown ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 121552f7358SJed Brown for (p = 0; p < size; ++p) { 122552f7358SJed Brown counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 123552f7358SJed Brown displs[p] = ranges[p]*ctx->dim; 124552f7358SJed Brown } 125552f7358SJed Brown ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 126552f7358SJed Brown } else { 127552f7358SJed Brown N = n; 128552f7358SJed Brown globalPoints = ctx->points; 12938ea73c8SJed Brown counts = displs = NULL; 13038ea73c8SJed Brown layout = NULL; 131552f7358SJed Brown } 132552f7358SJed Brown #if 0 133dcca6d9dSJed Brown ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 13419436ca2SJed Brown /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 135552f7358SJed Brown #else 136cb313848SJed Brown #if defined(PETSC_USE_COMPLEX) 137785e854fSJed Brown ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr); 138cb313848SJed Brown for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i]; 139cb313848SJed Brown #else 140cb313848SJed Brown globalPointsScalar = globalPoints; 141cb313848SJed Brown #endif 14204706141SMatthew G Knepley ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 143dcca6d9dSJed Brown ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 144552f7358SJed Brown ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr); 145552f7358SJed Brown ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr); 146552f7358SJed Brown #endif 147552f7358SJed Brown for (p = 0; p < N; ++p) { 1481aa26658SKarl Rupp if (foundCells[p] >= 0) foundProcs[p] = rank; 1491aa26658SKarl Rupp else foundProcs[p] = size; 150552f7358SJed Brown } 151552f7358SJed Brown /* Let the lowest rank process own each point */ 152efab3cc2SJed Brown ierr = MPI_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 153552f7358SJed Brown ctx->n = 0; 154552f7358SJed Brown for (p = 0; p < N; ++p) { 1550adebc6cSBarry Smith if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0); 1561aa26658SKarl Rupp else if (globalProcs[p] == rank) ctx->n++; 157552f7358SJed Brown } 158552f7358SJed Brown /* Create coordinates vector and array of owned cells */ 159785e854fSJed Brown ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 160552f7358SJed Brown ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 161552f7358SJed Brown ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 162552f7358SJed Brown ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 163c0dedaeaSBarry Smith ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 164552f7358SJed Brown ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 165552f7358SJed Brown for (p = 0, q = 0, i = 0; p < N; ++p) { 166552f7358SJed Brown if (globalProcs[p] == rank) { 167552f7358SJed Brown PetscInt d; 168552f7358SJed Brown 1691aa26658SKarl Rupp for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 170552f7358SJed Brown ctx->cells[q++] = foundCells[p]; 171552f7358SJed Brown } 172552f7358SJed Brown } 173552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 174552f7358SJed Brown #if 0 175552f7358SJed Brown ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 176552f7358SJed Brown #else 177552f7358SJed Brown ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 178552f7358SJed Brown ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr); 179552f7358SJed Brown ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 180552f7358SJed Brown ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 181552f7358SJed Brown #endif 182cb313848SJed Brown if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 183d343d804SMatthew G. Knepley if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 184552f7358SJed Brown ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 185552f7358SJed Brown PetscFunctionReturn(0); 186552f7358SJed Brown } 187552f7358SJed Brown 188552f7358SJed Brown #undef __FUNCT__ 189552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates" 1900adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 1910adebc6cSBarry Smith { 192552f7358SJed Brown PetscFunctionBegin; 193552f7358SJed Brown PetscValidPointer(coordinates, 2); 1940adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 195552f7358SJed Brown *coordinates = ctx->coords; 196552f7358SJed Brown PetscFunctionReturn(0); 197552f7358SJed Brown } 198552f7358SJed Brown 199552f7358SJed Brown #undef __FUNCT__ 200552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector" 2010adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 2020adebc6cSBarry Smith { 203552f7358SJed Brown PetscErrorCode ierr; 204552f7358SJed Brown 205552f7358SJed Brown PetscFunctionBegin; 206552f7358SJed Brown PetscValidPointer(v, 2); 2070adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 208552f7358SJed Brown ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 209552f7358SJed Brown ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 210552f7358SJed Brown ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 211c0dedaeaSBarry Smith ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 212552f7358SJed Brown PetscFunctionReturn(0); 213552f7358SJed Brown } 214552f7358SJed Brown 215552f7358SJed Brown #undef __FUNCT__ 216552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector" 2170adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 2180adebc6cSBarry Smith { 219552f7358SJed Brown PetscErrorCode ierr; 220552f7358SJed Brown 221552f7358SJed Brown PetscFunctionBegin; 222552f7358SJed Brown PetscValidPointer(v, 2); 2230adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 224552f7358SJed Brown ierr = VecDestroy(v);CHKERRQ(ierr); 225552f7358SJed Brown PetscFunctionReturn(0); 226552f7358SJed Brown } 227552f7358SJed Brown 228552f7358SJed Brown #undef __FUNCT__ 2297a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private" 2307a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 231a6dfd86eSKarl Rupp { 232552f7358SJed Brown PetscReal *v0, *J, *invJ, detJ; 233552f7358SJed Brown PetscScalar *a, *coords; 234552f7358SJed Brown PetscInt p; 235552f7358SJed Brown PetscErrorCode ierr; 236552f7358SJed Brown 237552f7358SJed Brown PetscFunctionBegin; 238dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 239552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 240552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 241552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 242552f7358SJed Brown PetscInt c = ctx->cells[p]; 243a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 244552f7358SJed Brown PetscReal xi[4]; 245552f7358SJed Brown PetscInt d, f, comp; 246552f7358SJed Brown 2478e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 248552f7358SJed Brown if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2490298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2501aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2511aa26658SKarl Rupp 252552f7358SJed Brown for (d = 0; d < ctx->dim; ++d) { 253552f7358SJed Brown xi[d] = 0.0; 2541aa26658SKarl Rupp for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]); 2551aa26658SKarl Rupp 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]; 256552f7358SJed Brown } 2570298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 258552f7358SJed Brown } 259552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 260552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 261552f7358SJed Brown ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 262552f7358SJed Brown PetscFunctionReturn(0); 263552f7358SJed Brown } 264552f7358SJed Brown 265552f7358SJed Brown #undef __FUNCT__ 2667a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private" 2677a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 2687a1931ceSMatthew G. Knepley { 2697a1931ceSMatthew G. Knepley PetscReal *v0, *J, *invJ, detJ; 2707a1931ceSMatthew G. Knepley PetscScalar *a, *coords; 2717a1931ceSMatthew G. Knepley PetscInt p; 2727a1931ceSMatthew G. Knepley PetscErrorCode ierr; 2737a1931ceSMatthew G. Knepley 2747a1931ceSMatthew G. Knepley PetscFunctionBegin; 275dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 2767a1931ceSMatthew G. Knepley ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 2777a1931ceSMatthew G. Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2787a1931ceSMatthew G. Knepley for (p = 0; p < ctx->n; ++p) { 2797a1931ceSMatthew G. Knepley PetscInt c = ctx->cells[p]; 2807a1931ceSMatthew G. Knepley const PetscInt order[3] = {2, 1, 3}; 2812584bbe8SMatthew G. Knepley PetscScalar *x = NULL; 2827a1931ceSMatthew G. Knepley PetscReal xi[4]; 2837a1931ceSMatthew G. Knepley PetscInt d, f, comp; 2847a1931ceSMatthew G. Knepley 2858e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2867a1931ceSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2877a1931ceSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2887a1931ceSMatthew G. Knepley for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2897a1931ceSMatthew G. Knepley 2907a1931ceSMatthew G. Knepley for (d = 0; d < ctx->dim; ++d) { 2917a1931ceSMatthew G. Knepley xi[d] = 0.0; 2927a1931ceSMatthew G. Knepley for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]); 2937a1931ceSMatthew G. Knepley 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]; 2947a1931ceSMatthew G. Knepley } 2957a1931ceSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2967a1931ceSMatthew G. Knepley } 2977a1931ceSMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2987a1931ceSMatthew G. Knepley ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 2997a1931ceSMatthew G. Knepley ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 3007a1931ceSMatthew G. Knepley PetscFunctionReturn(0); 3017a1931ceSMatthew G. Knepley } 3027a1931ceSMatthew G. Knepley 3037a1931ceSMatthew G. Knepley #undef __FUNCT__ 304552f7358SJed Brown #define __FUNCT__ "QuadMap_Private" 3055820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 306552f7358SJed Brown { 307552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 308552f7358SJed Brown const PetscScalar x0 = vertices[0]; 309552f7358SJed Brown const PetscScalar y0 = vertices[1]; 310552f7358SJed Brown const PetscScalar x1 = vertices[2]; 311552f7358SJed Brown const PetscScalar y1 = vertices[3]; 312552f7358SJed Brown const PetscScalar x2 = vertices[4]; 313552f7358SJed Brown const PetscScalar y2 = vertices[5]; 314552f7358SJed Brown const PetscScalar x3 = vertices[6]; 315552f7358SJed Brown const PetscScalar y3 = vertices[7]; 316552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 317552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 318552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 319552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 320552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 321552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 322552f7358SJed Brown PetscScalar *ref, *real; 323552f7358SJed Brown PetscErrorCode ierr; 324552f7358SJed Brown 325552f7358SJed Brown PetscFunctionBegin; 326552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 327552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 328552f7358SJed Brown { 329552f7358SJed Brown const PetscScalar p0 = ref[0]; 330552f7358SJed Brown const PetscScalar p1 = ref[1]; 331552f7358SJed Brown 332552f7358SJed Brown real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 333552f7358SJed Brown real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 334552f7358SJed Brown } 335552f7358SJed Brown ierr = PetscLogFlops(28);CHKERRQ(ierr); 336552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 337552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 338552f7358SJed Brown PetscFunctionReturn(0); 339552f7358SJed Brown } 340552f7358SJed Brown 341c0dedaeaSBarry Smith #include <petsc-private/dmimpl.h> 342552f7358SJed Brown #undef __FUNCT__ 343552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private" 344d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 345552f7358SJed Brown { 346552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 347552f7358SJed Brown const PetscScalar x0 = vertices[0]; 348552f7358SJed Brown const PetscScalar y0 = vertices[1]; 349552f7358SJed Brown const PetscScalar x1 = vertices[2]; 350552f7358SJed Brown const PetscScalar y1 = vertices[3]; 351552f7358SJed Brown const PetscScalar x2 = vertices[4]; 352552f7358SJed Brown const PetscScalar y2 = vertices[5]; 353552f7358SJed Brown const PetscScalar x3 = vertices[6]; 354552f7358SJed Brown const PetscScalar y3 = vertices[7]; 355552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 356552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 357552f7358SJed Brown PetscScalar *ref; 358552f7358SJed Brown PetscErrorCode ierr; 359552f7358SJed Brown 360552f7358SJed Brown PetscFunctionBegin; 361552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 362552f7358SJed Brown { 363552f7358SJed Brown const PetscScalar x = ref[0]; 364552f7358SJed Brown const PetscScalar y = ref[1]; 365552f7358SJed Brown const PetscInt rows[2] = {0, 1}; 366da80777bSKarl Rupp PetscScalar values[4]; 367da80777bSKarl Rupp 368da80777bSKarl Rupp values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 369da80777bSKarl Rupp values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 37094ab13aaSBarry Smith ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 371552f7358SJed Brown } 372552f7358SJed Brown ierr = PetscLogFlops(30);CHKERRQ(ierr); 373552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 37494ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 37594ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 376552f7358SJed Brown PetscFunctionReturn(0); 377552f7358SJed Brown } 378552f7358SJed Brown 379552f7358SJed Brown #undef __FUNCT__ 380552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private" 381a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 382a6dfd86eSKarl Rupp { 383fafc0619SMatthew G Knepley DM dmCoord; 384552f7358SJed Brown SNES snes; 385552f7358SJed Brown KSP ksp; 386552f7358SJed Brown PC pc; 387552f7358SJed Brown Vec coordsLocal, r, ref, real; 388552f7358SJed Brown Mat J; 389552f7358SJed Brown PetscScalar *a, *coords; 390552f7358SJed Brown PetscInt p; 391552f7358SJed Brown PetscErrorCode ierr; 392552f7358SJed Brown 393552f7358SJed Brown PetscFunctionBegin; 394552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 395fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 396552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 397552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 398552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 399552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 400c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 401552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 402552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 403552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 404552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 405552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 406552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 4070298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 4080298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 409552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 410552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 411552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 412552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 413552f7358SJed Brown 414552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 415552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 416552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 417a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 418552f7358SJed Brown PetscScalar *xi; 419cb313848SJed Brown PetscReal xir[2]; 420552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 421552f7358SJed Brown 422552f7358SJed Brown /* Can make this do all points at once */ 4230298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4240adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 4250298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 4260adebc6cSBarry Smith if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof); 4270298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 4280298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 429552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 430552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 431552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 432552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 433552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 434552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 435cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 436cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 4371aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1]; 4381aa26658SKarl Rupp 439552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 4400298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4410298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 442552f7358SJed Brown } 443552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 444552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 445552f7358SJed Brown 446552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 447552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 448552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 449552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 450552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 451552f7358SJed Brown PetscFunctionReturn(0); 452552f7358SJed Brown } 453552f7358SJed Brown 454552f7358SJed Brown #undef __FUNCT__ 455552f7358SJed Brown #define __FUNCT__ "HexMap_Private" 4565820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 457552f7358SJed Brown { 458552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 459552f7358SJed Brown const PetscScalar x0 = vertices[0]; 460552f7358SJed Brown const PetscScalar y0 = vertices[1]; 461552f7358SJed Brown const PetscScalar z0 = vertices[2]; 4627a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 4637a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 4647a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 465552f7358SJed Brown const PetscScalar x2 = vertices[6]; 466552f7358SJed Brown const PetscScalar y2 = vertices[7]; 467552f7358SJed Brown const PetscScalar z2 = vertices[8]; 4687a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 4697a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 4707a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 471552f7358SJed Brown const PetscScalar x4 = vertices[12]; 472552f7358SJed Brown const PetscScalar y4 = vertices[13]; 473552f7358SJed Brown const PetscScalar z4 = vertices[14]; 474552f7358SJed Brown const PetscScalar x5 = vertices[15]; 475552f7358SJed Brown const PetscScalar y5 = vertices[16]; 476552f7358SJed Brown const PetscScalar z5 = vertices[17]; 477552f7358SJed Brown const PetscScalar x6 = vertices[18]; 478552f7358SJed Brown const PetscScalar y6 = vertices[19]; 479552f7358SJed Brown const PetscScalar z6 = vertices[20]; 480552f7358SJed Brown const PetscScalar x7 = vertices[21]; 481552f7358SJed Brown const PetscScalar y7 = vertices[22]; 482552f7358SJed Brown const PetscScalar z7 = vertices[23]; 483552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 484552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 485552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 486552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 487552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 488552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 489552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 490552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 491552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 492552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 493552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 494552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 495552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 496552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 497552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 498552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 499552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 500552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 501552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 502552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 503552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 504552f7358SJed Brown PetscScalar *ref, *real; 505552f7358SJed Brown PetscErrorCode ierr; 506552f7358SJed Brown 507552f7358SJed Brown PetscFunctionBegin; 508552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 509552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 510552f7358SJed Brown { 511552f7358SJed Brown const PetscScalar p0 = ref[0]; 512552f7358SJed Brown const PetscScalar p1 = ref[1]; 513552f7358SJed Brown const PetscScalar p2 = ref[2]; 514552f7358SJed Brown 515552f7358SJed Brown 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; 516552f7358SJed Brown 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; 517552f7358SJed Brown 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; 518552f7358SJed Brown } 519552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 520552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 521552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 522552f7358SJed Brown PetscFunctionReturn(0); 523552f7358SJed Brown } 524552f7358SJed Brown 525552f7358SJed Brown #undef __FUNCT__ 526552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private" 527d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 528552f7358SJed Brown { 529552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 530552f7358SJed Brown const PetscScalar x0 = vertices[0]; 531552f7358SJed Brown const PetscScalar y0 = vertices[1]; 532552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5337a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5347a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5357a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 536552f7358SJed Brown const PetscScalar x2 = vertices[6]; 537552f7358SJed Brown const PetscScalar y2 = vertices[7]; 538552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5397a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5407a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5417a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 542552f7358SJed Brown const PetscScalar x4 = vertices[12]; 543552f7358SJed Brown const PetscScalar y4 = vertices[13]; 544552f7358SJed Brown const PetscScalar z4 = vertices[14]; 545552f7358SJed Brown const PetscScalar x5 = vertices[15]; 546552f7358SJed Brown const PetscScalar y5 = vertices[16]; 547552f7358SJed Brown const PetscScalar z5 = vertices[17]; 548552f7358SJed Brown const PetscScalar x6 = vertices[18]; 549552f7358SJed Brown const PetscScalar y6 = vertices[19]; 550552f7358SJed Brown const PetscScalar z6 = vertices[20]; 551552f7358SJed Brown const PetscScalar x7 = vertices[21]; 552552f7358SJed Brown const PetscScalar y7 = vertices[22]; 553552f7358SJed Brown const PetscScalar z7 = vertices[23]; 554552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 555552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 556552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 557552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 558552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 559552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 560552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 561552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 562552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 563552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 564552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 565552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 566552f7358SJed Brown PetscScalar *ref; 567552f7358SJed Brown PetscErrorCode ierr; 568552f7358SJed Brown 569552f7358SJed Brown PetscFunctionBegin; 570552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 571552f7358SJed Brown { 572552f7358SJed Brown const PetscScalar x = ref[0]; 573552f7358SJed Brown const PetscScalar y = ref[1]; 574552f7358SJed Brown const PetscScalar z = ref[2]; 575552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 576da80777bSKarl Rupp PetscScalar values[9]; 577da80777bSKarl Rupp 578da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 579da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 580da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 581da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 582da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 583da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 584da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 585da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 586da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 5871aa26658SKarl Rupp 58894ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 589552f7358SJed Brown } 590552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 591552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 59294ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 59394ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 594552f7358SJed Brown PetscFunctionReturn(0); 595552f7358SJed Brown } 596552f7358SJed Brown 597552f7358SJed Brown #undef __FUNCT__ 598552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private" 599a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 600a6dfd86eSKarl Rupp { 601fafc0619SMatthew G Knepley DM dmCoord; 602552f7358SJed Brown SNES snes; 603552f7358SJed Brown KSP ksp; 604552f7358SJed Brown PC pc; 605552f7358SJed Brown Vec coordsLocal, r, ref, real; 606552f7358SJed Brown Mat J; 607552f7358SJed Brown PetscScalar *a, *coords; 608552f7358SJed Brown PetscInt p; 609552f7358SJed Brown PetscErrorCode ierr; 610552f7358SJed Brown 611552f7358SJed Brown PetscFunctionBegin; 612552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 613fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 614552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 615552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 616552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 617552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 618c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 619552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 620552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 621552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 622552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 623552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 624552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 6250298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 6260298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 627552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 628552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 629552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 630552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 631552f7358SJed Brown 632552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 633552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 634552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 635a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 636552f7358SJed Brown PetscScalar *xi; 637cb313848SJed Brown PetscReal xir[3]; 638552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 639552f7358SJed Brown 640552f7358SJed Brown /* Can make this do all points at once */ 6410298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6420adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 6430298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 6440adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 6450298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 6460298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 647552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 648552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 649552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 650552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 651552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 652552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 653552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 654cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 655cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 656cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 657552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 658552f7358SJed Brown a[p*ctx->dof+comp] = 659cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 6607a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 661cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 6627a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 663cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 664cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 665cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 666cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 667552f7358SJed Brown } 668552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 6690298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6700298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 671552f7358SJed Brown } 672552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 673552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 674552f7358SJed Brown 675552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 676552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 677552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 678552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 679552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 680552f7358SJed Brown PetscFunctionReturn(0); 681552f7358SJed Brown } 682552f7358SJed Brown 683552f7358SJed Brown #undef __FUNCT__ 684552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate" 685552f7358SJed Brown /* 686552f7358SJed Brown Input Parameters: 687552f7358SJed Brown + ctx - The DMInterpolationInfo context 688552f7358SJed Brown . dm - The DM 689552f7358SJed Brown - x - The local vector containing the field to be interpolated 690552f7358SJed Brown 691552f7358SJed Brown Output Parameters: 692552f7358SJed Brown . v - The vector containing the interpolated values 693552f7358SJed Brown */ 6940adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 6950adebc6cSBarry Smith { 696552f7358SJed Brown PetscInt dim, coneSize, n; 697552f7358SJed Brown PetscErrorCode ierr; 698552f7358SJed Brown 699552f7358SJed Brown PetscFunctionBegin; 700552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 701552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 702552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 703552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 7040adebc6cSBarry Smith 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); 705552f7358SJed Brown if (n) { 706c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 707552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 708552f7358SJed Brown if (dim == 2) { 709552f7358SJed Brown if (coneSize == 3) { 7107a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 711552f7358SJed Brown } else if (coneSize == 4) { 712552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 7130adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 714552f7358SJed Brown } else if (dim == 3) { 715552f7358SJed Brown if (coneSize == 4) { 7167a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 717552f7358SJed Brown } else { 718552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 719552f7358SJed Brown } 7200adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 721552f7358SJed Brown } 722552f7358SJed Brown PetscFunctionReturn(0); 723552f7358SJed Brown } 724552f7358SJed Brown 725552f7358SJed Brown #undef __FUNCT__ 726552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy" 7270adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 7280adebc6cSBarry Smith { 729552f7358SJed Brown PetscErrorCode ierr; 730552f7358SJed Brown 731552f7358SJed Brown PetscFunctionBegin; 732552f7358SJed Brown PetscValidPointer(ctx, 2); 733552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 734552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 735552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 736552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 7370298fd71SBarry Smith *ctx = NULL; 738552f7358SJed Brown PetscFunctionReturn(0); 739552f7358SJed Brown } 740cc0c4584SMatthew G. Knepley 741cc0c4584SMatthew G. Knepley #undef __FUNCT__ 742cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields" 743cc0c4584SMatthew G. Knepley /*@C 744cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 745cc0c4584SMatthew G. Knepley 746cc0c4584SMatthew G. Knepley Collective on SNES 747cc0c4584SMatthew G. Knepley 748cc0c4584SMatthew G. Knepley Input Parameters: 749cc0c4584SMatthew G. Knepley + snes - the SNES context 750cc0c4584SMatthew G. Knepley . its - iteration number 751cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 752cc0c4584SMatthew G. Knepley - dummy - unused context 753cc0c4584SMatthew G. Knepley 754cc0c4584SMatthew G. Knepley Notes: 755cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 756cc0c4584SMatthew G. Knepley 757cc0c4584SMatthew G. Knepley Level: intermediate 758cc0c4584SMatthew G. Knepley 759cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 760cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 761cc0c4584SMatthew G. Knepley @*/ 762cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy) 763cc0c4584SMatthew G. Knepley { 764cc0c4584SMatthew G. Knepley PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) snes)); 765cc0c4584SMatthew G. Knepley Vec res; 766cc0c4584SMatthew G. Knepley DM dm; 767cc0c4584SMatthew G. Knepley PetscSection s; 768cc0c4584SMatthew G. Knepley const PetscScalar *r; 769cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 770cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 771cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 772cc0c4584SMatthew G. Knepley 773cc0c4584SMatthew G. Knepley PetscFunctionBegin; 774cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 775cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 776cc0c4584SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 777cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 778cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 779cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 780cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 781cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 782cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 783cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 784cc0c4584SMatthew G. Knepley 785cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 786cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 787cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 788cc0c4584SMatthew G. Knepley } 789cc0c4584SMatthew G. Knepley } 790cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 791cc0c4584SMatthew G. Knepley ierr = MPI_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 792cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 793cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 794cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 795cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 796cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 797cc0c4584SMatthew G. Knepley } 798cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 799cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 800cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 801cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 802cc0c4584SMatthew G. Knepley } 80324cdb843SMatthew G. Knepley 80424cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 80524cdb843SMatthew G. Knepley 80624cdb843SMatthew G. Knepley #undef __FUNCT__ 8077d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 8087d4028c8SMatthew G. Knepley /*@ 8097d4028c8SMatthew G. Knepley DMPlexSNESGetGeometryFEM - Return precomputed geometric data 8107d4028c8SMatthew G. Knepley 8117d4028c8SMatthew G. Knepley Input Parameter: 8127d4028c8SMatthew G. Knepley . dm - The DM 8137d4028c8SMatthew G. Knepley 8147d4028c8SMatthew G. Knepley Output Parameters: 8157d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 8167d4028c8SMatthew G. Knepley 8177d4028c8SMatthew G. Knepley Level: developer 8187d4028c8SMatthew G. Knepley 8197d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal() 8207d4028c8SMatthew G. Knepley @*/ 8217d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 8227d4028c8SMatthew G. Knepley { 8237d4028c8SMatthew G. Knepley DMSNES dmsnes; 8247d4028c8SMatthew G. Knepley PetscObject obj; 8257d4028c8SMatthew G. Knepley PetscErrorCode ierr; 8267d4028c8SMatthew G. Knepley 8277d4028c8SMatthew G. Knepley PetscFunctionBegin; 8287d4028c8SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8297d4028c8SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 8307d4028c8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 8317d4028c8SMatthew G. Knepley if (!obj) { 8327d4028c8SMatthew G. Knepley Vec cellgeom; 8337d4028c8SMatthew G. Knepley 8347d4028c8SMatthew G. Knepley ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 8357d4028c8SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 8367d4028c8SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 8377d4028c8SMatthew G. Knepley } 8387d4028c8SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 8397d4028c8SMatthew G. Knepley PetscFunctionReturn(0); 8407d4028c8SMatthew G. Knepley } 8417d4028c8SMatthew G. Knepley 8427d4028c8SMatthew G. Knepley #undef __FUNCT__ 84308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM" 84408449791SMatthew G. Knepley /*@ 84508449791SMatthew G. Knepley DMPlexSNESGetGeometryFVM - Return precomputed geometric data 84608449791SMatthew G. Knepley 84708449791SMatthew G. Knepley Input Parameter: 84808449791SMatthew G. Knepley . dm - The DM 84908449791SMatthew G. Knepley 85008449791SMatthew G. Knepley Output Parameters: 85108449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry 85208449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 85308449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 85408449791SMatthew G. Knepley 85508449791SMatthew G. Knepley Level: developer 85608449791SMatthew G. Knepley 85708449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 85808449791SMatthew G. Knepley @*/ 85908449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 86024cdb843SMatthew G. Knepley { 86108449791SMatthew G. Knepley DMSNES dmsnes; 86208449791SMatthew G. Knepley PetscObject obj; 86324cdb843SMatthew G. Knepley PetscErrorCode ierr; 86424cdb843SMatthew G. Knepley 86524cdb843SMatthew G. Knepley PetscFunctionBegin; 86608449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 86708449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 86808449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr); 86908449791SMatthew G. Knepley if (!obj) { 87008449791SMatthew G. Knepley Vec cellgeom, facegeom; 87124cdb843SMatthew G. Knepley 87208449791SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr); 87308449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr); 87408449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr); 87508449791SMatthew G. Knepley ierr = VecDestroy(&facegeom);CHKERRQ(ierr); 87608449791SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 87708449791SMatthew G. Knepley } 87808449791SMatthew G. Knepley if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);} 87908449791SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);} 88008449791SMatthew G. Knepley if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);} 88124cdb843SMatthew G. Knepley PetscFunctionReturn(0); 88224cdb843SMatthew G. Knepley } 88324cdb843SMatthew G. Knepley 88424cdb843SMatthew G. Knepley #undef __FUNCT__ 88508449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM" 886dbd489d2SMatthew G. Knepley /*@ 88708449791SMatthew G. Knepley DMPlexSNESGetGradientDM - Return gradient data layout 88808449791SMatthew G. Knepley 88908449791SMatthew G. Knepley Input Parameters: 89008449791SMatthew G. Knepley + dm - The DM 89108449791SMatthew G. Knepley - fv - The PetscFV 89208449791SMatthew G. Knepley 89308449791SMatthew G. Knepley Output Parameter: 89408449791SMatthew G. Knepley . dmGrad - The layout for gradient values 89508449791SMatthew G. Knepley 89608449791SMatthew G. Knepley Level: developer 89708449791SMatthew G. Knepley 89808449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 89908449791SMatthew G. Knepley @*/ 90008449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 90124cdb843SMatthew G. Knepley { 90208449791SMatthew G. Knepley DMSNES dmsnes; 90308449791SMatthew G. Knepley PetscObject obj; 90408449791SMatthew G. Knepley PetscBool computeGradients; 90524cdb843SMatthew G. Knepley PetscErrorCode ierr; 90624cdb843SMatthew G. Knepley 90724cdb843SMatthew G. Knepley PetscFunctionBegin; 90808449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 90908449791SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 91008449791SMatthew G. Knepley PetscValidPointer(dmGrad,3); 91108449791SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 91208449791SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 91308449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 91408449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr); 91508449791SMatthew G. Knepley if (!obj) { 91608449791SMatthew G. Knepley DM dmGrad; 91708449791SMatthew G. Knepley Vec faceGeometry, cellGeometry; 91808449791SMatthew G. Knepley 91908449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr); 92008449791SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr); 92108449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr); 92208449791SMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 92308449791SMatthew G. Knepley } 92408449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr); 92508449791SMatthew G. Knepley PetscFunctionReturn(0); 92608449791SMatthew G. Knepley } 92708449791SMatthew G. Knepley 92808449791SMatthew G. Knepley #undef __FUNCT__ 92908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields" 93008449791SMatthew G. Knepley /*@C 93108449791SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 93208449791SMatthew G. Knepley 93308449791SMatthew G. Knepley Input Parameters: 93408449791SMatthew G. Knepley + dm - The DM 93508449791SMatthew G. Knepley . cStart - The first cell to include 93608449791SMatthew G. Knepley . cEnd - The first cell to exclude 93708449791SMatthew G. Knepley . locX - A local vector with the solution fields 93808449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 93908449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 94008449791SMatthew G. Knepley 94108449791SMatthew G. Knepley Output Parameters: 94208449791SMatthew G. Knepley + u - The field coefficients 94308449791SMatthew G. Knepley . u_t - The fields derivative coefficients 94408449791SMatthew G. Knepley - a - The auxiliary field coefficients 94508449791SMatthew G. Knepley 94608449791SMatthew G. Knepley Level: developer 94708449791SMatthew G. Knepley 94808449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 94908449791SMatthew G. Knepley @*/ 95008449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 95108449791SMatthew G. Knepley { 95208449791SMatthew G. Knepley DM dmAux; 95308449791SMatthew G. Knepley PetscSection section, sectionAux; 95408449791SMatthew G. Knepley PetscDS prob; 95508449791SMatthew G. Knepley PetscInt numCells = cEnd - cStart, totDim, totDimAux, c; 95608449791SMatthew G. Knepley PetscErrorCode ierr; 95708449791SMatthew G. Knepley 95808449791SMatthew G. Knepley PetscFunctionBegin; 95908449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 96008449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 96108449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 96208449791SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 96308449791SMatthew G. Knepley PetscValidPointer(u, 7); 96408449791SMatthew G. Knepley PetscValidPointer(u_t, 8); 96508449791SMatthew G. Knepley PetscValidPointer(a, 9); 96624cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 96724cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 96824cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 96908449791SMatthew G. Knepley if (locA) { 97008449791SMatthew G. Knepley PetscDS probAux; 97108449791SMatthew G. Knepley 97208449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 97324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 97424cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 97524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 97624cdb843SMatthew G. Knepley } 97708449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr); 97808449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, locX_t ? numCells*totDim : 0, PETSC_SCALAR, u_t);CHKERRQ(ierr); 97908449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, locA ? numCells*totDimAux : 0, PETSC_SCALAR, a);CHKERRQ(ierr); 98024cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 98108449791SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 98224cdb843SMatthew G. Knepley PetscInt i; 98324cdb843SMatthew G. Knepley 98408449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 98508449791SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[c*totDim+i] = x[i]; 98608449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 98708449791SMatthew G. Knepley if (locX_t) { 98808449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 98908449791SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[c*totDim+i] = x_t[i]; 99008449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 99124cdb843SMatthew G. Knepley } 99208449791SMatthew G. Knepley if (locA) { 99308449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 99408449791SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[c*totDimAux+i] = x[i]; 99508449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 99624cdb843SMatthew G. Knepley } 99724cdb843SMatthew G. Knepley } 99808449791SMatthew G. Knepley PetscFunctionReturn(0); 99908449791SMatthew G. Knepley } 100024cdb843SMatthew G. Knepley 100108449791SMatthew G. Knepley #undef __FUNCT__ 100208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields" 100308449791SMatthew G. Knepley /*@C 100408449791SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 100508449791SMatthew G. Knepley 100608449791SMatthew G. Knepley Input Parameters: 100708449791SMatthew G. Knepley + dm - The DM 100808449791SMatthew G. Knepley . cStart - The first cell to include 100908449791SMatthew G. Knepley . cEnd - The first cell to exclude 101008449791SMatthew G. Knepley . locX - A local vector with the solution fields 101108449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 101208449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 101308449791SMatthew G. Knepley 101408449791SMatthew G. Knepley Output Parameters: 101508449791SMatthew G. Knepley + u - The field coefficients 101608449791SMatthew G. Knepley . u_t - The fields derivative coefficients 101708449791SMatthew G. Knepley - a - The auxiliary field coefficients 101808449791SMatthew G. Knepley 101908449791SMatthew G. Knepley Level: developer 102008449791SMatthew G. Knepley 102108449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 102208449791SMatthew G. Knepley @*/ 102308449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 102408449791SMatthew G. Knepley { 102508449791SMatthew G. Knepley PetscErrorCode ierr; 102608449791SMatthew G. Knepley 102708449791SMatthew G. Knepley PetscFunctionBegin; 102808449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr); 102908449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr); 103008449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr); 103108449791SMatthew G. Knepley PetscFunctionReturn(0); 103224cdb843SMatthew G. Knepley } 103308449791SMatthew G. Knepley 103408449791SMatthew G. Knepley #undef __FUNCT__ 103508449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields" 103608449791SMatthew G. Knepley /*@C 103708449791SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 103808449791SMatthew G. Knepley 103908449791SMatthew G. Knepley Input Parameters: 104008449791SMatthew G. Knepley + dm - The DM 104108449791SMatthew G. Knepley . fStart - The first face to include 104208449791SMatthew G. Knepley . fEnd - The first face to exclude 104308449791SMatthew G. Knepley . locX - A local vector with the solution fields 104408449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 104508449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 104608449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 104708449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 104808449791SMatthew G. Knepley 104908449791SMatthew G. Knepley Output Parameters: 1050*477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1051*477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 105208449791SMatthew G. Knepley 105308449791SMatthew G. Knepley Level: developer 105408449791SMatthew G. Knepley 105508449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 105608449791SMatthew G. Knepley @*/ 105708449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 105808449791SMatthew G. Knepley { 105908449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 106008449791SMatthew G. Knepley PetscDS prob; 106108449791SMatthew G. Knepley DMLabel ghostLabel; 106208449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1063*477f7dfdSMatthew G. Knepley PetscBool *isFE; 1064*477f7dfdSMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 106508449791SMatthew G. Knepley PetscErrorCode ierr; 106608449791SMatthew G. Knepley 106708449791SMatthew G. Knepley PetscFunctionBegin; 106808449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 106908449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 107008449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 107108449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 107208449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 107308449791SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 107408449791SMatthew G. Knepley PetscValidPointer(uL, 9); 107508449791SMatthew G. Knepley PetscValidPointer(uR, 10); 107608449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 107708449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1078*477f7dfdSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1079*477f7dfdSMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1080*477f7dfdSMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1081*477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1082*477f7dfdSMatthew G. Knepley PetscObject obj; 1083*477f7dfdSMatthew G. Knepley PetscClassId id; 1084*477f7dfdSMatthew G. Knepley 1085*477f7dfdSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 1086*477f7dfdSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1087*477f7dfdSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1088*477f7dfdSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1089*477f7dfdSMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 1090*477f7dfdSMatthew G. Knepley } 109108449791SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 109208449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 109308449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 109408449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 109508449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 109608449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 109708449791SMatthew G. Knepley if (locGrad) { 109808449791SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 109908449791SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 110024cdb843SMatthew G. Knepley } 1101*477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr); 1102*477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr); 1103*477f7dfdSMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 110408449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 110508449791SMatthew G. Knepley const PetscInt *cells; 110608449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 110708449791SMatthew G. Knepley const PetscFVCellGeom *cgL, *cgR; 110808449791SMatthew G. Knepley const PetscScalar *xL, *xR, *gL, *gR; 110908449791SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 1110*477f7dfdSMatthew G. Knepley PetscInt ghost; 111108449791SMatthew G. Knepley 111208449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 111308449791SMatthew G. Knepley if (ghost >= 0) continue; 111408449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 111508449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 111608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 111708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1118*477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1119*477f7dfdSMatthew G. Knepley PetscInt off; 1120*477f7dfdSMatthew G. Knepley 1121*477f7dfdSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &off);CHKERRQ(ierr); 1122*477f7dfdSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1123*477f7dfdSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 1124*477f7dfdSMatthew G. Knepley if (isFE[f]) { 1125*477f7dfdSMatthew G. Knepley const PetscInt *cone; 1126*477f7dfdSMatthew G. Knepley PetscInt coneSize, faceLocL, faceLocR; 1127*477f7dfdSMatthew G. Knepley 1128*477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 1129*477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr); 1130*477f7dfdSMatthew G. Knepley for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break; 1131*477f7dfdSMatthew G. Knepley if (faceLocL == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[0]); 1132*477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 1133*477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr); 1134*477f7dfdSMatthew G. Knepley for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break; 1135*477f7dfdSMatthew G. Knepley if (faceLocR == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[1]); 1136*477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 1137*477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr); 1138*477f7dfdSMatthew G. Knepley } else { 1139*477f7dfdSMatthew G. Knepley PetscFV fv; 1140*477f7dfdSMatthew G. Knepley PetscInt numComp, c; 1141*477f7dfdSMatthew G. Knepley 1142*477f7dfdSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1143*477f7dfdSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 114408449791SMatthew G. Knepley if (dmGrad) { 114508449791SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 114608449791SMatthew G. Knepley 114708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 114808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 114908449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 115008449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1151*477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1152*477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1153*477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 115408449791SMatthew G. Knepley } 115508449791SMatthew G. Knepley } else { 1156*477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1157*477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 1158*477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 1159*477f7dfdSMatthew G. Knepley } 1160*477f7dfdSMatthew G. Knepley } 116108449791SMatthew G. Knepley } 116208449791SMatthew G. Knepley } 116308449791SMatthew G. Knepley ++iface; 116408449791SMatthew G. Knepley } 116508449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 116608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 116708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 116808449791SMatthew G. Knepley if (locGrad) { 116908449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 117008449791SMatthew G. Knepley } 1171*477f7dfdSMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 117208449791SMatthew G. Knepley PetscFunctionReturn(0); 117308449791SMatthew G. Knepley } 117408449791SMatthew G. Knepley 117508449791SMatthew G. Knepley #undef __FUNCT__ 117608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields" 117708449791SMatthew G. Knepley /*@C 117808449791SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 117908449791SMatthew G. Knepley 118008449791SMatthew G. Knepley Input Parameters: 118108449791SMatthew G. Knepley + dm - The DM 118208449791SMatthew G. Knepley . fStart - The first face to include 118308449791SMatthew G. Knepley . fEnd - The first face to exclude 118408449791SMatthew G. Knepley . locX - A local vector with the solution fields 118508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 118608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 118708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 118808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 118908449791SMatthew G. Knepley 119008449791SMatthew G. Knepley Output Parameters: 1191*477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1192*477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 119308449791SMatthew G. Knepley 119408449791SMatthew G. Knepley Level: developer 119508449791SMatthew G. Knepley 119608449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 119708449791SMatthew G. Knepley @*/ 119808449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 119908449791SMatthew G. Knepley { 120008449791SMatthew G. Knepley PetscErrorCode ierr; 120108449791SMatthew G. Knepley 120208449791SMatthew G. Knepley PetscFunctionBegin; 120308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr); 120408449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr); 120508449791SMatthew G. Knepley PetscFunctionReturn(0); 120608449791SMatthew G. Knepley } 120708449791SMatthew G. Knepley 120808449791SMatthew G. Knepley #undef __FUNCT__ 120908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry" 121008449791SMatthew G. Knepley /*@C 121108449791SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 121208449791SMatthew G. Knepley 121308449791SMatthew G. Knepley Input Parameters: 121408449791SMatthew G. Knepley + dm - The DM 121508449791SMatthew G. Knepley . fStart - The first face to include 121608449791SMatthew G. Knepley . fEnd - The first face to exclude 121708449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 121808449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 121908449791SMatthew G. Knepley 122008449791SMatthew G. Knepley Output Parameters: 122108449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 122208449791SMatthew G. Knepley - vol - The cell volume 122308449791SMatthew G. Knepley 122408449791SMatthew G. Knepley Level: developer 122508449791SMatthew G. Knepley 122608449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 122708449791SMatthew G. Knepley @*/ 122808449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 122908449791SMatthew G. Knepley { 123008449791SMatthew G. Knepley DM dmFace, dmCell; 123108449791SMatthew G. Knepley DMLabel ghostLabel; 123208449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 123308449791SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 123408449791SMatthew G. Knepley PetscErrorCode ierr; 123508449791SMatthew G. Knepley 123608449791SMatthew G. Knepley PetscFunctionBegin; 123708449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 123808449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 123908449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 124008449791SMatthew G. Knepley PetscValidPointer(fgeom, 6); 124108449791SMatthew G. Knepley PetscValidPointer(vol, 7); 124208449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 124308449791SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 124408449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 124508449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 124608449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 124708449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 124808449791SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 124908449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr); 125008449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 125108449791SMatthew G. Knepley const PetscInt *cells; 125208449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 125308449791SMatthew G. Knepley const PetscFVCellGeom *cgL, *cgR; 125408449791SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 12552eefff9cSMatthew G. Knepley PetscReal *voll = *vol; 125608449791SMatthew G. Knepley PetscInt ghost, d; 125708449791SMatthew G. Knepley 125808449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 125908449791SMatthew G. Knepley if (ghost >= 0) continue; 126008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 126108449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 126208449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 126308449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 126408449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 126508449791SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 126608449791SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 126708449791SMatthew G. Knepley } 126808449791SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 126908449791SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 127008449791SMatthew G. Knepley ++iface; 127108449791SMatthew G. Knepley } 127208449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 127308449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 127408449791SMatthew G. Knepley PetscFunctionReturn(0); 127508449791SMatthew G. Knepley } 127608449791SMatthew G. Knepley 127708449791SMatthew G. Knepley #undef __FUNCT__ 127808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry" 127908449791SMatthew G. Knepley /*@C 128008449791SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 128108449791SMatthew G. Knepley 128208449791SMatthew G. Knepley Input Parameters: 128308449791SMatthew G. Knepley + dm - The DM 128408449791SMatthew G. Knepley . fStart - The first face to include 128508449791SMatthew G. Knepley . fEnd - The first face to exclude 128608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 128708449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 128808449791SMatthew G. Knepley 128908449791SMatthew G. Knepley Output Parameters: 129008449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 129108449791SMatthew G. Knepley - vol - The cell volume 129208449791SMatthew G. Knepley 129308449791SMatthew G. Knepley Level: developer 129408449791SMatthew G. Knepley 129508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 129608449791SMatthew G. Knepley @*/ 129708449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 129808449791SMatthew G. Knepley { 129908449791SMatthew G. Knepley PetscErrorCode ierr; 130008449791SMatthew G. Knepley 130108449791SMatthew G. Knepley PetscFunctionBegin; 130208449791SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 130308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr); 130408449791SMatthew G. Knepley PetscFunctionReturn(0); 130508449791SMatthew G. Knepley } 130608449791SMatthew G. Knepley 130708449791SMatthew G. Knepley #undef __FUNCT__ 130808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal" 130908449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad) 131008449791SMatthew G. Knepley { 131108449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 131208449791SMatthew G. Knepley DMLabel ghostLabel; 131308449791SMatthew G. Knepley PetscDS prob; 131408449791SMatthew G. Knepley PetscFV fvm; 131508449791SMatthew G. Knepley PetscLimiter lim; 131608449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x; 131708449791SMatthew G. Knepley PetscScalar *gr; 131808449791SMatthew G. Knepley PetscReal *cellPhi; 131908449791SMatthew G. Knepley PetscInt dim, face, cell, totDim, cStart, cEnd, cEndInterior; 132008449791SMatthew G. Knepley PetscErrorCode ierr; 132108449791SMatthew G. Knepley 132208449791SMatthew G. Knepley PetscFunctionBegin; 132308449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 132408449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 132508449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 132608449791SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 132708449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr); 132808449791SMatthew G. Knepley ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr); 132908449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 133008449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 133108449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 133208449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 133308449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 133408449791SMatthew G. Knepley ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr); 133508449791SMatthew G. Knepley ierr = VecZeroEntries(grad);CHKERRQ(ierr); 133608449791SMatthew G. Knepley ierr = VecGetArray(grad, &gr);CHKERRQ(ierr); 133708449791SMatthew G. Knepley /* Reconstruct gradients */ 133808449791SMatthew G. Knepley for (face = fStart; face < fEnd; ++face) { 133908449791SMatthew G. Knepley const PetscInt *cells; 134008449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 134108449791SMatthew G. Knepley const PetscScalar *cx[2]; 134208449791SMatthew G. Knepley PetscScalar *cgrad[2]; 134308449791SMatthew G. Knepley PetscBool boundary; 134408449791SMatthew G. Knepley PetscInt ghost, c, pd, d; 134508449791SMatthew G. Knepley 134608449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 134708449791SMatthew G. Knepley if (ghost >= 0) continue; 134808449791SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr); 134908449791SMatthew G. Knepley if (boundary) continue; 135008449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 135108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 135208449791SMatthew G. Knepley for (c = 0; c < 2; ++c) { 135308449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr); 135408449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr); 135508449791SMatthew G. Knepley } 135608449791SMatthew G. Knepley for (pd = 0; pd < totDim; ++pd) { 135708449791SMatthew G. Knepley PetscScalar delta = cx[1][pd] - cx[0][pd]; 135808449791SMatthew G. Knepley 135908449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 136008449791SMatthew G. Knepley if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta; 136108449791SMatthew G. Knepley if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta; 136208449791SMatthew G. Knepley } 136308449791SMatthew G. Knepley } 136408449791SMatthew G. Knepley } 136508449791SMatthew G. Knepley /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */ 136608449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 136708449791SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 136808449791SMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 136908449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 137008449791SMatthew G. Knepley for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) { 137108449791SMatthew G. Knepley const PetscInt *faces; 137208449791SMatthew G. Knepley const PetscScalar *cx; 137308449791SMatthew G. Knepley const PetscFVCellGeom *cg; 137408449791SMatthew G. Knepley PetscScalar *cgrad; 137508449791SMatthew G. Knepley PetscInt coneSize, f, pd, d; 137608449791SMatthew G. Knepley 137708449791SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 137808449791SMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 137908449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr); 138008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr); 138108449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr); 138208449791SMatthew G. Knepley if (!cgrad) continue; /* Unowned overlap cell, we do not compute */ 138308449791SMatthew G. Knepley /* Limiter will be minimum value over all neighbors */ 138408449791SMatthew G. Knepley for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL; 138508449791SMatthew G. Knepley for (f = 0; f < coneSize; ++f) { 138608449791SMatthew G. Knepley const PetscScalar *ncx; 138708449791SMatthew G. Knepley const PetscFVCellGeom *ncg; 138808449791SMatthew G. Knepley const PetscInt *fcells; 138908449791SMatthew G. Knepley PetscInt face = faces[f], ncell, ghost; 139008449791SMatthew G. Knepley PetscReal v[3]; 139108449791SMatthew G. Knepley PetscBool boundary; 139208449791SMatthew G. Knepley 139308449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 139408449791SMatthew G. Knepley ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr); 139508449791SMatthew G. Knepley if ((ghost >= 0) || boundary) continue; 139608449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr); 139708449791SMatthew G. Knepley ncell = cell == fcells[0] ? fcells[1] : fcells[0]; 139808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr); 139908449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr); 140008449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v); 140108449791SMatthew G. Knepley for (d = 0; d < totDim; ++d) { 140208449791SMatthew G. Knepley /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */ 140308449791SMatthew G. Knepley PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v); 140408449791SMatthew G. Knepley 140508449791SMatthew G. Knepley ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr); 140608449791SMatthew G. Knepley cellPhi[d] = PetscMin(cellPhi[d], phi); 140708449791SMatthew G. Knepley } 140808449791SMatthew G. Knepley } 140908449791SMatthew G. Knepley /* Apply limiter to gradient */ 141008449791SMatthew G. Knepley for (pd = 0; pd < totDim; ++pd) 141108449791SMatthew G. Knepley /* Scalar limiter applied to each component separately */ 141208449791SMatthew G. Knepley for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd]; 141308449791SMatthew G. Knepley } 141408449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 141508449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 141608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 141708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 141808449791SMatthew G. Knepley ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr); 141908449791SMatthew G. Knepley PetscFunctionReturn(0); 142008449791SMatthew G. Knepley } 142108449791SMatthew G. Knepley 142208449791SMatthew G. Knepley #undef __FUNCT__ 142308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal" 142408449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user) 142508449791SMatthew G. Knepley { 142608449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 142708449791SMatthew G. Knepley PetscSection section; 142808449791SMatthew G. Knepley PetscDS prob; 142908449791SMatthew G. Knepley DMLabel depth; 143008449791SMatthew G. Knepley PetscFECellGeom *cgeom; 143108449791SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *elemVec = NULL; 143208449791SMatthew G. Knepley PetscInt dim, Nf, f, totDimBd, numBd, bd; 143308449791SMatthew G. Knepley PetscErrorCode ierr; 143408449791SMatthew G. Knepley 143508449791SMatthew G. Knepley PetscFunctionBegin; 143608449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 143708449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 143808449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 143908449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 144008449791SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 144124cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 144224cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 144324cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 144424cdb843SMatthew G. Knepley const char *bdLabel; 144524cdb843SMatthew G. Knepley DMLabel label; 144624cdb843SMatthew G. Knepley IS pointIS; 144724cdb843SMatthew G. Knepley const PetscInt *points; 144824cdb843SMatthew G. Knepley const PetscInt *values; 144924cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 145024cdb843SMatthew G. Knepley PetscBool isEssential; 145124cdb843SMatthew G. Knepley 145224cdb843SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 145324cdb843SMatthew G. Knepley if (isEssential) continue; 145424cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 145524cdb843SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 145624cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 145724cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 145824cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 145924cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 146024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 146124cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 146224cdb843SMatthew G. Knepley } 1463bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 146408449791SMatthew G. Knepley if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 146524cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 146624cdb843SMatthew G. Knepley const PetscInt point = points[p]; 146724cdb843SMatthew G. Knepley PetscScalar *x = NULL; 146824cdb843SMatthew G. Knepley PetscInt i; 146924cdb843SMatthew G. Knepley 147024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 147124cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1472bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1473bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n); 1474bbce034cSMatthew G. Knepley if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point); 147508449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 147624cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 147708449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 147808449791SMatthew G. Knepley if (locX_t) { 147908449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 148024cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 148108449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 148224cdb843SMatthew G. Knepley } 148324cdb843SMatthew G. Knepley ++f; 148424cdb843SMatthew G. Knepley } 148524cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 148624cdb843SMatthew G. Knepley PetscFE fe; 148708449791SMatthew G. Knepley PetscQuadrature q; 148824cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 148924cdb843SMatthew G. Knepley /* Conforming batches */ 149024cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 149124cdb843SMatthew G. Knepley /* Remainder */ 149224cdb843SMatthew G. Knepley PetscInt Nr, offset; 149324cdb843SMatthew G. Knepley 149424cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 149524cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 149624cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 149724cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 149824cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 149924cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 150024cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 150124cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 150224cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 150324cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 150424cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 150524cdb843SMatthew G. Knepley offset = numFaces - Nr; 1506bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr); 1507bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr); 150824cdb843SMatthew G. Knepley } 150924cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 151024cdb843SMatthew G. Knepley const PetscInt point = points[p]; 151124cdb843SMatthew G. Knepley 151224cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 151324cdb843SMatthew G. Knepley if (dep != dim-1) continue; 151424cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 151508449791SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr); 151624cdb843SMatthew G. Knepley ++f; 151724cdb843SMatthew G. Knepley } 151824cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 151924cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1520bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 152108449791SMatthew G. Knepley if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 152224cdb843SMatthew G. Knepley } 152308449791SMatthew G. Knepley PetscFunctionReturn(0); 152408449791SMatthew G. Knepley } 152508449791SMatthew G. Knepley 152608449791SMatthew G. Knepley #undef __FUNCT__ 152708449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal" 152808449791SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user) 152908449791SMatthew G. Knepley { 153008449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 153108449791SMatthew G. Knepley const char *name = "Residual"; 153208449791SMatthew G. Knepley DM dmAux = NULL; 153308449791SMatthew G. Knepley DM dmGrad = NULL; 153408449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 153508449791SMatthew G. Knepley PetscDS prob = NULL; 153608449791SMatthew G. Knepley PetscDS probAux = NULL; 153708449791SMatthew G. Knepley PetscSection section = NULL; 153808449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 153908449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 154008449791SMatthew G. Knepley PetscFV fvm = NULL; 154108449791SMatthew G. Knepley PetscFECellGeom *cgeomFEM = NULL; 154208449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 154308449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 154408449791SMatthew G. Knepley Vec locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 154508449791SMatthew G. Knepley PetscScalar *u, *u_t, *a, *uL, *uR; 15469ac3fadcSMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, cStart, cEnd, cEndInterior, fStart, fEnd; 154708449791SMatthew G. Knepley PetscErrorCode ierr; 154808449791SMatthew G. Knepley 154908449791SMatthew G. Knepley PetscFunctionBegin; 155008449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 155108449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 155208449791SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the preclac functions return exactly what we need */ 155308449791SMatthew G. Knepley /* FEM+FVM */ 155408449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 155508449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 155608449791SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 155708449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 155808449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 155908449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 156008449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 156108449791SMatthew G. Knepley if (locA) { 156208449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 156308449791SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 156408449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 156508449791SMatthew G. Knepley } 156608449791SMatthew G. Knepley /* 2: Get geometric data */ 156708449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 156808449791SMatthew G. Knepley PetscObject obj; 156908449791SMatthew G. Knepley PetscClassId id; 157008449791SMatthew G. Knepley 157108449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 157208449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 157308449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 157408449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 157508449791SMatthew G. Knepley } 157608449791SMatthew G. Knepley if (useFEM) { 157708449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr); 157808449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFEM, (const PetscScalar **) &cgeomFEM);CHKERRQ(ierr); 157908449791SMatthew G. Knepley } 158008449791SMatthew G. Knepley if (useFVM) { 158108449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 158208449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 158308449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 158408449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 158508449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 158608449791SMatthew G. Knepley if (dmGrad) { 158708449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 158808449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 158908449791SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 159008449791SMatthew G. Knepley /* Communicate gradient values */ 159108449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 159208449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 159308449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 159408449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 159508449791SMatthew G. Knepley } 159608449791SMatthew G. Knepley } 159708449791SMatthew G. Knepley /* Handle boundary values */ 159808449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 159908449791SMatthew G. Knepley /* Loop over chunks */ 160008449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 16019ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 16029ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 160308449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 160408449791SMatthew G. Knepley numChunks = 1; 160508449791SMatthew G. Knepley cellChunkSize = (cEnd - cStart)/numChunks; 160608449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 160708449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 16082eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 16092eefff9cSMatthew G. Knepley PetscReal *vol; 161008449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 161108449791SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell; 161208449791SMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face; 161308449791SMatthew G. Knepley 161408449791SMatthew G. Knepley /* Extract field coefficients */ 161508449791SMatthew G. Knepley if (useFEM) { 161608449791SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 161708449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1618215c4595SMatthew G. Knepley ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 161908449791SMatthew G. Knepley } 162008449791SMatthew G. Knepley if (useFVM) { 162108449791SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 162208449791SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 162308449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 162408449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1625215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1626215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 162708449791SMatthew G. Knepley } 162808449791SMatthew G. Knepley /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 162908449791SMatthew G. Knepley /* Loop over fields */ 163008449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 163108449791SMatthew G. Knepley PetscObject obj; 163208449791SMatthew G. Knepley PetscClassId id; 163308449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 163408449791SMatthew G. Knepley 163508449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 163608449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 163708449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 163808449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 163908449791SMatthew G. Knepley PetscQuadrature q; 164008449791SMatthew G. Knepley PetscInt Nq, Nb; 164108449791SMatthew G. Knepley 164208449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 164308449791SMatthew G. Knepley 164408449791SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 164508449791SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 164608449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 164708449791SMatthew G. Knepley blockSize = Nb*Nq; 164808449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 164908449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 165008449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 165108449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 165208449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 165308449791SMatthew G. Knepley offset = numCells - Nr; 165408449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 165508449791SMatthew G. Knepley /* 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) */ 165608449791SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 165708449791SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 165808449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 165908449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 166008449791SMatthew G. Knepley 166108449791SMatthew G. Knepley Ne = numFaces; 166208449791SMatthew G. Knepley Nr = 0; 166308449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 166408449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 166508449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 166608449791SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 166708449791SMatthew G. Knepley } 166808449791SMatthew G. Knepley /* Loop over domain */ 166908449791SMatthew G. Knepley if (useFEM) { 167008449791SMatthew G. Knepley /* Add elemVec to locX */ 167108449791SMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 167208449791SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);} 167308449791SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_VALUES);CHKERRQ(ierr); 167408449791SMatthew G. Knepley } 167508449791SMatthew G. Knepley } 167608449791SMatthew G. Knepley if (useFVM) { 167708449791SMatthew G. Knepley PetscScalar *fa; 167808449791SMatthew G. Knepley PetscInt iface; 167908449791SMatthew G. Knepley 168008449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 168108449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 168208449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 168308449791SMatthew G. Knepley const PetscInt *cells; 168408449791SMatthew G. Knepley PetscScalar *fL, *fR; 168508449791SMatthew G. Knepley PetscInt ghost, d; 168608449791SMatthew G. Knepley 168708449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 168808449791SMatthew G. Knepley if (ghost >= 0) continue; 168908449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 169008449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dm, cells[0], fa, &fL);CHKERRQ(ierr); 169108449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dm, cells[1], fa, &fR);CHKERRQ(ierr); 169208449791SMatthew G. Knepley for (d = 0; d < totDim; ++d) { 169308449791SMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+d]; 169408449791SMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+d]; 169508449791SMatthew G. Knepley } 169608449791SMatthew G. Knepley ++iface; 169708449791SMatthew G. Knepley } 169808449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 169908449791SMatthew G. Knepley } 170008449791SMatthew G. Knepley if (useFEM) { 170108449791SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 170208449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 170308449791SMatthew G. Knepley } 170408449791SMatthew G. Knepley if (useFVM) { 170508449791SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 170608449791SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 170708449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 170808449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 170908449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 171008449791SMatthew G. Knepley } 171108449791SMatthew G. Knepley } 171208449791SMatthew G. Knepley 171308449791SMatthew G. Knepley if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);} 171408449791SMatthew G. Knepley 171508449791SMatthew G. Knepley /* FEM */ 171608449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 171708449791SMatthew G. Knepley /* 2: Get geometric data */ 171808449791SMatthew G. Knepley /* 3: Handle boundary values */ 171908449791SMatthew G. Knepley /* 4: Loop over domain */ 172008449791SMatthew G. Knepley /* Extract coefficients */ 172108449791SMatthew G. Knepley /* Loop over fields */ 172208449791SMatthew G. Knepley /* Set tiling for FE*/ 172308449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 172408449791SMatthew G. Knepley /* Loop over subdomain */ 172508449791SMatthew G. Knepley /* Loop over quad points */ 172608449791SMatthew G. Knepley /* Transform coords to real space */ 172708449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 172808449791SMatthew G. Knepley /* Evaluate residual at point */ 172908449791SMatthew G. Knepley /* Transform residual to real space */ 173008449791SMatthew G. Knepley /* Add residual to elemVec */ 173108449791SMatthew G. Knepley /* Loop over domain */ 173208449791SMatthew G. Knepley /* Add elemVec to locX */ 173308449791SMatthew G. Knepley 173408449791SMatthew G. Knepley /* FVM */ 173508449791SMatthew G. Knepley /* Get geometric data */ 173608449791SMatthew G. Knepley /* If using gradients */ 173708449791SMatthew G. Knepley /* Compute gradient data */ 173808449791SMatthew G. Knepley /* Loop over domain faces */ 173908449791SMatthew G. Knepley /* Count computational faces */ 174008449791SMatthew G. Knepley /* Reconstruct cell gradient */ 174108449791SMatthew G. Knepley /* Loop over domain cells */ 174208449791SMatthew G. Knepley /* Limit cell gradients */ 174308449791SMatthew G. Knepley /* Handle boundary values */ 174408449791SMatthew G. Knepley /* Loop over domain faces */ 174508449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 174608449791SMatthew G. Knepley /* Riemann solve over faces */ 174708449791SMatthew G. Knepley /* Loop over domain faces */ 174808449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 174908449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 175008449791SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, locF);CHKERRQ(ierr);} 175124cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 175224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 175324cdb843SMatthew G. Knepley } 175424cdb843SMatthew G. Knepley 175524cdb843SMatthew G. Knepley #undef __FUNCT__ 175624cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 175724cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 175824cdb843SMatthew G. Knepley { 175924cdb843SMatthew G. Knepley DM dmCh, dmAux; 1760bbce034cSMatthew G. Knepley Vec A, cellgeom; 176124cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 176224cdb843SMatthew G. Knepley PetscQuadrature q; 176324cdb843SMatthew G. Knepley PetscSection section, sectionAux; 1764bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 176524cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 176624cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 176724cdb843SMatthew G. Knepley PetscInt totDim, totDimAux, diffCell = 0; 176824cdb843SMatthew G. Knepley PetscErrorCode ierr; 176924cdb843SMatthew G. Knepley 177024cdb843SMatthew G. Knepley PetscFunctionBegin; 177124cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 177224cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 177324cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 177424cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 177524cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 177624cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 177724cdb843SMatthew G. Knepley numCells = cEnd - cStart; 177824cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 177924cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 178024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 178124cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 178224cdb843SMatthew G. Knepley if (dmAux) { 178324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 178424cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 178524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 178624cdb843SMatthew G. Knepley } 178708449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 178824cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1789bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 179024cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 179124cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1792bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 1793bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 179424cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 179524cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 179624cdb843SMatthew G. Knepley PetscInt i; 179724cdb843SMatthew G. Knepley 179824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 179924cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 180024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 180124cdb843SMatthew G. Knepley if (X_t) { 180224cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 180324cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 180424cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 180524cdb843SMatthew G. Knepley } 180624cdb843SMatthew G. Knepley if (dmAux) { 180724cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 180824cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 180924cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 181024cdb843SMatthew G. Knepley } 181124cdb843SMatthew G. Knepley } 181224cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 181324cdb843SMatthew G. Knepley PetscFE fe, feCh; 181424cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 181524cdb843SMatthew G. Knepley /* Conforming batches */ 181624cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 181724cdb843SMatthew G. Knepley /* Remainder */ 181824cdb843SMatthew G. Knepley PetscInt Nr, offset; 181924cdb843SMatthew G. Knepley 182024cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 182124cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 182224cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 182324cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 182424cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 182524cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 182624cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 182724cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 182824cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 182924cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 183024cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 183124cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 183224cdb843SMatthew G. Knepley offset = numCells - Nr; 1833bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 1834bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr); 1835bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 1836bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr); 183724cdb843SMatthew G. Knepley } 183824cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 183924cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 184024cdb843SMatthew G. Knepley PetscInt d; 184124cdb843SMatthew G. Knepley 184224cdb843SMatthew G. Knepley for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;} 184324cdb843SMatthew G. Knepley if (diff) { 184424cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 184524cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 184624cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 184724cdb843SMatthew G. Knepley ++diffCell; 184824cdb843SMatthew G. Knepley } 184924cdb843SMatthew G. Knepley if (diffCell > 9) break; 185024cdb843SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 185124cdb843SMatthew G. Knepley } 1852bbce034cSMatthew G. Knepley ierr = VecRestoreArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 1853bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 185424cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 185524cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 185624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 185724cdb843SMatthew G. Knepley } 185824cdb843SMatthew G. Knepley 185924cdb843SMatthew G. Knepley #undef __FUNCT__ 186024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 186124cdb843SMatthew G. Knepley /*@ 186224cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 186324cdb843SMatthew G. Knepley 186424cdb843SMatthew G. Knepley Input Parameters: 186524cdb843SMatthew G. Knepley + dm - The mesh 186624cdb843SMatthew G. Knepley . X - Local solution 186724cdb843SMatthew G. Knepley - user - The user context 186824cdb843SMatthew G. Knepley 186924cdb843SMatthew G. Knepley Output Parameter: 187024cdb843SMatthew G. Knepley . F - Local output vector 187124cdb843SMatthew G. Knepley 187224cdb843SMatthew G. Knepley Level: developer 187324cdb843SMatthew G. Knepley 187424cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 187524cdb843SMatthew G. Knepley @*/ 187624cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 187724cdb843SMatthew G. Knepley { 187824cdb843SMatthew G. Knepley PetscObject check; 187924cdb843SMatthew G. Knepley PetscErrorCode ierr; 188024cdb843SMatthew G. Knepley 188124cdb843SMatthew G. Knepley PetscFunctionBegin; 188224cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 188324cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr); 188424cdb843SMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);} 188508449791SMatthew G. Knepley else {ierr = DMPlexComputeResidual_Internal(dm, 0.0, X, NULL, F, user);CHKERRQ(ierr);} 188624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 188724cdb843SMatthew G. Knepley } 188824cdb843SMatthew G. Knepley 188924cdb843SMatthew G. Knepley #undef __FUNCT__ 189024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal" 189124cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 189224cdb843SMatthew G. Knepley { 189324cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 189424cdb843SMatthew G. Knepley const char *name = "Jacobian"; 189524cdb843SMatthew G. Knepley DM dmAux; 189624cdb843SMatthew G. Knepley DMLabel depth; 1897bbce034cSMatthew G. Knepley Vec A, cellgeom; 189824cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 189924cdb843SMatthew G. Knepley PetscQuadrature quad; 190024cdb843SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 1901bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 190224cdb843SMatthew G. Knepley PetscScalar *elemMat, *u, *u_t, *a = NULL; 190324cdb843SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c; 190424cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 190524cdb843SMatthew G. Knepley PetscBool isShell; 190624cdb843SMatthew G. Knepley PetscErrorCode ierr; 190724cdb843SMatthew G. Knepley 190824cdb843SMatthew G. Knepley PetscFunctionBegin; 190924cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 191024cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 191124cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 191224cdb843SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 191324cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 191424cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 191524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 191624cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 191724cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 191824cdb843SMatthew G. Knepley numCells = cEnd - cStart; 191924cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 192024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 192124cdb843SMatthew G. Knepley if (dmAux) { 192224cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 192324cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 192424cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 192524cdb843SMatthew G. Knepley } 192608449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 192724cdb843SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 1928bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr); 192924cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1930bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 1931bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 193224cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 193324cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 193424cdb843SMatthew G. Knepley PetscInt i; 193524cdb843SMatthew G. Knepley 193624cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 193724cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 193824cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 193924cdb843SMatthew G. Knepley if (X_t) { 194024cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 194124cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 194224cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 194324cdb843SMatthew G. Knepley } 194424cdb843SMatthew G. Knepley if (dmAux) { 194524cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 194624cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 194724cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 194824cdb843SMatthew G. Knepley } 194924cdb843SMatthew G. Knepley } 195024cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 195124cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 195224cdb843SMatthew G. Knepley PetscFE fe; 195324cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 195424cdb843SMatthew G. Knepley /* Conforming batches */ 195524cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 195624cdb843SMatthew G. Knepley /* Remainder */ 195724cdb843SMatthew G. Knepley PetscInt Nr, offset; 195824cdb843SMatthew G. Knepley 195924cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 196024cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 196124cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 196224cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 196324cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 196424cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 196524cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 196624cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 196724cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 196824cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 196924cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 197024cdb843SMatthew G. Knepley offset = numCells - Nr; 197124cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 1972bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr); 1973bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 197424cdb843SMatthew G. Knepley } 197524cdb843SMatthew G. Knepley } 197624cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 197724cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);} 197824cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 197924cdb843SMatthew G. Knepley } 1980bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 1981bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr); 198224cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 198324cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 198424cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 198524cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 198624cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 198724cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 198824cdb843SMatthew G. Knepley const char *bdLabel; 198924cdb843SMatthew G. Knepley DMLabel label; 199024cdb843SMatthew G. Knepley IS pointIS; 199124cdb843SMatthew G. Knepley const PetscInt *points; 199224cdb843SMatthew G. Knepley const PetscInt *values; 199324cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 199424cdb843SMatthew G. Knepley PetscBool isEssential; 199524cdb843SMatthew G. Knepley 199624cdb843SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 199724cdb843SMatthew G. Knepley if (isEssential) continue; 199824cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 199924cdb843SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 200024cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 200124cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 200224cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 200324cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 200424cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 200524cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 200624cdb843SMatthew G. Knepley } 2007bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 200824cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 200924cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 201024cdb843SMatthew G. Knepley const PetscInt point = points[p]; 201124cdb843SMatthew G. Knepley PetscScalar *x = NULL; 201224cdb843SMatthew G. Knepley PetscInt i; 201324cdb843SMatthew G. Knepley 201424cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 201524cdb843SMatthew G. Knepley if (dep != dim-1) continue; 2016bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 2017bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n); 2018bbce034cSMatthew G. Knepley if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point); 201924cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 202024cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 202124cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 202224cdb843SMatthew G. Knepley if (X_t) { 202324cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 202424cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 202524cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 202624cdb843SMatthew G. Knepley } 202724cdb843SMatthew G. Knepley ++f; 202824cdb843SMatthew G. Knepley } 202924cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 203024cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 203124cdb843SMatthew G. Knepley PetscFE fe; 203224cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 203324cdb843SMatthew G. Knepley /* Conforming batches */ 203424cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 203524cdb843SMatthew G. Knepley /* Remainder */ 203624cdb843SMatthew G. Knepley PetscInt Nr, offset; 203724cdb843SMatthew G. Knepley 203824cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 203924cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 204024cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 204124cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 204224cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 204324cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 204424cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 204524cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 204624cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 204724cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 204824cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 204924cdb843SMatthew G. Knepley offset = numFaces - Nr; 205024cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2051bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr); 2052bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr); 205324cdb843SMatthew G. Knepley } 205424cdb843SMatthew G. Knepley } 205524cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 205624cdb843SMatthew G. Knepley const PetscInt point = points[p]; 205724cdb843SMatthew G. Knepley 205824cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 205924cdb843SMatthew G. Knepley if (dep != dim-1) continue; 206024cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 206124cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 206224cdb843SMatthew G. Knepley ++f; 206324cdb843SMatthew G. Knepley } 206424cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 206524cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2066bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 206724cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 206824cdb843SMatthew G. Knepley } 206924cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 207024cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 207124cdb843SMatthew G. Knepley if (mesh->printFEM) { 207224cdb843SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 207324cdb843SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 207424cdb843SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 207524cdb843SMatthew G. Knepley } 207624cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 207724cdb843SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 207824cdb843SMatthew G. Knepley if (isShell) { 207924cdb843SMatthew G. Knepley JacActionCtx *jctx; 208024cdb843SMatthew G. Knepley 208124cdb843SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 208224cdb843SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 208324cdb843SMatthew G. Knepley } 208424cdb843SMatthew G. Knepley PetscFunctionReturn(0); 208524cdb843SMatthew G. Knepley } 208624cdb843SMatthew G. Knepley 208724cdb843SMatthew G. Knepley #undef __FUNCT__ 208824cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 208924cdb843SMatthew G. Knepley /*@ 209024cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 209124cdb843SMatthew G. Knepley 209224cdb843SMatthew G. Knepley Input Parameters: 209324cdb843SMatthew G. Knepley + dm - The mesh 209424cdb843SMatthew G. Knepley . X - Local input vector 209524cdb843SMatthew G. Knepley - user - The user context 209624cdb843SMatthew G. Knepley 209724cdb843SMatthew G. Knepley Output Parameter: 209824cdb843SMatthew G. Knepley . Jac - Jacobian matrix 209924cdb843SMatthew G. Knepley 210024cdb843SMatthew G. Knepley Note: 210124cdb843SMatthew G. Knepley The first member of the user context must be an FEMContext. 210224cdb843SMatthew G. Knepley 210324cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 210424cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 210524cdb843SMatthew G. Knepley 210624cdb843SMatthew G. Knepley Level: developer 210724cdb843SMatthew G. Knepley 210824cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 210924cdb843SMatthew G. Knepley @*/ 211024cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 211124cdb843SMatthew G. Knepley { 211224cdb843SMatthew G. Knepley PetscErrorCode ierr; 211324cdb843SMatthew G. Knepley 211424cdb843SMatthew G. Knepley PetscFunctionBegin; 211524cdb843SMatthew G. Knepley ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 211624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 211724cdb843SMatthew G. Knepley } 2118