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> 5552f7358SJed Brown 624cdb843SMatthew G. Knepley /************************** Interpolation *******************************/ 724cdb843SMatthew G. Knepley 8552f7358SJed Brown #undef __FUNCT__ 9552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate" 100adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 110adebc6cSBarry Smith { 12552f7358SJed Brown PetscErrorCode ierr; 13552f7358SJed Brown 14552f7358SJed Brown PetscFunctionBegin; 15552f7358SJed Brown PetscValidPointer(ctx, 2); 16552f7358SJed Brown ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr); 171aa26658SKarl Rupp 18552f7358SJed Brown (*ctx)->comm = comm; 19552f7358SJed Brown (*ctx)->dim = -1; 20552f7358SJed Brown (*ctx)->nInput = 0; 210298fd71SBarry Smith (*ctx)->points = NULL; 220298fd71SBarry Smith (*ctx)->cells = NULL; 23552f7358SJed Brown (*ctx)->n = -1; 240298fd71SBarry Smith (*ctx)->coords = NULL; 25552f7358SJed Brown PetscFunctionReturn(0); 26552f7358SJed Brown } 27552f7358SJed Brown 28552f7358SJed Brown #undef __FUNCT__ 29552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim" 300adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 310adebc6cSBarry Smith { 32552f7358SJed Brown PetscFunctionBegin; 330adebc6cSBarry Smith if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 34552f7358SJed Brown ctx->dim = dim; 35552f7358SJed Brown PetscFunctionReturn(0); 36552f7358SJed Brown } 37552f7358SJed Brown 38552f7358SJed Brown #undef __FUNCT__ 39552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim" 400adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 410adebc6cSBarry Smith { 42552f7358SJed Brown PetscFunctionBegin; 43552f7358SJed Brown PetscValidIntPointer(dim, 2); 44552f7358SJed Brown *dim = ctx->dim; 45552f7358SJed Brown PetscFunctionReturn(0); 46552f7358SJed Brown } 47552f7358SJed Brown 48552f7358SJed Brown #undef __FUNCT__ 49552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof" 500adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 510adebc6cSBarry Smith { 52552f7358SJed Brown PetscFunctionBegin; 530adebc6cSBarry Smith if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 54552f7358SJed Brown ctx->dof = dof; 55552f7358SJed Brown PetscFunctionReturn(0); 56552f7358SJed Brown } 57552f7358SJed Brown 58552f7358SJed Brown #undef __FUNCT__ 59552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof" 600adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 610adebc6cSBarry Smith { 62552f7358SJed Brown PetscFunctionBegin; 63552f7358SJed Brown PetscValidIntPointer(dof, 2); 64552f7358SJed Brown *dof = ctx->dof; 65552f7358SJed Brown PetscFunctionReturn(0); 66552f7358SJed Brown } 67552f7358SJed Brown 68552f7358SJed Brown #undef __FUNCT__ 69552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints" 700adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 710adebc6cSBarry Smith { 72552f7358SJed Brown PetscErrorCode ierr; 73552f7358SJed Brown 74552f7358SJed Brown PetscFunctionBegin; 750adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 760adebc6cSBarry Smith if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 77552f7358SJed Brown ctx->nInput = n; 781aa26658SKarl Rupp 79785e854fSJed Brown ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 80552f7358SJed Brown ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 81552f7358SJed Brown PetscFunctionReturn(0); 82552f7358SJed Brown } 83552f7358SJed Brown 84552f7358SJed Brown #undef __FUNCT__ 85552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp" 860adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 870adebc6cSBarry Smith { 88552f7358SJed Brown MPI_Comm comm = ctx->comm; 89552f7358SJed Brown PetscScalar *a; 90552f7358SJed Brown PetscInt p, q, i; 91552f7358SJed Brown PetscMPIInt rank, size; 92552f7358SJed Brown PetscErrorCode ierr; 93552f7358SJed Brown Vec pointVec; 94552f7358SJed Brown IS cellIS; 95552f7358SJed Brown PetscLayout layout; 96552f7358SJed Brown PetscReal *globalPoints; 97cb313848SJed Brown PetscScalar *globalPointsScalar; 98552f7358SJed Brown const PetscInt *ranges; 99552f7358SJed Brown PetscMPIInt *counts, *displs; 100552f7358SJed Brown const PetscInt *foundCells; 101552f7358SJed Brown PetscMPIInt *foundProcs, *globalProcs; 10219436ca2SJed Brown PetscInt n, N; 103552f7358SJed Brown 10419436ca2SJed Brown PetscFunctionBegin; 10519436ca2SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10619436ca2SJed Brown ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 10719436ca2SJed Brown ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 1080adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 10919436ca2SJed Brown /* Locate points */ 11019436ca2SJed Brown n = ctx->nInput; 111552f7358SJed Brown if (!redundantPoints) { 112552f7358SJed Brown ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 113552f7358SJed Brown ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 114552f7358SJed Brown ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 115552f7358SJed Brown ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 116552f7358SJed Brown ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 117552f7358SJed Brown /* Communicate all points to all processes */ 118dcca6d9dSJed Brown ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 119552f7358SJed Brown ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 120552f7358SJed Brown for (p = 0; p < size; ++p) { 121552f7358SJed Brown counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 122552f7358SJed Brown displs[p] = ranges[p]*ctx->dim; 123552f7358SJed Brown } 124552f7358SJed Brown ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 125552f7358SJed Brown } else { 126552f7358SJed Brown N = n; 127552f7358SJed Brown globalPoints = ctx->points; 12838ea73c8SJed Brown counts = displs = NULL; 12938ea73c8SJed Brown layout = NULL; 130552f7358SJed Brown } 131552f7358SJed Brown #if 0 132dcca6d9dSJed Brown ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 13319436ca2SJed Brown /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 134552f7358SJed Brown #else 135cb313848SJed Brown #if defined(PETSC_USE_COMPLEX) 136785e854fSJed Brown ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr); 137cb313848SJed Brown for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i]; 138cb313848SJed Brown #else 139cb313848SJed Brown globalPointsScalar = globalPoints; 140cb313848SJed Brown #endif 14104706141SMatthew G Knepley ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 142dcca6d9dSJed Brown ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 143552f7358SJed Brown ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr); 144552f7358SJed Brown ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr); 145552f7358SJed Brown #endif 146552f7358SJed Brown for (p = 0; p < N; ++p) { 1471aa26658SKarl Rupp if (foundCells[p] >= 0) foundProcs[p] = rank; 1481aa26658SKarl Rupp else foundProcs[p] = size; 149552f7358SJed Brown } 150552f7358SJed Brown /* Let the lowest rank process own each point */ 151efab3cc2SJed Brown ierr = MPI_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 152552f7358SJed Brown ctx->n = 0; 153552f7358SJed Brown for (p = 0; p < N; ++p) { 1540adebc6cSBarry 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); 1551aa26658SKarl Rupp else if (globalProcs[p] == rank) ctx->n++; 156552f7358SJed Brown } 157552f7358SJed Brown /* Create coordinates vector and array of owned cells */ 158785e854fSJed Brown ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 159552f7358SJed Brown ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 160552f7358SJed Brown ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 161552f7358SJed Brown ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 162c0dedaeaSBarry Smith ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 163552f7358SJed Brown ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 164552f7358SJed Brown for (p = 0, q = 0, i = 0; p < N; ++p) { 165552f7358SJed Brown if (globalProcs[p] == rank) { 166552f7358SJed Brown PetscInt d; 167552f7358SJed Brown 1681aa26658SKarl Rupp for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 169552f7358SJed Brown ctx->cells[q++] = foundCells[p]; 170552f7358SJed Brown } 171552f7358SJed Brown } 172552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 173552f7358SJed Brown #if 0 174552f7358SJed Brown ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 175552f7358SJed Brown #else 176552f7358SJed Brown ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 177552f7358SJed Brown ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr); 178552f7358SJed Brown ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 179552f7358SJed Brown ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 180552f7358SJed Brown #endif 181cb313848SJed Brown if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 182d343d804SMatthew G. Knepley if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 183552f7358SJed Brown ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 184552f7358SJed Brown PetscFunctionReturn(0); 185552f7358SJed Brown } 186552f7358SJed Brown 187552f7358SJed Brown #undef __FUNCT__ 188552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates" 1890adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 1900adebc6cSBarry Smith { 191552f7358SJed Brown PetscFunctionBegin; 192552f7358SJed Brown PetscValidPointer(coordinates, 2); 1930adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 194552f7358SJed Brown *coordinates = ctx->coords; 195552f7358SJed Brown PetscFunctionReturn(0); 196552f7358SJed Brown } 197552f7358SJed Brown 198552f7358SJed Brown #undef __FUNCT__ 199552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector" 2000adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 2010adebc6cSBarry Smith { 202552f7358SJed Brown PetscErrorCode ierr; 203552f7358SJed Brown 204552f7358SJed Brown PetscFunctionBegin; 205552f7358SJed Brown PetscValidPointer(v, 2); 2060adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 207552f7358SJed Brown ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 208552f7358SJed Brown ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 209552f7358SJed Brown ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 210c0dedaeaSBarry Smith ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 211552f7358SJed Brown PetscFunctionReturn(0); 212552f7358SJed Brown } 213552f7358SJed Brown 214552f7358SJed Brown #undef __FUNCT__ 215552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector" 2160adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 2170adebc6cSBarry Smith { 218552f7358SJed Brown PetscErrorCode ierr; 219552f7358SJed Brown 220552f7358SJed Brown PetscFunctionBegin; 221552f7358SJed Brown PetscValidPointer(v, 2); 2220adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 223552f7358SJed Brown ierr = VecDestroy(v);CHKERRQ(ierr); 224552f7358SJed Brown PetscFunctionReturn(0); 225552f7358SJed Brown } 226552f7358SJed Brown 227552f7358SJed Brown #undef __FUNCT__ 2287a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private" 2297a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 230a6dfd86eSKarl Rupp { 231552f7358SJed Brown PetscReal *v0, *J, *invJ, detJ; 232552f7358SJed Brown PetscScalar *a, *coords; 233552f7358SJed Brown PetscInt p; 234552f7358SJed Brown PetscErrorCode ierr; 235552f7358SJed Brown 236552f7358SJed Brown PetscFunctionBegin; 237dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 238552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 239552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 240552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 241552f7358SJed Brown PetscInt c = ctx->cells[p]; 242a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 243552f7358SJed Brown PetscReal xi[4]; 244552f7358SJed Brown PetscInt d, f, comp; 245552f7358SJed Brown 2468e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 247552f7358SJed Brown if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2480298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2491aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2501aa26658SKarl Rupp 251552f7358SJed Brown for (d = 0; d < ctx->dim; ++d) { 252552f7358SJed Brown xi[d] = 0.0; 2531aa26658SKarl 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]); 2541aa26658SKarl 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]; 255552f7358SJed Brown } 2560298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 257552f7358SJed Brown } 258552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 259552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 260552f7358SJed Brown ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 261552f7358SJed Brown PetscFunctionReturn(0); 262552f7358SJed Brown } 263552f7358SJed Brown 264552f7358SJed Brown #undef __FUNCT__ 2657a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private" 2667a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 2677a1931ceSMatthew G. Knepley { 2687a1931ceSMatthew G. Knepley PetscReal *v0, *J, *invJ, detJ; 2697a1931ceSMatthew G. Knepley PetscScalar *a, *coords; 2707a1931ceSMatthew G. Knepley PetscInt p; 2717a1931ceSMatthew G. Knepley PetscErrorCode ierr; 2727a1931ceSMatthew G. Knepley 2737a1931ceSMatthew G. Knepley PetscFunctionBegin; 274dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 2757a1931ceSMatthew G. Knepley ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 2767a1931ceSMatthew G. Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2777a1931ceSMatthew G. Knepley for (p = 0; p < ctx->n; ++p) { 2787a1931ceSMatthew G. Knepley PetscInt c = ctx->cells[p]; 2797a1931ceSMatthew G. Knepley const PetscInt order[3] = {2, 1, 3}; 2802584bbe8SMatthew G. Knepley PetscScalar *x = NULL; 2817a1931ceSMatthew G. Knepley PetscReal xi[4]; 2827a1931ceSMatthew G. Knepley PetscInt d, f, comp; 2837a1931ceSMatthew G. Knepley 2848e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2857a1931ceSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2867a1931ceSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2877a1931ceSMatthew G. Knepley for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2887a1931ceSMatthew G. Knepley 2897a1931ceSMatthew G. Knepley for (d = 0; d < ctx->dim; ++d) { 2907a1931ceSMatthew G. Knepley xi[d] = 0.0; 2917a1931ceSMatthew 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]); 2927a1931ceSMatthew 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]; 2937a1931ceSMatthew G. Knepley } 2947a1931ceSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2957a1931ceSMatthew G. Knepley } 2967a1931ceSMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2977a1931ceSMatthew G. Knepley ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 2987a1931ceSMatthew G. Knepley ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 2997a1931ceSMatthew G. Knepley PetscFunctionReturn(0); 3007a1931ceSMatthew G. Knepley } 3017a1931ceSMatthew G. Knepley 3027a1931ceSMatthew G. Knepley #undef __FUNCT__ 303552f7358SJed Brown #define __FUNCT__ "QuadMap_Private" 3045820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 305552f7358SJed Brown { 306552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 307552f7358SJed Brown const PetscScalar x0 = vertices[0]; 308552f7358SJed Brown const PetscScalar y0 = vertices[1]; 309552f7358SJed Brown const PetscScalar x1 = vertices[2]; 310552f7358SJed Brown const PetscScalar y1 = vertices[3]; 311552f7358SJed Brown const PetscScalar x2 = vertices[4]; 312552f7358SJed Brown const PetscScalar y2 = vertices[5]; 313552f7358SJed Brown const PetscScalar x3 = vertices[6]; 314552f7358SJed Brown const PetscScalar y3 = vertices[7]; 315552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 316552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 317552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 318552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 319552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 320552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 321552f7358SJed Brown PetscScalar *ref, *real; 322552f7358SJed Brown PetscErrorCode ierr; 323552f7358SJed Brown 324552f7358SJed Brown PetscFunctionBegin; 325552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 326552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 327552f7358SJed Brown { 328552f7358SJed Brown const PetscScalar p0 = ref[0]; 329552f7358SJed Brown const PetscScalar p1 = ref[1]; 330552f7358SJed Brown 331552f7358SJed Brown real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 332552f7358SJed Brown real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 333552f7358SJed Brown } 334552f7358SJed Brown ierr = PetscLogFlops(28);CHKERRQ(ierr); 335552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 336552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 337552f7358SJed Brown PetscFunctionReturn(0); 338552f7358SJed Brown } 339552f7358SJed Brown 340c0dedaeaSBarry Smith #include <petsc-private/dmimpl.h> 341552f7358SJed Brown #undef __FUNCT__ 342552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private" 343d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 344552f7358SJed Brown { 345552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 346552f7358SJed Brown const PetscScalar x0 = vertices[0]; 347552f7358SJed Brown const PetscScalar y0 = vertices[1]; 348552f7358SJed Brown const PetscScalar x1 = vertices[2]; 349552f7358SJed Brown const PetscScalar y1 = vertices[3]; 350552f7358SJed Brown const PetscScalar x2 = vertices[4]; 351552f7358SJed Brown const PetscScalar y2 = vertices[5]; 352552f7358SJed Brown const PetscScalar x3 = vertices[6]; 353552f7358SJed Brown const PetscScalar y3 = vertices[7]; 354552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 355552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 356552f7358SJed Brown PetscScalar *ref; 357552f7358SJed Brown PetscErrorCode ierr; 358552f7358SJed Brown 359552f7358SJed Brown PetscFunctionBegin; 360552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 361552f7358SJed Brown { 362552f7358SJed Brown const PetscScalar x = ref[0]; 363552f7358SJed Brown const PetscScalar y = ref[1]; 364552f7358SJed Brown const PetscInt rows[2] = {0, 1}; 365da80777bSKarl Rupp PetscScalar values[4]; 366da80777bSKarl Rupp 367da80777bSKarl Rupp values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 368da80777bSKarl Rupp values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 36994ab13aaSBarry Smith ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 370552f7358SJed Brown } 371552f7358SJed Brown ierr = PetscLogFlops(30);CHKERRQ(ierr); 372552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 37394ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 37494ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 375552f7358SJed Brown PetscFunctionReturn(0); 376552f7358SJed Brown } 377552f7358SJed Brown 378552f7358SJed Brown #undef __FUNCT__ 379552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private" 380a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 381a6dfd86eSKarl Rupp { 382fafc0619SMatthew G Knepley DM dmCoord; 383552f7358SJed Brown SNES snes; 384552f7358SJed Brown KSP ksp; 385552f7358SJed Brown PC pc; 386552f7358SJed Brown Vec coordsLocal, r, ref, real; 387552f7358SJed Brown Mat J; 388552f7358SJed Brown PetscScalar *a, *coords; 389552f7358SJed Brown PetscInt p; 390552f7358SJed Brown PetscErrorCode ierr; 391552f7358SJed Brown 392552f7358SJed Brown PetscFunctionBegin; 393552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 394fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 395552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 396552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 397552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 398552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 399c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 400552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 401552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 402552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 403552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 404552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 405552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 4060298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 4070298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 408552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 409552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 410552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 411552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 412552f7358SJed Brown 413552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 414552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 415552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 416a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 417552f7358SJed Brown PetscScalar *xi; 418cb313848SJed Brown PetscReal xir[2]; 419552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 420552f7358SJed Brown 421552f7358SJed Brown /* Can make this do all points at once */ 4220298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4230adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 4240298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 4250adebc6cSBarry Smith if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof); 4260298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 4270298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 428552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 429552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 430552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 431552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 432552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 433552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 434cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 435cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 4361aa26658SKarl 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]; 4371aa26658SKarl Rupp 438552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 4390298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4400298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 441552f7358SJed Brown } 442552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 443552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 444552f7358SJed Brown 445552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 446552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 447552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 448552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 449552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 450552f7358SJed Brown PetscFunctionReturn(0); 451552f7358SJed Brown } 452552f7358SJed Brown 453552f7358SJed Brown #undef __FUNCT__ 454552f7358SJed Brown #define __FUNCT__ "HexMap_Private" 4555820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 456552f7358SJed Brown { 457552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 458552f7358SJed Brown const PetscScalar x0 = vertices[0]; 459552f7358SJed Brown const PetscScalar y0 = vertices[1]; 460552f7358SJed Brown const PetscScalar z0 = vertices[2]; 4617a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 4627a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 4637a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 464552f7358SJed Brown const PetscScalar x2 = vertices[6]; 465552f7358SJed Brown const PetscScalar y2 = vertices[7]; 466552f7358SJed Brown const PetscScalar z2 = vertices[8]; 4677a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 4687a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 4697a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 470552f7358SJed Brown const PetscScalar x4 = vertices[12]; 471552f7358SJed Brown const PetscScalar y4 = vertices[13]; 472552f7358SJed Brown const PetscScalar z4 = vertices[14]; 473552f7358SJed Brown const PetscScalar x5 = vertices[15]; 474552f7358SJed Brown const PetscScalar y5 = vertices[16]; 475552f7358SJed Brown const PetscScalar z5 = vertices[17]; 476552f7358SJed Brown const PetscScalar x6 = vertices[18]; 477552f7358SJed Brown const PetscScalar y6 = vertices[19]; 478552f7358SJed Brown const PetscScalar z6 = vertices[20]; 479552f7358SJed Brown const PetscScalar x7 = vertices[21]; 480552f7358SJed Brown const PetscScalar y7 = vertices[22]; 481552f7358SJed Brown const PetscScalar z7 = vertices[23]; 482552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 483552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 484552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 485552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 486552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 487552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 488552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 489552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 490552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 491552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 492552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 493552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 494552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 495552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 496552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 497552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 498552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 499552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 500552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 501552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 502552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 503552f7358SJed Brown PetscScalar *ref, *real; 504552f7358SJed Brown PetscErrorCode ierr; 505552f7358SJed Brown 506552f7358SJed Brown PetscFunctionBegin; 507552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 508552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 509552f7358SJed Brown { 510552f7358SJed Brown const PetscScalar p0 = ref[0]; 511552f7358SJed Brown const PetscScalar p1 = ref[1]; 512552f7358SJed Brown const PetscScalar p2 = ref[2]; 513552f7358SJed Brown 514552f7358SJed 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; 515552f7358SJed 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; 516552f7358SJed 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; 517552f7358SJed Brown } 518552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 519552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 520552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 521552f7358SJed Brown PetscFunctionReturn(0); 522552f7358SJed Brown } 523552f7358SJed Brown 524552f7358SJed Brown #undef __FUNCT__ 525552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private" 526d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 527552f7358SJed Brown { 528552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 529552f7358SJed Brown const PetscScalar x0 = vertices[0]; 530552f7358SJed Brown const PetscScalar y0 = vertices[1]; 531552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5327a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5337a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5347a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 535552f7358SJed Brown const PetscScalar x2 = vertices[6]; 536552f7358SJed Brown const PetscScalar y2 = vertices[7]; 537552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5387a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5397a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5407a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 541552f7358SJed Brown const PetscScalar x4 = vertices[12]; 542552f7358SJed Brown const PetscScalar y4 = vertices[13]; 543552f7358SJed Brown const PetscScalar z4 = vertices[14]; 544552f7358SJed Brown const PetscScalar x5 = vertices[15]; 545552f7358SJed Brown const PetscScalar y5 = vertices[16]; 546552f7358SJed Brown const PetscScalar z5 = vertices[17]; 547552f7358SJed Brown const PetscScalar x6 = vertices[18]; 548552f7358SJed Brown const PetscScalar y6 = vertices[19]; 549552f7358SJed Brown const PetscScalar z6 = vertices[20]; 550552f7358SJed Brown const PetscScalar x7 = vertices[21]; 551552f7358SJed Brown const PetscScalar y7 = vertices[22]; 552552f7358SJed Brown const PetscScalar z7 = vertices[23]; 553552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 554552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 555552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 556552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 557552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 558552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 559552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 560552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 561552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 562552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 563552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 564552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 565552f7358SJed Brown PetscScalar *ref; 566552f7358SJed Brown PetscErrorCode ierr; 567552f7358SJed Brown 568552f7358SJed Brown PetscFunctionBegin; 569552f7358SJed Brown ierr = VecGetArray(Xref, &ref);CHKERRQ(ierr); 570552f7358SJed Brown { 571552f7358SJed Brown const PetscScalar x = ref[0]; 572552f7358SJed Brown const PetscScalar y = ref[1]; 573552f7358SJed Brown const PetscScalar z = ref[2]; 574552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 575da80777bSKarl Rupp PetscScalar values[9]; 576da80777bSKarl Rupp 577da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 578da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 579da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 580da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 581da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 582da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 583da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 584da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 585da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 5861aa26658SKarl Rupp 58794ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 588552f7358SJed Brown } 589552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 590552f7358SJed Brown ierr = VecRestoreArray(Xref, &ref);CHKERRQ(ierr); 59194ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 59294ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 593552f7358SJed Brown PetscFunctionReturn(0); 594552f7358SJed Brown } 595552f7358SJed Brown 596552f7358SJed Brown #undef __FUNCT__ 597552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private" 598a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 599a6dfd86eSKarl Rupp { 600fafc0619SMatthew G Knepley DM dmCoord; 601552f7358SJed Brown SNES snes; 602552f7358SJed Brown KSP ksp; 603552f7358SJed Brown PC pc; 604552f7358SJed Brown Vec coordsLocal, r, ref, real; 605552f7358SJed Brown Mat J; 606552f7358SJed Brown PetscScalar *a, *coords; 607552f7358SJed Brown PetscInt p; 608552f7358SJed Brown PetscErrorCode ierr; 609552f7358SJed Brown 610552f7358SJed Brown PetscFunctionBegin; 611552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 612fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 613552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 614552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 615552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 616552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 617c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 618552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 619552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 620552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 621552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 622552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 623552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 6240298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 6250298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 626552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 627552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 628552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 629552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 630552f7358SJed Brown 631552f7358SJed Brown ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr); 632552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 633552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 634a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 635552f7358SJed Brown PetscScalar *xi; 636cb313848SJed Brown PetscReal xir[3]; 637552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 638552f7358SJed Brown 639552f7358SJed Brown /* Can make this do all points at once */ 6400298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6410adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 6420298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 6430adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 6440298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 6450298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 646552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 647552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 648552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 649552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 650552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 651552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 652552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 653cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 654cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 655cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 656552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 657552f7358SJed Brown a[p*ctx->dof+comp] = 658cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 6597a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 660cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 6617a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 662cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 663cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 664cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 665cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 666552f7358SJed Brown } 667552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 6680298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6690298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 670552f7358SJed Brown } 671552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 672552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr); 673552f7358SJed Brown 674552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 675552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 676552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 677552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 678552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 679552f7358SJed Brown PetscFunctionReturn(0); 680552f7358SJed Brown } 681552f7358SJed Brown 682552f7358SJed Brown #undef __FUNCT__ 683552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate" 684552f7358SJed Brown /* 685552f7358SJed Brown Input Parameters: 686552f7358SJed Brown + ctx - The DMInterpolationInfo context 687552f7358SJed Brown . dm - The DM 688552f7358SJed Brown - x - The local vector containing the field to be interpolated 689552f7358SJed Brown 690552f7358SJed Brown Output Parameters: 691552f7358SJed Brown . v - The vector containing the interpolated values 692552f7358SJed Brown */ 6930adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 6940adebc6cSBarry Smith { 695552f7358SJed Brown PetscInt dim, coneSize, n; 696552f7358SJed Brown PetscErrorCode ierr; 697552f7358SJed Brown 698552f7358SJed Brown PetscFunctionBegin; 699552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 700552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 701552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 702552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 7030adebc6cSBarry 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); 704552f7358SJed Brown if (n) { 705c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 706552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 707552f7358SJed Brown if (dim == 2) { 708552f7358SJed Brown if (coneSize == 3) { 7097a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 710552f7358SJed Brown } else if (coneSize == 4) { 711552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 7120adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 713552f7358SJed Brown } else if (dim == 3) { 714552f7358SJed Brown if (coneSize == 4) { 7157a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 716552f7358SJed Brown } else { 717552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 718552f7358SJed Brown } 7190adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 720552f7358SJed Brown } 721552f7358SJed Brown PetscFunctionReturn(0); 722552f7358SJed Brown } 723552f7358SJed Brown 724552f7358SJed Brown #undef __FUNCT__ 725552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy" 7260adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 7270adebc6cSBarry Smith { 728552f7358SJed Brown PetscErrorCode ierr; 729552f7358SJed Brown 730552f7358SJed Brown PetscFunctionBegin; 731552f7358SJed Brown PetscValidPointer(ctx, 2); 732552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 733552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 734552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 735552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 7360298fd71SBarry Smith *ctx = NULL; 737552f7358SJed Brown PetscFunctionReturn(0); 738552f7358SJed Brown } 739cc0c4584SMatthew G. Knepley 740cc0c4584SMatthew G. Knepley #undef __FUNCT__ 741cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields" 742cc0c4584SMatthew G. Knepley /*@C 743cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 744cc0c4584SMatthew G. Knepley 745cc0c4584SMatthew G. Knepley Collective on SNES 746cc0c4584SMatthew G. Knepley 747cc0c4584SMatthew G. Knepley Input Parameters: 748cc0c4584SMatthew G. Knepley + snes - the SNES context 749cc0c4584SMatthew G. Knepley . its - iteration number 750cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 751cc0c4584SMatthew G. Knepley - dummy - unused context 752cc0c4584SMatthew G. Knepley 753cc0c4584SMatthew G. Knepley Notes: 754cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 755cc0c4584SMatthew G. Knepley 756cc0c4584SMatthew G. Knepley Level: intermediate 757cc0c4584SMatthew G. Knepley 758cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 759cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 760cc0c4584SMatthew G. Knepley @*/ 761cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy) 762cc0c4584SMatthew G. Knepley { 763cc0c4584SMatthew G. Knepley PetscViewer viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) snes)); 764cc0c4584SMatthew G. Knepley Vec res; 765cc0c4584SMatthew G. Knepley DM dm; 766cc0c4584SMatthew G. Knepley PetscSection s; 767cc0c4584SMatthew G. Knepley const PetscScalar *r; 768cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 769cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 770cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 771cc0c4584SMatthew G. Knepley 772cc0c4584SMatthew G. Knepley PetscFunctionBegin; 773cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 774cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 775cc0c4584SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 776cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 777cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 778cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 779cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 780cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 781cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 782cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 783cc0c4584SMatthew G. Knepley 784cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 785cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 786cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 787cc0c4584SMatthew G. Knepley } 788cc0c4584SMatthew G. Knepley } 789cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 790cc0c4584SMatthew G. Knepley ierr = MPI_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 791cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 792cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 793cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 794cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 795cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 796cc0c4584SMatthew G. Knepley } 797cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 798cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 799cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 800cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 801cc0c4584SMatthew G. Knepley } 80224cdb843SMatthew G. Knepley 80324cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 80424cdb843SMatthew G. Knepley 80524cdb843SMatthew G. Knepley #undef __FUNCT__ 8067d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 8077d4028c8SMatthew G. Knepley /*@ 8087d4028c8SMatthew G. Knepley DMPlexSNESGetGeometryFEM - Return precomputed geometric data 8097d4028c8SMatthew G. Knepley 8107d4028c8SMatthew G. Knepley Input Parameter: 8117d4028c8SMatthew G. Knepley . dm - The DM 8127d4028c8SMatthew G. Knepley 8137d4028c8SMatthew G. Knepley Output Parameters: 8147d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 8157d4028c8SMatthew G. Knepley 8167d4028c8SMatthew G. Knepley Level: developer 8177d4028c8SMatthew G. Knepley 8187d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal() 8197d4028c8SMatthew G. Knepley @*/ 8207d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 8217d4028c8SMatthew G. Knepley { 8227d4028c8SMatthew G. Knepley DMSNES dmsnes; 8237d4028c8SMatthew G. Knepley PetscObject obj; 8247d4028c8SMatthew G. Knepley PetscErrorCode ierr; 8257d4028c8SMatthew G. Knepley 8267d4028c8SMatthew G. Knepley PetscFunctionBegin; 8277d4028c8SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8287d4028c8SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 8297d4028c8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 8307d4028c8SMatthew G. Knepley if (!obj) { 8317d4028c8SMatthew G. Knepley Vec cellgeom; 8327d4028c8SMatthew G. Knepley 8337d4028c8SMatthew G. Knepley ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 8347d4028c8SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 8357d4028c8SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 8367d4028c8SMatthew G. Knepley } 8377d4028c8SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 8387d4028c8SMatthew G. Knepley PetscFunctionReturn(0); 8397d4028c8SMatthew G. Knepley } 8407d4028c8SMatthew G. Knepley 8417d4028c8SMatthew G. Knepley #undef __FUNCT__ 84224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal" 84324cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 84424cdb843SMatthew G. Knepley { 84524cdb843SMatthew G. Knepley PetscErrorCode ierr; 84624cdb843SMatthew G. Knepley 84724cdb843SMatthew G. Knepley PetscFunctionBegin; 84824cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 84924cdb843SMatthew G. Knepley /* FEM+FVM */ 85024cdb843SMatthew G. Knepley /* Get sizes from dm and dmAux */ 85124cdb843SMatthew G. Knepley /* ONCE: Compute geometric data (includes gradient), stash in mesh */ 85224cdb843SMatthew G. Knepley /* Use named vectors */ 85324cdb843SMatthew G. Knepley /* Limit cell gradients */ 85424cdb843SMatthew G. Knepley /* Handle boundary values */ 85524cdb843SMatthew G. Knepley /* Loop over domain */ 85624cdb843SMatthew G. Knepley /* Extract geometry and coefficients */ 85724cdb843SMatthew G. Knepley /* Loop over fields */ 85824cdb843SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 85924cdb843SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 86024cdb843SMatthew G. Knepley /* Loop over domain */ 86124cdb843SMatthew G. Knepley /* Add elemVec to locX */ 86224cdb843SMatthew G. Knepley /* Accumulate fluxes to cells */ 86324cdb843SMatthew G. Knepley 86424cdb843SMatthew G. Knepley /* FEM */ 86524cdb843SMatthew G. Knepley /* Get sizes from dm and dmAux */ 86624cdb843SMatthew G. Knepley /* Handle boundary values */ 86724cdb843SMatthew G. Knepley /* Loop over domain */ 86824cdb843SMatthew G. Knepley /* Calculate geometry */ 86924cdb843SMatthew G. Knepley /* Extract coefficients */ 87024cdb843SMatthew G. Knepley /* Loop over fields */ 87124cdb843SMatthew G. Knepley /* Set tiling for FE*/ 87224cdb843SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 87324cdb843SMatthew G. Knepley /* Loop over subdomain */ 87424cdb843SMatthew G. Knepley /* Loop over quad points */ 87524cdb843SMatthew G. Knepley /* Transform coords to real space */ 87624cdb843SMatthew G. Knepley /* Evaluate field and aux fields at point */ 87724cdb843SMatthew G. Knepley /* Evaluate residual at point */ 87824cdb843SMatthew G. Knepley /* Transform residual to real space */ 87924cdb843SMatthew G. Knepley /* Add residual to elemVec */ 88024cdb843SMatthew G. Knepley /* Loop over domain */ 88124cdb843SMatthew G. Knepley /* Add elemVec to locX */ 88224cdb843SMatthew G. Knepley 88324cdb843SMatthew G. Knepley /* FVM */ 88424cdb843SMatthew G. Knepley /* Compute geometric data */ 88524cdb843SMatthew G. Knepley /* If using gradients */ 88624cdb843SMatthew G. Knepley /* Compute gradient data */ 88724cdb843SMatthew G. Knepley /* Loop over domain faces */ 88824cdb843SMatthew G. Knepley /* Count computational faces */ 88924cdb843SMatthew G. Knepley /* Reconstruct cell gradient */ 89024cdb843SMatthew G. Knepley /* Loop over domain cells */ 89124cdb843SMatthew G. Knepley /* Limit cell gradients */ 89224cdb843SMatthew G. Knepley /* Handle boundary values */ 89324cdb843SMatthew G. Knepley /* Loop over domain faces */ 89424cdb843SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 89524cdb843SMatthew G. Knepley /* Riemann solve over faces */ 89624cdb843SMatthew G. Knepley /* Loop over domain faces */ 89724cdb843SMatthew G. Knepley /* Accumulate fluxes to cells */ 89824cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 89924cdb843SMatthew G. Knepley PetscFunctionReturn(0); 90024cdb843SMatthew G. Knepley } 90124cdb843SMatthew G. Knepley 90224cdb843SMatthew G. Knepley #undef __FUNCT__ 90324cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal" 90424cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 90524cdb843SMatthew G. Knepley { 90624cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 90724cdb843SMatthew G. Knepley const char *name = "Residual"; 90824cdb843SMatthew G. Knepley DM dmAux; 90924cdb843SMatthew G. Knepley DMLabel depth; 910*bbce034cSMatthew G. Knepley Vec A, cellgeom; 91124cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 91224cdb843SMatthew G. Knepley PetscQuadrature q; 91324cdb843SMatthew G. Knepley PetscSection section, sectionAux; 914*bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 91524cdb843SMatthew G. Knepley PetscScalar *elemVec, *u, *u_t, *a = NULL; 91624cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd; 91724cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux; 91824cdb843SMatthew G. Knepley PetscErrorCode ierr; 91924cdb843SMatthew G. Knepley 92024cdb843SMatthew G. Knepley PetscFunctionBegin; 92124cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 92224cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 92324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 92424cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 92524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 92624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 92724cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 92824cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 92924cdb843SMatthew G. Knepley numCells = cEnd - cStart; 93024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 93124cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 93224cdb843SMatthew G. Knepley if (dmAux) { 93324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 93424cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 93524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 93624cdb843SMatthew G. Knepley } 93724cdb843SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 93824cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 939*bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 94024cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 941*bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 942*bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 94324cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 94424cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 94524cdb843SMatthew G. Knepley PetscInt i; 94624cdb843SMatthew G. Knepley 94724cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 94824cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 94924cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 95024cdb843SMatthew G. Knepley if (X_t) { 95124cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 95224cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 95324cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 95424cdb843SMatthew G. Knepley } 95524cdb843SMatthew G. Knepley if (dmAux) { 95624cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 95724cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 95824cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 95924cdb843SMatthew G. Knepley } 96024cdb843SMatthew G. Knepley } 96124cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 96224cdb843SMatthew G. Knepley PetscFE fe; 96324cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 96424cdb843SMatthew G. Knepley /* Conforming batches */ 96524cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 96624cdb843SMatthew G. Knepley /* Remainder */ 96724cdb843SMatthew G. Knepley PetscInt Nr, offset; 96824cdb843SMatthew G. Knepley 96924cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 97024cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 97124cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 97224cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 97324cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 97424cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 97524cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 97624cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 97724cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 97824cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 97924cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 98024cdb843SMatthew G. Knepley offset = numCells - Nr; 981*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 982*bbce034cSMatthew 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); 98324cdb843SMatthew G. Knepley } 98424cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 98524cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);} 98624cdb843SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 98724cdb843SMatthew G. Knepley } 988*bbce034cSMatthew G. Knepley ierr = VecRestoreArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 989*bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 99024cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 99124cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 99224cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 99324cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 99424cdb843SMatthew G. Knepley const char *bdLabel; 99524cdb843SMatthew G. Knepley DMLabel label; 99624cdb843SMatthew G. Knepley IS pointIS; 99724cdb843SMatthew G. Knepley const PetscInt *points; 99824cdb843SMatthew G. Knepley const PetscInt *values; 99924cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 100024cdb843SMatthew G. Knepley PetscBool isEssential; 100124cdb843SMatthew G. Knepley 100224cdb843SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 100324cdb843SMatthew G. Knepley if (isEssential) continue; 100424cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 100524cdb843SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 100624cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 100724cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 100824cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 100924cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 101024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 101124cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 101224cdb843SMatthew G. Knepley } 1013*bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 101424cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 101524cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 101624cdb843SMatthew G. Knepley const PetscInt point = points[p]; 101724cdb843SMatthew G. Knepley PetscScalar *x = NULL; 101824cdb843SMatthew G. Knepley PetscInt i; 101924cdb843SMatthew G. Knepley 102024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 102124cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1022*bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1023*bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n); 1024*bbce034cSMatthew 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); 102524cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 102624cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 102724cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 102824cdb843SMatthew G. Knepley if (X_t) { 102924cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 103024cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 103124cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 103224cdb843SMatthew G. Knepley } 103324cdb843SMatthew G. Knepley ++f; 103424cdb843SMatthew G. Knepley } 103524cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 103624cdb843SMatthew G. Knepley PetscFE fe; 103724cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 103824cdb843SMatthew G. Knepley /* Conforming batches */ 103924cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 104024cdb843SMatthew G. Knepley /* Remainder */ 104124cdb843SMatthew G. Knepley PetscInt Nr, offset; 104224cdb843SMatthew G. Knepley 104324cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 104424cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 104524cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 104624cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 104724cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 104824cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 104924cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 105024cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 105124cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 105224cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 105324cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 105424cdb843SMatthew G. Knepley offset = numFaces - Nr; 1055*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr); 1056*bbce034cSMatthew 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); 105724cdb843SMatthew G. Knepley } 105824cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 105924cdb843SMatthew G. Knepley const PetscInt point = points[p]; 106024cdb843SMatthew G. Knepley 106124cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 106224cdb843SMatthew G. Knepley if (dep != dim-1) continue; 106324cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 106424cdb843SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr); 106524cdb843SMatthew G. Knepley ++f; 106624cdb843SMatthew G. Knepley } 106724cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 106824cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1069*bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 107024cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 107124cdb843SMatthew G. Knepley } 107224cdb843SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);} 107324cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 107424cdb843SMatthew G. Knepley PetscFunctionReturn(0); 107524cdb843SMatthew G. Knepley } 107624cdb843SMatthew G. Knepley 107724cdb843SMatthew G. Knepley #undef __FUNCT__ 107824cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 107924cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 108024cdb843SMatthew G. Knepley { 108124cdb843SMatthew G. Knepley DM dmCh, dmAux; 1082*bbce034cSMatthew G. Knepley Vec A, cellgeom; 108324cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 108424cdb843SMatthew G. Knepley PetscQuadrature q; 108524cdb843SMatthew G. Knepley PetscSection section, sectionAux; 1086*bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 108724cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 108824cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 108924cdb843SMatthew G. Knepley PetscInt totDim, totDimAux, diffCell = 0; 109024cdb843SMatthew G. Knepley PetscErrorCode ierr; 109124cdb843SMatthew G. Knepley 109224cdb843SMatthew G. Knepley PetscFunctionBegin; 109324cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 109424cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 109524cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 109624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 109724cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 109824cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 109924cdb843SMatthew G. Knepley numCells = cEnd - cStart; 110024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 110124cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 110224cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 110324cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 110424cdb843SMatthew G. Knepley if (dmAux) { 110524cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 110624cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 110724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 110824cdb843SMatthew G. Knepley } 110924cdb843SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 111024cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1111*bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 111224cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 111324cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1114*bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 1115*bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 111624cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 111724cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 111824cdb843SMatthew G. Knepley PetscInt i; 111924cdb843SMatthew G. Knepley 112024cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 112124cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 112224cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 112324cdb843SMatthew G. Knepley if (X_t) { 112424cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 112524cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 112624cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 112724cdb843SMatthew G. Knepley } 112824cdb843SMatthew G. Knepley if (dmAux) { 112924cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 113024cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 113124cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 113224cdb843SMatthew G. Knepley } 113324cdb843SMatthew G. Knepley } 113424cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 113524cdb843SMatthew G. Knepley PetscFE fe, feCh; 113624cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 113724cdb843SMatthew G. Knepley /* Conforming batches */ 113824cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 113924cdb843SMatthew G. Knepley /* Remainder */ 114024cdb843SMatthew G. Knepley PetscInt Nr, offset; 114124cdb843SMatthew G. Knepley 114224cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 114324cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 114424cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 114524cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 114624cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 114724cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 114824cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 114924cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 115024cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 115124cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 115224cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 115324cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 115424cdb843SMatthew G. Knepley offset = numCells - Nr; 1155*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 1156*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr); 1157*bbce034cSMatthew 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); 1158*bbce034cSMatthew 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); 115924cdb843SMatthew G. Knepley } 116024cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 116124cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 116224cdb843SMatthew G. Knepley PetscInt d; 116324cdb843SMatthew G. Knepley 116424cdb843SMatthew 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;} 116524cdb843SMatthew G. Knepley if (diff) { 116624cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 116724cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 116824cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 116924cdb843SMatthew G. Knepley ++diffCell; 117024cdb843SMatthew G. Knepley } 117124cdb843SMatthew G. Knepley if (diffCell > 9) break; 117224cdb843SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 117324cdb843SMatthew G. Knepley } 1174*bbce034cSMatthew G. Knepley ierr = VecRestoreArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 1175*bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 117624cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 117724cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 117824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 117924cdb843SMatthew G. Knepley } 118024cdb843SMatthew G. Knepley 118124cdb843SMatthew G. Knepley #undef __FUNCT__ 118224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 118324cdb843SMatthew G. Knepley /*@ 118424cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 118524cdb843SMatthew G. Knepley 118624cdb843SMatthew G. Knepley Input Parameters: 118724cdb843SMatthew G. Knepley + dm - The mesh 118824cdb843SMatthew G. Knepley . X - Local solution 118924cdb843SMatthew G. Knepley - user - The user context 119024cdb843SMatthew G. Knepley 119124cdb843SMatthew G. Knepley Output Parameter: 119224cdb843SMatthew G. Knepley . F - Local output vector 119324cdb843SMatthew G. Knepley 119424cdb843SMatthew G. Knepley Level: developer 119524cdb843SMatthew G. Knepley 119624cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 119724cdb843SMatthew G. Knepley @*/ 119824cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 119924cdb843SMatthew G. Knepley { 120024cdb843SMatthew G. Knepley PetscObject check; 120124cdb843SMatthew G. Knepley PetscErrorCode ierr; 120224cdb843SMatthew G. Knepley 120324cdb843SMatthew G. Knepley PetscFunctionBegin; 120424cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 120524cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr); 120624cdb843SMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);} 120724cdb843SMatthew G. Knepley else {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);} 120824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 120924cdb843SMatthew G. Knepley } 121024cdb843SMatthew G. Knepley 121124cdb843SMatthew G. Knepley #undef __FUNCT__ 121224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal" 121324cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 121424cdb843SMatthew G. Knepley { 121524cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 121624cdb843SMatthew G. Knepley const char *name = "Jacobian"; 121724cdb843SMatthew G. Knepley DM dmAux; 121824cdb843SMatthew G. Knepley DMLabel depth; 1219*bbce034cSMatthew G. Knepley Vec A, cellgeom; 122024cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 122124cdb843SMatthew G. Knepley PetscQuadrature quad; 122224cdb843SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 1223*bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 122424cdb843SMatthew G. Knepley PetscScalar *elemMat, *u, *u_t, *a = NULL; 122524cdb843SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c; 122624cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 122724cdb843SMatthew G. Knepley PetscBool isShell; 122824cdb843SMatthew G. Knepley PetscErrorCode ierr; 122924cdb843SMatthew G. Knepley 123024cdb843SMatthew G. Knepley PetscFunctionBegin; 123124cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 123224cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 123324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 123424cdb843SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 123524cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 123624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 123724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 123824cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 123924cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 124024cdb843SMatthew G. Knepley numCells = cEnd - cStart; 124124cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 124224cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 124324cdb843SMatthew G. Knepley if (dmAux) { 124424cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 124524cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 124624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 124724cdb843SMatthew G. Knepley } 124824cdb843SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 124924cdb843SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 1250*bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr); 125124cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1252*bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 1253*bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 125424cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 125524cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 125624cdb843SMatthew G. Knepley PetscInt i; 125724cdb843SMatthew G. Knepley 125824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 125924cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 126024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 126124cdb843SMatthew G. Knepley if (X_t) { 126224cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 126324cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 126424cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 126524cdb843SMatthew G. Knepley } 126624cdb843SMatthew G. Knepley if (dmAux) { 126724cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 126824cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 126924cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 127024cdb843SMatthew G. Knepley } 127124cdb843SMatthew G. Knepley } 127224cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 127324cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 127424cdb843SMatthew G. Knepley PetscFE fe; 127524cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 127624cdb843SMatthew G. Knepley /* Conforming batches */ 127724cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 127824cdb843SMatthew G. Knepley /* Remainder */ 127924cdb843SMatthew G. Knepley PetscInt Nr, offset; 128024cdb843SMatthew G. Knepley 128124cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 128224cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 128324cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 128424cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 128524cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 128624cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 128724cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 128824cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 128924cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 129024cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 129124cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 129224cdb843SMatthew G. Knepley offset = numCells - Nr; 129324cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 1294*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr); 1295*bbce034cSMatthew 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); 129624cdb843SMatthew G. Knepley } 129724cdb843SMatthew G. Knepley } 129824cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 129924cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);} 130024cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 130124cdb843SMatthew G. Knepley } 1302*bbce034cSMatthew G. Knepley ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr); 1303*bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr); 130424cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 130524cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 130624cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 130724cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 130824cdb843SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 130924cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 131024cdb843SMatthew G. Knepley const char *bdLabel; 131124cdb843SMatthew G. Knepley DMLabel label; 131224cdb843SMatthew G. Knepley IS pointIS; 131324cdb843SMatthew G. Knepley const PetscInt *points; 131424cdb843SMatthew G. Knepley const PetscInt *values; 131524cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 131624cdb843SMatthew G. Knepley PetscBool isEssential; 131724cdb843SMatthew G. Knepley 131824cdb843SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 131924cdb843SMatthew G. Knepley if (isEssential) continue; 132024cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 132124cdb843SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 132224cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 132324cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 132424cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 132524cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 132624cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 132724cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 132824cdb843SMatthew G. Knepley } 1329*bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 133024cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 133124cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 133224cdb843SMatthew G. Knepley const PetscInt point = points[p]; 133324cdb843SMatthew G. Knepley PetscScalar *x = NULL; 133424cdb843SMatthew G. Knepley PetscInt i; 133524cdb843SMatthew G. Knepley 133624cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 133724cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1338*bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1339*bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n); 1340*bbce034cSMatthew 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); 134124cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 134224cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 134324cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 134424cdb843SMatthew G. Knepley if (X_t) { 134524cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 134624cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 134724cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 134824cdb843SMatthew G. Knepley } 134924cdb843SMatthew G. Knepley ++f; 135024cdb843SMatthew G. Knepley } 135124cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 135224cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 135324cdb843SMatthew G. Knepley PetscFE fe; 135424cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 135524cdb843SMatthew G. Knepley /* Conforming batches */ 135624cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 135724cdb843SMatthew G. Knepley /* Remainder */ 135824cdb843SMatthew G. Knepley PetscInt Nr, offset; 135924cdb843SMatthew G. Knepley 136024cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 136124cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 136224cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 136324cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 136424cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 136524cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 136624cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 136724cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 136824cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 136924cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 137024cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 137124cdb843SMatthew G. Knepley offset = numFaces - Nr; 137224cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 1373*bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr); 1374*bbce034cSMatthew 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); 137524cdb843SMatthew G. Knepley } 137624cdb843SMatthew G. Knepley } 137724cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 137824cdb843SMatthew G. Knepley const PetscInt point = points[p]; 137924cdb843SMatthew G. Knepley 138024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 138124cdb843SMatthew G. Knepley if (dep != dim-1) continue; 138224cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 138324cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 138424cdb843SMatthew G. Knepley ++f; 138524cdb843SMatthew G. Knepley } 138624cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 138724cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1388*bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 138924cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 139024cdb843SMatthew G. Knepley } 139124cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 139224cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 139324cdb843SMatthew G. Knepley if (mesh->printFEM) { 139424cdb843SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 139524cdb843SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 139624cdb843SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 139724cdb843SMatthew G. Knepley } 139824cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 139924cdb843SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 140024cdb843SMatthew G. Knepley if (isShell) { 140124cdb843SMatthew G. Knepley JacActionCtx *jctx; 140224cdb843SMatthew G. Knepley 140324cdb843SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 140424cdb843SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 140524cdb843SMatthew G. Knepley } 140624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 140724cdb843SMatthew G. Knepley } 140824cdb843SMatthew G. Knepley 140924cdb843SMatthew G. Knepley #undef __FUNCT__ 141024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 141124cdb843SMatthew G. Knepley /*@ 141224cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 141324cdb843SMatthew G. Knepley 141424cdb843SMatthew G. Knepley Input Parameters: 141524cdb843SMatthew G. Knepley + dm - The mesh 141624cdb843SMatthew G. Knepley . X - Local input vector 141724cdb843SMatthew G. Knepley - user - The user context 141824cdb843SMatthew G. Knepley 141924cdb843SMatthew G. Knepley Output Parameter: 142024cdb843SMatthew G. Knepley . Jac - Jacobian matrix 142124cdb843SMatthew G. Knepley 142224cdb843SMatthew G. Knepley Note: 142324cdb843SMatthew G. Knepley The first member of the user context must be an FEMContext. 142424cdb843SMatthew G. Knepley 142524cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 142624cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 142724cdb843SMatthew G. Knepley 142824cdb843SMatthew G. Knepley Level: developer 142924cdb843SMatthew G. Knepley 143024cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 143124cdb843SMatthew G. Knepley @*/ 143224cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 143324cdb843SMatthew G. Knepley { 143424cdb843SMatthew G. Knepley PetscErrorCode ierr; 143524cdb843SMatthew G. Knepley 143624cdb843SMatthew G. Knepley PetscFunctionBegin; 143724cdb843SMatthew G. Knepley ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 143824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 143924cdb843SMatthew G. Knepley } 1440