1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2af0996ceSBarry Smith #include <petsc/private/snesimpl.h> /*I "petscsnes.h" I*/ 324cdb843SMatthew G. Knepley #include <petscds.h> 4af0996ceSBarry Smith #include <petsc/private/petscimpl.h> 5af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 6552f7358SJed Brown 724cdb843SMatthew G. Knepley /************************** Interpolation *******************************/ 824cdb843SMatthew G. Knepley 9552f7358SJed Brown #undef __FUNCT__ 106da023fcSToby Isaac #define __FUNCT__ "DMSNESConvertPlex" 116da023fcSToby Isaac static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy) 126da023fcSToby Isaac { 136da023fcSToby Isaac PetscBool isPlex; 146da023fcSToby Isaac PetscErrorCode ierr; 156da023fcSToby Isaac 166da023fcSToby Isaac PetscFunctionBegin; 176da023fcSToby Isaac ierr = PetscObjectTypeCompare((PetscObject)dm,DMPLEX,&isPlex);CHKERRQ(ierr); 186da023fcSToby Isaac if (isPlex) { 196da023fcSToby Isaac *plex = dm; 206da023fcSToby Isaac ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 216da023fcSToby Isaac } 226da023fcSToby Isaac else { 236da023fcSToby Isaac ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 246da023fcSToby Isaac if (copy) { 256da023fcSToby Isaac PetscInt i; 266da023fcSToby Isaac PetscObject obj; 276da023fcSToby Isaac const char *comps[3] = {"A","dmAux","dmCh"}; 286da023fcSToby Isaac 296da023fcSToby Isaac ierr = DMCopyDMSNES(dm,*plex);CHKERRQ(ierr); 306da023fcSToby Isaac for (i = 0; i < 3; i++) { 316da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject)dm,comps[i],&obj);CHKERRQ(ierr); 326da023fcSToby Isaac ierr = PetscObjectCompose((PetscObject)*plex,comps[i],obj);CHKERRQ(ierr); 336da023fcSToby Isaac } 346da023fcSToby Isaac } 356da023fcSToby Isaac } 366da023fcSToby Isaac PetscFunctionReturn(0); 376da023fcSToby Isaac } 386da023fcSToby Isaac 396da023fcSToby Isaac #undef __FUNCT__ 40552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate" 410adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 420adebc6cSBarry Smith { 43552f7358SJed Brown PetscErrorCode ierr; 44552f7358SJed Brown 45552f7358SJed Brown PetscFunctionBegin; 46552f7358SJed Brown PetscValidPointer(ctx, 2); 47552f7358SJed Brown ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr); 481aa26658SKarl Rupp 49552f7358SJed Brown (*ctx)->comm = comm; 50552f7358SJed Brown (*ctx)->dim = -1; 51552f7358SJed Brown (*ctx)->nInput = 0; 520298fd71SBarry Smith (*ctx)->points = NULL; 530298fd71SBarry Smith (*ctx)->cells = NULL; 54552f7358SJed Brown (*ctx)->n = -1; 550298fd71SBarry Smith (*ctx)->coords = NULL; 56552f7358SJed Brown PetscFunctionReturn(0); 57552f7358SJed Brown } 58552f7358SJed Brown 59552f7358SJed Brown #undef __FUNCT__ 60552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim" 610adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 620adebc6cSBarry Smith { 63552f7358SJed Brown PetscFunctionBegin; 640adebc6cSBarry Smith if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 65552f7358SJed Brown ctx->dim = dim; 66552f7358SJed Brown PetscFunctionReturn(0); 67552f7358SJed Brown } 68552f7358SJed Brown 69552f7358SJed Brown #undef __FUNCT__ 70552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim" 710adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 720adebc6cSBarry Smith { 73552f7358SJed Brown PetscFunctionBegin; 74552f7358SJed Brown PetscValidIntPointer(dim, 2); 75552f7358SJed Brown *dim = ctx->dim; 76552f7358SJed Brown PetscFunctionReturn(0); 77552f7358SJed Brown } 78552f7358SJed Brown 79552f7358SJed Brown #undef __FUNCT__ 80552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof" 810adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 820adebc6cSBarry Smith { 83552f7358SJed Brown PetscFunctionBegin; 840adebc6cSBarry Smith if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 85552f7358SJed Brown ctx->dof = dof; 86552f7358SJed Brown PetscFunctionReturn(0); 87552f7358SJed Brown } 88552f7358SJed Brown 89552f7358SJed Brown #undef __FUNCT__ 90552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof" 910adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 920adebc6cSBarry Smith { 93552f7358SJed Brown PetscFunctionBegin; 94552f7358SJed Brown PetscValidIntPointer(dof, 2); 95552f7358SJed Brown *dof = ctx->dof; 96552f7358SJed Brown PetscFunctionReturn(0); 97552f7358SJed Brown } 98552f7358SJed Brown 99552f7358SJed Brown #undef __FUNCT__ 100552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints" 1010adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 1020adebc6cSBarry Smith { 103552f7358SJed Brown PetscErrorCode ierr; 104552f7358SJed Brown 105552f7358SJed Brown PetscFunctionBegin; 1060adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 1070adebc6cSBarry Smith if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 108552f7358SJed Brown ctx->nInput = n; 1091aa26658SKarl Rupp 110785e854fSJed Brown ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 111552f7358SJed Brown ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 112552f7358SJed Brown PetscFunctionReturn(0); 113552f7358SJed Brown } 114552f7358SJed Brown 115552f7358SJed Brown #undef __FUNCT__ 116552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp" 1170adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 1180adebc6cSBarry Smith { 119552f7358SJed Brown MPI_Comm comm = ctx->comm; 120552f7358SJed Brown PetscScalar *a; 121552f7358SJed Brown PetscInt p, q, i; 122552f7358SJed Brown PetscMPIInt rank, size; 123552f7358SJed Brown PetscErrorCode ierr; 124552f7358SJed Brown Vec pointVec; 125552f7358SJed Brown IS cellIS; 126552f7358SJed Brown PetscLayout layout; 127552f7358SJed Brown PetscReal *globalPoints; 128cb313848SJed Brown PetscScalar *globalPointsScalar; 129552f7358SJed Brown const PetscInt *ranges; 130552f7358SJed Brown PetscMPIInt *counts, *displs; 131552f7358SJed Brown const PetscInt *foundCells; 132552f7358SJed Brown PetscMPIInt *foundProcs, *globalProcs; 13319436ca2SJed Brown PetscInt n, N; 134552f7358SJed Brown 13519436ca2SJed Brown PetscFunctionBegin; 13619436ca2SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 13719436ca2SJed Brown ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 13819436ca2SJed Brown ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 1390adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 14019436ca2SJed Brown /* Locate points */ 14119436ca2SJed Brown n = ctx->nInput; 142552f7358SJed Brown if (!redundantPoints) { 143552f7358SJed Brown ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 144552f7358SJed Brown ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 145552f7358SJed Brown ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 146552f7358SJed Brown ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 147552f7358SJed Brown ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 148552f7358SJed Brown /* Communicate all points to all processes */ 149dcca6d9dSJed Brown ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 150552f7358SJed Brown ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 151552f7358SJed Brown for (p = 0; p < size; ++p) { 152552f7358SJed Brown counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 153552f7358SJed Brown displs[p] = ranges[p]*ctx->dim; 154552f7358SJed Brown } 155552f7358SJed Brown ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 156552f7358SJed Brown } else { 157552f7358SJed Brown N = n; 158552f7358SJed Brown globalPoints = ctx->points; 15938ea73c8SJed Brown counts = displs = NULL; 16038ea73c8SJed Brown layout = NULL; 161552f7358SJed Brown } 162552f7358SJed Brown #if 0 163dcca6d9dSJed Brown ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 16419436ca2SJed Brown /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 165552f7358SJed Brown #else 166cb313848SJed Brown #if defined(PETSC_USE_COMPLEX) 167785e854fSJed Brown ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr); 168cb313848SJed Brown for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i]; 169cb313848SJed Brown #else 170cb313848SJed Brown globalPointsScalar = globalPoints; 171cb313848SJed Brown #endif 17204706141SMatthew G Knepley ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 173dcca6d9dSJed Brown ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 174552f7358SJed Brown ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr); 175552f7358SJed Brown ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr); 176552f7358SJed Brown #endif 177552f7358SJed Brown for (p = 0; p < N; ++p) { 1781aa26658SKarl Rupp if (foundCells[p] >= 0) foundProcs[p] = rank; 1791aa26658SKarl Rupp else foundProcs[p] = size; 180552f7358SJed Brown } 181552f7358SJed Brown /* Let the lowest rank process own each point */ 182b2566f29SBarry Smith ierr = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 183552f7358SJed Brown ctx->n = 0; 184552f7358SJed Brown for (p = 0; p < N; ++p) { 1850adebc6cSBarry 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); 1861aa26658SKarl Rupp else if (globalProcs[p] == rank) ctx->n++; 187552f7358SJed Brown } 188552f7358SJed Brown /* Create coordinates vector and array of owned cells */ 189785e854fSJed Brown ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 190552f7358SJed Brown ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 191552f7358SJed Brown ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 192552f7358SJed Brown ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 193c0dedaeaSBarry Smith ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 194552f7358SJed Brown ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 195552f7358SJed Brown for (p = 0, q = 0, i = 0; p < N; ++p) { 196552f7358SJed Brown if (globalProcs[p] == rank) { 197552f7358SJed Brown PetscInt d; 198552f7358SJed Brown 1991aa26658SKarl Rupp for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 200552f7358SJed Brown ctx->cells[q++] = foundCells[p]; 201552f7358SJed Brown } 202552f7358SJed Brown } 203552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 204552f7358SJed Brown #if 0 205552f7358SJed Brown ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 206552f7358SJed Brown #else 207552f7358SJed Brown ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 208552f7358SJed Brown ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr); 209552f7358SJed Brown ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 210552f7358SJed Brown ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 211552f7358SJed Brown #endif 212cb313848SJed Brown if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 213d343d804SMatthew G. Knepley if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 214552f7358SJed Brown ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 215552f7358SJed Brown PetscFunctionReturn(0); 216552f7358SJed Brown } 217552f7358SJed Brown 218552f7358SJed Brown #undef __FUNCT__ 219552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates" 2200adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 2210adebc6cSBarry Smith { 222552f7358SJed Brown PetscFunctionBegin; 223552f7358SJed Brown PetscValidPointer(coordinates, 2); 2240adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 225552f7358SJed Brown *coordinates = ctx->coords; 226552f7358SJed Brown PetscFunctionReturn(0); 227552f7358SJed Brown } 228552f7358SJed Brown 229552f7358SJed Brown #undef __FUNCT__ 230552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector" 2310adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 2320adebc6cSBarry Smith { 233552f7358SJed Brown PetscErrorCode ierr; 234552f7358SJed Brown 235552f7358SJed Brown PetscFunctionBegin; 236552f7358SJed Brown PetscValidPointer(v, 2); 2370adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 238552f7358SJed Brown ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 239552f7358SJed Brown ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 240552f7358SJed Brown ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 241c0dedaeaSBarry Smith ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 242552f7358SJed Brown PetscFunctionReturn(0); 243552f7358SJed Brown } 244552f7358SJed Brown 245552f7358SJed Brown #undef __FUNCT__ 246552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector" 2470adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 2480adebc6cSBarry Smith { 249552f7358SJed Brown PetscErrorCode ierr; 250552f7358SJed Brown 251552f7358SJed Brown PetscFunctionBegin; 252552f7358SJed Brown PetscValidPointer(v, 2); 2530adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 254552f7358SJed Brown ierr = VecDestroy(v);CHKERRQ(ierr); 255552f7358SJed Brown PetscFunctionReturn(0); 256552f7358SJed Brown } 257552f7358SJed Brown 258552f7358SJed Brown #undef __FUNCT__ 2597a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private" 2607a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 261a6dfd86eSKarl Rupp { 262552f7358SJed Brown PetscReal *v0, *J, *invJ, detJ; 26356044e6dSMatthew G. Knepley const PetscScalar *coords; 26456044e6dSMatthew G. Knepley PetscScalar *a; 265552f7358SJed Brown PetscInt p; 266552f7358SJed Brown PetscErrorCode ierr; 267552f7358SJed Brown 268552f7358SJed Brown PetscFunctionBegin; 269dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 27056044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 271552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 272552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 273552f7358SJed Brown PetscInt c = ctx->cells[p]; 274a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 275552f7358SJed Brown PetscReal xi[4]; 276552f7358SJed Brown PetscInt d, f, comp; 277552f7358SJed Brown 2788e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 279552f7358SJed Brown if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2800298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2811aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2821aa26658SKarl Rupp 283552f7358SJed Brown for (d = 0; d < ctx->dim; ++d) { 284552f7358SJed Brown xi[d] = 0.0; 2851aa26658SKarl 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]); 2861aa26658SKarl 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]; 287552f7358SJed Brown } 2880298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 289552f7358SJed Brown } 290552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 29156044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 292552f7358SJed Brown ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 293552f7358SJed Brown PetscFunctionReturn(0); 294552f7358SJed Brown } 295552f7358SJed Brown 296552f7358SJed Brown #undef __FUNCT__ 2977a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private" 2987a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 2997a1931ceSMatthew G. Knepley { 3007a1931ceSMatthew G. Knepley PetscReal *v0, *J, *invJ, detJ; 30156044e6dSMatthew G. Knepley const PetscScalar *coords; 30256044e6dSMatthew G. Knepley PetscScalar *a; 3037a1931ceSMatthew G. Knepley PetscInt p; 3047a1931ceSMatthew G. Knepley PetscErrorCode ierr; 3057a1931ceSMatthew G. Knepley 3067a1931ceSMatthew G. Knepley PetscFunctionBegin; 307dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 30856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 3097a1931ceSMatthew G. Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 3107a1931ceSMatthew G. Knepley for (p = 0; p < ctx->n; ++p) { 3117a1931ceSMatthew G. Knepley PetscInt c = ctx->cells[p]; 3127a1931ceSMatthew G. Knepley const PetscInt order[3] = {2, 1, 3}; 3132584bbe8SMatthew G. Knepley PetscScalar *x = NULL; 3147a1931ceSMatthew G. Knepley PetscReal xi[4]; 3157a1931ceSMatthew G. Knepley PetscInt d, f, comp; 3167a1931ceSMatthew G. Knepley 3178e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 3187a1931ceSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 3197a1931ceSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 3207a1931ceSMatthew G. Knepley for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 3217a1931ceSMatthew G. Knepley 3227a1931ceSMatthew G. Knepley for (d = 0; d < ctx->dim; ++d) { 3237a1931ceSMatthew G. Knepley xi[d] = 0.0; 3247a1931ceSMatthew 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]); 3257a1931ceSMatthew 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]; 3267a1931ceSMatthew G. Knepley } 3277a1931ceSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 3287a1931ceSMatthew G. Knepley } 3297a1931ceSMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 33056044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 3317a1931ceSMatthew G. Knepley ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 3327a1931ceSMatthew G. Knepley PetscFunctionReturn(0); 3337a1931ceSMatthew G. Knepley } 3347a1931ceSMatthew G. Knepley 3357a1931ceSMatthew G. Knepley #undef __FUNCT__ 336552f7358SJed Brown #define __FUNCT__ "QuadMap_Private" 3375820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 338552f7358SJed Brown { 339552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 340552f7358SJed Brown const PetscScalar x0 = vertices[0]; 341552f7358SJed Brown const PetscScalar y0 = vertices[1]; 342552f7358SJed Brown const PetscScalar x1 = vertices[2]; 343552f7358SJed Brown const PetscScalar y1 = vertices[3]; 344552f7358SJed Brown const PetscScalar x2 = vertices[4]; 345552f7358SJed Brown const PetscScalar y2 = vertices[5]; 346552f7358SJed Brown const PetscScalar x3 = vertices[6]; 347552f7358SJed Brown const PetscScalar y3 = vertices[7]; 348552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 349552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 350552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 351552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 352552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 353552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 35456044e6dSMatthew G. Knepley const PetscScalar *ref; 35556044e6dSMatthew G. Knepley PetscScalar *real; 356552f7358SJed Brown PetscErrorCode ierr; 357552f7358SJed Brown 358552f7358SJed Brown PetscFunctionBegin; 35956044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 360552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 361552f7358SJed Brown { 362552f7358SJed Brown const PetscScalar p0 = ref[0]; 363552f7358SJed Brown const PetscScalar p1 = ref[1]; 364552f7358SJed Brown 365552f7358SJed Brown real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 366552f7358SJed Brown real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 367552f7358SJed Brown } 368552f7358SJed Brown ierr = PetscLogFlops(28);CHKERRQ(ierr); 36956044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 370552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 371552f7358SJed Brown PetscFunctionReturn(0); 372552f7358SJed Brown } 373552f7358SJed Brown 374af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 375552f7358SJed Brown #undef __FUNCT__ 376552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private" 377d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 378552f7358SJed Brown { 379552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 380552f7358SJed Brown const PetscScalar x0 = vertices[0]; 381552f7358SJed Brown const PetscScalar y0 = vertices[1]; 382552f7358SJed Brown const PetscScalar x1 = vertices[2]; 383552f7358SJed Brown const PetscScalar y1 = vertices[3]; 384552f7358SJed Brown const PetscScalar x2 = vertices[4]; 385552f7358SJed Brown const PetscScalar y2 = vertices[5]; 386552f7358SJed Brown const PetscScalar x3 = vertices[6]; 387552f7358SJed Brown const PetscScalar y3 = vertices[7]; 388552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 389552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 39056044e6dSMatthew G. Knepley const PetscScalar *ref; 391552f7358SJed Brown PetscErrorCode ierr; 392552f7358SJed Brown 393552f7358SJed Brown PetscFunctionBegin; 39456044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 395552f7358SJed Brown { 396552f7358SJed Brown const PetscScalar x = ref[0]; 397552f7358SJed Brown const PetscScalar y = ref[1]; 398552f7358SJed Brown const PetscInt rows[2] = {0, 1}; 399da80777bSKarl Rupp PetscScalar values[4]; 400da80777bSKarl Rupp 401da80777bSKarl Rupp values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 402da80777bSKarl Rupp values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 40394ab13aaSBarry Smith ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 404552f7358SJed Brown } 405552f7358SJed Brown ierr = PetscLogFlops(30);CHKERRQ(ierr); 40656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 40794ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 40894ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 409552f7358SJed Brown PetscFunctionReturn(0); 410552f7358SJed Brown } 411552f7358SJed Brown 412552f7358SJed Brown #undef __FUNCT__ 413552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private" 414a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 415a6dfd86eSKarl Rupp { 416fafc0619SMatthew G Knepley DM dmCoord; 417552f7358SJed Brown SNES snes; 418552f7358SJed Brown KSP ksp; 419552f7358SJed Brown PC pc; 420552f7358SJed Brown Vec coordsLocal, r, ref, real; 421552f7358SJed Brown Mat J; 42256044e6dSMatthew G. Knepley const PetscScalar *coords; 42356044e6dSMatthew G. Knepley PetscScalar *a; 424552f7358SJed Brown PetscInt p; 425552f7358SJed Brown PetscErrorCode ierr; 426552f7358SJed Brown 427552f7358SJed Brown PetscFunctionBegin; 428552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 429fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 430552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 431552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 432552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 433552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 434c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 435552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 436552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 437552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 438552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 439552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 440552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 4410298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 4420298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 443552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 444552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 445552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 446552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 447552f7358SJed Brown 44856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 449552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 450552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 451a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 452552f7358SJed Brown PetscScalar *xi; 453cb313848SJed Brown PetscReal xir[2]; 454552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 455552f7358SJed Brown 456552f7358SJed Brown /* Can make this do all points at once */ 4570298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4580adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 4590298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 4600adebc6cSBarry Smith if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof); 4610298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 4620298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 463552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 464552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 465552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 466552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 467552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 468552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 469cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 470cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 4711aa26658SKarl 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]; 4721aa26658SKarl Rupp 473552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 4740298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4750298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 476552f7358SJed Brown } 477552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 47856044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 479552f7358SJed Brown 480552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 481552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 482552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 483552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 484552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 485552f7358SJed Brown PetscFunctionReturn(0); 486552f7358SJed Brown } 487552f7358SJed Brown 488552f7358SJed Brown #undef __FUNCT__ 489552f7358SJed Brown #define __FUNCT__ "HexMap_Private" 4905820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 491552f7358SJed Brown { 492552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 493552f7358SJed Brown const PetscScalar x0 = vertices[0]; 494552f7358SJed Brown const PetscScalar y0 = vertices[1]; 495552f7358SJed Brown const PetscScalar z0 = vertices[2]; 4967a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 4977a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 4987a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 499552f7358SJed Brown const PetscScalar x2 = vertices[6]; 500552f7358SJed Brown const PetscScalar y2 = vertices[7]; 501552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5027a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5037a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5047a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 505552f7358SJed Brown const PetscScalar x4 = vertices[12]; 506552f7358SJed Brown const PetscScalar y4 = vertices[13]; 507552f7358SJed Brown const PetscScalar z4 = vertices[14]; 508552f7358SJed Brown const PetscScalar x5 = vertices[15]; 509552f7358SJed Brown const PetscScalar y5 = vertices[16]; 510552f7358SJed Brown const PetscScalar z5 = vertices[17]; 511552f7358SJed Brown const PetscScalar x6 = vertices[18]; 512552f7358SJed Brown const PetscScalar y6 = vertices[19]; 513552f7358SJed Brown const PetscScalar z6 = vertices[20]; 514552f7358SJed Brown const PetscScalar x7 = vertices[21]; 515552f7358SJed Brown const PetscScalar y7 = vertices[22]; 516552f7358SJed Brown const PetscScalar z7 = vertices[23]; 517552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 518552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 519552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 520552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 521552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 522552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 523552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 524552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 525552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 526552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 527552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 528552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 529552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 530552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 531552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 532552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 533552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 534552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 535552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 536552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 537552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 53856044e6dSMatthew G. Knepley const PetscScalar *ref; 53956044e6dSMatthew G. Knepley PetscScalar *real; 540552f7358SJed Brown PetscErrorCode ierr; 541552f7358SJed Brown 542552f7358SJed Brown PetscFunctionBegin; 54356044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 544552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 545552f7358SJed Brown { 546552f7358SJed Brown const PetscScalar p0 = ref[0]; 547552f7358SJed Brown const PetscScalar p1 = ref[1]; 548552f7358SJed Brown const PetscScalar p2 = ref[2]; 549552f7358SJed Brown 550552f7358SJed 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; 551552f7358SJed 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; 552552f7358SJed 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; 553552f7358SJed Brown } 554552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 55556044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 556552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 557552f7358SJed Brown PetscFunctionReturn(0); 558552f7358SJed Brown } 559552f7358SJed Brown 560552f7358SJed Brown #undef __FUNCT__ 561552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private" 562d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 563552f7358SJed Brown { 564552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 565552f7358SJed Brown const PetscScalar x0 = vertices[0]; 566552f7358SJed Brown const PetscScalar y0 = vertices[1]; 567552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5687a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5697a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5707a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 571552f7358SJed Brown const PetscScalar x2 = vertices[6]; 572552f7358SJed Brown const PetscScalar y2 = vertices[7]; 573552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5747a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5757a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5767a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 577552f7358SJed Brown const PetscScalar x4 = vertices[12]; 578552f7358SJed Brown const PetscScalar y4 = vertices[13]; 579552f7358SJed Brown const PetscScalar z4 = vertices[14]; 580552f7358SJed Brown const PetscScalar x5 = vertices[15]; 581552f7358SJed Brown const PetscScalar y5 = vertices[16]; 582552f7358SJed Brown const PetscScalar z5 = vertices[17]; 583552f7358SJed Brown const PetscScalar x6 = vertices[18]; 584552f7358SJed Brown const PetscScalar y6 = vertices[19]; 585552f7358SJed Brown const PetscScalar z6 = vertices[20]; 586552f7358SJed Brown const PetscScalar x7 = vertices[21]; 587552f7358SJed Brown const PetscScalar y7 = vertices[22]; 588552f7358SJed Brown const PetscScalar z7 = vertices[23]; 589552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 590552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 591552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 592552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 593552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 594552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 595552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 596552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 597552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 598552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 599552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 600552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 60156044e6dSMatthew G. Knepley const PetscScalar *ref; 602552f7358SJed Brown PetscErrorCode ierr; 603552f7358SJed Brown 604552f7358SJed Brown PetscFunctionBegin; 60556044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 606552f7358SJed Brown { 607552f7358SJed Brown const PetscScalar x = ref[0]; 608552f7358SJed Brown const PetscScalar y = ref[1]; 609552f7358SJed Brown const PetscScalar z = ref[2]; 610552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 611da80777bSKarl Rupp PetscScalar values[9]; 612da80777bSKarl Rupp 613da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 614da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 615da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 616da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 617da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 618da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 619da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 620da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 621da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 6221aa26658SKarl Rupp 62394ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 624552f7358SJed Brown } 625552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 62656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 62794ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 62894ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 629552f7358SJed Brown PetscFunctionReturn(0); 630552f7358SJed Brown } 631552f7358SJed Brown 632552f7358SJed Brown #undef __FUNCT__ 633552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private" 634a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 635a6dfd86eSKarl Rupp { 636fafc0619SMatthew G Knepley DM dmCoord; 637552f7358SJed Brown SNES snes; 638552f7358SJed Brown KSP ksp; 639552f7358SJed Brown PC pc; 640552f7358SJed Brown Vec coordsLocal, r, ref, real; 641552f7358SJed Brown Mat J; 64256044e6dSMatthew G. Knepley const PetscScalar *coords; 64356044e6dSMatthew G. Knepley PetscScalar *a; 644552f7358SJed Brown PetscInt p; 645552f7358SJed Brown PetscErrorCode ierr; 646552f7358SJed Brown 647552f7358SJed Brown PetscFunctionBegin; 648552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 649fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 650552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 651552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 652552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 653552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 654c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 655552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 656552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 657552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 658552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 659552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 660552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 6610298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 6620298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 663552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 664552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 665552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 666552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 667552f7358SJed Brown 66856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 669552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 670552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 671a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 672552f7358SJed Brown PetscScalar *xi; 673cb313848SJed Brown PetscReal xir[3]; 674552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 675552f7358SJed Brown 676552f7358SJed Brown /* Can make this do all points at once */ 6770298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6780adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 6790298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 6800adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 6810298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 6820298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 683552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 684552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 685552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 686552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 687552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 688552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 689552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 690cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 691cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 692cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 693552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 694552f7358SJed Brown a[p*ctx->dof+comp] = 695cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 6967a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 697cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 6987a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 699cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 700cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 701cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 702cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 703552f7358SJed Brown } 704552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 7050298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 7060298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 707552f7358SJed Brown } 708552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 70956044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 710552f7358SJed Brown 711552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 712552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 713552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 714552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 715552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 716552f7358SJed Brown PetscFunctionReturn(0); 717552f7358SJed Brown } 718552f7358SJed Brown 719552f7358SJed Brown #undef __FUNCT__ 720552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate" 721552f7358SJed Brown /* 722552f7358SJed Brown Input Parameters: 723552f7358SJed Brown + ctx - The DMInterpolationInfo context 724552f7358SJed Brown . dm - The DM 725552f7358SJed Brown - x - The local vector containing the field to be interpolated 726552f7358SJed Brown 727552f7358SJed Brown Output Parameters: 728552f7358SJed Brown . v - The vector containing the interpolated values 729552f7358SJed Brown */ 7300adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 7310adebc6cSBarry Smith { 732552f7358SJed Brown PetscInt dim, coneSize, n; 733552f7358SJed Brown PetscErrorCode ierr; 734552f7358SJed Brown 735552f7358SJed Brown PetscFunctionBegin; 736552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 737552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 738552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 739552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 7400adebc6cSBarry 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); 741552f7358SJed Brown if (n) { 742c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 743552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 744552f7358SJed Brown if (dim == 2) { 745552f7358SJed Brown if (coneSize == 3) { 7467a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 747552f7358SJed Brown } else if (coneSize == 4) { 748552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 7490adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 750552f7358SJed Brown } else if (dim == 3) { 751552f7358SJed Brown if (coneSize == 4) { 7527a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 753552f7358SJed Brown } else { 754552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 755552f7358SJed Brown } 7560adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 757552f7358SJed Brown } 758552f7358SJed Brown PetscFunctionReturn(0); 759552f7358SJed Brown } 760552f7358SJed Brown 761552f7358SJed Brown #undef __FUNCT__ 762552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy" 7630adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 7640adebc6cSBarry Smith { 765552f7358SJed Brown PetscErrorCode ierr; 766552f7358SJed Brown 767552f7358SJed Brown PetscFunctionBegin; 768552f7358SJed Brown PetscValidPointer(ctx, 2); 769552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 770552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 771552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 772552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 7730298fd71SBarry Smith *ctx = NULL; 774552f7358SJed Brown PetscFunctionReturn(0); 775552f7358SJed Brown } 776cc0c4584SMatthew G. Knepley 777cc0c4584SMatthew G. Knepley #undef __FUNCT__ 778cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields" 779cc0c4584SMatthew G. Knepley /*@C 780cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 781cc0c4584SMatthew G. Knepley 782cc0c4584SMatthew G. Knepley Collective on SNES 783cc0c4584SMatthew G. Knepley 784cc0c4584SMatthew G. Knepley Input Parameters: 785cc0c4584SMatthew G. Knepley + snes - the SNES context 786cc0c4584SMatthew G. Knepley . its - iteration number 787cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 7884d4332d5SBarry Smith - dummy - PetscViewer of type ASCII 789cc0c4584SMatthew G. Knepley 790cc0c4584SMatthew G. Knepley Notes: 791cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 792cc0c4584SMatthew G. Knepley 793cc0c4584SMatthew G. Knepley Level: intermediate 794cc0c4584SMatthew G. Knepley 795cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 796cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 797cc0c4584SMatthew G. Knepley @*/ 798cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy) 799cc0c4584SMatthew G. Knepley { 8004d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 801cc0c4584SMatthew G. Knepley Vec res; 802cc0c4584SMatthew G. Knepley DM dm; 803cc0c4584SMatthew G. Knepley PetscSection s; 804cc0c4584SMatthew G. Knepley const PetscScalar *r; 805cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 806cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 807cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 808cc0c4584SMatthew G. Knepley 809cc0c4584SMatthew G. Knepley PetscFunctionBegin; 8104d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 811cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 812cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 813cc0c4584SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 814cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 815cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 816cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 817cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 818cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 819cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 820cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 821cc0c4584SMatthew G. Knepley 822cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 823cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 824cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 825cc0c4584SMatthew G. Knepley } 826cc0c4584SMatthew G. Knepley } 827cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 828b2566f29SBarry Smith ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 829cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 830cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 831cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 832cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 833cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 834cc0c4584SMatthew G. Knepley } 835cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 836cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 837cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 838cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 839cc0c4584SMatthew G. Knepley } 84024cdb843SMatthew G. Knepley 84124cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 84224cdb843SMatthew G. Knepley 84324cdb843SMatthew G. Knepley #undef __FUNCT__ 8447d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 8457d4028c8SMatthew G. Knepley /*@ 8467d4028c8SMatthew G. Knepley DMPlexSNESGetGeometryFEM - Return precomputed geometric data 8477d4028c8SMatthew G. Knepley 8487d4028c8SMatthew G. Knepley Input Parameter: 8497d4028c8SMatthew G. Knepley . dm - The DM 8507d4028c8SMatthew G. Knepley 8517d4028c8SMatthew G. Knepley Output Parameters: 8527d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 8537d4028c8SMatthew G. Knepley 8547d4028c8SMatthew G. Knepley Level: developer 8557d4028c8SMatthew G. Knepley 8567d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal() 8577d4028c8SMatthew G. Knepley @*/ 8587d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 8597d4028c8SMatthew G. Knepley { 8607d4028c8SMatthew G. Knepley DMSNES dmsnes; 8617d4028c8SMatthew G. Knepley PetscObject obj; 8627d4028c8SMatthew G. Knepley PetscErrorCode ierr; 8637d4028c8SMatthew G. Knepley 8647d4028c8SMatthew G. Knepley PetscFunctionBegin; 8657d4028c8SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8667d4028c8SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 8677d4028c8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 8687d4028c8SMatthew G. Knepley if (!obj) { 8697d4028c8SMatthew G. Knepley Vec cellgeom; 8707d4028c8SMatthew G. Knepley 8717d4028c8SMatthew G. Knepley ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 8727d4028c8SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 8737d4028c8SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 8747d4028c8SMatthew G. Knepley } 8757d4028c8SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 8767d4028c8SMatthew G. Knepley PetscFunctionReturn(0); 8777d4028c8SMatthew G. Knepley } 8787d4028c8SMatthew G. Knepley 8797d4028c8SMatthew G. Knepley #undef __FUNCT__ 88008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM" 88108449791SMatthew G. Knepley /*@ 88208449791SMatthew G. Knepley DMPlexSNESGetGeometryFVM - Return precomputed geometric data 88308449791SMatthew G. Knepley 88408449791SMatthew G. Knepley Input Parameter: 88508449791SMatthew G. Knepley . dm - The DM 88608449791SMatthew G. Knepley 88708449791SMatthew G. Knepley Output Parameters: 88808449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry 88908449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 89008449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 89108449791SMatthew G. Knepley 89208449791SMatthew G. Knepley Level: developer 89308449791SMatthew G. Knepley 89408449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 89508449791SMatthew G. Knepley @*/ 89608449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 89724cdb843SMatthew G. Knepley { 89808449791SMatthew G. Knepley DMSNES dmsnes; 89908449791SMatthew G. Knepley PetscObject obj; 90024cdb843SMatthew G. Knepley PetscErrorCode ierr; 90124cdb843SMatthew G. Knepley 90224cdb843SMatthew G. Knepley PetscFunctionBegin; 90308449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 90408449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 90508449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr); 90608449791SMatthew G. Knepley if (!obj) { 90708449791SMatthew G. Knepley Vec cellgeom, facegeom; 90824cdb843SMatthew G. Knepley 90908449791SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr); 91008449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr); 91108449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr); 91208449791SMatthew G. Knepley ierr = VecDestroy(&facegeom);CHKERRQ(ierr); 91308449791SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 91408449791SMatthew G. Knepley } 91508449791SMatthew G. Knepley if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);} 91608449791SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);} 91708449791SMatthew G. Knepley if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);} 91824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 91924cdb843SMatthew G. Knepley } 92024cdb843SMatthew G. Knepley 92124cdb843SMatthew G. Knepley #undef __FUNCT__ 92208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM" 923dbd489d2SMatthew G. Knepley /*@ 92408449791SMatthew G. Knepley DMPlexSNESGetGradientDM - Return gradient data layout 92508449791SMatthew G. Knepley 92608449791SMatthew G. Knepley Input Parameters: 92708449791SMatthew G. Knepley + dm - The DM 92808449791SMatthew G. Knepley - fv - The PetscFV 92908449791SMatthew G. Knepley 93008449791SMatthew G. Knepley Output Parameter: 93108449791SMatthew G. Knepley . dmGrad - The layout for gradient values 93208449791SMatthew G. Knepley 93308449791SMatthew G. Knepley Level: developer 93408449791SMatthew G. Knepley 93508449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 93608449791SMatthew G. Knepley @*/ 93708449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 93824cdb843SMatthew G. Knepley { 93908449791SMatthew G. Knepley DMSNES dmsnes; 94008449791SMatthew G. Knepley PetscObject obj; 94108449791SMatthew G. Knepley PetscBool computeGradients; 94224cdb843SMatthew G. Knepley PetscErrorCode ierr; 94324cdb843SMatthew G. Knepley 94424cdb843SMatthew G. Knepley PetscFunctionBegin; 94508449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 94608449791SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 94708449791SMatthew G. Knepley PetscValidPointer(dmGrad,3); 94808449791SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 94908449791SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 95008449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 95108449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr); 95208449791SMatthew G. Knepley if (!obj) { 95308449791SMatthew G. Knepley DM dmGrad; 95408449791SMatthew G. Knepley Vec faceGeometry, cellGeometry; 95508449791SMatthew G. Knepley 95608449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr); 95708449791SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr); 95808449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr); 95908449791SMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 96008449791SMatthew G. Knepley } 96108449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr); 96208449791SMatthew G. Knepley PetscFunctionReturn(0); 96308449791SMatthew G. Knepley } 96408449791SMatthew G. Knepley 96508449791SMatthew G. Knepley #undef __FUNCT__ 96608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields" 96708449791SMatthew G. Knepley /*@C 96808449791SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 96908449791SMatthew G. Knepley 97008449791SMatthew G. Knepley Input Parameters: 97108449791SMatthew G. Knepley + dm - The DM 97208449791SMatthew G. Knepley . cStart - The first cell to include 97308449791SMatthew G. Knepley . cEnd - The first cell to exclude 97408449791SMatthew G. Knepley . locX - A local vector with the solution fields 97508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 97608449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 97708449791SMatthew G. Knepley 97808449791SMatthew G. Knepley Output Parameters: 97908449791SMatthew G. Knepley + u - The field coefficients 98008449791SMatthew G. Knepley . u_t - The fields derivative coefficients 98108449791SMatthew G. Knepley - a - The auxiliary field coefficients 98208449791SMatthew G. Knepley 98308449791SMatthew G. Knepley Level: developer 98408449791SMatthew G. Knepley 98508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 98608449791SMatthew G. Knepley @*/ 98708449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 98808449791SMatthew G. Knepley { 98908449791SMatthew G. Knepley DM dmAux; 99008449791SMatthew G. Knepley PetscSection section, sectionAux; 99108449791SMatthew G. Knepley PetscDS prob; 99208449791SMatthew G. Knepley PetscInt numCells = cEnd - cStart, totDim, totDimAux, c; 99308449791SMatthew G. Knepley PetscErrorCode ierr; 99408449791SMatthew G. Knepley 99508449791SMatthew G. Knepley PetscFunctionBegin; 99608449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 99708449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 99808449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 99908449791SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 100008449791SMatthew G. Knepley PetscValidPointer(u, 7); 100108449791SMatthew G. Knepley PetscValidPointer(u_t, 8); 100208449791SMatthew G. Knepley PetscValidPointer(a, 9); 100324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 100424cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 100524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 100608449791SMatthew G. Knepley if (locA) { 100708449791SMatthew G. Knepley PetscDS probAux; 100808449791SMatthew G. Knepley 100908449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 101024cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 101124cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 101224cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 101324cdb843SMatthew G. Knepley } 101408449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr); 101549073227SMatthew G. Knepley if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 101649073227SMatthew G. Knepley if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 101724cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 101808449791SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 101924cdb843SMatthew G. Knepley PetscInt i; 102024cdb843SMatthew G. Knepley 102108449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 10229f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i]; 102308449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 102408449791SMatthew G. Knepley if (locX_t) { 102508449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 10269f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i]; 102708449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 102824cdb843SMatthew G. Knepley } 102908449791SMatthew G. Knepley if (locA) { 10306da023fcSToby Isaac DM dmAuxPlex; 10316da023fcSToby Isaac 10326da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 10336da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10349f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i]; 10356da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10366da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 103724cdb843SMatthew G. Knepley } 103824cdb843SMatthew G. Knepley } 103908449791SMatthew G. Knepley PetscFunctionReturn(0); 104008449791SMatthew G. Knepley } 104124cdb843SMatthew G. Knepley 104208449791SMatthew G. Knepley #undef __FUNCT__ 104308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields" 104408449791SMatthew G. Knepley /*@C 104508449791SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 104608449791SMatthew G. Knepley 104708449791SMatthew G. Knepley Input Parameters: 104808449791SMatthew G. Knepley + dm - The DM 104908449791SMatthew G. Knepley . cStart - The first cell to include 105008449791SMatthew G. Knepley . cEnd - The first cell to exclude 105108449791SMatthew G. Knepley . locX - A local vector with the solution fields 105208449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 105308449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 105408449791SMatthew G. Knepley 105508449791SMatthew G. Knepley Output Parameters: 105608449791SMatthew G. Knepley + u - The field coefficients 105708449791SMatthew G. Knepley . u_t - The fields derivative coefficients 105808449791SMatthew G. Knepley - a - The auxiliary field coefficients 105908449791SMatthew G. Knepley 106008449791SMatthew G. Knepley Level: developer 106108449791SMatthew G. Knepley 106208449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 106308449791SMatthew G. Knepley @*/ 106408449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 106508449791SMatthew G. Knepley { 106608449791SMatthew G. Knepley PetscErrorCode ierr; 106708449791SMatthew G. Knepley 106808449791SMatthew G. Knepley PetscFunctionBegin; 106908449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr); 107049073227SMatthew G. Knepley if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);} 107149073227SMatthew G. Knepley if (*a) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);} 107208449791SMatthew G. Knepley PetscFunctionReturn(0); 107324cdb843SMatthew G. Knepley } 107408449791SMatthew G. Knepley 107508449791SMatthew G. Knepley #undef __FUNCT__ 107608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields" 107708449791SMatthew G. Knepley /*@C 107808449791SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 107908449791SMatthew G. Knepley 108008449791SMatthew G. Knepley Input Parameters: 108108449791SMatthew G. Knepley + dm - The DM 108208449791SMatthew G. Knepley . fStart - The first face to include 108308449791SMatthew G. Knepley . fEnd - The first face to exclude 108408449791SMatthew G. Knepley . locX - A local vector with the solution fields 108508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 108608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 108708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 108808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 108908449791SMatthew G. Knepley 109008449791SMatthew G. Knepley Output Parameters: 1091477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1092477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 109308449791SMatthew G. Knepley 109408449791SMatthew G. Knepley Level: developer 109508449791SMatthew G. Knepley 109608449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 109708449791SMatthew G. Knepley @*/ 109808449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 109908449791SMatthew G. Knepley { 110008449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 1101195142f5SMatthew G. Knepley PetscSection section; 110208449791SMatthew G. Knepley PetscDS prob; 110308449791SMatthew G. Knepley DMLabel ghostLabel; 110408449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1105477f7dfdSMatthew G. Knepley PetscBool *isFE; 1106477f7dfdSMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 110708449791SMatthew G. Knepley PetscErrorCode ierr; 110808449791SMatthew G. Knepley 110908449791SMatthew G. Knepley PetscFunctionBegin; 111008449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 111108449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 111208449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 111308449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 111408449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 111508449791SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 111608449791SMatthew G. Knepley PetscValidPointer(uL, 9); 111708449791SMatthew G. Knepley PetscValidPointer(uR, 10); 111808449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 111908449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1120195142f5SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1121477f7dfdSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1122477f7dfdSMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1123477f7dfdSMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1124477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1125477f7dfdSMatthew G. Knepley PetscObject obj; 1126477f7dfdSMatthew G. Knepley PetscClassId id; 1127477f7dfdSMatthew G. Knepley 1128477f7dfdSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 1129477f7dfdSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1130477f7dfdSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1131477f7dfdSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1132477f7dfdSMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 1133477f7dfdSMatthew G. Knepley } 1134c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 113508449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 113608449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 113708449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 113808449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 113908449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 114008449791SMatthew G. Knepley if (locGrad) { 114108449791SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 114208449791SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 114324cdb843SMatthew G. Knepley } 1144477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr); 1145477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr); 1146477f7dfdSMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 114708449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 114808449791SMatthew G. Knepley const PetscInt *cells; 114908449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 115008449791SMatthew G. Knepley const PetscFVCellGeom *cgL, *cgR; 115108449791SMatthew G. Knepley const PetscScalar *xL, *xR, *gL, *gR; 115208449791SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 1153e697831aSToby Isaac PetscInt ghost, nsupp; 115408449791SMatthew G. Knepley 115508449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1156e697831aSToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 115708449791SMatthew G. Knepley if (ghost >= 0) continue; 115808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 115908449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 116008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 116108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1162477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1163477f7dfdSMatthew G. Knepley PetscInt off; 1164477f7dfdSMatthew G. Knepley 116537a43ebbSMatthew G. Knepley ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1166477f7dfdSMatthew G. Knepley if (isFE[f]) { 1167477f7dfdSMatthew G. Knepley const PetscInt *cone; 1168cca9989dSMatthew G. Knepley PetscInt comp, coneSize, faceLocL, faceLocR, ldof, rdof, d; 1169477f7dfdSMatthew G. Knepley 1170cca9989dSMatthew G. Knepley xL = xR = NULL; 1171cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1172cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1173477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 1174477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr); 1175477f7dfdSMatthew G. Knepley for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break; 1176477f7dfdSMatthew G. Knepley if (faceLocL == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[0]); 1177477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 1178477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr); 1179477f7dfdSMatthew G. Knepley for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break; 1180477f7dfdSMatthew G. Knepley if (faceLocR == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[1]); 1181195142f5SMatthew G. Knepley /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 1182477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 1183cca9989dSMatthew G. Knepley if (rdof == ldof) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 1184cca9989dSMatthew G. Knepley else {ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];} 1185cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1186cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1187477f7dfdSMatthew G. Knepley } else { 1188477f7dfdSMatthew G. Knepley PetscFV fv; 1189477f7dfdSMatthew G. Knepley PetscInt numComp, c; 1190477f7dfdSMatthew G. Knepley 1191477f7dfdSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1192477f7dfdSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1193e697831aSToby Isaac if (nsupp > 2) { 1194e697831aSToby Isaac for (f = 0; f < Nf; ++f) { 1195e697831aSToby Isaac PetscInt off; 1196e697831aSToby Isaac 1197e697831aSToby Isaac ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1198e697831aSToby Isaac ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1199e697831aSToby Isaac for (c = 0; c < numComp; ++c) { 1200e697831aSToby Isaac uLl[iface*Nc+off+c] = 0.; 1201e697831aSToby Isaac uRl[iface*Nc+off+c] = 0.; 1202e697831aSToby Isaac } 1203e697831aSToby Isaac } 1204e697831aSToby Isaac continue; 1205e697831aSToby Isaac } 1206cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1207cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 120808449791SMatthew G. Knepley if (dmGrad) { 120908449791SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 121008449791SMatthew G. Knepley 121108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 121208449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 121308449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 121408449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1215477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1216477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1217477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 121808449791SMatthew G. Knepley } 121908449791SMatthew G. Knepley } else { 1220477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1221477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 1222477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 1223477f7dfdSMatthew G. Knepley } 1224477f7dfdSMatthew G. Knepley } 122508449791SMatthew G. Knepley } 122608449791SMatthew G. Knepley } 122708449791SMatthew G. Knepley ++iface; 122808449791SMatthew G. Knepley } 122908449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 123008449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 123108449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 123208449791SMatthew G. Knepley if (locGrad) { 123308449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 123408449791SMatthew G. Knepley } 1235477f7dfdSMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 123608449791SMatthew G. Knepley PetscFunctionReturn(0); 123708449791SMatthew G. Knepley } 123808449791SMatthew G. Knepley 123908449791SMatthew G. Knepley #undef __FUNCT__ 124008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields" 124108449791SMatthew G. Knepley /*@C 124208449791SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 124308449791SMatthew G. Knepley 124408449791SMatthew G. Knepley Input Parameters: 124508449791SMatthew G. Knepley + dm - The DM 124608449791SMatthew G. Knepley . fStart - The first face to include 124708449791SMatthew G. Knepley . fEnd - The first face to exclude 124808449791SMatthew G. Knepley . locX - A local vector with the solution fields 124908449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 125008449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 125108449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 125208449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 125308449791SMatthew G. Knepley 125408449791SMatthew G. Knepley Output Parameters: 1255477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1256477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 125708449791SMatthew G. Knepley 125808449791SMatthew G. Knepley Level: developer 125908449791SMatthew G. Knepley 126008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 126108449791SMatthew G. Knepley @*/ 126208449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR) 126308449791SMatthew G. Knepley { 126408449791SMatthew G. Knepley PetscErrorCode ierr; 126508449791SMatthew G. Knepley 126608449791SMatthew G. Knepley PetscFunctionBegin; 126708449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr); 126808449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr); 126908449791SMatthew G. Knepley PetscFunctionReturn(0); 127008449791SMatthew G. Knepley } 127108449791SMatthew G. Knepley 127208449791SMatthew G. Knepley #undef __FUNCT__ 127308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry" 127408449791SMatthew G. Knepley /*@C 127508449791SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 127608449791SMatthew G. Knepley 127708449791SMatthew G. Knepley Input Parameters: 127808449791SMatthew G. Knepley + dm - The DM 127908449791SMatthew G. Knepley . fStart - The first face to include 128008449791SMatthew G. Knepley . fEnd - The first face to exclude 128108449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 128208449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 128308449791SMatthew G. Knepley 128408449791SMatthew G. Knepley Output Parameters: 128508449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 128608449791SMatthew G. Knepley - vol - The cell volume 128708449791SMatthew G. Knepley 128808449791SMatthew G. Knepley Level: developer 128908449791SMatthew G. Knepley 129008449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 129108449791SMatthew G. Knepley @*/ 129208449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 129308449791SMatthew G. Knepley { 129408449791SMatthew G. Knepley DM dmFace, dmCell; 129508449791SMatthew G. Knepley DMLabel ghostLabel; 129608449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 129708449791SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 129808449791SMatthew G. Knepley PetscErrorCode ierr; 129908449791SMatthew G. Knepley 130008449791SMatthew G. Knepley PetscFunctionBegin; 130108449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 130208449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 130308449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 130408449791SMatthew G. Knepley PetscValidPointer(fgeom, 6); 130508449791SMatthew G. Knepley PetscValidPointer(vol, 7); 130608449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1307c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 130808449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 130908449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 131008449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 131108449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 131208449791SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 131308449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr); 131408449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 131508449791SMatthew G. Knepley const PetscInt *cells; 131608449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 131708449791SMatthew G. Knepley const PetscFVCellGeom *cgL, *cgR; 131808449791SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 13192eefff9cSMatthew G. Knepley PetscReal *voll = *vol; 132008449791SMatthew G. Knepley PetscInt ghost, d; 132108449791SMatthew G. Knepley 132208449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 132308449791SMatthew G. Knepley if (ghost >= 0) continue; 132408449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 132508449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 132608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 132708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 132808449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 132908449791SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 133008449791SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 133108449791SMatthew G. Knepley } 133208449791SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 133308449791SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 133408449791SMatthew G. Knepley ++iface; 133508449791SMatthew G. Knepley } 133608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 133708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 133808449791SMatthew G. Knepley PetscFunctionReturn(0); 133908449791SMatthew G. Knepley } 134008449791SMatthew G. Knepley 134108449791SMatthew G. Knepley #undef __FUNCT__ 134208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry" 134308449791SMatthew G. Knepley /*@C 134408449791SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 134508449791SMatthew G. Knepley 134608449791SMatthew G. Knepley Input Parameters: 134708449791SMatthew G. Knepley + dm - The DM 134808449791SMatthew G. Knepley . fStart - The first face to include 134908449791SMatthew G. Knepley . fEnd - The first face to exclude 135008449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 135108449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 135208449791SMatthew G. Knepley 135308449791SMatthew G. Knepley Output Parameters: 135408449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 135508449791SMatthew G. Knepley - vol - The cell volume 135608449791SMatthew G. Knepley 135708449791SMatthew G. Knepley Level: developer 135808449791SMatthew G. Knepley 135908449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 136008449791SMatthew G. Knepley @*/ 136108449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 136208449791SMatthew G. Knepley { 136308449791SMatthew G. Knepley PetscErrorCode ierr; 136408449791SMatthew G. Knepley 136508449791SMatthew G. Knepley PetscFunctionBegin; 136608449791SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 136708449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr); 136808449791SMatthew G. Knepley PetscFunctionReturn(0); 136908449791SMatthew G. Knepley } 137008449791SMatthew G. Knepley 137108449791SMatthew G. Knepley #undef __FUNCT__ 1372cd0a786dSToby Isaac #define __FUNCT__ "DMPlexApplyLimiter_Internal" 1373655c4201SToby Isaac static PetscErrorCode DMPlexApplyLimiter_Internal (DM dm, DM dmCell, PetscLimiter lim, PetscInt dim, PetscInt totDim, PetscInt cell, PetscInt face, PetscInt fStart, PetscInt fEnd, PetscReal *cellPhi, const PetscScalar *x, 1374cd0a786dSToby Isaac const PetscScalar *cellgeom, const PetscFVCellGeom *cg, const PetscScalar *cx, const PetscScalar *cgrad) 1375cd0a786dSToby Isaac { 1376cd0a786dSToby Isaac const PetscInt *children; 1377cd0a786dSToby Isaac PetscInt numChildren; 1378cd0a786dSToby Isaac PetscErrorCode ierr; 1379cd0a786dSToby Isaac 1380cd0a786dSToby Isaac PetscFunctionBegin; 1381cd0a786dSToby Isaac ierr = DMPlexGetTreeChildren(dm,face,&numChildren,&children);CHKERRQ(ierr); 1382cd0a786dSToby Isaac if (numChildren) { 1383cd0a786dSToby Isaac PetscInt c; 1384cd0a786dSToby Isaac 1385cd0a786dSToby Isaac for (c = 0; c < numChildren; c++) { 1386cd0a786dSToby Isaac PetscInt childFace = children[c]; 1387655c4201SToby Isaac 1388655c4201SToby Isaac if (childFace >= fStart && childFace < fEnd) { 1389655c4201SToby Isaac ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,childFace,fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 1390655c4201SToby Isaac } 1391cd0a786dSToby Isaac } 1392cd0a786dSToby Isaac } 1393cd0a786dSToby Isaac else { 1394cd0a786dSToby Isaac const PetscScalar *ncx; 1395cd0a786dSToby Isaac const PetscFVCellGeom *ncg; 1396cd0a786dSToby Isaac const PetscInt *fcells; 1397cd0a786dSToby Isaac PetscInt ncell, d; 1398cd0a786dSToby Isaac PetscReal v[3]; 1399cd0a786dSToby Isaac 1400cd0a786dSToby Isaac ierr = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr); 1401cd0a786dSToby Isaac ncell = cell == fcells[0] ? fcells[1] : fcells[0]; 1402cd0a786dSToby Isaac ierr = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr); 1403cd0a786dSToby Isaac ierr = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr); 1404cd0a786dSToby Isaac DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v); 1405cd0a786dSToby Isaac for (d = 0; d < totDim; ++d) { 1406cd0a786dSToby Isaac /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */ 1407cd0a786dSToby Isaac PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v); 1408cd0a786dSToby Isaac 1409cd0a786dSToby Isaac ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr); 1410cd0a786dSToby Isaac cellPhi[d] = PetscMin(cellPhi[d], phi); 1411cd0a786dSToby Isaac } 1412cd0a786dSToby Isaac } 1413cd0a786dSToby Isaac PetscFunctionReturn(0); 1414cd0a786dSToby Isaac } 1415cd0a786dSToby Isaac 1416cd0a786dSToby Isaac #undef __FUNCT__ 141708449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal" 141808449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad) 141908449791SMatthew G. Knepley { 142008449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 142108449791SMatthew G. Knepley DMLabel ghostLabel; 142208449791SMatthew G. Knepley PetscDS prob; 142308449791SMatthew G. Knepley PetscFV fvm; 142408449791SMatthew G. Knepley PetscLimiter lim; 142508449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x; 142608449791SMatthew G. Knepley PetscScalar *gr; 142708449791SMatthew G. Knepley PetscReal *cellPhi; 142808449791SMatthew G. Knepley PetscInt dim, face, cell, totDim, cStart, cEnd, cEndInterior; 142908449791SMatthew G. Knepley PetscErrorCode ierr; 143008449791SMatthew G. Knepley 143108449791SMatthew G. Knepley PetscFunctionBegin; 143208449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 143308449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 143408449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1435c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 143608449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr); 143708449791SMatthew G. Knepley ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr); 143808449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 143908449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 144008449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 144108449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 144208449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 144308449791SMatthew G. Knepley ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr); 144408449791SMatthew G. Knepley ierr = VecZeroEntries(grad);CHKERRQ(ierr); 144508449791SMatthew G. Knepley ierr = VecGetArray(grad, &gr);CHKERRQ(ierr); 144608449791SMatthew G. Knepley /* Reconstruct gradients */ 144708449791SMatthew G. Knepley for (face = fStart; face < fEnd; ++face) { 144808449791SMatthew G. Knepley const PetscInt *cells; 144908449791SMatthew G. Knepley const PetscFVFaceGeom *fg; 145008449791SMatthew G. Knepley const PetscScalar *cx[2]; 145108449791SMatthew G. Knepley PetscScalar *cgrad[2]; 145208449791SMatthew G. Knepley PetscBool boundary; 145350d63984SToby Isaac PetscInt ghost, c, pd, d, numChildren, numCells; 145408449791SMatthew G. Knepley 145508449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1456a6ba4734SToby Isaac ierr = DMIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr); 145750d63984SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &numChildren, NULL);CHKERRQ(ierr); 145850d63984SToby Isaac if (ghost >= 0 || boundary || numChildren) continue; 145950d63984SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &numCells);CHKERRQ(ierr); 146050d63984SToby Isaac if (numCells != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "facet %d has %d support points: expected 2",face,numCells); 146108449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 146208449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 146308449791SMatthew G. Knepley for (c = 0; c < 2; ++c) { 146408449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr); 146508449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr); 146608449791SMatthew G. Knepley } 146708449791SMatthew G. Knepley for (pd = 0; pd < totDim; ++pd) { 146808449791SMatthew G. Knepley PetscScalar delta = cx[1][pd] - cx[0][pd]; 146908449791SMatthew G. Knepley 147008449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 147108449791SMatthew G. Knepley if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta; 147208449791SMatthew G. Knepley if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta; 147308449791SMatthew G. Knepley } 147408449791SMatthew G. Knepley } 147508449791SMatthew G. Knepley } 147608449791SMatthew G. Knepley /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */ 147708449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 147808449791SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 147908449791SMatthew G. Knepley cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior; 148008449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 148108449791SMatthew G. Knepley for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) { 148208449791SMatthew G. Knepley const PetscInt *faces; 148308449791SMatthew G. Knepley const PetscScalar *cx; 148408449791SMatthew G. Knepley const PetscFVCellGeom *cg; 148508449791SMatthew G. Knepley PetscScalar *cgrad; 148608449791SMatthew G. Knepley PetscInt coneSize, f, pd, d; 148708449791SMatthew G. Knepley 148808449791SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 148908449791SMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr); 149008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr); 149108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr); 149208449791SMatthew G. Knepley ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr); 149308449791SMatthew G. Knepley if (!cgrad) continue; /* Unowned overlap cell, we do not compute */ 149408449791SMatthew G. Knepley /* Limiter will be minimum value over all neighbors */ 149508449791SMatthew G. Knepley for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL; 149608449791SMatthew G. Knepley for (f = 0; f < coneSize; ++f) { 1497655c4201SToby Isaac ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,faces[f],fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr); 149808449791SMatthew G. Knepley } 149908449791SMatthew G. Knepley /* Apply limiter to gradient */ 150008449791SMatthew G. Knepley for (pd = 0; pd < totDim; ++pd) 150108449791SMatthew G. Knepley /* Scalar limiter applied to each component separately */ 150208449791SMatthew G. Knepley for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd]; 150308449791SMatthew G. Knepley } 150408449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr); 150508449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 150608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 150708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 150808449791SMatthew G. Knepley ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr); 150908449791SMatthew G. Knepley PetscFunctionReturn(0); 151008449791SMatthew G. Knepley } 151108449791SMatthew G. Knepley 151208449791SMatthew G. Knepley #undef __FUNCT__ 151308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal" 151408449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user) 151508449791SMatthew G. Knepley { 151608449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 151708449791SMatthew G. Knepley PetscSection section; 151808449791SMatthew G. Knepley PetscDS prob; 151908449791SMatthew G. Knepley DMLabel depth; 152008449791SMatthew G. Knepley PetscFECellGeom *cgeom; 152108449791SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *elemVec = NULL; 152208449791SMatthew G. Knepley PetscInt dim, Nf, f, totDimBd, numBd, bd; 152308449791SMatthew G. Knepley PetscErrorCode ierr; 152408449791SMatthew G. Knepley 152508449791SMatthew G. Knepley PetscFunctionBegin; 152608449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 152708449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 152808449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 152908449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 153008449791SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 153124cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 1532a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 153324cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 153424cdb843SMatthew G. Knepley const char *bdLabel; 153524cdb843SMatthew G. Knepley DMLabel label; 153624cdb843SMatthew G. Knepley IS pointIS; 153724cdb843SMatthew G. Knepley const PetscInt *points; 153824cdb843SMatthew G. Knepley const PetscInt *values; 153924cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 154024cdb843SMatthew G. Knepley PetscBool isEssential; 154124cdb843SMatthew G. Knepley 1542a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 154324cdb843SMatthew G. Knepley if (isEssential) continue; 154424cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 1545c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 154624cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 154724cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 154824cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 154924cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 155024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 155124cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 155224cdb843SMatthew G. Knepley } 1553bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 155408449791SMatthew G. Knepley if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 155524cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 155624cdb843SMatthew G. Knepley const PetscInt point = points[p]; 155724cdb843SMatthew G. Knepley PetscScalar *x = NULL; 155824cdb843SMatthew G. Knepley PetscInt i; 155924cdb843SMatthew G. Knepley 156024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 156124cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1562bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1563302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 1564bbce034cSMatthew 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); 1565*929b1830SToby Isaac /* TODO: Matt, this is wrong if feBd does not match fe: i.e., if the order differs. */ 156608449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 156724cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 156808449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 156908449791SMatthew G. Knepley if (locX_t) { 157008449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 157124cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 157208449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 157324cdb843SMatthew G. Knepley } 157424cdb843SMatthew G. Knepley ++f; 157524cdb843SMatthew G. Knepley } 157624cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 157724cdb843SMatthew G. Knepley PetscFE fe; 157808449791SMatthew G. Knepley PetscQuadrature q; 157924cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 158024cdb843SMatthew G. Knepley /* Conforming batches */ 158124cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 158224cdb843SMatthew G. Knepley /* Remainder */ 158324cdb843SMatthew G. Knepley PetscInt Nr, offset; 158424cdb843SMatthew G. Knepley 158524cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 158624cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 158724cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 158824cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 158924cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 159024cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 159124cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 159224cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 159324cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 159424cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 159524cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 159624cdb843SMatthew G. Knepley offset = numFaces - Nr; 1597bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr); 1598bbce034cSMatthew 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); 159924cdb843SMatthew G. Knepley } 160024cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 160124cdb843SMatthew G. Knepley const PetscInt point = points[p]; 160224cdb843SMatthew G. Knepley 160324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 160424cdb843SMatthew G. Knepley if (dep != dim-1) continue; 160524cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 160608449791SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr); 160724cdb843SMatthew G. Knepley ++f; 160824cdb843SMatthew G. Knepley } 160924cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 161024cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1611bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 161208449791SMatthew G. Knepley if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 161324cdb843SMatthew G. Knepley } 161408449791SMatthew G. Knepley PetscFunctionReturn(0); 161508449791SMatthew G. Knepley } 161608449791SMatthew G. Knepley 161708449791SMatthew G. Knepley #undef __FUNCT__ 1618f2c5ccc7SToby Isaac #define __FUNCT__ "DMPlexReconstructGradientsFVM" 1619f2c5ccc7SToby Isaac /*@ 1620f2c5ccc7SToby Isaac DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method. 1621f2c5ccc7SToby Isaac 1622f2c5ccc7SToby Isaac Input Parameters: 1623f2c5ccc7SToby Isaac + dm - the mesh 1624f2c5ccc7SToby Isaac - locX - the local representation of the vector 1625f2c5ccc7SToby Isaac 1626f2c5ccc7SToby Isaac Output Parameter: 1627f2c5ccc7SToby Isaac . grad - the global representation of the gradient 1628f2c5ccc7SToby Isaac 1629f2c5ccc7SToby Isaac Level: developer 1630f2c5ccc7SToby Isaac 1631f2c5ccc7SToby Isaac .seealso: DMPlexSNESGetGradientDM() 1632f2c5ccc7SToby Isaac @*/ 1633f2c5ccc7SToby Isaac PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad) 1634f2c5ccc7SToby Isaac { 1635f2c5ccc7SToby Isaac PetscDS prob; 1636f2c5ccc7SToby Isaac PetscInt Nf, f, fStart, fEnd; 1637f2c5ccc7SToby Isaac PetscBool useFVM; 1638f2c5ccc7SToby Isaac PetscFV fvm = NULL; 1639f2c5ccc7SToby Isaac Vec faceGeometryFVM, cellGeometryFVM; 1640f2c5ccc7SToby Isaac PetscFVCellGeom *cgeomFVM = NULL; 1641f2c5ccc7SToby Isaac PetscFVFaceGeom *fgeomFVM = NULL; 1642f2c5ccc7SToby Isaac DM dmGrad = NULL; 1643f2c5ccc7SToby Isaac PetscErrorCode ierr; 1644f2c5ccc7SToby Isaac 1645f2c5ccc7SToby Isaac PetscFunctionBegin; 1646f2c5ccc7SToby Isaac ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1647f2c5ccc7SToby Isaac ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1648f2c5ccc7SToby Isaac for (f = 0; f < Nf; ++f) { 1649f2c5ccc7SToby Isaac PetscObject obj; 1650f2c5ccc7SToby Isaac PetscClassId id; 1651f2c5ccc7SToby Isaac 1652f2c5ccc7SToby Isaac ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1653f2c5ccc7SToby Isaac ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1654f2c5ccc7SToby Isaac if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1655f2c5ccc7SToby Isaac } 1656f2c5ccc7SToby Isaac if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization"); 1657f2c5ccc7SToby Isaac ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 1658f2c5ccc7SToby Isaac if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients"); 1659f2c5ccc7SToby Isaac ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 1660f2c5ccc7SToby Isaac ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1661f2c5ccc7SToby Isaac ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1662f2c5ccc7SToby Isaac ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1663f2c5ccc7SToby Isaac ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1664f2c5ccc7SToby Isaac PetscFunctionReturn(0); 1665f2c5ccc7SToby Isaac } 1666f2c5ccc7SToby Isaac 1667f2c5ccc7SToby Isaac #undef __FUNCT__ 166808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal" 1669c4d4a4f8SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user) 167008449791SMatthew G. Knepley { 167108449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 167208449791SMatthew G. Knepley const char *name = "Residual"; 167308449791SMatthew G. Knepley DM dmAux = NULL; 167408449791SMatthew G. Knepley DM dmGrad = NULL; 167508449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 167608449791SMatthew G. Knepley PetscDS prob = NULL; 167708449791SMatthew G. Knepley PetscDS probAux = NULL; 167808449791SMatthew G. Knepley PetscSection section = NULL; 167908449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 168008449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 1681b2666ceaSMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 168208449791SMatthew G. Knepley PetscFV fvm = NULL; 168308449791SMatthew G. Knepley PetscFECellGeom *cgeomFEM = NULL; 16842f84e9bcSToby Isaac PetscScalar *cgeomScal; 168508449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 168608449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 168708449791SMatthew G. Knepley Vec locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 168808449791SMatthew G. Knepley PetscScalar *u, *u_t, *a, *uL, *uR; 1689c4d4a4f8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 169008449791SMatthew G. Knepley PetscErrorCode ierr; 169108449791SMatthew G. Knepley 169208449791SMatthew G. Knepley PetscFunctionBegin; 169308449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 169408449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1695195142f5SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 169608449791SMatthew G. Knepley /* FEM+FVM */ 169708449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 169808449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1699c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 170008449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 170108449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 170208449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 170308449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 170408449791SMatthew G. Knepley if (locA) { 170508449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 170608449791SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 170708449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 170808449791SMatthew G. Knepley } 170908449791SMatthew G. Knepley /* 2: Get geometric data */ 171008449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 171108449791SMatthew G. Knepley PetscObject obj; 171208449791SMatthew G. Knepley PetscClassId id; 17137173168dSMatthew G. Knepley PetscBool fimp; 171408449791SMatthew G. Knepley 17157173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 17167173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 171708449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 171808449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 171908449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 172008449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 172108449791SMatthew G. Knepley } 172208449791SMatthew G. Knepley if (useFEM) { 172308449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr); 17242f84e9bcSToby Isaac ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr); 17252f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 17262f84e9bcSToby Isaac DM dmCell; 17272f84e9bcSToby Isaac PetscInt c; 17282f84e9bcSToby Isaac 17292f84e9bcSToby Isaac ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr); 17302f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr); 17312f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 17322f84e9bcSToby Isaac PetscScalar *thisgeom; 17332f84e9bcSToby Isaac 17342f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 17352f84e9bcSToby Isaac cgeomFEM[c] = *((PetscFECellGeom *) thisgeom); 17362f84e9bcSToby Isaac } 17372f84e9bcSToby Isaac } 17382f84e9bcSToby Isaac else { 17392f84e9bcSToby Isaac cgeomFEM = (PetscFECellGeom *) cgeomScal; 17402f84e9bcSToby Isaac } 174108449791SMatthew G. Knepley } 174208449791SMatthew G. Knepley if (useFVM) { 174308449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 174408449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 174508449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 174608449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 174708449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 174808449791SMatthew G. Knepley if (dmGrad) { 174908449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 175008449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 175108449791SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 175208449791SMatthew G. Knepley /* Communicate gradient values */ 175308449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 175408449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 175508449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 175608449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 175708449791SMatthew G. Knepley } 175808449791SMatthew G. Knepley } 175908449791SMatthew G. Knepley /* Handle boundary values */ 176008449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 176108449791SMatthew G. Knepley /* Loop over chunks */ 176208449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 176308449791SMatthew G. Knepley numChunks = 1; 176408449791SMatthew G. Knepley cellChunkSize = (cEnd - cStart)/numChunks; 176508449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 176608449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 17672eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 17682eefff9cSMatthew G. Knepley PetscReal *vol; 176908449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 177008449791SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell; 177108449791SMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face; 177208449791SMatthew G. Knepley 177308449791SMatthew G. Knepley /* Extract field coefficients */ 177408449791SMatthew G. Knepley if (useFEM) { 177508449791SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 177608449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1777215c4595SMatthew G. Knepley ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 177808449791SMatthew G. Knepley } 177908449791SMatthew G. Knepley if (useFVM) { 178008449791SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 178108449791SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 178208449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 178308449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1784215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1785215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 178608449791SMatthew G. Knepley } 178708449791SMatthew G. Knepley /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 178808449791SMatthew G. Knepley /* Loop over fields */ 178908449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 179008449791SMatthew G. Knepley PetscObject obj; 179108449791SMatthew G. Knepley PetscClassId id; 17927173168dSMatthew G. Knepley PetscBool fimp; 179308449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 179408449791SMatthew G. Knepley 17957173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 17967173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 179708449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 179808449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 179908449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 180008449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 180108449791SMatthew G. Knepley PetscQuadrature q; 180208449791SMatthew G. Knepley PetscInt Nq, Nb; 180308449791SMatthew G. Knepley 180408449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 180508449791SMatthew G. Knepley 180608449791SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 180708449791SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 180808449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 180908449791SMatthew G. Knepley blockSize = Nb*Nq; 181008449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 181108449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 181208449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 181308449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 181408449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 181508449791SMatthew G. Knepley offset = numCells - Nr; 181608449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 181708449791SMatthew G. Knepley /* For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */ 181808449791SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 181908449791SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 182008449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 182108449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 182208449791SMatthew G. Knepley 182308449791SMatthew G. Knepley Ne = numFaces; 182408449791SMatthew G. Knepley Nr = 0; 182508449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 182608449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 182708449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 182808449791SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 182908449791SMatthew G. Knepley } 18302f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 18312f84e9bcSToby Isaac ierr = PetscFree(cgeomFEM);CHKERRQ(ierr); 18322f84e9bcSToby Isaac } 18332f84e9bcSToby Isaac else { 18342f84e9bcSToby Isaac cgeomFEM = NULL; 18352f84e9bcSToby Isaac } 18360163fd50SMatthew G. Knepley if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);} 183708449791SMatthew G. Knepley /* Loop over domain */ 183808449791SMatthew G. Knepley if (useFEM) { 183908449791SMatthew G. Knepley /* Add elemVec to locX */ 184008449791SMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 184108449791SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);} 184208449791SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_VALUES);CHKERRQ(ierr); 184308449791SMatthew G. Knepley } 184408449791SMatthew G. Knepley } 184508449791SMatthew G. Knepley if (useFVM) { 18464a394323SMatthew G. Knepley PetscScalar *fa; 184708449791SMatthew G. Knepley PetscInt iface; 184808449791SMatthew G. Knepley 184908449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1850c10b5f1bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1851c10b5f1bSMatthew G. Knepley PetscFV fv; 1852c10b5f1bSMatthew G. Knepley PetscObject obj; 1853c10b5f1bSMatthew G. Knepley PetscClassId id; 18544a394323SMatthew G. Knepley PetscInt foff, pdim; 1855c10b5f1bSMatthew G. Knepley 1856c10b5f1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1857c10b5f1bSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1858c10b5f1bSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1859c10b5f1bSMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1860c10b5f1bSMatthew G. Knepley fv = (PetscFV) obj; 1861c10b5f1bSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1862c10b5f1bSMatthew G. Knepley /* Accumulate fluxes to cells */ 186308449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 186408449791SMatthew G. Knepley const PetscInt *cells; 186508449791SMatthew G. Knepley PetscScalar *fL, *fR; 1866ffe9ad51SToby Isaac PetscInt ghost, d, nsupp; 186708449791SMatthew G. Knepley 186808449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1869ffe9ad51SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 1870ffe9ad51SToby Isaac if (ghost >= 0 || nsupp > 2) continue; 187108449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1872c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr); 1873c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr); 1874c10b5f1bSMatthew G. Knepley for (d = 0; d < pdim; ++d) { 1875c10b5f1bSMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1876c10b5f1bSMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 187708449791SMatthew G. Knepley } 187808449791SMatthew G. Knepley ++iface; 187908449791SMatthew G. Knepley } 1880dab51205SMatthew G. Knepley } 1881dab51205SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1882dab51205SMatthew G. Knepley } 1883c10b5f1bSMatthew G. Knepley /* Handle time derivative */ 1884c10b5f1bSMatthew G. Knepley if (locX_t) { 1885dab51205SMatthew G. Knepley PetscScalar *x_t, *fa; 1886dab51205SMatthew G. Knepley 1887dab51205SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1888c10b5f1bSMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1889dab51205SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1890dab51205SMatthew G. Knepley PetscFV fv; 1891dab51205SMatthew G. Knepley PetscObject obj; 1892dab51205SMatthew G. Knepley PetscClassId id; 1893dab51205SMatthew G. Knepley PetscInt pdim, d; 1894dab51205SMatthew G. Knepley 1895dab51205SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1896dab51205SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1897dab51205SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1898dab51205SMatthew G. Knepley fv = (PetscFV) obj; 1899dab51205SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1900c10b5f1bSMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 1901c10b5f1bSMatthew G. Knepley PetscScalar *u_t, *r; 1902c10b5f1bSMatthew G. Knepley 1903c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1904c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1905d63b37e5SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1906c10b5f1bSMatthew G. Knepley } 1907dab51205SMatthew G. Knepley } 1908c10b5f1bSMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 190908449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 191008449791SMatthew G. Knepley } 191108449791SMatthew G. Knepley if (useFEM) { 191208449791SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 191308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 191408449791SMatthew G. Knepley } 191508449791SMatthew G. Knepley if (useFVM) { 191608449791SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 191708449791SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 191808449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 191908449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 192008449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 192108449791SMatthew G. Knepley } 192208449791SMatthew G. Knepley } 192308449791SMatthew G. Knepley 192408449791SMatthew G. Knepley if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);} 192508449791SMatthew G. Knepley 192608449791SMatthew G. Knepley /* FEM */ 192708449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 192808449791SMatthew G. Knepley /* 2: Get geometric data */ 192908449791SMatthew G. Knepley /* 3: Handle boundary values */ 193008449791SMatthew G. Knepley /* 4: Loop over domain */ 193108449791SMatthew G. Knepley /* Extract coefficients */ 193208449791SMatthew G. Knepley /* Loop over fields */ 193308449791SMatthew G. Knepley /* Set tiling for FE*/ 193408449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 193508449791SMatthew G. Knepley /* Loop over subdomain */ 193608449791SMatthew G. Knepley /* Loop over quad points */ 193708449791SMatthew G. Knepley /* Transform coords to real space */ 193808449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 193908449791SMatthew G. Knepley /* Evaluate residual at point */ 194008449791SMatthew G. Knepley /* Transform residual to real space */ 194108449791SMatthew G. Knepley /* Add residual to elemVec */ 194208449791SMatthew G. Knepley /* Loop over domain */ 194308449791SMatthew G. Knepley /* Add elemVec to locX */ 194408449791SMatthew G. Knepley 194508449791SMatthew G. Knepley /* FVM */ 194608449791SMatthew G. Knepley /* Get geometric data */ 194708449791SMatthew G. Knepley /* If using gradients */ 194808449791SMatthew G. Knepley /* Compute gradient data */ 194908449791SMatthew G. Knepley /* Loop over domain faces */ 195008449791SMatthew G. Knepley /* Count computational faces */ 195108449791SMatthew G. Knepley /* Reconstruct cell gradient */ 195208449791SMatthew G. Knepley /* Loop over domain cells */ 195308449791SMatthew G. Knepley /* Limit cell gradients */ 195408449791SMatthew G. Knepley /* Handle boundary values */ 195508449791SMatthew G. Knepley /* Loop over domain faces */ 195608449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 195708449791SMatthew G. Knepley /* Riemann solve over faces */ 195808449791SMatthew G. Knepley /* Loop over domain faces */ 195908449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 196008449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 196108449791SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, locF);CHKERRQ(ierr);} 196224cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 196324cdb843SMatthew G. Knepley PetscFunctionReturn(0); 196424cdb843SMatthew G. Knepley } 196524cdb843SMatthew G. Knepley 196624cdb843SMatthew G. Knepley #undef __FUNCT__ 196724cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 196824cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 196924cdb843SMatthew G. Knepley { 197024cdb843SMatthew G. Knepley DM dmCh, dmAux; 1971bbce034cSMatthew G. Knepley Vec A, cellgeom; 197224cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 197324cdb843SMatthew G. Knepley PetscQuadrature q; 197424cdb843SMatthew G. Knepley PetscSection section, sectionAux; 19752f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 19762f84e9bcSToby Isaac PetscScalar *cgeomScal; 197724cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 197824cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 197924cdb843SMatthew G. Knepley PetscInt totDim, totDimAux, diffCell = 0; 198024cdb843SMatthew G. Knepley PetscErrorCode ierr; 198124cdb843SMatthew G. Knepley 198224cdb843SMatthew G. Knepley PetscFunctionBegin; 198324cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 198424cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 198524cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 198624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 198724cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 198824cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 198924cdb843SMatthew G. Knepley numCells = cEnd - cStart; 199024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 199124cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 199224cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 199324cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 199424cdb843SMatthew G. Knepley if (dmAux) { 199524cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 199624cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 199724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 199824cdb843SMatthew G. Knepley } 199908449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 200024cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 2001bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 200224cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 200324cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2004bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 20052f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 20062f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 20072f84e9bcSToby Isaac DM dmCell; 20082f84e9bcSToby Isaac 20092f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 20102f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 20112f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 20127726bd6bSToby Isaac PetscScalar *thisgeom; 20132f84e9bcSToby Isaac 20142f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 20152f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 20162f84e9bcSToby Isaac } 20172f84e9bcSToby Isaac } 20182f84e9bcSToby Isaac else { 20192f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 20202f84e9bcSToby Isaac } 202124cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 202224cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 202324cdb843SMatthew G. Knepley PetscInt i; 202424cdb843SMatthew G. Knepley 202524cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 202624cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 202724cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 202824cdb843SMatthew G. Knepley if (X_t) { 202924cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 203024cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 203124cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 203224cdb843SMatthew G. Knepley } 203324cdb843SMatthew G. Knepley if (dmAux) { 20346da023fcSToby Isaac DM dmAuxPlex; 20356da023fcSToby Isaac 20366da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 20376da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 203824cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 20396da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 20406da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 204124cdb843SMatthew G. Knepley } 204224cdb843SMatthew G. Knepley } 204324cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 204424cdb843SMatthew G. Knepley PetscFE fe, feCh; 204524cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 204624cdb843SMatthew G. Knepley /* Conforming batches */ 204724cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 204824cdb843SMatthew G. Knepley /* Remainder */ 204924cdb843SMatthew G. Knepley PetscInt Nr, offset; 205024cdb843SMatthew G. Knepley 205124cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 205224cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 205324cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 205424cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 205524cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 205624cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 205724cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 205824cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 205924cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 206024cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 206124cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 206224cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 206324cdb843SMatthew G. Knepley offset = numCells - Nr; 2064bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 2065bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr); 2066bbce034cSMatthew 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); 2067bbce034cSMatthew 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); 206824cdb843SMatthew G. Knepley } 206924cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 207024cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 207124cdb843SMatthew G. Knepley PetscInt d; 207224cdb843SMatthew G. Knepley 207324cdb843SMatthew 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;} 207424cdb843SMatthew G. Knepley if (diff) { 207524cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 207624cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 207724cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 207824cdb843SMatthew G. Knepley ++diffCell; 207924cdb843SMatthew G. Knepley } 208024cdb843SMatthew G. Knepley if (diffCell > 9) break; 208124cdb843SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 208224cdb843SMatthew G. Knepley } 20832f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 20842f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 20852f84e9bcSToby Isaac } 20862f84e9bcSToby Isaac else { 20872f84e9bcSToby Isaac cgeom = NULL; 20882f84e9bcSToby Isaac } 20892f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2090bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 209124cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 209224cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 209324cdb843SMatthew G. Knepley PetscFunctionReturn(0); 209424cdb843SMatthew G. Knepley } 209524cdb843SMatthew G. Knepley 209624cdb843SMatthew G. Knepley #undef __FUNCT__ 209724cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 209824cdb843SMatthew G. Knepley /*@ 209924cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 210024cdb843SMatthew G. Knepley 210124cdb843SMatthew G. Knepley Input Parameters: 210224cdb843SMatthew G. Knepley + dm - The mesh 210324cdb843SMatthew G. Knepley . X - Local solution 210424cdb843SMatthew G. Knepley - user - The user context 210524cdb843SMatthew G. Knepley 210624cdb843SMatthew G. Knepley Output Parameter: 210724cdb843SMatthew G. Knepley . F - Local output vector 210824cdb843SMatthew G. Knepley 210924cdb843SMatthew G. Knepley Level: developer 211024cdb843SMatthew G. Knepley 211124cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 211224cdb843SMatthew G. Knepley @*/ 211324cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 211424cdb843SMatthew G. Knepley { 211524cdb843SMatthew G. Knepley PetscObject check; 2116c4d4a4f8SMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 21176da023fcSToby Isaac DM plex; 211824cdb843SMatthew G. Knepley PetscErrorCode ierr; 211924cdb843SMatthew G. Knepley 212024cdb843SMatthew G. Knepley PetscFunctionBegin; 21216da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 21226da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 21236da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2124c4d4a4f8SMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 212524cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 21266da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 21276da023fcSToby Isaac if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, F, user);CHKERRQ(ierr);} 21286da023fcSToby Isaac else {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, F, user);CHKERRQ(ierr);} 21299a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 213024cdb843SMatthew G. Knepley PetscFunctionReturn(0); 213124cdb843SMatthew G. Knepley } 213224cdb843SMatthew G. Knepley 213324cdb843SMatthew G. Knepley #undef __FUNCT__ 2134b953af5fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobian_Internal" 2135b953af5fSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 213624cdb843SMatthew G. Knepley { 213724cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 213824cdb843SMatthew G. Knepley const char *name = "Jacobian"; 213924cdb843SMatthew G. Knepley DM dmAux; 214024cdb843SMatthew G. Knepley DMLabel depth; 2141bbce034cSMatthew G. Knepley Vec A, cellgeom; 214224cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 214324cdb843SMatthew G. Knepley PetscQuadrature quad; 214424cdb843SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 21452f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 21462f84e9bcSToby Isaac PetscScalar *cgeomScal; 214755ad3c34SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *u, *u_t, *a = NULL; 2148b953af5fSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, c; 214924cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 215055ad3c34SMatthew G. Knepley PetscBool isShell, hasPrec; 215124cdb843SMatthew G. Knepley PetscErrorCode ierr; 215224cdb843SMatthew G. Knepley 215324cdb843SMatthew G. Knepley PetscFunctionBegin; 215424cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 215524cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 215624cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 215724cdb843SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 215824cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 215924cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 216024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 216155ad3c34SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 216224cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 216324cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 216424cdb843SMatthew G. Knepley numCells = cEnd - cStart; 216524cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 216624cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 216724cdb843SMatthew G. Knepley if (dmAux) { 216824cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 216924cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 217024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 217124cdb843SMatthew G. Knepley } 217208449791SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 217324cdb843SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 217455ad3c34SMatthew G. Knepley ierr = PetscMalloc4(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP);CHKERRQ(ierr); 217524cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2176bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 21772f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 21782f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 21792f84e9bcSToby Isaac DM dmCell; 21802f84e9bcSToby Isaac 21812f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 21822f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 21832f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 21847726bd6bSToby Isaac PetscScalar *thisgeom; 21852f84e9bcSToby Isaac 21862f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 21872f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 21882f84e9bcSToby Isaac } 21892f84e9bcSToby Isaac } 21902f84e9bcSToby Isaac else { 21912f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 21922f84e9bcSToby Isaac } 219324cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 219424cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 219524cdb843SMatthew G. Knepley PetscInt i; 219624cdb843SMatthew G. Knepley 219724cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 21989f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 219924cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 220024cdb843SMatthew G. Knepley if (X_t) { 220124cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 22029f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 220324cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 220424cdb843SMatthew G. Knepley } 220524cdb843SMatthew G. Knepley if (dmAux) { 220624cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 22079f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 220824cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 220924cdb843SMatthew G. Knepley } 221024cdb843SMatthew G. Knepley } 221124cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 221255ad3c34SMatthew G. Knepley if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 221324cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 221424cdb843SMatthew G. Knepley PetscFE fe; 221524cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 221624cdb843SMatthew G. Knepley /* Conforming batches */ 221724cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 221824cdb843SMatthew G. Knepley /* Remainder */ 221924cdb843SMatthew G. Knepley PetscInt Nr, offset; 222024cdb843SMatthew G. Knepley 222124cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 222224cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 222324cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 222424cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 222524cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 222624cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 222724cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 222824cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 222924cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 223024cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 223124cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 223224cdb843SMatthew G. Knepley offset = numCells - Nr; 223324cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 223455ad3c34SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSC_FALSE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr); 223555ad3c34SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSC_FALSE, 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); 223655ad3c34SMatthew G. Knepley if (hasPrec) { 223755ad3c34SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSC_TRUE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMatP);CHKERRQ(ierr); 223855ad3c34SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSC_TRUE, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 223955ad3c34SMatthew G. Knepley } 224024cdb843SMatthew G. Knepley } 224124cdb843SMatthew G. Knepley } 224224cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 224355ad3c34SMatthew G. Knepley if (hasPrec) { 224455ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 224555ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 224655ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 224755ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 224855ad3c34SMatthew G. Knepley } else { 22499f11d433SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 22509f11d433SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 225124cdb843SMatthew G. Knepley } 225255ad3c34SMatthew G. Knepley } 22532f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 22542f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 22552f84e9bcSToby Isaac } 22562f84e9bcSToby Isaac else { 22572f84e9bcSToby Isaac cgeom = NULL; 22582f84e9bcSToby Isaac } 22592f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 226055ad3c34SMatthew G. Knepley ierr = PetscFree4(u,u_t,elemMat,elemMatP);CHKERRQ(ierr); 226124cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 226224cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2263a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 226424cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2265a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 226624cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 226724cdb843SMatthew G. Knepley const char *bdLabel; 226824cdb843SMatthew G. Knepley DMLabel label; 226924cdb843SMatthew G. Knepley IS pointIS; 227024cdb843SMatthew G. Knepley const PetscInt *points; 227124cdb843SMatthew G. Knepley const PetscInt *values; 227224cdb843SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 227324cdb843SMatthew G. Knepley PetscBool isEssential; 227424cdb843SMatthew G. Knepley 2275a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 227624cdb843SMatthew G. Knepley if (isEssential) continue; 227724cdb843SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 2278c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 227924cdb843SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 228024cdb843SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 228124cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 228224cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 228324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 228424cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 228524cdb843SMatthew G. Knepley } 2286bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 228724cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 228824cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 228924cdb843SMatthew G. Knepley const PetscInt point = points[p]; 229024cdb843SMatthew G. Knepley PetscScalar *x = NULL; 229124cdb843SMatthew G. Knepley PetscInt i; 229224cdb843SMatthew G. Knepley 229324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 229424cdb843SMatthew G. Knepley if (dep != dim-1) continue; 2295bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 2296302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 2297bbce034cSMatthew 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); 229824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 229924cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 230024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 230124cdb843SMatthew G. Knepley if (X_t) { 230224cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 230324cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 230424cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 230524cdb843SMatthew G. Knepley } 230624cdb843SMatthew G. Knepley ++f; 230724cdb843SMatthew G. Knepley } 230824cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 230924cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 231024cdb843SMatthew G. Knepley PetscFE fe; 231124cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 231224cdb843SMatthew G. Knepley /* Conforming batches */ 231324cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 231424cdb843SMatthew G. Knepley /* Remainder */ 231524cdb843SMatthew G. Knepley PetscInt Nr, offset; 231624cdb843SMatthew G. Knepley 231724cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 231824cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 231924cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 232024cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 232124cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 232224cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 232324cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 232424cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 232524cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 232624cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 232724cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 232824cdb843SMatthew G. Knepley offset = numFaces - Nr; 232924cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2330bbce034cSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr); 2331bbce034cSMatthew 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); 233224cdb843SMatthew G. Knepley } 233324cdb843SMatthew G. Knepley } 233424cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 233524cdb843SMatthew G. Knepley const PetscInt point = points[p]; 233624cdb843SMatthew G. Knepley 233724cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 233824cdb843SMatthew G. Knepley if (dep != dim-1) continue; 233924cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 234024cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 234124cdb843SMatthew G. Knepley ++f; 234224cdb843SMatthew G. Knepley } 234324cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 234424cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2345bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 234624cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 234724cdb843SMatthew G. Knepley } 234824cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 234924cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 235024cdb843SMatthew G. Knepley if (mesh->printFEM) { 235124cdb843SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 235224cdb843SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 235324cdb843SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 235424cdb843SMatthew G. Knepley } 235524cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 235624cdb843SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 235724cdb843SMatthew G. Knepley if (isShell) { 235824cdb843SMatthew G. Knepley JacActionCtx *jctx; 235924cdb843SMatthew G. Knepley 236024cdb843SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 236124cdb843SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 236224cdb843SMatthew G. Knepley } 236324cdb843SMatthew G. Knepley PetscFunctionReturn(0); 236424cdb843SMatthew G. Knepley } 236524cdb843SMatthew G. Knepley 236624cdb843SMatthew G. Knepley #undef __FUNCT__ 236724cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 236824cdb843SMatthew G. Knepley /*@ 236924cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 237024cdb843SMatthew G. Knepley 237124cdb843SMatthew G. Knepley Input Parameters: 237224cdb843SMatthew G. Knepley + dm - The mesh 237324cdb843SMatthew G. Knepley . X - Local input vector 237424cdb843SMatthew G. Knepley - user - The user context 237524cdb843SMatthew G. Knepley 237624cdb843SMatthew G. Knepley Output Parameter: 237724cdb843SMatthew G. Knepley . Jac - Jacobian matrix 237824cdb843SMatthew G. Knepley 237924cdb843SMatthew G. Knepley Note: 238024cdb843SMatthew G. Knepley The first member of the user context must be an FEMContext. 238124cdb843SMatthew G. Knepley 238224cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 238324cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 238424cdb843SMatthew G. Knepley 238524cdb843SMatthew G. Knepley Level: developer 238624cdb843SMatthew G. Knepley 238724cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 238824cdb843SMatthew G. Knepley @*/ 238924cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 239024cdb843SMatthew G. Knepley { 2391b953af5fSMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 23926da023fcSToby Isaac DM plex; 239324cdb843SMatthew G. Knepley PetscErrorCode ierr; 239424cdb843SMatthew G. Knepley 239524cdb843SMatthew G. Knepley PetscFunctionBegin; 23966da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 23976da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 23986da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2399b953af5fSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 24006da023fcSToby Isaac ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 24019a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 240224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 240324cdb843SMatthew G. Knepley } 24041878804aSMatthew G. Knepley 24051878804aSMatthew G. Knepley #undef __FUNCT__ 24061878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions_Internal" 24070163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, Vec sol, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs) 24081878804aSMatthew G. Knepley { 24091878804aSMatthew G. Knepley Mat J, M; 24101878804aSMatthew G. Knepley Vec r, b; 24111878804aSMatthew G. Knepley MatNullSpace nullSpace; 24121878804aSMatthew G. Knepley PetscReal *error, res = 0.0; 24131878804aSMatthew G. Knepley PetscInt numFields; 24141878804aSMatthew G. Knepley PetscErrorCode ierr; 24151878804aSMatthew G. Knepley 24161878804aSMatthew G. Knepley PetscFunctionBegin; 24171878804aSMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 24181878804aSMatthew G. Knepley ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 24191878804aSMatthew G. Knepley M = J; 24201878804aSMatthew G. Knepley /* TODO Null space for J */ 24211878804aSMatthew G. Knepley /* Check discretization error */ 24221878804aSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 24231878804aSMatthew G. Knepley ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 24241878804aSMatthew G. Knepley if (numFields > 1) { 24251878804aSMatthew G. Knepley PetscInt f; 24261878804aSMatthew G. Knepley 24271189c1efSToby Isaac ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr); 24281878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 24291878804aSMatthew G. Knepley for (f = 0; f < numFields; ++f) { 24301878804aSMatthew G. Knepley if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 24311878804aSMatthew G. Knepley if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);} 24321878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 24331878804aSMatthew G. Knepley } 24341878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 24351878804aSMatthew G. Knepley } else { 24360709b2feSToby Isaac ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr); 24371878804aSMatthew G. Knepley if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);} 24381878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 24391878804aSMatthew G. Knepley } 24401878804aSMatthew G. Knepley ierr = PetscFree(error);CHKERRQ(ierr); 24411878804aSMatthew G. Knepley /* Check residual */ 24421878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 24431878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 24441878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr); 24451878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 24461878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2447685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2448685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 24491878804aSMatthew G. Knepley /* Check Jacobian */ 24501878804aSMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr); 24511878804aSMatthew G. Knepley ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 24521878804aSMatthew G. Knepley if (nullSpace) { 24531878804aSMatthew G. Knepley PetscBool isNull; 24541878804aSMatthew G. Knepley ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 24551878804aSMatthew G. Knepley if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 24561878804aSMatthew G. Knepley } 24571878804aSMatthew G. Knepley ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 24581878804aSMatthew G. Knepley ierr = VecSet(r, 0.0);CHKERRQ(ierr); 24591878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 24601878804aSMatthew G. Knepley ierr = MatMult(M, u, r);CHKERRQ(ierr); 24611878804aSMatthew G. Knepley ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 24621878804aSMatthew G. Knepley ierr = VecDestroy(&b);CHKERRQ(ierr); 24631878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 24641878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr); 24651878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 24661878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2467685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2468685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 24691878804aSMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 24701878804aSMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 24711878804aSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 24721878804aSMatthew G. Knepley PetscFunctionReturn(0); 24731878804aSMatthew G. Knepley } 24741878804aSMatthew G. Knepley 24751878804aSMatthew G. Knepley #undef __FUNCT__ 24761878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions" 24770163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs) 24781878804aSMatthew G. Knepley { 24791878804aSMatthew G. Knepley DM dm; 24801878804aSMatthew G. Knepley Vec sol; 24811878804aSMatthew G. Knepley PetscBool check; 24821878804aSMatthew G. Knepley PetscErrorCode ierr; 24831878804aSMatthew G. Knepley 24841878804aSMatthew G. Knepley PetscFunctionBegin; 2485c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 24861878804aSMatthew G. Knepley if (!check) PetscFunctionReturn(0); 24871878804aSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 24881878804aSMatthew G. Knepley ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 24891878804aSMatthew G. Knepley ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 24901878804aSMatthew G. Knepley ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr); 24911878804aSMatthew G. Knepley ierr = VecDestroy(&sol);CHKERRQ(ierr); 2492552f7358SJed Brown PetscFunctionReturn(0); 2493552f7358SJed Brown } 2494