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 106da023fcSToby Isaac static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy) 116da023fcSToby Isaac { 126da023fcSToby Isaac PetscBool isPlex; 136da023fcSToby Isaac PetscErrorCode ierr; 146da023fcSToby Isaac 156da023fcSToby Isaac PetscFunctionBegin; 166da023fcSToby Isaac ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr); 176da023fcSToby Isaac if (isPlex) { 186da023fcSToby Isaac *plex = dm; 196da023fcSToby Isaac ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 20f7148743SMatthew G. Knepley } else { 21f7148743SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr); 22f7148743SMatthew G. Knepley if (!*plex) { 236da023fcSToby Isaac ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 24f7148743SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr); 256da023fcSToby Isaac if (copy) { 266da023fcSToby Isaac PetscInt i; 276da023fcSToby Isaac PetscObject obj; 286da023fcSToby Isaac const char *comps[3] = {"A","dmAux","dmCh"}; 296da023fcSToby Isaac 306da023fcSToby Isaac ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr); 316da023fcSToby Isaac for (i = 0; i < 3; i++) { 326da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr); 336da023fcSToby Isaac ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr); 346da023fcSToby Isaac } 356da023fcSToby Isaac } 36f7148743SMatthew G. Knepley } else { 37f7148743SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr); 38f7148743SMatthew G. Knepley } 396da023fcSToby Isaac } 406da023fcSToby Isaac PetscFunctionReturn(0); 416da023fcSToby Isaac } 426da023fcSToby Isaac 434267b1a3SMatthew G. Knepley /*@C 444267b1a3SMatthew G. Knepley DMInterpolationCreate - Creates a DMInterpolationInfo context 454267b1a3SMatthew G. Knepley 464267b1a3SMatthew G. Knepley Collective on comm 474267b1a3SMatthew G. Knepley 484267b1a3SMatthew G. Knepley Input Parameter: 494267b1a3SMatthew G. Knepley . comm - the communicator 504267b1a3SMatthew G. Knepley 514267b1a3SMatthew G. Knepley Output Parameter: 524267b1a3SMatthew G. Knepley . ctx - the context 534267b1a3SMatthew G. Knepley 544267b1a3SMatthew G. Knepley Level: beginner 554267b1a3SMatthew G. Knepley 564267b1a3SMatthew G. Knepley .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationDestroy() 574267b1a3SMatthew G. Knepley @*/ 580adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx) 590adebc6cSBarry Smith { 60552f7358SJed Brown PetscErrorCode ierr; 61552f7358SJed Brown 62552f7358SJed Brown PetscFunctionBegin; 63552f7358SJed Brown PetscValidPointer(ctx, 2); 6495dccacaSBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 651aa26658SKarl Rupp 66552f7358SJed Brown (*ctx)->comm = comm; 67552f7358SJed Brown (*ctx)->dim = -1; 68552f7358SJed Brown (*ctx)->nInput = 0; 690298fd71SBarry Smith (*ctx)->points = NULL; 700298fd71SBarry Smith (*ctx)->cells = NULL; 71552f7358SJed Brown (*ctx)->n = -1; 720298fd71SBarry Smith (*ctx)->coords = NULL; 73552f7358SJed Brown PetscFunctionReturn(0); 74552f7358SJed Brown } 75552f7358SJed Brown 764267b1a3SMatthew G. Knepley /*@C 774267b1a3SMatthew G. Knepley DMInterpolationSetDim - Sets the spatial dimension for the interpolation context 784267b1a3SMatthew G. Knepley 794267b1a3SMatthew G. Knepley Not collective 804267b1a3SMatthew G. Knepley 814267b1a3SMatthew G. Knepley Input Parameters: 824267b1a3SMatthew G. Knepley + ctx - the context 834267b1a3SMatthew G. Knepley - dim - the spatial dimension 844267b1a3SMatthew G. Knepley 854267b1a3SMatthew G. Knepley Level: intermediate 864267b1a3SMatthew G. Knepley 874267b1a3SMatthew G. Knepley .seealso: DMInterpolationGetDim(), DMInterpolationEvaluate(), DMInterpolationAddPoints() 884267b1a3SMatthew G. Knepley @*/ 890adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim) 900adebc6cSBarry Smith { 91552f7358SJed Brown PetscFunctionBegin; 920adebc6cSBarry Smith if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim); 93552f7358SJed Brown ctx->dim = dim; 94552f7358SJed Brown PetscFunctionReturn(0); 95552f7358SJed Brown } 96552f7358SJed Brown 974267b1a3SMatthew G. Knepley /*@C 984267b1a3SMatthew G. Knepley DMInterpolationGetDim - Gets the spatial dimension for the interpolation context 994267b1a3SMatthew G. Knepley 1004267b1a3SMatthew G. Knepley Not collective 1014267b1a3SMatthew G. Knepley 1024267b1a3SMatthew G. Knepley Input Parameter: 1034267b1a3SMatthew G. Knepley . ctx - the context 1044267b1a3SMatthew G. Knepley 1054267b1a3SMatthew G. Knepley Output Parameter: 1064267b1a3SMatthew G. Knepley . dim - the spatial dimension 1074267b1a3SMatthew G. Knepley 1084267b1a3SMatthew G. Knepley Level: intermediate 1094267b1a3SMatthew G. Knepley 1104267b1a3SMatthew G. Knepley .seealso: DMInterpolationSetDim(), DMInterpolationEvaluate(), DMInterpolationAddPoints() 1114267b1a3SMatthew G. Knepley @*/ 1120adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim) 1130adebc6cSBarry Smith { 114552f7358SJed Brown PetscFunctionBegin; 115552f7358SJed Brown PetscValidIntPointer(dim, 2); 116552f7358SJed Brown *dim = ctx->dim; 117552f7358SJed Brown PetscFunctionReturn(0); 118552f7358SJed Brown } 119552f7358SJed Brown 1204267b1a3SMatthew G. Knepley /*@C 1214267b1a3SMatthew G. Knepley DMInterpolationSetDof - Sets the number of fields interpolated at a point for the interpolation context 1224267b1a3SMatthew G. Knepley 1234267b1a3SMatthew G. Knepley Not collective 1244267b1a3SMatthew G. Knepley 1254267b1a3SMatthew G. Knepley Input Parameters: 1264267b1a3SMatthew G. Knepley + ctx - the context 1274267b1a3SMatthew G. Knepley - dof - the number of fields 1284267b1a3SMatthew G. Knepley 1294267b1a3SMatthew G. Knepley Level: intermediate 1304267b1a3SMatthew G. Knepley 1314267b1a3SMatthew G. Knepley .seealso: DMInterpolationGetDof(), DMInterpolationEvaluate(), DMInterpolationAddPoints() 1324267b1a3SMatthew G. Knepley @*/ 1330adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof) 1340adebc6cSBarry Smith { 135552f7358SJed Brown PetscFunctionBegin; 1360adebc6cSBarry Smith if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof); 137552f7358SJed Brown ctx->dof = dof; 138552f7358SJed Brown PetscFunctionReturn(0); 139552f7358SJed Brown } 140552f7358SJed Brown 1414267b1a3SMatthew G. Knepley /*@C 1424267b1a3SMatthew G. Knepley DMInterpolationGetDof - Gets the number of fields interpolated at a point for the interpolation context 1434267b1a3SMatthew G. Knepley 1444267b1a3SMatthew G. Knepley Not collective 1454267b1a3SMatthew G. Knepley 1464267b1a3SMatthew G. Knepley Input Parameter: 1474267b1a3SMatthew G. Knepley . ctx - the context 1484267b1a3SMatthew G. Knepley 1494267b1a3SMatthew G. Knepley Output Parameter: 1504267b1a3SMatthew G. Knepley . dof - the number of fields 1514267b1a3SMatthew G. Knepley 1524267b1a3SMatthew G. Knepley Level: intermediate 1534267b1a3SMatthew G. Knepley 1544267b1a3SMatthew G. Knepley .seealso: DMInterpolationSetDof(), DMInterpolationEvaluate(), DMInterpolationAddPoints() 1554267b1a3SMatthew G. Knepley @*/ 1560adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof) 1570adebc6cSBarry Smith { 158552f7358SJed Brown PetscFunctionBegin; 159552f7358SJed Brown PetscValidIntPointer(dof, 2); 160552f7358SJed Brown *dof = ctx->dof; 161552f7358SJed Brown PetscFunctionReturn(0); 162552f7358SJed Brown } 163552f7358SJed Brown 1644267b1a3SMatthew G. Knepley /*@C 1654267b1a3SMatthew G. Knepley DMInterpolationAddPoints - Add points at which we will interpolate the fields 1664267b1a3SMatthew G. Knepley 1674267b1a3SMatthew G. Knepley Not collective 1684267b1a3SMatthew G. Knepley 1694267b1a3SMatthew G. Knepley Input Parameters: 1704267b1a3SMatthew G. Knepley + ctx - the context 1714267b1a3SMatthew G. Knepley . n - the number of points 1724267b1a3SMatthew G. Knepley - points - the coordinates for each point, an array of size n * dim 1734267b1a3SMatthew G. Knepley 1744267b1a3SMatthew G. Knepley Note: The coordinate information is copied. 1754267b1a3SMatthew G. Knepley 1764267b1a3SMatthew G. Knepley Level: intermediate 1774267b1a3SMatthew G. Knepley 1784267b1a3SMatthew G. Knepley .seealso: DMInterpolationSetDim(), DMInterpolationEvaluate(), DMInterpolationCreate() 1794267b1a3SMatthew G. Knepley @*/ 1800adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[]) 1810adebc6cSBarry Smith { 182552f7358SJed Brown PetscErrorCode ierr; 183552f7358SJed Brown 184552f7358SJed Brown PetscFunctionBegin; 1850adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 1860adebc6cSBarry Smith if (ctx->points) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet"); 187552f7358SJed Brown ctx->nInput = n; 1881aa26658SKarl Rupp 189785e854fSJed Brown ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr); 190552f7358SJed Brown ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr); 191552f7358SJed Brown PetscFunctionReturn(0); 192552f7358SJed Brown } 193552f7358SJed Brown 1944267b1a3SMatthew G. Knepley /*@C 1954267b1a3SMatthew G. Knepley DMInterpolationSetUp - Computea spatial indices that add in point location during interpolation 1964267b1a3SMatthew G. Knepley 1974267b1a3SMatthew G. Knepley Collective on ctx 1984267b1a3SMatthew G. Knepley 1994267b1a3SMatthew G. Knepley Input Parameters: 2004267b1a3SMatthew G. Knepley + ctx - the context 2014267b1a3SMatthew G. Knepley . dm - the DM for the function space used for interpolation 2024267b1a3SMatthew G. Knepley - redundantPoints - If PETSC_TRUE, all processes are passing in the same array of points. Otherwise, points need to be communicated among processes. 2034267b1a3SMatthew G. Knepley 2044267b1a3SMatthew G. Knepley Level: intermediate 2054267b1a3SMatthew G. Knepley 2064267b1a3SMatthew G. Knepley .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 2074267b1a3SMatthew G. Knepley @*/ 2080adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints) 2090adebc6cSBarry Smith { 210552f7358SJed Brown MPI_Comm comm = ctx->comm; 211552f7358SJed Brown PetscScalar *a; 212552f7358SJed Brown PetscInt p, q, i; 213552f7358SJed Brown PetscMPIInt rank, size; 214552f7358SJed Brown PetscErrorCode ierr; 215552f7358SJed Brown Vec pointVec; 2163a93e3b7SToby Isaac PetscSF cellSF; 217552f7358SJed Brown PetscLayout layout; 218552f7358SJed Brown PetscReal *globalPoints; 219cb313848SJed Brown PetscScalar *globalPointsScalar; 220552f7358SJed Brown const PetscInt *ranges; 221552f7358SJed Brown PetscMPIInt *counts, *displs; 2223a93e3b7SToby Isaac const PetscSFNode *foundCells; 2233a93e3b7SToby Isaac const PetscInt *foundPoints; 224552f7358SJed Brown PetscMPIInt *foundProcs, *globalProcs; 2253a93e3b7SToby Isaac PetscInt n, N, numFound; 226552f7358SJed Brown 22719436ca2SJed Brown PetscFunctionBegin; 22819436ca2SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 22919436ca2SJed Brown ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 23019436ca2SJed Brown ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 2310adebc6cSBarry Smith if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set"); 23219436ca2SJed Brown /* Locate points */ 23319436ca2SJed Brown n = ctx->nInput; 234552f7358SJed Brown if (!redundantPoints) { 235552f7358SJed Brown ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 236552f7358SJed Brown ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 237552f7358SJed Brown ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr); 238552f7358SJed Brown ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 239552f7358SJed Brown ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr); 240552f7358SJed Brown /* Communicate all points to all processes */ 241dcca6d9dSJed Brown ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr); 242552f7358SJed Brown ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 243552f7358SJed Brown for (p = 0; p < size; ++p) { 244552f7358SJed Brown counts[p] = (ranges[p+1] - ranges[p])*ctx->dim; 245552f7358SJed Brown displs[p] = ranges[p]*ctx->dim; 246552f7358SJed Brown } 247552f7358SJed Brown ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr); 248552f7358SJed Brown } else { 249552f7358SJed Brown N = n; 250552f7358SJed Brown globalPoints = ctx->points; 25138ea73c8SJed Brown counts = displs = NULL; 25238ea73c8SJed Brown layout = NULL; 253552f7358SJed Brown } 254552f7358SJed Brown #if 0 255dcca6d9dSJed Brown ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 25619436ca2SJed Brown /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */ 257552f7358SJed Brown #else 258cb313848SJed Brown #if defined(PETSC_USE_COMPLEX) 25946b3086cSToby Isaac ierr = PetscMalloc1(N*ctx->dim,&globalPointsScalar);CHKERRQ(ierr); 26046b3086cSToby Isaac for (i=0; i<N*ctx->dim; i++) globalPointsScalar[i] = globalPoints[i]; 261cb313848SJed Brown #else 262cb313848SJed Brown globalPointsScalar = globalPoints; 263cb313848SJed Brown #endif 26404706141SMatthew G Knepley ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr); 265dcca6d9dSJed Brown ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr); 2667b5f2079SMatthew G. Knepley for (p = 0; p < N; ++p) {foundProcs[p] = size;} 2673a93e3b7SToby Isaac cellSF = NULL; 2682d1fa6caSMatthew G. Knepley ierr = DMLocatePoints(dm, pointVec, DM_POINTLOCATION_REMOVE, &cellSF);CHKERRQ(ierr); 2693a93e3b7SToby Isaac ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr); 270552f7358SJed Brown #endif 2713a93e3b7SToby Isaac for (p = 0; p < numFound; ++p) { 2723a93e3b7SToby Isaac if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank; 273552f7358SJed Brown } 274552f7358SJed Brown /* Let the lowest rank process own each point */ 275b2566f29SBarry Smith ierr = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr); 276552f7358SJed Brown ctx->n = 0; 277552f7358SJed Brown for (p = 0; p < N; ++p) { 278e5b268a4SToby Isaac if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, (double)globalPoints[p*ctx->dim+0], (double)(ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0), (double)(ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0)); 2791aa26658SKarl Rupp else if (globalProcs[p] == rank) ctx->n++; 280552f7358SJed Brown } 281552f7358SJed Brown /* Create coordinates vector and array of owned cells */ 282785e854fSJed Brown ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr); 283552f7358SJed Brown ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr); 284552f7358SJed Brown ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr); 285552f7358SJed Brown ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr); 286c0dedaeaSBarry Smith ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr); 287552f7358SJed Brown ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr); 288552f7358SJed Brown for (p = 0, q = 0, i = 0; p < N; ++p) { 289552f7358SJed Brown if (globalProcs[p] == rank) { 290552f7358SJed Brown PetscInt d; 291552f7358SJed Brown 2921aa26658SKarl Rupp for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d]; 293f317b747SMatthew G. Knepley ctx->cells[q] = foundCells[q].index; 294f317b747SMatthew G. Knepley ++q; 295552f7358SJed Brown } 296552f7358SJed Brown } 297552f7358SJed Brown ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr); 298552f7358SJed Brown #if 0 299552f7358SJed Brown ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr); 300552f7358SJed Brown #else 301552f7358SJed Brown ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr); 3023a93e3b7SToby Isaac ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr); 303552f7358SJed Brown ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 304552f7358SJed Brown #endif 305cb313848SJed Brown if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);} 306d343d804SMatthew G. Knepley if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);} 307552f7358SJed Brown ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 308552f7358SJed Brown PetscFunctionReturn(0); 309552f7358SJed Brown } 310552f7358SJed Brown 3114267b1a3SMatthew G. Knepley /*@C 3124267b1a3SMatthew G. Knepley DMInterpolationGetCoordinates - Gets a Vec with the coordinates of each interpolation point 3134267b1a3SMatthew G. Knepley 3144267b1a3SMatthew G. Knepley Collective on ctx 3154267b1a3SMatthew G. Knepley 3164267b1a3SMatthew G. Knepley Input Parameter: 3174267b1a3SMatthew G. Knepley . ctx - the context 3184267b1a3SMatthew G. Knepley 3194267b1a3SMatthew G. Knepley Output Parameter: 3204267b1a3SMatthew G. Knepley . coordinates - the coordinates of interpolation points 3214267b1a3SMatthew G. Knepley 3224267b1a3SMatthew G. Knepley Note: The local vector entries correspond to interpolation points lying on this process, according to the associated DM. This is a borrowed vector that the user should not destroy. 3234267b1a3SMatthew G. Knepley 3244267b1a3SMatthew G. Knepley Level: intermediate 3254267b1a3SMatthew G. Knepley 3264267b1a3SMatthew G. Knepley .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 3274267b1a3SMatthew G. Knepley @*/ 3280adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates) 3290adebc6cSBarry Smith { 330552f7358SJed Brown PetscFunctionBegin; 331552f7358SJed Brown PetscValidPointer(coordinates, 2); 3320adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 333552f7358SJed Brown *coordinates = ctx->coords; 334552f7358SJed Brown PetscFunctionReturn(0); 335552f7358SJed Brown } 336552f7358SJed Brown 3374267b1a3SMatthew G. Knepley /*@C 3384267b1a3SMatthew G. Knepley DMInterpolationGetVector - Gets a Vec which can hold all the interpolated field values 3394267b1a3SMatthew G. Knepley 3404267b1a3SMatthew G. Knepley Collective on ctx 3414267b1a3SMatthew G. Knepley 3424267b1a3SMatthew G. Knepley Input Parameter: 3434267b1a3SMatthew G. Knepley . ctx - the context 3444267b1a3SMatthew G. Knepley 3454267b1a3SMatthew G. Knepley Output Parameter: 3464267b1a3SMatthew G. Knepley . v - a vector capable of holding the interpolated field values 3474267b1a3SMatthew G. Knepley 3484267b1a3SMatthew G. Knepley Note: This vector should be returned using DMInterpolationRestoreVector(). 3494267b1a3SMatthew G. Knepley 3504267b1a3SMatthew G. Knepley Level: intermediate 3514267b1a3SMatthew G. Knepley 3524267b1a3SMatthew G. Knepley .seealso: DMInterpolationRestoreVector(), DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 3534267b1a3SMatthew G. Knepley @*/ 3540adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v) 3550adebc6cSBarry Smith { 356552f7358SJed Brown PetscErrorCode ierr; 357552f7358SJed Brown 358552f7358SJed Brown PetscFunctionBegin; 359552f7358SJed Brown PetscValidPointer(v, 2); 3600adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 361552f7358SJed Brown ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr); 362552f7358SJed Brown ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr); 363552f7358SJed Brown ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr); 364c0dedaeaSBarry Smith ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr); 365552f7358SJed Brown PetscFunctionReturn(0); 366552f7358SJed Brown } 367552f7358SJed Brown 3684267b1a3SMatthew G. Knepley /*@C 3694267b1a3SMatthew G. Knepley DMInterpolationRestoreVector - Returns a Vec which can hold all the interpolated field values 3704267b1a3SMatthew G. Knepley 3714267b1a3SMatthew G. Knepley Collective on ctx 3724267b1a3SMatthew G. Knepley 3734267b1a3SMatthew G. Knepley Input Parameters: 3744267b1a3SMatthew G. Knepley + ctx - the context 3754267b1a3SMatthew G. Knepley - v - a vector capable of holding the interpolated field values 3764267b1a3SMatthew G. Knepley 3774267b1a3SMatthew G. Knepley Level: intermediate 3784267b1a3SMatthew G. Knepley 3794267b1a3SMatthew G. Knepley .seealso: DMInterpolationGetVector(), DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 3804267b1a3SMatthew G. Knepley @*/ 3810adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v) 3820adebc6cSBarry Smith { 383552f7358SJed Brown PetscErrorCode ierr; 384552f7358SJed Brown 385552f7358SJed Brown PetscFunctionBegin; 386552f7358SJed Brown PetscValidPointer(v, 2); 3870adebc6cSBarry Smith if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup."); 388552f7358SJed Brown ierr = VecDestroy(v);CHKERRQ(ierr); 389552f7358SJed Brown PetscFunctionReturn(0); 390552f7358SJed Brown } 391552f7358SJed Brown 3927a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 393a6dfd86eSKarl Rupp { 394552f7358SJed Brown PetscReal *v0, *J, *invJ, detJ; 39556044e6dSMatthew G. Knepley const PetscScalar *coords; 39656044e6dSMatthew G. Knepley PetscScalar *a; 397552f7358SJed Brown PetscInt p; 398552f7358SJed Brown PetscErrorCode ierr; 399552f7358SJed Brown 400552f7358SJed Brown PetscFunctionBegin; 401dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 40256044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 403552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 404552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 405552f7358SJed Brown PetscInt c = ctx->cells[p]; 406a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 407552f7358SJed Brown PetscReal xi[4]; 408552f7358SJed Brown PetscInt d, f, comp; 409552f7358SJed Brown 4108e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 411e5b268a4SToby Isaac if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c); 4120298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 4131aa26658SKarl Rupp for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 4141aa26658SKarl Rupp 415552f7358SJed Brown for (d = 0; d < ctx->dim; ++d) { 416552f7358SJed Brown xi[d] = 0.0; 4171aa26658SKarl 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]); 4181aa26658SKarl 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]; 419552f7358SJed Brown } 4200298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 421552f7358SJed Brown } 422552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 42356044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 424552f7358SJed Brown ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 425552f7358SJed Brown PetscFunctionReturn(0); 426552f7358SJed Brown } 427552f7358SJed Brown 4287a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 4297a1931ceSMatthew G. Knepley { 4307a1931ceSMatthew G. Knepley PetscReal *v0, *J, *invJ, detJ; 43156044e6dSMatthew G. Knepley const PetscScalar *coords; 43256044e6dSMatthew G. Knepley PetscScalar *a; 4337a1931ceSMatthew G. Knepley PetscInt p; 4347a1931ceSMatthew G. Knepley PetscErrorCode ierr; 4357a1931ceSMatthew G. Knepley 4367a1931ceSMatthew G. Knepley PetscFunctionBegin; 437dcca6d9dSJed Brown ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr); 43856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 4397a1931ceSMatthew G. Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 4407a1931ceSMatthew G. Knepley for (p = 0; p < ctx->n; ++p) { 4417a1931ceSMatthew G. Knepley PetscInt c = ctx->cells[p]; 4427a1931ceSMatthew G. Knepley const PetscInt order[3] = {2, 1, 3}; 4432584bbe8SMatthew G. Knepley PetscScalar *x = NULL; 4447a1931ceSMatthew G. Knepley PetscReal xi[4]; 4457a1931ceSMatthew G. Knepley PetscInt d, f, comp; 4467a1931ceSMatthew G. Knepley 4478e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 448e5b268a4SToby Isaac if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c); 4497a1931ceSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 4507a1931ceSMatthew G. Knepley for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]; 4517a1931ceSMatthew G. Knepley 4527a1931ceSMatthew G. Knepley for (d = 0; d < ctx->dim; ++d) { 4537a1931ceSMatthew G. Knepley xi[d] = 0.0; 4547a1931ceSMatthew 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]); 4557a1931ceSMatthew 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]; 4567a1931ceSMatthew G. Knepley } 4577a1931ceSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr); 4587a1931ceSMatthew G. Knepley } 4597a1931ceSMatthew G. Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 46056044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 4617a1931ceSMatthew G. Knepley ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr); 4627a1931ceSMatthew G. Knepley PetscFunctionReturn(0); 4637a1931ceSMatthew G. Knepley } 4647a1931ceSMatthew G. Knepley 4655820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 466552f7358SJed Brown { 467552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 468552f7358SJed Brown const PetscScalar x0 = vertices[0]; 469552f7358SJed Brown const PetscScalar y0 = vertices[1]; 470552f7358SJed Brown const PetscScalar x1 = vertices[2]; 471552f7358SJed Brown const PetscScalar y1 = vertices[3]; 472552f7358SJed Brown const PetscScalar x2 = vertices[4]; 473552f7358SJed Brown const PetscScalar y2 = vertices[5]; 474552f7358SJed Brown const PetscScalar x3 = vertices[6]; 475552f7358SJed Brown const PetscScalar y3 = vertices[7]; 476552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 477552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 478552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 479552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 480552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 481552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 48256044e6dSMatthew G. Knepley const PetscScalar *ref; 48356044e6dSMatthew G. Knepley PetscScalar *real; 484552f7358SJed Brown PetscErrorCode ierr; 485552f7358SJed Brown 486552f7358SJed Brown PetscFunctionBegin; 48756044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 488552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 489552f7358SJed Brown { 490552f7358SJed Brown const PetscScalar p0 = ref[0]; 491552f7358SJed Brown const PetscScalar p1 = ref[1]; 492552f7358SJed Brown 493552f7358SJed Brown real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1; 494552f7358SJed Brown real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1; 495552f7358SJed Brown } 496552f7358SJed Brown ierr = PetscLogFlops(28);CHKERRQ(ierr); 49756044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 498552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 499552f7358SJed Brown PetscFunctionReturn(0); 500552f7358SJed Brown } 501552f7358SJed Brown 502af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 503d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 504552f7358SJed Brown { 505552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 506552f7358SJed Brown const PetscScalar x0 = vertices[0]; 507552f7358SJed Brown const PetscScalar y0 = vertices[1]; 508552f7358SJed Brown const PetscScalar x1 = vertices[2]; 509552f7358SJed Brown const PetscScalar y1 = vertices[3]; 510552f7358SJed Brown const PetscScalar x2 = vertices[4]; 511552f7358SJed Brown const PetscScalar y2 = vertices[5]; 512552f7358SJed Brown const PetscScalar x3 = vertices[6]; 513552f7358SJed Brown const PetscScalar y3 = vertices[7]; 514552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 515552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 51656044e6dSMatthew G. Knepley const PetscScalar *ref; 517552f7358SJed Brown PetscErrorCode ierr; 518552f7358SJed Brown 519552f7358SJed Brown PetscFunctionBegin; 52056044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 521552f7358SJed Brown { 522552f7358SJed Brown const PetscScalar x = ref[0]; 523552f7358SJed Brown const PetscScalar y = ref[1]; 524552f7358SJed Brown const PetscInt rows[2] = {0, 1}; 525da80777bSKarl Rupp PetscScalar values[4]; 526da80777bSKarl Rupp 527da80777bSKarl Rupp values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5; 528da80777bSKarl Rupp values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5; 52994ab13aaSBarry Smith ierr = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr); 530552f7358SJed Brown } 531552f7358SJed Brown ierr = PetscLogFlops(30);CHKERRQ(ierr); 53256044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 53394ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 53494ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 535552f7358SJed Brown PetscFunctionReturn(0); 536552f7358SJed Brown } 537552f7358SJed Brown 538a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 539a6dfd86eSKarl Rupp { 540fafc0619SMatthew G Knepley DM dmCoord; 541de73a395SMatthew G. Knepley PetscFE fem = NULL; 542552f7358SJed Brown SNES snes; 543552f7358SJed Brown KSP ksp; 544552f7358SJed Brown PC pc; 545552f7358SJed Brown Vec coordsLocal, r, ref, real; 546552f7358SJed Brown Mat J; 54756044e6dSMatthew G. Knepley const PetscScalar *coords; 54856044e6dSMatthew G. Knepley PetscScalar *a; 549de73a395SMatthew G. Knepley PetscInt Nf, p; 5505509d985SMatthew G. Knepley const PetscInt dof = ctx->dof; 551552f7358SJed Brown PetscErrorCode ierr; 552552f7358SJed Brown 553552f7358SJed Brown PetscFunctionBegin; 554de73a395SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 55544a7f3ddSMatthew G. Knepley if (Nf) {ierr = DMGetField(dm, 0, NULL, (PetscObject *) &fem);CHKERRQ(ierr);} 556552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 557fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 558552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 559552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 560552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 561552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 562c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 563552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 564552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 565552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 566552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 567552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 568552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 5690298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 5700298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 571552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 572552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 573552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 574552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 575552f7358SJed Brown 57656044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 577552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 578552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 579a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 580552f7358SJed Brown PetscScalar *xi; 581cb313848SJed Brown PetscReal xir[2]; 582552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 583552f7358SJed Brown 584552f7358SJed Brown /* Can make this do all points at once */ 5850298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 5860adebc6cSBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2); 5870298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 5880298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 5890298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 590552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 591552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 592552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 593552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 594552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 595552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 596cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 597cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 5985509d985SMatthew G. Knepley if (4*dof != xSize) { 5995509d985SMatthew G. Knepley PetscReal *B; 6005509d985SMatthew G. Knepley PetscInt d; 6011aa26658SKarl Rupp 6025509d985SMatthew G. Knepley xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0; 6035509d985SMatthew G. Knepley ierr = PetscFEGetTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr); 6045509d985SMatthew G. Knepley for (comp = 0; comp < dof; ++comp) { 6055509d985SMatthew G. Knepley a[p*dof+comp] = 0.0; 6065509d985SMatthew G. Knepley for (d = 0; d < xSize/dof; ++d) { 6075509d985SMatthew G. Knepley a[p*dof+comp] += x[d*dof+comp]*B[d*dof+comp]; 6085509d985SMatthew G. Knepley } 6095509d985SMatthew G. Knepley } 6105509d985SMatthew G. Knepley ierr = PetscFERestoreTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr); 6115509d985SMatthew G. Knepley } else { 6125509d985SMatthew G. Knepley for (comp = 0; comp < dof; ++comp) 6135509d985SMatthew G. Knepley a[p*dof+comp] = x[0*dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*dof+comp]*xir[0]*(1 - xir[1]) + x[2*dof+comp]*xir[0]*xir[1] + x[3*dof+comp]*(1 - xir[0])*xir[1]; 6145509d985SMatthew G. Knepley } 615552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 6160298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 6170298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 618552f7358SJed Brown } 619552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 62056044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 621552f7358SJed Brown 622552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 623552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 624552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 625552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 626552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 627552f7358SJed Brown PetscFunctionReturn(0); 628552f7358SJed Brown } 629552f7358SJed Brown 6305820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 631552f7358SJed Brown { 632552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 633552f7358SJed Brown const PetscScalar x0 = vertices[0]; 634552f7358SJed Brown const PetscScalar y0 = vertices[1]; 635552f7358SJed Brown const PetscScalar z0 = vertices[2]; 6367a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 6377a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 6387a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 639552f7358SJed Brown const PetscScalar x2 = vertices[6]; 640552f7358SJed Brown const PetscScalar y2 = vertices[7]; 641552f7358SJed Brown const PetscScalar z2 = vertices[8]; 6427a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 6437a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 6447a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 645552f7358SJed Brown const PetscScalar x4 = vertices[12]; 646552f7358SJed Brown const PetscScalar y4 = vertices[13]; 647552f7358SJed Brown const PetscScalar z4 = vertices[14]; 648552f7358SJed Brown const PetscScalar x5 = vertices[15]; 649552f7358SJed Brown const PetscScalar y5 = vertices[16]; 650552f7358SJed Brown const PetscScalar z5 = vertices[17]; 651552f7358SJed Brown const PetscScalar x6 = vertices[18]; 652552f7358SJed Brown const PetscScalar y6 = vertices[19]; 653552f7358SJed Brown const PetscScalar z6 = vertices[20]; 654552f7358SJed Brown const PetscScalar x7 = vertices[21]; 655552f7358SJed Brown const PetscScalar y7 = vertices[22]; 656552f7358SJed Brown const PetscScalar z7 = vertices[23]; 657552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 658552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 659552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 660552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 661552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 662552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 663552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 664552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 665552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 666552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 667552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 668552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 669552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 670552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 671552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 672552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 673552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 674552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 675552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 676552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 677552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 67856044e6dSMatthew G. Knepley const PetscScalar *ref; 67956044e6dSMatthew G. Knepley PetscScalar *real; 680552f7358SJed Brown PetscErrorCode ierr; 681552f7358SJed Brown 682552f7358SJed Brown PetscFunctionBegin; 68356044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 684552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 685552f7358SJed Brown { 686552f7358SJed Brown const PetscScalar p0 = ref[0]; 687552f7358SJed Brown const PetscScalar p1 = ref[1]; 688552f7358SJed Brown const PetscScalar p2 = ref[2]; 689552f7358SJed Brown 690552f7358SJed 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; 691552f7358SJed 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; 692552f7358SJed 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; 693552f7358SJed Brown } 694552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 69556044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 696552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 697552f7358SJed Brown PetscFunctionReturn(0); 698552f7358SJed Brown } 699552f7358SJed Brown 700d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 701552f7358SJed Brown { 702552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 703552f7358SJed Brown const PetscScalar x0 = vertices[0]; 704552f7358SJed Brown const PetscScalar y0 = vertices[1]; 705552f7358SJed Brown const PetscScalar z0 = vertices[2]; 7067a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 7077a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 7087a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 709552f7358SJed Brown const PetscScalar x2 = vertices[6]; 710552f7358SJed Brown const PetscScalar y2 = vertices[7]; 711552f7358SJed Brown const PetscScalar z2 = vertices[8]; 7127a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 7137a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 7147a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 715552f7358SJed Brown const PetscScalar x4 = vertices[12]; 716552f7358SJed Brown const PetscScalar y4 = vertices[13]; 717552f7358SJed Brown const PetscScalar z4 = vertices[14]; 718552f7358SJed Brown const PetscScalar x5 = vertices[15]; 719552f7358SJed Brown const PetscScalar y5 = vertices[16]; 720552f7358SJed Brown const PetscScalar z5 = vertices[17]; 721552f7358SJed Brown const PetscScalar x6 = vertices[18]; 722552f7358SJed Brown const PetscScalar y6 = vertices[19]; 723552f7358SJed Brown const PetscScalar z6 = vertices[20]; 724552f7358SJed Brown const PetscScalar x7 = vertices[21]; 725552f7358SJed Brown const PetscScalar y7 = vertices[22]; 726552f7358SJed Brown const PetscScalar z7 = vertices[23]; 727552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 728552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 729552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 730552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 731552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 732552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 733552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 734552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 735552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 736552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 737552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 738552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 73956044e6dSMatthew G. Knepley const PetscScalar *ref; 740552f7358SJed Brown PetscErrorCode ierr; 741552f7358SJed Brown 742552f7358SJed Brown PetscFunctionBegin; 74356044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 744552f7358SJed Brown { 745552f7358SJed Brown const PetscScalar x = ref[0]; 746552f7358SJed Brown const PetscScalar y = ref[1]; 747552f7358SJed Brown const PetscScalar z = ref[2]; 748552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 749da80777bSKarl Rupp PetscScalar values[9]; 750da80777bSKarl Rupp 751da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 752da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 753da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 754da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 755da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 756da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 757da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 758da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 759da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 7601aa26658SKarl Rupp 76194ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 762552f7358SJed Brown } 763552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 76456044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 76594ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 76694ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 767552f7358SJed Brown PetscFunctionReturn(0); 768552f7358SJed Brown } 769552f7358SJed Brown 770a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 771a6dfd86eSKarl Rupp { 772fafc0619SMatthew G Knepley DM dmCoord; 773552f7358SJed Brown SNES snes; 774552f7358SJed Brown KSP ksp; 775552f7358SJed Brown PC pc; 776552f7358SJed Brown Vec coordsLocal, r, ref, real; 777552f7358SJed Brown Mat J; 77856044e6dSMatthew G. Knepley const PetscScalar *coords; 77956044e6dSMatthew G. Knepley PetscScalar *a; 780552f7358SJed Brown PetscInt p; 781552f7358SJed Brown PetscErrorCode ierr; 782552f7358SJed Brown 783552f7358SJed Brown PetscFunctionBegin; 784552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 785fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 786552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 787552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 788552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 789552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 790c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 791552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 792552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 793552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 794552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 795552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 796552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 7970298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 7980298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 799552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 800552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 801552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 802552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 803552f7358SJed Brown 80456044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 805552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 806552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 807a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 808552f7358SJed Brown PetscScalar *xi; 809cb313848SJed Brown PetscReal xir[3]; 810552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 811552f7358SJed Brown 812552f7358SJed Brown /* Can make this do all points at once */ 8130298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 8140adebc6cSBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3); 8150298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 8160adebc6cSBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof); 8170298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 8180298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 819552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 820552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 821552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 822552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 823552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 824552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 825552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 826cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 827cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 828cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 829552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 830552f7358SJed Brown a[p*ctx->dof+comp] = 831cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 8327a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 833cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 8347a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 835cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 836cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 837cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 838cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 839552f7358SJed Brown } 840552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 8410298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 8420298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 843552f7358SJed Brown } 844552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 84556044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 846552f7358SJed Brown 847552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 848552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 849552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 850552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 851552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 852552f7358SJed Brown PetscFunctionReturn(0); 853552f7358SJed Brown } 854552f7358SJed Brown 8554267b1a3SMatthew G. Knepley /*@C 8564267b1a3SMatthew G. Knepley DMInterpolationEvaluate - Using the input from dm and x, calculates interpolated field values at the interpolation points. 8574267b1a3SMatthew G. Knepley 858552f7358SJed Brown Input Parameters: 859552f7358SJed Brown + ctx - The DMInterpolationInfo context 860552f7358SJed Brown . dm - The DM 861552f7358SJed Brown - x - The local vector containing the field to be interpolated 862552f7358SJed Brown 863552f7358SJed Brown Output Parameters: 864552f7358SJed Brown . v - The vector containing the interpolated values 8654267b1a3SMatthew G. Knepley 8664267b1a3SMatthew G. Knepley Note: A suitable v can be obtained using DMInterpolationGetVector(). 8674267b1a3SMatthew G. Knepley 8684267b1a3SMatthew G. Knepley Level: beginner 8694267b1a3SMatthew G. Knepley 8704267b1a3SMatthew G. Knepley .seealso: DMInterpolationGetVector(), DMInterpolationAddPoints(), DMInterpolationCreate() 8714267b1a3SMatthew G. Knepley @*/ 8720adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 8730adebc6cSBarry Smith { 874552f7358SJed Brown PetscInt dim, coneSize, n; 875552f7358SJed Brown PetscErrorCode ierr; 876552f7358SJed Brown 877552f7358SJed Brown PetscFunctionBegin; 878552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 879552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 880552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 881552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 8820adebc6cSBarry 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); 883552f7358SJed Brown if (n) { 884c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 885552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 886552f7358SJed Brown if (dim == 2) { 887552f7358SJed Brown if (coneSize == 3) { 8887a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 889552f7358SJed Brown } else if (coneSize == 4) { 890552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 8910adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 892552f7358SJed Brown } else if (dim == 3) { 893552f7358SJed Brown if (coneSize == 4) { 8947a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 895552f7358SJed Brown } else { 896552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 897552f7358SJed Brown } 8980adebc6cSBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim); 899552f7358SJed Brown } 900552f7358SJed Brown PetscFunctionReturn(0); 901552f7358SJed Brown } 902552f7358SJed Brown 9034267b1a3SMatthew G. Knepley /*@C 9044267b1a3SMatthew G. Knepley DMInterpolationDestroy - Destroys a DMInterpolationInfo context 9054267b1a3SMatthew G. Knepley 9064267b1a3SMatthew G. Knepley Collective on ctx 9074267b1a3SMatthew G. Knepley 9084267b1a3SMatthew G. Knepley Input Parameter: 9094267b1a3SMatthew G. Knepley . ctx - the context 9104267b1a3SMatthew G. Knepley 9114267b1a3SMatthew G. Knepley Level: beginner 9124267b1a3SMatthew G. Knepley 9134267b1a3SMatthew G. Knepley .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 9144267b1a3SMatthew G. Knepley @*/ 9150adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 9160adebc6cSBarry Smith { 917552f7358SJed Brown PetscErrorCode ierr; 918552f7358SJed Brown 919552f7358SJed Brown PetscFunctionBegin; 920552f7358SJed Brown PetscValidPointer(ctx, 2); 921552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 922552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 923552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 924552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 9250298fd71SBarry Smith *ctx = NULL; 926552f7358SJed Brown PetscFunctionReturn(0); 927552f7358SJed Brown } 928cc0c4584SMatthew G. Knepley 929cc0c4584SMatthew G. Knepley /*@C 930cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 931cc0c4584SMatthew G. Knepley 932cc0c4584SMatthew G. Knepley Collective on SNES 933cc0c4584SMatthew G. Knepley 934cc0c4584SMatthew G. Knepley Input Parameters: 935cc0c4584SMatthew G. Knepley + snes - the SNES context 936cc0c4584SMatthew G. Knepley . its - iteration number 937cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 938d43b4f6eSBarry Smith - vf - PetscViewerAndFormat of type ASCII 939cc0c4584SMatthew G. Knepley 940cc0c4584SMatthew G. Knepley Notes: 941cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 942cc0c4584SMatthew G. Knepley 943cc0c4584SMatthew G. Knepley Level: intermediate 944cc0c4584SMatthew G. Knepley 945cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm 946cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault() 947cc0c4584SMatthew G. Knepley @*/ 948d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf) 949cc0c4584SMatthew G. Knepley { 950d43b4f6eSBarry Smith PetscViewer viewer = vf->viewer; 951cc0c4584SMatthew G. Knepley Vec res; 952cc0c4584SMatthew G. Knepley DM dm; 953cc0c4584SMatthew G. Knepley PetscSection s; 954cc0c4584SMatthew G. Knepley const PetscScalar *r; 955cc0c4584SMatthew G. Knepley PetscReal *lnorms, *norms; 956cc0c4584SMatthew G. Knepley PetscInt numFields, f, pStart, pEnd, p; 957cc0c4584SMatthew G. Knepley PetscErrorCode ierr; 958cc0c4584SMatthew G. Knepley 959cc0c4584SMatthew G. Knepley PetscFunctionBegin; 9604d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 961cc0c4584SMatthew G. Knepley ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr); 962cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 963e87a4003SBarry Smith ierr = DMGetSection(dm, &s);CHKERRQ(ierr); 964cc0c4584SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr); 965cc0c4584SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 966cc0c4584SMatthew G. Knepley ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr); 967cc0c4584SMatthew G. Knepley ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr); 968cc0c4584SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 969cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 970cc0c4584SMatthew G. Knepley PetscInt fdof, foff, d; 971cc0c4584SMatthew G. Knepley 972cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr); 973cc0c4584SMatthew G. Knepley ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr); 974cc0c4584SMatthew G. Knepley for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d])); 975cc0c4584SMatthew G. Knepley } 976cc0c4584SMatthew G. Knepley } 977cc0c4584SMatthew G. Knepley ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr); 978b2566f29SBarry Smith ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 979d43b4f6eSBarry Smith ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr); 980cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 981cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr); 982cc0c4584SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 983cc0c4584SMatthew G. Knepley if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);} 984cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr); 985cc0c4584SMatthew G. Knepley } 986cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr); 987cc0c4584SMatthew G. Knepley ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr); 988d43b4f6eSBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 989cc0c4584SMatthew G. Knepley ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr); 990cc0c4584SMatthew G. Knepley PetscFunctionReturn(0); 991cc0c4584SMatthew G. Knepley } 99224cdb843SMatthew G. Knepley 99324cdb843SMatthew G. Knepley /********************* Residual Computation **************************/ 99424cdb843SMatthew G. Knepley 9957d4028c8SMatthew G. Knepley 99608449791SMatthew G. Knepley /*@ 99708449791SMatthew G. Knepley DMPlexSNESGetGeometryFVM - Return precomputed geometric data 99808449791SMatthew G. Knepley 99908449791SMatthew G. Knepley Input Parameter: 100008449791SMatthew G. Knepley . dm - The DM 100108449791SMatthew G. Knepley 100208449791SMatthew G. Knepley Output Parameters: 100308449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry 100408449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 100508449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 100608449791SMatthew G. Knepley 100708449791SMatthew G. Knepley Level: developer 100808449791SMatthew G. Knepley 100908449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 101008449791SMatthew G. Knepley @*/ 101108449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 101224cdb843SMatthew G. Knepley { 10134b32e5bbSToby Isaac DM plex; 101424cdb843SMatthew G. Knepley PetscErrorCode ierr; 101524cdb843SMatthew G. Knepley 101624cdb843SMatthew G. Knepley PetscFunctionBegin; 101708449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 10184b32e5bbSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 10194b32e5bbSToby Isaac ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr); 10204b32e5bbSToby Isaac if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);} 10214b32e5bbSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 102224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 102324cdb843SMatthew G. Knepley } 102424cdb843SMatthew G. Knepley 1025dbd489d2SMatthew G. Knepley /*@ 102608449791SMatthew G. Knepley DMPlexSNESGetGradientDM - Return gradient data layout 102708449791SMatthew G. Knepley 102808449791SMatthew G. Knepley Input Parameters: 102908449791SMatthew G. Knepley + dm - The DM 103008449791SMatthew G. Knepley - fv - The PetscFV 103108449791SMatthew G. Knepley 103208449791SMatthew G. Knepley Output Parameter: 103308449791SMatthew G. Knepley . dmGrad - The layout for gradient values 103408449791SMatthew G. Knepley 103508449791SMatthew G. Knepley Level: developer 103608449791SMatthew G. Knepley 103708449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 103808449791SMatthew G. Knepley @*/ 103908449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 104024cdb843SMatthew G. Knepley { 10414b32e5bbSToby Isaac DM plex; 104208449791SMatthew G. Knepley PetscBool computeGradients; 104324cdb843SMatthew G. Knepley PetscErrorCode ierr; 104424cdb843SMatthew G. Knepley 104524cdb843SMatthew G. Knepley PetscFunctionBegin; 104608449791SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 104708449791SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 104808449791SMatthew G. Knepley PetscValidPointer(dmGrad,3); 104908449791SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 105008449791SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 10514b32e5bbSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 10524b32e5bbSToby Isaac ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr); 10534b32e5bbSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 105408449791SMatthew G. Knepley PetscFunctionReturn(0); 105508449791SMatthew G. Knepley } 105608449791SMatthew G. Knepley 105708449791SMatthew G. Knepley /*@C 105808449791SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 105908449791SMatthew G. Knepley 106008449791SMatthew G. Knepley Input Parameters: 106108449791SMatthew G. Knepley + dm - The DM 10629044fa66SMatthew G. Knepley . cellIS - The cells to include 106308449791SMatthew G. Knepley . locX - A local vector with the solution fields 106408449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 106508449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 106608449791SMatthew G. Knepley 106708449791SMatthew G. Knepley Output Parameters: 106808449791SMatthew G. Knepley + u - The field coefficients 106908449791SMatthew G. Knepley . u_t - The fields derivative coefficients 107008449791SMatthew G. Knepley - a - The auxiliary field coefficients 107108449791SMatthew G. Knepley 107208449791SMatthew G. Knepley Level: developer 107308449791SMatthew G. Knepley 107408449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 107508449791SMatthew G. Knepley @*/ 10769044fa66SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 107708449791SMatthew G. Knepley { 10789044fa66SMatthew G. Knepley DM plex, plexA = NULL; 107908449791SMatthew G. Knepley PetscSection section, sectionAux; 108008449791SMatthew G. Knepley PetscDS prob; 10819044fa66SMatthew G. Knepley const PetscInt *cells; 10829044fa66SMatthew G. Knepley PetscInt cStart, cEnd, numCells, totDim, totDimAux, c; 108308449791SMatthew G. Knepley PetscErrorCode ierr; 108408449791SMatthew G. Knepley 108508449791SMatthew G. Knepley PetscFunctionBegin; 108608449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 108708449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 108808449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 108908449791SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 109008449791SMatthew G. Knepley PetscValidPointer(u, 7); 109108449791SMatthew G. Knepley PetscValidPointer(u_t, 8); 109208449791SMatthew G. Knepley PetscValidPointer(a, 9); 10939044fa66SMatthew G. Knepley ierr = DMSNESConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr); 10949044fa66SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1095e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1096e5e52638SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 109724cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 109808449791SMatthew G. Knepley if (locA) { 10999044fa66SMatthew G. Knepley DM dmAux; 110008449791SMatthew G. Knepley PetscDS probAux; 110108449791SMatthew G. Knepley 110208449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 11039044fa66SMatthew G. Knepley ierr = DMSNESConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr); 1104e87a4003SBarry Smith ierr = DMGetSection(dmAux, §ionAux);CHKERRQ(ierr); 110524cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 110624cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 110724cdb843SMatthew G. Knepley } 11089044fa66SMatthew G. Knepley numCells = cEnd - cStart; 110969291d52SBarry Smith ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr); 111069291d52SBarry Smith if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 111169291d52SBarry Smith if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 111224cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 11139044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 11149044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 111508449791SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 111624cdb843SMatthew G. Knepley PetscInt i; 111724cdb843SMatthew G. Knepley 11189044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr); 11199044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i]; 11209044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr); 112108449791SMatthew G. Knepley if (locX_t) { 11229044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr); 11239044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i]; 11249044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr); 112524cdb843SMatthew G. Knepley } 112608449791SMatthew G. Knepley if (locA) { 1127*44171101SMatthew G. Knepley PetscInt subcell; 1128*44171101SMatthew G. Knepley ierr = DMPlexGetAuxiliaryPoint(plex, plexA, cell, &subcell);CHKERRQ(ierr); 1129*44171101SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr); 11309044fa66SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i]; 1131*44171101SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr); 113224cdb843SMatthew G. Knepley } 113324cdb843SMatthew G. Knepley } 11349044fa66SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 11359044fa66SMatthew G. Knepley if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 11369044fa66SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 113708449791SMatthew G. Knepley PetscFunctionReturn(0); 113808449791SMatthew G. Knepley } 113924cdb843SMatthew G. Knepley 114008449791SMatthew G. Knepley /*@C 114108449791SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 114208449791SMatthew G. Knepley 114308449791SMatthew G. Knepley Input Parameters: 114408449791SMatthew G. Knepley + dm - The DM 11459044fa66SMatthew G. Knepley . cellIS - The cells to include 114608449791SMatthew G. Knepley . locX - A local vector with the solution fields 114708449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 114808449791SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 114908449791SMatthew G. Knepley 115008449791SMatthew G. Knepley Output Parameters: 115108449791SMatthew G. Knepley + u - The field coefficients 115208449791SMatthew G. Knepley . u_t - The fields derivative coefficients 115308449791SMatthew G. Knepley - a - The auxiliary field coefficients 115408449791SMatthew G. Knepley 115508449791SMatthew G. Knepley Level: developer 115608449791SMatthew G. Knepley 115708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 115808449791SMatthew G. Knepley @*/ 11599044fa66SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 116008449791SMatthew G. Knepley { 116108449791SMatthew G. Knepley PetscErrorCode ierr; 116208449791SMatthew G. Knepley 116308449791SMatthew G. Knepley PetscFunctionBegin; 116469291d52SBarry Smith ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr); 1165040f60c8SVaclav Hapla if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);} 1166040f60c8SVaclav Hapla if (locA) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);} 116708449791SMatthew G. Knepley PetscFunctionReturn(0); 116824cdb843SMatthew G. Knepley } 116908449791SMatthew G. Knepley 117008449791SMatthew G. Knepley /*@C 117108449791SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 117208449791SMatthew G. Knepley 117308449791SMatthew G. Knepley Input Parameters: 117408449791SMatthew G. Knepley + dm - The DM 117508449791SMatthew G. Knepley . fStart - The first face to include 117608449791SMatthew G. Knepley . fEnd - The first face to exclude 117708449791SMatthew G. Knepley . locX - A local vector with the solution fields 117808449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 117908449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 118008449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 118108449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 118208449791SMatthew G. Knepley 118308449791SMatthew G. Knepley Output Parameters: 11845f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 11855f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face 1186477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 118708449791SMatthew G. Knepley 118808449791SMatthew G. Knepley Level: developer 118908449791SMatthew G. Knepley 119008449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 119108449791SMatthew G. Knepley @*/ 11925f942ad5SMatthew 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) 119308449791SMatthew G. Knepley { 119408449791SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 1195195142f5SMatthew G. Knepley PetscSection section; 119608449791SMatthew G. Knepley PetscDS prob; 119708449791SMatthew G. Knepley DMLabel ghostLabel; 119808449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 1199477f7dfdSMatthew G. Knepley PetscBool *isFE; 1200477f7dfdSMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 120108449791SMatthew G. Knepley PetscErrorCode ierr; 120208449791SMatthew G. Knepley 120308449791SMatthew G. Knepley PetscFunctionBegin; 120408449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 120508449791SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 120608449791SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 120708449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 120808449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 120908449791SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 121008449791SMatthew G. Knepley PetscValidPointer(uL, 9); 121108449791SMatthew G. Knepley PetscValidPointer(uR, 10); 121208449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 121308449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1214e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1215477f7dfdSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1216477f7dfdSMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 1217477f7dfdSMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 1218477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1219477f7dfdSMatthew G. Knepley PetscObject obj; 1220477f7dfdSMatthew G. Knepley PetscClassId id; 1221477f7dfdSMatthew G. Knepley 12224a270516SSander Arens ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1223477f7dfdSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1224477f7dfdSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 1225477f7dfdSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 1226477f7dfdSMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 1227477f7dfdSMatthew G. Knepley } 1228c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 122908449791SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 123008449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 123108449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 123208449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 123308449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 123408449791SMatthew G. Knepley if (locGrad) { 123508449791SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 123608449791SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 123724cdb843SMatthew G. Knepley } 123869291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr); 123969291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr); 1240477f7dfdSMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 124108449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 124208449791SMatthew G. Knepley const PetscInt *cells; 1243640bce14SSatish Balay PetscFVFaceGeom *fg; 1244640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 1245640bce14SSatish Balay PetscScalar *xL, *xR, *gL, *gR; 124608449791SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 12473e64cd2fSToby Isaac PetscInt ghost, nsupp, nchild; 124808449791SMatthew G. Knepley 124908449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1250e697831aSToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 12513e64cd2fSToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 12527c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 125308449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 125408449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 125508449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 125608449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 1257477f7dfdSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1258477f7dfdSMatthew G. Knepley PetscInt off; 1259477f7dfdSMatthew G. Knepley 126037a43ebbSMatthew G. Knepley ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 1261477f7dfdSMatthew G. Knepley if (isFE[f]) { 1262477f7dfdSMatthew G. Knepley const PetscInt *cone; 12633e64cd2fSToby Isaac PetscInt comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d; 1264477f7dfdSMatthew G. Knepley 1265cca9989dSMatthew G. Knepley xL = xR = NULL; 12667c45b140SToby Isaac ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 1267cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1268cca9989dSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1269477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 12703e64cd2fSToby Isaac ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr); 12713e64cd2fSToby Isaac for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break; 1272477f7dfdSMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 12733e64cd2fSToby Isaac ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr); 12743e64cd2fSToby Isaac for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break; 12753e64cd2fSToby 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]); 1276195142f5SMatthew G. Knepley /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 12773e64cd2fSToby Isaac /* TODO: this is a hack that might not be right for nonconforming */ 12783e64cd2fSToby Isaac if (faceLocL < coneSizeL) { 1279477f7dfdSMatthew G. Knepley ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 12803e64cd2fSToby Isaac if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 12817c45b140SToby Isaac else {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];} 12823e64cd2fSToby Isaac } 12833e64cd2fSToby Isaac else { 12843e64cd2fSToby Isaac ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr); 12853e64cd2fSToby Isaac ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 12863e64cd2fSToby Isaac for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d]; 12873e64cd2fSToby Isaac } 1288cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 1289cca9989dSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 1290477f7dfdSMatthew G. Knepley } else { 1291477f7dfdSMatthew G. Knepley PetscFV fv; 1292477f7dfdSMatthew G. Knepley PetscInt numComp, c; 1293477f7dfdSMatthew G. Knepley 1294477f7dfdSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 1295477f7dfdSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 1296cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 1297cca9989dSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 129808449791SMatthew G. Knepley if (dmGrad) { 129908449791SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 130008449791SMatthew G. Knepley 130108449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 130208449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 130308449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 130408449791SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 1305477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1306477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 1307477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 130808449791SMatthew G. Knepley } 130908449791SMatthew G. Knepley } else { 1310477f7dfdSMatthew G. Knepley for (c = 0; c < numComp; ++c) { 1311477f7dfdSMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 1312477f7dfdSMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 1313477f7dfdSMatthew G. Knepley } 1314477f7dfdSMatthew G. Knepley } 131508449791SMatthew G. Knepley } 131608449791SMatthew G. Knepley } 131708449791SMatthew G. Knepley ++iface; 131808449791SMatthew G. Knepley } 13195f942ad5SMatthew G. Knepley *Nface = iface; 132008449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 132108449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 132208449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 132308449791SMatthew G. Knepley if (locGrad) { 132408449791SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 132508449791SMatthew G. Knepley } 1326477f7dfdSMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 132708449791SMatthew G. Knepley PetscFunctionReturn(0); 132808449791SMatthew G. Knepley } 132908449791SMatthew G. Knepley 133008449791SMatthew G. Knepley /*@C 133108449791SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 133208449791SMatthew G. Knepley 133308449791SMatthew G. Knepley Input Parameters: 133408449791SMatthew G. Knepley + dm - The DM 133508449791SMatthew G. Knepley . fStart - The first face to include 133608449791SMatthew G. Knepley . fEnd - The first face to exclude 133708449791SMatthew G. Knepley . locX - A local vector with the solution fields 133808449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 133908449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 134008449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 134108449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 134208449791SMatthew G. Knepley 134308449791SMatthew G. Knepley Output Parameters: 13445f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 13455f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face 1346477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face 134708449791SMatthew G. Knepley 134808449791SMatthew G. Knepley Level: developer 134908449791SMatthew G. Knepley 135008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 135108449791SMatthew G. Knepley @*/ 13525f942ad5SMatthew 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) 135308449791SMatthew G. Knepley { 135408449791SMatthew G. Knepley PetscErrorCode ierr; 135508449791SMatthew G. Knepley 135608449791SMatthew G. Knepley PetscFunctionBegin; 135769291d52SBarry Smith ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr); 135869291d52SBarry Smith ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr); 135908449791SMatthew G. Knepley PetscFunctionReturn(0); 136008449791SMatthew G. Knepley } 136108449791SMatthew G. Knepley 136208449791SMatthew G. Knepley /*@C 136308449791SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 136408449791SMatthew G. Knepley 136508449791SMatthew G. Knepley Input Parameters: 136608449791SMatthew G. Knepley + dm - The DM 136708449791SMatthew G. Knepley . fStart - The first face to include 136808449791SMatthew G. Knepley . fEnd - The first face to exclude 136908449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 137008449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 137108449791SMatthew G. Knepley 137208449791SMatthew G. Knepley Output Parameters: 13735f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 13745f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal 137508449791SMatthew G. Knepley - vol - The cell volume 137608449791SMatthew G. Knepley 137708449791SMatthew G. Knepley Level: developer 137808449791SMatthew G. Knepley 137908449791SMatthew G. Knepley .seealso: DMPlexGetCellFields() 138008449791SMatthew G. Knepley @*/ 13815f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 138208449791SMatthew G. Knepley { 138308449791SMatthew G. Knepley DM dmFace, dmCell; 138408449791SMatthew G. Knepley DMLabel ghostLabel; 138508449791SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 138608449791SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 138708449791SMatthew G. Knepley PetscErrorCode ierr; 138808449791SMatthew G. Knepley 138908449791SMatthew G. Knepley PetscFunctionBegin; 139008449791SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 139108449791SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 139208449791SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 139308449791SMatthew G. Knepley PetscValidPointer(fgeom, 6); 139408449791SMatthew G. Knepley PetscValidPointer(vol, 7); 139508449791SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1396c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 139708449791SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 139808449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 139908449791SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 140008449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 140108449791SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 140269291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr); 140308449791SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 140408449791SMatthew G. Knepley const PetscInt *cells; 1405640bce14SSatish Balay PetscFVFaceGeom *fg; 1406640bce14SSatish Balay PetscFVCellGeom *cgL, *cgR; 140708449791SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 14082eefff9cSMatthew G. Knepley PetscReal *voll = *vol; 14097c45b140SToby Isaac PetscInt ghost, d, nchild, nsupp; 141008449791SMatthew G. Knepley 141108449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 14127c45b140SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 14137c45b140SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 14147c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 141508449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 141608449791SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 141708449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 141808449791SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 141908449791SMatthew G. Knepley for (d = 0; d < dim; ++d) { 142008449791SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 142108449791SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 142208449791SMatthew G. Knepley } 142308449791SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 142408449791SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 142508449791SMatthew G. Knepley ++iface; 142608449791SMatthew G. Knepley } 14275f942ad5SMatthew G. Knepley *Nface = iface; 142808449791SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 142908449791SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 143008449791SMatthew G. Knepley PetscFunctionReturn(0); 143108449791SMatthew G. Knepley } 143208449791SMatthew G. Knepley 143308449791SMatthew G. Knepley /*@C 143408449791SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 143508449791SMatthew G. Knepley 143608449791SMatthew G. Knepley Input Parameters: 143708449791SMatthew G. Knepley + dm - The DM 143808449791SMatthew G. Knepley . fStart - The first face to include 143908449791SMatthew G. Knepley . fEnd - The first face to exclude 144008449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry 144108449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 144208449791SMatthew G. Knepley 144308449791SMatthew G. Knepley Output Parameters: 14445f942ad5SMatthew G. Knepley + Nface - The number of faces with field values 14455f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal 144608449791SMatthew G. Knepley - vol - The cell volume 144708449791SMatthew G. Knepley 144808449791SMatthew G. Knepley Level: developer 144908449791SMatthew G. Knepley 145008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 145108449791SMatthew G. Knepley @*/ 14525f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 145308449791SMatthew G. Knepley { 145408449791SMatthew G. Knepley PetscErrorCode ierr; 145508449791SMatthew G. Knepley 145608449791SMatthew G. Knepley PetscFunctionBegin; 145708449791SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 145869291d52SBarry Smith ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr); 145908449791SMatthew G. Knepley PetscFunctionReturn(0); 146008449791SMatthew G. Knepley } 146108449791SMatthew G. Knepley 1462c330f8ffSToby Isaac static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS) 146308449791SMatthew G. Knepley { 146408449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1465023fee6eSMatthew G. Knepley DM plex = NULL, plexA = NULL; 14664236e4b7SMatthew G. Knepley PetscDS prob, probAux = NULL; 14674236e4b7SMatthew G. Knepley PetscSection section, sectionAux = NULL; 14682d91c981SSander Arens Vec locA = NULL; 14692d91c981SSander Arens PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL; 14704236e4b7SMatthew G. Knepley PetscInt v; 1471c330f8ffSToby Isaac PetscInt totDim, totDimAux = 0; 147208449791SMatthew G. Knepley PetscErrorCode ierr; 147308449791SMatthew G. Knepley 147408449791SMatthew G. Knepley PetscFunctionBegin; 1475023fee6eSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 1476e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 147708449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 14784d0b9603SSander Arens ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 14792d91c981SSander Arens ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 14802d91c981SSander Arens if (locA) { 14814236e4b7SMatthew G. Knepley DM dmAux; 14824236e4b7SMatthew G. Knepley 14832d91c981SSander Arens ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1484023fee6eSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 1485023fee6eSMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 14864d0b9603SSander Arens ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1487cf0b7c11SKarl Rupp ierr = DMGetSection(plexA, §ionAux);CHKERRQ(ierr); 14882d91c981SSander Arens } 14894236e4b7SMatthew G. Knepley for (v = 0; v < numValues; ++v) { 149051e9edccSMatthew G. Knepley PetscFEGeom *fgeom; 1491b7260050SToby Isaac PetscInt maxDegree; 1492c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 149324cdb843SMatthew G. Knepley IS pointIS; 149424cdb843SMatthew G. Knepley const PetscInt *points; 1495f74ed4f0SToby Isaac PetscInt numFaces, face, Nq; 149624cdb843SMatthew G. Knepley 1497a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 149822734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 1499c330f8ffSToby Isaac { 1500c330f8ffSToby Isaac IS isectIS; 1501c330f8ffSToby Isaac 15024a3e9fdbSToby Isaac /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 150300199b93SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 1504c330f8ffSToby Isaac ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1505c330f8ffSToby Isaac pointIS = isectIS; 150624cdb843SMatthew G. Knepley } 1507c330f8ffSToby Isaac ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr); 1508c330f8ffSToby Isaac ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr); 1509c330f8ffSToby Isaac ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 1510b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 1511b7260050SToby Isaac if (maxDegree <= 1) { 1512c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 1513c330f8ffSToby Isaac } 1514c330f8ffSToby Isaac if (!qGeom) { 1515c330f8ffSToby Isaac PetscFE fe; 1516c330f8ffSToby Isaac 1517c330f8ffSToby Isaac ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1518c330f8ffSToby Isaac ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 1519c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 1520c330f8ffSToby Isaac } 1521c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 15224a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1523c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1524c330f8ffSToby Isaac const PetscInt point = points[face], *support, *cone; 152524cdb843SMatthew G. Knepley PetscScalar *x = NULL; 15269bfb2fe4SJed Brown PetscInt i, coneSize, faceLoc; 152724cdb843SMatthew G. Knepley 15284d0b9603SSander Arens ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 15294d0b9603SSander Arens ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 15304d0b9603SSander Arens ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 15314d0b9603SSander Arens for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 15324236e4b7SMatthew G. Knepley if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]); 1533c330f8ffSToby Isaac fgeom->face[face][0] = faceLoc; 153451e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 15354d0b9603SSander Arens for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 1536023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 153708449791SMatthew G. Knepley if (locX_t) { 1538023fee6eSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 15394d0b9603SSander Arens for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 1540023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 154124cdb843SMatthew G. Knepley } 15422d91c981SSander Arens if (locA) { 154306adec37SMatthew G. Knepley DMLabel spmap; 154406adec37SMatthew G. Knepley PetscInt subp = support[0]; 154506adec37SMatthew G. Knepley 154606adec37SMatthew G. Knepley /* If dm is a submesh, do not get subpoint */ 154706adec37SMatthew G. Knepley ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr); 154806adec37SMatthew G. Knepley if (!spmap) {ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);} 1549023fee6eSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 15504d0b9603SSander Arens for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 1551023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 155224cdb843SMatthew G. Knepley } 155324cdb843SMatthew G. Knepley } 15544d0b9603SSander Arens ierr = PetscMemzero(elemVec, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1555cbf52bb1SSander Arens { 155624cdb843SMatthew G. Knepley PetscFE fe; 1557f74ed4f0SToby Isaac PetscInt Nb; 1558c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 155924cdb843SMatthew G. Knepley /* Conforming batches */ 156024cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 156124cdb843SMatthew G. Knepley /* Remainder */ 156224cdb843SMatthew G. Knepley PetscInt Nr, offset; 156324cdb843SMatthew G. Knepley 15644d0b9603SSander Arens ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 156524cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 156624cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1567c330f8ffSToby Isaac /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */ 1568c330f8ffSToby Isaac blockSize = Nb; 156924cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 157024cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 157124cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 157224cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 157324cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 157424cdb843SMatthew G. Knepley offset = numFaces - Nr; 1575c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 1576c330f8ffSToby Isaac ierr = PetscFEIntegrateBdResidual(fe, prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 1577a57bfeafSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr); 1578c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 1579c330f8ffSToby Isaac ierr = PetscFEIntegrateBdResidual(fe, prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr); 1580c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 158124cdb843SMatthew G. Knepley } 1582c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1583c330f8ffSToby Isaac const PetscInt point = points[face], *support; 158424cdb843SMatthew G. Knepley 15854d0b9603SSander Arens if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);} 1586023fee6eSMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 1587023fee6eSMatthew G. Knepley ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 158824cdb843SMatthew G. Knepley } 15894a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1590c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 159124cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 159224cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1593c330f8ffSToby Isaac ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr); 159424cdb843SMatthew G. Knepley } 1595023fee6eSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 1596023fee6eSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 15974236e4b7SMatthew G. Knepley PetscFunctionReturn(0); 1598a8e83e26SSanderA } 15994236e4b7SMatthew G. Knepley 1600317e5c34SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF) 1601317e5c34SMatthew G. Knepley { 1602317e5c34SMatthew G. Knepley DMField coordField; 1603317e5c34SMatthew G. Knepley DMLabel depthLabel; 1604317e5c34SMatthew G. Knepley IS facetIS; 1605317e5c34SMatthew G. Knepley PetscInt dim; 1606317e5c34SMatthew G. Knepley PetscErrorCode ierr; 1607317e5c34SMatthew G. Knepley 1608317e5c34SMatthew G. Knepley PetscFunctionBegin; 1609317e5c34SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1610317e5c34SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1611317e5c34SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 1612317e5c34SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1613a79ed8ccSMatthew G. Knepley ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 1614317e5c34SMatthew G. Knepley PetscFunctionReturn(0); 1615317e5c34SMatthew G. Knepley } 1616317e5c34SMatthew G. Knepley 16174236e4b7SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 16184236e4b7SMatthew G. Knepley { 16194236e4b7SMatthew G. Knepley PetscDS prob; 162033badb77SMatthew G. Knepley PetscInt numBd, bd; 1621c330f8ffSToby Isaac DMField coordField = NULL; 162233badb77SMatthew G. Knepley IS facetIS = NULL; 1623e5e52638SMatthew G. Knepley DMLabel depthLabel; 1624e5e52638SMatthew G. Knepley PetscInt dim; 16254236e4b7SMatthew G. Knepley PetscErrorCode ierr; 16264236e4b7SMatthew G. Knepley 16274236e4b7SMatthew G. Knepley PetscFunctionBegin; 16284236e4b7SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1629e5e52638SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1630e5e52638SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1631e5e52638SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr); 16324236e4b7SMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 16334236e4b7SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 16344236e4b7SMatthew G. Knepley DMBoundaryConditionType type; 16354236e4b7SMatthew G. Knepley const char *bdLabel; 16364236e4b7SMatthew G. Knepley DMLabel label; 16374236e4b7SMatthew G. Knepley const PetscInt *values; 16384236e4b7SMatthew G. Knepley PetscInt field, numValues; 16394236e4b7SMatthew G. Knepley PetscObject obj; 16404236e4b7SMatthew G. Knepley PetscClassId id; 16414236e4b7SMatthew G. Knepley 16424236e4b7SMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 16434236e4b7SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 16444236e4b7SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 16454236e4b7SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 164633badb77SMatthew G. Knepley if (!facetIS) { 164733badb77SMatthew G. Knepley DMLabel depthLabel; 164833badb77SMatthew G. Knepley PetscInt dim; 164933badb77SMatthew G. Knepley 165033badb77SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 165133badb77SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 165233badb77SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr); 165333badb77SMatthew G. Knepley } 1654e5e52638SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 16554236e4b7SMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1656c330f8ffSToby Isaac ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 16574236e4b7SMatthew G. Knepley } 1658c330f8ffSToby Isaac ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 165908449791SMatthew G. Knepley PetscFunctionReturn(0); 166008449791SMatthew G. Knepley } 166108449791SMatthew G. Knepley 16624a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 166308449791SMatthew G. Knepley { 166408449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 166508449791SMatthew G. Knepley const char *name = "Residual"; 166608449791SMatthew G. Knepley DM dmAux = NULL; 166708449791SMatthew G. Knepley DM dmGrad = NULL; 166808449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 166908449791SMatthew G. Knepley PetscDS prob = NULL; 167008449791SMatthew G. Knepley PetscDS probAux = NULL; 167108449791SMatthew G. Knepley PetscSection section = NULL; 167208449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 167308449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 1674b2666ceaSMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 167508449791SMatthew G. Knepley PetscFV fvm = NULL; 167608449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 167708449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 1678c330f8ffSToby Isaac DMField coordField = NULL; 1679c330f8ffSToby Isaac Vec locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 16803755293bSMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a, *uL, *uR; 16819044fa66SMatthew G. Knepley IS chunkIS; 16829044fa66SMatthew G. Knepley const PetscInt *cells; 16839044fa66SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 1684c4d4a4f8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 1685b7260050SToby Isaac PetscInt maxDegree = PETSC_MAX_INT; 16864a3e9fdbSToby Isaac PetscQuadrature affineQuad = NULL, *quads = NULL; 16874a3e9fdbSToby Isaac PetscFEGeom *affineGeom = NULL, **geoms = NULL; 168808449791SMatthew G. Knepley PetscErrorCode ierr; 168908449791SMatthew G. Knepley 169008449791SMatthew G. Knepley PetscFunctionBegin; 169108449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 169208449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1693195142f5SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 169408449791SMatthew G. Knepley /* FEM+FVM */ 1695e5e52638SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1696e5e52638SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 169708449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 1698e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1699c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1700e5e52638SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 170108449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 170208449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 170308449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 170408449791SMatthew G. Knepley if (locA) { 1705*44171101SMatthew G. Knepley PetscInt subcell; 1706*44171101SMatthew G. Knepley ierr = DMPlexGetAuxiliaryPoint(dm, dmAux, cStart, &subcell);CHKERRQ(ierr); 170708449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1708*44171101SMatthew G. Knepley ierr = DMGetCellDS(dmAux, subcell, &probAux);CHKERRQ(ierr); 170908449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 171008449791SMatthew G. Knepley } 171108449791SMatthew G. Knepley /* 2: Get geometric data */ 171208449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 171308449791SMatthew G. Knepley PetscObject obj; 171408449791SMatthew G. Knepley PetscClassId id; 17157173168dSMatthew G. Knepley PetscBool fimp; 171608449791SMatthew G. Knepley 17177173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 17187173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 171908449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 172008449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 172108449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 172208449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 172308449791SMatthew G. Knepley } 172408449791SMatthew G. Knepley if (useFEM) { 17254a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1726b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 1727b7260050SToby Isaac if (maxDegree <= 1) { 17284a3e9fdbSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1729741db25dSToby Isaac if (affineQuad) { 17304a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 1731741db25dSToby Isaac } 17324a3e9fdbSToby Isaac } else { 17334a3e9fdbSToby Isaac ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 17344a3e9fdbSToby Isaac for (f = 0; f < Nf; ++f) { 17354a3e9fdbSToby Isaac PetscObject obj; 17364a3e9fdbSToby Isaac PetscClassId id; 17374a3e9fdbSToby Isaac PetscBool fimp; 17382f84e9bcSToby Isaac 17394a3e9fdbSToby Isaac ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 17404a3e9fdbSToby Isaac if (isImplicit != fimp) continue; 17414a3e9fdbSToby Isaac ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 17424a3e9fdbSToby Isaac ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 17434a3e9fdbSToby Isaac if (id == PETSCFE_CLASSID) { 17444a3e9fdbSToby Isaac PetscFE fe = (PetscFE) obj; 17452f84e9bcSToby Isaac 17464a3e9fdbSToby Isaac ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 17474a3e9fdbSToby Isaac ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr); 17484a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 17492f84e9bcSToby Isaac } 17502f84e9bcSToby Isaac } 17512f84e9bcSToby Isaac } 175208449791SMatthew G. Knepley } 175308449791SMatthew G. Knepley if (useFVM) { 175408449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 175508449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 175608449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 175708449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 175808449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 175908449791SMatthew G. Knepley if (dmGrad) { 176008449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 176108449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1762de555695SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 176308449791SMatthew G. Knepley /* Communicate gradient values */ 176408449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 176508449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 176608449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 176708449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 176808449791SMatthew G. Knepley } 1769bdd6f66aSToby Isaac /* Handle non-essential (e.g. outflow) boundary values */ 1770bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 177108449791SMatthew G. Knepley } 177208449791SMatthew G. Knepley /* Loop over chunks */ 17739044fa66SMatthew G. Knepley if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);} 17749044fa66SMatthew G. Knepley numCells = cEnd - cStart; 177508449791SMatthew G. Knepley numChunks = 1; 17764a3e9fdbSToby Isaac cellChunkSize = numCells/numChunks; 177708449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 1778741db25dSToby Isaac numChunks = PetscMin(1,numCells); 177908449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 17802eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 17812eefff9cSMatthew G. Knepley PetscReal *vol; 178208449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 17839044fa66SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 17843755293bSMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face; 178508449791SMatthew G. Knepley 178608449791SMatthew G. Knepley /* Extract field coefficients */ 178708449791SMatthew G. Knepley if (useFEM) { 17889044fa66SMatthew G. Knepley ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr); 17899044fa66SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 179069291d52SBarry Smith ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1791215c4595SMatthew G. Knepley ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 179208449791SMatthew G. Knepley } 179308449791SMatthew G. Knepley if (useFVM) { 17945f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 17955f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 179669291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 179769291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 1798215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1799215c4595SMatthew G. Knepley ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 180008449791SMatthew G. Knepley } 180108449791SMatthew 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 */ 180208449791SMatthew G. Knepley /* Loop over fields */ 180308449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 180408449791SMatthew G. Knepley PetscObject obj; 180508449791SMatthew G. Knepley PetscClassId id; 18067173168dSMatthew G. Knepley PetscBool fimp; 180708449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 180808449791SMatthew G. Knepley 18097173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 18107173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 181108449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 181208449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 181308449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 181408449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 18154a3e9fdbSToby Isaac PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 18164a3e9fdbSToby Isaac PetscFEGeom *chunkGeom = NULL; 18174a3e9fdbSToby Isaac PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 181808449791SMatthew G. Knepley PetscInt Nq, Nb; 181908449791SMatthew G. Knepley 182008449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 18214a3e9fdbSToby Isaac ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 182208449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1823c330f8ffSToby Isaac blockSize = Nb; 182408449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 182508449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 182608449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 182708449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 182808449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 182908449791SMatthew G. Knepley offset = numCells - Nr; 183008449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 183108449791SMatthew 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) */ 18324a3e9fdbSToby Isaac ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 1833c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 18344a3e9fdbSToby Isaac ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1835c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 18364a3e9fdbSToby Isaac ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 183708449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 183808449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 183908449791SMatthew G. Knepley 184008449791SMatthew G. Knepley Ne = numFaces; 184108449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 184208449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 184308449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 184408449791SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 184508449791SMatthew G. Knepley } 184608449791SMatthew G. Knepley /* Loop over domain */ 184708449791SMatthew G. Knepley if (useFEM) { 184808449791SMatthew G. Knepley /* Add elemVec to locX */ 18499044fa66SMatthew G. Knepley for (c = cS; c < cE; ++c) { 18509044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 18519044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 1852f905620eSMatthew G. Knepley 18539044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 1854b4920ed3SToby Isaac if (ghostLabel) { 1855b4920ed3SToby Isaac PetscInt ghostVal; 1856b4920ed3SToby Isaac 1857b4920ed3SToby Isaac ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 1858b4920ed3SToby Isaac if (ghostVal > 0) continue; 1859b4920ed3SToby Isaac } 18609044fa66SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 186108449791SMatthew G. Knepley } 186208449791SMatthew G. Knepley } 186308449791SMatthew G. Knepley if (useFVM) { 18644a394323SMatthew G. Knepley PetscScalar *fa; 186508449791SMatthew G. Knepley PetscInt iface; 186608449791SMatthew G. Knepley 186708449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1868c10b5f1bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1869c10b5f1bSMatthew G. Knepley PetscFV fv; 1870c10b5f1bSMatthew G. Knepley PetscObject obj; 1871c10b5f1bSMatthew G. Knepley PetscClassId id; 18724a394323SMatthew G. Knepley PetscInt foff, pdim; 1873c10b5f1bSMatthew G. Knepley 1874c10b5f1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1875c10b5f1bSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1876c10b5f1bSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1877c10b5f1bSMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1878c10b5f1bSMatthew G. Knepley fv = (PetscFV) obj; 1879c10b5f1bSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1880c10b5f1bSMatthew G. Knepley /* Accumulate fluxes to cells */ 188108449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 18829044fa66SMatthew G. Knepley const PetscInt *scells; 1883b4920ed3SToby Isaac PetscScalar *fL = NULL, *fR = NULL; 18847c45b140SToby Isaac PetscInt ghost, d, nsupp, nchild; 188508449791SMatthew G. Knepley 188608449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1887ffe9ad51SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 18887c45b140SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 18897c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 18909044fa66SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr); 18919044fa66SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr); 18929044fa66SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);} 18939044fa66SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr); 18949044fa66SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);} 1895c10b5f1bSMatthew G. Knepley for (d = 0; d < pdim; ++d) { 1896c10b5f1bSMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1897c10b5f1bSMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 189808449791SMatthew G. Knepley } 189908449791SMatthew G. Knepley ++iface; 190008449791SMatthew G. Knepley } 1901dab51205SMatthew G. Knepley } 1902dab51205SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1903dab51205SMatthew G. Knepley } 1904c10b5f1bSMatthew G. Knepley /* Handle time derivative */ 1905c10b5f1bSMatthew G. Knepley if (locX_t) { 1906dab51205SMatthew G. Knepley PetscScalar *x_t, *fa; 1907dab51205SMatthew G. Knepley 1908dab51205SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1909c10b5f1bSMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1910dab51205SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1911dab51205SMatthew G. Knepley PetscFV fv; 1912dab51205SMatthew G. Knepley PetscObject obj; 1913dab51205SMatthew G. Knepley PetscClassId id; 1914dab51205SMatthew G. Knepley PetscInt pdim, d; 1915dab51205SMatthew G. Knepley 1916dab51205SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1917dab51205SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1918dab51205SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1919dab51205SMatthew G. Knepley fv = (PetscFV) obj; 1920dab51205SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 19219044fa66SMatthew G. Knepley for (c = cS; c < cE; ++c) { 19229044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 1923c10b5f1bSMatthew G. Knepley PetscScalar *u_t, *r; 1924c10b5f1bSMatthew G. Knepley 1925b4920ed3SToby Isaac if (ghostLabel) { 1926b4920ed3SToby Isaac PetscInt ghostVal; 1927b4920ed3SToby Isaac 1928b4920ed3SToby Isaac ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr); 1929b4920ed3SToby Isaac if (ghostVal > 0) continue; 1930b4920ed3SToby Isaac } 1931c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1932c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1933d63b37e5SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1934c10b5f1bSMatthew G. Knepley } 1935dab51205SMatthew G. Knepley } 1936c10b5f1bSMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 193708449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 193808449791SMatthew G. Knepley } 193908449791SMatthew G. Knepley if (useFEM) { 19409044fa66SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 194169291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 194208449791SMatthew G. Knepley } 194308449791SMatthew G. Knepley if (useFVM) { 19445f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 19455f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 194669291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 194769291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 194808449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 194908449791SMatthew G. Knepley } 195008449791SMatthew G. Knepley } 195103fee3f0SMatthew G. Knepley if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);} 19529044fa66SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 195308449791SMatthew G. Knepley 19544a3e9fdbSToby Isaac if (useFEM) { 19554a3e9fdbSToby Isaac ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr); 19564a3e9fdbSToby Isaac 1957b7260050SToby Isaac if (maxDegree <= 1) { 19584a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 19594a3e9fdbSToby Isaac ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 19604a3e9fdbSToby Isaac } else { 19614a3e9fdbSToby Isaac for (f = 0; f < Nf; ++f) { 19624a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 19634a3e9fdbSToby Isaac ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr); 19644a3e9fdbSToby Isaac } 19654a3e9fdbSToby Isaac ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 19664a3e9fdbSToby Isaac } 19674a3e9fdbSToby Isaac } 196808449791SMatthew G. Knepley 196908449791SMatthew G. Knepley /* FEM */ 197008449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 197108449791SMatthew G. Knepley /* 2: Get geometric data */ 197208449791SMatthew G. Knepley /* 3: Handle boundary values */ 197308449791SMatthew G. Knepley /* 4: Loop over domain */ 197408449791SMatthew G. Knepley /* Extract coefficients */ 197508449791SMatthew G. Knepley /* Loop over fields */ 197608449791SMatthew G. Knepley /* Set tiling for FE*/ 197708449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 197808449791SMatthew G. Knepley /* Loop over subdomain */ 197908449791SMatthew G. Knepley /* Loop over quad points */ 198008449791SMatthew G. Knepley /* Transform coords to real space */ 198108449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 198208449791SMatthew G. Knepley /* Evaluate residual at point */ 198308449791SMatthew G. Knepley /* Transform residual to real space */ 198408449791SMatthew G. Knepley /* Add residual to elemVec */ 198508449791SMatthew G. Knepley /* Loop over domain */ 198608449791SMatthew G. Knepley /* Add elemVec to locX */ 198708449791SMatthew G. Knepley 198808449791SMatthew G. Knepley /* FVM */ 198908449791SMatthew G. Knepley /* Get geometric data */ 199008449791SMatthew G. Knepley /* If using gradients */ 199108449791SMatthew G. Knepley /* Compute gradient data */ 199208449791SMatthew G. Knepley /* Loop over domain faces */ 199308449791SMatthew G. Knepley /* Count computational faces */ 199408449791SMatthew G. Knepley /* Reconstruct cell gradient */ 199508449791SMatthew G. Knepley /* Loop over domain cells */ 199608449791SMatthew G. Knepley /* Limit cell gradients */ 199708449791SMatthew G. Knepley /* Handle boundary values */ 199808449791SMatthew G. Knepley /* Loop over domain faces */ 199908449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 200008449791SMatthew G. Knepley /* Riemann solve over faces */ 200108449791SMatthew G. Knepley /* Loop over domain faces */ 200208449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 200308449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 2004247ba720SToby Isaac if (mesh->printFEM) { 2005247ba720SToby Isaac Vec locFbc; 2006247ba720SToby Isaac PetscInt pStart, pEnd, p, maxDof; 2007247ba720SToby Isaac PetscScalar *zeroes; 2008247ba720SToby Isaac 2009247ba720SToby Isaac ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 2010247ba720SToby Isaac ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 2011247ba720SToby Isaac ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 2012247ba720SToby Isaac ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 2013247ba720SToby Isaac ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 2014247ba720SToby Isaac for (p = pStart; p < pEnd; p++) { 2015247ba720SToby Isaac ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 2016247ba720SToby Isaac } 2017247ba720SToby Isaac ierr = PetscFree(zeroes);CHKERRQ(ierr); 2018247ba720SToby Isaac ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 2019247ba720SToby Isaac ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 2020247ba720SToby Isaac } 202124cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 202224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 202324cdb843SMatthew G. Knepley } 202424cdb843SMatthew G. Knepley 202511dd639bSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user) 202624cdb843SMatthew G. Knepley { 202724cdb843SMatthew G. Knepley DM dmCh, dmAux; 2028c330f8ffSToby Isaac Vec A; 2029c330f8ffSToby Isaac DMField coordField = NULL; 203024cdb843SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 203124cdb843SMatthew G. Knepley PetscSection section, sectionAux; 203224cdb843SMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 2033c330f8ffSToby Isaac PetscInt Nf, f, numCells, cStart, cEnd, c; 20343755293bSMatthew G. Knepley PetscInt totDim, totDimAux = 0, diffCell = 0; 20354a3e9fdbSToby Isaac PetscInt depth; 2036b7260050SToby Isaac PetscInt maxDegree; 2037c330f8ffSToby Isaac IS cellIS; 20384a3e9fdbSToby Isaac DMLabel depthLabel; 203924cdb843SMatthew G. Knepley PetscErrorCode ierr; 204024cdb843SMatthew G. Knepley 204124cdb843SMatthew G. Knepley PetscFunctionBegin; 2042e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 204324cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 204424cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 204524cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 204624cdb843SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 204724cdb843SMatthew G. Knepley numCells = cEnd - cStart; 204824cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 204924cdb843SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 205024cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 205124cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 205224cdb843SMatthew G. Knepley if (dmAux) { 2053e87a4003SBarry Smith ierr = DMGetSection(dmAux, §ionAux);CHKERRQ(ierr); 205424cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 205524cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 205624cdb843SMatthew G. Knepley } 205724cdb843SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 2058bbce034cSMatthew G. Knepley ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr); 205924cdb843SMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 206024cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 20614a3e9fdbSToby Isaac ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 20624a3e9fdbSToby Isaac ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr); 20634a3e9fdbSToby Isaac ierr = DMLabelGetStratumIS(depthLabel,depth,&cellIS);CHKERRQ(ierr); 20644a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 206524cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 206624cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 206724cdb843SMatthew G. Knepley PetscInt i; 206824cdb843SMatthew G. Knepley 206924cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 207024cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 207124cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 207224cdb843SMatthew G. Knepley if (X_t) { 207324cdb843SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 207424cdb843SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 207524cdb843SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 207624cdb843SMatthew G. Knepley } 207724cdb843SMatthew G. Knepley if (dmAux) { 20786da023fcSToby Isaac DM dmAuxPlex; 20796da023fcSToby Isaac 20806da023fcSToby Isaac ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr); 20816da023fcSToby Isaac ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 208224cdb843SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 20836da023fcSToby Isaac ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 20846da023fcSToby Isaac ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr); 208524cdb843SMatthew G. Knepley } 208624cdb843SMatthew G. Knepley } 208724cdb843SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 208824cdb843SMatthew G. Knepley PetscFE fe, feCh; 2089c330f8ffSToby Isaac PetscInt Nq, Nb; 209024cdb843SMatthew G. Knepley /* Conforming batches */ 209124cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 209224cdb843SMatthew G. Knepley /* Remainder */ 209324cdb843SMatthew G. Knepley PetscInt Nr, offset; 2094c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 2095c330f8ffSToby Isaac PetscFEGeom *cgeomFEM, *chunkGeom = NULL; 209624cdb843SMatthew G. Knepley 209724cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 209824cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 209924cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 210024cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2101b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 2102b7260050SToby Isaac if (maxDegree <= 1) { 2103c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2104c330f8ffSToby Isaac } 2105c330f8ffSToby Isaac if (!qGeom) { 2106c330f8ffSToby Isaac ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr); 2107c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2108c330f8ffSToby Isaac } 2109c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 21104a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2111c330f8ffSToby Isaac blockSize = Nb; 211224cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 211324cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 211424cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 211524cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 211624cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 211724cdb843SMatthew G. Knepley offset = numCells - Nr; 2118c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2119c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 2120c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr); 2121c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 2122c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 2123c330f8ffSToby Isaac ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVecCh[offset*totDim]);CHKERRQ(ierr); 2124c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 21254a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2126c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 212724cdb843SMatthew G. Knepley } 2128c330f8ffSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 212924cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 213024cdb843SMatthew G. Knepley PetscBool diff = PETSC_FALSE; 213124cdb843SMatthew G. Knepley PetscInt d; 213224cdb843SMatthew G. Knepley 213324cdb843SMatthew 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;} 213424cdb843SMatthew G. Knepley if (diff) { 213524cdb843SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 213624cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 213724cdb843SMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 213824cdb843SMatthew G. Knepley ++diffCell; 213924cdb843SMatthew G. Knepley } 214024cdb843SMatthew G. Knepley if (diffCell > 9) break; 2141c14a31d2SToby Isaac ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 214224cdb843SMatthew G. Knepley } 2143bbce034cSMatthew G. Knepley ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr); 214424cdb843SMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 214524cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 214624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 214724cdb843SMatthew G. Knepley } 214824cdb843SMatthew G. Knepley 214924cdb843SMatthew G. Knepley /*@ 215024cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 215124cdb843SMatthew G. Knepley 215224cdb843SMatthew G. Knepley Input Parameters: 215324cdb843SMatthew G. Knepley + dm - The mesh 215424cdb843SMatthew G. Knepley . X - Local solution 215524cdb843SMatthew G. Knepley - user - The user context 215624cdb843SMatthew G. Knepley 215724cdb843SMatthew G. Knepley Output Parameter: 215824cdb843SMatthew G. Knepley . F - Local output vector 215924cdb843SMatthew G. Knepley 216024cdb843SMatthew G. Knepley Level: developer 216124cdb843SMatthew G. Knepley 21627a73cf09SMatthew G. Knepley .seealso: DMPlexComputeJacobianAction() 216324cdb843SMatthew G. Knepley @*/ 216424cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 216524cdb843SMatthew G. Knepley { 216624cdb843SMatthew G. Knepley PetscObject check; 21676da023fcSToby Isaac DM plex; 21684a3e9fdbSToby Isaac IS cellIS; 21694a3e9fdbSToby Isaac PetscInt depth; 217024cdb843SMatthew G. Knepley PetscErrorCode ierr; 217124cdb843SMatthew G. Knepley 217224cdb843SMatthew G. Knepley PetscFunctionBegin; 21736da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 21744a3e9fdbSToby Isaac ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2175aeadca18SToby Isaac ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 21764a3e9fdbSToby Isaac if (!cellIS) { 21774a3e9fdbSToby Isaac ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr); 21784a3e9fdbSToby Isaac } 217924cdb843SMatthew G. Knepley /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */ 21806da023fcSToby Isaac ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr); 218111dd639bSMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 21824a3e9fdbSToby Isaac else {ierr = DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);} 21834a3e9fdbSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 21849a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 218524cdb843SMatthew G. Knepley PetscFunctionReturn(0); 218624cdb843SMatthew G. Knepley } 218724cdb843SMatthew G. Knepley 2188bdd6f66aSToby Isaac /*@ 2189bdd6f66aSToby Isaac DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 2190bdd6f66aSToby Isaac 2191bdd6f66aSToby Isaac Input Parameters: 2192bdd6f66aSToby Isaac + dm - The mesh 2193bdd6f66aSToby Isaac - user - The user context 2194bdd6f66aSToby Isaac 2195bdd6f66aSToby Isaac Output Parameter: 2196bdd6f66aSToby Isaac . X - Local solution 2197bdd6f66aSToby Isaac 2198bdd6f66aSToby Isaac Level: developer 2199bdd6f66aSToby Isaac 22007a73cf09SMatthew G. Knepley .seealso: DMPlexComputeJacobianAction() 2201bdd6f66aSToby Isaac @*/ 2202bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 2203bdd6f66aSToby Isaac { 2204bdd6f66aSToby Isaac DM plex; 2205bdd6f66aSToby Isaac PetscErrorCode ierr; 2206bdd6f66aSToby Isaac 2207bdd6f66aSToby Isaac PetscFunctionBegin; 2208bdd6f66aSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 2209bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 2210bdd6f66aSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 2211bdd6f66aSToby Isaac PetscFunctionReturn(0); 2212bdd6f66aSToby Isaac } 2213bdd6f66aSToby Isaac 221451e9edccSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS) 2215089bfe53SSander Arens { 2216089bfe53SSander Arens DM_Plex *mesh = (DM_Plex *) dm->data; 221751e9edccSMatthew G. Knepley DM plex = NULL, plexA = NULL; 22182d91c981SSander Arens PetscDS prob, probAux = NULL; 221951e9edccSMatthew G. Knepley PetscSection section, sectionAux = NULL; 222051e9edccSMatthew G. Knepley PetscSection globalSection, subSection = NULL; 22212d91c981SSander Arens Vec locA = NULL; 22222d91c981SSander Arens PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL; 222351e9edccSMatthew G. Knepley PetscInt v; 222451e9edccSMatthew G. Knepley PetscInt Nf, totDim, totDimAux = 0; 2225089bfe53SSander Arens PetscBool isMatISP; 2226089bfe53SSander Arens PetscErrorCode ierr; 2227089bfe53SSander Arens 2228089bfe53SSander Arens PetscFunctionBegin; 222951e9edccSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 2230e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 2231089bfe53SSander Arens ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2232089bfe53SSander Arens ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 22334d0b9603SSander Arens ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 22342d91c981SSander Arens ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 22352d91c981SSander Arens if (locA) { 223651e9edccSMatthew G. Knepley DM dmAux; 223751e9edccSMatthew G. Knepley 22382d91c981SSander Arens ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 223951e9edccSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 224051e9edccSMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 22414d0b9603SSander Arens ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 224251e9edccSMatthew G. Knepley ierr = DMGetSection(plexA, §ionAux);CHKERRQ(ierr); 22432d91c981SSander Arens } 224451e9edccSMatthew G. Knepley 224551e9edccSMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 224651e9edccSMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 224751e9edccSMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 224851e9edccSMatthew G. Knepley for (v = 0; v < numValues; ++v) { 224951e9edccSMatthew G. Knepley PetscFEGeom *fgeom; 2250b7260050SToby Isaac PetscInt maxDegree; 225151e9edccSMatthew G. Knepley PetscQuadrature qGeom = NULL; 2252089bfe53SSander Arens IS pointIS; 2253089bfe53SSander Arens const PetscInt *points; 225451e9edccSMatthew G. Knepley PetscInt numFaces, face, Nq; 2255c330f8ffSToby Isaac 2256089bfe53SSander Arens ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 2257089bfe53SSander Arens if (!pointIS) continue; /* No points with that id on this process */ 2258c330f8ffSToby Isaac { 2259c330f8ffSToby Isaac IS isectIS; 2260c330f8ffSToby Isaac 2261c330f8ffSToby Isaac /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */ 226200199b93SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 2263c330f8ffSToby Isaac ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 2264c330f8ffSToby Isaac pointIS = isectIS; 2265089bfe53SSander Arens } 2266c330f8ffSToby Isaac ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 2267c330f8ffSToby Isaac ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 226851e9edccSMatthew G. Knepley ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 2269b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 2270b7260050SToby Isaac if (maxDegree <= 1) { 2271c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 2272c330f8ffSToby Isaac } 2273c330f8ffSToby Isaac if (!qGeom) { 227451e9edccSMatthew G. Knepley PetscFE fe; 227551e9edccSMatthew G. Knepley 227651e9edccSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2277c330f8ffSToby Isaac ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 2278c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2279c330f8ffSToby Isaac } 2280c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 22814a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 2282c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 2283c330f8ffSToby Isaac const PetscInt point = points[face], *support, *cone; 2284089bfe53SSander Arens PetscScalar *x = NULL; 22859bfb2fe4SJed Brown PetscInt i, coneSize, faceLoc; 2286089bfe53SSander Arens 22874d0b9603SSander Arens ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 22884d0b9603SSander Arens ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 22894d0b9603SSander Arens ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 22904d0b9603SSander Arens for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 22914d0b9603SSander Arens if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of support[0] %d", point, support[0]); 2292f99c8401SToby Isaac fgeom->face[face][0] = faceLoc; 229351e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 22944d0b9603SSander Arens for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 229551e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 2296089bfe53SSander Arens if (locX_t) { 229751e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 22984d0b9603SSander Arens for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 229951e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 2300089bfe53SSander Arens } 23012d91c981SSander Arens if (locA) { 230251e9edccSMatthew G. Knepley PetscInt subp; 230351e9edccSMatthew G. Knepley ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr); 230451e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 23054d0b9603SSander Arens for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 230651e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 23072d91c981SSander Arens } 2308089bfe53SSander Arens } 23094d0b9603SSander Arens ierr = PetscMemzero(elemMat, numFaces*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2310089bfe53SSander Arens { 2311089bfe53SSander Arens PetscFE fe; 2312c330f8ffSToby Isaac PetscInt Nb; 2313089bfe53SSander Arens /* Conforming batches */ 2314089bfe53SSander Arens PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2315089bfe53SSander Arens /* Remainder */ 2316c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 231751e9edccSMatthew G. Knepley PetscInt fieldJ, Nr, offset; 2318089bfe53SSander Arens 23194d0b9603SSander Arens ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2320089bfe53SSander Arens ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2321089bfe53SSander Arens ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2322c330f8ffSToby Isaac blockSize = Nb; 2323089bfe53SSander Arens batchSize = numBlocks * blockSize; 2324089bfe53SSander Arens ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2325089bfe53SSander Arens numChunks = numFaces / (numBatches*batchSize); 2326089bfe53SSander Arens Ne = numChunks*numBatches*batchSize; 2327089bfe53SSander Arens Nr = numFaces % (numBatches*batchSize); 2328089bfe53SSander Arens offset = numFaces - Nr; 2329c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 2330089bfe53SSander Arens for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2331c330f8ffSToby Isaac ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2332089bfe53SSander Arens } 2333c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 2334c330f8ffSToby Isaac for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2335c330f8ffSToby Isaac ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2336089bfe53SSander Arens } 2337c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 2338c330f8ffSToby Isaac } 2339c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 2340c330f8ffSToby Isaac const PetscInt point = points[face], *support; 2341089bfe53SSander Arens 23424d0b9603SSander Arens if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 234351e9edccSMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 2344089bfe53SSander Arens if (!isMatISP) { 234551e9edccSMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2346089bfe53SSander Arens } else { 2347089bfe53SSander Arens Mat lJ; 2348089bfe53SSander Arens 2349089bfe53SSander Arens ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr); 235051e9edccSMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2351089bfe53SSander Arens } 2352089bfe53SSander Arens } 23534a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 2354c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2355089bfe53SSander Arens ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 2356089bfe53SSander Arens ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 235751e9edccSMatthew G. Knepley ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr); 23582d91c981SSander Arens } 235951e9edccSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 236051e9edccSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 236151e9edccSMatthew G. Knepley PetscFunctionReturn(0); 236251e9edccSMatthew G. Knepley } 236351e9edccSMatthew G. Knepley 236451e9edccSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP) 236551e9edccSMatthew G. Knepley { 236651e9edccSMatthew G. Knepley DMField coordField; 236751e9edccSMatthew G. Knepley DMLabel depthLabel; 236851e9edccSMatthew G. Knepley IS facetIS; 236951e9edccSMatthew G. Knepley PetscInt dim; 237051e9edccSMatthew G. Knepley PetscErrorCode ierr; 237151e9edccSMatthew G. Knepley 237251e9edccSMatthew G. Knepley PetscFunctionBegin; 237351e9edccSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 237451e9edccSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 237551e9edccSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 237651e9edccSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 237751e9edccSMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 237851e9edccSMatthew G. Knepley PetscFunctionReturn(0); 237951e9edccSMatthew G. Knepley } 238051e9edccSMatthew G. Knepley 238151e9edccSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user) 238251e9edccSMatthew G. Knepley { 238351e9edccSMatthew G. Knepley PetscDS prob; 238451e9edccSMatthew G. Knepley PetscInt dim, numBd, bd; 238551e9edccSMatthew G. Knepley DMLabel depthLabel; 238651e9edccSMatthew G. Knepley DMField coordField = NULL; 238751e9edccSMatthew G. Knepley IS facetIS; 238851e9edccSMatthew G. Knepley PetscErrorCode ierr; 238951e9edccSMatthew G. Knepley 239051e9edccSMatthew G. Knepley PetscFunctionBegin; 239151e9edccSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 239251e9edccSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 239351e9edccSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 239451e9edccSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 239551e9edccSMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 239651e9edccSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 239751e9edccSMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 239851e9edccSMatthew G. Knepley DMBoundaryConditionType type; 239951e9edccSMatthew G. Knepley const char *bdLabel; 240051e9edccSMatthew G. Knepley DMLabel label; 240151e9edccSMatthew G. Knepley const PetscInt *values; 240251e9edccSMatthew G. Knepley PetscInt fieldI, numValues; 240351e9edccSMatthew G. Knepley PetscObject obj; 240451e9edccSMatthew G. Knepley PetscClassId id; 240551e9edccSMatthew G. Knepley 240651e9edccSMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 240751e9edccSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr); 240851e9edccSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 240951e9edccSMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 241051e9edccSMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 241151e9edccSMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 2412089bfe53SSander Arens } 2413c330f8ffSToby Isaac ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 2414089bfe53SSander Arens PetscFunctionReturn(0); 2415089bfe53SSander Arens } 2416089bfe53SSander Arens 24174a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 241824cdb843SMatthew G. Knepley { 241924cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 242024cdb843SMatthew G. Knepley const char *name = "Jacobian"; 2421f7ed7b21SMatthew G. Knepley DM dmAux, plex; 2422c330f8ffSToby Isaac Vec A; 2423c330f8ffSToby Isaac DMField coordField; 242424cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 2425be36d101SStefano Zampini PetscSection section, globalSection, subSection, sectionAux; 2426426ff135SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 24279044fa66SMatthew G. Knepley const PetscInt *cells; 24289044fa66SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ; 24299044fa66SMatthew G. Knepley PetscInt totDim, totDimAux, cStart, cEnd, numCells, c; 24306f158342SMatthew G. Knepley PetscBool isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE; 243124cdb843SMatthew G. Knepley PetscErrorCode ierr; 243224cdb843SMatthew G. Knepley 243324cdb843SMatthew G. Knepley PetscFunctionBegin; 243424cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2435e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 2436be36d101SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 2437e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 24385bf53532SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 243924cdb843SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 244024cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2441efc10488SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 244255ad3c34SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2443426ff135SMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2444426ff135SMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 244524cdb843SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 24464a3e9fdbSToby Isaac ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 24479044fa66SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 244824cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 244924cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 245024cdb843SMatthew G. Knepley if (dmAux) { 2451f7ed7b21SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 2452e87a4003SBarry Smith ierr = DMGetSection(plex, §ionAux);CHKERRQ(ierr); 245324cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 245424cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 245524cdb843SMatthew G. Knepley } 2456efc10488SMatthew 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); 245724cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 24584a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 245924cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 24609044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 24619044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 246224cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 246324cdb843SMatthew G. Knepley PetscInt i; 246424cdb843SMatthew G. Knepley 24659044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 24669044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 24679044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 246824cdb843SMatthew G. Knepley if (X_t) { 24699044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 24709044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 24719044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 247224cdb843SMatthew G. Knepley } 247324cdb843SMatthew G. Knepley if (dmAux) { 2474*44171101SMatthew G. Knepley PetscInt subcell; 2475*44171101SMatthew G. Knepley ierr = DMPlexGetAuxiliaryPoint(dm, dmAux, cell, &subcell);CHKERRQ(ierr); 2476*44171101SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 24779044fa66SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 2478*44171101SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 247924cdb843SMatthew G. Knepley } 248024cdb843SMatthew G. Knepley } 2481efc10488SMatthew G. Knepley if (hasJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 248255ad3c34SMatthew G. Knepley if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2483426ff135SMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 248424cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 24855244db28SMatthew G. Knepley PetscClassId id; 248624cdb843SMatthew G. Knepley PetscFE fe; 2487c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 2488c330f8ffSToby Isaac PetscInt Nb; 248924cdb843SMatthew G. Knepley /* Conforming batches */ 249024cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 249124cdb843SMatthew G. Knepley /* Remainder */ 2492c330f8ffSToby Isaac PetscInt Nr, offset, Nq; 2493b7260050SToby Isaac PetscInt maxDegree; 2494c330f8ffSToby Isaac PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 249524cdb843SMatthew G. Knepley 249624cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 24975244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 24985244db28SMatthew G. Knepley if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 249924cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 250024cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2501b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 2502b7260050SToby Isaac if (maxDegree <= 1) { 2503c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2504c330f8ffSToby Isaac } 2505c330f8ffSToby Isaac if (!qGeom) { 2506c330f8ffSToby Isaac ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2507c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2508c330f8ffSToby Isaac } 2509c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 25104a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2511c330f8ffSToby Isaac blockSize = Nb; 251224cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 251324cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 251424cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 251524cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 251624cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 251724cdb843SMatthew G. Knepley offset = numCells - Nr; 2518c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2519c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 252024cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2521efc10488SMatthew G. Knepley if (hasJac) { 2522c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2523c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2524efc10488SMatthew G. Knepley } 252555ad3c34SMatthew G. Knepley if (hasPrec) { 2526c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 2527c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 2528426ff135SMatthew G. Knepley } 2529426ff135SMatthew G. Knepley if (hasDyn) { 2530c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 2531c330f8ffSToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 253255ad3c34SMatthew G. Knepley } 253324cdb843SMatthew G. Knepley } 2534c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2535c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 25364a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2537c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 253824cdb843SMatthew G. Knepley } 25395bf53532SMatthew G. Knepley /* Add contribution from X_t */ 25409044fa66SMatthew G. Knepley if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];} 25415244db28SMatthew G. Knepley if (hasFV) { 25425244db28SMatthew G. Knepley PetscClassId id; 25435244db28SMatthew G. Knepley PetscFV fv; 25445244db28SMatthew G. Knepley PetscInt offsetI, NcI, NbI = 1, fc, f; 25455244db28SMatthew G. Knepley 25465244db28SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 25475244db28SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 25485244db28SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 25495244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 25505244db28SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 25515244db28SMatthew G. Knepley /* Put in the identity */ 25525244db28SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 25535244db28SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 25549044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 25559044fa66SMatthew G. Knepley const PetscInt eOffset = cind*totDim*totDim; 25565244db28SMatthew G. Knepley for (fc = 0; fc < NcI; ++fc) { 25575244db28SMatthew G. Knepley for (f = 0; f < NbI; ++f) { 25585244db28SMatthew G. Knepley const PetscInt i = offsetI + f*NcI+fc; 25595244db28SMatthew G. Knepley if (hasPrec) { 25605244db28SMatthew G. Knepley if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 25615244db28SMatthew G. Knepley elemMatP[eOffset+i*totDim+i] = 1.0; 25625244db28SMatthew G. Knepley } else {elemMat[eOffset+i*totDim+i] = 1.0;} 25635244db28SMatthew G. Knepley } 25645244db28SMatthew G. Knepley } 25655244db28SMatthew G. Knepley } 25665244db28SMatthew G. Knepley } 25675244db28SMatthew G. Knepley /* No allocated space for FV stuff, so ignore the zero entries */ 25685244db28SMatthew G. Knepley ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 25695244db28SMatthew G. Knepley } 25705bf53532SMatthew G. Knepley /* Insert values into matrix */ 2571be36d101SStefano Zampini isMatIS = PETSC_FALSE; 2572be36d101SStefano Zampini if (hasPrec && hasJac) { 2573be36d101SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr); 2574be36d101SStefano Zampini } 2575be36d101SStefano Zampini if (isMatIS && !subSection) { 2576be36d101SStefano Zampini ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 2577be36d101SStefano Zampini } 257824cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 25799044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 25809044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 25819044fa66SMatthew G. Knepley 258255ad3c34SMatthew G. Knepley if (hasPrec) { 2583efc10488SMatthew G. Knepley if (hasJac) { 25849044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2585be36d101SStefano Zampini if (!isMatIS) { 25869044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2587be36d101SStefano Zampini } else { 2588be36d101SStefano Zampini Mat lJ; 2589be36d101SStefano Zampini 2590be36d101SStefano Zampini ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 25919044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2592be36d101SStefano Zampini } 2593efc10488SMatthew G. Knepley } 25949044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 2595be36d101SStefano Zampini if (!isMatISP) { 25969044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 259755ad3c34SMatthew G. Knepley } else { 2598be36d101SStefano Zampini Mat lJ; 2599be36d101SStefano Zampini 2600be36d101SStefano Zampini ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 26019044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2602be36d101SStefano Zampini } 2603be36d101SStefano Zampini } else { 26049044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2605be36d101SStefano Zampini if (!isMatISP) { 26069044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2607be36d101SStefano Zampini } else { 2608be36d101SStefano Zampini Mat lJ; 2609be36d101SStefano Zampini 2610be36d101SStefano Zampini ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 26119044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2612be36d101SStefano Zampini } 261324cdb843SMatthew G. Knepley } 261455ad3c34SMatthew G. Knepley } 26159044fa66SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 26165244db28SMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 2617426ff135SMatthew G. Knepley ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2618f7ed7b21SMatthew G. Knepley if (dmAux) { 2619f7ed7b21SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2620f7ed7b21SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2621f7ed7b21SMatthew G. Knepley } 26225bf53532SMatthew G. Knepley /* Compute boundary integrals */ 2623089bfe53SSander Arens ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr); 26245bf53532SMatthew G. Knepley /* Assemble matrix */ 2625efc10488SMatthew G. Knepley if (hasJac && hasPrec) { 262682ef7567SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 262782ef7567SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 262882ef7567SMatthew G. Knepley } 262924cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 263024cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 263124cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 263224cdb843SMatthew G. Knepley PetscFunctionReturn(0); 263324cdb843SMatthew G. Knepley } 263424cdb843SMatthew G. Knepley 26357a73cf09SMatthew G. Knepley /*@ 26367a73cf09SMatthew G. Knepley DMPlexComputeJacobianAction - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user. 26377a73cf09SMatthew G. Knepley 26387a73cf09SMatthew G. Knepley Input Parameters: 26397a73cf09SMatthew G. Knepley + dm - The mesh 26407a73cf09SMatthew G. Knepley . cellIS - 26417a73cf09SMatthew G. Knepley . t - The time 26427a73cf09SMatthew G. Knepley . X_tShift - The multiplier for the Jacobian with repsect to X_t 26437a73cf09SMatthew G. Knepley . X - Local solution vector 26447a73cf09SMatthew G. Knepley . X_t - Time-derivative of the local solution vector 26457a73cf09SMatthew G. Knepley . Y - Local input vector 26467a73cf09SMatthew G. Knepley - user - The user context 26477a73cf09SMatthew G. Knepley 26487a73cf09SMatthew G. Knepley Output Parameter: 26497a73cf09SMatthew G. Knepley . Z - Local output vector 26507a73cf09SMatthew G. Knepley 26517a73cf09SMatthew G. Knepley Note: 26527a73cf09SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 26537a73cf09SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 26547a73cf09SMatthew G. Knepley 26557a73cf09SMatthew G. Knepley Level: developer 26567a73cf09SMatthew G. Knepley 26577a73cf09SMatthew G. Knepley .seealso: FormFunctionLocal() 26587a73cf09SMatthew G. Knepley @*/ 26597a73cf09SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianAction(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user) 2660a925c78cSMatthew G. Knepley { 2661a925c78cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2662a925c78cSMatthew G. Knepley const char *name = "Jacobian"; 26637a73cf09SMatthew G. Knepley DM dmAux, plex, plexAux = NULL; 2664c330f8ffSToby Isaac Vec A; 2665a925c78cSMatthew G. Knepley PetscDS prob, probAux = NULL; 2666a925c78cSMatthew G. Knepley PetscQuadrature quad; 2667a925c78cSMatthew G. Knepley PetscSection section, globalSection, sectionAux; 2668a925c78cSMatthew G. Knepley PetscScalar *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z; 26699044fa66SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ; 26704d0b9603SSander Arens PetscInt totDim, totDimAux = 0; 26719044fa66SMatthew G. Knepley const PetscInt *cells; 26729044fa66SMatthew G. Knepley PetscInt cStart, cEnd, numCells, c; 267375b37a90SMatthew G. Knepley PetscBool hasDyn; 2674c330f8ffSToby Isaac DMField coordField; 2675a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2676a925c78cSMatthew G. Knepley 2677a925c78cSMatthew G. Knepley PetscFunctionBegin; 2678a925c78cSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 26797a73cf09SMatthew G. Knepley ierr = DMSNESConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr); 26807a73cf09SMatthew G. Knepley if (!cellIS) { 26817a73cf09SMatthew G. Knepley PetscInt depth; 26827a73cf09SMatthew G. Knepley 26837a73cf09SMatthew G. Knepley ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 26847a73cf09SMatthew G. Knepley ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 26857a73cf09SMatthew G. Knepley if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);} 26867a73cf09SMatthew G. Knepley } else { 26877a73cf09SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) cellIS);CHKERRQ(ierr); 26887a73cf09SMatthew G. Knepley } 2689e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 2690e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2691a925c78cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2692a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2693a925c78cSMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2694a925c78cSMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2695a925c78cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 26964a3e9fdbSToby Isaac ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 26979044fa66SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2698a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2699a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2700a925c78cSMatthew G. Knepley if (dmAux) { 27017a73cf09SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexAux);CHKERRQ(ierr); 2702cf0b7c11SKarl Rupp ierr = DMGetSection(plexAux, §ionAux);CHKERRQ(ierr); 2703a925c78cSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2704a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2705a925c78cSMatthew G. Knepley } 2706a925c78cSMatthew G. Knepley ierr = VecSet(Z, 0.0);CHKERRQ(ierr); 2707a925c78cSMatthew 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); 2708a925c78cSMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 27094a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2710a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 27119044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 27129044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 2713a925c78cSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 2714a925c78cSMatthew G. Knepley PetscInt i; 2715a925c78cSMatthew G. Knepley 27169044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 27179044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 27189044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2719a925c78cSMatthew G. Knepley if (X_t) { 27209044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 27219044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 27229044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2723a925c78cSMatthew G. Knepley } 2724a925c78cSMatthew G. Knepley if (dmAux) { 2725*44171101SMatthew G. Knepley PetscInt subcell; 2726*44171101SMatthew G. Knepley ierr = DMPlexGetAuxiliaryPoint(dm, dmAux, cell, &subcell);CHKERRQ(ierr); 2727*44171101SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 27289044fa66SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 2729*44171101SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 2730a925c78cSMatthew G. Knepley } 27319044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 27329044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i]; 27339044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 2734a925c78cSMatthew G. Knepley } 2735a925c78cSMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 2736a925c78cSMatthew G. Knepley if (hasDyn) {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2737a925c78cSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 2738a925c78cSMatthew G. Knepley PetscFE fe; 2739c330f8ffSToby Isaac PetscInt Nb; 2740a925c78cSMatthew G. Knepley /* Conforming batches */ 2741a925c78cSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2742a925c78cSMatthew G. Knepley /* Remainder */ 2743c330f8ffSToby Isaac PetscInt Nr, offset, Nq; 2744c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 2745b7260050SToby Isaac PetscInt maxDegree; 2746c330f8ffSToby Isaac PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 2747a925c78cSMatthew G. Knepley 2748a925c78cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2749a925c78cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2750a925c78cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2751a925c78cSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2752b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 2753b7260050SToby Isaac if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);} 2754c330f8ffSToby Isaac if (!qGeom) { 2755c330f8ffSToby Isaac ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2756c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2757c330f8ffSToby Isaac } 2758c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 27594a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2760c330f8ffSToby Isaac blockSize = Nb; 2761a925c78cSMatthew G. Knepley batchSize = numBlocks * blockSize; 2762a925c78cSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2763a925c78cSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 2764a925c78cSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 2765a925c78cSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 2766a925c78cSMatthew G. Knepley offset = numCells - Nr; 2767c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2768c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2769a925c78cSMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2770f99c8401SToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2771f99c8401SToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 2772a925c78cSMatthew G. Knepley if (hasDyn) { 2773f99c8401SToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 2774f99c8401SToby Isaac ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 2775a925c78cSMatthew G. Knepley } 2776a925c78cSMatthew G. Knepley } 2777c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2778c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 27794a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2780c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2781a925c78cSMatthew G. Knepley } 2782a925c78cSMatthew G. Knepley if (hasDyn) { 27839044fa66SMatthew G. Knepley for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2784a925c78cSMatthew G. Knepley } 2785a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 27869044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 27879044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 2788a925c78cSMatthew G. Knepley const PetscBLASInt M = totDim, one = 1; 2789a925c78cSMatthew G. Knepley const PetscScalar a = 1.0, b = 0.0; 2790a925c78cSMatthew G. Knepley 27919044fa66SMatthew G. Knepley PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one)); 2792a925c78cSMatthew G. Knepley if (mesh->printFEM > 1) { 27939044fa66SMatthew G. Knepley ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr); 27949044fa66SMatthew G. Knepley ierr = DMPrintCellVector(c, "Y", totDim, &y[cind*totDim]);CHKERRQ(ierr); 2795a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Z", totDim, z);CHKERRQ(ierr); 2796a925c78cSMatthew G. Knepley } 27979044fa66SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr); 2798a925c78cSMatthew G. Knepley } 2799a925c78cSMatthew G. Knepley ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr); 2800a925c78cSMatthew G. Knepley if (mesh->printFEM) { 2801a925c78cSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr); 2802a925c78cSMatthew G. Knepley ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 2803a925c78cSMatthew G. Knepley } 28047a73cf09SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 28057a73cf09SMatthew G. Knepley ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 28067a73cf09SMatthew G. Knepley ierr = DMDestroy(&plexAux);CHKERRQ(ierr); 28077a73cf09SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2808a925c78cSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2809a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2810a925c78cSMatthew G. Knepley } 2811a925c78cSMatthew G. Knepley 281224cdb843SMatthew G. Knepley /*@ 281324cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 281424cdb843SMatthew G. Knepley 281524cdb843SMatthew G. Knepley Input Parameters: 281624cdb843SMatthew G. Knepley + dm - The mesh 281724cdb843SMatthew G. Knepley . X - Local input vector 281824cdb843SMatthew G. Knepley - user - The user context 281924cdb843SMatthew G. Knepley 282024cdb843SMatthew G. Knepley Output Parameter: 282124cdb843SMatthew G. Knepley . Jac - Jacobian matrix 282224cdb843SMatthew G. Knepley 282324cdb843SMatthew G. Knepley Note: 282424cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 282524cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 282624cdb843SMatthew G. Knepley 282724cdb843SMatthew G. Knepley Level: developer 282824cdb843SMatthew G. Knepley 282924cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 283024cdb843SMatthew G. Knepley @*/ 283124cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 283224cdb843SMatthew G. Knepley { 28336da023fcSToby Isaac DM plex; 2834f04eb4edSMatthew G. Knepley PetscDS prob; 28354a3e9fdbSToby Isaac IS cellIS; 2836f04eb4edSMatthew G. Knepley PetscBool hasJac, hasPrec; 28374a3e9fdbSToby Isaac PetscInt depth; 283824cdb843SMatthew G. Knepley PetscErrorCode ierr; 283924cdb843SMatthew G. Knepley 284024cdb843SMatthew G. Knepley PetscFunctionBegin; 28416da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 28424a3e9fdbSToby Isaac ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2843aeadca18SToby Isaac ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 28449044fa66SMatthew G. Knepley if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);} 2845f04eb4edSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2846f04eb4edSMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2847f04eb4edSMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2848f04eb4edSMatthew G. Knepley if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);} 2849f04eb4edSMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 28504a3e9fdbSToby Isaac ierr = DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 28514a3e9fdbSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 28529a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 285324cdb843SMatthew G. Knepley PetscFunctionReturn(0); 285424cdb843SMatthew G. Knepley } 28551878804aSMatthew G. Knepley 2856a925c78cSMatthew G. Knepley /*@ 28579f520fc2SToby Isaac DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 28589f520fc2SToby Isaac 28599f520fc2SToby Isaac Input Parameters: 28609f520fc2SToby Isaac + dm - The DM object 2861dff059c6SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary()) 28629f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 28639f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 28641a244344SSatish Balay 28651a244344SSatish Balay Level: developer 28669f520fc2SToby Isaac @*/ 28679f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 28689f520fc2SToby Isaac { 28699f520fc2SToby Isaac PetscErrorCode ierr; 28709f520fc2SToby Isaac 28719f520fc2SToby Isaac PetscFunctionBegin; 28729f520fc2SToby Isaac ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 28739f520fc2SToby Isaac ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 28749f520fc2SToby Isaac ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 28759f520fc2SToby Isaac PetscFunctionReturn(0); 28769f520fc2SToby Isaac } 28779f520fc2SToby Isaac 2878e7f46db8SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs) 28791878804aSMatthew G. Knepley { 2880e7f46db8SMatthew G. Knepley PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 2881282e7bb4SMatthew G. Knepley PetscDS prob; 28821878804aSMatthew G. Knepley Mat J, M; 28831878804aSMatthew G. Knepley Vec r, b; 28841878804aSMatthew G. Knepley MatNullSpace nullSpace; 28851878804aSMatthew G. Knepley PetscReal *error, res = 0.0; 28861878804aSMatthew G. Knepley PetscInt numFields; 2887282e7bb4SMatthew G. Knepley PetscBool hasJac, hasPrec; 2888e7f46db8SMatthew G. Knepley PetscInt Nf, f; 28891878804aSMatthew G. Knepley PetscErrorCode ierr; 28901878804aSMatthew G. Knepley 28911878804aSMatthew G. Knepley PetscFunctionBegin; 289244a7f3ddSMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2893e7f46db8SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2894e7f46db8SMatthew G. Knepley ierr = PetscMalloc1(Nf, &exacts);CHKERRQ(ierr); 2895e7f46db8SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(prob, f, &exacts[f]);CHKERRQ(ierr);} 28961878804aSMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 28971878804aSMatthew G. Knepley ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 28981878804aSMatthew G. Knepley /* TODO Null space for J */ 28991878804aSMatthew G. Knepley /* Check discretization error */ 29001878804aSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 29011878804aSMatthew G. Knepley ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr); 2902a5bb2cc9SMatthew G. Knepley ierr = DMProjectFunction(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr); 29031878804aSMatthew G. Knepley if (numFields > 1) { 29041878804aSMatthew G. Knepley PetscInt f; 29051878804aSMatthew G. Knepley 2906e7f46db8SMatthew G. Knepley ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, u, error);CHKERRQ(ierr); 29071878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr); 29081878804aSMatthew G. Knepley for (f = 0; f < numFields; ++f) { 29091878804aSMatthew G. Knepley if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);} 2910e5b268a4SToby Isaac if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", (double)error[f]);CHKERRQ(ierr);} 29111878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);} 29121878804aSMatthew G. Knepley } 29131878804aSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr); 29141878804aSMatthew G. Knepley } else { 2915e7f46db8SMatthew G. Knepley ierr = DMComputeL2Diff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs, u, &error[0]);CHKERRQ(ierr); 2916e5b268a4SToby Isaac if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", (double)error[0]);CHKERRQ(ierr);} 29171878804aSMatthew G. Knepley else {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 29181878804aSMatthew G. Knepley } 29191878804aSMatthew G. Knepley ierr = PetscFree(error);CHKERRQ(ierr); 29201878804aSMatthew G. Knepley /* Check residual */ 29211878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 29221878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2923e5b268a4SToby Isaac ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", (double)res);CHKERRQ(ierr); 29241878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 29251878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2926685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2927685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 29281878804aSMatthew G. Knepley /* Check Jacobian */ 2929282e7bb4SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2930282e7bb4SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2931282e7bb4SMatthew G. Knepley if (hasJac && hasPrec) { 2932282e7bb4SMatthew G. Knepley ierr = DMCreateMatrix(dm, &M);CHKERRQ(ierr); 2933282e7bb4SMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, J, M);CHKERRQ(ierr); 2934282e7bb4SMatthew G. Knepley ierr = PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");CHKERRQ(ierr); 2935282e7bb4SMatthew G. Knepley ierr = MatViewFromOptions(M, NULL, "-mat_view");CHKERRQ(ierr); 2936282e7bb4SMatthew G. Knepley ierr = MatDestroy(&M);CHKERRQ(ierr); 2937282e7bb4SMatthew G. Knepley } else { 2938282e7bb4SMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, J, J);CHKERRQ(ierr); 2939282e7bb4SMatthew G. Knepley } 2940282e7bb4SMatthew G. Knepley ierr = PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");CHKERRQ(ierr); 2941282e7bb4SMatthew G. Knepley ierr = MatViewFromOptions(J, NULL, "-mat_view");CHKERRQ(ierr); 29421878804aSMatthew G. Knepley ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr); 29431878804aSMatthew G. Knepley if (nullSpace) { 29441878804aSMatthew G. Knepley PetscBool isNull; 29451878804aSMatthew G. Knepley ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr); 29461878804aSMatthew G. Knepley if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 29471878804aSMatthew G. Knepley } 29481878804aSMatthew G. Knepley ierr = VecDuplicate(u, &b);CHKERRQ(ierr); 29491878804aSMatthew G. Knepley ierr = VecSet(r, 0.0);CHKERRQ(ierr); 29501878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr); 2951282e7bb4SMatthew G. Knepley ierr = MatMult(J, u, r);CHKERRQ(ierr); 29521878804aSMatthew G. Knepley ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr); 29531878804aSMatthew G. Knepley ierr = VecDestroy(&b);CHKERRQ(ierr); 29541878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2955e5b268a4SToby Isaac ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", (double)res);CHKERRQ(ierr); 29561878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 29571878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr); 2958685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr); 2959685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 29601878804aSMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 29611878804aSMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr); 29621878804aSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 2963e7f46db8SMatthew G. Knepley ierr = PetscFree(exacts);CHKERRQ(ierr); 29641878804aSMatthew G. Knepley PetscFunctionReturn(0); 29651878804aSMatthew G. Knepley } 29661878804aSMatthew G. Knepley 2967bee9a294SMatthew G. Knepley /*@C 2968bee9a294SMatthew G. Knepley DMSNESCheckFromOptions - Check the residual and Jacobian functions using the exact solution by outputting some diagnostic information 2969bee9a294SMatthew G. Knepley 2970bee9a294SMatthew G. Knepley Input Parameters: 2971bee9a294SMatthew G. Knepley + snes - the SNES object 2972bee9a294SMatthew G. Knepley . u - representative SNES vector 2973bee9a294SMatthew G. Knepley . exactFuncs - pointwise functions of the exact solution for each field 2974bee9a294SMatthew G. Knepley - ctxs - contexts for the functions 2975bee9a294SMatthew G. Knepley 2976bee9a294SMatthew G. Knepley Level: developer 2977bee9a294SMatthew G. Knepley @*/ 29780163fd50SMatthew 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) 29791878804aSMatthew G. Knepley { 298077ec165aSMatthew G. Knepley PetscErrorCode (**exact)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *) = NULL; 29811878804aSMatthew G. Knepley DM dm; 298277ec165aSMatthew G. Knepley PetscDS prob; 29831878804aSMatthew G. Knepley Vec sol; 29841878804aSMatthew G. Knepley PetscBool check; 298577ec165aSMatthew G. Knepley PetscInt Nf, f; 29861878804aSMatthew G. Knepley PetscErrorCode ierr; 29871878804aSMatthew G. Knepley 29881878804aSMatthew G. Knepley PetscFunctionBegin; 2989c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 29901878804aSMatthew G. Knepley if (!check) PetscFunctionReturn(0); 29911878804aSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 299277ec165aSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 299377ec165aSMatthew G. Knepley if (!exactFuncs) { 299477ec165aSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 299577ec165aSMatthew G. Knepley ierr = PetscMalloc1(Nf, &exact);CHKERRQ(ierr); 299677ec165aSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(prob, f, &exact[f]);CHKERRQ(ierr);} 299777ec165aSMatthew G. Knepley } 29981878804aSMatthew G. Knepley ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 29991878804aSMatthew G. Knepley ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 300077ec165aSMatthew G. Knepley ierr = DMSNESCheckFromOptions_Internal(snes, dm, sol, exactFuncs ? exactFuncs : exact, ctxs);CHKERRQ(ierr); 30011878804aSMatthew G. Knepley ierr = VecDestroy(&sol);CHKERRQ(ierr); 300277ec165aSMatthew G. Knepley ierr = PetscFree(exact);CHKERRQ(ierr); 3003552f7358SJed Brown PetscFunctionReturn(0); 3004552f7358SJed Brown } 3005