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> 4a925c78cSMatthew G. Knepley #include <petscblaslapack.h> 5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> 6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 7552f7358SJed Brown 824cdb843SMatthew G. Knepley /************************** Interpolation *******************************/ 924cdb843SMatthew G. Knepley 10552f7358SJed Brown #undef __FUNCT__ 116da023fcSToby Isaac #define __FUNCT__ "DMSNESConvertPlex" 126da023fcSToby Isaac static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy) 136da023fcSToby Isaac { 146da023fcSToby Isaac PetscBool isPlex; 156da023fcSToby Isaac PetscErrorCode ierr; 166da023fcSToby Isaac 176da023fcSToby Isaac PetscFunctionBegin; 186da023fcSToby Isaac ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr); 196da023fcSToby Isaac if (isPlex) { 206da023fcSToby Isaac *plex = dm; 216da023fcSToby Isaac ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 22f7148743SMatthew G. Knepley } else { 23f7148743SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr); 24f7148743SMatthew G. Knepley if (!*plex) { 256da023fcSToby Isaac ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 26f7148743SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr); 276da023fcSToby Isaac if (copy) { 286da023fcSToby Isaac PetscInt i; 296da023fcSToby Isaac PetscObject obj; 306da023fcSToby Isaac const char *comps[3] = {"A","dmAux","dmCh"}; 316da023fcSToby Isaac 326da023fcSToby Isaac ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr); 336da023fcSToby Isaac for (i = 0; i < 3; i++) { 346da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr); 356da023fcSToby Isaac ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr); 366da023fcSToby Isaac } 376da023fcSToby Isaac } 38f7148743SMatthew G. Knepley } else { 39f7148743SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr); 40f7148743SMatthew G. Knepley } 416da023fcSToby Isaac } 426da023fcSToby Isaac PetscFunctionReturn(0); 436da023fcSToby Isaac } 446da023fcSToby Isaac 456da023fcSToby Isaac #undef __FUNCT__ 46552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate" 470adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 480adebc6cSBarry Smith { 49552f7358SJed Brown PetscErrorCode ierr; 50552f7358SJed Brown 51552f7358SJed Brown PetscFunctionBegin; 52552f7358SJed Brown PetscValidPointer(ctx, 2); 53552f7358SJed Brown ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr); 541aa26658SKarl Rupp 55552f7358SJed Brown (*ctx)->comm = comm; 56552f7358SJed Brown (*ctx)->dim = -1; 57552f7358SJed Brown (*ctx)->nInput = 0; 580298fd71SBarry Smith (*ctx)->points = NULL; 590298fd71SBarry Smith (*ctx)->cells = NULL; 60552f7358SJed Brown (*ctx)->n = -1; 610298fd71SBarry Smith (*ctx)->coords = NULL; 62552f7358SJed Brown PetscFunctionReturn(0); 63552f7358SJed Brown } 64552f7358SJed Brown 65552f7358SJed Brown #undef __FUNCT__ 66552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim" 670adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 680adebc6cSBarry Smith { 69552f7358SJed Brown PetscFunctionBegin; 700adebc6cSBarry Smith if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 71552f7358SJed Brown ctx->dim = dim; 72552f7358SJed Brown PetscFunctionReturn(0); 73552f7358SJed Brown } 74552f7358SJed Brown 75552f7358SJed Brown #undef __FUNCT__ 76552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim" 770adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 780adebc6cSBarry Smith { 79552f7358SJed Brown PetscFunctionBegin; 80552f7358SJed Brown PetscValidIntPointer(dim, 2); 81552f7358SJed Brown *dim = ctx->dim; 82552f7358SJed Brown PetscFunctionReturn(0); 83552f7358SJed Brown } 84552f7358SJed Brown 85552f7358SJed Brown #undef __FUNCT__ 86552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof" 870adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 880adebc6cSBarry Smith { 89552f7358SJed Brown PetscFunctionBegin; 900adebc6cSBarry Smith if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 91552f7358SJed Brown ctx->dof = dof; 92552f7358SJed Brown PetscFunctionReturn(0); 93552f7358SJed Brown } 94552f7358SJed Brown 95552f7358SJed Brown #undef __FUNCT__ 96552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof" 970adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 980adebc6cSBarry Smith { 99552f7358SJed Brown PetscFunctionBegin; 100552f7358SJed Brown PetscValidIntPointer(dof, 2); 101552f7358SJed Brown *dof = ctx->dof; 102552f7358SJed Brown PetscFunctionReturn(0); 103552f7358SJed Brown } 104552f7358SJed Brown 105552f7358SJed Brown #undef __FUNCT__ 106552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints" 1070adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 1080adebc6cSBarry Smith { 109552f7358SJed Brown PetscErrorCode ierr; 110552f7358SJed Brown 111552f7358SJed Brown PetscFunctionBegin; 1120adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 1130adebc6cSBarry Smith if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 114552f7358SJed Brown ctx->nInput = n; 1151aa26658SKarl Rupp 116785e854fSJed Brown ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 117552f7358SJed Brown ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 118552f7358SJed Brown PetscFunctionReturn(0); 119552f7358SJed Brown } 120552f7358SJed Brown 121552f7358SJed Brown #undef __FUNCT__ 122552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp" 1230adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 1240adebc6cSBarry Smith { 125552f7358SJed Brown MPI_Comm comm = ctx->comm; 126552f7358SJed Brown PetscScalar *a; 127552f7358SJed Brown PetscInt p, q, i; 128552f7358SJed Brown PetscMPIInt rank, size; 129552f7358SJed Brown PetscErrorCode ierr; 130552f7358SJed Brown Vec pointVec; 1313a93e3b7SToby Isaac PetscSF cellSF; 132552f7358SJed Brown PetscLayout layout; 133552f7358SJed Brown PetscReal *globalPoints; 134cb313848SJed Brown PetscScalar *globalPointsScalar; 135552f7358SJed Brown const PetscInt *ranges; 136552f7358SJed Brown PetscMPIInt *counts, *displs; 1373a93e3b7SToby Isaac const PetscSFNode *foundCells; 1383a93e3b7SToby Isaac const PetscInt *foundPoints; 139552f7358SJed Brown PetscMPIInt *foundProcs, *globalProcs; 1403a93e3b7SToby Isaac PetscInt n, N, numFound; 141552f7358SJed Brown 14219436ca2SJed Brown PetscFunctionBegin; 14319436ca2SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14419436ca2SJed Brown ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 14519436ca2SJed Brown ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 1460adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 14719436ca2SJed Brown /* Locate points */ 14819436ca2SJed Brown n = ctx->nInput; 149552f7358SJed Brown if (!redundantPoints) { 150552f7358SJed Brown ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 151552f7358SJed Brown ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 152552f7358SJed Brown ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 153552f7358SJed Brown ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 154552f7358SJed Brown ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 155552f7358SJed Brown /* Communicate all points to all processes */ 156dcca6d9dSJed Brown ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 157552f7358SJed Brown ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 158552f7358SJed Brown for (p = 0; p < size; ++p) { 159552f7358SJed Brown counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 160552f7358SJed Brown displs[p] = ranges[p]*ctx->dim; 161552f7358SJed Brown } 162552f7358SJed Brown ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 163552f7358SJed Brown } else { 164552f7358SJed Brown N = n; 165552f7358SJed Brown globalPoints = ctx->points; 16638ea73c8SJed Brown counts = displs = NULL; 16738ea73c8SJed Brown layout = NULL; 168552f7358SJed Brown } 169552f7358SJed Brown #if 0 170dcca6d9dSJed Brown ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 17119436ca2SJed Brown /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 172552f7358SJed Brown #else 173cb313848SJed Brown #if defined(PETSC_USE_COMPLEX) 174785e854fSJed Brown ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr); 175cb313848SJed Brown for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i]; 176cb313848SJed Brown #else 177cb313848SJed Brown globalPointsScalar = globalPoints; 178cb313848SJed Brown #endif 17904706141SMatthew G Knepley ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 180dcca6d9dSJed Brown ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 1813a93e3b7SToby Isaac cellSF = NULL; 182*2d1fa6caSMatthew G. Knepley ierr = DMLocatePoints(dm, pointVec, DM_POINTLOCATION_REMOVE, &cellSF);CHKERRQ(ierr); 1833a93e3b7SToby Isaac ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr); 184552f7358SJed Brown #endif 1853a93e3b7SToby Isaac for (p = 0; p < numFound; ++p) { 1863a93e3b7SToby Isaac if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank; 1873a93e3b7SToby Isaac else foundProcs[foundPoints ? foundPoints[p] : p] = size; 188552f7358SJed Brown } 189552f7358SJed Brown /* Let the lowest rank process own each point */ 190b2566f29SBarry Smith ierr = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 191552f7358SJed Brown ctx->n = 0; 192552f7358SJed Brown for (p = 0; p < N; ++p) { 1930adebc6cSBarry 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); 1941aa26658SKarl Rupp else if (globalProcs[p] == rank) ctx->n++; 195552f7358SJed Brown } 196552f7358SJed Brown /* Create coordinates vector and array of owned cells */ 197785e854fSJed Brown ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 198552f7358SJed Brown ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 199552f7358SJed Brown ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 200552f7358SJed Brown ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 201c0dedaeaSBarry Smith ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 202552f7358SJed Brown ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 203552f7358SJed Brown for (p = 0, q = 0, i = 0; p < N; ++p) { 204552f7358SJed Brown if (globalProcs[p] == rank) { 205552f7358SJed Brown PetscInt d; 206552f7358SJed Brown 2071aa26658SKarl Rupp for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 2083a93e3b7SToby Isaac ctx->cells[q++] = foundCells[p].index; 209552f7358SJed Brown } 210552f7358SJed Brown } 211552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 212552f7358SJed Brown #if 0 213552f7358SJed Brown ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 214552f7358SJed Brown #else 215552f7358SJed Brown ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 2163a93e3b7SToby Isaac ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr); 217552f7358SJed Brown ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 218552f7358SJed Brown #endif 219cb313848SJed Brown if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 220d343d804SMatthew G. Knepley if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 221552f7358SJed Brown ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 222552f7358SJed Brown PetscFunctionReturn(0); 223552f7358SJed Brown } 224552f7358SJed Brown 225552f7358SJed Brown #undef __FUNCT__ 226552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates" 2270adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 2280adebc6cSBarry Smith { 229552f7358SJed Brown PetscFunctionBegin; 230552f7358SJed Brown PetscValidPointer(coordinates, 2); 2310adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 232552f7358SJed Brown *coordinates = ctx->coords; 233552f7358SJed Brown PetscFunctionReturn(0); 234552f7358SJed Brown } 235552f7358SJed Brown 236552f7358SJed Brown #undef __FUNCT__ 237552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector" 2380adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 2390adebc6cSBarry Smith { 240552f7358SJed Brown PetscErrorCode ierr; 241552f7358SJed Brown 242552f7358SJed Brown PetscFunctionBegin; 243552f7358SJed Brown PetscValidPointer(v, 2); 2440adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 245552f7358SJed Brown ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 246552f7358SJed Brown ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 247552f7358SJed Brown ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 248c0dedaeaSBarry Smith ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 249552f7358SJed Brown PetscFunctionReturn(0); 250552f7358SJed Brown } 251552f7358SJed Brown 252552f7358SJed Brown #undef __FUNCT__ 253552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector" 2540adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 2550adebc6cSBarry Smith { 256552f7358SJed Brown PetscErrorCode ierr; 257552f7358SJed Brown 258552f7358SJed Brown PetscFunctionBegin; 259552f7358SJed Brown PetscValidPointer(v, 2); 2600adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 261552f7358SJed Brown ierr = VecDestroy(v);CHKERRQ(ierr); 262552f7358SJed Brown PetscFunctionReturn(0); 263552f7358SJed Brown } 264552f7358SJed Brown 265552f7358SJed Brown #undef __FUNCT__ 2667a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private" 2677a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 268a6dfd86eSKarl Rupp { 269552f7358SJed Brown PetscReal *v0, *J, *invJ, detJ; 27056044e6dSMatthew G. Knepley const PetscScalar *coords; 27156044e6dSMatthew G. Knepley PetscScalar *a; 272552f7358SJed Brown PetscInt p; 273552f7358SJed Brown PetscErrorCode ierr; 274552f7358SJed Brown 275552f7358SJed Brown PetscFunctionBegin; 276dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 27756044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 278552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 279552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 280552f7358SJed Brown PetscInt c = ctx->cells[p]; 281a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 282552f7358SJed Brown PetscReal xi[4]; 283552f7358SJed Brown PetscInt d, f, comp; 284552f7358SJed Brown 2858e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 286552f7358SJed Brown if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 2870298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 2881aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 2891aa26658SKarl Rupp 290552f7358SJed Brown for (d = 0; d < ctx->dim; ++d) { 291552f7358SJed Brown xi[d] = 0.0; 2921aa26658SKarl 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]); 2931aa26658SKarl 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]; 294552f7358SJed Brown } 2950298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 296552f7358SJed Brown } 297552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 29856044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 299552f7358SJed Brown ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 300552f7358SJed Brown PetscFunctionReturn(0); 301552f7358SJed Brown } 302552f7358SJed Brown 303552f7358SJed Brown #undef __FUNCT__ 3047a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private" 3057a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 3067a1931ceSMatthew G. Knepley { 3077a1931ceSMatthew G. Knepley PetscReal *v0, *J, *invJ, detJ; 30856044e6dSMatthew G. Knepley const PetscScalar *coords; 30956044e6dSMatthew G. Knepley PetscScalar *a; 3107a1931ceSMatthew G. Knepley PetscInt p; 3117a1931ceSMatthew G. Knepley PetscErrorCode ierr; 3127a1931ceSMatthew G. Knepley 3137a1931ceSMatthew G. Knepley PetscFunctionBegin; 314dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 31556044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 3167a1931ceSMatthew G. Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 3177a1931ceSMatthew G. Knepley for (p = 0; p < ctx->n; ++p) { 3187a1931ceSMatthew G. Knepley PetscInt c = ctx->cells[p]; 3197a1931ceSMatthew G. Knepley const PetscInt order[3] = {2, 1, 3}; 3202584bbe8SMatthew G. Knepley PetscScalar *x = NULL; 3217a1931ceSMatthew G. Knepley PetscReal xi[4]; 3227a1931ceSMatthew G. Knepley PetscInt d, f, comp; 3237a1931ceSMatthew G. Knepley 3248e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 3257a1931ceSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 3267a1931ceSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 3277a1931ceSMatthew G. Knepley for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 3287a1931ceSMatthew G. Knepley 3297a1931ceSMatthew G. Knepley for (d = 0; d < ctx->dim; ++d) { 3307a1931ceSMatthew G. Knepley xi[d] = 0.0; 3317a1931ceSMatthew 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]); 3327a1931ceSMatthew 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]; 3337a1931ceSMatthew G. Knepley } 3347a1931ceSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 3357a1931ceSMatthew G. Knepley } 3367a1931ceSMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 33756044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 3387a1931ceSMatthew G. Knepley ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 3397a1931ceSMatthew G. Knepley PetscFunctionReturn(0); 3407a1931ceSMatthew G. Knepley } 3417a1931ceSMatthew G. Knepley 3427a1931ceSMatthew G. Knepley #undef __FUNCT__ 343552f7358SJed Brown #define __FUNCT__ "QuadMap_Private" 3445820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 345552f7358SJed Brown { 346552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 347552f7358SJed Brown const PetscScalar x0 = vertices[0]; 348552f7358SJed Brown const PetscScalar y0 = vertices[1]; 349552f7358SJed Brown const PetscScalar x1 = vertices[2]; 350552f7358SJed Brown const PetscScalar y1 = vertices[3]; 351552f7358SJed Brown const PetscScalar x2 = vertices[4]; 352552f7358SJed Brown const PetscScalar y2 = vertices[5]; 353552f7358SJed Brown const PetscScalar x3 = vertices[6]; 354552f7358SJed Brown const PetscScalar y3 = vertices[7]; 355552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 356552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 357552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 358552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 359552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 360552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 36156044e6dSMatthew G. Knepley const PetscScalar *ref; 36256044e6dSMatthew G. Knepley PetscScalar *real; 363552f7358SJed Brown PetscErrorCode ierr; 364552f7358SJed Brown 365552f7358SJed Brown PetscFunctionBegin; 36656044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 367552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 368552f7358SJed Brown { 369552f7358SJed Brown const PetscScalar p0 = ref[0]; 370552f7358SJed Brown const PetscScalar p1 = ref[1]; 371552f7358SJed Brown 372552f7358SJed Brown real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 373552f7358SJed Brown real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 374552f7358SJed Brown } 375552f7358SJed Brown ierr = PetscLogFlops(28);CHKERRQ(ierr); 37656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 377552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 378552f7358SJed Brown PetscFunctionReturn(0); 379552f7358SJed Brown } 380552f7358SJed Brown 381af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 382552f7358SJed Brown #undef __FUNCT__ 383552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private" 384d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 385552f7358SJed Brown { 386552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 387552f7358SJed Brown const PetscScalar x0 = vertices[0]; 388552f7358SJed Brown const PetscScalar y0 = vertices[1]; 389552f7358SJed Brown const PetscScalar x1 = vertices[2]; 390552f7358SJed Brown const PetscScalar y1 = vertices[3]; 391552f7358SJed Brown const PetscScalar x2 = vertices[4]; 392552f7358SJed Brown const PetscScalar y2 = vertices[5]; 393552f7358SJed Brown const PetscScalar x3 = vertices[6]; 394552f7358SJed Brown const PetscScalar y3 = vertices[7]; 395552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 396552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 39756044e6dSMatthew G. Knepley const PetscScalar *ref; 398552f7358SJed Brown PetscErrorCode ierr; 399552f7358SJed Brown 400552f7358SJed Brown PetscFunctionBegin; 40156044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 402552f7358SJed Brown { 403552f7358SJed Brown const PetscScalar x = ref[0]; 404552f7358SJed Brown const PetscScalar y = ref[1]; 405552f7358SJed Brown const PetscInt rows[2] = {0, 1}; 406da80777bSKarl Rupp PetscScalar values[4]; 407da80777bSKarl Rupp 408da80777bSKarl Rupp values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 409da80777bSKarl Rupp values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 41094ab13aaSBarry Smith ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 411552f7358SJed Brown } 412552f7358SJed Brown ierr = PetscLogFlops(30);CHKERRQ(ierr); 41356044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 41494ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 41594ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 416552f7358SJed Brown PetscFunctionReturn(0); 417552f7358SJed Brown } 418552f7358SJed Brown 419552f7358SJed Brown #undef __FUNCT__ 420552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private" 421a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 422a6dfd86eSKarl Rupp { 423fafc0619SMatthew G Knepley DM dmCoord; 4245509d985SMatthew G. Knepley PetscFE fem; 425552f7358SJed Brown SNES snes; 426552f7358SJed Brown KSP ksp; 427552f7358SJed Brown PC pc; 428552f7358SJed Brown Vec coordsLocal, r, ref, real; 429552f7358SJed Brown Mat J; 43056044e6dSMatthew G. Knepley const PetscScalar *coords; 43156044e6dSMatthew G. Knepley PetscScalar *a; 432552f7358SJed Brown PetscInt p; 4335509d985SMatthew G. Knepley const PetscInt dof = ctx->dof; 434552f7358SJed Brown PetscErrorCode ierr; 435552f7358SJed Brown 436552f7358SJed Brown PetscFunctionBegin; 4375509d985SMatthew G. Knepley ierr = DMGetField(dm, 0, (PetscObject *) &fem);CHKERRQ(ierr); 438552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 439fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 440552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 441552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 442552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 443552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 444c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 445552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 446552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 447552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 448552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 449552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 450552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 4510298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 4520298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 453552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 454552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 455552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 456552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 457552f7358SJed Brown 45856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 459552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 460552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 461a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 462552f7358SJed Brown PetscScalar *xi; 463cb313848SJed Brown PetscReal xir[2]; 464552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 465552f7358SJed Brown 466552f7358SJed Brown /* Can make this do all points at once */ 4670298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4680adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 4690298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 4700298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 4710298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 472552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 473552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 474552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 475552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 476552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 477552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 478cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 479cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 4805509d985SMatthew G. Knepley if (4*dof != xSize) { 4815509d985SMatthew G. Knepley PetscReal *B; 4825509d985SMatthew G. Knepley PetscInt d; 4831aa26658SKarl Rupp 4845509d985SMatthew G. Knepley xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0; 4855509d985SMatthew G. Knepley ierr = PetscFEGetTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr); 4865509d985SMatthew G. Knepley for (comp = 0; comp < dof; ++comp) { 4875509d985SMatthew G. Knepley a[p*dof+comp] = 0.0; 4885509d985SMatthew G. Knepley for (d = 0; d < xSize/dof; ++d) { 4895509d985SMatthew G. Knepley a[p*dof+comp] += x[d*dof+comp]*B[d*dof+comp]; 4905509d985SMatthew G. Knepley } 4915509d985SMatthew G. Knepley } 4925509d985SMatthew G. Knepley ierr = PetscFERestoreTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr); 4935509d985SMatthew G. Knepley } else { 4945509d985SMatthew G. Knepley for (comp = 0; comp < dof; ++comp) 4955509d985SMatthew G. Knepley a[p*dof+comp] = x[0*dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*dof+comp]*xir[0]*(1 - xir[1]) + x[2*dof+comp]*xir[0]*xir[1] + x[3*dof+comp]*(1 - xir[0])*xir[1]; 4965509d985SMatthew G. Knepley } 497552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 4980298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4990298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 500552f7358SJed Brown } 501552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 50256044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 503552f7358SJed Brown 504552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 505552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 506552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 507552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 508552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 509552f7358SJed Brown PetscFunctionReturn(0); 510552f7358SJed Brown } 511552f7358SJed Brown 512552f7358SJed Brown #undef __FUNCT__ 513552f7358SJed Brown #define __FUNCT__ "HexMap_Private" 5145820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 515552f7358SJed Brown { 516552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 517552f7358SJed Brown const PetscScalar x0 = vertices[0]; 518552f7358SJed Brown const PetscScalar y0 = vertices[1]; 519552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5207a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5217a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5227a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 523552f7358SJed Brown const PetscScalar x2 = vertices[6]; 524552f7358SJed Brown const PetscScalar y2 = vertices[7]; 525552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5267a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5277a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5287a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 529552f7358SJed Brown const PetscScalar x4 = vertices[12]; 530552f7358SJed Brown const PetscScalar y4 = vertices[13]; 531552f7358SJed Brown const PetscScalar z4 = vertices[14]; 532552f7358SJed Brown const PetscScalar x5 = vertices[15]; 533552f7358SJed Brown const PetscScalar y5 = vertices[16]; 534552f7358SJed Brown const PetscScalar z5 = vertices[17]; 535552f7358SJed Brown const PetscScalar x6 = vertices[18]; 536552f7358SJed Brown const PetscScalar y6 = vertices[19]; 537552f7358SJed Brown const PetscScalar z6 = vertices[20]; 538552f7358SJed Brown const PetscScalar x7 = vertices[21]; 539552f7358SJed Brown const PetscScalar y7 = vertices[22]; 540552f7358SJed Brown const PetscScalar z7 = vertices[23]; 541552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 542552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 543552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 544552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 545552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 546552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 547552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 548552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 549552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 550552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 551552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 552552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 553552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 554552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 555552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 556552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 557552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 558552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 559552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 560552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 561552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 56256044e6dSMatthew G. Knepley const PetscScalar *ref; 56356044e6dSMatthew G. Knepley PetscScalar *real; 564552f7358SJed Brown PetscErrorCode ierr; 565552f7358SJed Brown 566552f7358SJed Brown PetscFunctionBegin; 56756044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 568552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 569552f7358SJed Brown { 570552f7358SJed Brown const PetscScalar p0 = ref[0]; 571552f7358SJed Brown const PetscScalar p1 = ref[1]; 572552f7358SJed Brown const PetscScalar p2 = ref[2]; 573552f7358SJed Brown 574552f7358SJed 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; 575552f7358SJed 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; 576552f7358SJed 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; 577552f7358SJed Brown } 578552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 57956044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 580552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 581552f7358SJed Brown PetscFunctionReturn(0); 582552f7358SJed Brown } 583552f7358SJed Brown 584552f7358SJed Brown #undef __FUNCT__ 585552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private" 586d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 587552f7358SJed Brown { 588552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 589552f7358SJed Brown const PetscScalar x0 = vertices[0]; 590552f7358SJed Brown const PetscScalar y0 = vertices[1]; 591552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5927a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5937a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5947a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 595552f7358SJed Brown const PetscScalar x2 = vertices[6]; 596552f7358SJed Brown const PetscScalar y2 = vertices[7]; 597552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5987a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5997a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 6007a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 601552f7358SJed Brown const PetscScalar x4 = vertices[12]; 602552f7358SJed Brown const PetscScalar y4 = vertices[13]; 603552f7358SJed Brown const PetscScalar z4 = vertices[14]; 604552f7358SJed Brown const PetscScalar x5 = vertices[15]; 605552f7358SJed Brown const PetscScalar y5 = vertices[16]; 606552f7358SJed Brown const PetscScalar z5 = vertices[17]; 607552f7358SJed Brown const PetscScalar x6 = vertices[18]; 608552f7358SJed Brown const PetscScalar y6 = vertices[19]; 609552f7358SJed Brown const PetscScalar z6 = vertices[20]; 610552f7358SJed Brown const PetscScalar x7 = vertices[21]; 611552f7358SJed Brown const PetscScalar y7 = vertices[22]; 612552f7358SJed Brown const PetscScalar z7 = vertices[23]; 613552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 614552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 615552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 616552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 617552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 618552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 619552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 620552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 621552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 622552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 623552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 624552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 62556044e6dSMatthew G. Knepley const PetscScalar *ref; 626552f7358SJed Brown PetscErrorCode ierr; 627552f7358SJed Brown 628552f7358SJed Brown PetscFunctionBegin; 62956044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 630552f7358SJed Brown { 631552f7358SJed Brown const PetscScalar x = ref[0]; 632552f7358SJed Brown const PetscScalar y = ref[1]; 633552f7358SJed Brown const PetscScalar z = ref[2]; 634552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 635da80777bSKarl Rupp PetscScalar values[9]; 636da80777bSKarl Rupp 637da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 638da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 639da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 640da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 641da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 642da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 643da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 644da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 645da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 6461aa26658SKarl Rupp 64794ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 648552f7358SJed Brown } 649552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 65056044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 65194ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 65294ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 653552f7358SJed Brown PetscFunctionReturn(0); 654552f7358SJed Brown } 655552f7358SJed Brown 656552f7358SJed Brown #undef __FUNCT__ 657552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private" 658a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 659a6dfd86eSKarl Rupp { 660fafc0619SMatthew G Knepley DM dmCoord; 661552f7358SJed Brown SNES snes; 662552f7358SJed Brown KSP ksp; 663552f7358SJed Brown PC pc; 664552f7358SJed Brown Vec coordsLocal, r, ref, real; 665552f7358SJed Brown Mat J; 66656044e6dSMatthew G. Knepley const PetscScalar *coords; 66756044e6dSMatthew G. Knepley PetscScalar *a; 668552f7358SJed Brown PetscInt p; 669552f7358SJed Brown PetscErrorCode ierr; 670552f7358SJed Brown 671552f7358SJed Brown PetscFunctionBegin; 672552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 673fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 674552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 675552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 676552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 677552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 678c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 679552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 680552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 681552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 682552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 683552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 684552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 6850298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 6860298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 687552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 688552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 689552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 690552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 691552f7358SJed Brown 69256044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 693552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 694552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 695a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 696552f7358SJed Brown PetscScalar *xi; 697cb313848SJed Brown PetscReal xir[3]; 698552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 699552f7358SJed Brown 700552f7358SJed Brown /* Can make this do all points at once */ 7010298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 7020adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 7030298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 7040adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 7050298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 7060298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 707552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 708552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 709552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 710552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 711552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 712552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 713552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 714cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 715cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 716cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 717552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 718552f7358SJed Brown a[p*ctx->dof+comp] = 719cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 7207a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 721cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 7227a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 723cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 724cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 725cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 726cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 727552f7358SJed Brown } 728552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 7290298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 7300298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 731552f7358SJed Brown } 732552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 73356044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 734552f7358SJed Brown 735552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 736552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 737552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 738552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 739552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 740552f7358SJed Brown PetscFunctionReturn(0); 741552f7358SJed Brown } 742552f7358SJed Brown 743552f7358SJed Brown #undef __FUNCT__ 744552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate" 745552f7358SJed Brown /* 746552f7358SJed Brown Input Parameters: 747552f7358SJed Brown + ctx - The DMInterpolationInfo context 748552f7358SJed Brown . dm - The DM 749552f7358SJed Brown - x - The local vector containing the field to be interpolated 750552f7358SJed Brown 751552f7358SJed Brown Output Parameters: 752552f7358SJed Brown . v - The vector containing the interpolated values 753552f7358SJed Brown */ 7540adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 7550adebc6cSBarry Smith { 756552f7358SJed Brown PetscInt dim, coneSize, n; 757552f7358SJed Brown PetscErrorCode ierr; 758552f7358SJed Brown 759552f7358SJed Brown PetscFunctionBegin; 760552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 761552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 762552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 763552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 7640adebc6cSBarry 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); 765552f7358SJed Brown if (n) { 766c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 767552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 768552f7358SJed Brown if (dim == 2) { 769552f7358SJed Brown if (coneSize == 3) { 7707a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 771552f7358SJed Brown } else if (coneSize == 4) { 772552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 7730adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 774552f7358SJed Brown } else if (dim == 3) { 775552f7358SJed Brown if (coneSize == 4) { 7767a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 777552f7358SJed Brown } else { 778552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 779552f7358SJed Brown } 7800adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 781552f7358SJed Brown } 782552f7358SJed Brown PetscFunctionReturn(0); 783552f7358SJed Brown } 784552f7358SJed Brown 785552f7358SJed Brown #undef __FUNCT__ 786552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy" 7870adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 7880adebc6cSBarry Smith { 789552f7358SJed Brown PetscErrorCode ierr; 790552f7358SJed Brown 791552f7358SJed Brown PetscFunctionBegin; 792552f7358SJed Brown PetscValidPointer(ctx, 2); 793552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 794552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 795552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 796552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 7970298fd71SBarry Smith *ctx = NULL; 798552f7358SJed Brown PetscFunctionReturn(0); 799552f7358SJed Brown } 800cc0c4584SMatthew G. Knepley 801cc0c4584SMatthew G. Knepley #undef __FUNCT__ 802cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields" 803cc0c4584SMatthew G. Knepley /*@C 804cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 805cc0c4584SMatthew G. Knepley 806cc0c4584SMatthew G. Knepley Collective on SNES 807cc0c4584SMatthew G. Knepley 808cc0c4584SMatthew G. Knepley Input Parameters: 809cc0c4584SMatthew G. Knepley + snes - the SNES context 810cc0c4584SMatthew G. Knepley . its - iteration number 811cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 812d43b4f6eSBarry Smith - vf - PetscViewerAndFormat of type ASCII 813cc0c4584SMatthew G. Knepley 814cc0c4584SMatthew G. Knepley Notes: 815cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 816cc0c4584SMatthew G. Knepley 817cc0c4584SMatthew G. Knepley Level: intermediate 818cc0c4584SMatthew G. Knepley 819cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 820cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 821cc0c4584SMatthew G. Knepley @*/ 822d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf) 823cc0c4584SMatthew G. Knepley { 824d43b4f6eSBarry Smith PetscViewer viewer = vf->viewer; 825cc0c4584SMatthew G. Knepley Vec res; 826cc0c4584SMatthew G. Knepley DM dm; 827cc0c4584SMatthew G. Knepley PetscSection s; 828cc0c4584SMatthew G. Knepley const PetscScalar *r; 829cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 830cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 831cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 832cc0c4584SMatthew G. Knepley 833cc0c4584SMatthew G. Knepley PetscFunctionBegin; 8344d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 835cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 836cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 837cc0c4584SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 838cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 839cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 840cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 841cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 842cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 843cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 844cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 845cc0c4584SMatthew G. Knepley 846cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 847cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 848cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 849cc0c4584SMatthew G. Knepley } 850cc0c4584SMatthew G. Knepley } 851cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 852b2566f29SBarry Smith ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 853d43b4f6eSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 854cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 855cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 856cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 857cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 858cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 859cc0c4584SMatthew G. Knepley } 860cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 861cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 862d43b4f6eSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 863cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 864cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 865cc0c4584SMatthew G. Knepley } 86624cdb843SMatthew G. Knepley 86724cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 86824cdb843SMatthew G. Knepley 86924cdb843SMatthew G. Knepley #undef __FUNCT__ 8707d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 8717d4028c8SMatthew G. Knepley /*@ 8727d4028c8SMatthew G. Knepley DMPlexSNESGetGeometryFEM - Return precomputed geometric data 8737d4028c8SMatthew G. Knepley 8747d4028c8SMatthew G. Knepley Input Parameter: 8757d4028c8SMatthew G. Knepley . dm - The DM 8767d4028c8SMatthew G. Knepley 8777d4028c8SMatthew G. Knepley Output Parameters: 8787d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 8797d4028c8SMatthew G. Knepley 8807d4028c8SMatthew G. Knepley Level: developer 8817d4028c8SMatthew G. Knepley 8827d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal() 8837d4028c8SMatthew G. Knepley @*/ 8847d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 8857d4028c8SMatthew G. Knepley { 8867d4028c8SMatthew G. Knepley DMSNES dmsnes; 8877d4028c8SMatthew G. Knepley PetscObject obj; 8887d4028c8SMatthew G. Knepley PetscErrorCode ierr; 8897d4028c8SMatthew G. Knepley 8907d4028c8SMatthew G. Knepley PetscFunctionBegin; 8917d4028c8SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8927d4028c8SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 8937d4028c8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 8947d4028c8SMatthew G. Knepley if (!obj) { 8957d4028c8SMatthew G. Knepley Vec cellgeom; 8967d4028c8SMatthew G. Knepley 8977d4028c8SMatthew G. Knepley ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 8987d4028c8SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 8997d4028c8SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 9007d4028c8SMatthew G. Knepley } 9017d4028c8SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 9027d4028c8SMatthew G. Knepley PetscFunctionReturn(0); 9037d4028c8SMatthew G. Knepley } 9047d4028c8SMatthew G. Knepley 9057d4028c8SMatthew G. Knepley #undef __FUNCT__ 90608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM" 90708449791SMatthew G. Knepley /*@ 90808449791SMatthew G. Knepley DMPlexSNESGetGeometryFVM - Return precomputed geometric data 90908449791SMatthew G. Knepley 91008449791SMatthew G. Knepley Input Parameter: 91108449791SMatthew G. Knepley . dm - The DM 91208449791SMatthew G. Knepley 91308449791SMatthew G. Knepley Output Parameters: 91408449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry 91508449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 91608449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 91708449791SMatthew G. Knepley 91808449791SMatthew G. Knepley Level: developer 91908449791SMatthew G. Knepley 92008449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 92108449791SMatthew G. Knepley @*/ 92208449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 92324cdb843SMatthew G. Knepley { 92408449791SMatthew G. Knepley DMSNES dmsnes; 92508449791SMatthew G. Knepley PetscObject obj; 92624cdb843SMatthew G. Knepley PetscErrorCode ierr; 92724cdb843SMatthew G. Knepley 92824cdb843SMatthew G. Knepley PetscFunctionBegin; 92908449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 93008449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 93108449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr); 93208449791SMatthew G. Knepley if (!obj) { 93308449791SMatthew G. Knepley Vec cellgeom, facegeom; 93424cdb843SMatthew G. Knepley 93508449791SMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr); 93608449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr); 93708449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr); 93808449791SMatthew G. Knepley ierr = VecDestroy(&facegeom);CHKERRQ(ierr); 93908449791SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 94008449791SMatthew G. Knepley } 94108449791SMatthew G. Knepley if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);} 94208449791SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);} 94308449791SMatthew G. Knepley if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);} 94424cdb843SMatthew G. Knepley PetscFunctionReturn(0); 94524cdb843SMatthew G. Knepley } 94624cdb843SMatthew G. Knepley 94724cdb843SMatthew G. Knepley #undef __FUNCT__ 94808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM" 949dbd489d2SMatthew G. Knepley /*@ 95008449791SMatthew G. Knepley DMPlexSNESGetGradientDM - Return gradient data layout 95108449791SMatthew G. Knepley 95208449791SMatthew G. Knepley Input Parameters: 95308449791SMatthew G. Knepley + dm - The DM 95408449791SMatthew G. Knepley - fv - The PetscFV 95508449791SMatthew G. Knepley 95608449791SMatthew G. Knepley Output Parameter: 95708449791SMatthew G. Knepley . dmGrad - The layout for gradient values 95808449791SMatthew G. Knepley 95908449791SMatthew G. Knepley Level: developer 96008449791SMatthew G. Knepley 96108449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 96208449791SMatthew G. Knepley @*/ 96308449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 96424cdb843SMatthew G. Knepley { 96508449791SMatthew G. Knepley DMSNES dmsnes; 96608449791SMatthew G. Knepley PetscObject obj; 96708449791SMatthew G. Knepley PetscBool computeGradients; 96824cdb843SMatthew G. Knepley PetscErrorCode ierr; 96924cdb843SMatthew G. Knepley 97024cdb843SMatthew G. Knepley PetscFunctionBegin; 97108449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 97208449791SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 97308449791SMatthew G. Knepley PetscValidPointer(dmGrad,3); 97408449791SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 97508449791SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 97608449791SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 97708449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr); 97808449791SMatthew G. Knepley if (!obj) { 97908449791SMatthew G. Knepley DM dmGrad; 98008449791SMatthew G. Knepley Vec faceGeometry, cellGeometry; 98108449791SMatthew G. Knepley 98208449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr); 98308449791SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr); 98408449791SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr); 98508449791SMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 98608449791SMatthew G. Knepley } 98708449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr); 98808449791SMatthew G. Knepley PetscFunctionReturn(0); 98908449791SMatthew G. Knepley } 99008449791SMatthew G. Knepley 99108449791SMatthew G. Knepley #undef __FUNCT__ 99208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields" 99308449791SMatthew G. Knepley /*@C 99408449791SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 99508449791SMatthew G. Knepley 99608449791SMatthew G. Knepley Input Parameters: 99708449791SMatthew G. Knepley + dm - The DM 99808449791SMatthew G. Knepley . cStart - The first cell to include 99908449791SMatthew G. Knepley . cEnd - The first cell to exclude 100008449791SMatthew G. Knepley . locX - A local vector with the solution fields 100108449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 100208449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 100308449791SMatthew G. Knepley 100408449791SMatthew G. Knepley Output Parameters: 100508449791SMatthew G. Knepley + u - The field coefficients 100608449791SMatthew G. Knepley . u_t - The fields derivative coefficients 100708449791SMatthew G. Knepley - a - The auxiliary field coefficients 100808449791SMatthew G. Knepley 100908449791SMatthew G. Knepley Level: developer 101008449791SMatthew G. Knepley 101108449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 101208449791SMatthew G. Knepley @*/ 101308449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 101408449791SMatthew G. Knepley { 101508449791SMatthew G. Knepley DM dmAux; 101608449791SMatthew G. Knepley PetscSection section, sectionAux; 101708449791SMatthew G. Knepley PetscDS prob; 101808449791SMatthew G. Knepley PetscInt numCells = cEnd - cStart, totDim, totDimAux, c; 101908449791SMatthew G. Knepley PetscErrorCode ierr; 102008449791SMatthew G. Knepley 102108449791SMatthew G. Knepley PetscFunctionBegin; 102208449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 102308449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 102408449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 102508449791SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 102608449791SMatthew G. Knepley PetscValidPointer(u, 7); 102708449791SMatthew G. Knepley PetscValidPointer(u_t, 8); 102808449791SMatthew G. Knepley PetscValidPointer(a, 9); 102924cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 103024cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 103124cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 103208449791SMatthew G. Knepley if (locA) { 103308449791SMatthew G. Knepley PetscDS probAux; 103408449791SMatthew G. Knepley 103508449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 103624cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 103724cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 103824cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 103924cdb843SMatthew G. Knepley } 104008449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr); 104149073227SMatthew G. Knepley if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 104249073227SMatthew G. Knepley if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 104324cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 104408449791SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 104524cdb843SMatthew G. Knepley PetscInt i; 104624cdb843SMatthew G. Knepley 104708449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 10489f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i]; 104908449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 105008449791SMatthew G. Knepley if (locX_t) { 105108449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 10529f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i]; 105308449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 105424cdb843SMatthew G. Knepley } 105508449791SMatthew G. Knepley if (locA) { 10566da023fcSToby Isaac DM dmAuxPlex; 10576da023fcSToby Isaac 10586da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 10596da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10609f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i]; 10616da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10626da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 106324cdb843SMatthew G. Knepley } 106424cdb843SMatthew G. Knepley } 106508449791SMatthew G. Knepley PetscFunctionReturn(0); 106608449791SMatthew G. Knepley } 106724cdb843SMatthew G. Knepley 106808449791SMatthew G. Knepley #undef __FUNCT__ 106908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields" 107008449791SMatthew G. Knepley /*@C 107108449791SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 107208449791SMatthew G. Knepley 107308449791SMatthew G. Knepley Input Parameters: 107408449791SMatthew G. Knepley + dm - The DM 107508449791SMatthew G. Knepley . cStart - The first cell to include 107608449791SMatthew G. Knepley . cEnd - The first cell to exclude 107708449791SMatthew G. Knepley . locX - A local vector with the solution fields 107808449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 107908449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 108008449791SMatthew G. Knepley 108108449791SMatthew G. Knepley Output Parameters: 108208449791SMatthew G. Knepley + u - The field coefficients 108308449791SMatthew G. Knepley . u_t - The fields derivative coefficients 108408449791SMatthew G. Knepley - a - The auxiliary field coefficients 108508449791SMatthew G. Knepley 108608449791SMatthew G. Knepley Level: developer 108708449791SMatthew G. Knepley 108808449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 108908449791SMatthew G. Knepley @*/ 109008449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 109108449791SMatthew G. Knepley { 109208449791SMatthew G. Knepley PetscErrorCode ierr; 109308449791SMatthew G. Knepley 109408449791SMatthew G. Knepley PetscFunctionBegin; 109508449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr); 109649073227SMatthew G. Knepley if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);} 109749073227SMatthew G. Knepley if (*a) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);} 109808449791SMatthew G. Knepley PetscFunctionReturn(0); 109924cdb843SMatthew G. Knepley } 110008449791SMatthew G. Knepley 110108449791SMatthew G. Knepley #undef __FUNCT__ 110208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields" 110308449791SMatthew G. Knepley /*@C 110408449791SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 110508449791SMatthew G. Knepley 110608449791SMatthew G. Knepley Input Parameters: 110708449791SMatthew G. Knepley + dm - The DM 110808449791SMatthew G. Knepley . fStart - The first face to include 110908449791SMatthew G. Knepley . fEnd - The first face to exclude 111008449791SMatthew G. Knepley . locX - A local vector with the solution fields 111108449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 111208449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 111308449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 111408449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 111508449791SMatthew G. Knepley 111608449791SMatthew G. Knepley Output Parameters: 1117477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1118477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 111908449791SMatthew G. Knepley 112008449791SMatthew G. Knepley Level: developer 112108449791SMatthew G. Knepley 112208449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 112308449791SMatthew G. Knepley @*/ 112408449791SMatthew 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) 112508449791SMatthew G. Knepley { 112608449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 1127195142f5SMatthew G. Knepley PetscSection section; 112808449791SMatthew G. Knepley PetscDS prob; 112908449791SMatthew G. Knepley DMLabel ghostLabel; 113008449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1131477f7dfdSMatthew G. Knepley PetscBool *isFE; 1132477f7dfdSMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 113308449791SMatthew G. Knepley PetscErrorCode ierr; 113408449791SMatthew G. Knepley 113508449791SMatthew G. Knepley PetscFunctionBegin; 113608449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 113708449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 113808449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 113908449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 114008449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 114108449791SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 114208449791SMatthew G. Knepley PetscValidPointer(uL, 9); 114308449791SMatthew G. Knepley PetscValidPointer(uR, 10); 114408449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 114508449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1146195142f5SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1147477f7dfdSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1148477f7dfdSMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1149477f7dfdSMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1150477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1151477f7dfdSMatthew G. Knepley PetscObject obj; 1152477f7dfdSMatthew G. Knepley PetscClassId id; 1153477f7dfdSMatthew G. Knepley 1154477f7dfdSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 1155477f7dfdSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1156477f7dfdSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1157477f7dfdSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1158477f7dfdSMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 1159477f7dfdSMatthew G. Knepley } 1160c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 116108449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 116208449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 116308449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 116408449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 116508449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 116608449791SMatthew G. Knepley if (locGrad) { 116708449791SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 116808449791SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 116924cdb843SMatthew G. Knepley } 1170477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr); 1171477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr); 1172477f7dfdSMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 117308449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 117408449791SMatthew G. Knepley const PetscInt *cells; 1175640bce14SSatish Balay PetscFVFaceGeom *fg; 1176640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 1177640bce14SSatish Balay PetscScalar *xL, *xR, *gL, *gR; 117808449791SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 1179e697831aSToby Isaac PetscInt ghost, nsupp; 118008449791SMatthew G. Knepley 118108449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1182e697831aSToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 118308449791SMatthew G. Knepley if (ghost >= 0) continue; 118408449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 118508449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 118608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 118708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1188477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1189477f7dfdSMatthew G. Knepley PetscInt off; 1190477f7dfdSMatthew G. Knepley 119137a43ebbSMatthew G. Knepley ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1192477f7dfdSMatthew G. Knepley if (isFE[f]) { 1193477f7dfdSMatthew G. Knepley const PetscInt *cone; 1194cca9989dSMatthew G. Knepley PetscInt comp, coneSize, faceLocL, faceLocR, ldof, rdof, d; 1195477f7dfdSMatthew G. Knepley 1196cca9989dSMatthew G. Knepley xL = xR = NULL; 1197cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1198cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1199477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 1200477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr); 1201477f7dfdSMatthew G. Knepley for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break; 1202477f7dfdSMatthew 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]); 1203477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 1204477f7dfdSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr); 1205477f7dfdSMatthew G. Knepley for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break; 1206477f7dfdSMatthew 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]); 1207195142f5SMatthew G. Knepley /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 1208477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 1209cca9989dSMatthew G. Knepley if (rdof == ldof) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 1210cca9989dSMatthew 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];} 1211cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1212cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1213477f7dfdSMatthew G. Knepley } else { 1214477f7dfdSMatthew G. Knepley PetscFV fv; 1215477f7dfdSMatthew G. Knepley PetscInt numComp, c; 1216477f7dfdSMatthew G. Knepley 1217477f7dfdSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1218477f7dfdSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1219e697831aSToby Isaac if (nsupp > 2) { 1220e697831aSToby Isaac for (f = 0; f < Nf; ++f) { 1221e697831aSToby Isaac PetscInt off; 1222e697831aSToby Isaac 1223e697831aSToby Isaac ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1224e697831aSToby Isaac ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1225e697831aSToby Isaac for (c = 0; c < numComp; ++c) { 1226e697831aSToby Isaac uLl[iface*Nc+off+c] = 0.; 1227e697831aSToby Isaac uRl[iface*Nc+off+c] = 0.; 1228e697831aSToby Isaac } 1229e697831aSToby Isaac } 1230e697831aSToby Isaac continue; 1231e697831aSToby Isaac } 1232cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1233cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 123408449791SMatthew G. Knepley if (dmGrad) { 123508449791SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 123608449791SMatthew G. Knepley 123708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 123808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 123908449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 124008449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1241477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1242477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1243477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 124408449791SMatthew G. Knepley } 124508449791SMatthew G. Knepley } else { 1246477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1247477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 1248477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 1249477f7dfdSMatthew G. Knepley } 1250477f7dfdSMatthew G. Knepley } 125108449791SMatthew G. Knepley } 125208449791SMatthew G. Knepley } 125308449791SMatthew G. Knepley ++iface; 125408449791SMatthew G. Knepley } 125508449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 125608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 125708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 125808449791SMatthew G. Knepley if (locGrad) { 125908449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 126008449791SMatthew G. Knepley } 1261477f7dfdSMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 126208449791SMatthew G. Knepley PetscFunctionReturn(0); 126308449791SMatthew G. Knepley } 126408449791SMatthew G. Knepley 126508449791SMatthew G. Knepley #undef __FUNCT__ 126608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields" 126708449791SMatthew G. Knepley /*@C 126808449791SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 126908449791SMatthew G. Knepley 127008449791SMatthew G. Knepley Input Parameters: 127108449791SMatthew G. Knepley + dm - The DM 127208449791SMatthew G. Knepley . fStart - The first face to include 127308449791SMatthew G. Knepley . fEnd - The first face to exclude 127408449791SMatthew G. Knepley . locX - A local vector with the solution fields 127508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 127608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 127708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 127808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 127908449791SMatthew G. Knepley 128008449791SMatthew G. Knepley Output Parameters: 1281477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face 1282477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 128308449791SMatthew G. Knepley 128408449791SMatthew G. Knepley Level: developer 128508449791SMatthew G. Knepley 128608449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 128708449791SMatthew G. Knepley @*/ 128808449791SMatthew 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) 128908449791SMatthew G. Knepley { 129008449791SMatthew G. Knepley PetscErrorCode ierr; 129108449791SMatthew G. Knepley 129208449791SMatthew G. Knepley PetscFunctionBegin; 129308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr); 129408449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr); 129508449791SMatthew G. Knepley PetscFunctionReturn(0); 129608449791SMatthew G. Knepley } 129708449791SMatthew G. Knepley 129808449791SMatthew G. Knepley #undef __FUNCT__ 129908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry" 130008449791SMatthew G. Knepley /*@C 130108449791SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 130208449791SMatthew G. Knepley 130308449791SMatthew G. Knepley Input Parameters: 130408449791SMatthew G. Knepley + dm - The DM 130508449791SMatthew G. Knepley . fStart - The first face to include 130608449791SMatthew G. Knepley . fEnd - The first face to exclude 130708449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 130808449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 130908449791SMatthew G. Knepley 131008449791SMatthew G. Knepley Output Parameters: 131108449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 131208449791SMatthew G. Knepley - vol - The cell volume 131308449791SMatthew G. Knepley 131408449791SMatthew G. Knepley Level: developer 131508449791SMatthew G. Knepley 131608449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 131708449791SMatthew G. Knepley @*/ 131808449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 131908449791SMatthew G. Knepley { 132008449791SMatthew G. Knepley DM dmFace, dmCell; 132108449791SMatthew G. Knepley DMLabel ghostLabel; 132208449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 132308449791SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 132408449791SMatthew G. Knepley PetscErrorCode ierr; 132508449791SMatthew G. Knepley 132608449791SMatthew G. Knepley PetscFunctionBegin; 132708449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 132808449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 132908449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 133008449791SMatthew G. Knepley PetscValidPointer(fgeom, 6); 133108449791SMatthew G. Knepley PetscValidPointer(vol, 7); 133208449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1333c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 133408449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 133508449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 133608449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 133708449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 133808449791SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 133908449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr); 134008449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 134108449791SMatthew G. Knepley const PetscInt *cells; 1342640bce14SSatish Balay PetscFVFaceGeom *fg; 1343640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 134408449791SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 13452eefff9cSMatthew G. Knepley PetscReal *voll = *vol; 134608449791SMatthew G. Knepley PetscInt ghost, d; 134708449791SMatthew G. Knepley 134808449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 134908449791SMatthew G. Knepley if (ghost >= 0) continue; 135008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 135108449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 135208449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 135308449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 135408449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 135508449791SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 135608449791SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 135708449791SMatthew G. Knepley } 135808449791SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 135908449791SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 136008449791SMatthew G. Knepley ++iface; 136108449791SMatthew G. Knepley } 136208449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 136308449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 136408449791SMatthew G. Knepley PetscFunctionReturn(0); 136508449791SMatthew G. Knepley } 136608449791SMatthew G. Knepley 136708449791SMatthew G. Knepley #undef __FUNCT__ 136808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry" 136908449791SMatthew G. Knepley /*@C 137008449791SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 137108449791SMatthew G. Knepley 137208449791SMatthew G. Knepley Input Parameters: 137308449791SMatthew G. Knepley + dm - The DM 137408449791SMatthew G. Knepley . fStart - The first face to include 137508449791SMatthew G. Knepley . fEnd - The first face to exclude 137608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 137708449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 137808449791SMatthew G. Knepley 137908449791SMatthew G. Knepley Output Parameters: 138008449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal 138108449791SMatthew G. Knepley - vol - The cell volume 138208449791SMatthew G. Knepley 138308449791SMatthew G. Knepley Level: developer 138408449791SMatthew G. Knepley 138508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 138608449791SMatthew G. Knepley @*/ 138708449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol) 138808449791SMatthew G. Knepley { 138908449791SMatthew G. Knepley PetscErrorCode ierr; 139008449791SMatthew G. Knepley 139108449791SMatthew G. Knepley PetscFunctionBegin; 139208449791SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 139308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr); 139408449791SMatthew G. Knepley PetscFunctionReturn(0); 139508449791SMatthew G. Knepley } 139608449791SMatthew G. Knepley 139708449791SMatthew G. Knepley #undef __FUNCT__ 139808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal" 139911dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 140008449791SMatthew G. Knepley { 140108449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 140208449791SMatthew G. Knepley PetscSection section; 140308449791SMatthew G. Knepley PetscDS prob; 140408449791SMatthew G. Knepley DMLabel depth; 140508449791SMatthew G. Knepley PetscFECellGeom *cgeom; 140608449791SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *elemVec = NULL; 140708449791SMatthew G. Knepley PetscInt dim, Nf, f, totDimBd, numBd, bd; 140808449791SMatthew G. Knepley PetscErrorCode ierr; 140908449791SMatthew G. Knepley 141008449791SMatthew G. Knepley PetscFunctionBegin; 141108449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 141208449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 141308449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 141408449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 141508449791SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 141624cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 1417a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 141824cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 141924cdb843SMatthew G. Knepley const char *bdLabel; 142024cdb843SMatthew G. Knepley DMLabel label; 142124cdb843SMatthew G. Knepley IS pointIS; 142224cdb843SMatthew G. Knepley const PetscInt *points; 142324cdb843SMatthew G. Knepley const PetscInt *values; 1424a8e83e26SSanderA PetscInt field, numValues, v, numPoints, p, dep, numFaces; 142524cdb843SMatthew G. Knepley PetscBool isEssential; 142680bc4632SMatthew G. Knepley PetscObject obj; 142780bc4632SMatthew G. Knepley PetscClassId id; 142824cdb843SMatthew G. Knepley 1429a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 143080bc4632SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 143180bc4632SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 143280bc4632SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || isEssential) continue; 1433c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1434a8e83e26SSanderA for (v = 0; v < numValues; ++v) { 1435a8e83e26SSanderA ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr); 1436a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 143722734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 143824cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 143924cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 144024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 144124cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 144224cdb843SMatthew G. Knepley } 1443bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 144408449791SMatthew G. Knepley if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 144524cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 144624cdb843SMatthew G. Knepley const PetscInt point = points[p]; 144724cdb843SMatthew G. Knepley PetscScalar *x = NULL; 144824cdb843SMatthew G. Knepley PetscInt i; 144924cdb843SMatthew G. Knepley 145024cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 145124cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1452bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1453302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 1454bbce034cSMatthew 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); 1455929b1830SToby Isaac /* TODO: Matt, this is wrong if feBd does not match fe: i.e., if the order differs. */ 145608449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 145724cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 145808449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 145908449791SMatthew G. Knepley if (locX_t) { 146008449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 146124cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 146208449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 146324cdb843SMatthew G. Knepley } 146424cdb843SMatthew G. Knepley ++f; 146524cdb843SMatthew G. Knepley } 146624cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 146724cdb843SMatthew G. Knepley PetscFE fe; 146808449791SMatthew G. Knepley PetscQuadrature q; 146924cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 147024cdb843SMatthew G. Knepley /* Conforming batches */ 147124cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 147224cdb843SMatthew G. Knepley /* Remainder */ 147324cdb843SMatthew G. Knepley PetscInt Nr, offset; 147424cdb843SMatthew G. Knepley 147524cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 147624cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 147724cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 147824cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 147924cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 148024cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 148124cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 148224cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 148324cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 148424cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 148524cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 148624cdb843SMatthew G. Knepley offset = numFaces - Nr; 148711dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, t, elemVec);CHKERRQ(ierr); 148811dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, t, &elemVec[offset*totDimBd]);CHKERRQ(ierr); 148924cdb843SMatthew G. Knepley } 149024cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 149124cdb843SMatthew G. Knepley const PetscInt point = points[p]; 149224cdb843SMatthew G. Knepley 149324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 149424cdb843SMatthew G. Knepley if (dep != dim-1) continue; 149524cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 1496c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_ALL_VALUES);CHKERRQ(ierr); 149724cdb843SMatthew G. Knepley ++f; 149824cdb843SMatthew G. Knepley } 149924cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 150024cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1501bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 150208449791SMatthew G. Knepley if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 150324cdb843SMatthew G. Knepley } 1504a8e83e26SSanderA } 150508449791SMatthew G. Knepley PetscFunctionReturn(0); 150608449791SMatthew G. Knepley } 150708449791SMatthew G. Knepley 150808449791SMatthew G. Knepley #undef __FUNCT__ 1509f2c5ccc7SToby Isaac #define __FUNCT__ "DMPlexReconstructGradientsFVM" 1510f2c5ccc7SToby Isaac /*@ 1511f2c5ccc7SToby Isaac DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method. 1512f2c5ccc7SToby Isaac 1513f2c5ccc7SToby Isaac Input Parameters: 1514f2c5ccc7SToby Isaac + dm - the mesh 1515f2c5ccc7SToby Isaac - locX - the local representation of the vector 1516f2c5ccc7SToby Isaac 1517f2c5ccc7SToby Isaac Output Parameter: 1518f2c5ccc7SToby Isaac . grad - the global representation of the gradient 1519f2c5ccc7SToby Isaac 1520f2c5ccc7SToby Isaac Level: developer 1521f2c5ccc7SToby Isaac 1522f2c5ccc7SToby Isaac .seealso: DMPlexSNESGetGradientDM() 1523f2c5ccc7SToby Isaac @*/ 1524f2c5ccc7SToby Isaac PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad) 1525f2c5ccc7SToby Isaac { 1526f2c5ccc7SToby Isaac PetscDS prob; 1527f2c5ccc7SToby Isaac PetscInt Nf, f, fStart, fEnd; 15289fc93327SToby Isaac PetscBool useFVM = PETSC_FALSE; 1529f2c5ccc7SToby Isaac PetscFV fvm = NULL; 1530f2c5ccc7SToby Isaac Vec faceGeometryFVM, cellGeometryFVM; 1531f2c5ccc7SToby Isaac PetscFVCellGeom *cgeomFVM = NULL; 1532f2c5ccc7SToby Isaac PetscFVFaceGeom *fgeomFVM = NULL; 1533f2c5ccc7SToby Isaac DM dmGrad = NULL; 1534f2c5ccc7SToby Isaac PetscErrorCode ierr; 1535f2c5ccc7SToby Isaac 1536f2c5ccc7SToby Isaac PetscFunctionBegin; 1537f2c5ccc7SToby Isaac ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1538f2c5ccc7SToby Isaac ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1539f2c5ccc7SToby Isaac for (f = 0; f < Nf; ++f) { 1540f2c5ccc7SToby Isaac PetscObject obj; 1541f2c5ccc7SToby Isaac PetscClassId id; 1542f2c5ccc7SToby Isaac 1543f2c5ccc7SToby Isaac ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1544f2c5ccc7SToby Isaac ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1545f2c5ccc7SToby Isaac if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 1546f2c5ccc7SToby Isaac } 1547f2c5ccc7SToby Isaac if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization"); 1548f2c5ccc7SToby Isaac ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 1549f2c5ccc7SToby Isaac if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients"); 1550f2c5ccc7SToby Isaac ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 1551f2c5ccc7SToby Isaac ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 1552f2c5ccc7SToby Isaac ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1553f2c5ccc7SToby Isaac ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1554de555695SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1555f2c5ccc7SToby Isaac PetscFunctionReturn(0); 1556f2c5ccc7SToby Isaac } 1557f2c5ccc7SToby Isaac 1558f2c5ccc7SToby Isaac #undef __FUNCT__ 155908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal" 156011dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 156108449791SMatthew G. Knepley { 156208449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 156308449791SMatthew G. Knepley const char *name = "Residual"; 156408449791SMatthew G. Knepley DM dmAux = NULL; 156508449791SMatthew G. Knepley DM dmGrad = NULL; 156608449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 156708449791SMatthew G. Knepley PetscDS prob = NULL; 156808449791SMatthew G. Knepley PetscDS probAux = NULL; 156908449791SMatthew G. Knepley PetscSection section = NULL; 157008449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 157108449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 1572b2666ceaSMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 157308449791SMatthew G. Knepley PetscFV fvm = NULL; 157408449791SMatthew G. Knepley PetscFECellGeom *cgeomFEM = NULL; 15752f84e9bcSToby Isaac PetscScalar *cgeomScal; 157608449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 157708449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 157808449791SMatthew G. Knepley Vec locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 157908449791SMatthew G. Knepley PetscScalar *u, *u_t, *a, *uL, *uR; 1580c4d4a4f8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 158108449791SMatthew G. Knepley PetscErrorCode ierr; 158208449791SMatthew G. Knepley 158308449791SMatthew G. Knepley PetscFunctionBegin; 158408449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 158508449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1586195142f5SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 158708449791SMatthew G. Knepley /* FEM+FVM */ 158808449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 158908449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1590c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 159108449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 159208449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 159308449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 159408449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 159508449791SMatthew G. Knepley if (locA) { 159608449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 159708449791SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 159808449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 159908449791SMatthew G. Knepley } 160008449791SMatthew G. Knepley /* 2: Get geometric data */ 160108449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 160208449791SMatthew G. Knepley PetscObject obj; 160308449791SMatthew G. Knepley PetscClassId id; 16047173168dSMatthew G. Knepley PetscBool fimp; 160508449791SMatthew G. Knepley 16067173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 16077173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 160808449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 160908449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 161008449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 161108449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 161208449791SMatthew G. Knepley } 161308449791SMatthew G. Knepley if (useFEM) { 161408449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr); 16152f84e9bcSToby Isaac ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr); 16162f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 16172f84e9bcSToby Isaac DM dmCell; 16182f84e9bcSToby Isaac PetscInt c; 16192f84e9bcSToby Isaac 16202f84e9bcSToby Isaac ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr); 16212f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr); 16222f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 16232f84e9bcSToby Isaac PetscScalar *thisgeom; 16242f84e9bcSToby Isaac 16252f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 16262f84e9bcSToby Isaac cgeomFEM[c] = *((PetscFECellGeom *) thisgeom); 16272f84e9bcSToby Isaac } 16282f84e9bcSToby Isaac } 16292f84e9bcSToby Isaac else { 16302f84e9bcSToby Isaac cgeomFEM = (PetscFECellGeom *) cgeomScal; 16312f84e9bcSToby Isaac } 163208449791SMatthew G. Knepley } 163308449791SMatthew G. Knepley if (useFVM) { 163408449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 163508449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 163608449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 163708449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 163808449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 163908449791SMatthew G. Knepley if (dmGrad) { 164008449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 164108449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1642de555695SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 164308449791SMatthew G. Knepley /* Communicate gradient values */ 164408449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 164508449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 164608449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 164708449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 164808449791SMatthew G. Knepley } 1649bdd6f66aSToby Isaac /* Handle non-essential (e.g. outflow) boundary values */ 1650bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 165108449791SMatthew G. Knepley } 165208449791SMatthew G. Knepley /* Loop over chunks */ 165308449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 165408449791SMatthew G. Knepley numChunks = 1; 165508449791SMatthew G. Knepley cellChunkSize = (cEnd - cStart)/numChunks; 165608449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 165708449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 16582eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 16592eefff9cSMatthew G. Knepley PetscReal *vol; 166008449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 166108449791SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell; 166208449791SMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face; 166308449791SMatthew G. Knepley 166408449791SMatthew G. Knepley /* Extract field coefficients */ 166508449791SMatthew G. Knepley if (useFEM) { 166608449791SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 166708449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1668215c4595SMatthew G. Knepley ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 166908449791SMatthew G. Knepley } 167008449791SMatthew G. Knepley if (useFVM) { 167108449791SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 167208449791SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 167308449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 167408449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1675215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1676215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 167708449791SMatthew G. Knepley } 167808449791SMatthew 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 */ 167908449791SMatthew G. Knepley /* Loop over fields */ 168008449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 168108449791SMatthew G. Knepley PetscObject obj; 168208449791SMatthew G. Knepley PetscClassId id; 16837173168dSMatthew G. Knepley PetscBool fimp; 168408449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 168508449791SMatthew G. Knepley 16867173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 16877173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 168808449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 168908449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 169008449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 169108449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 169208449791SMatthew G. Knepley PetscQuadrature q; 169308449791SMatthew G. Knepley PetscInt Nq, Nb; 169408449791SMatthew G. Knepley 169508449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 169608449791SMatthew G. Knepley 169708449791SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 169808449791SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 169908449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 170008449791SMatthew G. Knepley blockSize = Nb*Nq; 170108449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 170208449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 170308449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 170408449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 170508449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 170608449791SMatthew G. Knepley offset = numCells - Nr; 170708449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 170808449791SMatthew 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) */ 170911dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 171011dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 171108449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 171208449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 171308449791SMatthew G. Knepley 171408449791SMatthew G. Knepley Ne = numFaces; 171508449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 171608449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 171708449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 171808449791SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 171908449791SMatthew G. Knepley } 17202f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 17212f84e9bcSToby Isaac ierr = PetscFree(cgeomFEM);CHKERRQ(ierr); 17222f84e9bcSToby Isaac } 17232f84e9bcSToby Isaac else { 17242f84e9bcSToby Isaac cgeomFEM = NULL; 17252f84e9bcSToby Isaac } 17260163fd50SMatthew G. Knepley if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);} 172708449791SMatthew G. Knepley /* Loop over domain */ 172808449791SMatthew G. Knepley if (useFEM) { 172908449791SMatthew G. Knepley /* Add elemVec to locX */ 173008449791SMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 173108449791SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);} 1732c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 173308449791SMatthew G. Knepley } 173408449791SMatthew G. Knepley } 173508449791SMatthew G. Knepley if (useFVM) { 17364a394323SMatthew G. Knepley PetscScalar *fa; 173708449791SMatthew G. Knepley PetscInt iface; 173808449791SMatthew G. Knepley 173908449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1740c10b5f1bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1741c10b5f1bSMatthew G. Knepley PetscFV fv; 1742c10b5f1bSMatthew G. Knepley PetscObject obj; 1743c10b5f1bSMatthew G. Knepley PetscClassId id; 17444a394323SMatthew G. Knepley PetscInt foff, pdim; 1745c10b5f1bSMatthew G. Knepley 1746c10b5f1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1747c10b5f1bSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1748c10b5f1bSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1749c10b5f1bSMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1750c10b5f1bSMatthew G. Knepley fv = (PetscFV) obj; 1751c10b5f1bSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1752c10b5f1bSMatthew G. Knepley /* Accumulate fluxes to cells */ 175308449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 175408449791SMatthew G. Knepley const PetscInt *cells; 175508449791SMatthew G. Knepley PetscScalar *fL, *fR; 1756ffe9ad51SToby Isaac PetscInt ghost, d, nsupp; 175708449791SMatthew G. Knepley 175808449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1759ffe9ad51SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 17601993971cSToby Isaac if (ghost >= 0) continue; 17611993971cSToby Isaac if (nsupp > 2) { /* noop */ 17621993971cSToby Isaac ++iface; 17631993971cSToby Isaac continue; 17641993971cSToby Isaac } 176508449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1766c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr); 1767c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr); 1768c10b5f1bSMatthew G. Knepley for (d = 0; d < pdim; ++d) { 1769c10b5f1bSMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1770c10b5f1bSMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 177108449791SMatthew G. Knepley } 177208449791SMatthew G. Knepley ++iface; 177308449791SMatthew G. Knepley } 1774dab51205SMatthew G. Knepley } 1775dab51205SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1776dab51205SMatthew G. Knepley } 1777c10b5f1bSMatthew G. Knepley /* Handle time derivative */ 1778c10b5f1bSMatthew G. Knepley if (locX_t) { 1779dab51205SMatthew G. Knepley PetscScalar *x_t, *fa; 1780dab51205SMatthew G. Knepley 1781dab51205SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1782c10b5f1bSMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1783dab51205SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1784dab51205SMatthew G. Knepley PetscFV fv; 1785dab51205SMatthew G. Knepley PetscObject obj; 1786dab51205SMatthew G. Knepley PetscClassId id; 1787dab51205SMatthew G. Knepley PetscInt pdim, d; 1788dab51205SMatthew G. Knepley 1789dab51205SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1790dab51205SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1791dab51205SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1792dab51205SMatthew G. Knepley fv = (PetscFV) obj; 1793dab51205SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1794c10b5f1bSMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 1795c10b5f1bSMatthew G. Knepley PetscScalar *u_t, *r; 1796c10b5f1bSMatthew G. Knepley 1797c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1798c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1799d63b37e5SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1800c10b5f1bSMatthew G. Knepley } 1801dab51205SMatthew G. Knepley } 1802c10b5f1bSMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 180308449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 180408449791SMatthew G. Knepley } 180508449791SMatthew G. Knepley if (useFEM) { 180608449791SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 180708449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 180808449791SMatthew G. Knepley } 180908449791SMatthew G. Knepley if (useFVM) { 181008449791SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr); 181108449791SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr); 181208449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 181308449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 181408449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 181508449791SMatthew G. Knepley } 181608449791SMatthew G. Knepley } 181708449791SMatthew G. Knepley 181811dd639bSMatthew G. Knepley if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);} 181908449791SMatthew G. Knepley 182008449791SMatthew G. Knepley /* FEM */ 182108449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 182208449791SMatthew G. Knepley /* 2: Get geometric data */ 182308449791SMatthew G. Knepley /* 3: Handle boundary values */ 182408449791SMatthew G. Knepley /* 4: Loop over domain */ 182508449791SMatthew G. Knepley /* Extract coefficients */ 182608449791SMatthew G. Knepley /* Loop over fields */ 182708449791SMatthew G. Knepley /* Set tiling for FE*/ 182808449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 182908449791SMatthew G. Knepley /* Loop over subdomain */ 183008449791SMatthew G. Knepley /* Loop over quad points */ 183108449791SMatthew G. Knepley /* Transform coords to real space */ 183208449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 183308449791SMatthew G. Knepley /* Evaluate residual at point */ 183408449791SMatthew G. Knepley /* Transform residual to real space */ 183508449791SMatthew G. Knepley /* Add residual to elemVec */ 183608449791SMatthew G. Knepley /* Loop over domain */ 183708449791SMatthew G. Knepley /* Add elemVec to locX */ 183808449791SMatthew G. Knepley 183908449791SMatthew G. Knepley /* FVM */ 184008449791SMatthew G. Knepley /* Get geometric data */ 184108449791SMatthew G. Knepley /* If using gradients */ 184208449791SMatthew G. Knepley /* Compute gradient data */ 184308449791SMatthew G. Knepley /* Loop over domain faces */ 184408449791SMatthew G. Knepley /* Count computational faces */ 184508449791SMatthew G. Knepley /* Reconstruct cell gradient */ 184608449791SMatthew G. Knepley /* Loop over domain cells */ 184708449791SMatthew G. Knepley /* Limit cell gradients */ 184808449791SMatthew G. Knepley /* Handle boundary values */ 184908449791SMatthew G. Knepley /* Loop over domain faces */ 185008449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 185108449791SMatthew G. Knepley /* Riemann solve over faces */ 185208449791SMatthew G. Knepley /* Loop over domain faces */ 185308449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 185408449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 1855247ba720SToby Isaac if (mesh->printFEM) { 1856247ba720SToby Isaac Vec locFbc; 1857247ba720SToby Isaac PetscInt pStart, pEnd, p, maxDof; 1858247ba720SToby Isaac PetscScalar *zeroes; 1859247ba720SToby Isaac 1860247ba720SToby Isaac ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 1861247ba720SToby Isaac ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 1862247ba720SToby Isaac ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 1863247ba720SToby Isaac ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 1864247ba720SToby Isaac ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 1865247ba720SToby Isaac for (p = pStart; p < pEnd; p++) { 1866247ba720SToby Isaac ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 1867247ba720SToby Isaac } 1868247ba720SToby Isaac ierr = PetscFree(zeroes);CHKERRQ(ierr); 1869247ba720SToby Isaac ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 1870247ba720SToby Isaac ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 1871247ba720SToby Isaac } 187224cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 187324cdb843SMatthew G. Knepley PetscFunctionReturn(0); 187424cdb843SMatthew G. Knepley } 187524cdb843SMatthew G. Knepley 187624cdb843SMatthew G. Knepley #undef __FUNCT__ 187724cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 187811dd639bSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user) 187924cdb843SMatthew G. Knepley { 188024cdb843SMatthew G. Knepley DM dmCh, dmAux; 1881bbce034cSMatthew G. Knepley Vec A, cellgeom; 188224cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 188324cdb843SMatthew G. Knepley PetscQuadrature q; 188424cdb843SMatthew G. Knepley PetscSection section, sectionAux; 18852f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 18862f84e9bcSToby Isaac PetscScalar *cgeomScal; 188724cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 188824cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 188924cdb843SMatthew G. Knepley PetscInt totDim, totDimAux, diffCell = 0; 189024cdb843SMatthew G. Knepley PetscErrorCode ierr; 189124cdb843SMatthew G. Knepley 189224cdb843SMatthew G. Knepley PetscFunctionBegin; 189324cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 189424cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 189524cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 189624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 189724cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 189824cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 189924cdb843SMatthew G. Knepley numCells = cEnd - cStart; 190024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 190124cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 190224cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 190324cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 190424cdb843SMatthew G. Knepley if (dmAux) { 190524cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 190624cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 190724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 190824cdb843SMatthew G. Knepley } 190924cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1910bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 191124cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 191224cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1913bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 19142f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 19152f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 19162f84e9bcSToby Isaac DM dmCell; 19172f84e9bcSToby Isaac 19182f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 19192f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 19202f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 19217726bd6bSToby Isaac PetscScalar *thisgeom; 19222f84e9bcSToby Isaac 19232f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 19242f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 19252f84e9bcSToby Isaac } 19262f84e9bcSToby Isaac } 19272f84e9bcSToby Isaac else { 19282f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 19292f84e9bcSToby Isaac } 193024cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 193124cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 193224cdb843SMatthew G. Knepley PetscInt i; 193324cdb843SMatthew G. Knepley 193424cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 193524cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 193624cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 193724cdb843SMatthew G. Knepley if (X_t) { 193824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 193924cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 194024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 194124cdb843SMatthew G. Knepley } 194224cdb843SMatthew G. Knepley if (dmAux) { 19436da023fcSToby Isaac DM dmAuxPlex; 19446da023fcSToby Isaac 19456da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 19466da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 194724cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 19486da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 19496da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 195024cdb843SMatthew G. Knepley } 195124cdb843SMatthew G. Knepley } 195224cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 195324cdb843SMatthew G. Knepley PetscFE fe, feCh; 195424cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 195524cdb843SMatthew G. Knepley /* Conforming batches */ 195624cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 195724cdb843SMatthew G. Knepley /* Remainder */ 195824cdb843SMatthew G. Knepley PetscInt Nr, offset; 195924cdb843SMatthew G. Knepley 196024cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 196124cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 196224cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 196324cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 196424cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 196524cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 196624cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 196724cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 196824cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 196924cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 197024cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 197124cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 197224cdb843SMatthew G. Knepley offset = numCells - Nr; 197311dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 197411dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr); 197511dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 197611dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVecCh[offset*totDim]);CHKERRQ(ierr); 197724cdb843SMatthew G. Knepley } 197824cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 197924cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 198024cdb843SMatthew G. Knepley PetscInt d; 198124cdb843SMatthew G. Knepley 198224cdb843SMatthew 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;} 198324cdb843SMatthew G. Knepley if (diff) { 198424cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 198524cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 198624cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 198724cdb843SMatthew G. Knepley ++diffCell; 198824cdb843SMatthew G. Knepley } 198924cdb843SMatthew G. Knepley if (diffCell > 9) break; 1990c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 199124cdb843SMatthew G. Knepley } 19922f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 19932f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 19942f84e9bcSToby Isaac } 19952f84e9bcSToby Isaac else { 19962f84e9bcSToby Isaac cgeom = NULL; 19972f84e9bcSToby Isaac } 19982f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 1999bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 200024cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 200124cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 200224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 200324cdb843SMatthew G. Knepley } 200424cdb843SMatthew G. Knepley 200524cdb843SMatthew G. Knepley #undef __FUNCT__ 200624cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 200724cdb843SMatthew G. Knepley /*@ 200824cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 200924cdb843SMatthew G. Knepley 201024cdb843SMatthew G. Knepley Input Parameters: 201124cdb843SMatthew G. Knepley + dm - The mesh 201224cdb843SMatthew G. Knepley . X - Local solution 201324cdb843SMatthew G. Knepley - user - The user context 201424cdb843SMatthew G. Knepley 201524cdb843SMatthew G. Knepley Output Parameter: 201624cdb843SMatthew G. Knepley . F - Local output vector 201724cdb843SMatthew G. Knepley 201824cdb843SMatthew G. Knepley Level: developer 201924cdb843SMatthew G. Knepley 202024cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 202124cdb843SMatthew G. Knepley @*/ 202224cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 202324cdb843SMatthew G. Knepley { 202424cdb843SMatthew G. Knepley PetscObject check; 2025c4d4a4f8SMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 20266da023fcSToby Isaac DM plex; 202724cdb843SMatthew G. Knepley PetscErrorCode ierr; 202824cdb843SMatthew G. Knepley 202924cdb843SMatthew G. Knepley PetscFunctionBegin; 20306da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 20316da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 20326da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2033c4d4a4f8SMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 203424cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 20356da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 203611dd639bSMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 203711dd639bSMatthew G. Knepley else {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 20389a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 203924cdb843SMatthew G. Knepley PetscFunctionReturn(0); 204024cdb843SMatthew G. Knepley } 204124cdb843SMatthew G. Knepley 204224cdb843SMatthew G. Knepley #undef __FUNCT__ 2043bdd6f66aSToby Isaac #define __FUNCT__ "DMPlexSNESComputeBoundaryFEM" 2044bdd6f66aSToby Isaac /*@ 2045bdd6f66aSToby Isaac DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 2046bdd6f66aSToby Isaac 2047bdd6f66aSToby Isaac Input Parameters: 2048bdd6f66aSToby Isaac + dm - The mesh 2049bdd6f66aSToby Isaac - user - The user context 2050bdd6f66aSToby Isaac 2051bdd6f66aSToby Isaac Output Parameter: 2052bdd6f66aSToby Isaac . X - Local solution 2053bdd6f66aSToby Isaac 2054bdd6f66aSToby Isaac Level: developer 2055bdd6f66aSToby Isaac 2056bdd6f66aSToby Isaac .seealso: DMPlexComputeJacobianActionFEM() 2057bdd6f66aSToby Isaac @*/ 2058bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 2059bdd6f66aSToby Isaac { 2060bdd6f66aSToby Isaac DM plex; 2061bdd6f66aSToby Isaac PetscErrorCode ierr; 2062bdd6f66aSToby Isaac 2063bdd6f66aSToby Isaac PetscFunctionBegin; 2064bdd6f66aSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2065bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 2066bdd6f66aSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 2067bdd6f66aSToby Isaac PetscFunctionReturn(0); 2068bdd6f66aSToby Isaac } 2069bdd6f66aSToby Isaac 2070bdd6f66aSToby Isaac #undef __FUNCT__ 2071b953af5fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobian_Internal" 2072b953af5fSMatthew 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) 207324cdb843SMatthew G. Knepley { 207424cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 207524cdb843SMatthew G. Knepley const char *name = "Jacobian"; 2076f7ed7b21SMatthew G. Knepley DM dmAux, plex; 207724cdb843SMatthew G. Knepley DMLabel depth; 2078bbce034cSMatthew G. Knepley Vec A, cellgeom; 207924cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 208024cdb843SMatthew G. Knepley PetscQuadrature quad; 208124cdb843SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 20822f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 20832f84e9bcSToby Isaac PetscScalar *cgeomScal; 2084426ff135SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 2085b953af5fSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, c; 208624cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 20875244db28SMatthew G. Knepley PetscBool isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE; 208824cdb843SMatthew G. Knepley PetscErrorCode ierr; 208924cdb843SMatthew G. Knepley 209024cdb843SMatthew G. Knepley PetscFunctionBegin; 209124cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 209224cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 209324cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 209424cdb843SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 209524cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 209624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 209724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 2098efc10488SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 209955ad3c34SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2100426ff135SMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2101426ff135SMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 210224cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 210324cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 210424cdb843SMatthew G. Knepley numCells = cEnd - cStart; 210524cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 210624cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 210724cdb843SMatthew G. Knepley if (dmAux) { 2108f7ed7b21SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2109f7ed7b21SMatthew G. Knepley ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 211024cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 211124cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 211224cdb843SMatthew G. Knepley } 211324cdb843SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 2114efc10488SMatthew G. Knepley ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr); 211524cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2116bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 21172f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 21182f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 21192f84e9bcSToby Isaac DM dmCell; 21202f84e9bcSToby Isaac 21212f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 21222f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 21232f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 21247726bd6bSToby Isaac PetscScalar *thisgeom; 21252f84e9bcSToby Isaac 21262f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 21272f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 21282f84e9bcSToby Isaac } 2129efc10488SMatthew G. Knepley } else { 21302f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 21312f84e9bcSToby Isaac } 213224cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 213324cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 213424cdb843SMatthew G. Knepley PetscInt i; 213524cdb843SMatthew G. Knepley 213624cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 21379f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 213824cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 213924cdb843SMatthew G. Knepley if (X_t) { 214024cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 21419f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 214224cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 214324cdb843SMatthew G. Knepley } 214424cdb843SMatthew G. Knepley if (dmAux) { 2145f7ed7b21SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 21469f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 2147f7ed7b21SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 214824cdb843SMatthew G. Knepley } 214924cdb843SMatthew G. Knepley } 2150efc10488SMatthew G. Knepley if (hasJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 215155ad3c34SMatthew G. Knepley if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2152426ff135SMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 215324cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 21545244db28SMatthew G. Knepley PetscClassId id; 215524cdb843SMatthew G. Knepley PetscFE fe; 215624cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 215724cdb843SMatthew G. Knepley /* Conforming batches */ 215824cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 215924cdb843SMatthew G. Knepley /* Remainder */ 216024cdb843SMatthew G. Knepley PetscInt Nr, offset; 216124cdb843SMatthew G. Knepley 216224cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 21635244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 21645244db28SMatthew G. Knepley if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 216524cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 216624cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 216724cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 216824cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 216924cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 217024cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 217124cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 217224cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 217324cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 217424cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 217524cdb843SMatthew G. Knepley offset = numCells - Nr; 217624cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2177efc10488SMatthew G. Knepley if (hasJac) { 217811dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 217911dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2180efc10488SMatthew G. Knepley } 218155ad3c34SMatthew G. Knepley if (hasPrec) { 218211dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 218311dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 2184426ff135SMatthew G. Knepley } 2185426ff135SMatthew G. Knepley if (hasDyn) { 218611dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 218711dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 218855ad3c34SMatthew G. Knepley } 218924cdb843SMatthew G. Knepley } 219024cdb843SMatthew G. Knepley } 2191426ff135SMatthew G. Knepley if (hasDyn) { 2192426ff135SMatthew G. Knepley for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2193426ff135SMatthew G. Knepley } 21945244db28SMatthew G. Knepley if (hasFV) { 21955244db28SMatthew G. Knepley PetscClassId id; 21965244db28SMatthew G. Knepley PetscFV fv; 21975244db28SMatthew G. Knepley PetscInt offsetI, NcI, NbI = 1, fc, f; 21985244db28SMatthew G. Knepley 21995244db28SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 22005244db28SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 22015244db28SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 22025244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 22035244db28SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 22045244db28SMatthew G. Knepley /* Put in the identity */ 22055244db28SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 22065244db28SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 22075244db28SMatthew G. Knepley const PetscInt eOffset = (c-cStart)*totDim*totDim; 22085244db28SMatthew G. Knepley for (fc = 0; fc < NcI; ++fc) { 22095244db28SMatthew G. Knepley for (f = 0; f < NbI; ++f) { 22105244db28SMatthew G. Knepley const PetscInt i = offsetI + f*NcI+fc; 22115244db28SMatthew G. Knepley if (hasPrec) { 22125244db28SMatthew G. Knepley if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 22135244db28SMatthew G. Knepley elemMatP[eOffset+i*totDim+i] = 1.0; 22145244db28SMatthew G. Knepley } else {elemMat[eOffset+i*totDim+i] = 1.0;} 22155244db28SMatthew G. Knepley } 22165244db28SMatthew G. Knepley } 22175244db28SMatthew G. Knepley } 22185244db28SMatthew G. Knepley } 22195244db28SMatthew G. Knepley /* No allocated space for FV stuff, so ignore the zero entries */ 22205244db28SMatthew G. Knepley ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 22215244db28SMatthew G. Knepley } 222224cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 222355ad3c34SMatthew G. Knepley if (hasPrec) { 2224efc10488SMatthew G. Knepley if (hasJac) { 222555ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 222655ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2227efc10488SMatthew G. Knepley } 222855ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 222955ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 223055ad3c34SMatthew G. Knepley } else { 22319f11d433SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 22329f11d433SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 223324cdb843SMatthew G. Knepley } 223455ad3c34SMatthew G. Knepley } 22355244db28SMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 22362f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 22372f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 22385244db28SMatthew G. Knepley } else { 22392f84e9bcSToby Isaac cgeom = NULL; 22402f84e9bcSToby Isaac } 22412f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2242426ff135SMatthew G. Knepley ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2243f7ed7b21SMatthew G. Knepley if (dmAux) { 2244f7ed7b21SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2245f7ed7b21SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2246f7ed7b21SMatthew G. Knepley } 224724cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2248a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 224924cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2250a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 225124cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 225224cdb843SMatthew G. Knepley const char *bdLabel; 225324cdb843SMatthew G. Knepley DMLabel label; 225424cdb843SMatthew G. Knepley IS pointIS; 225524cdb843SMatthew G. Knepley const PetscInt *points; 225624cdb843SMatthew G. Knepley const PetscInt *values; 2257a8e83e26SSanderA PetscInt field, numValues, v, numPoints, p, dep, numFaces; 225824cdb843SMatthew G. Knepley PetscBool isEssential; 225980bc4632SMatthew G. Knepley PetscObject obj; 226080bc4632SMatthew G. Knepley PetscClassId id; 226124cdb843SMatthew G. Knepley 2262a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 226380bc4632SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 226480bc4632SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 226580bc4632SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || isEssential) continue; 2266c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 2267a8e83e26SSanderA for (v = 0; v < numValues; ++v) { 2268a8e83e26SSanderA ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr); 2269a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 227022734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 227124cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 227224cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 227324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 227424cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 227524cdb843SMatthew G. Knepley } 2276bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 227724cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 227824cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 227924cdb843SMatthew G. Knepley const PetscInt point = points[p]; 228024cdb843SMatthew G. Knepley PetscScalar *x = NULL; 228124cdb843SMatthew G. Knepley PetscInt i; 228224cdb843SMatthew G. Knepley 228324cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 228424cdb843SMatthew G. Knepley if (dep != dim-1) continue; 2285bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 2286302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 2287bbce034cSMatthew 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); 228824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 228924cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 229024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 229124cdb843SMatthew G. Knepley if (X_t) { 229224cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 229324cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 229424cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 229524cdb843SMatthew G. Knepley } 229624cdb843SMatthew G. Knepley ++f; 229724cdb843SMatthew G. Knepley } 229824cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 229924cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 230024cdb843SMatthew G. Knepley PetscFE fe; 230124cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 230224cdb843SMatthew G. Knepley /* Conforming batches */ 230324cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 230424cdb843SMatthew G. Knepley /* Remainder */ 230524cdb843SMatthew G. Knepley PetscInt Nr, offset; 230624cdb843SMatthew G. Knepley 230724cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 230824cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 230924cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 231024cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 231124cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 231224cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 231324cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 231424cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 231524cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 231624cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 231724cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 231824cdb843SMatthew G. Knepley offset = numFaces - Nr; 231924cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 232011dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, t, X_tShift, elemMat);CHKERRQ(ierr); 232111dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, t, X_tShift, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr); 232224cdb843SMatthew G. Knepley } 232324cdb843SMatthew G. Knepley } 232424cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 232524cdb843SMatthew G. Knepley const PetscInt point = points[p]; 232624cdb843SMatthew G. Knepley 232724cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 232824cdb843SMatthew G. Knepley if (dep != dim-1) continue; 232924cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 233024cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 233124cdb843SMatthew G. Knepley ++f; 233224cdb843SMatthew G. Knepley } 233324cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 233424cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2335bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 233624cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 233724cdb843SMatthew G. Knepley } 2338a8e83e26SSanderA } 2339efc10488SMatthew G. Knepley if (hasJac && hasPrec) { 234082ef7567SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 234182ef7567SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 234282ef7567SMatthew G. Knepley } 234324cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 234424cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 234524cdb843SMatthew G. Knepley if (mesh->printFEM) { 234624cdb843SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 234724cdb843SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 234824cdb843SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 234924cdb843SMatthew G. Knepley } 235024cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 235124cdb843SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 235224cdb843SMatthew G. Knepley if (isShell) { 235324cdb843SMatthew G. Knepley JacActionCtx *jctx; 235424cdb843SMatthew G. Knepley 235524cdb843SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 235624cdb843SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 235724cdb843SMatthew G. Knepley } 235824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 235924cdb843SMatthew G. Knepley } 236024cdb843SMatthew G. Knepley 2361a925c78cSMatthew G. Knepley 2362a925c78cSMatthew G. Knepley #undef __FUNCT__ 2363a925c78cSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianAction_Internal" 2364a925c78cSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianAction_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user) 2365a925c78cSMatthew G. Knepley { 2366a925c78cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2367a925c78cSMatthew G. Knepley const char *name = "Jacobian"; 2368a925c78cSMatthew G. Knepley DM dmAux, plex; 2369a925c78cSMatthew G. Knepley Vec A, cellgeom; 2370a925c78cSMatthew G. Knepley PetscDS prob, probAux = NULL; 2371a925c78cSMatthew G. Knepley PetscQuadrature quad; 2372a925c78cSMatthew G. Knepley PetscSection section, globalSection, sectionAux; 2373a925c78cSMatthew G. Knepley PetscFECellGeom *cgeom = NULL; 2374a925c78cSMatthew G. Knepley PetscScalar *cgeomScal; 2375a925c78cSMatthew G. Knepley PetscScalar *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z; 237675b37a90SMatthew G. Knepley PetscInt dim, Nf, fieldI, fieldJ, numCells, c; 237775b37a90SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux; 237875b37a90SMatthew G. Knepley PetscBool hasDyn; 2379a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2380a925c78cSMatthew G. Knepley 2381a925c78cSMatthew G. Knepley PetscFunctionBegin; 2382a925c78cSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2383a925c78cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2384a925c78cSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2385a925c78cSMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2386a925c78cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2387a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2388a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 2389a925c78cSMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2390a925c78cSMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2391a925c78cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2392a925c78cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2393a925c78cSMatthew G. Knepley numCells = cEnd - cStart; 2394a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2395a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2396a925c78cSMatthew G. Knepley if (dmAux) { 2397a925c78cSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2398a925c78cSMatthew G. Knepley ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2399a925c78cSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2400a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2401a925c78cSMatthew G. Knepley } 2402a925c78cSMatthew G. Knepley ierr = VecSet(Z, 0.0);CHKERRQ(ierr); 2403a925c78cSMatthew G. Knepley ierr = PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);CHKERRQ(ierr); 2404a925c78cSMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2405a925c78cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 2406a925c78cSMatthew G. Knepley ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2407a925c78cSMatthew G. Knepley if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2408a925c78cSMatthew G. Knepley DM dmCell; 2409a925c78cSMatthew G. Knepley 2410a925c78cSMatthew G. Knepley ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 2411a925c78cSMatthew G. Knepley ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 2412a925c78cSMatthew G. Knepley for (c = 0; c < cEnd - cStart; c++) { 2413a925c78cSMatthew G. Knepley PetscScalar *thisgeom; 2414a925c78cSMatthew G. Knepley 2415a925c78cSMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 2416a925c78cSMatthew G. Knepley cgeom[c] = *((PetscFECellGeom *) thisgeom); 2417a925c78cSMatthew G. Knepley } 2418a925c78cSMatthew G. Knepley } else { 2419a925c78cSMatthew G. Knepley cgeom = (PetscFECellGeom *) cgeomScal; 2420a925c78cSMatthew G. Knepley } 2421a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2422a925c78cSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 2423a925c78cSMatthew G. Knepley PetscInt i; 2424a925c78cSMatthew G. Knepley 2425a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2426a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 2427a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2428a925c78cSMatthew G. Knepley if (X_t) { 2429a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2430a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 2431a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2432a925c78cSMatthew G. Knepley } 2433a925c78cSMatthew G. Knepley if (dmAux) { 2434a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2435a925c78cSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 2436a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2437a925c78cSMatthew G. Knepley } 2438a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr); 2439a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) y[(c-cStart)*totDim+i] = x[i]; 2440a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr); 2441a925c78cSMatthew G. Knepley } 2442a925c78cSMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2443a925c78cSMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2444a925c78cSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 2445a925c78cSMatthew G. Knepley PetscFE fe; 2446a925c78cSMatthew G. Knepley PetscInt numQuadPoints, Nb; 2447a925c78cSMatthew G. Knepley /* Conforming batches */ 2448a925c78cSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2449a925c78cSMatthew G. Knepley /* Remainder */ 2450a925c78cSMatthew G. Knepley PetscInt Nr, offset; 2451a925c78cSMatthew G. Knepley 2452a925c78cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2453a925c78cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2454a925c78cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2455a925c78cSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2456a925c78cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 2457a925c78cSMatthew G. Knepley blockSize = Nb*numQuadPoints; 2458a925c78cSMatthew G. Knepley batchSize = numBlocks * blockSize; 2459a925c78cSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2460a925c78cSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 2461a925c78cSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 2462a925c78cSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 2463a925c78cSMatthew G. Knepley offset = numCells - Nr; 2464a925c78cSMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 24651bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 24661bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2467a925c78cSMatthew G. Knepley if (hasDyn) { 24681bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 24691bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 2470a925c78cSMatthew G. Knepley } 2471a925c78cSMatthew G. Knepley } 2472a925c78cSMatthew G. Knepley } 2473a925c78cSMatthew G. Knepley if (hasDyn) { 2474a925c78cSMatthew G. Knepley for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2475a925c78cSMatthew G. Knepley } 2476a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2477a925c78cSMatthew G. Knepley const PetscBLASInt M = totDim, one = 1; 2478a925c78cSMatthew G. Knepley const PetscScalar a = 1.0, b = 0.0; 2479a925c78cSMatthew G. Knepley 2480a925c78cSMatthew G. Knepley PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[(c-cStart)*totDim*totDim], &M, &y[(c-cStart)*totDim], &one, &b, z, &one)); 2481a925c78cSMatthew G. Knepley if (mesh->printFEM > 1) { 2482a925c78cSMatthew G. Knepley ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr); 2483a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Y", totDim, &y[(c-cStart)*totDim]);CHKERRQ(ierr); 2484a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Z", totDim, z);CHKERRQ(ierr); 2485a925c78cSMatthew G. Knepley } 2486a925c78cSMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, Z, c, z, ADD_VALUES);CHKERRQ(ierr); 2487a925c78cSMatthew G. Knepley } 2488a925c78cSMatthew G. Knepley if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {ierr = PetscFree(cgeom);CHKERRQ(ierr);} 2489a925c78cSMatthew G. Knepley else {cgeom = NULL;} 2490a925c78cSMatthew G. Knepley ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2491a925c78cSMatthew G. Knepley ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr); 2492a925c78cSMatthew G. Knepley if (dmAux) { 2493a925c78cSMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2494a925c78cSMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2495a925c78cSMatthew G. Knepley } 2496a925c78cSMatthew G. Knepley if (mesh->printFEM) { 2497a925c78cSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr); 2498a925c78cSMatthew G. Knepley ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2499a925c78cSMatthew G. Knepley } 2500a925c78cSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2501a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2502a925c78cSMatthew G. Knepley } 2503a925c78cSMatthew G. Knepley 250424cdb843SMatthew G. Knepley #undef __FUNCT__ 250524cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 250624cdb843SMatthew G. Knepley /*@ 250724cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 250824cdb843SMatthew G. Knepley 250924cdb843SMatthew G. Knepley Input Parameters: 251024cdb843SMatthew G. Knepley + dm - The mesh 251124cdb843SMatthew G. Knepley . X - Local input vector 251224cdb843SMatthew G. Knepley - user - The user context 251324cdb843SMatthew G. Knepley 251424cdb843SMatthew G. Knepley Output Parameter: 251524cdb843SMatthew G. Knepley . Jac - Jacobian matrix 251624cdb843SMatthew G. Knepley 251724cdb843SMatthew G. Knepley Note: 251824cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 251924cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 252024cdb843SMatthew G. Knepley 252124cdb843SMatthew G. Knepley Level: developer 252224cdb843SMatthew G. Knepley 252324cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 252424cdb843SMatthew G. Knepley @*/ 252524cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 252624cdb843SMatthew G. Knepley { 2527b953af5fSMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 25286da023fcSToby Isaac DM plex; 252924cdb843SMatthew G. Knepley PetscErrorCode ierr; 253024cdb843SMatthew G. Knepley 253124cdb843SMatthew G. Knepley PetscFunctionBegin; 25326da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 25336da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 25346da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2535b953af5fSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 25366da023fcSToby Isaac ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 25379a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 253824cdb843SMatthew G. Knepley PetscFunctionReturn(0); 253924cdb843SMatthew G. Knepley } 25401878804aSMatthew G. Knepley 25411878804aSMatthew G. Knepley #undef __FUNCT__ 2542a925c78cSMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianActionFEM" 2543a925c78cSMatthew G. Knepley /*@ 2544a925c78cSMatthew G. Knepley DMPlexSNESComputeJacobianActionFEM - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user. 2545a925c78cSMatthew G. Knepley 2546a925c78cSMatthew G. Knepley Input Parameters: 2547a925c78cSMatthew G. Knepley + dm - The mesh 2548a925c78cSMatthew G. Knepley . X - Local solution vector 2549a925c78cSMatthew G. Knepley . Y - Local input vector 2550a925c78cSMatthew G. Knepley - user - The user context 2551a925c78cSMatthew G. Knepley 2552a925c78cSMatthew G. Knepley Output Parameter: 2553a925c78cSMatthew G. Knepley . Z - Local output vector 2554a925c78cSMatthew G. Knepley 2555a925c78cSMatthew G. Knepley Note: 2556a925c78cSMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 2557a925c78cSMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 2558a925c78cSMatthew G. Knepley 2559a925c78cSMatthew G. Knepley Level: developer 2560a925c78cSMatthew G. Knepley 2561a925c78cSMatthew G. Knepley .seealso: FormFunctionLocal() 2562a925c78cSMatthew G. Knepley @*/ 2563a925c78cSMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianActionFEM(DM dm, Vec X, Vec Y, Vec Z, void *user) 2564a925c78cSMatthew G. Knepley { 2565a925c78cSMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 2566a925c78cSMatthew G. Knepley DM plex; 2567a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2568a925c78cSMatthew G. Knepley 2569a925c78cSMatthew G. Knepley PetscFunctionBegin; 2570a925c78cSMatthew G. Knepley ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2571a925c78cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 2572a925c78cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2573a925c78cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2574a925c78cSMatthew G. Knepley ierr = DMPlexComputeJacobianAction_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Y, Z, user);CHKERRQ(ierr); 2575a925c78cSMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2576a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2577a925c78cSMatthew G. Knepley } 2578a925c78cSMatthew G. Knepley 2579a925c78cSMatthew G. Knepley #undef __FUNCT__ 25809f520fc2SToby Isaac #define __FUNCT__ "DMPlexSetSNESLocalFEM" 25819f520fc2SToby Isaac /*@ 25829f520fc2SToby Isaac DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 25839f520fc2SToby Isaac 25849f520fc2SToby Isaac Input Parameters: 25859f520fc2SToby Isaac + dm - The DM object 25869f520fc2SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see DMAddBoundary()) 25879f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 25889f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 25891a244344SSatish Balay 25901a244344SSatish Balay Level: developer 25919f520fc2SToby Isaac @*/ 25929f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 25939f520fc2SToby Isaac { 25949f520fc2SToby Isaac PetscErrorCode ierr; 25959f520fc2SToby Isaac 25969f520fc2SToby Isaac PetscFunctionBegin; 25979f520fc2SToby Isaac ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 25989f520fc2SToby Isaac ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 25999f520fc2SToby Isaac ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 26009f520fc2SToby Isaac PetscFunctionReturn(0); 26019f520fc2SToby Isaac } 26029f520fc2SToby Isaac 26039f520fc2SToby Isaac #undef __FUNCT__ 26041878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions_Internal" 26050163fd50SMatthew 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) 26061878804aSMatthew G. Knepley { 26071878804aSMatthew G. Knepley Mat J, M; 26081878804aSMatthew G. Knepley Vec r, b; 26091878804aSMatthew G. Knepley MatNullSpace nullSpace; 26101878804aSMatthew G. Knepley PetscReal *error, res = 0.0; 26111878804aSMatthew G. Knepley PetscInt numFields; 26121878804aSMatthew G. Knepley PetscErrorCode ierr; 26131878804aSMatthew G. Knepley 26141878804aSMatthew G. Knepley PetscFunctionBegin; 26151878804aSMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 26161878804aSMatthew G. Knepley ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 26171878804aSMatthew G. Knepley M = J; 26181878804aSMatthew G. Knepley /* TODO Null space for J */ 26191878804aSMatthew G. Knepley /* Check discretization error */ 26201878804aSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 26211878804aSMatthew G. Knepley ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 26221878804aSMatthew G. Knepley if (numFields > 1) { 26231878804aSMatthew G. Knepley PetscInt f; 26241878804aSMatthew G. Knepley 26251189c1efSToby Isaac ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr); 26261878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 26271878804aSMatthew G. Knepley for (f = 0; f < numFields; ++f) { 26281878804aSMatthew G. Knepley if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 26291878804aSMatthew G. Knepley if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);} 26301878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 26311878804aSMatthew G. Knepley } 26321878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 26331878804aSMatthew G. Knepley } else { 26340709b2feSToby Isaac ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr); 26351878804aSMatthew G. Knepley if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);} 26361878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 26371878804aSMatthew G. Knepley } 26381878804aSMatthew G. Knepley ierr = PetscFree(error);CHKERRQ(ierr); 26391878804aSMatthew G. Knepley /* Check residual */ 26401878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 26411878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 26421878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr); 26431878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 26441878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2645685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2646685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 26471878804aSMatthew G. Knepley /* Check Jacobian */ 26481878804aSMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr); 26491878804aSMatthew G. Knepley ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 26501878804aSMatthew G. Knepley if (nullSpace) { 26511878804aSMatthew G. Knepley PetscBool isNull; 26521878804aSMatthew G. Knepley ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 26531878804aSMatthew G. Knepley if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 26541878804aSMatthew G. Knepley } 26551878804aSMatthew G. Knepley ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 26561878804aSMatthew G. Knepley ierr = VecSet(r, 0.0);CHKERRQ(ierr); 26571878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 26581878804aSMatthew G. Knepley ierr = MatMult(M, u, r);CHKERRQ(ierr); 26591878804aSMatthew G. Knepley ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 26601878804aSMatthew G. Knepley ierr = VecDestroy(&b);CHKERRQ(ierr); 26611878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 26621878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr); 26631878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 26641878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2665685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2666685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 26671878804aSMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 26681878804aSMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 26691878804aSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 26701878804aSMatthew G. Knepley PetscFunctionReturn(0); 26711878804aSMatthew G. Knepley } 26721878804aSMatthew G. Knepley 26731878804aSMatthew G. Knepley #undef __FUNCT__ 26741878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions" 26750163fd50SMatthew 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) 26761878804aSMatthew G. Knepley { 26771878804aSMatthew G. Knepley DM dm; 26781878804aSMatthew G. Knepley Vec sol; 26791878804aSMatthew G. Knepley PetscBool check; 26801878804aSMatthew G. Knepley PetscErrorCode ierr; 26811878804aSMatthew G. Knepley 26821878804aSMatthew G. Knepley PetscFunctionBegin; 2683c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 26841878804aSMatthew G. Knepley if (!check) PetscFunctionReturn(0); 26851878804aSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 26861878804aSMatthew G. Knepley ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 26871878804aSMatthew G. Knepley ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 26881878804aSMatthew G. Knepley ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr); 26891878804aSMatthew G. Knepley ierr = VecDestroy(&sol);CHKERRQ(ierr); 2690552f7358SJed Brown PetscFunctionReturn(0); 2691552f7358SJed Brown } 2692