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); 53*95dccacaSBarry Smith ierr = PetscNew(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; 18262a38674SMatthew G. Knepley ierr = DMLocatePoints(dm, pointVec, DM_POINTLOCATION_NONE, &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; 424552f7358SJed Brown SNES snes; 425552f7358SJed Brown KSP ksp; 426552f7358SJed Brown PC pc; 427552f7358SJed Brown Vec coordsLocal, r, ref, real; 428552f7358SJed Brown Mat J; 42956044e6dSMatthew G. Knepley const PetscScalar *coords; 43056044e6dSMatthew G. Knepley PetscScalar *a; 431552f7358SJed Brown PetscInt p; 432552f7358SJed Brown PetscErrorCode ierr; 433552f7358SJed Brown 434552f7358SJed Brown PetscFunctionBegin; 435552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 436fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 437552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 438552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 439552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 440552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 441c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 442552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 443552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 444552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 445552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 446552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 447552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 4480298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 4490298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 450552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 451552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 452552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 453552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 454552f7358SJed Brown 45556044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 456552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 457552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 458a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 459552f7358SJed Brown PetscScalar *xi; 460cb313848SJed Brown PetscReal xir[2]; 461552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 462552f7358SJed Brown 463552f7358SJed Brown /* Can make this do all points at once */ 4640298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4650adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 4660298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 4670adebc6cSBarry Smith if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof); 4680298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 4690298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 470552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 471552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 472552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 473552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 474552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 475552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 476cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 477cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 4781aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1]; 4791aa26658SKarl Rupp 480552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 4810298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 4820298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 483552f7358SJed Brown } 484552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 48556044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 486552f7358SJed Brown 487552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 488552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 489552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 490552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 491552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 492552f7358SJed Brown PetscFunctionReturn(0); 493552f7358SJed Brown } 494552f7358SJed Brown 495552f7358SJed Brown #undef __FUNCT__ 496552f7358SJed Brown #define __FUNCT__ "HexMap_Private" 4975820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 498552f7358SJed Brown { 499552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 500552f7358SJed Brown const PetscScalar x0 = vertices[0]; 501552f7358SJed Brown const PetscScalar y0 = vertices[1]; 502552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5037a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5047a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5057a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 506552f7358SJed Brown const PetscScalar x2 = vertices[6]; 507552f7358SJed Brown const PetscScalar y2 = vertices[7]; 508552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5097a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5107a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5117a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 512552f7358SJed Brown const PetscScalar x4 = vertices[12]; 513552f7358SJed Brown const PetscScalar y4 = vertices[13]; 514552f7358SJed Brown const PetscScalar z4 = vertices[14]; 515552f7358SJed Brown const PetscScalar x5 = vertices[15]; 516552f7358SJed Brown const PetscScalar y5 = vertices[16]; 517552f7358SJed Brown const PetscScalar z5 = vertices[17]; 518552f7358SJed Brown const PetscScalar x6 = vertices[18]; 519552f7358SJed Brown const PetscScalar y6 = vertices[19]; 520552f7358SJed Brown const PetscScalar z6 = vertices[20]; 521552f7358SJed Brown const PetscScalar x7 = vertices[21]; 522552f7358SJed Brown const PetscScalar y7 = vertices[22]; 523552f7358SJed Brown const PetscScalar z7 = vertices[23]; 524552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 525552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 526552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 527552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 528552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 529552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 530552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 531552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 532552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 533552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 534552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 535552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 536552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 537552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 538552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 539552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 540552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 541552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 542552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 543552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 544552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 54556044e6dSMatthew G. Knepley const PetscScalar *ref; 54656044e6dSMatthew G. Knepley PetscScalar *real; 547552f7358SJed Brown PetscErrorCode ierr; 548552f7358SJed Brown 549552f7358SJed Brown PetscFunctionBegin; 55056044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 551552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 552552f7358SJed Brown { 553552f7358SJed Brown const PetscScalar p0 = ref[0]; 554552f7358SJed Brown const PetscScalar p1 = ref[1]; 555552f7358SJed Brown const PetscScalar p2 = ref[2]; 556552f7358SJed Brown 557552f7358SJed 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; 558552f7358SJed 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; 559552f7358SJed 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; 560552f7358SJed Brown } 561552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 56256044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 563552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 564552f7358SJed Brown PetscFunctionReturn(0); 565552f7358SJed Brown } 566552f7358SJed Brown 567552f7358SJed Brown #undef __FUNCT__ 568552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private" 569d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 570552f7358SJed Brown { 571552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 572552f7358SJed Brown const PetscScalar x0 = vertices[0]; 573552f7358SJed Brown const PetscScalar y0 = vertices[1]; 574552f7358SJed Brown const PetscScalar z0 = vertices[2]; 5757a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 5767a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 5777a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 578552f7358SJed Brown const PetscScalar x2 = vertices[6]; 579552f7358SJed Brown const PetscScalar y2 = vertices[7]; 580552f7358SJed Brown const PetscScalar z2 = vertices[8]; 5817a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 5827a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 5837a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 584552f7358SJed Brown const PetscScalar x4 = vertices[12]; 585552f7358SJed Brown const PetscScalar y4 = vertices[13]; 586552f7358SJed Brown const PetscScalar z4 = vertices[14]; 587552f7358SJed Brown const PetscScalar x5 = vertices[15]; 588552f7358SJed Brown const PetscScalar y5 = vertices[16]; 589552f7358SJed Brown const PetscScalar z5 = vertices[17]; 590552f7358SJed Brown const PetscScalar x6 = vertices[18]; 591552f7358SJed Brown const PetscScalar y6 = vertices[19]; 592552f7358SJed Brown const PetscScalar z6 = vertices[20]; 593552f7358SJed Brown const PetscScalar x7 = vertices[21]; 594552f7358SJed Brown const PetscScalar y7 = vertices[22]; 595552f7358SJed Brown const PetscScalar z7 = vertices[23]; 596552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 597552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 598552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 599552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 600552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 601552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 602552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 603552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 604552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 605552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 606552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 607552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 60856044e6dSMatthew G. Knepley const PetscScalar *ref; 609552f7358SJed Brown PetscErrorCode ierr; 610552f7358SJed Brown 611552f7358SJed Brown PetscFunctionBegin; 61256044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 613552f7358SJed Brown { 614552f7358SJed Brown const PetscScalar x = ref[0]; 615552f7358SJed Brown const PetscScalar y = ref[1]; 616552f7358SJed Brown const PetscScalar z = ref[2]; 617552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 618da80777bSKarl Rupp PetscScalar values[9]; 619da80777bSKarl Rupp 620da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 621da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 622da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 623da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 624da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 625da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 626da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 627da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 628da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 6291aa26658SKarl Rupp 63094ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 631552f7358SJed Brown } 632552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 63356044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 63494ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 63594ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 636552f7358SJed Brown PetscFunctionReturn(0); 637552f7358SJed Brown } 638552f7358SJed Brown 639552f7358SJed Brown #undef __FUNCT__ 640552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private" 641a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 642a6dfd86eSKarl Rupp { 643fafc0619SMatthew G Knepley DM dmCoord; 644552f7358SJed Brown SNES snes; 645552f7358SJed Brown KSP ksp; 646552f7358SJed Brown PC pc; 647552f7358SJed Brown Vec coordsLocal, r, ref, real; 648552f7358SJed Brown Mat J; 64956044e6dSMatthew G. Knepley const PetscScalar *coords; 65056044e6dSMatthew G. Knepley PetscScalar *a; 651552f7358SJed Brown PetscInt p; 652552f7358SJed Brown PetscErrorCode ierr; 653552f7358SJed Brown 654552f7358SJed Brown PetscFunctionBegin; 655552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 656fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 657552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 658552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 659552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 660552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 661c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 662552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 663552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 664552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 665552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 666552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 667552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 6680298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 6690298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 670552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 671552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 672552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 673552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 674552f7358SJed Brown 67556044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 676552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 677552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 678a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 679552f7358SJed Brown PetscScalar *xi; 680cb313848SJed Brown PetscReal xir[3]; 681552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 682552f7358SJed Brown 683552f7358SJed Brown /* Can make this do all points at once */ 6840298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6850adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 6860298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 6870adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 6880298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 6890298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 690552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 691552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 692552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 693552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 694552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 695552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 696552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 697cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 698cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 699cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 700552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 701552f7358SJed Brown a[p*ctx->dof+comp] = 702cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 7037a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 704cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 7057a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 706cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 707cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 708cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 709cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 710552f7358SJed Brown } 711552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 7120298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 7130298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 714552f7358SJed Brown } 715552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 71656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 717552f7358SJed Brown 718552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 719552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 720552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 721552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 722552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 723552f7358SJed Brown PetscFunctionReturn(0); 724552f7358SJed Brown } 725552f7358SJed Brown 726552f7358SJed Brown #undef __FUNCT__ 727552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate" 728552f7358SJed Brown /* 729552f7358SJed Brown Input Parameters: 730552f7358SJed Brown + ctx - The DMInterpolationInfo context 731552f7358SJed Brown . dm - The DM 732552f7358SJed Brown - x - The local vector containing the field to be interpolated 733552f7358SJed Brown 734552f7358SJed Brown Output Parameters: 735552f7358SJed Brown . v - The vector containing the interpolated values 736552f7358SJed Brown */ 7370adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 7380adebc6cSBarry Smith { 739552f7358SJed Brown PetscInt dim, coneSize, n; 740552f7358SJed Brown PetscErrorCode ierr; 741552f7358SJed Brown 742552f7358SJed Brown PetscFunctionBegin; 743552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 744552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 745552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 746552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 7470adebc6cSBarry 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); 748552f7358SJed Brown if (n) { 749c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 750552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 751552f7358SJed Brown if (dim == 2) { 752552f7358SJed Brown if (coneSize == 3) { 7537a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 754552f7358SJed Brown } else if (coneSize == 4) { 755552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 7560adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 757552f7358SJed Brown } else if (dim == 3) { 758552f7358SJed Brown if (coneSize == 4) { 7597a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 760552f7358SJed Brown } else { 761552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 762552f7358SJed Brown } 7630adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 764552f7358SJed Brown } 765552f7358SJed Brown PetscFunctionReturn(0); 766552f7358SJed Brown } 767552f7358SJed Brown 768552f7358SJed Brown #undef __FUNCT__ 769552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy" 7700adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 7710adebc6cSBarry Smith { 772552f7358SJed Brown PetscErrorCode ierr; 773552f7358SJed Brown 774552f7358SJed Brown PetscFunctionBegin; 775552f7358SJed Brown PetscValidPointer(ctx, 2); 776552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 777552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 778552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 779552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 7800298fd71SBarry Smith *ctx = NULL; 781552f7358SJed Brown PetscFunctionReturn(0); 782552f7358SJed Brown } 783cc0c4584SMatthew G. Knepley 784cc0c4584SMatthew G. Knepley #undef __FUNCT__ 785cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields" 786cc0c4584SMatthew G. Knepley /*@C 787cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 788cc0c4584SMatthew G. Knepley 789cc0c4584SMatthew G. Knepley Collective on SNES 790cc0c4584SMatthew G. Knepley 791cc0c4584SMatthew G. Knepley Input Parameters: 792cc0c4584SMatthew G. Knepley + snes - the SNES context 793cc0c4584SMatthew G. Knepley . its - iteration number 794cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 795d43b4f6eSBarry Smith - vf - PetscViewerAndFormat of type ASCII 796cc0c4584SMatthew G. Knepley 797cc0c4584SMatthew G. Knepley Notes: 798cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 799cc0c4584SMatthew G. Knepley 800cc0c4584SMatthew G. Knepley Level: intermediate 801cc0c4584SMatthew G. Knepley 802cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 803cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 804cc0c4584SMatthew G. Knepley @*/ 805d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf) 806cc0c4584SMatthew G. Knepley { 807d43b4f6eSBarry Smith PetscViewer viewer = vf->viewer; 808cc0c4584SMatthew G. Knepley Vec res; 809cc0c4584SMatthew G. Knepley DM dm; 810cc0c4584SMatthew G. Knepley PetscSection s; 811cc0c4584SMatthew G. Knepley const PetscScalar *r; 812cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 813cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 814cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 815cc0c4584SMatthew G. Knepley 816cc0c4584SMatthew G. Knepley PetscFunctionBegin; 8174d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 818cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 819cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 820cc0c4584SMatthew G. Knepley ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr); 821cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 822cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 823cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 824cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 825cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 826cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 827cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 828cc0c4584SMatthew G. Knepley 829cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 830cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 831cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 832cc0c4584SMatthew G. Knepley } 833cc0c4584SMatthew G. Knepley } 834cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 835b2566f29SBarry Smith ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 836d43b4f6eSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 837cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 838cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 839cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 840cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 841cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 842cc0c4584SMatthew G. Knepley } 843cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 844cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 845d43b4f6eSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 846cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 847cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 848cc0c4584SMatthew G. Knepley } 84924cdb843SMatthew G. Knepley 85024cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 85124cdb843SMatthew G. Knepley 85224cdb843SMatthew G. Knepley #undef __FUNCT__ 8537d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM" 8547d4028c8SMatthew G. Knepley /*@ 8557d4028c8SMatthew G. Knepley DMPlexSNESGetGeometryFEM - Return precomputed geometric data 8567d4028c8SMatthew G. Knepley 8577d4028c8SMatthew G. Knepley Input Parameter: 8587d4028c8SMatthew G. Knepley . dm - The DM 8597d4028c8SMatthew G. Knepley 8607d4028c8SMatthew G. Knepley Output Parameters: 8617d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 8627d4028c8SMatthew G. Knepley 8637d4028c8SMatthew G. Knepley Level: developer 8647d4028c8SMatthew G. Knepley 8657d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal() 8667d4028c8SMatthew G. Knepley @*/ 8677d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom) 8687d4028c8SMatthew G. Knepley { 8697d4028c8SMatthew G. Knepley DMSNES dmsnes; 8707d4028c8SMatthew G. Knepley PetscObject obj; 8717d4028c8SMatthew G. Knepley PetscErrorCode ierr; 8727d4028c8SMatthew G. Knepley 8737d4028c8SMatthew G. Knepley PetscFunctionBegin; 8747d4028c8SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8757d4028c8SMatthew G. Knepley ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr); 8767d4028c8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr); 8777d4028c8SMatthew G. Knepley if (!obj) { 8787d4028c8SMatthew G. Knepley Vec cellgeom; 8797d4028c8SMatthew G. Knepley 8807d4028c8SMatthew G. Knepley ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 8817d4028c8SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr); 8827d4028c8SMatthew G. Knepley ierr = VecDestroy(&cellgeom);CHKERRQ(ierr); 8837d4028c8SMatthew G. Knepley } 8847d4028c8SMatthew G. Knepley if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);} 8857d4028c8SMatthew G. Knepley PetscFunctionReturn(0); 8867d4028c8SMatthew G. Knepley } 8877d4028c8SMatthew G. Knepley 8887d4028c8SMatthew G. Knepley #undef __FUNCT__ 88908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM" 89008449791SMatthew G. Knepley /*@ 89108449791SMatthew G. Knepley DMPlexSNESGetGeometryFVM - Return precomputed geometric data 89208449791SMatthew G. Knepley 89308449791SMatthew G. Knepley Input Parameter: 89408449791SMatthew G. Knepley . dm - The DM 89508449791SMatthew G. Knepley 89608449791SMatthew G. Knepley Output Parameters: 89708449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry 89808449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 89908449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 90008449791SMatthew G. Knepley 90108449791SMatthew G. Knepley Level: developer 90208449791SMatthew G. Knepley 90308449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 90408449791SMatthew G. Knepley @*/ 90508449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 90624cdb843SMatthew G. Knepley { 9074b32e5bbSToby Isaac DM plex; 90824cdb843SMatthew G. Knepley PetscErrorCode ierr; 90924cdb843SMatthew G. Knepley 91024cdb843SMatthew G. Knepley PetscFunctionBegin; 91108449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 9124b32e5bbSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 9134b32e5bbSToby Isaac ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr); 9144b32e5bbSToby Isaac if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);} 9154b32e5bbSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 91624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 91724cdb843SMatthew G. Knepley } 91824cdb843SMatthew G. Knepley 91924cdb843SMatthew G. Knepley #undef __FUNCT__ 92008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM" 921dbd489d2SMatthew G. Knepley /*@ 92208449791SMatthew G. Knepley DMPlexSNESGetGradientDM - Return gradient data layout 92308449791SMatthew G. Knepley 92408449791SMatthew G. Knepley Input Parameters: 92508449791SMatthew G. Knepley + dm - The DM 92608449791SMatthew G. Knepley - fv - The PetscFV 92708449791SMatthew G. Knepley 92808449791SMatthew G. Knepley Output Parameter: 92908449791SMatthew G. Knepley . dmGrad - The layout for gradient values 93008449791SMatthew G. Knepley 93108449791SMatthew G. Knepley Level: developer 93208449791SMatthew G. Knepley 93308449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 93408449791SMatthew G. Knepley @*/ 93508449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 93624cdb843SMatthew G. Knepley { 9374b32e5bbSToby Isaac DM plex; 93808449791SMatthew G. Knepley PetscBool computeGradients; 93924cdb843SMatthew G. Knepley PetscErrorCode ierr; 94024cdb843SMatthew G. Knepley 94124cdb843SMatthew G. Knepley PetscFunctionBegin; 94208449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 94308449791SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 94408449791SMatthew G. Knepley PetscValidPointer(dmGrad,3); 94508449791SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 94608449791SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 9474b32e5bbSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 9484b32e5bbSToby Isaac ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr); 9494b32e5bbSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 95008449791SMatthew G. Knepley PetscFunctionReturn(0); 95108449791SMatthew G. Knepley } 95208449791SMatthew G. Knepley 95308449791SMatthew G. Knepley #undef __FUNCT__ 95408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields" 95508449791SMatthew G. Knepley /*@C 95608449791SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 95708449791SMatthew G. Knepley 95808449791SMatthew G. Knepley Input Parameters: 95908449791SMatthew G. Knepley + dm - The DM 96008449791SMatthew G. Knepley . cStart - The first cell to include 96108449791SMatthew G. Knepley . cEnd - The first cell to exclude 96208449791SMatthew G. Knepley . locX - A local vector with the solution fields 96308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 96408449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 96508449791SMatthew G. Knepley 96608449791SMatthew G. Knepley Output Parameters: 96708449791SMatthew G. Knepley + u - The field coefficients 96808449791SMatthew G. Knepley . u_t - The fields derivative coefficients 96908449791SMatthew G. Knepley - a - The auxiliary field coefficients 97008449791SMatthew G. Knepley 97108449791SMatthew G. Knepley Level: developer 97208449791SMatthew G. Knepley 97308449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 97408449791SMatthew G. Knepley @*/ 97508449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 97608449791SMatthew G. Knepley { 97708449791SMatthew G. Knepley DM dmAux; 97808449791SMatthew G. Knepley PetscSection section, sectionAux; 97908449791SMatthew G. Knepley PetscDS prob; 98008449791SMatthew G. Knepley PetscInt numCells = cEnd - cStart, totDim, totDimAux, c; 98108449791SMatthew G. Knepley PetscErrorCode ierr; 98208449791SMatthew G. Knepley 98308449791SMatthew G. Knepley PetscFunctionBegin; 98408449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 98508449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 98608449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 98708449791SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 98808449791SMatthew G. Knepley PetscValidPointer(u, 7); 98908449791SMatthew G. Knepley PetscValidPointer(u_t, 8); 99008449791SMatthew G. Knepley PetscValidPointer(a, 9); 99124cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 99224cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 99324cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 99408449791SMatthew G. Knepley if (locA) { 99508449791SMatthew G. Knepley PetscDS probAux; 99608449791SMatthew G. Knepley 99708449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 99824cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 99924cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 100024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 100124cdb843SMatthew G. Knepley } 100208449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr); 100349073227SMatthew G. Knepley if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 100449073227SMatthew G. Knepley if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 100524cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 100608449791SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 100724cdb843SMatthew G. Knepley PetscInt i; 100824cdb843SMatthew G. Knepley 100908449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 10109f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i]; 101108449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 101208449791SMatthew G. Knepley if (locX_t) { 101308449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 10149f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i]; 101508449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr); 101624cdb843SMatthew G. Knepley } 101708449791SMatthew G. Knepley if (locA) { 10186da023fcSToby Isaac DM dmAuxPlex; 10196da023fcSToby Isaac 10206da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 10216da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10229f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i]; 10236da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 10246da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 102524cdb843SMatthew G. Knepley } 102624cdb843SMatthew G. Knepley } 102708449791SMatthew G. Knepley PetscFunctionReturn(0); 102808449791SMatthew G. Knepley } 102924cdb843SMatthew G. Knepley 103008449791SMatthew G. Knepley #undef __FUNCT__ 103108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields" 103208449791SMatthew G. Knepley /*@C 103308449791SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 103408449791SMatthew G. Knepley 103508449791SMatthew G. Knepley Input Parameters: 103608449791SMatthew G. Knepley + dm - The DM 103708449791SMatthew G. Knepley . cStart - The first cell to include 103808449791SMatthew G. Knepley . cEnd - The first cell to exclude 103908449791SMatthew G. Knepley . locX - A local vector with the solution fields 104008449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 104108449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 104208449791SMatthew G. Knepley 104308449791SMatthew G. Knepley Output Parameters: 104408449791SMatthew G. Knepley + u - The field coefficients 104508449791SMatthew G. Knepley . u_t - The fields derivative coefficients 104608449791SMatthew G. Knepley - a - The auxiliary field coefficients 104708449791SMatthew G. Knepley 104808449791SMatthew G. Knepley Level: developer 104908449791SMatthew G. Knepley 105008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 105108449791SMatthew G. Knepley @*/ 105208449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 105308449791SMatthew G. Knepley { 105408449791SMatthew G. Knepley PetscErrorCode ierr; 105508449791SMatthew G. Knepley 105608449791SMatthew G. Knepley PetscFunctionBegin; 105708449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr); 105849073227SMatthew G. Knepley if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);} 105949073227SMatthew G. Knepley if (*a) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);} 106008449791SMatthew G. Knepley PetscFunctionReturn(0); 106124cdb843SMatthew G. Knepley } 106208449791SMatthew G. Knepley 106308449791SMatthew G. Knepley #undef __FUNCT__ 106408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields" 106508449791SMatthew G. Knepley /*@C 106608449791SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 106708449791SMatthew G. Knepley 106808449791SMatthew G. Knepley Input Parameters: 106908449791SMatthew G. Knepley + dm - The DM 107008449791SMatthew G. Knepley . fStart - The first face to include 107108449791SMatthew G. Knepley . fEnd - The first face to exclude 107208449791SMatthew G. Knepley . locX - A local vector with the solution fields 107308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 107408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 107508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 107608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 107708449791SMatthew G. Knepley 107808449791SMatthew G. Knepley Output Parameters: 10795f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 10805f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face 1081477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 108208449791SMatthew G. Knepley 108308449791SMatthew G. Knepley Level: developer 108408449791SMatthew G. Knepley 108508449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 108608449791SMatthew G. Knepley @*/ 10875f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR) 108808449791SMatthew G. Knepley { 108908449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 1090195142f5SMatthew G. Knepley PetscSection section; 109108449791SMatthew G. Knepley PetscDS prob; 109208449791SMatthew G. Knepley DMLabel ghostLabel; 109308449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1094477f7dfdSMatthew G. Knepley PetscBool *isFE; 1095477f7dfdSMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 109608449791SMatthew G. Knepley PetscErrorCode ierr; 109708449791SMatthew G. Knepley 109808449791SMatthew G. Knepley PetscFunctionBegin; 109908449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 110008449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 110108449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 110208449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 110308449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 110408449791SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 110508449791SMatthew G. Knepley PetscValidPointer(uL, 9); 110608449791SMatthew G. Knepley PetscValidPointer(uR, 10); 110708449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 110808449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1109195142f5SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1110477f7dfdSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1111477f7dfdSMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1112477f7dfdSMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1113477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1114477f7dfdSMatthew G. Knepley PetscObject obj; 1115477f7dfdSMatthew G. Knepley PetscClassId id; 1116477f7dfdSMatthew G. Knepley 1117477f7dfdSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 1118477f7dfdSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1119477f7dfdSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1120477f7dfdSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1121477f7dfdSMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 1122477f7dfdSMatthew G. Knepley } 1123c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 112408449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 112508449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 112608449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 112708449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 112808449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 112908449791SMatthew G. Knepley if (locGrad) { 113008449791SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 113108449791SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 113224cdb843SMatthew G. Knepley } 1133477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr); 1134477f7dfdSMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr); 1135477f7dfdSMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 113608449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 113708449791SMatthew G. Knepley const PetscInt *cells; 1138640bce14SSatish Balay PetscFVFaceGeom *fg; 1139640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 1140640bce14SSatish Balay PetscScalar *xL, *xR, *gL, *gR; 114108449791SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 11423e64cd2fSToby Isaac PetscInt ghost, nsupp, nchild; 114308449791SMatthew G. Knepley 114408449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1145e697831aSToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 11463e64cd2fSToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 11477c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 114808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 114908449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 115008449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 115108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1152477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1153477f7dfdSMatthew G. Knepley PetscInt off; 1154477f7dfdSMatthew G. Knepley 115537a43ebbSMatthew G. Knepley ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1156477f7dfdSMatthew G. Knepley if (isFE[f]) { 1157477f7dfdSMatthew G. Knepley const PetscInt *cone; 11583e64cd2fSToby Isaac PetscInt comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d; 1159477f7dfdSMatthew G. Knepley 1160cca9989dSMatthew G. Knepley xL = xR = NULL; 11617c45b140SToby Isaac ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 1162cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1163cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1164477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 11653e64cd2fSToby Isaac ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr); 11663e64cd2fSToby Isaac for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break; 1167477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 11683e64cd2fSToby Isaac ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr); 11693e64cd2fSToby Isaac for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break; 11703e64cd2fSToby Isaac if (faceLocL == coneSizeL && faceLocR == coneSizeR) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d or cell %d", face, cells[0], cells[1]); 1171195142f5SMatthew G. Knepley /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 11723e64cd2fSToby Isaac /* TODO: this is a hack that might not be right for nonconforming */ 11733e64cd2fSToby Isaac if (faceLocL < coneSizeL) { 1174477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 11753e64cd2fSToby Isaac if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 11767c45b140SToby Isaac else {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];} 11773e64cd2fSToby Isaac } 11783e64cd2fSToby Isaac else { 11793e64cd2fSToby Isaac ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr); 11803e64cd2fSToby Isaac ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 11813e64cd2fSToby Isaac for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d]; 11823e64cd2fSToby Isaac } 1183cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1184cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1185477f7dfdSMatthew G. Knepley } else { 1186477f7dfdSMatthew G. Knepley PetscFV fv; 1187477f7dfdSMatthew G. Knepley PetscInt numComp, c; 1188477f7dfdSMatthew G. Knepley 1189477f7dfdSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1190477f7dfdSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1191cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1192cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 119308449791SMatthew G. Knepley if (dmGrad) { 119408449791SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 119508449791SMatthew G. Knepley 119608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 119708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 119808449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 119908449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1200477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1201477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1202477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 120308449791SMatthew G. Knepley } 120408449791SMatthew G. Knepley } else { 1205477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1206477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 1207477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 1208477f7dfdSMatthew G. Knepley } 1209477f7dfdSMatthew G. Knepley } 121008449791SMatthew G. Knepley } 121108449791SMatthew G. Knepley } 121208449791SMatthew G. Knepley ++iface; 121308449791SMatthew G. Knepley } 12145f942ad5SMatthew G. Knepley *Nface = iface; 121508449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 121608449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 121708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 121808449791SMatthew G. Knepley if (locGrad) { 121908449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 122008449791SMatthew G. Knepley } 1221477f7dfdSMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 122208449791SMatthew G. Knepley PetscFunctionReturn(0); 122308449791SMatthew G. Knepley } 122408449791SMatthew G. Knepley 122508449791SMatthew G. Knepley #undef __FUNCT__ 122608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields" 122708449791SMatthew G. Knepley /*@C 122808449791SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 122908449791SMatthew G. Knepley 123008449791SMatthew G. Knepley Input Parameters: 123108449791SMatthew G. Knepley + dm - The DM 123208449791SMatthew G. Knepley . fStart - The first face to include 123308449791SMatthew G. Knepley . fEnd - The first face to exclude 123408449791SMatthew G. Knepley . locX - A local vector with the solution fields 123508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 123608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 123708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 123808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 123908449791SMatthew G. Knepley 124008449791SMatthew G. Knepley Output Parameters: 12415f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 12425f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face 1243477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 124408449791SMatthew G. Knepley 124508449791SMatthew G. Knepley Level: developer 124608449791SMatthew G. Knepley 124708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 124808449791SMatthew G. Knepley @*/ 12495f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR) 125008449791SMatthew G. Knepley { 125108449791SMatthew G. Knepley PetscErrorCode ierr; 125208449791SMatthew G. Knepley 125308449791SMatthew G. Knepley PetscFunctionBegin; 125408449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr); 125508449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr); 125608449791SMatthew G. Knepley PetscFunctionReturn(0); 125708449791SMatthew G. Knepley } 125808449791SMatthew G. Knepley 125908449791SMatthew G. Knepley #undef __FUNCT__ 126008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry" 126108449791SMatthew G. Knepley /*@C 126208449791SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 126308449791SMatthew G. Knepley 126408449791SMatthew G. Knepley Input Parameters: 126508449791SMatthew G. Knepley + dm - The DM 126608449791SMatthew G. Knepley . fStart - The first face to include 126708449791SMatthew G. Knepley . fEnd - The first face to exclude 126808449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 126908449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 127008449791SMatthew G. Knepley 127108449791SMatthew G. Knepley Output Parameters: 12725f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 12735f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal 127408449791SMatthew G. Knepley - vol - The cell volume 127508449791SMatthew G. Knepley 127608449791SMatthew G. Knepley Level: developer 127708449791SMatthew G. Knepley 127808449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 127908449791SMatthew G. Knepley @*/ 12805f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 128108449791SMatthew G. Knepley { 128208449791SMatthew G. Knepley DM dmFace, dmCell; 128308449791SMatthew G. Knepley DMLabel ghostLabel; 128408449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 128508449791SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 128608449791SMatthew G. Knepley PetscErrorCode ierr; 128708449791SMatthew G. Knepley 128808449791SMatthew G. Knepley PetscFunctionBegin; 128908449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 129008449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 129108449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 129208449791SMatthew G. Knepley PetscValidPointer(fgeom, 6); 129308449791SMatthew G. Knepley PetscValidPointer(vol, 7); 129408449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1295c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 129608449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 129708449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 129808449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 129908449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 130008449791SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 130108449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr); 130208449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 130308449791SMatthew G. Knepley const PetscInt *cells; 1304640bce14SSatish Balay PetscFVFaceGeom *fg; 1305640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 130608449791SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 13072eefff9cSMatthew G. Knepley PetscReal *voll = *vol; 13087c45b140SToby Isaac PetscInt ghost, d, nchild, nsupp; 130908449791SMatthew G. Knepley 131008449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 13117c45b140SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 13127c45b140SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 13137c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 131408449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 131508449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 131608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 131708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 131808449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 131908449791SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 132008449791SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 132108449791SMatthew G. Knepley } 132208449791SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 132308449791SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 132408449791SMatthew G. Knepley ++iface; 132508449791SMatthew G. Knepley } 13265f942ad5SMatthew G. Knepley *Nface = iface; 132708449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 132808449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 132908449791SMatthew G. Knepley PetscFunctionReturn(0); 133008449791SMatthew G. Knepley } 133108449791SMatthew G. Knepley 133208449791SMatthew G. Knepley #undef __FUNCT__ 133308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry" 133408449791SMatthew G. Knepley /*@C 133508449791SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 133608449791SMatthew G. Knepley 133708449791SMatthew G. Knepley Input Parameters: 133808449791SMatthew G. Knepley + dm - The DM 133908449791SMatthew G. Knepley . fStart - The first face to include 134008449791SMatthew G. Knepley . fEnd - The first face to exclude 134108449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 134208449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 134308449791SMatthew G. Knepley 134408449791SMatthew G. Knepley Output Parameters: 13455f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 13465f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal 134708449791SMatthew G. Knepley - vol - The cell volume 134808449791SMatthew G. Knepley 134908449791SMatthew G. Knepley Level: developer 135008449791SMatthew G. Knepley 135108449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 135208449791SMatthew G. Knepley @*/ 13535f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 135408449791SMatthew G. Knepley { 135508449791SMatthew G. Knepley PetscErrorCode ierr; 135608449791SMatthew G. Knepley 135708449791SMatthew G. Knepley PetscFunctionBegin; 135808449791SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 135908449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr); 136008449791SMatthew G. Knepley PetscFunctionReturn(0); 136108449791SMatthew G. Knepley } 136208449791SMatthew G. Knepley 136308449791SMatthew G. Knepley #undef __FUNCT__ 136408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal" 136511dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 136608449791SMatthew G. Knepley { 136708449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 136808449791SMatthew G. Knepley PetscSection section; 136908449791SMatthew G. Knepley PetscDS prob; 137008449791SMatthew G. Knepley DMLabel depth; 137108449791SMatthew G. Knepley PetscFECellGeom *cgeom; 137208449791SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *elemVec = NULL; 137308449791SMatthew G. Knepley PetscInt dim, Nf, f, totDimBd, numBd, bd; 137408449791SMatthew G. Knepley PetscErrorCode ierr; 137508449791SMatthew G. Knepley 137608449791SMatthew G. Knepley PetscFunctionBegin; 137708449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 137808449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 137908449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 138008449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 138108449791SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 138224cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 1383a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 138424cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 138524cdb843SMatthew G. Knepley const char *bdLabel; 138624cdb843SMatthew G. Knepley DMLabel label; 138724cdb843SMatthew G. Knepley IS pointIS; 138824cdb843SMatthew G. Knepley const PetscInt *points; 138924cdb843SMatthew G. Knepley const PetscInt *values; 1390a8e83e26SSanderA PetscInt field, numValues, v, numPoints, p, dep, numFaces; 139124cdb843SMatthew G. Knepley PetscBool isEssential; 139280bc4632SMatthew G. Knepley PetscObject obj; 139380bc4632SMatthew G. Knepley PetscClassId id; 139424cdb843SMatthew G. Knepley 1395a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 139680bc4632SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 139780bc4632SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 139880bc4632SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || isEssential) continue; 1399c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1400a8e83e26SSanderA for (v = 0; v < numValues; ++v) { 1401a8e83e26SSanderA ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr); 1402a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 140322734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 140424cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 140524cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 140624cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 140724cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 140824cdb843SMatthew G. Knepley } 1409bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 141008449791SMatthew G. Knepley if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 141124cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 141224cdb843SMatthew G. Knepley const PetscInt point = points[p]; 141324cdb843SMatthew G. Knepley PetscScalar *x = NULL; 141424cdb843SMatthew G. Knepley PetscInt i; 141524cdb843SMatthew G. Knepley 141624cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 141724cdb843SMatthew G. Knepley if (dep != dim-1) continue; 1418bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 1419302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 1420bbce034cSMatthew 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); 1421929b1830SToby Isaac /* TODO: Matt, this is wrong if feBd does not match fe: i.e., if the order differs. */ 142208449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 142324cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 142408449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr); 142508449791SMatthew G. Knepley if (locX_t) { 142608449791SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 142724cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 142808449791SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr); 142924cdb843SMatthew G. Knepley } 143024cdb843SMatthew G. Knepley ++f; 143124cdb843SMatthew G. Knepley } 143224cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 143324cdb843SMatthew G. Knepley PetscFE fe; 143408449791SMatthew G. Knepley PetscQuadrature q; 143524cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 143624cdb843SMatthew G. Knepley /* Conforming batches */ 143724cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 143824cdb843SMatthew G. Knepley /* Remainder */ 143924cdb843SMatthew G. Knepley PetscInt Nr, offset; 144024cdb843SMatthew G. Knepley 144124cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 144224cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 144324cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 144424cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 144524cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 144624cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 144724cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 144824cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 144924cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 145024cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 145124cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 145224cdb843SMatthew G. Knepley offset = numFaces - Nr; 145311dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, t, elemVec);CHKERRQ(ierr); 145411dd639bSMatthew 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); 145524cdb843SMatthew G. Knepley } 145624cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 145724cdb843SMatthew G. Knepley const PetscInt point = points[p]; 145824cdb843SMatthew G. Knepley 145924cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 146024cdb843SMatthew G. Knepley if (dep != dim-1) continue; 146124cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 1462c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_ALL_VALUES);CHKERRQ(ierr); 146324cdb843SMatthew G. Knepley ++f; 146424cdb843SMatthew G. Knepley } 146524cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 146624cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1467bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr); 146808449791SMatthew G. Knepley if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 146924cdb843SMatthew G. Knepley } 1470a8e83e26SSanderA } 147108449791SMatthew G. Knepley PetscFunctionReturn(0); 147208449791SMatthew G. Knepley } 147308449791SMatthew G. Knepley 147408449791SMatthew G. Knepley #undef __FUNCT__ 147508449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal" 147611dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 147708449791SMatthew G. Knepley { 147808449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 147908449791SMatthew G. Knepley const char *name = "Residual"; 148008449791SMatthew G. Knepley DM dmAux = NULL; 148108449791SMatthew G. Knepley DM dmGrad = NULL; 148208449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 148308449791SMatthew G. Knepley PetscDS prob = NULL; 148408449791SMatthew G. Knepley PetscDS probAux = NULL; 148508449791SMatthew G. Knepley PetscSection section = NULL; 148608449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 148708449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 1488b2666ceaSMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 148908449791SMatthew G. Knepley PetscFV fvm = NULL; 149008449791SMatthew G. Knepley PetscFECellGeom *cgeomFEM = NULL; 14912f84e9bcSToby Isaac PetscScalar *cgeomScal; 149208449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 149308449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 149408449791SMatthew G. Knepley Vec locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 14953755293bSMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a, *uL, *uR; 1496c4d4a4f8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 149708449791SMatthew G. Knepley PetscErrorCode ierr; 149808449791SMatthew G. Knepley 149908449791SMatthew G. Knepley PetscFunctionBegin; 150008449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 150108449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1502195142f5SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 150308449791SMatthew G. Knepley /* FEM+FVM */ 150408449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 150508449791SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1506c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 150708449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 150808449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 150908449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 151008449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 151108449791SMatthew G. Knepley if (locA) { 151208449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 151308449791SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 151408449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 151508449791SMatthew G. Knepley } 151608449791SMatthew G. Knepley /* 2: Get geometric data */ 151708449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 151808449791SMatthew G. Knepley PetscObject obj; 151908449791SMatthew G. Knepley PetscClassId id; 15207173168dSMatthew G. Knepley PetscBool fimp; 152108449791SMatthew G. Knepley 15227173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 15237173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 152408449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 152508449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 152608449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 152708449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 152808449791SMatthew G. Knepley } 152908449791SMatthew G. Knepley if (useFEM) { 153008449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr); 15312f84e9bcSToby Isaac ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr); 15322f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 15332f84e9bcSToby Isaac DM dmCell; 15342f84e9bcSToby Isaac PetscInt c; 15352f84e9bcSToby Isaac 15362f84e9bcSToby Isaac ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr); 15372f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr); 15382f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 15392f84e9bcSToby Isaac PetscScalar *thisgeom; 15402f84e9bcSToby Isaac 15412f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 15422f84e9bcSToby Isaac cgeomFEM[c] = *((PetscFECellGeom *) thisgeom); 15432f84e9bcSToby Isaac } 15442f84e9bcSToby Isaac } 15452f84e9bcSToby Isaac else { 15462f84e9bcSToby Isaac cgeomFEM = (PetscFECellGeom *) cgeomScal; 15472f84e9bcSToby Isaac } 154808449791SMatthew G. Knepley } 154908449791SMatthew G. Knepley if (useFVM) { 155008449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 155108449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 155208449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 155308449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 155408449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 155508449791SMatthew G. Knepley if (dmGrad) { 155608449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 155708449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1558de555695SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 155908449791SMatthew G. Knepley /* Communicate gradient values */ 156008449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 156108449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 156208449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 156308449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 156408449791SMatthew G. Knepley } 1565bdd6f66aSToby Isaac /* Handle non-essential (e.g. outflow) boundary values */ 1566bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 156708449791SMatthew G. Knepley } 156808449791SMatthew G. Knepley /* Loop over chunks */ 156908449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 157008449791SMatthew G. Knepley numChunks = 1; 157108449791SMatthew G. Knepley cellChunkSize = (cEnd - cStart)/numChunks; 157208449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 157308449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 15742eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 15752eefff9cSMatthew G. Knepley PetscReal *vol; 157608449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 157708449791SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell; 15783755293bSMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face; 157908449791SMatthew G. Knepley 158008449791SMatthew G. Knepley /* Extract field coefficients */ 158108449791SMatthew G. Knepley if (useFEM) { 158208449791SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 158308449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 1584215c4595SMatthew G. Knepley ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 158508449791SMatthew G. Knepley } 158608449791SMatthew G. Knepley if (useFVM) { 15875f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 15885f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 158908449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 159008449791SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 1591215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1592215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 159308449791SMatthew G. Knepley } 159408449791SMatthew 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 */ 159508449791SMatthew G. Knepley /* Loop over fields */ 159608449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 159708449791SMatthew G. Knepley PetscObject obj; 159808449791SMatthew G. Knepley PetscClassId id; 15997173168dSMatthew G. Knepley PetscBool fimp; 160008449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 160108449791SMatthew G. Knepley 16027173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 16037173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 160408449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 160508449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 160608449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 160708449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 160808449791SMatthew G. Knepley PetscQuadrature q; 160908449791SMatthew G. Knepley PetscInt Nq, Nb; 161008449791SMatthew G. Knepley 161108449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 161208449791SMatthew G. Knepley 161308449791SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 161408449791SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 161508449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 161608449791SMatthew G. Knepley blockSize = Nb*Nq; 161708449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 161808449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 161908449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 162008449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 162108449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 162208449791SMatthew G. Knepley offset = numCells - Nr; 162308449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 162408449791SMatthew 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) */ 162511dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 162611dd639bSMatthew 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); 162708449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 162808449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 162908449791SMatthew G. Knepley 163008449791SMatthew G. Knepley Ne = numFaces; 163108449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 163208449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 163308449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 163408449791SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 163508449791SMatthew G. Knepley } 16362f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 16372f84e9bcSToby Isaac ierr = PetscFree(cgeomFEM);CHKERRQ(ierr); 16382f84e9bcSToby Isaac } 16392f84e9bcSToby Isaac else { 16402f84e9bcSToby Isaac cgeomFEM = NULL; 16412f84e9bcSToby Isaac } 16420163fd50SMatthew G. Knepley if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);} 164308449791SMatthew G. Knepley /* Loop over domain */ 164408449791SMatthew G. Knepley if (useFEM) { 164508449791SMatthew G. Knepley /* Add elemVec to locX */ 164608449791SMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 164708449791SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);} 1648c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 164908449791SMatthew G. Knepley } 165008449791SMatthew G. Knepley } 165108449791SMatthew G. Knepley if (useFVM) { 16524a394323SMatthew G. Knepley PetscScalar *fa; 165308449791SMatthew G. Knepley PetscInt iface; 165408449791SMatthew G. Knepley 165508449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1656c10b5f1bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1657c10b5f1bSMatthew G. Knepley PetscFV fv; 1658c10b5f1bSMatthew G. Knepley PetscObject obj; 1659c10b5f1bSMatthew G. Knepley PetscClassId id; 16604a394323SMatthew G. Knepley PetscInt foff, pdim; 1661c10b5f1bSMatthew G. Knepley 1662c10b5f1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1663c10b5f1bSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1664c10b5f1bSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1665c10b5f1bSMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1666c10b5f1bSMatthew G. Knepley fv = (PetscFV) obj; 1667c10b5f1bSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1668c10b5f1bSMatthew G. Knepley /* Accumulate fluxes to cells */ 166908449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 167008449791SMatthew G. Knepley const PetscInt *cells; 167108449791SMatthew G. Knepley PetscScalar *fL, *fR; 16727c45b140SToby Isaac PetscInt ghost, d, nsupp, nchild; 167308449791SMatthew G. Knepley 167408449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1675ffe9ad51SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 16767c45b140SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 16777c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 167808449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 1679c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr); 1680c10b5f1bSMatthew G. Knepley ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr); 1681c10b5f1bSMatthew G. Knepley for (d = 0; d < pdim; ++d) { 1682c10b5f1bSMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1683c10b5f1bSMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 168408449791SMatthew G. Knepley } 168508449791SMatthew G. Knepley ++iface; 168608449791SMatthew G. Knepley } 1687dab51205SMatthew G. Knepley } 1688dab51205SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1689dab51205SMatthew G. Knepley } 1690c10b5f1bSMatthew G. Knepley /* Handle time derivative */ 1691c10b5f1bSMatthew G. Knepley if (locX_t) { 1692dab51205SMatthew G. Knepley PetscScalar *x_t, *fa; 1693dab51205SMatthew G. Knepley 1694dab51205SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1695c10b5f1bSMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1696dab51205SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1697dab51205SMatthew G. Knepley PetscFV fv; 1698dab51205SMatthew G. Knepley PetscObject obj; 1699dab51205SMatthew G. Knepley PetscClassId id; 1700dab51205SMatthew G. Knepley PetscInt pdim, d; 1701dab51205SMatthew G. Knepley 1702dab51205SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1703dab51205SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1704dab51205SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1705dab51205SMatthew G. Knepley fv = (PetscFV) obj; 1706dab51205SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1707c10b5f1bSMatthew G. Knepley for (cell = cS; cell < cE; ++cell) { 1708c10b5f1bSMatthew G. Knepley PetscScalar *u_t, *r; 1709c10b5f1bSMatthew G. Knepley 1710c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1711c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1712d63b37e5SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1713c10b5f1bSMatthew G. Knepley } 1714dab51205SMatthew G. Knepley } 1715c10b5f1bSMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 171608449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 171708449791SMatthew G. Knepley } 171808449791SMatthew G. Knepley if (useFEM) { 171908449791SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 172008449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr); 172108449791SMatthew G. Knepley } 172208449791SMatthew G. Knepley if (useFVM) { 17235f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 17245f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 172508449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr); 172608449791SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr); 172708449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 172808449791SMatthew G. Knepley } 172908449791SMatthew G. Knepley } 173008449791SMatthew G. Knepley 173111dd639bSMatthew G. Knepley if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);} 173208449791SMatthew G. Knepley 173308449791SMatthew G. Knepley /* FEM */ 173408449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 173508449791SMatthew G. Knepley /* 2: Get geometric data */ 173608449791SMatthew G. Knepley /* 3: Handle boundary values */ 173708449791SMatthew G. Knepley /* 4: Loop over domain */ 173808449791SMatthew G. Knepley /* Extract coefficients */ 173908449791SMatthew G. Knepley /* Loop over fields */ 174008449791SMatthew G. Knepley /* Set tiling for FE*/ 174108449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 174208449791SMatthew G. Knepley /* Loop over subdomain */ 174308449791SMatthew G. Knepley /* Loop over quad points */ 174408449791SMatthew G. Knepley /* Transform coords to real space */ 174508449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 174608449791SMatthew G. Knepley /* Evaluate residual at point */ 174708449791SMatthew G. Knepley /* Transform residual to real space */ 174808449791SMatthew G. Knepley /* Add residual to elemVec */ 174908449791SMatthew G. Knepley /* Loop over domain */ 175008449791SMatthew G. Knepley /* Add elemVec to locX */ 175108449791SMatthew G. Knepley 175208449791SMatthew G. Knepley /* FVM */ 175308449791SMatthew G. Knepley /* Get geometric data */ 175408449791SMatthew G. Knepley /* If using gradients */ 175508449791SMatthew G. Knepley /* Compute gradient data */ 175608449791SMatthew G. Knepley /* Loop over domain faces */ 175708449791SMatthew G. Knepley /* Count computational faces */ 175808449791SMatthew G. Knepley /* Reconstruct cell gradient */ 175908449791SMatthew G. Knepley /* Loop over domain cells */ 176008449791SMatthew G. Knepley /* Limit cell gradients */ 176108449791SMatthew G. Knepley /* Handle boundary values */ 176208449791SMatthew G. Knepley /* Loop over domain faces */ 176308449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 176408449791SMatthew G. Knepley /* Riemann solve over faces */ 176508449791SMatthew G. Knepley /* Loop over domain faces */ 176608449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 176708449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 1768247ba720SToby Isaac if (mesh->printFEM) { 1769247ba720SToby Isaac Vec locFbc; 1770247ba720SToby Isaac PetscInt pStart, pEnd, p, maxDof; 1771247ba720SToby Isaac PetscScalar *zeroes; 1772247ba720SToby Isaac 1773247ba720SToby Isaac ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 1774247ba720SToby Isaac ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 1775247ba720SToby Isaac ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 1776247ba720SToby Isaac ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 1777247ba720SToby Isaac ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 1778247ba720SToby Isaac for (p = pStart; p < pEnd; p++) { 1779247ba720SToby Isaac ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 1780247ba720SToby Isaac } 1781247ba720SToby Isaac ierr = PetscFree(zeroes);CHKERRQ(ierr); 1782247ba720SToby Isaac ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 1783247ba720SToby Isaac ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 1784247ba720SToby Isaac } 178524cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 178624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 178724cdb843SMatthew G. Knepley } 178824cdb843SMatthew G. Knepley 178924cdb843SMatthew G. Knepley #undef __FUNCT__ 179024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal" 179111dd639bSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user) 179224cdb843SMatthew G. Knepley { 179324cdb843SMatthew G. Knepley DM dmCh, dmAux; 1794bbce034cSMatthew G. Knepley Vec A, cellgeom; 179524cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 179624cdb843SMatthew G. Knepley PetscQuadrature q; 179724cdb843SMatthew G. Knepley PetscSection section, sectionAux; 17982f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 17992f84e9bcSToby Isaac PetscScalar *cgeomScal; 180024cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 180124cdb843SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 18023755293bSMatthew G. Knepley PetscInt totDim, totDimAux = 0, diffCell = 0; 180324cdb843SMatthew G. Knepley PetscErrorCode ierr; 180424cdb843SMatthew G. Knepley 180524cdb843SMatthew G. Knepley PetscFunctionBegin; 180624cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 180724cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 180824cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 180924cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 181024cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 181124cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 181224cdb843SMatthew G. Knepley numCells = cEnd - cStart; 181324cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 181424cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 181524cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 181624cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 181724cdb843SMatthew G. Knepley if (dmAux) { 181824cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 181924cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 182024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 182124cdb843SMatthew G. Knepley } 182224cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1823bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 182424cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 182524cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1826bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 18272f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 18282f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 18292f84e9bcSToby Isaac DM dmCell; 18302f84e9bcSToby Isaac 18312f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 18322f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 18332f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 18347726bd6bSToby Isaac PetscScalar *thisgeom; 18352f84e9bcSToby Isaac 18362f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 18372f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 18382f84e9bcSToby Isaac } 18392f84e9bcSToby Isaac } 18402f84e9bcSToby Isaac else { 18412f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 18422f84e9bcSToby Isaac } 184324cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 184424cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 184524cdb843SMatthew G. Knepley PetscInt i; 184624cdb843SMatthew G. Knepley 184724cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 184824cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 184924cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 185024cdb843SMatthew G. Knepley if (X_t) { 185124cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 185224cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 185324cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 185424cdb843SMatthew G. Knepley } 185524cdb843SMatthew G. Knepley if (dmAux) { 18566da023fcSToby Isaac DM dmAuxPlex; 18576da023fcSToby Isaac 18586da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 18596da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 186024cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 18616da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 18626da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 186324cdb843SMatthew G. Knepley } 186424cdb843SMatthew G. Knepley } 186524cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 186624cdb843SMatthew G. Knepley PetscFE fe, feCh; 186724cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 186824cdb843SMatthew G. Knepley /* Conforming batches */ 186924cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 187024cdb843SMatthew G. Knepley /* Remainder */ 187124cdb843SMatthew G. Knepley PetscInt Nr, offset; 187224cdb843SMatthew G. Knepley 187324cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 187424cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 187524cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 187624cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 187724cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 187824cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 187924cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 188024cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 188124cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 188224cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 188324cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 188424cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 188524cdb843SMatthew G. Knepley offset = numCells - Nr; 188611dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 188711dd639bSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr); 188811dd639bSMatthew 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); 188911dd639bSMatthew 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); 189024cdb843SMatthew G. Knepley } 189124cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 189224cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 189324cdb843SMatthew G. Knepley PetscInt d; 189424cdb843SMatthew G. Knepley 189524cdb843SMatthew 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;} 189624cdb843SMatthew G. Knepley if (diff) { 189724cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 189824cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 189924cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 190024cdb843SMatthew G. Knepley ++diffCell; 190124cdb843SMatthew G. Knepley } 190224cdb843SMatthew G. Knepley if (diffCell > 9) break; 1903c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 190424cdb843SMatthew G. Knepley } 19052f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 19062f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 19072f84e9bcSToby Isaac } 19082f84e9bcSToby Isaac else { 19092f84e9bcSToby Isaac cgeom = NULL; 19102f84e9bcSToby Isaac } 19112f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 1912bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 191324cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 191424cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 191524cdb843SMatthew G. Knepley PetscFunctionReturn(0); 191624cdb843SMatthew G. Knepley } 191724cdb843SMatthew G. Knepley 191824cdb843SMatthew G. Knepley #undef __FUNCT__ 191924cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 192024cdb843SMatthew G. Knepley /*@ 192124cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 192224cdb843SMatthew G. Knepley 192324cdb843SMatthew G. Knepley Input Parameters: 192424cdb843SMatthew G. Knepley + dm - The mesh 192524cdb843SMatthew G. Knepley . X - Local solution 192624cdb843SMatthew G. Knepley - user - The user context 192724cdb843SMatthew G. Knepley 192824cdb843SMatthew G. Knepley Output Parameter: 192924cdb843SMatthew G. Knepley . F - Local output vector 193024cdb843SMatthew G. Knepley 193124cdb843SMatthew G. Knepley Level: developer 193224cdb843SMatthew G. Knepley 193324cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 193424cdb843SMatthew G. Knepley @*/ 193524cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 193624cdb843SMatthew G. Knepley { 193724cdb843SMatthew G. Knepley PetscObject check; 1938c4d4a4f8SMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 19396da023fcSToby Isaac DM plex; 194024cdb843SMatthew G. Knepley PetscErrorCode ierr; 194124cdb843SMatthew G. Knepley 194224cdb843SMatthew G. Knepley PetscFunctionBegin; 19436da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 19446da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 19456da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1946c4d4a4f8SMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 194724cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 19486da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 194911dd639bSMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 195011dd639bSMatthew G. Knepley else {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 19519a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 195224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 195324cdb843SMatthew G. Knepley } 195424cdb843SMatthew G. Knepley 195524cdb843SMatthew G. Knepley #undef __FUNCT__ 1956bdd6f66aSToby Isaac #define __FUNCT__ "DMPlexSNESComputeBoundaryFEM" 1957bdd6f66aSToby Isaac /*@ 1958bdd6f66aSToby Isaac DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 1959bdd6f66aSToby Isaac 1960bdd6f66aSToby Isaac Input Parameters: 1961bdd6f66aSToby Isaac + dm - The mesh 1962bdd6f66aSToby Isaac - user - The user context 1963bdd6f66aSToby Isaac 1964bdd6f66aSToby Isaac Output Parameter: 1965bdd6f66aSToby Isaac . X - Local solution 1966bdd6f66aSToby Isaac 1967bdd6f66aSToby Isaac Level: developer 1968bdd6f66aSToby Isaac 1969bdd6f66aSToby Isaac .seealso: DMPlexComputeJacobianActionFEM() 1970bdd6f66aSToby Isaac @*/ 1971bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 1972bdd6f66aSToby Isaac { 1973bdd6f66aSToby Isaac DM plex; 1974bdd6f66aSToby Isaac PetscErrorCode ierr; 1975bdd6f66aSToby Isaac 1976bdd6f66aSToby Isaac PetscFunctionBegin; 1977bdd6f66aSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 1978bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 1979bdd6f66aSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 1980bdd6f66aSToby Isaac PetscFunctionReturn(0); 1981bdd6f66aSToby Isaac } 1982bdd6f66aSToby Isaac 1983bdd6f66aSToby Isaac #undef __FUNCT__ 1984b953af5fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobian_Internal" 1985b953af5fSMatthew 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) 198624cdb843SMatthew G. Knepley { 198724cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 198824cdb843SMatthew G. Knepley const char *name = "Jacobian"; 1989f7ed7b21SMatthew G. Knepley DM dmAux, plex; 199024cdb843SMatthew G. Knepley DMLabel depth; 1991bbce034cSMatthew G. Knepley Vec A, cellgeom; 199224cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 199324cdb843SMatthew G. Knepley PetscQuadrature quad; 199424cdb843SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 19952f84e9bcSToby Isaac PetscFECellGeom *cgeom = NULL; 19962f84e9bcSToby Isaac PetscScalar *cgeomScal; 1997426ff135SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 1998b953af5fSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, c; 199924cdb843SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 20005244db28SMatthew G. Knepley PetscBool isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE; 200124cdb843SMatthew G. Knepley PetscErrorCode ierr; 200224cdb843SMatthew G. Knepley 200324cdb843SMatthew G. Knepley PetscFunctionBegin; 200424cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 200524cdb843SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 200624cdb843SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 200724cdb843SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 200824cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 200924cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 201024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 2011efc10488SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 201255ad3c34SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2013426ff135SMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2014426ff135SMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 201524cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 201624cdb843SMatthew G. Knepley numCells = cEnd - cStart; 201724cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 201824cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 201924cdb843SMatthew G. Knepley if (dmAux) { 2020f7ed7b21SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2021f7ed7b21SMatthew G. Knepley ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 202224cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 202324cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 202424cdb843SMatthew G. Knepley } 202524cdb843SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 2026efc10488SMatthew 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); 202724cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2028bbce034cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 20292f84e9bcSToby Isaac ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 20302f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 20312f84e9bcSToby Isaac DM dmCell; 20322f84e9bcSToby Isaac 20332f84e9bcSToby Isaac ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 20342f84e9bcSToby Isaac ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 20352f84e9bcSToby Isaac for (c = 0; c < cEnd - cStart; c++) { 20367726bd6bSToby Isaac PetscScalar *thisgeom; 20372f84e9bcSToby Isaac 20382f84e9bcSToby Isaac ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 20392f84e9bcSToby Isaac cgeom[c] = *((PetscFECellGeom *) thisgeom); 20402f84e9bcSToby Isaac } 2041efc10488SMatthew G. Knepley } else { 20422f84e9bcSToby Isaac cgeom = (PetscFECellGeom *) cgeomScal; 20432f84e9bcSToby Isaac } 204424cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 204524cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 204624cdb843SMatthew G. Knepley PetscInt i; 204724cdb843SMatthew G. Knepley 204824cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 20499f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 205024cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 205124cdb843SMatthew G. Knepley if (X_t) { 205224cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 20539f11d433SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 205424cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 205524cdb843SMatthew G. Knepley } 205624cdb843SMatthew G. Knepley if (dmAux) { 2057f7ed7b21SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 20589f11d433SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 2059f7ed7b21SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 206024cdb843SMatthew G. Knepley } 206124cdb843SMatthew G. Knepley } 2062efc10488SMatthew G. Knepley if (hasJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 206355ad3c34SMatthew G. Knepley if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2064426ff135SMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 206524cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 20665244db28SMatthew G. Knepley PetscClassId id; 206724cdb843SMatthew G. Knepley PetscFE fe; 206824cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 206924cdb843SMatthew G. Knepley /* Conforming batches */ 207024cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 207124cdb843SMatthew G. Knepley /* Remainder */ 207224cdb843SMatthew G. Knepley PetscInt Nr, offset; 207324cdb843SMatthew G. Knepley 207424cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 20755244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 20765244db28SMatthew G. Knepley if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 207724cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 207824cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 207924cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 208024cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 208124cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 208224cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 208324cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 208424cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 208524cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 208624cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 208724cdb843SMatthew G. Knepley offset = numCells - Nr; 208824cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2089efc10488SMatthew G. Knepley if (hasJac) { 209011dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 209111dd639bSMatthew 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); 2092efc10488SMatthew G. Knepley } 209355ad3c34SMatthew G. Knepley if (hasPrec) { 209411dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 209511dd639bSMatthew 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); 2096426ff135SMatthew G. Knepley } 2097426ff135SMatthew G. Knepley if (hasDyn) { 209811dd639bSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 209911dd639bSMatthew 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); 210055ad3c34SMatthew G. Knepley } 210124cdb843SMatthew G. Knepley } 210224cdb843SMatthew G. Knepley } 2103426ff135SMatthew G. Knepley if (hasDyn) { 2104426ff135SMatthew G. Knepley for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2105426ff135SMatthew G. Knepley } 21065244db28SMatthew G. Knepley if (hasFV) { 21075244db28SMatthew G. Knepley PetscClassId id; 21085244db28SMatthew G. Knepley PetscFV fv; 21095244db28SMatthew G. Knepley PetscInt offsetI, NcI, NbI = 1, fc, f; 21105244db28SMatthew G. Knepley 21115244db28SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 21125244db28SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 21135244db28SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 21145244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 21155244db28SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 21165244db28SMatthew G. Knepley /* Put in the identity */ 21175244db28SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 21185244db28SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 21195244db28SMatthew G. Knepley const PetscInt eOffset = (c-cStart)*totDim*totDim; 21205244db28SMatthew G. Knepley for (fc = 0; fc < NcI; ++fc) { 21215244db28SMatthew G. Knepley for (f = 0; f < NbI; ++f) { 21225244db28SMatthew G. Knepley const PetscInt i = offsetI + f*NcI+fc; 21235244db28SMatthew G. Knepley if (hasPrec) { 21245244db28SMatthew G. Knepley if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 21255244db28SMatthew G. Knepley elemMatP[eOffset+i*totDim+i] = 1.0; 21265244db28SMatthew G. Knepley } else {elemMat[eOffset+i*totDim+i] = 1.0;} 21275244db28SMatthew G. Knepley } 21285244db28SMatthew G. Knepley } 21295244db28SMatthew G. Knepley } 21305244db28SMatthew G. Knepley } 21315244db28SMatthew G. Knepley /* No allocated space for FV stuff, so ignore the zero entries */ 21325244db28SMatthew G. Knepley ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 21335244db28SMatthew G. Knepley } 213424cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 213555ad3c34SMatthew G. Knepley if (hasPrec) { 2136efc10488SMatthew G. Knepley if (hasJac) { 213755ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 213855ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2139efc10488SMatthew G. Knepley } 214055ad3c34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 214155ad3c34SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 214255ad3c34SMatthew G. Knepley } else { 21439f11d433SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 21449f11d433SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 214524cdb843SMatthew G. Knepley } 214655ad3c34SMatthew G. Knepley } 21475244db28SMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 21482f84e9bcSToby Isaac if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 21492f84e9bcSToby Isaac ierr = PetscFree(cgeom);CHKERRQ(ierr); 21505244db28SMatthew G. Knepley } else { 21512f84e9bcSToby Isaac cgeom = NULL; 21522f84e9bcSToby Isaac } 21532f84e9bcSToby Isaac ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2154426ff135SMatthew G. Knepley ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2155f7ed7b21SMatthew G. Knepley if (dmAux) { 2156f7ed7b21SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2157f7ed7b21SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2158f7ed7b21SMatthew G. Knepley } 215924cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2160a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 216124cdb843SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 2162a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 216324cdb843SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 216424cdb843SMatthew G. Knepley const char *bdLabel; 216524cdb843SMatthew G. Knepley DMLabel label; 216624cdb843SMatthew G. Knepley IS pointIS; 216724cdb843SMatthew G. Knepley const PetscInt *points; 216824cdb843SMatthew G. Knepley const PetscInt *values; 2169a8e83e26SSanderA PetscInt field, numValues, v, numPoints, p, dep, numFaces; 217024cdb843SMatthew G. Knepley PetscBool isEssential; 217180bc4632SMatthew G. Knepley PetscObject obj; 217280bc4632SMatthew G. Knepley PetscClassId id; 217324cdb843SMatthew G. Knepley 2174a6ba4734SToby Isaac ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 217580bc4632SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 217680bc4632SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 217780bc4632SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || isEssential) continue; 2178c58f1c22SToby Isaac ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 2179a8e83e26SSanderA for (v = 0; v < numValues; ++v) { 2180a8e83e26SSanderA ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr); 2181a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 218222734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 218324cdb843SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 218424cdb843SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 218524cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 218624cdb843SMatthew G. Knepley if (dep == dim-1) ++numFaces; 218724cdb843SMatthew G. Knepley } 2188bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 218924cdb843SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 219024cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 219124cdb843SMatthew G. Knepley const PetscInt point = points[p]; 219224cdb843SMatthew G. Knepley PetscScalar *x = NULL; 219324cdb843SMatthew G. Knepley PetscInt i; 219424cdb843SMatthew G. Knepley 219524cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 219624cdb843SMatthew G. Knepley if (dep != dim-1) continue; 2197bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr); 2198302440fdSBarry Smith ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr); 2199bbce034cSMatthew 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); 220024cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 220124cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 220224cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 220324cdb843SMatthew G. Knepley if (X_t) { 220424cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 220524cdb843SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 220624cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 220724cdb843SMatthew G. Knepley } 220824cdb843SMatthew G. Knepley ++f; 220924cdb843SMatthew G. Knepley } 221024cdb843SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 221124cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 221224cdb843SMatthew G. Knepley PetscFE fe; 221324cdb843SMatthew G. Knepley PetscInt numQuadPoints, Nb; 221424cdb843SMatthew G. Knepley /* Conforming batches */ 221524cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 221624cdb843SMatthew G. Knepley /* Remainder */ 221724cdb843SMatthew G. Knepley PetscInt Nr, offset; 221824cdb843SMatthew G. Knepley 221924cdb843SMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 222024cdb843SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 222124cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 222224cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 222324cdb843SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 222424cdb843SMatthew G. Knepley blockSize = Nb*numQuadPoints; 222524cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 222624cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 222724cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 222824cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 222924cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 223024cdb843SMatthew G. Knepley offset = numFaces - Nr; 223124cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 223211dd639bSMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, t, X_tShift, elemMat);CHKERRQ(ierr); 223311dd639bSMatthew 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); 223424cdb843SMatthew G. Knepley } 223524cdb843SMatthew G. Knepley } 223624cdb843SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 223724cdb843SMatthew G. Knepley const PetscInt point = points[p]; 223824cdb843SMatthew G. Knepley 223924cdb843SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 224024cdb843SMatthew G. Knepley if (dep != dim-1) continue; 224124cdb843SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 224224cdb843SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 224324cdb843SMatthew G. Knepley ++f; 224424cdb843SMatthew G. Knepley } 224524cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 224624cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2247bbce034cSMatthew G. Knepley ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr); 224824cdb843SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 224924cdb843SMatthew G. Knepley } 2250a8e83e26SSanderA } 2251efc10488SMatthew G. Knepley if (hasJac && hasPrec) { 225282ef7567SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 225382ef7567SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 225482ef7567SMatthew G. Knepley } 225524cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 225624cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 225724cdb843SMatthew G. Knepley if (mesh->printFEM) { 225824cdb843SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 225924cdb843SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 226024cdb843SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 226124cdb843SMatthew G. Knepley } 226224cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 226324cdb843SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 226424cdb843SMatthew G. Knepley if (isShell) { 226524cdb843SMatthew G. Knepley JacActionCtx *jctx; 226624cdb843SMatthew G. Knepley 226724cdb843SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 226824cdb843SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 226924cdb843SMatthew G. Knepley } 227024cdb843SMatthew G. Knepley PetscFunctionReturn(0); 227124cdb843SMatthew G. Knepley } 227224cdb843SMatthew G. Knepley 2273a925c78cSMatthew G. Knepley 2274a925c78cSMatthew G. Knepley #undef __FUNCT__ 2275a925c78cSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianAction_Internal" 2276a925c78cSMatthew 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) 2277a925c78cSMatthew G. Knepley { 2278a925c78cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2279a925c78cSMatthew G. Knepley const char *name = "Jacobian"; 2280a925c78cSMatthew G. Knepley DM dmAux, plex; 2281a925c78cSMatthew G. Knepley Vec A, cellgeom; 2282a925c78cSMatthew G. Knepley PetscDS prob, probAux = NULL; 2283a925c78cSMatthew G. Knepley PetscQuadrature quad; 2284a925c78cSMatthew G. Knepley PetscSection section, globalSection, sectionAux; 2285a925c78cSMatthew G. Knepley PetscFECellGeom *cgeom = NULL; 2286a925c78cSMatthew G. Knepley PetscScalar *cgeomScal; 2287a925c78cSMatthew G. Knepley PetscScalar *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z; 228875b37a90SMatthew G. Knepley PetscInt dim, Nf, fieldI, fieldJ, numCells, c; 22893755293bSMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux = 0; 229075b37a90SMatthew G. Knepley PetscBool hasDyn; 2291a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2292a925c78cSMatthew G. Knepley 2293a925c78cSMatthew G. Knepley PetscFunctionBegin; 2294a925c78cSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2295a925c78cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2296a925c78cSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 2297a925c78cSMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2298a925c78cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2299a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2300a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 2301a925c78cSMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2302a925c78cSMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2303a925c78cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 2304a925c78cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2305a925c78cSMatthew G. Knepley numCells = cEnd - cStart; 2306a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2307a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2308a925c78cSMatthew G. Knepley if (dmAux) { 2309a925c78cSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2310a925c78cSMatthew G. Knepley ierr = DMGetDefaultSection(plex, §ionAux);CHKERRQ(ierr); 2311a925c78cSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2312a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2313a925c78cSMatthew G. Knepley } 2314a925c78cSMatthew G. Knepley ierr = VecSet(Z, 0.0);CHKERRQ(ierr); 2315a925c78cSMatthew 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); 2316a925c78cSMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 2317a925c78cSMatthew G. Knepley ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr); 2318a925c78cSMatthew G. Knepley ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2319a925c78cSMatthew G. Knepley if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) { 2320a925c78cSMatthew G. Knepley DM dmCell; 2321a925c78cSMatthew G. Knepley 2322a925c78cSMatthew G. Knepley ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr); 2323a925c78cSMatthew G. Knepley ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr); 2324a925c78cSMatthew G. Knepley for (c = 0; c < cEnd - cStart; c++) { 2325a925c78cSMatthew G. Knepley PetscScalar *thisgeom; 2326a925c78cSMatthew G. Knepley 2327a925c78cSMatthew G. Knepley ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr); 2328a925c78cSMatthew G. Knepley cgeom[c] = *((PetscFECellGeom *) thisgeom); 2329a925c78cSMatthew G. Knepley } 2330a925c78cSMatthew G. Knepley } else { 2331a925c78cSMatthew G. Knepley cgeom = (PetscFECellGeom *) cgeomScal; 2332a925c78cSMatthew G. Knepley } 2333a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2334a925c78cSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 2335a925c78cSMatthew G. Knepley PetscInt i; 2336a925c78cSMatthew G. Knepley 2337a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2338a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i]; 2339a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 2340a925c78cSMatthew G. Knepley if (X_t) { 2341a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2342a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i]; 2343a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 2344a925c78cSMatthew G. Knepley } 2345a925c78cSMatthew G. Knepley if (dmAux) { 2346a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2347a925c78cSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i]; 2348a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 2349a925c78cSMatthew G. Knepley } 2350a925c78cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr); 2351a925c78cSMatthew G. Knepley for (i = 0; i < totDim; ++i) y[(c-cStart)*totDim+i] = x[i]; 2352a925c78cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr); 2353a925c78cSMatthew G. Knepley } 2354a925c78cSMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2355a925c78cSMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2356a925c78cSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 2357a925c78cSMatthew G. Knepley PetscFE fe; 2358a925c78cSMatthew G. Knepley PetscInt numQuadPoints, Nb; 2359a925c78cSMatthew G. Knepley /* Conforming batches */ 2360a925c78cSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2361a925c78cSMatthew G. Knepley /* Remainder */ 2362a925c78cSMatthew G. Knepley PetscInt Nr, offset; 2363a925c78cSMatthew G. Knepley 2364a925c78cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2365a925c78cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2366a925c78cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2367a925c78cSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2368a925c78cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 2369a925c78cSMatthew G. Knepley blockSize = Nb*numQuadPoints; 2370a925c78cSMatthew G. Knepley batchSize = numBlocks * blockSize; 2371a925c78cSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2372a925c78cSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 2373a925c78cSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 2374a925c78cSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 2375a925c78cSMatthew G. Knepley offset = numCells - Nr; 2376a925c78cSMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 23771bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 23781bf9e2faSMatthew 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); 2379a925c78cSMatthew G. Knepley if (hasDyn) { 23801bf9e2faSMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 23811bf9e2faSMatthew 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); 2382a925c78cSMatthew G. Knepley } 2383a925c78cSMatthew G. Knepley } 2384a925c78cSMatthew G. Knepley } 2385a925c78cSMatthew G. Knepley if (hasDyn) { 2386a925c78cSMatthew G. Knepley for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2387a925c78cSMatthew G. Knepley } 2388a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2389a925c78cSMatthew G. Knepley const PetscBLASInt M = totDim, one = 1; 2390a925c78cSMatthew G. Knepley const PetscScalar a = 1.0, b = 0.0; 2391a925c78cSMatthew G. Knepley 2392a925c78cSMatthew G. Knepley PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[(c-cStart)*totDim*totDim], &M, &y[(c-cStart)*totDim], &one, &b, z, &one)); 2393a925c78cSMatthew G. Knepley if (mesh->printFEM > 1) { 2394a925c78cSMatthew G. Knepley ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr); 2395a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Y", totDim, &y[(c-cStart)*totDim]);CHKERRQ(ierr); 2396a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Z", totDim, z);CHKERRQ(ierr); 2397a925c78cSMatthew G. Knepley } 2398a925c78cSMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, Z, c, z, ADD_VALUES);CHKERRQ(ierr); 2399a925c78cSMatthew G. Knepley } 2400a925c78cSMatthew G. Knepley if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {ierr = PetscFree(cgeom);CHKERRQ(ierr);} 2401a925c78cSMatthew G. Knepley else {cgeom = NULL;} 2402a925c78cSMatthew G. Knepley ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr); 2403a925c78cSMatthew G. Knepley ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr); 2404a925c78cSMatthew G. Knepley if (dmAux) { 2405a925c78cSMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2406a925c78cSMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2407a925c78cSMatthew G. Knepley } 2408a925c78cSMatthew G. Knepley if (mesh->printFEM) { 2409a925c78cSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr); 2410a925c78cSMatthew G. Knepley ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2411a925c78cSMatthew G. Knepley } 2412a925c78cSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2413a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2414a925c78cSMatthew G. Knepley } 2415a925c78cSMatthew G. Knepley 241624cdb843SMatthew G. Knepley #undef __FUNCT__ 241724cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 241824cdb843SMatthew G. Knepley /*@ 241924cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 242024cdb843SMatthew G. Knepley 242124cdb843SMatthew G. Knepley Input Parameters: 242224cdb843SMatthew G. Knepley + dm - The mesh 242324cdb843SMatthew G. Knepley . X - Local input vector 242424cdb843SMatthew G. Knepley - user - The user context 242524cdb843SMatthew G. Knepley 242624cdb843SMatthew G. Knepley Output Parameter: 242724cdb843SMatthew G. Knepley . Jac - Jacobian matrix 242824cdb843SMatthew G. Knepley 242924cdb843SMatthew G. Knepley Note: 243024cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 243124cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 243224cdb843SMatthew G. Knepley 243324cdb843SMatthew G. Knepley Level: developer 243424cdb843SMatthew G. Knepley 243524cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 243624cdb843SMatthew G. Knepley @*/ 243724cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 243824cdb843SMatthew G. Knepley { 2439b953af5fSMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 24406da023fcSToby Isaac DM plex; 244124cdb843SMatthew G. Knepley PetscErrorCode ierr; 244224cdb843SMatthew G. Knepley 244324cdb843SMatthew G. Knepley PetscFunctionBegin; 24446da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 24456da023fcSToby Isaac ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 24466da023fcSToby Isaac ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2447b953af5fSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 24486da023fcSToby Isaac ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 24499a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 245024cdb843SMatthew G. Knepley PetscFunctionReturn(0); 245124cdb843SMatthew G. Knepley } 24521878804aSMatthew G. Knepley 24531878804aSMatthew G. Knepley #undef __FUNCT__ 2454a925c78cSMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianActionFEM" 2455a925c78cSMatthew G. Knepley /*@ 2456a925c78cSMatthew 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. 2457a925c78cSMatthew G. Knepley 2458a925c78cSMatthew G. Knepley Input Parameters: 2459a925c78cSMatthew G. Knepley + dm - The mesh 2460a925c78cSMatthew G. Knepley . X - Local solution vector 2461a925c78cSMatthew G. Knepley . Y - Local input vector 2462a925c78cSMatthew G. Knepley - user - The user context 2463a925c78cSMatthew G. Knepley 2464a925c78cSMatthew G. Knepley Output Parameter: 2465a925c78cSMatthew G. Knepley . Z - Local output vector 2466a925c78cSMatthew G. Knepley 2467a925c78cSMatthew G. Knepley Note: 2468a925c78cSMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 2469a925c78cSMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 2470a925c78cSMatthew G. Knepley 2471a925c78cSMatthew G. Knepley Level: developer 2472a925c78cSMatthew G. Knepley 2473a925c78cSMatthew G. Knepley .seealso: FormFunctionLocal() 2474a925c78cSMatthew G. Knepley @*/ 2475a925c78cSMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianActionFEM(DM dm, Vec X, Vec Y, Vec Z, void *user) 2476a925c78cSMatthew G. Knepley { 2477a925c78cSMatthew G. Knepley PetscInt cStart, cEnd, cEndInterior; 2478a925c78cSMatthew G. Knepley DM plex; 2479a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2480a925c78cSMatthew G. Knepley 2481a925c78cSMatthew G. Knepley PetscFunctionBegin; 2482a925c78cSMatthew G. Knepley ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2483a925c78cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr); 2484a925c78cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2485a925c78cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2486a925c78cSMatthew G. Knepley ierr = DMPlexComputeJacobianAction_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Y, Z, user);CHKERRQ(ierr); 2487a925c78cSMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2488a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2489a925c78cSMatthew G. Knepley } 2490a925c78cSMatthew G. Knepley 2491a925c78cSMatthew G. Knepley #undef __FUNCT__ 24929f520fc2SToby Isaac #define __FUNCT__ "DMPlexSetSNESLocalFEM" 24939f520fc2SToby Isaac /*@ 24949f520fc2SToby Isaac DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 24959f520fc2SToby Isaac 24969f520fc2SToby Isaac Input Parameters: 24979f520fc2SToby Isaac + dm - The DM object 24989f520fc2SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see DMAddBoundary()) 24999f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 25009f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 25011a244344SSatish Balay 25021a244344SSatish Balay Level: developer 25039f520fc2SToby Isaac @*/ 25049f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 25059f520fc2SToby Isaac { 25069f520fc2SToby Isaac PetscErrorCode ierr; 25079f520fc2SToby Isaac 25089f520fc2SToby Isaac PetscFunctionBegin; 25099f520fc2SToby Isaac ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 25109f520fc2SToby Isaac ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 25119f520fc2SToby Isaac ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 25129f520fc2SToby Isaac PetscFunctionReturn(0); 25139f520fc2SToby Isaac } 25149f520fc2SToby Isaac 25159f520fc2SToby Isaac #undef __FUNCT__ 25161878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions_Internal" 25170163fd50SMatthew 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) 25181878804aSMatthew G. Knepley { 25191878804aSMatthew G. Knepley Mat J, M; 25201878804aSMatthew G. Knepley Vec r, b; 25211878804aSMatthew G. Knepley MatNullSpace nullSpace; 25221878804aSMatthew G. Knepley PetscReal *error, res = 0.0; 25231878804aSMatthew G. Knepley PetscInt numFields; 25241878804aSMatthew G. Knepley PetscErrorCode ierr; 25251878804aSMatthew G. Knepley 25261878804aSMatthew G. Knepley PetscFunctionBegin; 25271878804aSMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 25281878804aSMatthew G. Knepley ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 25291878804aSMatthew G. Knepley M = J; 25301878804aSMatthew G. Knepley /* TODO Null space for J */ 25311878804aSMatthew G. Knepley /* Check discretization error */ 25321878804aSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 25331878804aSMatthew G. Knepley ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 25341878804aSMatthew G. Knepley if (numFields > 1) { 25351878804aSMatthew G. Knepley PetscInt f; 25361878804aSMatthew G. Knepley 25371189c1efSToby Isaac ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr); 25381878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 25391878804aSMatthew G. Knepley for (f = 0; f < numFields; ++f) { 25401878804aSMatthew G. Knepley if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 25411878804aSMatthew G. Knepley if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);} 25421878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 25431878804aSMatthew G. Knepley } 25441878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 25451878804aSMatthew G. Knepley } else { 25460709b2feSToby Isaac ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr); 25471878804aSMatthew G. Knepley if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);} 25481878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 25491878804aSMatthew G. Knepley } 25501878804aSMatthew G. Knepley ierr = PetscFree(error);CHKERRQ(ierr); 25511878804aSMatthew G. Knepley /* Check residual */ 25521878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 25531878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 25541878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr); 25551878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 25561878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2557685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2558685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 25591878804aSMatthew G. Knepley /* Check Jacobian */ 25601878804aSMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr); 25611878804aSMatthew G. Knepley ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 25621878804aSMatthew G. Knepley if (nullSpace) { 25631878804aSMatthew G. Knepley PetscBool isNull; 25641878804aSMatthew G. Knepley ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 25651878804aSMatthew G. Knepley if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 25661878804aSMatthew G. Knepley } 25671878804aSMatthew G. Knepley ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 25681878804aSMatthew G. Knepley ierr = VecSet(r, 0.0);CHKERRQ(ierr); 25691878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 25701878804aSMatthew G. Knepley ierr = MatMult(M, u, r);CHKERRQ(ierr); 25711878804aSMatthew G. Knepley ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 25721878804aSMatthew G. Knepley ierr = VecDestroy(&b);CHKERRQ(ierr); 25731878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 25741878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr); 25751878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 25761878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2577685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2578685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 25791878804aSMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 25801878804aSMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 25811878804aSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 25821878804aSMatthew G. Knepley PetscFunctionReturn(0); 25831878804aSMatthew G. Knepley } 25841878804aSMatthew G. Knepley 25851878804aSMatthew G. Knepley #undef __FUNCT__ 25861878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions" 25870163fd50SMatthew 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) 25881878804aSMatthew G. Knepley { 25891878804aSMatthew G. Knepley DM dm; 25901878804aSMatthew G. Knepley Vec sol; 25911878804aSMatthew G. Knepley PetscBool check; 25921878804aSMatthew G. Knepley PetscErrorCode ierr; 25931878804aSMatthew G. Knepley 25941878804aSMatthew G. Knepley PetscFunctionBegin; 2595c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 25961878804aSMatthew G. Knepley if (!check) PetscFunctionReturn(0); 25971878804aSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 25981878804aSMatthew G. Knepley ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 25991878804aSMatthew G. Knepley ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 26001878804aSMatthew G. Knepley ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr); 26011878804aSMatthew G. Knepley ierr = VecDestroy(&sol);CHKERRQ(ierr); 2602552f7358SJed Brown PetscFunctionReturn(0); 2603552f7358SJed Brown } 2604