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 46d083f849SBarry Smith Collective 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; 92ff1e0c32SBarry 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; 136ff1e0c32SBarry 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); 190580bdb30SBarry Smith ierr = PetscArraycpy(ctx->points, points, n*ctx->dim);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); 411ff1e0c32SBarry Smith 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); 448ff1e0c32SBarry Smith 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; 547ef0bb6c7SMatthew G. Knepley PetscTabulation T; 54856044e6dSMatthew G. Knepley const PetscScalar *coords; 54956044e6dSMatthew G. Knepley PetscScalar *a; 550ef0bb6c7SMatthew G. Knepley PetscReal xir[2]; 551de73a395SMatthew G. Knepley PetscInt Nf, p; 5525509d985SMatthew G. Knepley const PetscInt dof = ctx->dof; 553552f7358SJed Brown PetscErrorCode ierr; 554552f7358SJed Brown 555552f7358SJed Brown PetscFunctionBegin; 556de73a395SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 55744a7f3ddSMatthew G. Knepley if (Nf) {ierr = DMGetField(dm, 0, NULL, (PetscObject *) &fem);CHKERRQ(ierr);} 558552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 559fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 560552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 561552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr); 562552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 563552f7358SJed Brown ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr); 564c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 565552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 566552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 567552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 568552f7358SJed Brown ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr); 569552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 570552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 5710298fd71SBarry Smith ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr); 5720298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr); 573552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 574552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 575552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 576552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 577552f7358SJed Brown 57856044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 579552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 580ef0bb6c7SMatthew G. Knepley ierr = PetscFECreateTabulation(fem, 1, 1, xir, 0, &T);CHKERRQ(ierr); 581552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 582a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 583552f7358SJed Brown PetscScalar *xi; 584552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 585552f7358SJed Brown 586552f7358SJed Brown /* Can make this do all points at once */ 5870298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 588ff1e0c32SBarry Smith if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %d", coordSize, 4*2); 5890298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 5900298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 5910298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 592552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 593552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 594552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 595552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 596552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 597552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 598cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 599cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 6005509d985SMatthew G. Knepley if (4*dof != xSize) { 6015509d985SMatthew G. Knepley PetscInt d; 6021aa26658SKarl Rupp 6035509d985SMatthew G. Knepley xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0; 604ef0bb6c7SMatthew G. Knepley ierr = PetscFEComputeTabulation(fem, 1, xir, 0, T);CHKERRQ(ierr); 6055509d985SMatthew G. Knepley for (comp = 0; comp < dof; ++comp) { 6065509d985SMatthew G. Knepley a[p*dof+comp] = 0.0; 6075509d985SMatthew G. Knepley for (d = 0; d < xSize/dof; ++d) { 608ef0bb6c7SMatthew G. Knepley a[p*dof+comp] += x[d*dof+comp]*T->T[0][d*dof+comp]; 6095509d985SMatthew G. Knepley } 6105509d985SMatthew G. Knepley } 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 } 619ef0bb6c7SMatthew G. Knepley ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr); 620552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 62156044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 622552f7358SJed Brown 623552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 624552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 625552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 626552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 627552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 628552f7358SJed Brown PetscFunctionReturn(0); 629552f7358SJed Brown } 630552f7358SJed Brown 6315820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx) 632552f7358SJed Brown { 633552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 634552f7358SJed Brown const PetscScalar x0 = vertices[0]; 635552f7358SJed Brown const PetscScalar y0 = vertices[1]; 636552f7358SJed Brown const PetscScalar z0 = vertices[2]; 6377a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 6387a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 6397a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 640552f7358SJed Brown const PetscScalar x2 = vertices[6]; 641552f7358SJed Brown const PetscScalar y2 = vertices[7]; 642552f7358SJed Brown const PetscScalar z2 = vertices[8]; 6437a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 6447a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 6457a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 646552f7358SJed Brown const PetscScalar x4 = vertices[12]; 647552f7358SJed Brown const PetscScalar y4 = vertices[13]; 648552f7358SJed Brown const PetscScalar z4 = vertices[14]; 649552f7358SJed Brown const PetscScalar x5 = vertices[15]; 650552f7358SJed Brown const PetscScalar y5 = vertices[16]; 651552f7358SJed Brown const PetscScalar z5 = vertices[17]; 652552f7358SJed Brown const PetscScalar x6 = vertices[18]; 653552f7358SJed Brown const PetscScalar y6 = vertices[19]; 654552f7358SJed Brown const PetscScalar z6 = vertices[20]; 655552f7358SJed Brown const PetscScalar x7 = vertices[21]; 656552f7358SJed Brown const PetscScalar y7 = vertices[22]; 657552f7358SJed Brown const PetscScalar z7 = vertices[23]; 658552f7358SJed Brown const PetscScalar f_1 = x1 - x0; 659552f7358SJed Brown const PetscScalar g_1 = y1 - y0; 660552f7358SJed Brown const PetscScalar h_1 = z1 - z0; 661552f7358SJed Brown const PetscScalar f_3 = x3 - x0; 662552f7358SJed Brown const PetscScalar g_3 = y3 - y0; 663552f7358SJed Brown const PetscScalar h_3 = z3 - z0; 664552f7358SJed Brown const PetscScalar f_4 = x4 - x0; 665552f7358SJed Brown const PetscScalar g_4 = y4 - y0; 666552f7358SJed Brown const PetscScalar h_4 = z4 - z0; 667552f7358SJed Brown const PetscScalar f_01 = x2 - x1 - x3 + x0; 668552f7358SJed Brown const PetscScalar g_01 = y2 - y1 - y3 + y0; 669552f7358SJed Brown const PetscScalar h_01 = z2 - z1 - z3 + z0; 670552f7358SJed Brown const PetscScalar f_12 = x7 - x3 - x4 + x0; 671552f7358SJed Brown const PetscScalar g_12 = y7 - y3 - y4 + y0; 672552f7358SJed Brown const PetscScalar h_12 = z7 - z3 - z4 + z0; 673552f7358SJed Brown const PetscScalar f_02 = x5 - x1 - x4 + x0; 674552f7358SJed Brown const PetscScalar g_02 = y5 - y1 - y4 + y0; 675552f7358SJed Brown const PetscScalar h_02 = z5 - z1 - z4 + z0; 676552f7358SJed Brown const PetscScalar f_012 = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 677552f7358SJed Brown const PetscScalar g_012 = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 678552f7358SJed Brown const PetscScalar h_012 = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 67956044e6dSMatthew G. Knepley const PetscScalar *ref; 68056044e6dSMatthew G. Knepley PetscScalar *real; 681552f7358SJed Brown PetscErrorCode ierr; 682552f7358SJed Brown 683552f7358SJed Brown PetscFunctionBegin; 68456044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 685552f7358SJed Brown ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr); 686552f7358SJed Brown { 687552f7358SJed Brown const PetscScalar p0 = ref[0]; 688552f7358SJed Brown const PetscScalar p1 = ref[1]; 689552f7358SJed Brown const PetscScalar p2 = ref[2]; 690552f7358SJed Brown 691552f7358SJed 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; 692552f7358SJed 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; 693552f7358SJed 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; 694552f7358SJed Brown } 695552f7358SJed Brown ierr = PetscLogFlops(114);CHKERRQ(ierr); 69656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 697552f7358SJed Brown ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr); 698552f7358SJed Brown PetscFunctionReturn(0); 699552f7358SJed Brown } 700552f7358SJed Brown 701d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx) 702552f7358SJed Brown { 703552f7358SJed Brown const PetscScalar *vertices = (const PetscScalar*) ctx; 704552f7358SJed Brown const PetscScalar x0 = vertices[0]; 705552f7358SJed Brown const PetscScalar y0 = vertices[1]; 706552f7358SJed Brown const PetscScalar z0 = vertices[2]; 7077a1931ceSMatthew G. Knepley const PetscScalar x1 = vertices[9]; 7087a1931ceSMatthew G. Knepley const PetscScalar y1 = vertices[10]; 7097a1931ceSMatthew G. Knepley const PetscScalar z1 = vertices[11]; 710552f7358SJed Brown const PetscScalar x2 = vertices[6]; 711552f7358SJed Brown const PetscScalar y2 = vertices[7]; 712552f7358SJed Brown const PetscScalar z2 = vertices[8]; 7137a1931ceSMatthew G. Knepley const PetscScalar x3 = vertices[3]; 7147a1931ceSMatthew G. Knepley const PetscScalar y3 = vertices[4]; 7157a1931ceSMatthew G. Knepley const PetscScalar z3 = vertices[5]; 716552f7358SJed Brown const PetscScalar x4 = vertices[12]; 717552f7358SJed Brown const PetscScalar y4 = vertices[13]; 718552f7358SJed Brown const PetscScalar z4 = vertices[14]; 719552f7358SJed Brown const PetscScalar x5 = vertices[15]; 720552f7358SJed Brown const PetscScalar y5 = vertices[16]; 721552f7358SJed Brown const PetscScalar z5 = vertices[17]; 722552f7358SJed Brown const PetscScalar x6 = vertices[18]; 723552f7358SJed Brown const PetscScalar y6 = vertices[19]; 724552f7358SJed Brown const PetscScalar z6 = vertices[20]; 725552f7358SJed Brown const PetscScalar x7 = vertices[21]; 726552f7358SJed Brown const PetscScalar y7 = vertices[22]; 727552f7358SJed Brown const PetscScalar z7 = vertices[23]; 728552f7358SJed Brown const PetscScalar f_xy = x2 - x1 - x3 + x0; 729552f7358SJed Brown const PetscScalar g_xy = y2 - y1 - y3 + y0; 730552f7358SJed Brown const PetscScalar h_xy = z2 - z1 - z3 + z0; 731552f7358SJed Brown const PetscScalar f_yz = x7 - x3 - x4 + x0; 732552f7358SJed Brown const PetscScalar g_yz = y7 - y3 - y4 + y0; 733552f7358SJed Brown const PetscScalar h_yz = z7 - z3 - z4 + z0; 734552f7358SJed Brown const PetscScalar f_xz = x5 - x1 - x4 + x0; 735552f7358SJed Brown const PetscScalar g_xz = y5 - y1 - y4 + y0; 736552f7358SJed Brown const PetscScalar h_xz = z5 - z1 - z4 + z0; 737552f7358SJed Brown const PetscScalar f_xyz = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7; 738552f7358SJed Brown const PetscScalar g_xyz = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7; 739552f7358SJed Brown const PetscScalar h_xyz = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7; 74056044e6dSMatthew G. Knepley const PetscScalar *ref; 741552f7358SJed Brown PetscErrorCode ierr; 742552f7358SJed Brown 743552f7358SJed Brown PetscFunctionBegin; 74456044e6dSMatthew G. Knepley ierr = VecGetArrayRead(Xref, &ref);CHKERRQ(ierr); 745552f7358SJed Brown { 746552f7358SJed Brown const PetscScalar x = ref[0]; 747552f7358SJed Brown const PetscScalar y = ref[1]; 748552f7358SJed Brown const PetscScalar z = ref[2]; 749552f7358SJed Brown const PetscInt rows[3] = {0, 1, 2}; 750da80777bSKarl Rupp PetscScalar values[9]; 751da80777bSKarl Rupp 752da80777bSKarl Rupp values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0; 753da80777bSKarl Rupp values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0; 754da80777bSKarl Rupp values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0; 755da80777bSKarl Rupp values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0; 756da80777bSKarl Rupp values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0; 757da80777bSKarl Rupp values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0; 758da80777bSKarl Rupp values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0; 759da80777bSKarl Rupp values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0; 760da80777bSKarl Rupp values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0; 7611aa26658SKarl Rupp 76294ab13aaSBarry Smith ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr); 763552f7358SJed Brown } 764552f7358SJed Brown ierr = PetscLogFlops(152);CHKERRQ(ierr); 76556044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(Xref, &ref);CHKERRQ(ierr); 76694ab13aaSBarry Smith ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 76794ab13aaSBarry Smith ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 768552f7358SJed Brown PetscFunctionReturn(0); 769552f7358SJed Brown } 770552f7358SJed Brown 771a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v) 772a6dfd86eSKarl Rupp { 773fafc0619SMatthew G Knepley DM dmCoord; 774552f7358SJed Brown SNES snes; 775552f7358SJed Brown KSP ksp; 776552f7358SJed Brown PC pc; 777552f7358SJed Brown Vec coordsLocal, r, ref, real; 778552f7358SJed Brown Mat J; 77956044e6dSMatthew G. Knepley const PetscScalar *coords; 78056044e6dSMatthew G. Knepley PetscScalar *a; 781552f7358SJed Brown PetscInt p; 782552f7358SJed Brown PetscErrorCode ierr; 783552f7358SJed Brown 784552f7358SJed Brown PetscFunctionBegin; 785552f7358SJed Brown ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr); 786fafc0619SMatthew G Knepley ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr); 787552f7358SJed Brown ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr); 788552f7358SJed Brown ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr); 789552f7358SJed Brown ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr); 790552f7358SJed Brown ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr); 791c0dedaeaSBarry Smith ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr); 792552f7358SJed Brown ierr = VecDuplicate(r, &ref);CHKERRQ(ierr); 793552f7358SJed Brown ierr = VecDuplicate(r, &real);CHKERRQ(ierr); 794552f7358SJed Brown ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr); 795552f7358SJed Brown ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr); 796552f7358SJed Brown ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr); 797552f7358SJed Brown ierr = MatSetUp(J);CHKERRQ(ierr); 7980298fd71SBarry Smith ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr); 7990298fd71SBarry Smith ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr); 800552f7358SJed Brown ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 801552f7358SJed Brown ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 802552f7358SJed Brown ierr = PCSetType(pc, PCLU);CHKERRQ(ierr); 803552f7358SJed Brown ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 804552f7358SJed Brown 80556044e6dSMatthew G. Knepley ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 806552f7358SJed Brown ierr = VecGetArray(v, &a);CHKERRQ(ierr); 807552f7358SJed Brown for (p = 0; p < ctx->n; ++p) { 808a1e44745SMatthew G. Knepley PetscScalar *x = NULL, *vertices = NULL; 809552f7358SJed Brown PetscScalar *xi; 810cb313848SJed Brown PetscReal xir[3]; 811552f7358SJed Brown PetscInt c = ctx->cells[p], comp, coordSize, xSize; 812552f7358SJed Brown 813552f7358SJed Brown /* Can make this do all points at once */ 8140298fd71SBarry Smith ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 815ff1e0c32SBarry Smith if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %d", coordSize, 8*3); 8160298fd71SBarry Smith ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 817ff1e0c32SBarry Smith if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %D should be %D", xSize, 8*ctx->dof); 8180298fd71SBarry Smith ierr = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 8190298fd71SBarry Smith ierr = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr); 820552f7358SJed Brown ierr = VecGetArray(real, &xi);CHKERRQ(ierr); 821552f7358SJed Brown xi[0] = coords[p*ctx->dim+0]; 822552f7358SJed Brown xi[1] = coords[p*ctx->dim+1]; 823552f7358SJed Brown xi[2] = coords[p*ctx->dim+2]; 824552f7358SJed Brown ierr = VecRestoreArray(real, &xi);CHKERRQ(ierr); 825552f7358SJed Brown ierr = SNESSolve(snes, real, ref);CHKERRQ(ierr); 826552f7358SJed Brown ierr = VecGetArray(ref, &xi);CHKERRQ(ierr); 827cb313848SJed Brown xir[0] = PetscRealPart(xi[0]); 828cb313848SJed Brown xir[1] = PetscRealPart(xi[1]); 829cb313848SJed Brown xir[2] = PetscRealPart(xi[2]); 830552f7358SJed Brown for (comp = 0; comp < ctx->dof; ++comp) { 831552f7358SJed Brown a[p*ctx->dof+comp] = 832cb313848SJed Brown x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) + 8337a1931ceSMatthew G. Knepley x[3*ctx->dof+comp]* xir[0]*(1-xir[1])*(1-xir[2]) + 834cb313848SJed Brown x[2*ctx->dof+comp]* xir[0]* xir[1]*(1-xir[2]) + 8357a1931ceSMatthew G. Knepley x[1*ctx->dof+comp]*(1-xir[0])* xir[1]*(1-xir[2]) + 836cb313848SJed Brown x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])* xir[2] + 837cb313848SJed Brown x[5*ctx->dof+comp]* xir[0]*(1-xir[1])* xir[2] + 838cb313848SJed Brown x[6*ctx->dof+comp]* xir[0]* xir[1]* xir[2] + 839cb313848SJed Brown x[7*ctx->dof+comp]*(1-xir[0])* xir[1]* xir[2]; 840552f7358SJed Brown } 841552f7358SJed Brown ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr); 8420298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr); 8430298fd71SBarry Smith ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr); 844552f7358SJed Brown } 845552f7358SJed Brown ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 84656044e6dSMatthew G. Knepley ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr); 847552f7358SJed Brown 848552f7358SJed Brown ierr = SNESDestroy(&snes);CHKERRQ(ierr); 849552f7358SJed Brown ierr = VecDestroy(&r);CHKERRQ(ierr); 850552f7358SJed Brown ierr = VecDestroy(&ref);CHKERRQ(ierr); 851552f7358SJed Brown ierr = VecDestroy(&real);CHKERRQ(ierr); 852552f7358SJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 853552f7358SJed Brown PetscFunctionReturn(0); 854552f7358SJed Brown } 855552f7358SJed Brown 8564267b1a3SMatthew G. Knepley /*@C 8574267b1a3SMatthew G. Knepley DMInterpolationEvaluate - Using the input from dm and x, calculates interpolated field values at the interpolation points. 8584267b1a3SMatthew G. Knepley 859552f7358SJed Brown Input Parameters: 860552f7358SJed Brown + ctx - The DMInterpolationInfo context 861552f7358SJed Brown . dm - The DM 862552f7358SJed Brown - x - The local vector containing the field to be interpolated 863552f7358SJed Brown 864552f7358SJed Brown Output Parameters: 865552f7358SJed Brown . v - The vector containing the interpolated values 8664267b1a3SMatthew G. Knepley 8674267b1a3SMatthew G. Knepley Note: A suitable v can be obtained using DMInterpolationGetVector(). 8684267b1a3SMatthew G. Knepley 8694267b1a3SMatthew G. Knepley Level: beginner 8704267b1a3SMatthew G. Knepley 8714267b1a3SMatthew G. Knepley .seealso: DMInterpolationGetVector(), DMInterpolationAddPoints(), DMInterpolationCreate() 8724267b1a3SMatthew G. Knepley @*/ 8730adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v) 8740adebc6cSBarry Smith { 875552f7358SJed Brown PetscInt dim, coneSize, n; 876552f7358SJed Brown PetscErrorCode ierr; 877552f7358SJed Brown 878552f7358SJed Brown PetscFunctionBegin; 879552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 880552f7358SJed Brown PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 881552f7358SJed Brown PetscValidHeaderSpecific(v, VEC_CLASSID, 4); 882552f7358SJed Brown ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 883ff1e0c32SBarry 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); 884552f7358SJed Brown if (n) { 885c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 886552f7358SJed Brown ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr); 887552f7358SJed Brown if (dim == 2) { 888552f7358SJed Brown if (coneSize == 3) { 8897a1931ceSMatthew G. Knepley ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr); 890552f7358SJed Brown } else if (coneSize == 4) { 891552f7358SJed Brown ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr); 892ff1e0c32SBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %D for point interpolation", dim); 893552f7358SJed Brown } else if (dim == 3) { 894552f7358SJed Brown if (coneSize == 4) { 8957a1931ceSMatthew G. Knepley ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr); 896552f7358SJed Brown } else { 897552f7358SJed Brown ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr); 898552f7358SJed Brown } 899ff1e0c32SBarry Smith } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %D for point interpolation", dim); 900552f7358SJed Brown } 901552f7358SJed Brown PetscFunctionReturn(0); 902552f7358SJed Brown } 903552f7358SJed Brown 9044267b1a3SMatthew G. Knepley /*@C 9054267b1a3SMatthew G. Knepley DMInterpolationDestroy - Destroys a DMInterpolationInfo context 9064267b1a3SMatthew G. Knepley 9074267b1a3SMatthew G. Knepley Collective on ctx 9084267b1a3SMatthew G. Knepley 9094267b1a3SMatthew G. Knepley Input Parameter: 9104267b1a3SMatthew G. Knepley . ctx - the context 9114267b1a3SMatthew G. Knepley 9124267b1a3SMatthew G. Knepley Level: beginner 9134267b1a3SMatthew G. Knepley 9144267b1a3SMatthew G. Knepley .seealso: DMInterpolationEvaluate(), DMInterpolationAddPoints(), DMInterpolationCreate() 9154267b1a3SMatthew G. Knepley @*/ 9160adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx) 9170adebc6cSBarry Smith { 918552f7358SJed Brown PetscErrorCode ierr; 919552f7358SJed Brown 920552f7358SJed Brown PetscFunctionBegin; 921552f7358SJed Brown PetscValidPointer(ctx, 2); 922552f7358SJed Brown ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr); 923552f7358SJed Brown ierr = PetscFree((*ctx)->points);CHKERRQ(ierr); 924552f7358SJed Brown ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr); 925552f7358SJed Brown ierr = PetscFree(*ctx);CHKERRQ(ierr); 9260298fd71SBarry Smith *ctx = NULL; 927552f7358SJed Brown PetscFunctionReturn(0); 928552f7358SJed Brown } 929cc0c4584SMatthew G. Knepley 930cc0c4584SMatthew G. Knepley /*@C 931cc0c4584SMatthew G. Knepley SNESMonitorFields - Monitors the residual for each field separately 932cc0c4584SMatthew G. Knepley 933cc0c4584SMatthew G. Knepley Collective on SNES 934cc0c4584SMatthew G. Knepley 935cc0c4584SMatthew G. Knepley Input Parameters: 936cc0c4584SMatthew G. Knepley + snes - the SNES context 937cc0c4584SMatthew G. Knepley . its - iteration number 938cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual 939d43b4f6eSBarry Smith - vf - PetscViewerAndFormat of type ASCII 940cc0c4584SMatthew G. Knepley 941cc0c4584SMatthew G. Knepley Notes: 942cc0c4584SMatthew G. Knepley This routine prints the residual norm at each iteration. 943cc0c4584SMatthew G. Knepley 944cc0c4584SMatthew G. Knepley Level: intermediate 945cc0c4584SMatthew G. Knepley 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); 9619e5d0892SLisandro Dalcin ierr = SNESGetFunction(snes, &res, NULL, NULL);CHKERRQ(ierr); 962cc0c4584SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 96392fd8e1eSJed Brown ierr = DMGetLocalSection(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 1057c330f8ffSToby 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) 105808449791SMatthew G. Knepley { 105908449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1060023fee6eSMatthew G. Knepley DM plex = NULL, plexA = NULL; 1061a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 10624236e4b7SMatthew G. Knepley PetscDS prob, probAux = NULL; 10634236e4b7SMatthew G. Knepley PetscSection section, sectionAux = NULL; 10642d91c981SSander Arens Vec locA = NULL; 10652d91c981SSander Arens PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL; 10664236e4b7SMatthew G. Knepley PetscInt v; 1067c330f8ffSToby Isaac PetscInt totDim, totDimAux = 0; 106808449791SMatthew G. Knepley PetscErrorCode ierr; 106908449791SMatthew G. Knepley 107008449791SMatthew G. Knepley PetscFunctionBegin; 1071023fee6eSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 107292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 107308449791SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 10744d0b9603SSander Arens ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 10752d91c981SSander Arens ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 10762d91c981SSander Arens if (locA) { 10774236e4b7SMatthew G. Knepley DM dmAux; 10784236e4b7SMatthew G. Knepley 10792d91c981SSander Arens ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1080a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 1081023fee6eSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 1082023fee6eSMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 10834d0b9603SSander Arens ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 108492fd8e1eSJed Brown ierr = DMGetLocalSection(plexA, §ionAux);CHKERRQ(ierr); 10852d91c981SSander Arens } 10864236e4b7SMatthew G. Knepley for (v = 0; v < numValues; ++v) { 108751e9edccSMatthew G. Knepley PetscFEGeom *fgeom; 1088b7260050SToby Isaac PetscInt maxDegree; 1089c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 109024cdb843SMatthew G. Knepley IS pointIS; 109124cdb843SMatthew G. Knepley const PetscInt *points; 1092f74ed4f0SToby Isaac PetscInt numFaces, face, Nq; 109324cdb843SMatthew G. Knepley 1094a8e83e26SSanderA ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 109522734eb1Ssarens if (!pointIS) continue; /* No points with that id on this process */ 1096c330f8ffSToby Isaac { 1097c330f8ffSToby Isaac IS isectIS; 1098c330f8ffSToby Isaac 10994a3e9fdbSToby Isaac /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 110000199b93SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 1101c330f8ffSToby Isaac ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1102c330f8ffSToby Isaac pointIS = isectIS; 110324cdb843SMatthew G. Knepley } 1104c330f8ffSToby Isaac ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr); 1105c330f8ffSToby Isaac ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr); 1106c330f8ffSToby Isaac ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 1107b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 1108b7260050SToby Isaac if (maxDegree <= 1) { 1109c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 1110c330f8ffSToby Isaac } 1111c330f8ffSToby Isaac if (!qGeom) { 1112c330f8ffSToby Isaac PetscFE fe; 1113c330f8ffSToby Isaac 1114c330f8ffSToby Isaac ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1115c330f8ffSToby Isaac ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 1116c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 1117c330f8ffSToby Isaac } 1118c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 11194a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1120c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1121c330f8ffSToby Isaac const PetscInt point = points[face], *support, *cone; 112224cdb843SMatthew G. Knepley PetscScalar *x = NULL; 11239bfb2fe4SJed Brown PetscInt i, coneSize, faceLoc; 112424cdb843SMatthew G. Knepley 11254d0b9603SSander Arens ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 11264d0b9603SSander Arens ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 11274d0b9603SSander Arens ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 11284d0b9603SSander Arens for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 11294236e4b7SMatthew 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]); 1130c330f8ffSToby Isaac fgeom->face[face][0] = faceLoc; 113151e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 11324d0b9603SSander Arens for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 1133023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 113408449791SMatthew G. Knepley if (locX_t) { 1135023fee6eSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 11364d0b9603SSander Arens for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 1137023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 113824cdb843SMatthew G. Knepley } 11392d91c981SSander Arens if (locA) { 114080dc27feSMatthew G. Knepley PetscInt subp; 114106adec37SMatthew G. Knepley 1142a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr); 1143023fee6eSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 11444d0b9603SSander Arens for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 1145023fee6eSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 114624cdb843SMatthew G. Knepley } 114724cdb843SMatthew G. Knepley } 1148580bdb30SBarry Smith ierr = PetscArrayzero(elemVec, numFaces*totDim);CHKERRQ(ierr); 1149cbf52bb1SSander Arens { 115024cdb843SMatthew G. Knepley PetscFE fe; 1151f74ed4f0SToby Isaac PetscInt Nb; 1152c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 115324cdb843SMatthew G. Knepley /* Conforming batches */ 115424cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 115524cdb843SMatthew G. Knepley /* Remainder */ 115624cdb843SMatthew G. Knepley PetscInt Nr, offset; 115724cdb843SMatthew G. Knepley 11584d0b9603SSander Arens ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 115924cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 116024cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1161c330f8ffSToby Isaac /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */ 1162c330f8ffSToby Isaac blockSize = Nb; 116324cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 116424cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 116524cdb843SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 116624cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 116724cdb843SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 116824cdb843SMatthew G. Knepley offset = numFaces - Nr; 1169c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 11704bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 1171a57bfeafSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr); 1172c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 11734bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(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); 1174c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 117524cdb843SMatthew G. Knepley } 1176c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1177c330f8ffSToby Isaac const PetscInt point = points[face], *support; 117824cdb843SMatthew G. Knepley 11794d0b9603SSander Arens if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);} 1180023fee6eSMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 1181023fee6eSMatthew G. Knepley ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 118224cdb843SMatthew G. Knepley } 11834a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1184c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 118524cdb843SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 118624cdb843SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1187c330f8ffSToby Isaac ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr); 118824cdb843SMatthew G. Knepley } 1189023fee6eSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 1190023fee6eSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 11914236e4b7SMatthew G. Knepley PetscFunctionReturn(0); 1192a8e83e26SSanderA } 11934236e4b7SMatthew G. Knepley 1194317e5c34SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF) 1195317e5c34SMatthew G. Knepley { 1196317e5c34SMatthew G. Knepley DMField coordField; 1197317e5c34SMatthew G. Knepley DMLabel depthLabel; 1198317e5c34SMatthew G. Knepley IS facetIS; 1199317e5c34SMatthew G. Knepley PetscInt dim; 1200317e5c34SMatthew G. Knepley PetscErrorCode ierr; 1201317e5c34SMatthew G. Knepley 1202317e5c34SMatthew G. Knepley PetscFunctionBegin; 1203317e5c34SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1204317e5c34SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1205317e5c34SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 1206317e5c34SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1207a79ed8ccSMatthew G. Knepley ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 1208bb72f97dSMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 1209317e5c34SMatthew G. Knepley PetscFunctionReturn(0); 1210317e5c34SMatthew G. Knepley } 1211317e5c34SMatthew G. Knepley 12124236e4b7SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 12134236e4b7SMatthew G. Knepley { 12144236e4b7SMatthew G. Knepley PetscDS prob; 121533badb77SMatthew G. Knepley PetscInt numBd, bd; 1216c330f8ffSToby Isaac DMField coordField = NULL; 121733badb77SMatthew G. Knepley IS facetIS = NULL; 1218e5e52638SMatthew G. Knepley DMLabel depthLabel; 1219e5e52638SMatthew G. Knepley PetscInt dim; 12204236e4b7SMatthew G. Knepley PetscErrorCode ierr; 12214236e4b7SMatthew G. Knepley 12224236e4b7SMatthew G. Knepley PetscFunctionBegin; 12234236e4b7SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1224e5e52638SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1225e5e52638SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1226e5e52638SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr); 12274236e4b7SMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 12284236e4b7SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 12294236e4b7SMatthew G. Knepley DMBoundaryConditionType type; 12304236e4b7SMatthew G. Knepley const char *bdLabel; 12314236e4b7SMatthew G. Knepley DMLabel label; 12324236e4b7SMatthew G. Knepley const PetscInt *values; 12334236e4b7SMatthew G. Knepley PetscInt field, numValues; 12344236e4b7SMatthew G. Knepley PetscObject obj; 12354236e4b7SMatthew G. Knepley PetscClassId id; 12364236e4b7SMatthew G. Knepley 12374236e4b7SMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 12384236e4b7SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 12394236e4b7SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 12404236e4b7SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 124133badb77SMatthew G. Knepley if (!facetIS) { 124233badb77SMatthew G. Knepley DMLabel depthLabel; 124333badb77SMatthew G. Knepley PetscInt dim; 124433badb77SMatthew G. Knepley 124533badb77SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 124633badb77SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 124733badb77SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr); 124833badb77SMatthew G. Knepley } 1249e5e52638SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 12504236e4b7SMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 1251c330f8ffSToby Isaac ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 12524236e4b7SMatthew G. Knepley } 1253c330f8ffSToby Isaac ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 125408449791SMatthew G. Knepley PetscFunctionReturn(0); 125508449791SMatthew G. Knepley } 125608449791SMatthew G. Knepley 12574a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 125808449791SMatthew G. Knepley { 125908449791SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 126008449791SMatthew G. Knepley const char *name = "Residual"; 126108449791SMatthew G. Knepley DM dmAux = NULL; 126208449791SMatthew G. Knepley DM dmGrad = NULL; 126308449791SMatthew G. Knepley DMLabel ghostLabel = NULL; 126408449791SMatthew G. Knepley PetscDS prob = NULL; 126508449791SMatthew G. Knepley PetscDS probAux = NULL; 126608449791SMatthew G. Knepley PetscSection section = NULL; 126708449791SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 126808449791SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 1269b2666ceaSMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 127008449791SMatthew G. Knepley PetscFV fvm = NULL; 127108449791SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 127208449791SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 1273c330f8ffSToby Isaac DMField coordField = NULL; 1274c330f8ffSToby Isaac Vec locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 12753755293bSMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a, *uL, *uR; 12769044fa66SMatthew G. Knepley IS chunkIS; 12779044fa66SMatthew G. Knepley const PetscInt *cells; 12789044fa66SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 1279c4d4a4f8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 1280b7260050SToby Isaac PetscInt maxDegree = PETSC_MAX_INT; 12814a3e9fdbSToby Isaac PetscQuadrature affineQuad = NULL, *quads = NULL; 12824a3e9fdbSToby Isaac PetscFEGeom *affineGeom = NULL, **geoms = NULL; 128308449791SMatthew G. Knepley PetscErrorCode ierr; 128408449791SMatthew G. Knepley 128508449791SMatthew G. Knepley PetscFunctionBegin; 128608449791SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 128708449791SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1288195142f5SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 128908449791SMatthew G. Knepley /* FEM+FVM */ 1290e5e52638SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1291e5e52638SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 129208449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 129392fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 1294c58f1c22SToby Isaac ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1295e5e52638SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 129608449791SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 129708449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 129808449791SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 129908449791SMatthew G. Knepley if (locA) { 130044171101SMatthew G. Knepley PetscInt subcell; 130108449791SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1302a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);CHKERRQ(ierr); 130344171101SMatthew G. Knepley ierr = DMGetCellDS(dmAux, subcell, &probAux);CHKERRQ(ierr); 130408449791SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 130508449791SMatthew G. Knepley } 130608449791SMatthew G. Knepley /* 2: Get geometric data */ 130708449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 130808449791SMatthew G. Knepley PetscObject obj; 130908449791SMatthew G. Knepley PetscClassId id; 13107173168dSMatthew G. Knepley PetscBool fimp; 131108449791SMatthew G. Knepley 13127173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 13137173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 131408449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 131508449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 131608449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 131708449791SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 131808449791SMatthew G. Knepley } 131908449791SMatthew G. Knepley if (useFEM) { 13204a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1321b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 1322b7260050SToby Isaac if (maxDegree <= 1) { 13234a3e9fdbSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1324741db25dSToby Isaac if (affineQuad) { 13254a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 1326741db25dSToby Isaac } 13274a3e9fdbSToby Isaac } else { 13284a3e9fdbSToby Isaac ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 13294a3e9fdbSToby Isaac for (f = 0; f < Nf; ++f) { 13304a3e9fdbSToby Isaac PetscObject obj; 13314a3e9fdbSToby Isaac PetscClassId id; 13324a3e9fdbSToby Isaac PetscBool fimp; 13332f84e9bcSToby Isaac 13344a3e9fdbSToby Isaac ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 13354a3e9fdbSToby Isaac if (isImplicit != fimp) continue; 13364a3e9fdbSToby Isaac ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 13374a3e9fdbSToby Isaac ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 13384a3e9fdbSToby Isaac if (id == PETSCFE_CLASSID) { 13394a3e9fdbSToby Isaac PetscFE fe = (PetscFE) obj; 13402f84e9bcSToby Isaac 13414a3e9fdbSToby Isaac ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 13424a3e9fdbSToby Isaac ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr); 13434a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 13442f84e9bcSToby Isaac } 13452f84e9bcSToby Isaac } 13462f84e9bcSToby Isaac } 134708449791SMatthew G. Knepley } 134808449791SMatthew G. Knepley if (useFVM) { 134908449791SMatthew G. Knepley ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 135008449791SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 135108449791SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 135208449791SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 135308449791SMatthew G. Knepley ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 135408449791SMatthew G. Knepley if (dmGrad) { 135508449791SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 135608449791SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1357de555695SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 135808449791SMatthew G. Knepley /* Communicate gradient values */ 135908449791SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 136008449791SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 136108449791SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 136208449791SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 136308449791SMatthew G. Knepley } 1364bdd6f66aSToby Isaac /* Handle non-essential (e.g. outflow) boundary values */ 1365bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 136608449791SMatthew G. Knepley } 136708449791SMatthew G. Knepley /* Loop over chunks */ 13689044fa66SMatthew G. Knepley if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);} 13699044fa66SMatthew G. Knepley numCells = cEnd - cStart; 137008449791SMatthew G. Knepley numChunks = 1; 13714a3e9fdbSToby Isaac cellChunkSize = numCells/numChunks; 137208449791SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 1373741db25dSToby Isaac numChunks = PetscMin(1,numCells); 137408449791SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 13752eefff9cSMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 13762eefff9cSMatthew G. Knepley PetscReal *vol; 137708449791SMatthew G. Knepley PetscFVFaceGeom *fgeom; 13789044fa66SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 13793755293bSMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face; 138008449791SMatthew G. Knepley 138108449791SMatthew G. Knepley /* Extract field coefficients */ 138208449791SMatthew G. Knepley if (useFEM) { 13839044fa66SMatthew G. Knepley ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr); 13849044fa66SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 138569291d52SBarry Smith ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1386580bdb30SBarry Smith ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr); 138708449791SMatthew G. Knepley } 138808449791SMatthew G. Knepley if (useFVM) { 13895f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 13905f942ad5SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 139169291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 139269291d52SBarry Smith ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 1393580bdb30SBarry Smith ierr = PetscArrayzero(fluxL, numFaces*totDim);CHKERRQ(ierr); 1394580bdb30SBarry Smith ierr = PetscArrayzero(fluxR, numFaces*totDim);CHKERRQ(ierr); 139508449791SMatthew G. Knepley } 139608449791SMatthew 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 */ 139708449791SMatthew G. Knepley /* Loop over fields */ 139808449791SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 139908449791SMatthew G. Knepley PetscObject obj; 140008449791SMatthew G. Knepley PetscClassId id; 14017173168dSMatthew G. Knepley PetscBool fimp; 140208449791SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 140308449791SMatthew G. Knepley 14047173168dSMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 14057173168dSMatthew G. Knepley if (isImplicit != fimp) continue; 140608449791SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 140708449791SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 140808449791SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 140908449791SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 14104a3e9fdbSToby Isaac PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 14114a3e9fdbSToby Isaac PetscFEGeom *chunkGeom = NULL; 14124a3e9fdbSToby Isaac PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 141308449791SMatthew G. Knepley PetscInt Nq, Nb; 141408449791SMatthew G. Knepley 141508449791SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 14164a3e9fdbSToby Isaac ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 141708449791SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1418c330f8ffSToby Isaac blockSize = Nb; 141908449791SMatthew G. Knepley batchSize = numBlocks * blockSize; 142008449791SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 142108449791SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 142208449791SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 142308449791SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 142408449791SMatthew G. Knepley offset = numCells - Nr; 142508449791SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 142608449791SMatthew 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) */ 14274a3e9fdbSToby Isaac ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 14284bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 14294a3e9fdbSToby Isaac ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 14304bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 14314a3e9fdbSToby Isaac ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 143208449791SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 143308449791SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 143408449791SMatthew G. Knepley 143508449791SMatthew G. Knepley Ne = numFaces; 143608449791SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 143708449791SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 143808449791SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 1439ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f); 144008449791SMatthew G. Knepley } 144108449791SMatthew G. Knepley /* Loop over domain */ 144208449791SMatthew G. Knepley if (useFEM) { 144308449791SMatthew G. Knepley /* Add elemVec to locX */ 14449044fa66SMatthew G. Knepley for (c = cS; c < cE; ++c) { 14459044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 14469044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 1447f905620eSMatthew G. Knepley 14489044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 1449b4920ed3SToby Isaac if (ghostLabel) { 1450b4920ed3SToby Isaac PetscInt ghostVal; 1451b4920ed3SToby Isaac 1452b4920ed3SToby Isaac ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 1453b4920ed3SToby Isaac if (ghostVal > 0) continue; 1454b4920ed3SToby Isaac } 14559044fa66SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 145608449791SMatthew G. Knepley } 145708449791SMatthew G. Knepley } 145808449791SMatthew G. Knepley if (useFVM) { 14594a394323SMatthew G. Knepley PetscScalar *fa; 146008449791SMatthew G. Knepley PetscInt iface; 146108449791SMatthew G. Knepley 146208449791SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1463c10b5f1bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1464c10b5f1bSMatthew G. Knepley PetscFV fv; 1465c10b5f1bSMatthew G. Knepley PetscObject obj; 1466c10b5f1bSMatthew G. Knepley PetscClassId id; 14674a394323SMatthew G. Knepley PetscInt foff, pdim; 1468c10b5f1bSMatthew G. Knepley 1469c10b5f1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1470c10b5f1bSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1471c10b5f1bSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1472c10b5f1bSMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1473c10b5f1bSMatthew G. Knepley fv = (PetscFV) obj; 1474c10b5f1bSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 1475c10b5f1bSMatthew G. Knepley /* Accumulate fluxes to cells */ 147608449791SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 14779044fa66SMatthew G. Knepley const PetscInt *scells; 1478b4920ed3SToby Isaac PetscScalar *fL = NULL, *fR = NULL; 14797c45b140SToby Isaac PetscInt ghost, d, nsupp, nchild; 148008449791SMatthew G. Knepley 148108449791SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 1482ffe9ad51SToby Isaac ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 14837c45b140SToby Isaac ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 14847c45b140SToby Isaac if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 14859044fa66SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr); 14869044fa66SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr); 14879044fa66SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);} 14889044fa66SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr); 14899044fa66SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);} 1490c10b5f1bSMatthew G. Knepley for (d = 0; d < pdim; ++d) { 1491c10b5f1bSMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 1492c10b5f1bSMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 149308449791SMatthew G. Knepley } 149408449791SMatthew G. Knepley ++iface; 149508449791SMatthew G. Knepley } 1496dab51205SMatthew G. Knepley } 1497dab51205SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 1498dab51205SMatthew G. Knepley } 1499c10b5f1bSMatthew G. Knepley /* Handle time derivative */ 1500c10b5f1bSMatthew G. Knepley if (locX_t) { 1501dab51205SMatthew G. Knepley PetscScalar *x_t, *fa; 1502dab51205SMatthew G. Knepley 1503dab51205SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 1504c10b5f1bSMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 1505dab51205SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1506dab51205SMatthew G. Knepley PetscFV fv; 1507dab51205SMatthew G. Knepley PetscObject obj; 1508dab51205SMatthew G. Knepley PetscClassId id; 1509dab51205SMatthew G. Knepley PetscInt pdim, d; 1510dab51205SMatthew G. Knepley 1511dab51205SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1512dab51205SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1513dab51205SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 1514dab51205SMatthew G. Knepley fv = (PetscFV) obj; 1515dab51205SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 15169044fa66SMatthew G. Knepley for (c = cS; c < cE; ++c) { 15179044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 1518c10b5f1bSMatthew G. Knepley PetscScalar *u_t, *r; 1519c10b5f1bSMatthew G. Knepley 1520b4920ed3SToby Isaac if (ghostLabel) { 1521b4920ed3SToby Isaac PetscInt ghostVal; 1522b4920ed3SToby Isaac 1523b4920ed3SToby Isaac ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr); 1524b4920ed3SToby Isaac if (ghostVal > 0) continue; 1525b4920ed3SToby Isaac } 1526c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 1527c10b5f1bSMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 1528d63b37e5SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 1529c10b5f1bSMatthew G. Knepley } 1530dab51205SMatthew G. Knepley } 1531c10b5f1bSMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 153208449791SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 153308449791SMatthew G. Knepley } 153408449791SMatthew G. Knepley if (useFEM) { 15359044fa66SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 153669291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 153708449791SMatthew G. Knepley } 153808449791SMatthew G. Knepley if (useFVM) { 15395f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 15405f942ad5SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 154169291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 154269291d52SBarry Smith ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 154308449791SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 154408449791SMatthew G. Knepley } 154508449791SMatthew G. Knepley } 154603fee3f0SMatthew G. Knepley if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);} 15479044fa66SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 154808449791SMatthew G. Knepley 15494a3e9fdbSToby Isaac if (useFEM) { 15504a3e9fdbSToby Isaac ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr); 15514a3e9fdbSToby Isaac 1552b7260050SToby Isaac if (maxDegree <= 1) { 15534a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 15544a3e9fdbSToby Isaac ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 15554a3e9fdbSToby Isaac } else { 15564a3e9fdbSToby Isaac for (f = 0; f < Nf; ++f) { 15574a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 15584a3e9fdbSToby Isaac ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr); 15594a3e9fdbSToby Isaac } 15604a3e9fdbSToby Isaac ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 15614a3e9fdbSToby Isaac } 15624a3e9fdbSToby Isaac } 156308449791SMatthew G. Knepley 156408449791SMatthew G. Knepley /* FEM */ 156508449791SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 156608449791SMatthew G. Knepley /* 2: Get geometric data */ 156708449791SMatthew G. Knepley /* 3: Handle boundary values */ 156808449791SMatthew G. Knepley /* 4: Loop over domain */ 156908449791SMatthew G. Knepley /* Extract coefficients */ 157008449791SMatthew G. Knepley /* Loop over fields */ 157108449791SMatthew G. Knepley /* Set tiling for FE*/ 157208449791SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 157308449791SMatthew G. Knepley /* Loop over subdomain */ 157408449791SMatthew G. Knepley /* Loop over quad points */ 157508449791SMatthew G. Knepley /* Transform coords to real space */ 157608449791SMatthew G. Knepley /* Evaluate field and aux fields at point */ 157708449791SMatthew G. Knepley /* Evaluate residual at point */ 157808449791SMatthew G. Knepley /* Transform residual to real space */ 157908449791SMatthew G. Knepley /* Add residual to elemVec */ 158008449791SMatthew G. Knepley /* Loop over domain */ 158108449791SMatthew G. Knepley /* Add elemVec to locX */ 158208449791SMatthew G. Knepley 158308449791SMatthew G. Knepley /* FVM */ 158408449791SMatthew G. Knepley /* Get geometric data */ 158508449791SMatthew G. Knepley /* If using gradients */ 158608449791SMatthew G. Knepley /* Compute gradient data */ 158708449791SMatthew G. Knepley /* Loop over domain faces */ 158808449791SMatthew G. Knepley /* Count computational faces */ 158908449791SMatthew G. Knepley /* Reconstruct cell gradient */ 159008449791SMatthew G. Knepley /* Loop over domain cells */ 159108449791SMatthew G. Knepley /* Limit cell gradients */ 159208449791SMatthew G. Knepley /* Handle boundary values */ 159308449791SMatthew G. Knepley /* Loop over domain faces */ 159408449791SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 159508449791SMatthew G. Knepley /* Riemann solve over faces */ 159608449791SMatthew G. Knepley /* Loop over domain faces */ 159708449791SMatthew G. Knepley /* Accumulate fluxes to cells */ 159808449791SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 1599247ba720SToby Isaac if (mesh->printFEM) { 1600247ba720SToby Isaac Vec locFbc; 1601247ba720SToby Isaac PetscInt pStart, pEnd, p, maxDof; 1602247ba720SToby Isaac PetscScalar *zeroes; 1603247ba720SToby Isaac 1604247ba720SToby Isaac ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 1605247ba720SToby Isaac ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 1606247ba720SToby Isaac ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 1607247ba720SToby Isaac ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 1608247ba720SToby Isaac ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 1609247ba720SToby Isaac for (p = pStart; p < pEnd; p++) { 1610247ba720SToby Isaac ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 1611247ba720SToby Isaac } 1612247ba720SToby Isaac ierr = PetscFree(zeroes);CHKERRQ(ierr); 1613247ba720SToby Isaac ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 1614247ba720SToby Isaac ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 1615247ba720SToby Isaac } 161624cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 161724cdb843SMatthew G. Knepley PetscFunctionReturn(0); 161824cdb843SMatthew G. Knepley } 161924cdb843SMatthew G. Knepley 1620*27f02ce8SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 1621*27f02ce8SMatthew G. Knepley { 1622*27f02ce8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1623*27f02ce8SMatthew G. Knepley const char *name = "Hybrid Residual"; 1624*27f02ce8SMatthew G. Knepley DM dmAux = NULL; 1625*27f02ce8SMatthew G. Knepley DMLabel ghostLabel = NULL; 1626*27f02ce8SMatthew G. Knepley PetscDS prob = NULL; 1627*27f02ce8SMatthew G. Knepley PetscDS probAux = NULL; 1628*27f02ce8SMatthew G. Knepley PetscSection section = NULL; 1629*27f02ce8SMatthew G. Knepley DMField coordField = NULL; 1630*27f02ce8SMatthew G. Knepley Vec locA; 1631*27f02ce8SMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a; 1632*27f02ce8SMatthew G. Knepley PetscScalar *elemVec; 1633*27f02ce8SMatthew G. Knepley IS chunkIS; 1634*27f02ce8SMatthew G. Knepley const PetscInt *cells; 1635*27f02ce8SMatthew G. Knepley PetscInt *faces; 1636*27f02ce8SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 1637*27f02ce8SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk; 1638*27f02ce8SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 1639*27f02ce8SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 1640*27f02ce8SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 1641*27f02ce8SMatthew G. Knepley PetscErrorCode ierr; 1642*27f02ce8SMatthew G. Knepley 1643*27f02ce8SMatthew G. Knepley PetscFunctionBegin; 1644*27f02ce8SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1645*27f02ce8SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 1646*27f02ce8SMatthew G. Knepley /* FEM */ 1647*27f02ce8SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1648*27f02ce8SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 1649*27f02ce8SMatthew G. Knepley ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 1650*27f02ce8SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 1651*27f02ce8SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 1652*27f02ce8SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 1653*27f02ce8SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1654*27f02ce8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1655*27f02ce8SMatthew G. Knepley if (locA) { 1656*27f02ce8SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1657*27f02ce8SMatthew G. Knepley ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr); 1658*27f02ce8SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1659*27f02ce8SMatthew G. Knepley } 1660*27f02ce8SMatthew G. Knepley /* 2: Setup geometric data */ 1661*27f02ce8SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1662*27f02ce8SMatthew G. Knepley ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr); 1663*27f02ce8SMatthew G. Knepley if (maxDegree > 1) { 1664*27f02ce8SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 1665*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1666*27f02ce8SMatthew G. Knepley PetscFE fe; 1667*27f02ce8SMatthew G. Knepley 1668*27f02ce8SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1669*27f02ce8SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 1670*27f02ce8SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr); 1671*27f02ce8SMatthew G. Knepley } 1672*27f02ce8SMatthew G. Knepley } 1673*27f02ce8SMatthew G. Knepley /* Loop over chunks */ 1674*27f02ce8SMatthew G. Knepley numCells = cEnd - cStart; 1675*27f02ce8SMatthew G. Knepley cellChunkSize = numCells; 1676*27f02ce8SMatthew G. Knepley numChunks = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize); 1677*27f02ce8SMatthew G. Knepley ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr); 1678*27f02ce8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr); 1679*27f02ce8SMatthew G. Knepley /* Extract field coefficients */ 1680*27f02ce8SMatthew G. Knepley /* NOTE This needs the end cap faces to have identical orientations */ 1681*27f02ce8SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1682*27f02ce8SMatthew G. Knepley ierr = DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1683*27f02ce8SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 1684*27f02ce8SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 1685*27f02ce8SMatthew G. Knepley 1686*27f02ce8SMatthew G. Knepley ierr = PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 1687*27f02ce8SMatthew G. Knepley /* Get faces */ 1688*27f02ce8SMatthew G. Knepley for (c = cS; c < cE; ++c) { 1689*27f02ce8SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 1690*27f02ce8SMatthew G. Knepley const PetscInt *cone; 1691*27f02ce8SMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr); 1692*27f02ce8SMatthew G. Knepley faces[(c-cS)*2+0] = cone[0]; 1693*27f02ce8SMatthew G. Knepley faces[(c-cS)*2+1] = cone[1]; 1694*27f02ce8SMatthew G. Knepley } 1695*27f02ce8SMatthew G. Knepley ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr); 1696*27f02ce8SMatthew G. Knepley /* Get geometric data */ 1697*27f02ce8SMatthew G. Knepley if (maxDegree <= 1) { 1698*27f02ce8SMatthew G. Knepley if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);} 1699*27f02ce8SMatthew G. Knepley if (affineQuad) {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);} 1700*27f02ce8SMatthew G. Knepley } else { 1701*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1702*27f02ce8SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr); 1703*27f02ce8SMatthew G. Knepley } 1704*27f02ce8SMatthew G. Knepley } 1705*27f02ce8SMatthew G. Knepley /* Loop over fields */ 1706*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1707*27f02ce8SMatthew G. Knepley PetscFE fe; 1708*27f02ce8SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 1709*27f02ce8SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 1710*27f02ce8SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 1711*27f02ce8SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb; 1712*27f02ce8SMatthew G. Knepley 1713*27f02ce8SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1714*27f02ce8SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1715*27f02ce8SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1716*27f02ce8SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1717*27f02ce8SMatthew G. Knepley blockSize = Nb; 1718*27f02ce8SMatthew G. Knepley batchSize = numBlocks * blockSize; 1719*27f02ce8SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1720*27f02ce8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 1721*27f02ce8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1722*27f02ce8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 1723*27f02ce8SMatthew G. Knepley offset = numCells - Nr; 1724*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 1725*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 1726*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1727*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 1728*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1729*27f02ce8SMatthew G. Knepley } 1730*27f02ce8SMatthew G. Knepley /* Add elemVec to locX */ 1731*27f02ce8SMatthew G. Knepley for (c = cS; c < cE; ++c) { 1732*27f02ce8SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 1733*27f02ce8SMatthew G. Knepley const PetscInt cind = c - cStart; 1734*27f02ce8SMatthew G. Knepley 1735*27f02ce8SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 1736*27f02ce8SMatthew G. Knepley if (ghostLabel) { 1737*27f02ce8SMatthew G. Knepley PetscInt ghostVal; 1738*27f02ce8SMatthew G. Knepley 1739*27f02ce8SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 1740*27f02ce8SMatthew G. Knepley if (ghostVal > 0) continue; 1741*27f02ce8SMatthew G. Knepley } 1742*27f02ce8SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 1743*27f02ce8SMatthew G. Knepley } 1744*27f02ce8SMatthew G. Knepley } 1745*27f02ce8SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 1746*27f02ce8SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 1747*27f02ce8SMatthew G. Knepley ierr = PetscFree(faces);CHKERRQ(ierr); 1748*27f02ce8SMatthew G. Knepley ierr = ISDestroy(&chunkIS);CHKERRQ(ierr); 1749*27f02ce8SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 1750*27f02ce8SMatthew G. Knepley if (maxDegree <= 1) { 1751*27f02ce8SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 1752*27f02ce8SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 1753*27f02ce8SMatthew G. Knepley } else { 1754*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1755*27f02ce8SMatthew G. Knepley if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);} 1756*27f02ce8SMatthew G. Knepley if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);} 1757*27f02ce8SMatthew G. Knepley } 1758*27f02ce8SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 1759*27f02ce8SMatthew G. Knepley } 1760*27f02ce8SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1761*27f02ce8SMatthew G. Knepley PetscFunctionReturn(0); 1762*27f02ce8SMatthew G. Knepley } 1763*27f02ce8SMatthew G. Knepley 176424cdb843SMatthew G. Knepley /*@ 176524cdb843SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 176624cdb843SMatthew G. Knepley 176724cdb843SMatthew G. Knepley Input Parameters: 176824cdb843SMatthew G. Knepley + dm - The mesh 176924cdb843SMatthew G. Knepley . X - Local solution 177024cdb843SMatthew G. Knepley - user - The user context 177124cdb843SMatthew G. Knepley 177224cdb843SMatthew G. Knepley Output Parameter: 177324cdb843SMatthew G. Knepley . F - Local output vector 177424cdb843SMatthew G. Knepley 177524cdb843SMatthew G. Knepley Level: developer 177624cdb843SMatthew G. Knepley 17777a73cf09SMatthew G. Knepley .seealso: DMPlexComputeJacobianAction() 177824cdb843SMatthew G. Knepley @*/ 177924cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 178024cdb843SMatthew G. Knepley { 17816da023fcSToby Isaac DM plex; 17824a3e9fdbSToby Isaac IS cellIS; 17834a3e9fdbSToby Isaac PetscInt depth; 178424cdb843SMatthew G. Knepley PetscErrorCode ierr; 178524cdb843SMatthew G. Knepley 178624cdb843SMatthew G. Knepley PetscFunctionBegin; 17876da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 17884a3e9fdbSToby Isaac ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 1789aeadca18SToby Isaac ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 17904a3e9fdbSToby Isaac if (!cellIS) { 17914a3e9fdbSToby Isaac ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr); 17924a3e9fdbSToby Isaac } 17934bee2e38SMatthew G. Knepley ierr = DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr); 17944a3e9fdbSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 17959a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 179624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 179724cdb843SMatthew G. Knepley } 179824cdb843SMatthew G. Knepley 1799bdd6f66aSToby Isaac /*@ 1800bdd6f66aSToby Isaac DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X 1801bdd6f66aSToby Isaac 1802bdd6f66aSToby Isaac Input Parameters: 1803bdd6f66aSToby Isaac + dm - The mesh 1804bdd6f66aSToby Isaac - user - The user context 1805bdd6f66aSToby Isaac 1806bdd6f66aSToby Isaac Output Parameter: 1807bdd6f66aSToby Isaac . X - Local solution 1808bdd6f66aSToby Isaac 1809bdd6f66aSToby Isaac Level: developer 1810bdd6f66aSToby Isaac 18117a73cf09SMatthew G. Knepley .seealso: DMPlexComputeJacobianAction() 1812bdd6f66aSToby Isaac @*/ 1813bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user) 1814bdd6f66aSToby Isaac { 1815bdd6f66aSToby Isaac DM plex; 1816bdd6f66aSToby Isaac PetscErrorCode ierr; 1817bdd6f66aSToby Isaac 1818bdd6f66aSToby Isaac PetscFunctionBegin; 1819bdd6f66aSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 1820bdd6f66aSToby Isaac ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr); 1821bdd6f66aSToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 1822bdd6f66aSToby Isaac PetscFunctionReturn(0); 1823bdd6f66aSToby Isaac } 1824bdd6f66aSToby Isaac 182551e9edccSMatthew 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) 1826089bfe53SSander Arens { 1827089bfe53SSander Arens DM_Plex *mesh = (DM_Plex *) dm->data; 1828ca3d3a14SMatthew G. Knepley DM plex = NULL, plexA = NULL, tdm; 1829a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 18302d91c981SSander Arens PetscDS prob, probAux = NULL; 183151e9edccSMatthew G. Knepley PetscSection section, sectionAux = NULL; 183251e9edccSMatthew G. Knepley PetscSection globalSection, subSection = NULL; 1833ca3d3a14SMatthew G. Knepley Vec locA = NULL, tv; 18342d91c981SSander Arens PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL; 183551e9edccSMatthew G. Knepley PetscInt v; 183651e9edccSMatthew G. Knepley PetscInt Nf, totDim, totDimAux = 0; 1837ca3d3a14SMatthew G. Knepley PetscBool isMatISP, transform; 1838089bfe53SSander Arens PetscErrorCode ierr; 1839089bfe53SSander Arens 1840089bfe53SSander Arens PetscFunctionBegin; 184151e9edccSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 1842ca3d3a14SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 1843ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 1844ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 184592fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 1846089bfe53SSander Arens ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1847089bfe53SSander Arens ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 18484d0b9603SSander Arens ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 18492d91c981SSander Arens ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 18502d91c981SSander Arens if (locA) { 185151e9edccSMatthew G. Knepley DM dmAux; 185251e9edccSMatthew G. Knepley 18532d91c981SSander Arens ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1854a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 185551e9edccSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 185651e9edccSMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 18574d0b9603SSander Arens ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 185892fd8e1eSJed Brown ierr = DMGetLocalSection(plexA, §ionAux);CHKERRQ(ierr); 18592d91c981SSander Arens } 186051e9edccSMatthew G. Knepley 186151e9edccSMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 186251e9edccSMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 186351e9edccSMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 186451e9edccSMatthew G. Knepley for (v = 0; v < numValues; ++v) { 186551e9edccSMatthew G. Knepley PetscFEGeom *fgeom; 1866b7260050SToby Isaac PetscInt maxDegree; 186751e9edccSMatthew G. Knepley PetscQuadrature qGeom = NULL; 1868089bfe53SSander Arens IS pointIS; 1869089bfe53SSander Arens const PetscInt *points; 187051e9edccSMatthew G. Knepley PetscInt numFaces, face, Nq; 1871c330f8ffSToby Isaac 1872089bfe53SSander Arens ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 1873089bfe53SSander Arens if (!pointIS) continue; /* No points with that id on this process */ 1874c330f8ffSToby Isaac { 1875c330f8ffSToby Isaac IS isectIS; 1876c330f8ffSToby Isaac 1877c330f8ffSToby Isaac /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */ 187800199b93SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 1879c330f8ffSToby Isaac ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1880c330f8ffSToby Isaac pointIS = isectIS; 1881089bfe53SSander Arens } 1882c330f8ffSToby Isaac ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 1883c330f8ffSToby Isaac ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 188451e9edccSMatthew 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); 1885b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 1886b7260050SToby Isaac if (maxDegree <= 1) { 1887c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 1888c330f8ffSToby Isaac } 1889c330f8ffSToby Isaac if (!qGeom) { 189051e9edccSMatthew G. Knepley PetscFE fe; 189151e9edccSMatthew G. Knepley 189251e9edccSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 1893c330f8ffSToby Isaac ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 1894c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 1895c330f8ffSToby Isaac } 1896c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 18974a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1898c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1899c330f8ffSToby Isaac const PetscInt point = points[face], *support, *cone; 1900089bfe53SSander Arens PetscScalar *x = NULL; 19019bfb2fe4SJed Brown PetscInt i, coneSize, faceLoc; 1902089bfe53SSander Arens 19034d0b9603SSander Arens ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 19044d0b9603SSander Arens ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 19054d0b9603SSander Arens ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 19064d0b9603SSander Arens for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 1907ff1e0c32SBarry Smith if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]); 1908f99c8401SToby Isaac fgeom->face[face][0] = faceLoc; 190951e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 19104d0b9603SSander Arens for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 191151e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1912089bfe53SSander Arens if (locX_t) { 191351e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 19144d0b9603SSander Arens for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 191551e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 1916089bfe53SSander Arens } 19172d91c981SSander Arens if (locA) { 191851e9edccSMatthew G. Knepley PetscInt subp; 1919a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr); 192051e9edccSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 19214d0b9603SSander Arens for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 192251e9edccSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 19232d91c981SSander Arens } 1924089bfe53SSander Arens } 1925580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, numFaces*totDim*totDim);CHKERRQ(ierr); 1926089bfe53SSander Arens { 1927089bfe53SSander Arens PetscFE fe; 1928c330f8ffSToby Isaac PetscInt Nb; 1929089bfe53SSander Arens /* Conforming batches */ 1930089bfe53SSander Arens PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1931089bfe53SSander Arens /* Remainder */ 1932c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 193351e9edccSMatthew G. Knepley PetscInt fieldJ, Nr, offset; 1934089bfe53SSander Arens 19354d0b9603SSander Arens ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 1936089bfe53SSander Arens ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1937089bfe53SSander Arens ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1938c330f8ffSToby Isaac blockSize = Nb; 1939089bfe53SSander Arens batchSize = numBlocks * blockSize; 1940089bfe53SSander Arens ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1941089bfe53SSander Arens numChunks = numFaces / (numBatches*batchSize); 1942089bfe53SSander Arens Ne = numChunks*numBatches*batchSize; 1943089bfe53SSander Arens Nr = numFaces % (numBatches*batchSize); 1944089bfe53SSander Arens offset = numFaces - Nr; 1945c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 1946089bfe53SSander Arens for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 19474bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 1948089bfe53SSander Arens } 1949c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 1950c330f8ffSToby Isaac for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 19514bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(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); 1952089bfe53SSander Arens } 1953c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 1954c330f8ffSToby Isaac } 1955c330f8ffSToby Isaac for (face = 0; face < numFaces; ++face) { 1956c330f8ffSToby Isaac const PetscInt point = points[face], *support; 1957089bfe53SSander Arens 1958ca3d3a14SMatthew G. Knepley /* Transform to global basis before insertion in Jacobian */ 195951e9edccSMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 1960ca3d3a14SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 1961ca3d3a14SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 1962089bfe53SSander Arens if (!isMatISP) { 196351e9edccSMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 1964089bfe53SSander Arens } else { 1965089bfe53SSander Arens Mat lJ; 1966089bfe53SSander Arens 1967089bfe53SSander Arens ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr); 196851e9edccSMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 1969089bfe53SSander Arens } 1970089bfe53SSander Arens } 19714a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 1972c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 1973089bfe53SSander Arens ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 1974089bfe53SSander Arens ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 197551e9edccSMatthew G. Knepley ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr); 19762d91c981SSander Arens } 197751e9edccSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 197851e9edccSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 197951e9edccSMatthew G. Knepley PetscFunctionReturn(0); 198051e9edccSMatthew G. Knepley } 198151e9edccSMatthew G. Knepley 198251e9edccSMatthew 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) 198351e9edccSMatthew G. Knepley { 198451e9edccSMatthew G. Knepley DMField coordField; 198551e9edccSMatthew G. Knepley DMLabel depthLabel; 198651e9edccSMatthew G. Knepley IS facetIS; 198751e9edccSMatthew G. Knepley PetscInt dim; 198851e9edccSMatthew G. Knepley PetscErrorCode ierr; 198951e9edccSMatthew G. Knepley 199051e9edccSMatthew G. Knepley PetscFunctionBegin; 199151e9edccSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 199251e9edccSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 199351e9edccSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 199451e9edccSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 199551e9edccSMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 1996dbcb0379SMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 199751e9edccSMatthew G. Knepley PetscFunctionReturn(0); 199851e9edccSMatthew G. Knepley } 199951e9edccSMatthew G. Knepley 200051e9edccSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user) 200151e9edccSMatthew G. Knepley { 200251e9edccSMatthew G. Knepley PetscDS prob; 200351e9edccSMatthew G. Knepley PetscInt dim, numBd, bd; 200451e9edccSMatthew G. Knepley DMLabel depthLabel; 200551e9edccSMatthew G. Knepley DMField coordField = NULL; 200651e9edccSMatthew G. Knepley IS facetIS; 200751e9edccSMatthew G. Knepley PetscErrorCode ierr; 200851e9edccSMatthew G. Knepley 200951e9edccSMatthew G. Knepley PetscFunctionBegin; 201051e9edccSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 201151e9edccSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 201251e9edccSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 201351e9edccSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 201451e9edccSMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 201551e9edccSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 201651e9edccSMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 201751e9edccSMatthew G. Knepley DMBoundaryConditionType type; 201851e9edccSMatthew G. Knepley const char *bdLabel; 201951e9edccSMatthew G. Knepley DMLabel label; 202051e9edccSMatthew G. Knepley const PetscInt *values; 202151e9edccSMatthew G. Knepley PetscInt fieldI, numValues; 202251e9edccSMatthew G. Knepley PetscObject obj; 202351e9edccSMatthew G. Knepley PetscClassId id; 202451e9edccSMatthew G. Knepley 202551e9edccSMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 202651e9edccSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr); 202751e9edccSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 202851e9edccSMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 202951e9edccSMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 203051e9edccSMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 2031089bfe53SSander Arens } 2032c330f8ffSToby Isaac ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 2033089bfe53SSander Arens PetscFunctionReturn(0); 2034089bfe53SSander Arens } 2035089bfe53SSander Arens 20364a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 203724cdb843SMatthew G. Knepley { 203824cdb843SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 203924cdb843SMatthew G. Knepley const char *name = "Jacobian"; 2040ca3d3a14SMatthew G. Knepley DM dmAux, plex, tdm; 2041a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 2042ca3d3a14SMatthew G. Knepley Vec A, tv; 2043c330f8ffSToby Isaac DMField coordField; 204424cdb843SMatthew G. Knepley PetscDS prob, probAux = NULL; 2045be36d101SStefano Zampini PetscSection section, globalSection, subSection, sectionAux; 2046426ff135SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 20479044fa66SMatthew G. Knepley const PetscInt *cells; 20489044fa66SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ; 20499044fa66SMatthew G. Knepley PetscInt totDim, totDimAux, cStart, cEnd, numCells, c; 2050ca3d3a14SMatthew G. Knepley PetscBool isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform; 205124cdb843SMatthew G. Knepley PetscErrorCode ierr; 205224cdb843SMatthew G. Knepley 205324cdb843SMatthew G. Knepley PetscFunctionBegin; 205424cdb843SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2055*27f02ce8SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 2056*27f02ce8SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2057ca3d3a14SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 2058ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 2059ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 206092fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 2061be36d101SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 2062e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 20635bf53532SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 20649303deb7SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 20659303deb7SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 20669303deb7SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 20679303deb7SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 206824cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2069efc10488SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 207055ad3c34SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 207138cfc46eSPierre Jolivet /* user passed in the same matrix, avoid double contributions and 207238cfc46eSPierre Jolivet only assemble the Jacobian */ 207338cfc46eSPierre Jolivet if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE; 2074426ff135SMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2075426ff135SMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 207624cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 207724cdb843SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 207824cdb843SMatthew G. Knepley if (dmAux) { 2079a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 2080f7ed7b21SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 208192fd8e1eSJed Brown ierr = DMGetLocalSection(plex, §ionAux);CHKERRQ(ierr); 208224cdb843SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 208324cdb843SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 208424cdb843SMatthew G. Knepley } 2085efc10488SMatthew 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); 208624cdb843SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 20874a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 208824cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 20899044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 20909044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 209124cdb843SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 209224cdb843SMatthew G. Knepley PetscInt i; 209324cdb843SMatthew G. Knepley 20949044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 20959044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 20969044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 209724cdb843SMatthew G. Knepley if (X_t) { 20989044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 20999044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 21009044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 210124cdb843SMatthew G. Knepley } 210224cdb843SMatthew G. Knepley if (dmAux) { 210344171101SMatthew G. Knepley PetscInt subcell; 2104a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr); 210544171101SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 21069044fa66SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 210744171101SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 210824cdb843SMatthew G. Knepley } 210924cdb843SMatthew G. Knepley } 2110580bdb30SBarry Smith if (hasJac) {ierr = PetscArrayzero(elemMat, numCells*totDim*totDim);CHKERRQ(ierr);} 2111580bdb30SBarry Smith if (hasPrec) {ierr = PetscArrayzero(elemMatP, numCells*totDim*totDim);CHKERRQ(ierr);} 2112580bdb30SBarry Smith if (hasDyn) {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);} 211324cdb843SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 21145244db28SMatthew G. Knepley PetscClassId id; 211524cdb843SMatthew G. Knepley PetscFE fe; 2116c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 2117c330f8ffSToby Isaac PetscInt Nb; 211824cdb843SMatthew G. Knepley /* Conforming batches */ 211924cdb843SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 212024cdb843SMatthew G. Knepley /* Remainder */ 2121c330f8ffSToby Isaac PetscInt Nr, offset, Nq; 2122b7260050SToby Isaac PetscInt maxDegree; 2123c330f8ffSToby Isaac PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 212424cdb843SMatthew G. Knepley 212524cdb843SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 21265244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 21275244db28SMatthew G. Knepley if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 212824cdb843SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 212924cdb843SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2130b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 2131b7260050SToby Isaac if (maxDegree <= 1) { 2132c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 2133c330f8ffSToby Isaac } 2134c330f8ffSToby Isaac if (!qGeom) { 2135c330f8ffSToby Isaac ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2136c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2137c330f8ffSToby Isaac } 2138c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 21394a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2140c330f8ffSToby Isaac blockSize = Nb; 214124cdb843SMatthew G. Knepley batchSize = numBlocks * blockSize; 214224cdb843SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 214324cdb843SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 214424cdb843SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 214524cdb843SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 214624cdb843SMatthew G. Knepley offset = numCells - Nr; 2147c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2148c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 214924cdb843SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2150efc10488SMatthew G. Knepley if (hasJac) { 21514bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 21524bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(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); 2153efc10488SMatthew G. Knepley } 215455ad3c34SMatthew G. Knepley if (hasPrec) { 21554bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 21564bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(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); 2157426ff135SMatthew G. Knepley } 2158426ff135SMatthew G. Knepley if (hasDyn) { 21594bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 21604bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(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); 216155ad3c34SMatthew G. Knepley } 216224cdb843SMatthew G. Knepley } 2163c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2164c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 21654a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2166c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 216724cdb843SMatthew G. Knepley } 21685bf53532SMatthew G. Knepley /* Add contribution from X_t */ 21699044fa66SMatthew G. Knepley if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];} 21705244db28SMatthew G. Knepley if (hasFV) { 21715244db28SMatthew G. Knepley PetscClassId id; 21725244db28SMatthew G. Knepley PetscFV fv; 21735244db28SMatthew G. Knepley PetscInt offsetI, NcI, NbI = 1, fc, f; 21745244db28SMatthew G. Knepley 21755244db28SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 21765244db28SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 21775244db28SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 21785244db28SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 21795244db28SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 21805244db28SMatthew G. Knepley /* Put in the identity */ 21815244db28SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 21825244db28SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 21839044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 21849044fa66SMatthew G. Knepley const PetscInt eOffset = cind*totDim*totDim; 21855244db28SMatthew G. Knepley for (fc = 0; fc < NcI; ++fc) { 21865244db28SMatthew G. Knepley for (f = 0; f < NbI; ++f) { 21875244db28SMatthew G. Knepley const PetscInt i = offsetI + f*NcI+fc; 21885244db28SMatthew G. Knepley if (hasPrec) { 21895244db28SMatthew G. Knepley if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 21905244db28SMatthew G. Knepley elemMatP[eOffset+i*totDim+i] = 1.0; 21915244db28SMatthew G. Knepley } else {elemMat[eOffset+i*totDim+i] = 1.0;} 21925244db28SMatthew G. Knepley } 21935244db28SMatthew G. Knepley } 21945244db28SMatthew G. Knepley } 21955244db28SMatthew G. Knepley } 21965244db28SMatthew G. Knepley /* No allocated space for FV stuff, so ignore the zero entries */ 21975244db28SMatthew G. Knepley ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 21985244db28SMatthew G. Knepley } 21995bf53532SMatthew G. Knepley /* Insert values into matrix */ 2200be36d101SStefano Zampini isMatIS = PETSC_FALSE; 2201be36d101SStefano Zampini if (hasPrec && hasJac) { 2202be36d101SStefano Zampini ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr); 2203be36d101SStefano Zampini } 2204be36d101SStefano Zampini if (isMatIS && !subSection) { 2205be36d101SStefano Zampini ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 2206be36d101SStefano Zampini } 220724cdb843SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 22089044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 22099044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 22109044fa66SMatthew G. Knepley 2211ca3d3a14SMatthew G. Knepley /* Transform to global basis before insertion in Jacobian */ 2212ca3d3a14SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 221355ad3c34SMatthew G. Knepley if (hasPrec) { 2214efc10488SMatthew G. Knepley if (hasJac) { 22159044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2216be36d101SStefano Zampini if (!isMatIS) { 22179044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2218be36d101SStefano Zampini } else { 2219be36d101SStefano Zampini Mat lJ; 2220be36d101SStefano Zampini 2221be36d101SStefano Zampini ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 22229044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2223be36d101SStefano Zampini } 2224efc10488SMatthew G. Knepley } 22259044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 2226be36d101SStefano Zampini if (!isMatISP) { 22279044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 222855ad3c34SMatthew G. Knepley } else { 2229be36d101SStefano Zampini Mat lJ; 2230be36d101SStefano Zampini 2231be36d101SStefano Zampini ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 22329044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2233be36d101SStefano Zampini } 2234be36d101SStefano Zampini } else { 2235f38926e1SMatthew G. Knepley if (hasJac) { 22369044fa66SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2237be36d101SStefano Zampini if (!isMatISP) { 22389044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2239be36d101SStefano Zampini } else { 2240be36d101SStefano Zampini Mat lJ; 2241be36d101SStefano Zampini 2242be36d101SStefano Zampini ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 22439044fa66SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2244be36d101SStefano Zampini } 224524cdb843SMatthew G. Knepley } 224655ad3c34SMatthew G. Knepley } 2247f38926e1SMatthew G. Knepley } 22489044fa66SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 22495244db28SMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 2250426ff135SMatthew G. Knepley ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 2251f7ed7b21SMatthew G. Knepley if (dmAux) { 2252f7ed7b21SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 2253f7ed7b21SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2254f7ed7b21SMatthew G. Knepley } 22555bf53532SMatthew G. Knepley /* Compute boundary integrals */ 2256089bfe53SSander Arens ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr); 22575bf53532SMatthew G. Knepley /* Assemble matrix */ 2258efc10488SMatthew G. Knepley if (hasJac && hasPrec) { 225982ef7567SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 226082ef7567SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 226182ef7567SMatthew G. Knepley } 226224cdb843SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 226324cdb843SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 226424cdb843SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 226524cdb843SMatthew G. Knepley PetscFunctionReturn(0); 226624cdb843SMatthew G. Knepley } 226724cdb843SMatthew G. Knepley 2268*27f02ce8SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user) 2269*27f02ce8SMatthew G. Knepley { 2270*27f02ce8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2271*27f02ce8SMatthew G. Knepley const char *name = "Hybrid Jacobian"; 2272*27f02ce8SMatthew G. Knepley DM dmAux = NULL; 2273*27f02ce8SMatthew G. Knepley DM plex = NULL; 2274*27f02ce8SMatthew G. Knepley DM plexA = NULL; 2275*27f02ce8SMatthew G. Knepley DMLabel ghostLabel = NULL; 2276*27f02ce8SMatthew G. Knepley PetscDS prob = NULL; 2277*27f02ce8SMatthew G. Knepley PetscDS probAux = NULL; 2278*27f02ce8SMatthew G. Knepley PetscSection section = NULL; 2279*27f02ce8SMatthew G. Knepley DMField coordField = NULL; 2280*27f02ce8SMatthew G. Knepley Vec locA; 2281*27f02ce8SMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a = NULL; 2282*27f02ce8SMatthew G. Knepley PetscScalar *elemMat, *elemMatP; 2283*27f02ce8SMatthew G. Knepley PetscSection globalSection, subSection, sectionAux; 2284*27f02ce8SMatthew G. Knepley IS chunkIS; 2285*27f02ce8SMatthew G. Knepley const PetscInt *cells; 2286*27f02ce8SMatthew G. Knepley PetscInt *faces; 2287*27f02ce8SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 2288*27f02ce8SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ, totDim, totDimAux, numChunks, cellChunkSize, chunk; 2289*27f02ce8SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 2290*27f02ce8SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 2291*27f02ce8SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 2292*27f02ce8SMatthew G. Knepley PetscBool isMatIS = PETSC_FALSE, isMatISP = PETSC_FALSE, hasBdJac, hasBdPrec; 2293*27f02ce8SMatthew G. Knepley PetscErrorCode ierr; 2294*27f02ce8SMatthew G. Knepley 2295*27f02ce8SMatthew G. Knepley PetscFunctionBegin; 2296*27f02ce8SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2297*27f02ce8SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 2298*27f02ce8SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2299*27f02ce8SMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 2300*27f02ce8SMatthew G. Knepley ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 2301*27f02ce8SMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2302*27f02ce8SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 2303*27f02ce8SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 2304*27f02ce8SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 2305*27f02ce8SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2306*27f02ce8SMatthew G. Knepley ierr = PetscDSHasBdJacobian(prob, &hasBdJac);CHKERRQ(ierr); 2307*27f02ce8SMatthew G. Knepley ierr = PetscDSHasBdJacobianPreconditioner(prob, &hasBdPrec);CHKERRQ(ierr); 2308*27f02ce8SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 2309*27f02ce8SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);} 2310*27f02ce8SMatthew G. Knepley if (hasBdPrec && hasBdJac) {ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);} 2311*27f02ce8SMatthew G. Knepley if (isMatIS && !subSection) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);} 2312*27f02ce8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 2313*27f02ce8SMatthew G. Knepley if (locA) { 2314*27f02ce8SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 2315*27f02ce8SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 2316*27f02ce8SMatthew G. Knepley ierr = DMGetSection(dmAux, §ionAux);CHKERRQ(ierr); 2317*27f02ce8SMatthew G. Knepley ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr); 2318*27f02ce8SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2319*27f02ce8SMatthew G. Knepley } 2320*27f02ce8SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2321*27f02ce8SMatthew G. Knepley ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr); 2322*27f02ce8SMatthew G. Knepley if (maxDegree > 1) { 2323*27f02ce8SMatthew G. Knepley PetscInt f; 2324*27f02ce8SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 2325*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2326*27f02ce8SMatthew G. Knepley PetscFE fe; 2327*27f02ce8SMatthew G. Knepley 2328*27f02ce8SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 2329*27f02ce8SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 2330*27f02ce8SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr); 2331*27f02ce8SMatthew G. Knepley } 2332*27f02ce8SMatthew G. Knepley } 2333*27f02ce8SMatthew G. Knepley cellChunkSize = numCells; 2334*27f02ce8SMatthew G. Knepley numChunks = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize); 2335*27f02ce8SMatthew G. Knepley ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr); 2336*27f02ce8SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr); 2337*27f02ce8SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 2338*27f02ce8SMatthew G. Knepley ierr = DMGetWorkArray(dm, hasBdJac ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr); 2339*27f02ce8SMatthew G. Knepley ierr = DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr); 2340*27f02ce8SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 2341*27f02ce8SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 2342*27f02ce8SMatthew G. Knepley 2343*27f02ce8SMatthew G. Knepley if (hasBdJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2344*27f02ce8SMatthew G. Knepley if (hasBdPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 2345*27f02ce8SMatthew G. Knepley /* Get faces */ 2346*27f02ce8SMatthew G. Knepley for (c = cS; c < cE; ++c) { 2347*27f02ce8SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 2348*27f02ce8SMatthew G. Knepley const PetscInt *cone; 2349*27f02ce8SMatthew G. Knepley ierr = DMPlexGetCone(plex, cell, &cone);CHKERRQ(ierr); 2350*27f02ce8SMatthew G. Knepley faces[(c-cS)*2+0] = cone[0]; 2351*27f02ce8SMatthew G. Knepley faces[(c-cS)*2+1] = cone[1]; 2352*27f02ce8SMatthew G. Knepley } 2353*27f02ce8SMatthew G. Knepley ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr); 2354*27f02ce8SMatthew G. Knepley if (maxDegree <= 1) { 2355*27f02ce8SMatthew G. Knepley if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);} 2356*27f02ce8SMatthew G. Knepley if (affineQuad) {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);} 2357*27f02ce8SMatthew G. Knepley } else { 2358*27f02ce8SMatthew G. Knepley PetscInt f; 2359*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2360*27f02ce8SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr); 2361*27f02ce8SMatthew G. Knepley } 2362*27f02ce8SMatthew G. Knepley } 2363*27f02ce8SMatthew G. Knepley 2364*27f02ce8SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 2365*27f02ce8SMatthew G. Knepley PetscFE fe; 2366*27f02ce8SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[fieldI]; 2367*27f02ce8SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL, *remGeom = NULL; 2368*27f02ce8SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI]; 2369*27f02ce8SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb; 2370*27f02ce8SMatthew G. Knepley 2371*27f02ce8SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2372*27f02ce8SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2373*27f02ce8SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2374*27f02ce8SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2375*27f02ce8SMatthew G. Knepley blockSize = Nb; 2376*27f02ce8SMatthew G. Knepley batchSize = numBlocks * blockSize; 2377*27f02ce8SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2378*27f02ce8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 2379*27f02ce8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 2380*27f02ce8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 2381*27f02ce8SMatthew G. Knepley offset = numCells - Nr; 2382*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 2383*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr); 2384*27f02ce8SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 2385*27f02ce8SMatthew G. Knepley if (hasBdJac) { 2386*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 2387*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(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); 2388*27f02ce8SMatthew G. Knepley } 2389*27f02ce8SMatthew G. Knepley if (hasBdPrec) { 2390*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 2391*27f02ce8SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(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); 2392*27f02ce8SMatthew G. Knepley } 2393*27f02ce8SMatthew G. Knepley } 2394*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr); 2395*27f02ce8SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 2396*27f02ce8SMatthew G. Knepley } 2397*27f02ce8SMatthew G. Knepley /* Insert values into matrix */ 2398*27f02ce8SMatthew G. Knepley for (c = cS; c < cE; ++c) { 2399*27f02ce8SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 2400*27f02ce8SMatthew G. Knepley const PetscInt cind = c - cS; 2401*27f02ce8SMatthew G. Knepley 2402*27f02ce8SMatthew G. Knepley if (hasBdPrec) { 2403*27f02ce8SMatthew G. Knepley if (hasBdJac) { 2404*27f02ce8SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2405*27f02ce8SMatthew G. Knepley if (!isMatIS) { 2406*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2407*27f02ce8SMatthew G. Knepley } else { 2408*27f02ce8SMatthew G. Knepley Mat lJ; 2409*27f02ce8SMatthew G. Knepley 2410*27f02ce8SMatthew G. Knepley ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 2411*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2412*27f02ce8SMatthew G. Knepley } 2413*27f02ce8SMatthew G. Knepley } 2414*27f02ce8SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 2415*27f02ce8SMatthew G. Knepley if (!isMatISP) { 2416*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2417*27f02ce8SMatthew G. Knepley } else { 2418*27f02ce8SMatthew G. Knepley Mat lJ; 2419*27f02ce8SMatthew G. Knepley 2420*27f02ce8SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 2421*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2422*27f02ce8SMatthew G. Knepley } 2423*27f02ce8SMatthew G. Knepley } else if (hasBdJac) { 2424*27f02ce8SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 2425*27f02ce8SMatthew G. Knepley if (!isMatISP) { 2426*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2427*27f02ce8SMatthew G. Knepley } else { 2428*27f02ce8SMatthew G. Knepley Mat lJ; 2429*27f02ce8SMatthew G. Knepley 2430*27f02ce8SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 2431*27f02ce8SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 2432*27f02ce8SMatthew G. Knepley } 2433*27f02ce8SMatthew G. Knepley } 2434*27f02ce8SMatthew G. Knepley } 2435*27f02ce8SMatthew G. Knepley } 2436*27f02ce8SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 2437*27f02ce8SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, hasBdJac ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr); 2438*27f02ce8SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr); 2439*27f02ce8SMatthew G. Knepley ierr = PetscFree(faces);CHKERRQ(ierr); 2440*27f02ce8SMatthew G. Knepley ierr = ISDestroy(&chunkIS);CHKERRQ(ierr); 2441*27f02ce8SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2442*27f02ce8SMatthew G. Knepley if (maxDegree <= 1) { 2443*27f02ce8SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 2444*27f02ce8SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 2445*27f02ce8SMatthew G. Knepley } else { 2446*27f02ce8SMatthew G. Knepley PetscInt f; 2447*27f02ce8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2448*27f02ce8SMatthew G. Knepley if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);CHKERRQ(ierr);} 2449*27f02ce8SMatthew G. Knepley if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);} 2450*27f02ce8SMatthew G. Knepley } 2451*27f02ce8SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 2452*27f02ce8SMatthew G. Knepley } 2453*27f02ce8SMatthew G. Knepley if (dmAux) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 2454*27f02ce8SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2455*27f02ce8SMatthew G. Knepley /* Assemble matrix */ 2456*27f02ce8SMatthew G. Knepley if (hasBdJac && hasBdPrec) { 2457*27f02ce8SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2458*27f02ce8SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2459*27f02ce8SMatthew G. Knepley } 2460*27f02ce8SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2461*27f02ce8SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2462*27f02ce8SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2463*27f02ce8SMatthew G. Knepley PetscFunctionReturn(0); 2464*27f02ce8SMatthew G. Knepley } 2465*27f02ce8SMatthew G. Knepley 24667a73cf09SMatthew G. Knepley /*@ 24677a73cf09SMatthew 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. 24687a73cf09SMatthew G. Knepley 24697a73cf09SMatthew G. Knepley Input Parameters: 24707a73cf09SMatthew G. Knepley + dm - The mesh 24717a73cf09SMatthew G. Knepley . cellIS - 24727a73cf09SMatthew G. Knepley . t - The time 24737a73cf09SMatthew G. Knepley . X_tShift - The multiplier for the Jacobian with repsect to X_t 24747a73cf09SMatthew G. Knepley . X - Local solution vector 24757a73cf09SMatthew G. Knepley . X_t - Time-derivative of the local solution vector 24767a73cf09SMatthew G. Knepley . Y - Local input vector 24777a73cf09SMatthew G. Knepley - user - The user context 24787a73cf09SMatthew G. Knepley 24797a73cf09SMatthew G. Knepley Output Parameter: 24807a73cf09SMatthew G. Knepley . Z - Local output vector 24817a73cf09SMatthew G. Knepley 24827a73cf09SMatthew G. Knepley Note: 24837a73cf09SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 24847a73cf09SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 24857a73cf09SMatthew G. Knepley 24867a73cf09SMatthew G. Knepley Level: developer 24877a73cf09SMatthew G. Knepley 24887a73cf09SMatthew G. Knepley .seealso: FormFunctionLocal() 24897a73cf09SMatthew G. Knepley @*/ 24907a73cf09SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianAction(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user) 2491a925c78cSMatthew G. Knepley { 2492a925c78cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2493a925c78cSMatthew G. Knepley const char *name = "Jacobian"; 24947a73cf09SMatthew G. Knepley DM dmAux, plex, plexAux = NULL; 2495a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 2496c330f8ffSToby Isaac Vec A; 2497a925c78cSMatthew G. Knepley PetscDS prob, probAux = NULL; 2498a925c78cSMatthew G. Knepley PetscQuadrature quad; 2499a925c78cSMatthew G. Knepley PetscSection section, globalSection, sectionAux; 2500a925c78cSMatthew G. Knepley PetscScalar *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z; 25019044fa66SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ; 25024d0b9603SSander Arens PetscInt totDim, totDimAux = 0; 25039044fa66SMatthew G. Knepley const PetscInt *cells; 25049044fa66SMatthew G. Knepley PetscInt cStart, cEnd, numCells, c; 250575b37a90SMatthew G. Knepley PetscBool hasDyn; 2506c330f8ffSToby Isaac DMField coordField; 2507a925c78cSMatthew G. Knepley PetscErrorCode ierr; 2508a925c78cSMatthew G. Knepley 2509a925c78cSMatthew G. Knepley PetscFunctionBegin; 2510a925c78cSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 25117a73cf09SMatthew G. Knepley ierr = DMSNESConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr); 25127a73cf09SMatthew G. Knepley if (!cellIS) { 25137a73cf09SMatthew G. Knepley PetscInt depth; 25147a73cf09SMatthew G. Knepley 25157a73cf09SMatthew G. Knepley ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 25167a73cf09SMatthew G. Knepley ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 25177a73cf09SMatthew G. Knepley if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);} 25187a73cf09SMatthew G. Knepley } else { 25197a73cf09SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) cellIS);CHKERRQ(ierr); 25207a73cf09SMatthew G. Knepley } 252192fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 2522e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 2523a925c78cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2524a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2525a925c78cSMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 2526a925c78cSMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 2527a925c78cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 25284a3e9fdbSToby Isaac ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 25299044fa66SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 2530a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 2531a925c78cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 2532a925c78cSMatthew G. Knepley if (dmAux) { 2533a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 25347a73cf09SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexAux);CHKERRQ(ierr); 253592fd8e1eSJed Brown ierr = DMGetLocalSection(plexAux, §ionAux);CHKERRQ(ierr); 2536a925c78cSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 2537a925c78cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 2538a925c78cSMatthew G. Knepley } 2539a925c78cSMatthew G. Knepley ierr = VecSet(Z, 0.0);CHKERRQ(ierr); 2540a925c78cSMatthew 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); 2541a925c78cSMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 25424a3e9fdbSToby Isaac ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 2543a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 25449044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 25459044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 2546a925c78cSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 2547a925c78cSMatthew G. Knepley PetscInt i; 2548a925c78cSMatthew G. Knepley 25499044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 25509044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 25519044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 2552a925c78cSMatthew G. Knepley if (X_t) { 25539044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 25549044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 25559044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 2556a925c78cSMatthew G. Knepley } 2557a925c78cSMatthew G. Knepley if (dmAux) { 255844171101SMatthew G. Knepley PetscInt subcell; 2559a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr); 256044171101SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 25619044fa66SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 256244171101SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 2563a925c78cSMatthew G. Knepley } 25649044fa66SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 25659044fa66SMatthew G. Knepley for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i]; 25669044fa66SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, Y, cell, NULL, &x);CHKERRQ(ierr); 2567a925c78cSMatthew G. Knepley } 2568580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, numCells*totDim*totDim);CHKERRQ(ierr); 2569580bdb30SBarry Smith if (hasDyn) {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);} 2570a925c78cSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 2571a925c78cSMatthew G. Knepley PetscFE fe; 2572c330f8ffSToby Isaac PetscInt Nb; 2573a925c78cSMatthew G. Knepley /* Conforming batches */ 2574a925c78cSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 2575a925c78cSMatthew G. Knepley /* Remainder */ 2576c330f8ffSToby Isaac PetscInt Nr, offset, Nq; 2577c330f8ffSToby Isaac PetscQuadrature qGeom = NULL; 2578b7260050SToby Isaac PetscInt maxDegree; 2579c330f8ffSToby Isaac PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 2580a925c78cSMatthew G. Knepley 2581a925c78cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 2582a925c78cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 2583a925c78cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2584a925c78cSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 2585b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 2586b7260050SToby Isaac if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);} 2587c330f8ffSToby Isaac if (!qGeom) { 2588c330f8ffSToby Isaac ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 2589c330f8ffSToby Isaac ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 2590c330f8ffSToby Isaac } 2591c330f8ffSToby Isaac ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 25924a3e9fdbSToby Isaac ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2593c330f8ffSToby Isaac blockSize = Nb; 2594a925c78cSMatthew G. Knepley batchSize = numBlocks * blockSize; 2595a925c78cSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 2596a925c78cSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 2597a925c78cSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 2598a925c78cSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 2599a925c78cSMatthew G. Knepley offset = numCells - Nr; 2600c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 2601c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2602a925c78cSMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 26034bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 26044bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(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); 2605a925c78cSMatthew G. Knepley if (hasDyn) { 26064bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 26074bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(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); 2608a925c78cSMatthew G. Knepley } 2609a925c78cSMatthew G. Knepley } 2610c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 2611c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 26124a3e9fdbSToby Isaac ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2613c330f8ffSToby Isaac ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 2614a925c78cSMatthew G. Knepley } 2615a925c78cSMatthew G. Knepley if (hasDyn) { 26169044fa66SMatthew G. Knepley for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c]; 2617a925c78cSMatthew G. Knepley } 2618a925c78cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 26199044fa66SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 26209044fa66SMatthew G. Knepley const PetscInt cind = c - cStart; 2621a925c78cSMatthew G. Knepley const PetscBLASInt M = totDim, one = 1; 2622a925c78cSMatthew G. Knepley const PetscScalar a = 1.0, b = 0.0; 2623a925c78cSMatthew G. Knepley 26249044fa66SMatthew G. Knepley PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one)); 2625a925c78cSMatthew G. Knepley if (mesh->printFEM > 1) { 26269044fa66SMatthew G. Knepley ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr); 26279044fa66SMatthew G. Knepley ierr = DMPrintCellVector(c, "Y", totDim, &y[cind*totDim]);CHKERRQ(ierr); 2628a925c78cSMatthew G. Knepley ierr = DMPrintCellVector(c, "Z", totDim, z);CHKERRQ(ierr); 2629a925c78cSMatthew G. Knepley } 26309044fa66SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr); 2631a925c78cSMatthew G. Knepley } 2632a925c78cSMatthew G. Knepley ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr); 2633a925c78cSMatthew G. Knepley if (mesh->printFEM) { 2634a925c78cSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr); 2635a8c5bd36SStefano Zampini ierr = VecView(Z, NULL);CHKERRQ(ierr); 2636a925c78cSMatthew G. Knepley } 26377a73cf09SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 26387a73cf09SMatthew G. Knepley ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 26397a73cf09SMatthew G. Knepley ierr = DMDestroy(&plexAux);CHKERRQ(ierr); 26407a73cf09SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 2641a925c78cSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 2642a925c78cSMatthew G. Knepley PetscFunctionReturn(0); 2643a925c78cSMatthew G. Knepley } 2644a925c78cSMatthew G. Knepley 264524cdb843SMatthew G. Knepley /*@ 264624cdb843SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 264724cdb843SMatthew G. Knepley 264824cdb843SMatthew G. Knepley Input Parameters: 264924cdb843SMatthew G. Knepley + dm - The mesh 265024cdb843SMatthew G. Knepley . X - Local input vector 265124cdb843SMatthew G. Knepley - user - The user context 265224cdb843SMatthew G. Knepley 265324cdb843SMatthew G. Knepley Output Parameter: 265424cdb843SMatthew G. Knepley . Jac - Jacobian matrix 265524cdb843SMatthew G. Knepley 265624cdb843SMatthew G. Knepley Note: 265724cdb843SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 265824cdb843SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 265924cdb843SMatthew G. Knepley 266024cdb843SMatthew G. Knepley Level: developer 266124cdb843SMatthew G. Knepley 266224cdb843SMatthew G. Knepley .seealso: FormFunctionLocal() 266324cdb843SMatthew G. Knepley @*/ 266424cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 266524cdb843SMatthew G. Knepley { 26666da023fcSToby Isaac DM plex; 2667f04eb4edSMatthew G. Knepley PetscDS prob; 26684a3e9fdbSToby Isaac IS cellIS; 2669f04eb4edSMatthew G. Knepley PetscBool hasJac, hasPrec; 26704a3e9fdbSToby Isaac PetscInt depth; 267124cdb843SMatthew G. Knepley PetscErrorCode ierr; 267224cdb843SMatthew G. Knepley 267324cdb843SMatthew G. Knepley PetscFunctionBegin; 26746da023fcSToby Isaac ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 26754a3e9fdbSToby Isaac ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr); 2676aeadca18SToby Isaac ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr); 26779044fa66SMatthew G. Knepley if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);} 2678f04eb4edSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 2679f04eb4edSMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 2680f04eb4edSMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 2681f04eb4edSMatthew G. Knepley if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);} 2682f04eb4edSMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 26834a3e9fdbSToby Isaac ierr = DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 26844a3e9fdbSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 26859a81d013SToby Isaac ierr = DMDestroy(&plex);CHKERRQ(ierr); 268624cdb843SMatthew G. Knepley PetscFunctionReturn(0); 268724cdb843SMatthew G. Knepley } 26881878804aSMatthew G. Knepley 268938cfc46eSPierre Jolivet /* 269038cfc46eSPierre Jolivet MatComputeNeumannOverlap - Computes an unassembled (Neumann) local overlapping Mat in nonlinear context. 269138cfc46eSPierre Jolivet 269238cfc46eSPierre Jolivet Input Parameters: 269338cfc46eSPierre Jolivet + X - SNES linearization point 269438cfc46eSPierre Jolivet . ovl - index set of overlapping subdomains 269538cfc46eSPierre Jolivet 269638cfc46eSPierre Jolivet Output Parameter: 269738cfc46eSPierre Jolivet . J - unassembled (Neumann) local matrix 269838cfc46eSPierre Jolivet 269938cfc46eSPierre Jolivet Level: intermediate 270038cfc46eSPierre Jolivet 270138cfc46eSPierre Jolivet .seealso: DMCreateNeumannOverlap(), MATIS, PCHPDDMSetAuxiliaryMat() 270238cfc46eSPierre Jolivet */ 270338cfc46eSPierre Jolivet static PetscErrorCode MatComputeNeumannOverlap_Plex(Mat J, PetscReal t, Vec X, Vec X_t, PetscReal s, IS ovl, void *ctx) 270438cfc46eSPierre Jolivet { 270538cfc46eSPierre Jolivet SNES snes; 270638cfc46eSPierre Jolivet Mat pJ; 270738cfc46eSPierre Jolivet DM ovldm,origdm; 270838cfc46eSPierre Jolivet DMSNES sdm; 270938cfc46eSPierre Jolivet PetscErrorCode (*bfun)(DM,Vec,void*); 271038cfc46eSPierre Jolivet PetscErrorCode (*jfun)(DM,Vec,Mat,Mat,void*); 271138cfc46eSPierre Jolivet void *bctx,*jctx; 271238cfc46eSPierre Jolivet PetscErrorCode ierr; 271338cfc46eSPierre Jolivet 271438cfc46eSPierre Jolivet PetscFunctionBegin; 271538cfc46eSPierre Jolivet ierr = PetscObjectQuery((PetscObject)ovl,"_DM_Overlap_HPDDM_MATIS",(PetscObject*)&pJ);CHKERRQ(ierr); 271638cfc46eSPierre Jolivet if (!pJ) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing overlapping Mat");CHKERRQ(ierr); 271738cfc46eSPierre Jolivet ierr = PetscObjectQuery((PetscObject)ovl,"_DM_Original_HPDDM",(PetscObject*)&origdm);CHKERRQ(ierr); 271838cfc46eSPierre Jolivet if (!origdm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing original DM");CHKERRQ(ierr); 271938cfc46eSPierre Jolivet ierr = MatGetDM(pJ,&ovldm);CHKERRQ(ierr); 272038cfc46eSPierre Jolivet ierr = DMSNESGetBoundaryLocal(origdm,&bfun,&bctx);CHKERRQ(ierr); 272138cfc46eSPierre Jolivet ierr = DMSNESSetBoundaryLocal(ovldm,bfun,bctx);CHKERRQ(ierr); 272238cfc46eSPierre Jolivet ierr = DMSNESGetJacobianLocal(origdm,&jfun,&jctx);CHKERRQ(ierr); 272338cfc46eSPierre Jolivet ierr = DMSNESSetJacobianLocal(ovldm,jfun,jctx);CHKERRQ(ierr); 272438cfc46eSPierre Jolivet ierr = PetscObjectQuery((PetscObject)ovl,"_DM_Overlap_HPDDM_SNES",(PetscObject*)&snes);CHKERRQ(ierr); 272538cfc46eSPierre Jolivet if (!snes) { 272638cfc46eSPierre Jolivet ierr = SNESCreate(PetscObjectComm((PetscObject)ovl),&snes);CHKERRQ(ierr); 272738cfc46eSPierre Jolivet ierr = SNESSetDM(snes,ovldm);CHKERRQ(ierr); 272838cfc46eSPierre Jolivet ierr = PetscObjectCompose((PetscObject)ovl,"_DM_Overlap_HPDDM_SNES",(PetscObject)snes);CHKERRQ(ierr); 272938cfc46eSPierre Jolivet ierr = PetscObjectDereference((PetscObject)snes);CHKERRQ(ierr); 273038cfc46eSPierre Jolivet } 273138cfc46eSPierre Jolivet ierr = DMGetDMSNES(ovldm,&sdm);CHKERRQ(ierr); 273238cfc46eSPierre Jolivet ierr = VecLockReadPush(X);CHKERRQ(ierr); 273338cfc46eSPierre Jolivet PetscStackPush("SNES user Jacobian function"); 273438cfc46eSPierre Jolivet ierr = (*sdm->ops->computejacobian)(snes,X,pJ,pJ,sdm->jacobianctx);CHKERRQ(ierr); 273538cfc46eSPierre Jolivet PetscStackPop; 273638cfc46eSPierre Jolivet ierr = VecLockReadPop(X);CHKERRQ(ierr); 273738cfc46eSPierre Jolivet /* this is a no-hop, just in case we decide to change the placeholder for the local Neumann matrix */ 273838cfc46eSPierre Jolivet { 273938cfc46eSPierre Jolivet Mat locpJ; 274038cfc46eSPierre Jolivet 274138cfc46eSPierre Jolivet ierr = MatISGetLocalMat(pJ,&locpJ);CHKERRQ(ierr); 274238cfc46eSPierre Jolivet ierr = MatCopy(locpJ,J,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 274338cfc46eSPierre Jolivet } 274438cfc46eSPierre Jolivet PetscFunctionReturn(0); 274538cfc46eSPierre Jolivet } 274638cfc46eSPierre Jolivet 2747a925c78cSMatthew G. Knepley /*@ 27489f520fc2SToby Isaac DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian. 27499f520fc2SToby Isaac 27509f520fc2SToby Isaac Input Parameters: 27519f520fc2SToby Isaac + dm - The DM object 2752dff059c6SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary()) 27539f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual()) 27549f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian()) 27551a244344SSatish Balay 27561a244344SSatish Balay Level: developer 27579f520fc2SToby Isaac @*/ 27589f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx) 27599f520fc2SToby Isaac { 27609f520fc2SToby Isaac PetscErrorCode ierr; 27619f520fc2SToby Isaac 27629f520fc2SToby Isaac PetscFunctionBegin; 27639f520fc2SToby Isaac ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr); 27649f520fc2SToby Isaac ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr); 27659f520fc2SToby Isaac ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr); 276638cfc46eSPierre Jolivet ierr = PetscObjectComposeFunction((PetscObject)dm,"MatComputeNeumannOverlap_C",MatComputeNeumannOverlap_Plex);CHKERRQ(ierr); 27679f520fc2SToby Isaac PetscFunctionReturn(0); 27689f520fc2SToby Isaac } 27699f520fc2SToby Isaac 2770f017bcb6SMatthew G. Knepley /*@C 2771f017bcb6SMatthew G. Knepley DMSNESCheckDiscretization - Check the discretization error of the exact solution 2772f017bcb6SMatthew G. Knepley 2773f017bcb6SMatthew G. Knepley Input Parameters: 2774f017bcb6SMatthew G. Knepley + snes - the SNES object 2775f017bcb6SMatthew G. Knepley . dm - the DM 2776f017bcb6SMatthew G. Knepley . u - a DM vector 2777f017bcb6SMatthew G. Knepley . exactFuncs - pointwise functions of the exact solution for each field 2778f017bcb6SMatthew G. Knepley . ctxs - contexts for the functions 2779f0fc11ceSJed Brown - tol - A tolerance for the check, or -1 to print the results instead 2780f017bcb6SMatthew G. Knepley 2781f3c94560SMatthew G. Knepley Output Parameters: 2782f3c94560SMatthew G. Knepley . error - An array which holds the discretization error in each field, or NULL 2783f3c94560SMatthew G. Knepley 2784f017bcb6SMatthew G. Knepley Level: developer 2785f017bcb6SMatthew G. Knepley 2786f017bcb6SMatthew G. Knepley .seealso: DNSNESCheckFromOptions(), DMSNESCheckResidual(), DMSNESCheckJacobian() 2787f017bcb6SMatthew G. Knepley @*/ 2788f3c94560SMatthew G. Knepley PetscErrorCode DMSNESCheckDiscretization(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs, PetscReal tol, PetscReal error[]) 27891878804aSMatthew G. Knepley { 2790f017bcb6SMatthew G. Knepley PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 2791f017bcb6SMatthew G. Knepley void **ectxs; 2792f017bcb6SMatthew G. Knepley MPI_Comm comm; 2793f017bcb6SMatthew G. Knepley PetscDS ds; 2794f3c94560SMatthew G. Knepley PetscReal *err; 2795f017bcb6SMatthew G. Knepley PetscInt Nf, f; 27961878804aSMatthew G. Knepley PetscErrorCode ierr; 27971878804aSMatthew G. Knepley 27981878804aSMatthew G. Knepley PetscFunctionBegin; 2799f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2800f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 2801f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 3); 2802534a8f05SLisandro Dalcin if (error) PetscValidRealPointer(error, 6); 2803f017bcb6SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) snes, &comm);CHKERRQ(ierr); 2804f017bcb6SMatthew G. Knepley ierr = DMGetDS(dm, &ds);CHKERRQ(ierr); 2805f017bcb6SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2806f3c94560SMatthew G. Knepley ierr = PetscMalloc3(Nf, &exacts, Nf, &ectxs, PetscMax(1, Nf), &err);CHKERRQ(ierr); 2807f017bcb6SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(ds, f, &exacts[f], &ectxs[f]);CHKERRQ(ierr);} 2808f017bcb6SMatthew G. Knepley ierr = DMProjectFunction(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr); 28094bee2e38SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr); 28104bee2e38SMatthew G. Knepley ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr); 28114bee2e38SMatthew G. Knepley ierr = VecViewFromOptions(u, NULL, "-vec_view");CHKERRQ(ierr); 2812f017bcb6SMatthew G. Knepley if (Nf > 1) { 2813f3c94560SMatthew G. Knepley ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs, u, err);CHKERRQ(ierr); 2814f017bcb6SMatthew G. Knepley if (tol >= 0.0) { 2815f017bcb6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2816f3c94560SMatthew G. Knepley if (err[f] > tol) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "L_2 Error %g for field %D exceeds tolerance %g", (double) err[f], f, (double) tol); 28171878804aSMatthew G. Knepley } 2818b878532fSMatthew G. Knepley } else if (error) { 2819b878532fSMatthew G. Knepley for (f = 0; f < Nf; ++f) error[f] = err[f]; 28201878804aSMatthew G. Knepley } else { 2821f017bcb6SMatthew G. Knepley ierr = PetscPrintf(comm, "L_2 Error: [");CHKERRQ(ierr); 2822f017bcb6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2823f017bcb6SMatthew G. Knepley if (f) {ierr = PetscPrintf(comm, ", ");CHKERRQ(ierr);} 2824f3c94560SMatthew G. Knepley ierr = PetscPrintf(comm, "%g", (double)err[f]);CHKERRQ(ierr); 28251878804aSMatthew G. Knepley } 2826f017bcb6SMatthew G. Knepley ierr = PetscPrintf(comm, "]\n");CHKERRQ(ierr); 2827f017bcb6SMatthew G. Knepley } 2828f017bcb6SMatthew G. Knepley } else { 2829f3c94560SMatthew G. Knepley ierr = DMComputeL2Diff(dm, 0.0, exactFuncs ? exactFuncs : exacts, ctxs ? ctxs : ectxs , u, &err[0]);CHKERRQ(ierr); 2830f017bcb6SMatthew G. Knepley if (tol >= 0.0) { 2831f3c94560SMatthew G. Knepley if (err[0] > tol) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "L_2 Error %g exceeds tolerance %g", (double) err[0], (double) tol); 2832b878532fSMatthew G. Knepley } else if (error) { 2833b878532fSMatthew G. Knepley error[0] = err[0]; 2834f017bcb6SMatthew G. Knepley } else { 2835f3c94560SMatthew G. Knepley ierr = PetscPrintf(comm, "L_2 Error: %g\n", (double)err[0]);CHKERRQ(ierr); 2836f017bcb6SMatthew G. Knepley } 2837f017bcb6SMatthew G. Knepley } 2838f3c94560SMatthew G. Knepley ierr = PetscFree3(exacts, ectxs, err);CHKERRQ(ierr); 2839f017bcb6SMatthew G. Knepley PetscFunctionReturn(0); 2840f017bcb6SMatthew G. Knepley } 2841f017bcb6SMatthew G. Knepley 2842f017bcb6SMatthew G. Knepley /*@C 2843f017bcb6SMatthew G. Knepley DMSNESCheckResidual - Check the residual of the exact solution 2844f017bcb6SMatthew G. Knepley 2845f017bcb6SMatthew G. Knepley Input Parameters: 2846f017bcb6SMatthew G. Knepley + snes - the SNES object 2847f017bcb6SMatthew G. Knepley . dm - the DM 2848f017bcb6SMatthew G. Knepley . u - a DM vector 2849f0fc11ceSJed Brown - tol - A tolerance for the check, or -1 to print the results instead 2850f017bcb6SMatthew G. Knepley 2851f3c94560SMatthew G. Knepley Output Parameters: 2852f3c94560SMatthew G. Knepley . residual - The residual norm of the exact solution, or NULL 2853f3c94560SMatthew G. Knepley 2854f017bcb6SMatthew G. Knepley Level: developer 2855f017bcb6SMatthew G. Knepley 2856f017bcb6SMatthew G. Knepley .seealso: DNSNESCheckFromOptions(), DMSNESCheckDiscretization(), DMSNESCheckJacobian() 2857f017bcb6SMatthew G. Knepley @*/ 2858f3c94560SMatthew G. Knepley PetscErrorCode DMSNESCheckResidual(SNES snes, DM dm, Vec u, PetscReal tol, PetscReal *residual) 2859f017bcb6SMatthew G. Knepley { 2860f3c94560SMatthew G. Knepley PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 2861f3c94560SMatthew G. Knepley void **ectxs; 2862f017bcb6SMatthew G. Knepley MPI_Comm comm; 2863f3c94560SMatthew G. Knepley PetscDS ds; 2864f017bcb6SMatthew G. Knepley Vec r; 2865f017bcb6SMatthew G. Knepley PetscReal res; 2866f3c94560SMatthew G. Knepley PetscInt Nf, f; 2867f3c94560SMatthew G. Knepley PetscBool computeSol = PETSC_FALSE; 2868f017bcb6SMatthew G. Knepley PetscErrorCode ierr; 2869f017bcb6SMatthew G. Knepley 2870f017bcb6SMatthew G. Knepley PetscFunctionBegin; 2871f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2872f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 2873f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 3); 2874534a8f05SLisandro Dalcin if (residual) PetscValidRealPointer(residual, 5); 2875f017bcb6SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) snes, &comm);CHKERRQ(ierr); 2876f3c94560SMatthew G. Knepley ierr = DMGetDS(dm, &ds);CHKERRQ(ierr); 2877f3c94560SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2878f3c94560SMatthew G. Knepley ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr); 2879f3c94560SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2880f3c94560SMatthew G. Knepley ierr = PetscDSGetExactSolution(ds, f, &exacts[f], &ectxs[f]);CHKERRQ(ierr); 2881f3c94560SMatthew G. Knepley if (exacts[f]) computeSol = PETSC_TRUE; 2882f3c94560SMatthew G. Knepley } 2883f3c94560SMatthew G. Knepley if (computeSol) {ierr = DMProjectFunction(dm, 0.0, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);} 2884f3c94560SMatthew G. Knepley ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr); 2885f3c94560SMatthew G. Knepley 2886f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 28871878804aSMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 28881878804aSMatthew G. Knepley ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr); 2889f017bcb6SMatthew G. Knepley if (tol >= 0.0) { 2890f017bcb6SMatthew G. Knepley if (res > tol) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "L_2 Residual %g exceeds tolerance %g", (double) res, (double) tol); 2891b878532fSMatthew G. Knepley } else if (residual) { 2892b878532fSMatthew G. Knepley *residual = res; 2893f017bcb6SMatthew G. Knepley } else { 2894f017bcb6SMatthew G. Knepley ierr = PetscPrintf(comm, "L_2 Residual: %g\n", (double)res);CHKERRQ(ierr); 28951878804aSMatthew G. Knepley ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr); 28961878804aSMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr); 2897685405a1SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr); 2898685405a1SBarry Smith ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr); 2899f017bcb6SMatthew G. Knepley } 2900f017bcb6SMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 2901f017bcb6SMatthew G. Knepley PetscFunctionReturn(0); 2902f017bcb6SMatthew G. Knepley } 2903f017bcb6SMatthew G. Knepley 2904f017bcb6SMatthew G. Knepley /*@C 2905f3c94560SMatthew G. Knepley DMSNESCheckJacobian - Check the Jacobian of the exact solution against the residual using the Taylor Test 2906f017bcb6SMatthew G. Knepley 2907f017bcb6SMatthew G. Knepley Input Parameters: 2908f017bcb6SMatthew G. Knepley + snes - the SNES object 2909f017bcb6SMatthew G. Knepley . dm - the DM 2910f017bcb6SMatthew G. Knepley . u - a DM vector 2911f0fc11ceSJed Brown - tol - A tolerance for the check, or -1 to print the results instead 2912f017bcb6SMatthew G. Knepley 2913f3c94560SMatthew G. Knepley Output Parameters: 2914f3c94560SMatthew G. Knepley + isLinear - Flag indicaing that the function looks linear, or NULL 2915f3c94560SMatthew G. Knepley - convRate - The rate of convergence of the linear model, or NULL 2916f3c94560SMatthew G. Knepley 2917f017bcb6SMatthew G. Knepley Level: developer 2918f017bcb6SMatthew G. Knepley 2919f017bcb6SMatthew G. Knepley .seealso: DNSNESCheckFromOptions(), DMSNESCheckDiscretization(), DMSNESCheckResidual() 2920f017bcb6SMatthew G. Knepley @*/ 2921f3c94560SMatthew G. Knepley PetscErrorCode DMSNESCheckJacobian(SNES snes, DM dm, Vec u, PetscReal tol, PetscBool *isLinear, PetscReal *convRate) 2922f017bcb6SMatthew G. Knepley { 2923f3c94560SMatthew G. Knepley PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 2924f3c94560SMatthew G. Knepley void **ectxs; 2925f017bcb6SMatthew G. Knepley MPI_Comm comm; 2926f017bcb6SMatthew G. Knepley PetscDS ds; 2927f017bcb6SMatthew G. Knepley Mat J, M; 2928f017bcb6SMatthew G. Knepley MatNullSpace nullspace; 2929f3c94560SMatthew G. Knepley PetscReal slope, intercept; 2930f3c94560SMatthew G. Knepley PetscInt Nf, f; 2931f3c94560SMatthew G. Knepley PetscBool hasJac, hasPrec, isLin = PETSC_FALSE, computeSol = PETSC_FALSE; 2932f017bcb6SMatthew G. Knepley PetscErrorCode ierr; 2933f017bcb6SMatthew G. Knepley 2934f017bcb6SMatthew G. Knepley PetscFunctionBegin; 2935f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2936f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 2937f3c94560SMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 3); 2938534a8f05SLisandro Dalcin if (isLinear) PetscValidBoolPointer(isLinear, 5); 2939534a8f05SLisandro Dalcin if (convRate) PetscValidRealPointer(convRate, 5); 2940f017bcb6SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject) snes, &comm);CHKERRQ(ierr); 2941f017bcb6SMatthew G. Knepley ierr = DMGetDS(dm, &ds);CHKERRQ(ierr); 2942f3c94560SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2943f3c94560SMatthew G. Knepley ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr); 2944f3c94560SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 2945f3c94560SMatthew G. Knepley ierr = PetscDSGetExactSolution(ds, f, &exacts[f], &ectxs[f]);CHKERRQ(ierr); 2946f3c94560SMatthew G. Knepley if (exacts[f]) computeSol = PETSC_TRUE; 2947f3c94560SMatthew G. Knepley } 2948f3c94560SMatthew G. Knepley if (computeSol) {ierr = DMProjectFunction(dm, 0.0, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);} 2949f3c94560SMatthew G. Knepley ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr); 2950f3c94560SMatthew G. Knepley 2951f017bcb6SMatthew G. Knepley /* Create and view matrices */ 2952f017bcb6SMatthew G. Knepley ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr); 2953f017bcb6SMatthew G. Knepley ierr = PetscDSHasJacobian(ds, &hasJac);CHKERRQ(ierr); 2954f017bcb6SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(ds, &hasPrec);CHKERRQ(ierr); 2955282e7bb4SMatthew G. Knepley if (hasJac && hasPrec) { 2956282e7bb4SMatthew G. Knepley ierr = DMCreateMatrix(dm, &M);CHKERRQ(ierr); 2957282e7bb4SMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, J, M);CHKERRQ(ierr); 2958f017bcb6SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) M, "Preconditioning Matrix");CHKERRQ(ierr); 2959282e7bb4SMatthew G. Knepley ierr = PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");CHKERRQ(ierr); 2960282e7bb4SMatthew G. Knepley ierr = MatViewFromOptions(M, NULL, "-mat_view");CHKERRQ(ierr); 2961282e7bb4SMatthew G. Knepley ierr = MatDestroy(&M);CHKERRQ(ierr); 2962282e7bb4SMatthew G. Knepley } else { 2963282e7bb4SMatthew G. Knepley ierr = SNESComputeJacobian(snes, u, J, J);CHKERRQ(ierr); 2964282e7bb4SMatthew G. Knepley } 2965f017bcb6SMatthew G. Knepley ierr = PetscObjectSetName((PetscObject) J, "Jacobian");CHKERRQ(ierr); 2966282e7bb4SMatthew G. Knepley ierr = PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");CHKERRQ(ierr); 2967282e7bb4SMatthew G. Knepley ierr = MatViewFromOptions(J, NULL, "-mat_view");CHKERRQ(ierr); 2968f017bcb6SMatthew G. Knepley /* Check nullspace */ 2969f017bcb6SMatthew G. Knepley ierr = MatGetNullSpace(J, &nullspace);CHKERRQ(ierr); 2970f017bcb6SMatthew G. Knepley if (nullspace) { 29711878804aSMatthew G. Knepley PetscBool isNull; 2972f017bcb6SMatthew G. Knepley ierr = MatNullSpaceTest(nullspace, J, &isNull);CHKERRQ(ierr); 2973f017bcb6SMatthew G. Knepley if (!isNull) SETERRQ(comm, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid."); 29741878804aSMatthew G. Knepley } 2975f017bcb6SMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullspace);CHKERRQ(ierr); 2976f017bcb6SMatthew G. Knepley /* Taylor test */ 2977f017bcb6SMatthew G. Knepley { 2978f017bcb6SMatthew G. Knepley PetscRandom rand; 2979f017bcb6SMatthew G. Knepley Vec du, uhat, r, rhat, df; 2980f3c94560SMatthew G. Knepley PetscReal h; 2981f017bcb6SMatthew G. Knepley PetscReal *es, *hs, *errors; 2982f017bcb6SMatthew G. Knepley PetscReal hMax = 1.0, hMin = 1e-6, hMult = 0.1; 2983f017bcb6SMatthew G. Knepley PetscInt Nv, v; 2984f017bcb6SMatthew G. Knepley 2985f017bcb6SMatthew G. Knepley /* Choose a perturbation direction */ 2986f017bcb6SMatthew G. Knepley ierr = PetscRandomCreate(comm, &rand);CHKERRQ(ierr); 2987f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &du);CHKERRQ(ierr); 2988f017bcb6SMatthew G. Knepley ierr = VecSetRandom(du, rand); CHKERRQ(ierr); 2989f017bcb6SMatthew G. Knepley ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr); 2990f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &df);CHKERRQ(ierr); 2991f017bcb6SMatthew G. Knepley ierr = MatMult(J, du, df);CHKERRQ(ierr); 2992f017bcb6SMatthew G. Knepley /* Evaluate residual at u, F(u), save in vector r */ 2993f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &r);CHKERRQ(ierr); 2994f017bcb6SMatthew G. Knepley ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr); 2995f017bcb6SMatthew G. Knepley /* Look at the convergence of our Taylor approximation as we approach u */ 2996f017bcb6SMatthew G. Knepley for (h = hMax, Nv = 0; h >= hMin; h *= hMult, ++Nv); 2997f017bcb6SMatthew G. Knepley ierr = PetscCalloc3(Nv, &es, Nv, &hs, Nv, &errors);CHKERRQ(ierr); 2998f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &uhat);CHKERRQ(ierr); 2999f017bcb6SMatthew G. Knepley ierr = VecDuplicate(u, &rhat);CHKERRQ(ierr); 3000f017bcb6SMatthew G. Knepley for (h = hMax, Nv = 0; h >= hMin; h *= hMult, ++Nv) { 3001f017bcb6SMatthew G. Knepley ierr = VecWAXPY(uhat, h, du, u);CHKERRQ(ierr); 3002f017bcb6SMatthew G. Knepley /* F(\hat u) \approx F(u) + J(u) (uhat - u) = F(u) + h * J(u) du */ 3003f017bcb6SMatthew G. Knepley ierr = SNESComputeFunction(snes, uhat, rhat);CHKERRQ(ierr); 3004f017bcb6SMatthew G. Knepley ierr = VecAXPBYPCZ(rhat, -1.0, -h, 1.0, r, df);CHKERRQ(ierr); 3005f017bcb6SMatthew G. Knepley ierr = VecNorm(rhat, NORM_2, &errors[Nv]);CHKERRQ(ierr); 3006f017bcb6SMatthew G. Knepley 3007f017bcb6SMatthew G. Knepley es[Nv] = PetscLog10Real(errors[Nv]); 3008f017bcb6SMatthew G. Knepley hs[Nv] = PetscLog10Real(h); 3009f017bcb6SMatthew G. Knepley } 3010f017bcb6SMatthew G. Knepley ierr = VecDestroy(&uhat);CHKERRQ(ierr); 3011f017bcb6SMatthew G. Knepley ierr = VecDestroy(&rhat);CHKERRQ(ierr); 3012f017bcb6SMatthew G. Knepley ierr = VecDestroy(&df);CHKERRQ(ierr); 30131878804aSMatthew G. Knepley ierr = VecDestroy(&r);CHKERRQ(ierr); 3014f017bcb6SMatthew G. Knepley ierr = VecDestroy(&du);CHKERRQ(ierr); 3015f017bcb6SMatthew G. Knepley for (v = 0; v < Nv; ++v) { 3016f017bcb6SMatthew G. Knepley if ((tol >= 0) && (errors[v] > tol)) break; 3017f017bcb6SMatthew G. Knepley else if (errors[v] > PETSC_SMALL) break; 3018f017bcb6SMatthew G. Knepley } 3019f3c94560SMatthew G. Knepley if (v == Nv) isLin = PETSC_TRUE; 3020f017bcb6SMatthew G. Knepley ierr = PetscLinearRegression(Nv, hs, es, &slope, &intercept);CHKERRQ(ierr); 3021f017bcb6SMatthew G. Knepley ierr = PetscFree3(es, hs, errors);CHKERRQ(ierr); 3022f017bcb6SMatthew G. Knepley /* Slope should be about 2 */ 3023f017bcb6SMatthew G. Knepley if (tol >= 0) { 3024b878532fSMatthew G. Knepley if (!isLin && PetscAbsReal(2 - slope) > tol) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Taylor approximation convergence rate should be 2, not %0.2f", (double) slope); 3025b878532fSMatthew G. Knepley } else if (isLinear || convRate) { 3026b878532fSMatthew G. Knepley if (isLinear) *isLinear = isLin; 3027b878532fSMatthew G. Knepley if (convRate) *convRate = slope; 3028f017bcb6SMatthew G. Knepley } else { 3029f3c94560SMatthew G. Knepley if (!isLin) {ierr = PetscPrintf(comm, "Taylor approximation converging at order %3.2f\n", (double) slope);CHKERRQ(ierr);} 3030f017bcb6SMatthew G. Knepley else {ierr = PetscPrintf(comm, "Function appears to be linear\n");CHKERRQ(ierr);} 3031f017bcb6SMatthew G. Knepley } 3032f017bcb6SMatthew G. Knepley } 30331878804aSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 30341878804aSMatthew G. Knepley PetscFunctionReturn(0); 30351878804aSMatthew G. Knepley } 30361878804aSMatthew G. Knepley 3037f017bcb6SMatthew G. Knepley PetscErrorCode DMSNESCheck_Internal(SNES snes, DM dm, Vec u, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs) 3038f017bcb6SMatthew G. Knepley { 3039f017bcb6SMatthew G. Knepley PetscErrorCode ierr; 3040f017bcb6SMatthew G. Knepley 3041f017bcb6SMatthew G. Knepley PetscFunctionBegin; 3042f3c94560SMatthew G. Knepley ierr = DMSNESCheckDiscretization(snes, dm, u, exactFuncs, ctxs, -1.0, NULL);CHKERRQ(ierr); 3043f3c94560SMatthew G. Knepley ierr = DMSNESCheckResidual(snes, dm, u, -1.0, NULL);CHKERRQ(ierr); 3044f3c94560SMatthew G. Knepley ierr = DMSNESCheckJacobian(snes, dm, u, -1.0, NULL, NULL);CHKERRQ(ierr); 3045f017bcb6SMatthew G. Knepley PetscFunctionReturn(0); 3046f017bcb6SMatthew G. Knepley } 3047f017bcb6SMatthew G. Knepley 3048bee9a294SMatthew G. Knepley /*@C 3049bee9a294SMatthew G. Knepley DMSNESCheckFromOptions - Check the residual and Jacobian functions using the exact solution by outputting some diagnostic information 3050bee9a294SMatthew G. Knepley 3051bee9a294SMatthew G. Knepley Input Parameters: 3052bee9a294SMatthew G. Knepley + snes - the SNES object 3053bee9a294SMatthew G. Knepley . u - representative SNES vector 3054bee9a294SMatthew G. Knepley . exactFuncs - pointwise functions of the exact solution for each field 3055bee9a294SMatthew G. Knepley - ctxs - contexts for the functions 3056bee9a294SMatthew G. Knepley 3057bee9a294SMatthew G. Knepley Level: developer 3058bee9a294SMatthew G. Knepley @*/ 30590163fd50SMatthew 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) 30601878804aSMatthew G. Knepley { 30611878804aSMatthew G. Knepley DM dm; 30621878804aSMatthew G. Knepley Vec sol; 30631878804aSMatthew G. Knepley PetscBool check; 30641878804aSMatthew G. Knepley PetscErrorCode ierr; 30651878804aSMatthew G. Knepley 30661878804aSMatthew G. Knepley PetscFunctionBegin; 3067c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr); 30681878804aSMatthew G. Knepley if (!check) PetscFunctionReturn(0); 30691878804aSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 30701878804aSMatthew G. Knepley ierr = VecDuplicate(u, &sol);CHKERRQ(ierr); 30711878804aSMatthew G. Knepley ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr); 3072f017bcb6SMatthew G. Knepley ierr = DMSNESCheck_Internal(snes, dm, sol, exactFuncs, ctxs);CHKERRQ(ierr); 30731878804aSMatthew G. Knepley ierr = VecDestroy(&sol);CHKERRQ(ierr); 3074552f7358SJed Brown PetscFunctionReturn(0); 3075552f7358SJed Brown } 3076