1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2da97024aSMatthew G. Knepley #include <petscsf.h> 3cb1e1211SMatthew G Knepley 4e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h> 5af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 6af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h> 7a0845e3aSMatthew G. Knepley 8*9b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx) 9*9b6f715bSMatthew G. Knepley { 10*9b6f715bSMatthew G. Knepley PetscFEGeom *geom = (PetscFEGeom *) ctx; 11*9b6f715bSMatthew G. Knepley PetscErrorCode ierr; 12*9b6f715bSMatthew G. Knepley 13*9b6f715bSMatthew G. Knepley PetscFunctionBegin; 14*9b6f715bSMatthew G. Knepley ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr); 15*9b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 16*9b6f715bSMatthew G. Knepley } 17*9b6f715bSMatthew G. Knepley 18*9b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 19*9b6f715bSMatthew G. Knepley { 20*9b6f715bSMatthew G. Knepley char composeStr[33] = {0}; 21*9b6f715bSMatthew G. Knepley PetscObjectId id; 22*9b6f715bSMatthew G. Knepley PetscContainer container; 23*9b6f715bSMatthew G. Knepley PetscErrorCode ierr; 24*9b6f715bSMatthew G. Knepley 25*9b6f715bSMatthew G. Knepley PetscFunctionBegin; 26*9b6f715bSMatthew G. Knepley ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr); 27*9b6f715bSMatthew G. Knepley ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr); 28*9b6f715bSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr); 29*9b6f715bSMatthew G. Knepley if (container) { 30*9b6f715bSMatthew G. Knepley ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr); 31*9b6f715bSMatthew G. Knepley } else { 32*9b6f715bSMatthew G. Knepley ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr); 33*9b6f715bSMatthew G. Knepley ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 34*9b6f715bSMatthew G. Knepley ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr); 35*9b6f715bSMatthew G. Knepley ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr); 36*9b6f715bSMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr); 37*9b6f715bSMatthew G. Knepley ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 38*9b6f715bSMatthew G. Knepley } 39*9b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 40*9b6f715bSMatthew G. Knepley } 41*9b6f715bSMatthew G. Knepley 42*9b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 43*9b6f715bSMatthew G. Knepley { 44*9b6f715bSMatthew G. Knepley PetscFunctionBegin; 45*9b6f715bSMatthew G. Knepley *geom = NULL; 46*9b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 47*9b6f715bSMatthew G. Knepley } 48*9b6f715bSMatthew G. Knepley 4946fa42a0SMatthew G. Knepley /*@ 5046fa42a0SMatthew G. Knepley DMPlexGetScale - Get the scale for the specified fundamental unit 5146fa42a0SMatthew G. Knepley 5246fa42a0SMatthew G. Knepley Not collective 5346fa42a0SMatthew G. Knepley 5446fa42a0SMatthew G. Knepley Input Arguments: 5546fa42a0SMatthew G. Knepley + dm - the DM 5646fa42a0SMatthew G. Knepley - unit - The SI unit 5746fa42a0SMatthew G. Knepley 5846fa42a0SMatthew G. Knepley Output Argument: 5946fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit 6046fa42a0SMatthew G. Knepley 6146fa42a0SMatthew G. Knepley Level: advanced 6246fa42a0SMatthew G. Knepley 6346fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit 6446fa42a0SMatthew G. Knepley @*/ 65cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 66cb1e1211SMatthew G Knepley { 67cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 68cb1e1211SMatthew G Knepley 69cb1e1211SMatthew G Knepley PetscFunctionBegin; 70cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 71cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 72cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 73cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 74cb1e1211SMatthew G Knepley } 75cb1e1211SMatthew G Knepley 7646fa42a0SMatthew G. Knepley /*@ 7746fa42a0SMatthew G. Knepley DMPlexSetScale - Set the scale for the specified fundamental unit 7846fa42a0SMatthew G. Knepley 7946fa42a0SMatthew G. Knepley Not collective 8046fa42a0SMatthew G. Knepley 8146fa42a0SMatthew G. Knepley Input Arguments: 8246fa42a0SMatthew G. Knepley + dm - the DM 8346fa42a0SMatthew G. Knepley . unit - The SI unit 8446fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit 8546fa42a0SMatthew G. Knepley 8646fa42a0SMatthew G. Knepley Level: advanced 8746fa42a0SMatthew G. Knepley 8846fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit 8946fa42a0SMatthew G. Knepley @*/ 90cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 91cb1e1211SMatthew G Knepley { 92cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 93cb1e1211SMatthew G Knepley 94cb1e1211SMatthew G Knepley PetscFunctionBegin; 95cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 96cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 97cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 98cb1e1211SMatthew G Knepley } 99cb1e1211SMatthew G Knepley 100d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx) 101026175e5SToby Isaac { 10212adca46SMatthew G. Knepley const PetscInt eps[3][3][3] = {{{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{0, 0, -1}, {0, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0}}}; 103026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *) ctx; 104ad917190SMatthew G. Knepley PetscInt dim2 = ctxInt[0]; 105026175e5SToby Isaac PetscInt d = ctxInt[1]; 106026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 107026175e5SToby Isaac 108ad917190SMatthew G. Knepley PetscFunctionBegin; 109ad917190SMatthew G. Knepley if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2); 110026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 111026175e5SToby Isaac if (d < dim) { 11212adca46SMatthew G. Knepley mode[d] = 1.; /* Translation along axis d */ 113ad917190SMatthew G. Knepley } else { 114026175e5SToby Isaac for (i = 0; i < dim; i++) { 115026175e5SToby Isaac for (j = 0; j < dim; j++) { 11612adca46SMatthew G. Knepley mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */ 117026175e5SToby Isaac } 118026175e5SToby Isaac } 119026175e5SToby Isaac } 120ad917190SMatthew G. Knepley PetscFunctionReturn(0); 121026175e5SToby Isaac } 122026175e5SToby Isaac 123cb1e1211SMatthew G Knepley /*@C 12412adca46SMatthew G. Knepley DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation 125cb1e1211SMatthew G Knepley 126cb1e1211SMatthew G Knepley Collective on DM 127cb1e1211SMatthew G Knepley 128cb1e1211SMatthew G Knepley Input Arguments: 129026175e5SToby Isaac . dm - the DM 130cb1e1211SMatthew G Knepley 131cb1e1211SMatthew G Knepley Output Argument: 132cb1e1211SMatthew G Knepley . sp - the null space 133cb1e1211SMatthew G Knepley 13412adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 135cb1e1211SMatthew G Knepley 136cb1e1211SMatthew G Knepley Level: advanced 137cb1e1211SMatthew G Knepley 13812adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG 139cb1e1211SMatthew G Knepley @*/ 140026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp) 141cb1e1211SMatthew G Knepley { 142cb1e1211SMatthew G Knepley MPI_Comm comm; 143026175e5SToby Isaac Vec mode[6]; 144026175e5SToby Isaac PetscSection section, globalSection; 1459d8fbdeaSMatthew G. Knepley PetscInt dim, dimEmbed, n, m, d, i, j; 146cb1e1211SMatthew G Knepley PetscErrorCode ierr; 147cb1e1211SMatthew G Knepley 148cb1e1211SMatthew G Knepley PetscFunctionBegin; 149cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 150c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1519d8fbdeaSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 152cb1e1211SMatthew G Knepley if (dim == 1) { 153cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 154cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 155cb1e1211SMatthew G Knepley } 156e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 157e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 158cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 159cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 160cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 161cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 162cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 163cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 164026175e5SToby Isaac for (d = 0; d < m; d++) { 165330485fdSToby Isaac PetscInt ctx[2]; 166d1828a1cSMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private; 167026175e5SToby Isaac void *voidctx = (void *) (&ctx[0]); 168cb1e1211SMatthew G Knepley 1699d8fbdeaSMatthew G. Knepley ctx[0] = dimEmbed; 170330485fdSToby Isaac ctx[1] = d; 1710709b2feSToby Isaac ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 172cb1e1211SMatthew G Knepley } 173cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 174cb1e1211SMatthew G Knepley /* Orthonormalize system */ 175cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 176cb1e1211SMatthew G Knepley PetscScalar dots[6]; 177cb1e1211SMatthew G Knepley 178cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 179cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 180cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 181cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 182cb1e1211SMatthew G Knepley } 183cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 184cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 185cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 186cb1e1211SMatthew G Knepley } 187cb1e1211SMatthew G Knepley 18812adca46SMatthew G. Knepley /*@C 18912adca46SMatthew G. Knepley DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation 19012adca46SMatthew G. Knepley 19112adca46SMatthew G. Knepley Collective on DM 19212adca46SMatthew G. Knepley 19312adca46SMatthew G. Knepley Input Arguments: 19412adca46SMatthew G. Knepley + dm - the DM 19512adca46SMatthew G. Knepley . nb - The number of bodies 19612adca46SMatthew G. Knepley . label - The DMLabel marking each domain 19712adca46SMatthew G. Knepley . nids - The number of ids per body 19812adca46SMatthew G. Knepley - ids - An array of the label ids in sequence for each domain 19912adca46SMatthew G. Knepley 20012adca46SMatthew G. Knepley Output Argument: 20112adca46SMatthew G. Knepley . sp - the null space 20212adca46SMatthew G. Knepley 20312adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 20412adca46SMatthew G. Knepley 20512adca46SMatthew G. Knepley Level: advanced 20612adca46SMatthew G. Knepley 20712adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate() 20812adca46SMatthew G. Knepley @*/ 20912adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp) 21012adca46SMatthew G. Knepley { 21112adca46SMatthew G. Knepley MPI_Comm comm; 21212adca46SMatthew G. Knepley PetscSection section, globalSection; 21312adca46SMatthew G. Knepley Vec *mode; 21412adca46SMatthew G. Knepley PetscScalar *dots; 21512adca46SMatthew G. Knepley PetscInt dim, dimEmbed, n, m, b, d, i, j, off; 21612adca46SMatthew G. Knepley PetscErrorCode ierr; 21712adca46SMatthew G. Knepley 21812adca46SMatthew G. Knepley PetscFunctionBegin; 21912adca46SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 22012adca46SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 22112adca46SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 222e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 223e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 22412adca46SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 22512adca46SMatthew G. Knepley m = nb * (dim*(dim+1))/2; 22612adca46SMatthew G. Knepley ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr); 22712adca46SMatthew G. Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 22812adca46SMatthew G. Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 22912adca46SMatthew G. Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 23012adca46SMatthew G. Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 23112adca46SMatthew G. Knepley for (b = 0, off = 0; b < nb; ++b) { 23212adca46SMatthew G. Knepley for (d = 0; d < m/nb; ++d) { 23312adca46SMatthew G. Knepley PetscInt ctx[2]; 23412adca46SMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private; 23512adca46SMatthew G. Knepley void *voidctx = (void *) (&ctx[0]); 23612adca46SMatthew G. Knepley 23712adca46SMatthew G. Knepley ctx[0] = dimEmbed; 23812adca46SMatthew G. Knepley ctx[1] = d; 23912adca46SMatthew G. Knepley ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 24012adca46SMatthew G. Knepley off += nids[b]; 24112adca46SMatthew G. Knepley } 24212adca46SMatthew G. Knepley } 24312adca46SMatthew G. Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 24412adca46SMatthew G. Knepley /* Orthonormalize system */ 24512adca46SMatthew G. Knepley for (i = 0; i < m; ++i) { 24612adca46SMatthew G. Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 24712adca46SMatthew G. Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 24812adca46SMatthew G. Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 24912adca46SMatthew G. Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 25012adca46SMatthew G. Knepley } 25112adca46SMatthew G. Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 25212adca46SMatthew G. Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 25312adca46SMatthew G. Knepley ierr = PetscFree2(mode, dots);CHKERRQ(ierr); 25412adca46SMatthew G. Knepley PetscFunctionReturn(0); 25512adca46SMatthew G. Knepley } 25612adca46SMatthew G. Knepley 257b29cfa1cSToby Isaac /*@ 258b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 259b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 260b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 261b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 262b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 263b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 264b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 265b29cfa1cSToby Isaac 266b29cfa1cSToby Isaac Input Parameters: 267b29cfa1cSToby Isaac + dm - the DMPlex object 268b29cfa1cSToby Isaac - height - the maximum projection height >= 0 269b29cfa1cSToby Isaac 270b29cfa1cSToby Isaac Level: advanced 271b29cfa1cSToby Isaac 2724d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 273b29cfa1cSToby Isaac @*/ 274b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 275b29cfa1cSToby Isaac { 276b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 277b29cfa1cSToby Isaac 278b29cfa1cSToby Isaac PetscFunctionBegin; 279b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 280b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 281b29cfa1cSToby Isaac PetscFunctionReturn(0); 282b29cfa1cSToby Isaac } 283b29cfa1cSToby Isaac 284b29cfa1cSToby Isaac /*@ 285b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 286b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 287b29cfa1cSToby Isaac 288b29cfa1cSToby Isaac Input Parameters: 289b29cfa1cSToby Isaac . dm - the DMPlex object 290b29cfa1cSToby Isaac 291b29cfa1cSToby Isaac Output Parameters: 292b29cfa1cSToby Isaac . height - the maximum projection height 293b29cfa1cSToby Isaac 294b29cfa1cSToby Isaac Level: intermediate 295b29cfa1cSToby Isaac 2964d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 297b29cfa1cSToby Isaac @*/ 298b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 299b29cfa1cSToby Isaac { 300b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 301b29cfa1cSToby Isaac 302b29cfa1cSToby Isaac PetscFunctionBegin; 303b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 304b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 305b29cfa1cSToby Isaac PetscFunctionReturn(0); 306b29cfa1cSToby Isaac } 307b29cfa1cSToby Isaac 308b278463cSMatthew G. Knepley /*@C 309b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector 310b278463cSMatthew G. Knepley 311b278463cSMatthew G. Knepley Input Parameters: 312b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 313b278463cSMatthew G. Knepley . time - The time 314b278463cSMatthew G. Knepley . field - The field to constrain 3151c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 3161c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 317b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 318b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 319b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 320b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 321b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 322b278463cSMatthew G. Knepley 323b278463cSMatthew G. Knepley Output Parameter: 324b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 325b278463cSMatthew G. Knepley 326b278463cSMatthew G. Knepley Level: developer 327b278463cSMatthew G. Knepley 328b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 329b278463cSMatthew G. Knepley @*/ 3301c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssential(DM dm, PetscReal time, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX) 33155f2e967SMatthew G. Knepley { 3320163fd50SMatthew G. Knepley PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 33355f2e967SMatthew G. Knepley void **ctxs; 334d7ddef95SMatthew G. Knepley PetscInt numFields; 335d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 336d7ddef95SMatthew G. Knepley 337d7ddef95SMatthew G. Knepley PetscFunctionBegin; 338d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 339d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 340d7ddef95SMatthew G. Knepley funcs[field] = func; 341d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 3421c531cf8SMatthew G. Knepley ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 343d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 344d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 345d7ddef95SMatthew G. Knepley } 346d7ddef95SMatthew G. Knepley 347b278463cSMatthew G. Knepley /*@C 348b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector 349b278463cSMatthew G. Knepley 350b278463cSMatthew G. Knepley Input Parameters: 351b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 352b278463cSMatthew G. Knepley . time - The time 353b278463cSMatthew G. Knepley . locU - A local vector with the input solution values 354b278463cSMatthew G. Knepley . field - The field to constrain 3551c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 3561c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 357b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 358b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 359b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 360b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 361b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 362b278463cSMatthew G. Knepley 363b278463cSMatthew G. Knepley Output Parameter: 364b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 365b278463cSMatthew G. Knepley 366b278463cSMatthew G. Knepley Level: developer 367b278463cSMatthew G. Knepley 368b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary() 369b278463cSMatthew G. Knepley @*/ 3701c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], 371c60e475cSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 372c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 373c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 37497b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], 375b278463cSMatthew G. Knepley PetscScalar[]), 376b278463cSMatthew G. Knepley void *ctx, Vec locX) 377c60e475cSMatthew G. Knepley { 378c60e475cSMatthew G. Knepley void (**funcs)(PetscInt, PetscInt, PetscInt, 379c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 380c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 38197b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]); 382c60e475cSMatthew G. Knepley void **ctxs; 383c60e475cSMatthew G. Knepley PetscInt numFields; 384c60e475cSMatthew G. Knepley PetscErrorCode ierr; 385c60e475cSMatthew G. Knepley 386c60e475cSMatthew G. Knepley PetscFunctionBegin; 387c60e475cSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 388c60e475cSMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 389c60e475cSMatthew G. Knepley funcs[field] = func; 390c60e475cSMatthew G. Knepley ctxs[field] = ctx; 3911c531cf8SMatthew G. Knepley ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 392c60e475cSMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 393c60e475cSMatthew G. Knepley PetscFunctionReturn(0); 394c60e475cSMatthew G. Knepley } 395c60e475cSMatthew G. Knepley 396b278463cSMatthew G. Knepley /*@C 397b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector 398b278463cSMatthew G. Knepley 399b278463cSMatthew G. Knepley Input Parameters: 400b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 401b278463cSMatthew G. Knepley . time - The time 402b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information 403b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information 404b278463cSMatthew G. Knepley . Grad - A vector with the FVM cell gradient information 405b278463cSMatthew G. Knepley . field - The field to constrain 4061c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 4071c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 408b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 409b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 410b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 411b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 412b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 413b278463cSMatthew G. Knepley 414b278463cSMatthew G. Knepley Output Parameter: 415b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 416b278463cSMatthew G. Knepley 417b278463cSMatthew G. Knepley Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary() 418b278463cSMatthew G. Knepley 419b278463cSMatthew G. Knepley Level: developer 420b278463cSMatthew G. Knepley 421b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 422b278463cSMatthew G. Knepley @*/ 4231c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesRiemann(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], 424b278463cSMatthew G. Knepley PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX) 425d7ddef95SMatthew G. Knepley { 42661f58d28SMatthew G. Knepley PetscDS prob; 427da97024aSMatthew G. Knepley PetscSF sf; 428d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 42920369375SToby Isaac const PetscScalar *facegeom, *cellgeom = NULL, *grad; 430da97024aSMatthew G. Knepley const PetscInt *leaves; 431d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 432520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 433e735a8a9SMatthew G. Knepley PetscErrorCode ierr, ierru = 0; 434d7ddef95SMatthew G. Knepley 435d7ddef95SMatthew G. Knepley PetscFunctionBegin; 436da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 437da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 438da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 439d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 440d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 44161f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 442d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 443d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 44420369375SToby Isaac if (cellGeometry) { 445d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 446d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 44720369375SToby Isaac } 448d7ddef95SMatthew G. Knepley if (Grad) { 449c0a6632aSMatthew G. Knepley PetscFV fv; 450c0a6632aSMatthew G. Knepley 451c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 452d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 453d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 454d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 45569291d52SBarry Smith ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 456d7ddef95SMatthew G. Knepley } 457d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 458d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 459d7ddef95SMatthew G. Knepley IS faceIS; 460d7ddef95SMatthew G. Knepley const PetscInt *faces; 461d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 462d7ddef95SMatthew G. Knepley 463d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 464d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 465d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 466d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 467d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 468d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 469640bce14SSatish Balay PetscFVFaceGeom *fg; 470d7ddef95SMatthew G. Knepley 471d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 472da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 473da97024aSMatthew G. Knepley if (loc >= 0) continue; 474d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 475d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 476d7ddef95SMatthew G. Knepley if (Grad) { 477640bce14SSatish Balay PetscFVCellGeom *cg; 478640bce14SSatish Balay PetscScalar *cx, *cgrad; 479d7ddef95SMatthew G. Knepley PetscScalar *xG; 480d7ddef95SMatthew G. Knepley PetscReal dx[3]; 481d7ddef95SMatthew G. Knepley PetscInt d; 482d7ddef95SMatthew G. Knepley 483d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 484d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 485d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 486520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 487d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 488d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 489e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx); 490e735a8a9SMatthew G. Knepley if (ierru) { 491e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 492e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 493e735a8a9SMatthew G. Knepley goto cleanup; 494e735a8a9SMatthew G. Knepley } 495d7ddef95SMatthew G. Knepley } else { 496640bce14SSatish Balay PetscScalar *xI; 497d7ddef95SMatthew G. Knepley PetscScalar *xG; 498d7ddef95SMatthew G. Knepley 499d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 500520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 501e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx); 502e735a8a9SMatthew G. Knepley if (ierru) { 503e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 504e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 505e735a8a9SMatthew G. Knepley goto cleanup; 506e735a8a9SMatthew G. Knepley } 507d7ddef95SMatthew G. Knepley } 508d7ddef95SMatthew G. Knepley } 509d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 510d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 511d7ddef95SMatthew G. Knepley } 512e735a8a9SMatthew G. Knepley cleanup: 513d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 514d7ddef95SMatthew G. Knepley if (Grad) { 51569291d52SBarry Smith ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 516d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 517d7ddef95SMatthew G. Knepley } 518e735a8a9SMatthew G. Knepley if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);} 519d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 520e735a8a9SMatthew G. Knepley CHKERRQ(ierru); 521d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 522d7ddef95SMatthew G. Knepley } 523d7ddef95SMatthew G. Knepley 524f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 525d7ddef95SMatthew G. Knepley { 526d7ddef95SMatthew G. Knepley PetscInt numBd, b; 52755f2e967SMatthew G. Knepley PetscErrorCode ierr; 52855f2e967SMatthew G. Knepley 52955f2e967SMatthew G. Knepley PetscFunctionBegin; 530dff059c6SToby Isaac ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr); 53155f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 532f971fd6bSMatthew G. Knepley DMBoundaryConditionType type; 533d7ddef95SMatthew G. Knepley const char *labelname; 534d7ddef95SMatthew G. Knepley DMLabel label; 5351c531cf8SMatthew G. Knepley PetscInt field, Nc; 5361c531cf8SMatthew G. Knepley const PetscInt *comps; 537d7ddef95SMatthew G. Knepley PetscObject obj; 538d7ddef95SMatthew G. Knepley PetscClassId id; 539a30ec4eaSSatish Balay void (*func)(void); 540d7ddef95SMatthew G. Knepley PetscInt numids; 541d7ddef95SMatthew G. Knepley const PetscInt *ids; 54255f2e967SMatthew G. Knepley void *ctx; 54355f2e967SMatthew G. Knepley 5441c531cf8SMatthew G. Knepley ierr = DMGetBoundary(dm, b, &type, NULL, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 545f971fd6bSMatthew G. Knepley if (insertEssential != (type & DM_BC_ESSENTIAL)) continue; 546c58f1c22SToby Isaac ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 547d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 548d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 549d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 550c60e475cSMatthew G. Knepley switch (type) { 551c60e475cSMatthew G. Knepley /* for FEM, there is no insertion to be done for non-essential boundary conditions */ 552c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL: 553092e5057SToby Isaac ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 5541c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr); 555092e5057SToby Isaac ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 556c60e475cSMatthew G. Knepley break; 557c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL_FIELD: 558c60e475cSMatthew G. Knepley ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 5591c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, 560b278463cSMatthew G. Knepley (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 561c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 56297b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr); 563c60e475cSMatthew G. Knepley ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 564c60e475cSMatthew G. Knepley break; 565c60e475cSMatthew G. Knepley default: break; 566c60e475cSMatthew G. Knepley } 567d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 56843ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 5691c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids, 570b278463cSMatthew G. Knepley (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 571d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 57255f2e967SMatthew G. Knepley } 57355f2e967SMatthew G. Knepley PetscFunctionReturn(0); 57455f2e967SMatthew G. Knepley } 57555f2e967SMatthew G. Knepley 576f1d73a7aSMatthew G. Knepley /*@ 577f1d73a7aSMatthew G. Knepley DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector 578f1d73a7aSMatthew G. Knepley 579f1d73a7aSMatthew G. Knepley Input Parameters: 580f1d73a7aSMatthew G. Knepley + dm - The DM 581f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions 582f1d73a7aSMatthew G. Knepley . time - The time 583f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations 584f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations 585f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations 586f1d73a7aSMatthew G. Knepley 587f1d73a7aSMatthew G. Knepley Output Parameters: 588f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values 589f1d73a7aSMatthew G. Knepley 590f1d73a7aSMatthew G. Knepley Level: developer 591f1d73a7aSMatthew G. Knepley 592f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal() 593f1d73a7aSMatthew G. Knepley @*/ 594f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 595f1d73a7aSMatthew G. Knepley { 596f1d73a7aSMatthew G. Knepley PetscErrorCode ierr; 597f1d73a7aSMatthew G. Knepley 598f1d73a7aSMatthew G. Knepley PetscFunctionBegin; 599f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 600f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 601f1d73a7aSMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 602f1d73a7aSMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 603f1d73a7aSMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 604f1d73a7aSMatthew G. Knepley ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr); 605f1d73a7aSMatthew G. Knepley PetscFunctionReturn(0); 606f1d73a7aSMatthew G. Knepley } 607f1d73a7aSMatthew G. Knepley 6080709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 609cb1e1211SMatthew G Knepley { 610574a98acSMatthew G. Knepley Vec localX; 611574a98acSMatthew G. Knepley PetscErrorCode ierr; 612574a98acSMatthew G. Knepley 613574a98acSMatthew G. Knepley PetscFunctionBegin; 614574a98acSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 6155d42b983SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr); 616574a98acSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 617574a98acSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 618574a98acSMatthew G. Knepley ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr); 619574a98acSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 620574a98acSMatthew G. Knepley PetscFunctionReturn(0); 621574a98acSMatthew G. Knepley } 622574a98acSMatthew G. Knepley 623574a98acSMatthew G. Knepley /*@C 624574a98acSMatthew G. Knepley DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 625574a98acSMatthew G. Knepley 626574a98acSMatthew G. Knepley Input Parameters: 627574a98acSMatthew G. Knepley + dm - The DM 628574a98acSMatthew G. Knepley . time - The time 629574a98acSMatthew G. Knepley . funcs - The functions to evaluate for each field component 630574a98acSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 631574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector 632574a98acSMatthew G. Knepley 633574a98acSMatthew G. Knepley Output Parameter: 634574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2 635574a98acSMatthew G. Knepley 636574a98acSMatthew G. Knepley Level: developer 637574a98acSMatthew G. Knepley 638574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff() 639574a98acSMatthew G. Knepley @*/ 640574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff) 641574a98acSMatthew G. Knepley { 6420f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 643cb1e1211SMatthew G Knepley PetscSection section; 644c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 64515496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 6467318780aSToby Isaac PetscReal *coords, *detJ, *J; 647cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 6487318780aSToby Isaac const PetscReal *quadWeights; 649aed3cbd0SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset; 650cb1e1211SMatthew G Knepley PetscErrorCode ierr; 651cb1e1211SMatthew G Knepley 652cb1e1211SMatthew G Knepley PetscFunctionBegin; 653c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 6547318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 655e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 656cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 657cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 65815496722SMatthew G. Knepley PetscObject obj; 65915496722SMatthew G. Knepley PetscClassId id; 660c5bbbd5bSMatthew G. Knepley PetscInt Nc; 661c5bbbd5bSMatthew G. Knepley 66215496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 66315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 66415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 66515496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 66615496722SMatthew G. Knepley 6670f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 6680f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 66915496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 67015496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 67115496722SMatthew G. Knepley 67215496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 67315496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 67415496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 675c5bbbd5bSMatthew G. Knepley numComponents += Nc; 676cb1e1211SMatthew G Knepley } 6779c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr); 678beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 6797318780aSToby Isaac ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 680aed3cbd0SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 681aed3cbd0SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 6829ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 6839ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 684cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 685a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 686cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 6879c3cf19fSMatthew G. Knepley PetscInt qc = 0; 688cb1e1211SMatthew G Knepley 6897318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 690cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 691cb1e1211SMatthew G Knepley 69215496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 69315496722SMatthew G. Knepley PetscObject obj; 69415496722SMatthew G. Knepley PetscClassId id; 695c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 69615496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 697cb1e1211SMatthew G Knepley 69815496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 69915496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 70015496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 70115496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 70215496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 703cb1e1211SMatthew G Knepley if (debug) { 704cb1e1211SMatthew G Knepley char title[1024]; 705cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 7064c848028SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 707cb1e1211SMatthew G Knepley } 7087318780aSToby Isaac for (q = 0; q < Nq; ++q) { 7097318780aSToby Isaac if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", detJ[q], c, q); 7107318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[coordDim * q], Nc, funcVal, ctx); 711e735a8a9SMatthew G. Knepley if (ierr) { 712e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 713e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 714e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 7157318780aSToby Isaac ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 716e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 717e735a8a9SMatthew G. Knepley } 71815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 71915496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 72015496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 72115496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 722beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 723beaa55a6SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);} 724beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 725cb1e1211SMatthew G Knepley } 726cb1e1211SMatthew G Knepley } 7279c3cf19fSMatthew G. Knepley fieldOffset += Nb; 728beaa55a6SMatthew G. Knepley qc += Nc; 729cb1e1211SMatthew G Knepley } 730cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 731cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 732cb1e1211SMatthew G Knepley localDiff += elemDiff; 733cb1e1211SMatthew G Knepley } 7347318780aSToby Isaac ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 735b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 736cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 737cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 738cb1e1211SMatthew G Knepley } 739cb1e1211SMatthew G Knepley 740b698f381SToby Isaac PetscErrorCode DMComputeL2GradientDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 741cb1e1211SMatthew G Knepley { 7420f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 743cb1e1211SMatthew G Knepley PetscSection section; 74440e14135SMatthew G. Knepley PetscQuadrature quad; 74540e14135SMatthew G. Knepley Vec localX; 7469c3cf19fSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 7479c3cf19fSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7487318780aSToby Isaac PetscReal *coords, *realSpaceDer, *J, *invJ, *detJ; 74940e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 7509c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset; 751cb1e1211SMatthew G Knepley PetscErrorCode ierr; 752cb1e1211SMatthew G Knepley 753cb1e1211SMatthew G Knepley PetscFunctionBegin; 754c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 7557318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 756e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 75740e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 75840e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 75940e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 76040e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 761652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 7620f2d7e86SMatthew G. Knepley PetscFE fe; 76340e14135SMatthew G. Knepley PetscInt Nc; 764652b88e8SMatthew G. Knepley 7650f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7660f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7670f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 76840e14135SMatthew G. Knepley numComponents += Nc; 769652b88e8SMatthew G. Knepley } 7709c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 771beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 7724d6f44ffSToby Isaac /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 7739c3cf19fSMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*Nq,&realSpaceDer,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ,numComponents,&interpolant,Nq,&detJ);CHKERRQ(ierr); 77440e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 7759ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 7769ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 77740e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 77840e14135SMatthew G. Knepley PetscScalar *x = NULL; 77940e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 7809c3cf19fSMatthew G. Knepley PetscInt qc = 0; 781652b88e8SMatthew G. Knepley 7827318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr); 78340e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 78440e14135SMatthew G. Knepley 7859c3cf19fSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 7860f2d7e86SMatthew G. Knepley PetscFE fe; 78751259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 78840e14135SMatthew G. Knepley PetscReal *basisDer; 7899c3cf19fSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 79040e14135SMatthew G. Knepley 7910f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7920f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 7939c3cf19fSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 7940f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 79540e14135SMatthew G. Knepley if (debug) { 79640e14135SMatthew G. Knepley char title[1024]; 79740e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 7989c3cf19fSMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 799652b88e8SMatthew G. Knepley } 8009c3cf19fSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 8017318780aSToby Isaac if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", detJ[q], c, q); 8027318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx); 803e735a8a9SMatthew G. Knepley if (ierr) { 804e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 805e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 806e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 8079c3cf19fSMatthew G. Knepley ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2); 808e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 809e735a8a9SMatthew G. Knepley } 8109c3cf19fSMatthew G. Knepley ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr); 8119c3cf19fSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 812beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 813beaa55a6SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);} 814beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 81540e14135SMatthew G. Knepley } 81640e14135SMatthew G. Knepley } 8179c3cf19fSMatthew G. Knepley fieldOffset += Nb; 8189c3cf19fSMatthew G. Knepley qc += Nc; 81940e14135SMatthew G. Knepley } 82040e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 82140e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 82240e14135SMatthew G. Knepley localDiff += elemDiff; 82340e14135SMatthew G. Knepley } 8249c3cf19fSMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr); 82540e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 826b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 82740e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 828cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 829cb1e1211SMatthew G Knepley } 830cb1e1211SMatthew G Knepley 831c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 83273d901b8SMatthew G. Knepley { 8330f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 83473d901b8SMatthew G. Knepley PetscSection section; 83573d901b8SMatthew G. Knepley PetscQuadrature quad; 83673d901b8SMatthew G. Knepley Vec localX; 83715496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 8387318780aSToby Isaac PetscReal *coords, *detJ, *J; 83973d901b8SMatthew G. Knepley PetscReal *localDiff; 84015496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 8419c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset; 84273d901b8SMatthew G. Knepley PetscErrorCode ierr; 84373d901b8SMatthew G. Knepley 84473d901b8SMatthew G. Knepley PetscFunctionBegin; 845c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 8467318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 847e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 84873d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 84973d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 850bdd6f66aSToby Isaac ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 85173d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 85273d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 85373d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 85415496722SMatthew G. Knepley PetscObject obj; 85515496722SMatthew G. Knepley PetscClassId id; 85673d901b8SMatthew G. Knepley PetscInt Nc; 85773d901b8SMatthew G. Knepley 85815496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 85915496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 86015496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 86115496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 86215496722SMatthew G. Knepley 8630f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8640f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 86515496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 86615496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 86715496722SMatthew G. Knepley 86815496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 86915496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 87015496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 87173d901b8SMatthew G. Knepley numComponents += Nc; 87273d901b8SMatthew G. Knepley } 8739c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 874beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 8757318780aSToby Isaac ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 87673d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 8779ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 8789ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 87973d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 88073d901b8SMatthew G. Knepley PetscScalar *x = NULL; 8819c3cf19fSMatthew G. Knepley PetscInt qc = 0; 88273d901b8SMatthew G. Knepley 8837318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 88473d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 88573d901b8SMatthew G. Knepley 88615496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 88715496722SMatthew G. Knepley PetscObject obj; 88815496722SMatthew G. Knepley PetscClassId id; 88973d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 89015496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 89173d901b8SMatthew G. Knepley 89215496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 89315496722SMatthew G. Knepley 89415496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 89515496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 89615496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 89715496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 89815496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 89973d901b8SMatthew G. Knepley if (debug) { 90073d901b8SMatthew G. Knepley char title[1024]; 90173d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 90215496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 90373d901b8SMatthew G. Knepley } 9047318780aSToby Isaac for (q = 0; q < Nq; ++q) { 9057318780aSToby Isaac if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature point %D", detJ, c, q); 9067318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[coordDim*q], numFields, funcVal, ctx); 907e735a8a9SMatthew G. Knepley if (ierr) { 908e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 909e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 910e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 9117318780aSToby Isaac ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 912e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 913e735a8a9SMatthew G. Knepley } 91415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 91515496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 91615496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 91715496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 918beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 919beaa55a6SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d point %g %g %g diff %g\n", c, field, coordDim > 0 ? coords[0] : 0., coordDim > 1 ? coords[1] : 0., coordDim > 2 ? coords[2] : 0., PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);} 920beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 92173d901b8SMatthew G. Knepley } 92273d901b8SMatthew G. Knepley } 923beaa55a6SMatthew G. Knepley fieldOffset += Nb; 9249c3cf19fSMatthew G. Knepley qc += Nc; 92573d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 92673d901b8SMatthew G. Knepley } 92773d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 92873d901b8SMatthew G. Knepley } 92973d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 930b2566f29SBarry Smith ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 93173d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 9327318780aSToby Isaac ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 93373d901b8SMatthew G. Knepley PetscFunctionReturn(0); 93473d901b8SMatthew G. Knepley } 93573d901b8SMatthew G. Knepley 936e729f68cSMatthew G. Knepley /*@C 937e729f68cSMatthew G. Knepley DMPlexComputeL2DiffVec - This function computes the cellwise L_2 difference between a function u and an FEM interpolant solution u_h, and stores it in a Vec. 938e729f68cSMatthew G. Knepley 939e729f68cSMatthew G. Knepley Input Parameters: 940e729f68cSMatthew G. Knepley + dm - The DM 9410163fd50SMatthew G. Knepley . time - The time 942ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation 943e729f68cSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 944e729f68cSMatthew G. Knepley - X - The coefficient vector u_h 945e729f68cSMatthew G. Knepley 946e729f68cSMatthew G. Knepley Output Parameter: 947e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell 948e729f68cSMatthew G. Knepley 949e729f68cSMatthew G. Knepley Level: developer 950e729f68cSMatthew G. Knepley 951b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 952e729f68cSMatthew G. Knepley @*/ 9530163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D) 954e729f68cSMatthew G. Knepley { 955e729f68cSMatthew G. Knepley PetscSection section; 956e729f68cSMatthew G. Knepley PetscQuadrature quad; 957e729f68cSMatthew G. Knepley Vec localX; 958e729f68cSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 9597318780aSToby Isaac PetscReal *coords, *detJ, *J; 960e729f68cSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 9619c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset; 962e729f68cSMatthew G. Knepley PetscErrorCode ierr; 963e729f68cSMatthew G. Knepley 964e729f68cSMatthew G. Knepley PetscFunctionBegin; 965e729f68cSMatthew G. Knepley ierr = VecSet(D, 0.0);CHKERRQ(ierr); 966e729f68cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9677318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 968e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 969e729f68cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 970e729f68cSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 971bdd6f66aSToby Isaac ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 972e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 973e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 974e729f68cSMatthew G. Knepley for (field = 0; field < numFields; ++field) { 975e729f68cSMatthew G. Knepley PetscObject obj; 976e729f68cSMatthew G. Knepley PetscClassId id; 977e729f68cSMatthew G. Knepley PetscInt Nc; 978e729f68cSMatthew G. Knepley 979e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 980e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 981e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 982e729f68cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 983e729f68cSMatthew G. Knepley 984e729f68cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 985e729f68cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 986e729f68cSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 987e729f68cSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 988e729f68cSMatthew G. Knepley 989e729f68cSMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 990e729f68cSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 991e729f68cSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 992e729f68cSMatthew G. Knepley numComponents += Nc; 993e729f68cSMatthew G. Knepley } 9949c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 995beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 9967318780aSToby Isaac ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 997e729f68cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 998e729f68cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 999e729f68cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1000e729f68cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1001e729f68cSMatthew G. Knepley PetscScalar *x = NULL; 10026f288a59SMatthew G. Knepley PetscScalar elemDiff = 0.0; 10039c3cf19fSMatthew G. Knepley PetscInt qc = 0; 1004e729f68cSMatthew G. Knepley 10057318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 1006e729f68cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1007e729f68cSMatthew G. Knepley 1008e729f68cSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 1009e729f68cSMatthew G. Knepley PetscObject obj; 1010e729f68cSMatthew G. Knepley PetscClassId id; 1011e729f68cSMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 1012e729f68cSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 1013e729f68cSMatthew G. Knepley 1014e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1015e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1016e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 1017e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1018e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 101923f34ed2SToby Isaac if (funcs[field]) { 10207318780aSToby Isaac for (q = 0; q < Nq; ++q) { 10217f79407eSBarry Smith if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], c, q); 1022274e8aaeSMatthew G. Knepley ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx); 1023e735a8a9SMatthew G. Knepley if (ierr) { 1024e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 1025e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 10267f79407eSBarry Smith ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 1027e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 1028e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 1029e735a8a9SMatthew G. Knepley } 1030e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1031e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1032e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1033e729f68cSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1034beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 1035beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 1036e729f68cSMatthew G. Knepley } 1037e729f68cSMatthew G. Knepley } 103823f34ed2SToby Isaac } 1039beaa55a6SMatthew G. Knepley fieldOffset += Nb; 10409c3cf19fSMatthew G. Knepley qc += Nc; 1041e729f68cSMatthew G. Knepley } 1042e729f68cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 104323f34ed2SToby Isaac ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr); 1044e729f68cSMatthew G. Knepley } 10457318780aSToby Isaac ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 1046e729f68cSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1047e729f68cSMatthew G. Knepley ierr = VecSqrtAbs(D);CHKERRQ(ierr); 1048e729f68cSMatthew G. Knepley PetscFunctionReturn(0); 1049e729f68cSMatthew G. Knepley } 1050e729f68cSMatthew G. Knepley 10511555c271SMatthew G. Knepley /*@C 10521555c271SMatthew G. Knepley DMPlexComputeGradientClementInterpolant - This function computes the L2 projection of the cellwise gradient of a function u onto P1, and stores it in a Vec. 10531555c271SMatthew G. Knepley 10541555c271SMatthew G. Knepley Input Parameters: 10551555c271SMatthew G. Knepley + dm - The DM 10561555c271SMatthew G. Knepley - LocX - The coefficient vector u_h 10571555c271SMatthew G. Knepley 10581555c271SMatthew G. Knepley Output Parameter: 10591555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient 10601555c271SMatthew G. Knepley 106195452b02SPatrick Sanan Notes: 106295452b02SPatrick Sanan Add citation to (Clement, 1975) and definition of the interpolant 10631555c271SMatthew G. Knepley \nabla u_h(v_i) = \sum_{T_i \in support(v_i)} |T_i| \nabla u_h(T_i) / \sum_{T_i \in support(v_i)} |T_i| where |T_i| is the cell volume 10641555c271SMatthew G. Knepley 10651555c271SMatthew G. Knepley Level: developer 10661555c271SMatthew G. Knepley 10671555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 10681555c271SMatthew G. Knepley @*/ 10691555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC) 10701555c271SMatthew G. Knepley { 1071db1066baSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1072db1066baSMatthew G. Knepley PetscInt debug = mesh->printFEM; 10731555c271SMatthew G. Knepley DM dmC; 10741555c271SMatthew G. Knepley PetscSection section; 10751555c271SMatthew G. Knepley PetscQuadrature quad; 10761555c271SMatthew G. Knepley PetscScalar *interpolant, *gradsum; 10771555c271SMatthew G. Knepley PetscReal *coords, *detJ, *J, *invJ; 10781555c271SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 10791555c271SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset; 10801555c271SMatthew G. Knepley PetscErrorCode ierr; 10811555c271SMatthew G. Knepley 10821555c271SMatthew G. Knepley PetscFunctionBegin; 10831555c271SMatthew G. Knepley ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr); 10841555c271SMatthew G. Knepley ierr = VecSet(locC, 0.0);CHKERRQ(ierr); 10851555c271SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 10861555c271SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 1087e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 10881555c271SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 10891555c271SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 10901555c271SMatthew G. Knepley PetscObject obj; 10911555c271SMatthew G. Knepley PetscClassId id; 10921555c271SMatthew G. Knepley PetscInt Nc; 10931555c271SMatthew G. Knepley 10941555c271SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 10951555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 10961555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 10971555c271SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 10981555c271SMatthew G. Knepley 10991555c271SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 11001555c271SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 11011555c271SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 11021555c271SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 11031555c271SMatthew G. Knepley 11041555c271SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 11051555c271SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 11061555c271SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 11071555c271SMatthew G. Knepley numComponents += Nc; 11081555c271SMatthew G. Knepley } 11091555c271SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 11101555c271SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 11111555c271SMatthew G. Knepley ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ);CHKERRQ(ierr); 11121555c271SMatthew G. Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 11131555c271SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 11141555c271SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 11151555c271SMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 11161555c271SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 11171555c271SMatthew G. Knepley PetscScalar volsum = 0.0; 11181555c271SMatthew G. Knepley PetscInt *star = NULL; 11191555c271SMatthew G. Knepley PetscInt starSize, st, d, fc; 11201555c271SMatthew G. Knepley 11211555c271SMatthew G. Knepley ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr); 11221555c271SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 11231555c271SMatthew G. Knepley for (st = 0; st < starSize*2; st += 2) { 11241555c271SMatthew G. Knepley const PetscInt cell = star[st]; 11251555c271SMatthew G. Knepley PetscScalar *grad = &gradsum[coordDim*numComponents]; 11261555c271SMatthew G. Knepley PetscScalar *x = NULL; 11271555c271SMatthew G. Knepley PetscReal vol = 0.0; 11281555c271SMatthew G. Knepley 11291555c271SMatthew G. Knepley if ((cell < cStart) || (cell >= cEnd)) continue; 11301555c271SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr); 11311555c271SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 11321555c271SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 11331555c271SMatthew G. Knepley PetscObject obj; 11341555c271SMatthew G. Knepley PetscClassId id; 11351555c271SMatthew G. Knepley PetscInt Nb, Nc, q, qc = 0; 11361555c271SMatthew G. Knepley 11371555c271SMatthew G. Knepley ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr); 11381555c271SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 11391555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 11401555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 11411555c271SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 11421555c271SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 11431555c271SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 11441555c271SMatthew G. Knepley if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], cell, q); 11451555c271SMatthew G. Knepley if (ierr) { 11461555c271SMatthew G. Knepley PetscErrorCode ierr2; 11471555c271SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2); 11481555c271SMatthew G. Knepley ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2); 11491555c271SMatthew G. Knepley ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2); 11501555c271SMatthew G. Knepley CHKERRQ(ierr); 11511555c271SMatthew G. Knepley } 11521555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);} 11531555c271SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 11541555c271SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 11551555c271SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+qc+fc]; 11561555c271SMatthew G. Knepley 11571555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q]; 11581555c271SMatthew G. Knepley } 11591555c271SMatthew G. Knepley vol += quadWeights[q*qNc]*detJ[q]; 11601555c271SMatthew G. Knepley } 11611555c271SMatthew G. Knepley fieldOffset += Nb; 11621555c271SMatthew G. Knepley qc += Nc; 11631555c271SMatthew G. Knepley } 11641555c271SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 1165f8527842SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 1166f8527842SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 1167f8527842SMatthew G. Knepley gradsum[fc*coordDim+d] += grad[fc*coordDim+d]; 1168f8527842SMatthew G. Knepley } 1169f8527842SMatthew G. Knepley } 1170f8527842SMatthew G. Knepley volsum += vol; 1171db1066baSMatthew G. Knepley if (debug) { 11726d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr); 11731555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 11741555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 11751555c271SMatthew G. Knepley if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);} 11766d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr); 11771555c271SMatthew G. Knepley } 11781555c271SMatthew G. Knepley } 11791555c271SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr); 1180db1066baSMatthew G. Knepley } 11811555c271SMatthew G. Knepley } 11821555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 11831555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum; 11841555c271SMatthew G. Knepley } 11851555c271SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 11861555c271SMatthew G. Knepley ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr); 11871555c271SMatthew G. Knepley } 11881555c271SMatthew G. Knepley ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr); 11891555c271SMatthew G. Knepley PetscFunctionReturn(0); 11901555c271SMatthew G. Knepley } 11911555c271SMatthew G. Knepley 1192338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user) 119373d901b8SMatthew G. Knepley { 1194338f77d5SMatthew G. Knepley DM dmAux = NULL; 119561aaff12SToby Isaac PetscDS prob, probAux = NULL; 119673d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1197338f77d5SMatthew G. Knepley Vec locX, locA; 1198c330f8ffSToby Isaac PetscInt dim, numCells = cEnd - cStart, c, f; 1199c330f8ffSToby Isaac PetscBool useFVM = PETSC_FALSE; 1200338f77d5SMatthew G. Knepley /* DS */ 1201338f77d5SMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x, numConstants; 1202338f77d5SMatthew G. Knepley PetscInt NfAux, totDimAux, *aOff; 1203338f77d5SMatthew G. Knepley PetscScalar *u, *a; 1204338f77d5SMatthew G. Knepley const PetscScalar *constants; 1205338f77d5SMatthew G. Knepley /* Geometry */ 1206c330f8ffSToby Isaac PetscFEGeom *cgeomFEM; 1207338f77d5SMatthew G. Knepley DM dmGrad; 1208c330f8ffSToby Isaac PetscQuadrature affineQuad = NULL; 1209338f77d5SMatthew G. Knepley Vec cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL; 1210b5a3613cSMatthew G. Knepley PetscFVCellGeom *cgeomFVM; 1211338f77d5SMatthew G. Knepley const PetscScalar *lgrad; 1212c330f8ffSToby Isaac PetscBool isAffine; 1213c330f8ffSToby Isaac DMField coordField; 1214c330f8ffSToby Isaac IS cellIS; 121573d901b8SMatthew G. Knepley PetscErrorCode ierr; 121673d901b8SMatthew G. Knepley 121773d901b8SMatthew G. Knepley PetscFunctionBegin; 1218338f77d5SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1219c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1220e87a4003SBarry Smith ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 122173d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1222338f77d5SMatthew G. Knepley /* Determine which discretizations we have */ 1223b5a3613cSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1224b5a3613cSMatthew G. Knepley PetscObject obj; 1225b5a3613cSMatthew G. Knepley PetscClassId id; 1226b5a3613cSMatthew G. Knepley 1227b5a3613cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1228b5a3613cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1229338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE; 1230338f77d5SMatthew G. Knepley } 1231338f77d5SMatthew G. Knepley /* Get local solution with boundary values */ 1232338f77d5SMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 1233338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1234338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1235338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1236338f77d5SMatthew G. Knepley /* Read DS information */ 1237338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1238338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 1239338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 1240c330f8ffSToby Isaac ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr); 1241338f77d5SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 1242338f77d5SMatthew G. Knepley /* Read Auxiliary DS information */ 1243338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1244338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1245338f77d5SMatthew G. Knepley if (dmAux) { 1246338f77d5SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1247338f77d5SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 1248e87a4003SBarry Smith ierr = DMGetSection(dmAux, §ionAux);CHKERRQ(ierr); 1249338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1250338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 1251338f77d5SMatthew G. Knepley } 1252338f77d5SMatthew G. Knepley /* Allocate data arrays */ 1253338f77d5SMatthew G. Knepley ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr); 1254338f77d5SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1255338f77d5SMatthew G. Knepley /* Read out geometry */ 1256c330f8ffSToby Isaac ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr); 1257c330f8ffSToby Isaac ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 1258c330f8ffSToby Isaac if (isAffine) { 1259c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1260c330f8ffSToby Isaac if (affineQuad) { 1261c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 1262338f77d5SMatthew G. Knepley } 1263b5a3613cSMatthew G. Knepley } 1264b5a3613cSMatthew G. Knepley if (useFVM) { 1265338f77d5SMatthew G. Knepley PetscFV fv = NULL; 1266b5a3613cSMatthew G. Knepley Vec grad; 1267b5a3613cSMatthew G. Knepley PetscInt fStart, fEnd; 1268b5a3613cSMatthew G. Knepley PetscBool compGrad; 1269b5a3613cSMatthew G. Knepley 1270338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1271338f77d5SMatthew G. Knepley PetscObject obj; 1272338f77d5SMatthew G. Knepley PetscClassId id; 1273338f77d5SMatthew G. Knepley 1274338f77d5SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1275338f77d5SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1276338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;} 1277338f77d5SMatthew G. Knepley } 1278338f77d5SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr); 1279338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr); 1280b5a3613cSMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr); 1281338f77d5SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr); 1282338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr); 1283b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1284b5a3613cSMatthew G. Knepley /* Reconstruct and limit cell gradients */ 1285b5a3613cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1286b5a3613cSMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1287338f77d5SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1288b5a3613cSMatthew G. Knepley /* Communicate gradient values */ 1289b5a3613cSMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1290b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1291b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1292b5a3613cSMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1293b5a3613cSMatthew G. Knepley /* Handle non-essential (e.g. outflow) boundary values */ 1294338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 1295b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1296b5a3613cSMatthew G. Knepley } 1297338f77d5SMatthew G. Knepley /* Read out data from inputs */ 129873d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 129973d901b8SMatthew G. Knepley PetscScalar *x = NULL; 130073d901b8SMatthew G. Knepley PetscInt i; 130173d901b8SMatthew G. Knepley 1302338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 13030f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 1304338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 130573d901b8SMatthew G. Knepley if (dmAux) { 1306338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 13070f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 1308338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 130973d901b8SMatthew G. Knepley } 131073d901b8SMatthew G. Knepley } 1311338f77d5SMatthew G. Knepley /* Do integration for each field */ 131273d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1313c1f031eeSMatthew G. Knepley PetscObject obj; 1314c1f031eeSMatthew G. Knepley PetscClassId id; 1315c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 131673d901b8SMatthew G. Knepley 1317c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1318c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1319c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1320c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1321c1f031eeSMatthew G. Knepley PetscQuadrature q; 1322c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 1323c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1324c1f031eeSMatthew G. Knepley 13250f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1326c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 13279c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1328c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1329c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 133073d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 13310f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 133273d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 133373d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 133473d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 133573d901b8SMatthew G. Knepley offset = numCells - Nr; 1336c330f8ffSToby Isaac if (!affineQuad) { 1337c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 1338c330f8ffSToby Isaac } 1339c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 1340c330f8ffSToby Isaac ierr = PetscFEIntegrate(fe, prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr); 1341c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1342c330f8ffSToby Isaac ierr = PetscFEIntegrate(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr); 1343c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1344c330f8ffSToby Isaac if (!affineQuad) { 1345c330f8ffSToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 1346c330f8ffSToby Isaac } 1347c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1348c1f031eeSMatthew G. Knepley PetscInt foff; 1349420e96edSMatthew G. Knepley PetscPointFunc obj_func; 1350b69edc29SMatthew G. Knepley PetscScalar lint; 1351c1f031eeSMatthew G. Knepley 1352c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 1353c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1354c1f031eeSMatthew G. Knepley if (obj_func) { 1355c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 1356b5a3613cSMatthew G. Knepley PetscScalar *u_x; 1357b5a3613cSMatthew G. Knepley 1358b5a3613cSMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr); 1359338f77d5SMatthew G. Knepley obj_func(dim, Nf, NfAux, uOff, uOff_x, &u[totDim*c+foff], NULL, u_x, aOff, NULL, &a[totDimAux*c], NULL, NULL, 0.0, cgeomFVM[c].centroid, numConstants, constants, &lint); 1360338f77d5SMatthew G. Knepley cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume; 136173d901b8SMatthew G. Knepley } 1362c1f031eeSMatthew G. Knepley } 1363c1f031eeSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1364c1f031eeSMatthew G. Knepley } 1365338f77d5SMatthew G. Knepley /* Cleanup data arrays */ 1366b5a3613cSMatthew G. Knepley if (useFVM) { 1367b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1368b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1369b5a3613cSMatthew G. Knepley ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1370b5a3613cSMatthew G. Knepley ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr); 1371b5a3613cSMatthew G. Knepley ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr); 1372b5a3613cSMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 1373b5a3613cSMatthew G. Knepley } 137473d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 1375338f77d5SMatthew G. Knepley ierr = PetscFree(u);CHKERRQ(ierr); 1376338f77d5SMatthew G. Knepley /* Cleanup */ 1377f99c8401SToby Isaac if (affineQuad) { 1378f99c8401SToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 1379f99c8401SToby Isaac } 1380f99c8401SToby Isaac ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 1381c330f8ffSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 1382338f77d5SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 1383338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 1384338f77d5SMatthew G. Knepley } 1385338f77d5SMatthew G. Knepley 1386338f77d5SMatthew G. Knepley /*@ 1387338f77d5SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user 1388338f77d5SMatthew G. Knepley 1389338f77d5SMatthew G. Knepley Input Parameters: 1390338f77d5SMatthew G. Knepley + dm - The mesh 1391338f77d5SMatthew G. Knepley . X - Global input vector 1392338f77d5SMatthew G. Knepley - user - The user context 1393338f77d5SMatthew G. Knepley 1394338f77d5SMatthew G. Knepley Output Parameter: 1395338f77d5SMatthew G. Knepley . integral - Integral for each field 1396338f77d5SMatthew G. Knepley 1397338f77d5SMatthew G. Knepley Level: developer 1398338f77d5SMatthew G. Knepley 1399338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 1400338f77d5SMatthew G. Knepley @*/ 1401b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user) 1402338f77d5SMatthew G. Knepley { 1403338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1404b8feb594SMatthew G. Knepley PetscScalar *cintegral, *lintegral; 1405338f77d5SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell; 1406338f77d5SMatthew G. Knepley PetscErrorCode ierr; 1407338f77d5SMatthew G. Knepley 1408338f77d5SMatthew G. Knepley PetscFunctionBegin; 1409338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1410338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 1411338f77d5SMatthew G. Knepley PetscValidPointer(integral, 3); 1412338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1413338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 1414338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 1415338f77d5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 1416338f77d5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr); 1417338f77d5SMatthew G. Knepley cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight]; 1418338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 1419338f77d5SMatthew G. Knepley ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 1420338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 1421338f77d5SMatthew G. Knepley /* Sum up values */ 1422338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 1423338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 1424338f77d5SMatthew G. Knepley 1425338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 1426b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f]; 1427338f77d5SMatthew G. Knepley } 1428b8feb594SMatthew G. Knepley ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 142973d901b8SMatthew G. Knepley if (mesh->printFEM) { 1430338f77d5SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr); 1431b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);} 143273d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 143373d901b8SMatthew G. Knepley } 1434338f77d5SMatthew G. Knepley ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr); 1435338f77d5SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1436338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 1437338f77d5SMatthew G. Knepley } 1438338f77d5SMatthew G. Knepley 1439338f77d5SMatthew G. Knepley /*@ 1440338f77d5SMatthew G. Knepley DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user 1441338f77d5SMatthew G. Knepley 1442338f77d5SMatthew G. Knepley Input Parameters: 1443338f77d5SMatthew G. Knepley + dm - The mesh 1444338f77d5SMatthew G. Knepley . X - Global input vector 1445338f77d5SMatthew G. Knepley - user - The user context 1446338f77d5SMatthew G. Knepley 1447338f77d5SMatthew G. Knepley Output Parameter: 1448338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field 1449338f77d5SMatthew G. Knepley 1450338f77d5SMatthew G. Knepley Level: developer 1451338f77d5SMatthew G. Knepley 1452338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 1453338f77d5SMatthew G. Knepley @*/ 1454338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user) 1455338f77d5SMatthew G. Knepley { 1456338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1457338f77d5SMatthew G. Knepley DM dmF; 1458338f77d5SMatthew G. Knepley PetscSection sectionF; 1459338f77d5SMatthew G. Knepley PetscScalar *cintegral, *af; 1460338f77d5SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell; 1461338f77d5SMatthew G. Knepley PetscErrorCode ierr; 1462338f77d5SMatthew G. Knepley 1463338f77d5SMatthew G. Knepley PetscFunctionBegin; 1464338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1465338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 1466338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 1467338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1468338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 1469338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 1470338f77d5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 1471338f77d5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr); 1472338f77d5SMatthew G. Knepley cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight]; 1473338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 1474338f77d5SMatthew G. Knepley ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 1475338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 1476338f77d5SMatthew G. Knepley /* Put values in F*/ 1477338f77d5SMatthew G. Knepley ierr = VecGetDM(F, &dmF);CHKERRQ(ierr); 1478e87a4003SBarry Smith ierr = DMGetSection(dmF, §ionF);CHKERRQ(ierr); 1479338f77d5SMatthew G. Knepley ierr = VecGetArray(F, &af);CHKERRQ(ierr); 1480338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 1481338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 1482338f77d5SMatthew G. Knepley PetscInt dof, off; 1483338f77d5SMatthew G. Knepley 1484338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 1485338f77d5SMatthew G. Knepley ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr); 1486338f77d5SMatthew G. Knepley ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr); 1487338f77d5SMatthew G. Knepley if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf); 1488338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f]; 1489338f77d5SMatthew G. Knepley } 1490338f77d5SMatthew G. Knepley ierr = VecRestoreArray(F, &af);CHKERRQ(ierr); 1491338f77d5SMatthew G. Knepley ierr = PetscFree(cintegral);CHKERRQ(ierr); 1492c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 149373d901b8SMatthew G. Knepley PetscFunctionReturn(0); 149473d901b8SMatthew G. Knepley } 149573d901b8SMatthew G. Knepley 1496*9b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS, 1497*9b6f715bSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 149864c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 149964c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 150064c72086SMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), 150164c72086SMatthew G. Knepley PetscScalar *fintegral, void *user) 150264c72086SMatthew G. Knepley { 1503*9b6f715bSMatthew G. Knepley DM plex = NULL, plexA = NULL; 1504*9b6f715bSMatthew G. Knepley PetscDS prob, probAux = NULL; 1505*9b6f715bSMatthew G. Knepley PetscSection section, sectionAux = NULL; 1506*9b6f715bSMatthew G. Knepley Vec locA = NULL; 1507*9b6f715bSMatthew G. Knepley DMField coordField; 1508*9b6f715bSMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x; 1509*9b6f715bSMatthew G. Knepley PetscInt NfAux = 0, totDimAux = 0, *aOff = NULL; 1510*9b6f715bSMatthew G. Knepley PetscScalar *u, *a = NULL; 151164c72086SMatthew G. Knepley const PetscScalar *constants; 1512*9b6f715bSMatthew G. Knepley PetscInt numConstants, f; 151364c72086SMatthew G. Knepley PetscErrorCode ierr; 151464c72086SMatthew G. Knepley 151564c72086SMatthew G. Knepley PetscFunctionBegin; 1516*9b6f715bSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 1517*9b6f715bSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 151864c72086SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 151964c72086SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 152064c72086SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 152164c72086SMatthew G. Knepley /* Determine which discretizations we have */ 1522*9b6f715bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 152364c72086SMatthew G. Knepley PetscObject obj; 152464c72086SMatthew G. Knepley PetscClassId id; 152564c72086SMatthew G. Knepley 1526*9b6f715bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 152764c72086SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1528*9b6f715bSMatthew G. Knepley if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f); 152964c72086SMatthew G. Knepley } 153064c72086SMatthew G. Knepley /* Read DS information */ 153164c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 153264c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 153364c72086SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 153464c72086SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 153564c72086SMatthew G. Knepley /* Read Auxiliary DS information */ 153664c72086SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1537*9b6f715bSMatthew G. Knepley if (locA) { 1538*9b6f715bSMatthew G. Knepley DM dmAux; 1539*9b6f715bSMatthew G. Knepley 1540*9b6f715bSMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 1541*9b6f715bSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 154264c72086SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 154364c72086SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 154464c72086SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 154564c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 154664c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 154764c72086SMatthew G. Knepley } 1548*9b6f715bSMatthew G. Knepley /* Integrate over points */ 1549*9b6f715bSMatthew G. Knepley { 1550*9b6f715bSMatthew G. Knepley PetscFEGeom *fgeom, *chunkGeom = NULL; 1551*9b6f715bSMatthew G. Knepley PetscBool isAffine; 1552*9b6f715bSMatthew G. Knepley PetscQuadrature qGeom = NULL; 1553*9b6f715bSMatthew G. Knepley const PetscInt *points; 1554*9b6f715bSMatthew G. Knepley PetscInt numFaces, face, Nq, field; 1555*9b6f715bSMatthew G. Knepley PetscInt numChunks, chunkSize, chunk, Nr, offset; 155664c72086SMatthew G. Knepley 1557*9b6f715bSMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 1558*9b6f715bSMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 1559*9b6f715bSMatthew G. Knepley ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 1560*9b6f715bSMatthew G. Knepley ierr = DMFieldGetFEInvariance(coordField, pointIS, NULL, &isAffine, NULL);CHKERRQ(ierr); 156164c72086SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 156264c72086SMatthew G. Knepley PetscFE fe; 156364c72086SMatthew G. Knepley 156464c72086SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1565*9b6f715bSMatthew G. Knepley if (isAffine) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);} 1566*9b6f715bSMatthew G. Knepley if (!qGeom) { 1567*9b6f715bSMatthew G. Knepley ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 1568*9b6f715bSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr); 1569*9b6f715bSMatthew G. Knepley } 1570*9b6f715bSMatthew G. Knepley ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1571*9b6f715bSMatthew G. Knepley ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr); 1572*9b6f715bSMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 1573*9b6f715bSMatthew G. Knepley const PetscInt point = points[face], *support, *cone; 1574*9b6f715bSMatthew G. Knepley PetscScalar *x = NULL; 1575*9b6f715bSMatthew G. Knepley PetscInt i, coneSize, faceLoc; 1576*9b6f715bSMatthew G. Knepley 1577*9b6f715bSMatthew G. Knepley ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 1578*9b6f715bSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 1579*9b6f715bSMatthew G. Knepley ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 1580*9b6f715bSMatthew G. Knepley for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 1581*9b6f715bSMatthew G. Knepley if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", face, support[0]); 1582*9b6f715bSMatthew G. Knepley fgeom->face[face][0] = faceLoc; 1583*9b6f715bSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1584*9b6f715bSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 1585*9b6f715bSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1586*9b6f715bSMatthew G. Knepley if (locA) { 1587*9b6f715bSMatthew G. Knepley PetscInt subp; 1588*9b6f715bSMatthew G. Knepley ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr); 1589*9b6f715bSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 1590*9b6f715bSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i]; 1591*9b6f715bSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 1592*9b6f715bSMatthew G. Knepley } 1593*9b6f715bSMatthew G. Knepley } 1594*9b6f715bSMatthew G. Knepley /* Get blocking */ 1595*9b6f715bSMatthew G. Knepley { 1596*9b6f715bSMatthew G. Knepley PetscQuadrature q; 1597*9b6f715bSMatthew G. Knepley PetscInt numBatches, batchSize, numBlocks, blockSize; 1598*9b6f715bSMatthew G. Knepley PetscInt Nq, Nb; 1599*9b6f715bSMatthew G. Knepley 160064c72086SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 160164c72086SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 160264c72086SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 160364c72086SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 160464c72086SMatthew G. Knepley blockSize = Nb*Nq; 160564c72086SMatthew G. Knepley batchSize = numBlocks * blockSize; 1606*9b6f715bSMatthew G. Knepley chunkSize = numBatches*batchSize; 160764c72086SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1608*9b6f715bSMatthew G. Knepley numChunks = numFaces / chunkSize; 1609*9b6f715bSMatthew G. Knepley Nr = numFaces % chunkSize; 161064c72086SMatthew G. Knepley offset = numFaces - Nr; 161164c72086SMatthew G. Knepley } 1612*9b6f715bSMatthew G. Knepley /* Do integration for each field */ 1613*9b6f715bSMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 1614*9b6f715bSMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr); 1615*9b6f715bSMatthew G. Knepley ierr = PetscFEIntegrateBd(fe, prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr); 1616*9b6f715bSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr); 161764c72086SMatthew G. Knepley } 1618*9b6f715bSMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr); 1619*9b6f715bSMatthew G. Knepley ierr = PetscFEIntegrateBd(fe, prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr); 1620*9b6f715bSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr); 162164c72086SMatthew G. Knepley /* Cleanup data arrays */ 1622*9b6f715bSMatthew G. Knepley ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr); 1623*9b6f715bSMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 1624*9b6f715bSMatthew G. Knepley ierr = PetscFree2(u, a);CHKERRQ(ierr); 1625*9b6f715bSMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 162664c72086SMatthew G. Knepley } 162764c72086SMatthew G. Knepley } 1628*9b6f715bSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 1629*9b6f715bSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 1630*9b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 1631*9b6f715bSMatthew G. Knepley } 1632*9b6f715bSMatthew G. Knepley 1633*9b6f715bSMatthew G. Knepley /*@ 1634*9b6f715bSMatthew G. Knepley DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user 1635*9b6f715bSMatthew G. Knepley 1636*9b6f715bSMatthew G. Knepley Input Parameters: 1637*9b6f715bSMatthew G. Knepley + dm - The mesh 1638*9b6f715bSMatthew G. Knepley . X - Global input vector 1639*9b6f715bSMatthew G. Knepley . label - The boundary DMLabel 1640*9b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values 1641*9b6f715bSMatthew G. Knepley . vals - The label values to use, or PETSC_NULL for all values 1642*9b6f715bSMatthew G. Knepley . func = The function to integrate along the boundary 1643*9b6f715bSMatthew G. Knepley - user - The user context 1644*9b6f715bSMatthew G. Knepley 1645*9b6f715bSMatthew G. Knepley Output Parameter: 1646*9b6f715bSMatthew G. Knepley . integral - Integral for each field 1647*9b6f715bSMatthew G. Knepley 1648*9b6f715bSMatthew G. Knepley Level: developer 1649*9b6f715bSMatthew G. Knepley 1650*9b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM() 1651*9b6f715bSMatthew G. Knepley @*/ 1652*9b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[], 1653*9b6f715bSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 1654*9b6f715bSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1655*9b6f715bSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1656*9b6f715bSMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), 1657*9b6f715bSMatthew G. Knepley PetscScalar *integral, void *user) 1658*9b6f715bSMatthew G. Knepley { 1659*9b6f715bSMatthew G. Knepley Vec locX; 1660*9b6f715bSMatthew G. Knepley PetscSection section; 1661*9b6f715bSMatthew G. Knepley DMLabel depthLabel; 1662*9b6f715bSMatthew G. Knepley IS facetIS; 1663*9b6f715bSMatthew G. Knepley PetscInt dim, Nf, f, v; 1664*9b6f715bSMatthew G. Knepley PetscErrorCode ierr; 1665*9b6f715bSMatthew G. Knepley 1666*9b6f715bSMatthew G. Knepley PetscFunctionBegin; 1667*9b6f715bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1668*9b6f715bSMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 1669*9b6f715bSMatthew G. Knepley PetscValidPointer(label, 3); 1670*9b6f715bSMatthew G. Knepley if (vals) PetscValidPointer(vals, 5); 1671*9b6f715bSMatthew G. Knepley PetscValidPointer(integral, 6); 1672*9b6f715bSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1673*9b6f715bSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1674*9b6f715bSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1675*9b6f715bSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 1676*9b6f715bSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1677*9b6f715bSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1678*9b6f715bSMatthew G. Knepley /* Get local solution with boundary values */ 1679*9b6f715bSMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 1680*9b6f715bSMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1681*9b6f715bSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1682*9b6f715bSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1683*9b6f715bSMatthew G. Knepley /* Loop over label values */ 1684*9b6f715bSMatthew G. Knepley ierr = PetscMemzero(integral, Nf * sizeof(PetscScalar));CHKERRQ(ierr); 1685*9b6f715bSMatthew G. Knepley for (v = 0; v < numVals; ++v) { 1686*9b6f715bSMatthew G. Knepley IS pointIS; 1687*9b6f715bSMatthew G. Knepley PetscInt numFaces, face; 1688*9b6f715bSMatthew G. Knepley PetscScalar *fintegral; 1689*9b6f715bSMatthew G. Knepley 1690*9b6f715bSMatthew G. Knepley ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr); 1691*9b6f715bSMatthew G. Knepley if (!pointIS) continue; /* No points with that id on this process */ 1692*9b6f715bSMatthew G. Knepley { 1693*9b6f715bSMatthew G. Knepley IS isectIS; 1694*9b6f715bSMatthew G. Knepley 1695*9b6f715bSMatthew G. Knepley /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 1696*9b6f715bSMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr); 1697*9b6f715bSMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1698*9b6f715bSMatthew G. Knepley pointIS = isectIS; 1699*9b6f715bSMatthew G. Knepley } 1700*9b6f715bSMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 1701*9b6f715bSMatthew G. Knepley ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr); 1702*9b6f715bSMatthew G. Knepley ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr); 1703*9b6f715bSMatthew G. Knepley /* Sum point contributions into integral */ 1704*9b6f715bSMatthew G. Knepley for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f]; 1705*9b6f715bSMatthew G. Knepley ierr = PetscFree(fintegral);CHKERRQ(ierr); 1706*9b6f715bSMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1707*9b6f715bSMatthew G. Knepley } 170864c72086SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 1709*9b6f715bSMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 1710*9b6f715bSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 171164c72086SMatthew G. Knepley PetscFunctionReturn(0); 171264c72086SMatthew G. Knepley } 171364c72086SMatthew G. Knepley 1714d69c5d34SMatthew G. Knepley /*@ 171568132eb9SMatthew G. Knepley DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1716d69c5d34SMatthew G. Knepley 1717d69c5d34SMatthew G. Knepley Input Parameters: 1718d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1719d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1720d69c5d34SMatthew G. Knepley - user - The user context 1721d69c5d34SMatthew G. Knepley 1722d69c5d34SMatthew G. Knepley Output Parameter: 1723934789fcSMatthew G. Knepley . In - The interpolation matrix 1724d69c5d34SMatthew G. Knepley 1725d69c5d34SMatthew G. Knepley Level: developer 1726d69c5d34SMatthew G. Knepley 172768132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 1728d69c5d34SMatthew G. Knepley @*/ 172968132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user) 1730d69c5d34SMatthew G. Knepley { 1731d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1732d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 17332764a2aaSMatthew G. Knepley PetscDS prob; 1734d69c5d34SMatthew G. Knepley PetscFE *feRef; 173597c42addSMatthew G. Knepley PetscFV *fvRef; 1736d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1737d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1738d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 17399ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c; 17400f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1741d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1742d69c5d34SMatthew G. Knepley 1743d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1744d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1745c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1746e87a4003SBarry Smith ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr); 1747e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1748e87a4003SBarry Smith ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr); 1749e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1750d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1751d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 17529ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 17539ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 17542764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 175597c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 1756d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 175797c42addSMatthew G. Knepley PetscObject obj; 175897c42addSMatthew G. Knepley PetscClassId id; 1759aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 1760d69c5d34SMatthew G. Knepley 176197c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 176297c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 176397c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 176497c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 176597c42addSMatthew G. Knepley 17660f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1767d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 17680f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 176997c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 177097c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 177197c42addSMatthew G. Knepley PetscDualSpace Q; 177297c42addSMatthew G. Knepley 177397c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 177497c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 177597c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 177697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 177797c42addSMatthew G. Knepley } 17789c3cf19fSMatthew G. Knepley rTotDim += rNb; 1779d69c5d34SMatthew G. Knepley } 17802764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 17810f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 17820f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1783d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1784d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1785d69c5d34SMatthew G. Knepley PetscQuadrature f; 1786d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1787d69c5d34SMatthew G. Knepley PetscReal *points; 1788d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1789d69c5d34SMatthew G. Knepley 1790d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 179197c42addSMatthew G. Knepley if (feRef[fieldI]) { 1792d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 17930f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 179497c42addSMatthew G. Knepley } else { 179597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 179697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 179797c42addSMatthew G. Knepley } 1798d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1799d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1800d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 18019c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1802d69c5d34SMatthew G. Knepley npoints += Np; 1803d69c5d34SMatthew G. Knepley } 1804d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1805d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1806d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 18079c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1808d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1809d69c5d34SMatthew G. Knepley } 1810d69c5d34SMatthew G. Knepley 1811d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 181297c42addSMatthew G. Knepley PetscObject obj; 181397c42addSMatthew G. Knepley PetscClassId id; 1814d69c5d34SMatthew G. Knepley PetscReal *B; 18159c3cf19fSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j, qNc; 1816d69c5d34SMatthew G. Knepley 181797c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr); 181897c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 181997c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 182097c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1821d69c5d34SMatthew G. Knepley 1822d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 18230f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 18240f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1825ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1826ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 18279c3cf19fSMatthew G. Knepley if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", Nc, NcJ); 18280f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1829d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1830d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 18319c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 18329c3cf19fSMatthew G. Knepley if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ); 1833d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 183436a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 1835d172c84bSMatthew G. Knepley /* 1836d172c84bSMatthew G. Knepley cTotDim: Total columns in element interpolation matrix, sum of number of dual basis functionals in each field 1837d172c84bSMatthew G. Knepley offsetI, offsetJ: Offsets into the larger element interpolation matrix for different fields 1838d172c84bSMatthew G. Knepley fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals 1839d172c84bSMatthew G. Knepley qNC, Nc, Ncj, c: Number of components in this field 1840d172c84bSMatthew G. Knepley Np, p: Number of quad points in the fine grid functional i 1841d172c84bSMatthew G. Knepley k: i*Np + p, overall point number for the interpolation 1842d172c84bSMatthew G. Knepley */ 18439c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c]; 184436a6d9c0SMatthew G. Knepley } 1845d69c5d34SMatthew G. Knepley } 1846d69c5d34SMatthew G. Knepley } 18470f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1848ffe73a53SMatthew G. Knepley } 184997c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 185097c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 185197c42addSMatthew G. Knepley 185297c42addSMatthew G. Knepley /* Evaluate constant function at points */ 185397c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 185497c42addSMatthew G. Knepley cpdim = 1; 185597c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 185697c42addSMatthew G. Knepley if (fieldI == fieldJ) { 185797c42addSMatthew G. Knepley if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", Nc, NcJ); 185897c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 185997c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 18609c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 18619c3cf19fSMatthew G. Knepley if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ); 186297c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 186397c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 18649c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p*qNc+c]; 186597c42addSMatthew G. Knepley } 186697c42addSMatthew G. Knepley } 186797c42addSMatthew G. Knepley } 186897c42addSMatthew G. Knepley } 186997c42addSMatthew G. Knepley } 1870d172c84bSMatthew G. Knepley offsetJ += cpdim; 1871d69c5d34SMatthew G. Knepley } 1872d172c84bSMatthew G. Knepley offsetI += fpdim; 1873549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1874d69c5d34SMatthew G. Knepley } 18750f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 18767f5b169aSMatthew G. Knepley /* Preallocate matrix */ 18777f5b169aSMatthew G. Knepley { 1878c094ef40SMatthew G. Knepley Mat preallocator; 1879c094ef40SMatthew G. Knepley PetscScalar *vals; 1880c094ef40SMatthew G. Knepley PetscInt *cellCIndices, *cellFIndices; 1881c094ef40SMatthew G. Knepley PetscInt locRows, locCols, cell; 18827f5b169aSMatthew G. Knepley 1883c094ef40SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr); 1884c094ef40SMatthew G. Knepley ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr); 1885c094ef40SMatthew G. Knepley ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr); 1886c094ef40SMatthew G. Knepley ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr); 1887c094ef40SMatthew G. Knepley ierr = MatSetUp(preallocator);CHKERRQ(ierr); 1888c094ef40SMatthew G. Knepley ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 18897f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 18907f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 1891c094ef40SMatthew G. Knepley ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr); 18927f5b169aSMatthew G. Knepley } 1893c094ef40SMatthew G. Knepley ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr); 1894c094ef40SMatthew G. Knepley ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1895c094ef40SMatthew G. Knepley ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1896c094ef40SMatthew G. Knepley ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr); 1897c094ef40SMatthew G. Knepley ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 18987f5b169aSMatthew G. Knepley } 18997f5b169aSMatthew G. Knepley /* Fill matrix */ 19007f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1901d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1902934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1903d69c5d34SMatthew G. Knepley } 1904549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 190597c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 1906549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1907934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1908934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1909d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1910d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1911934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1912934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1913d69c5d34SMatthew G. Knepley } 1914d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1915d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1916d69c5d34SMatthew G. Knepley } 19176c73c22cSMatthew G. Knepley 1918bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user) 1919bd041c0cSMatthew G. Knepley { 1920bd041c0cSMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness"); 1921bd041c0cSMatthew G. Knepley } 1922bd041c0cSMatthew G. Knepley 192368132eb9SMatthew G. Knepley /*@ 192468132eb9SMatthew G. Knepley DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM. 192568132eb9SMatthew G. Knepley 192668132eb9SMatthew G. Knepley Input Parameters: 192768132eb9SMatthew G. Knepley + dmf - The fine mesh 192868132eb9SMatthew G. Knepley . dmc - The coarse mesh 192968132eb9SMatthew G. Knepley - user - The user context 193068132eb9SMatthew G. Knepley 193168132eb9SMatthew G. Knepley Output Parameter: 193268132eb9SMatthew G. Knepley . In - The interpolation matrix 193368132eb9SMatthew G. Knepley 193468132eb9SMatthew G. Knepley Level: developer 193568132eb9SMatthew G. Knepley 193668132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 193768132eb9SMatthew G. Knepley @*/ 193868132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user) 19394ef9d792SMatthew G. Knepley { 194064e98e1dSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 194164e98e1dSMatthew G. Knepley const char *name = "Interpolator"; 19424ef9d792SMatthew G. Knepley PetscDS prob; 19434ef9d792SMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 1944e8f14785SLisandro Dalcin PetscHSetIJ ht; 19454ef9d792SMatthew G. Knepley PetscLayout rLayout; 19464ef9d792SMatthew G. Knepley PetscInt *dnz, *onz; 19474ef9d792SMatthew G. Knepley PetscInt locRows, rStart, rEnd; 19484ef9d792SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 19494ef9d792SMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 19504ef9d792SMatthew G. Knepley PetscScalar *elemMat; 19514ef9d792SMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 19524ef9d792SMatthew G. Knepley PetscErrorCode ierr; 19534ef9d792SMatthew G. Knepley 19544ef9d792SMatthew G. Knepley PetscFunctionBegin; 195577711781SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 19564ef9d792SMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 19574ef9d792SMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 19584ef9d792SMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 19594ef9d792SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 19604ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 19614ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 1962e87a4003SBarry Smith ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr); 1963e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 1964e87a4003SBarry Smith ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr); 1965e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 19664ef9d792SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 19674ef9d792SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 19689c3cf19fSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 19694ef9d792SMatthew G. Knepley 19704ef9d792SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 19714ef9d792SMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 19724ef9d792SMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 19734ef9d792SMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 19744ef9d792SMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 19754ef9d792SMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 19764ef9d792SMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 19774ef9d792SMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 1978e8f14785SLisandro Dalcin ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr); 19794ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 19804ef9d792SMatthew G. Knepley PetscObject obj; 19814ef9d792SMatthew G. Knepley PetscClassId id; 1982c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 19834ef9d792SMatthew G. Knepley PetscQuadrature f; 198417f047d8SMatthew G. Knepley const PetscReal *qpoints; 198517f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 19864ef9d792SMatthew G. Knepley 19874ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 19884ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 19894ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 19904ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 19914ef9d792SMatthew G. Knepley 19924ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 19934ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 19944ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 19954ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 19964ef9d792SMatthew G. Knepley 19974ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 19984ef9d792SMatthew G. Knepley Nc = 1; 19994ef9d792SMatthew G. Knepley } 20004ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 20014ef9d792SMatthew G. Knepley /* For each fine grid cell */ 20024ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 20034ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 20044ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 20054ef9d792SMatthew G. Knepley 20066ecaa68aSToby Isaac ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 20074ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 20089c3cf19fSMatthew G. Knepley if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim); 20094ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 20104ef9d792SMatthew G. Knepley Vec pointVec; 20114ef9d792SMatthew G. Knepley PetscScalar *pV; 20123a93e3b7SToby Isaac PetscSF coarseCellSF = NULL; 20133a93e3b7SToby Isaac const PetscSFNode *coarseCells; 20149c3cf19fSMatthew G. Knepley PetscInt numCoarseCells, q, c; 20154ef9d792SMatthew G. Knepley 20164ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 20174ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 20189c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 20194ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 20204ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 20214ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 20224ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 2023c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2024c330f8ffSToby Isaac 20254ef9d792SMatthew G. Knepley /* Transform point to real space */ 2026c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 20274ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 20284ef9d792SMatthew G. Knepley } 20294ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 20304ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 20311555c271SMatthew G. Knepley /* OPT: Pack all quad points from fine cell */ 203262a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 20333a93e3b7SToby Isaac ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 20344ef9d792SMatthew G. Knepley /* Update preallocation info */ 20353a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 20363a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 20379c3cf19fSMatthew G. Knepley { 2038e8f14785SLisandro Dalcin PetscHashIJKey key; 2039e8f14785SLisandro Dalcin PetscBool missing; 20404ef9d792SMatthew G. Knepley 2041e8f14785SLisandro Dalcin key.i = findices[i]; 2042e8f14785SLisandro Dalcin if (key.i >= 0) { 20434ef9d792SMatthew G. Knepley /* Get indices for coarse elements */ 20444ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 20453a93e3b7SToby Isaac ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 20464ef9d792SMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 2047e8f14785SLisandro Dalcin key.j = cindices[c]; 2048e8f14785SLisandro Dalcin if (key.j < 0) continue; 2049e8f14785SLisandro Dalcin ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr); 20504ef9d792SMatthew G. Knepley if (missing) { 2051e8f14785SLisandro Dalcin if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart]; 2052e8f14785SLisandro Dalcin else ++onz[key.i-rStart]; 20534ef9d792SMatthew G. Knepley } 20544ef9d792SMatthew G. Knepley } 20553a93e3b7SToby Isaac ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 20564ef9d792SMatthew G. Knepley } 20574ef9d792SMatthew G. Knepley } 20588c543595SMatthew G. Knepley } 20593a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 20604ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 20614ef9d792SMatthew G. Knepley } 206246bdb399SToby Isaac ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 20634ef9d792SMatthew G. Knepley } 20644ef9d792SMatthew G. Knepley } 2065e8f14785SLisandro Dalcin ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr); 20664ef9d792SMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 20674ef9d792SMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 20684ef9d792SMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 20694ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 20704ef9d792SMatthew G. Knepley PetscObject obj; 20714ef9d792SMatthew G. Knepley PetscClassId id; 2072c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 20734ef9d792SMatthew G. Knepley PetscQuadrature f; 20744ef9d792SMatthew G. Knepley const PetscReal *qpoints, *qweights; 20759c3cf19fSMatthew G. Knepley PetscInt Nc, qNc, Np, fpdim, i, d; 20764ef9d792SMatthew G. Knepley 20774ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 20784ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 20794ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 20804ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 20814ef9d792SMatthew G. Knepley 20824ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 20834ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 20844ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 20854ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 20864ef9d792SMatthew G. Knepley 20874ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 20884ef9d792SMatthew G. Knepley Nc = 1; 208925ce1634SJed Brown } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %d",field); 20904ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 20914ef9d792SMatthew G. Knepley /* For each fine grid cell */ 20924ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 20934ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 20944ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 20954ef9d792SMatthew G. Knepley 20966ecaa68aSToby Isaac ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 20974ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 20989c3cf19fSMatthew G. Knepley if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim); 20994ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 21004ef9d792SMatthew G. Knepley Vec pointVec; 21014ef9d792SMatthew G. Knepley PetscScalar *pV; 210212111d7cSToby Isaac PetscSF coarseCellSF = NULL; 21033a93e3b7SToby Isaac const PetscSFNode *coarseCells; 210417f047d8SMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 21054ef9d792SMatthew G. Knepley 21064ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 21074ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 21089c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr); 21099c3cf19fSMatthew G. Knepley if (qNc != Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, Nc); 21104ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 21114ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 21124ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 21134ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 2114c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2115c330f8ffSToby Isaac 21164ef9d792SMatthew G. Knepley /* Transform point to real space */ 2117c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 21184ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 21194ef9d792SMatthew G. Knepley } 21204ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 21214ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 21221555c271SMatthew G. Knepley /* OPT: Read this out from preallocation information */ 212362a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 21244ef9d792SMatthew G. Knepley /* Update preallocation info */ 21253a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 21263a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 21274ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 21284ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2129826eb36dSMatthew G. Knepley PetscReal pVReal[3]; 2130c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2131826eb36dSMatthew G. Knepley 21323a93e3b7SToby Isaac ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 21334ef9d792SMatthew G. Knepley /* Transform points from real space to coarse reference space */ 21343a93e3b7SToby Isaac ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 2135e2d86523SMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 2136c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 21374ef9d792SMatthew G. Knepley 21384ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 21394ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 21404ef9d792SMatthew G. Knepley PetscReal *B; 21414ef9d792SMatthew G. Knepley 21424ef9d792SMatthew G. Knepley /* Evaluate coarse basis on contained point */ 21434ef9d792SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 21444ef9d792SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 21459c3cf19fSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 21464ef9d792SMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 21474ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 21489c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c]; 21494ef9d792SMatthew G. Knepley } 21504ef9d792SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 21514ef9d792SMatthew G. Knepley } else { 21524ef9d792SMatthew G. Knepley cpdim = 1; 21534ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 21549c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c]; 21554ef9d792SMatthew G. Knepley } 21564ef9d792SMatthew G. Knepley } 21574ef9d792SMatthew G. Knepley /* Update interpolator */ 21589c3cf19fSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 21599c3cf19fSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 21609c3cf19fSMatthew G. Knepley ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr); 21613a93e3b7SToby Isaac ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 21624ef9d792SMatthew G. Knepley } 21634ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 21643a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 21654ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 21664ef9d792SMatthew G. Knepley } 216746bdb399SToby Isaac ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 21684ef9d792SMatthew G. Knepley } 21694ef9d792SMatthew G. Knepley } 21704ef9d792SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 21714ef9d792SMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 21724ef9d792SMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 21734ef9d792SMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21744ef9d792SMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 217577711781SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 21764ef9d792SMatthew G. Knepley PetscFunctionReturn(0); 21774ef9d792SMatthew G. Knepley } 21784ef9d792SMatthew G. Knepley 217946fa42a0SMatthew G. Knepley /*@ 2180bd041c0cSMatthew G. Knepley DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM. 2181bd041c0cSMatthew G. Knepley 2182bd041c0cSMatthew G. Knepley Input Parameters: 2183bd041c0cSMatthew G. Knepley + dmf - The fine mesh 2184bd041c0cSMatthew G. Knepley . dmc - The coarse mesh 2185bd041c0cSMatthew G. Knepley - user - The user context 2186bd041c0cSMatthew G. Knepley 2187bd041c0cSMatthew G. Knepley Output Parameter: 2188bd041c0cSMatthew G. Knepley . mass - The mass matrix 2189bd041c0cSMatthew G. Knepley 2190bd041c0cSMatthew G. Knepley Level: developer 2191bd041c0cSMatthew G. Knepley 2192bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 2193bd041c0cSMatthew G. Knepley @*/ 2194bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user) 2195bd041c0cSMatthew G. Knepley { 2196bd041c0cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 2197bd041c0cSMatthew G. Knepley const char *name = "Mass Matrix"; 2198bd041c0cSMatthew G. Knepley PetscDS prob; 2199bd041c0cSMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 2200e8f14785SLisandro Dalcin PetscHSetIJ ht; 2201bd041c0cSMatthew G. Knepley PetscLayout rLayout; 2202bd041c0cSMatthew G. Knepley PetscInt *dnz, *onz; 2203bd041c0cSMatthew G. Knepley PetscInt locRows, rStart, rEnd; 2204bd041c0cSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 2205bd041c0cSMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 2206bd041c0cSMatthew G. Knepley PetscScalar *elemMat; 2207bd041c0cSMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 2208bd041c0cSMatthew G. Knepley PetscErrorCode ierr; 2209bd041c0cSMatthew G. Knepley 2210bd041c0cSMatthew G. Knepley PetscFunctionBegin; 2211bd041c0cSMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 2212bd041c0cSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 2213bd041c0cSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 2214bd041c0cSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 2215bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 2216bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 2217e87a4003SBarry Smith ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr); 2218e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 2219e87a4003SBarry Smith ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr); 2220e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 2221bd041c0cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 2222bd041c0cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2223bd041c0cSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 2224bd041c0cSMatthew G. Knepley 2225bd041c0cSMatthew G. Knepley ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr); 2226bd041c0cSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr); 2227bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 2228bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 2229bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 2230bd041c0cSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 2231bd041c0cSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 2232bd041c0cSMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 2233e8f14785SLisandro Dalcin ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr); 2234bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2235bd041c0cSMatthew G. Knepley PetscObject obj; 2236bd041c0cSMatthew G. Knepley PetscClassId id; 2237bd041c0cSMatthew G. Knepley PetscQuadrature quad; 2238bd041c0cSMatthew G. Knepley const PetscReal *qpoints; 2239bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 2240bd041c0cSMatthew G. Knepley 2241bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 2242bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2243bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);} 2244bd041c0cSMatthew G. Knepley else {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);} 2245bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr); 2246bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 2247bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2248bd041c0cSMatthew G. Knepley Vec pointVec; 2249bd041c0cSMatthew G. Knepley PetscScalar *pV; 2250bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 2251bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 2252bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, q, c; 2253bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 2254bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 2255bd041c0cSMatthew G. Knepley 2256bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2257bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2258bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 2259bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 2260bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 2261bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2262bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 2263c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2264c330f8ffSToby Isaac 2265bd041c0cSMatthew G. Knepley /* Transform point to real space */ 2266c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 2267bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 2268bd041c0cSMatthew G. Knepley } 2269bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2270bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 2271bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 2272bd041c0cSMatthew G. Knepley ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 2273bd041c0cSMatthew G. Knepley /* Update preallocation info */ 2274bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 2275bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 2276bd041c0cSMatthew G. Knepley { 2277e8f14785SLisandro Dalcin PetscHashIJKey key; 2278e8f14785SLisandro Dalcin PetscBool missing; 2279bd041c0cSMatthew G. Knepley 2280bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2281e8f14785SLisandro Dalcin key.i = findices[i]; 2282e8f14785SLisandro Dalcin if (key.i >= 0) { 2283bd041c0cSMatthew G. Knepley /* Get indices for coarse elements */ 2284bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2285bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2286bd041c0cSMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 2287e8f14785SLisandro Dalcin key.j = cindices[c]; 2288e8f14785SLisandro Dalcin if (key.j < 0) continue; 2289e8f14785SLisandro Dalcin ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr); 2290bd041c0cSMatthew G. Knepley if (missing) { 2291e8f14785SLisandro Dalcin if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart]; 2292e8f14785SLisandro Dalcin else ++onz[key.i-rStart]; 2293bd041c0cSMatthew G. Knepley } 2294bd041c0cSMatthew G. Knepley } 2295bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2296bd041c0cSMatthew G. Knepley } 2297bd041c0cSMatthew G. Knepley } 2298bd041c0cSMatthew G. Knepley } 2299bd041c0cSMatthew G. Knepley } 2300bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 2301bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 2302bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2303bd041c0cSMatthew G. Knepley } 2304bd041c0cSMatthew G. Knepley } 2305e8f14785SLisandro Dalcin ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr); 2306bd041c0cSMatthew G. Knepley ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 2307bd041c0cSMatthew G. Knepley ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 2308bd041c0cSMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 2309bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2310bd041c0cSMatthew G. Knepley PetscObject obj; 2311bd041c0cSMatthew G. Knepley PetscClassId id; 2312bd041c0cSMatthew G. Knepley PetscQuadrature quad; 2313bd041c0cSMatthew G. Knepley PetscReal *Bfine; 2314bd041c0cSMatthew G. Knepley const PetscReal *qpoints, *qweights; 2315bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 2316bd041c0cSMatthew G. Knepley 2317bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 2318bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2319bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);} 2320bd041c0cSMatthew G. Knepley else {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);} 2321bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr); 2322bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 2323bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2324bd041c0cSMatthew G. Knepley Vec pointVec; 2325bd041c0cSMatthew G. Knepley PetscScalar *pV; 2326bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 2327bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 2328bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 2329bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 2330bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 2331bd041c0cSMatthew G. Knepley 2332bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2333bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2334bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 2335bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 2336bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 2337bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2338bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 2339c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2340c330f8ffSToby Isaac 2341bd041c0cSMatthew G. Knepley /* Transform point to real space */ 2342c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 2343bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 2344bd041c0cSMatthew G. Knepley } 2345bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2346bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 2347bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 2348bd041c0cSMatthew G. Knepley /* Update matrix */ 2349bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 2350bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 2351bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2352bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2353bd041c0cSMatthew G. Knepley PetscReal pVReal[3]; 2354c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2355c330f8ffSToby Isaac 2356bd041c0cSMatthew G. Knepley 2357bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2358bd041c0cSMatthew G. Knepley /* Transform points from real space to coarse reference space */ 2359bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 2360bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 2361c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 2362bd041c0cSMatthew G. Knepley 2363bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 2364bd041c0cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2365bd041c0cSMatthew G. Knepley PetscReal *B; 2366bd041c0cSMatthew G. Knepley 2367bd041c0cSMatthew G. Knepley /* Evaluate coarse basis on contained point */ 2368bd041c0cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 2369bd041c0cSMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 2370bd041c0cSMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 2371bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2372bd041c0cSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 2373bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2374bd041c0cSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*Bfine[(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ; 2375bd041c0cSMatthew G. Knepley } 2376bd041c0cSMatthew G. Knepley /* Update interpolator */ 2377bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 2378bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 2379bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 2380bd041c0cSMatthew G. Knepley } 2381bd041c0cSMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 2382bd041c0cSMatthew G. Knepley } else { 2383bd041c0cSMatthew G. Knepley cpdim = 1; 2384bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2385bd041c0cSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 2386bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2387bd041c0cSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ; 2388bd041c0cSMatthew G. Knepley } 2389bd041c0cSMatthew G. Knepley /* Update interpolator */ 2390bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 2391bd041c0cSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %d %d Nf: %d %d Nc: %d %d\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr); 2392bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 2393bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 2394bd041c0cSMatthew G. Knepley } 2395bd041c0cSMatthew G. Knepley } 2396bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2397bd041c0cSMatthew G. Knepley } 2398bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2399bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 2400bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 2401bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2402bd041c0cSMatthew G. Knepley } 2403bd041c0cSMatthew G. Knepley } 2404bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 2405bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 2406bd041c0cSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 2407bd041c0cSMatthew G. Knepley ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2408bd041c0cSMatthew G. Knepley ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2409bd041c0cSMatthew G. Knepley PetscFunctionReturn(0); 2410bd041c0cSMatthew G. Knepley } 2411bd041c0cSMatthew G. Knepley 2412bd041c0cSMatthew G. Knepley /*@ 241346fa42a0SMatthew G. Knepley DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns 241446fa42a0SMatthew G. Knepley 241546fa42a0SMatthew G. Knepley Input Parameters: 241646fa42a0SMatthew G. Knepley + dmc - The coarse mesh 241746fa42a0SMatthew G. Knepley - dmf - The fine mesh 241846fa42a0SMatthew G. Knepley - user - The user context 241946fa42a0SMatthew G. Knepley 242046fa42a0SMatthew G. Knepley Output Parameter: 242146fa42a0SMatthew G. Knepley . sc - The mapping 242246fa42a0SMatthew G. Knepley 242346fa42a0SMatthew G. Knepley Level: developer 242446fa42a0SMatthew G. Knepley 242546fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 242646fa42a0SMatthew G. Knepley @*/ 24277c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 24287c927364SMatthew G. Knepley { 2429e9d4ef1bSMatthew G. Knepley PetscDS prob; 24307c927364SMatthew G. Knepley PetscFE *feRef; 243197c42addSMatthew G. Knepley PetscFV *fvRef; 24327c927364SMatthew G. Knepley Vec fv, cv; 24337c927364SMatthew G. Knepley IS fis, cis; 24347c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 24357c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 24360bd915a7SMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m; 24377c927364SMatthew G. Knepley PetscErrorCode ierr; 24387c927364SMatthew G. Knepley 24397c927364SMatthew G. Knepley PetscFunctionBegin; 244075a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2441c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 2442e87a4003SBarry Smith ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr); 2443e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 2444e87a4003SBarry Smith ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr); 2445e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 24467c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 24477c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 24489ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 24499ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2450e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 245197c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 24527c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 245397c42addSMatthew G. Knepley PetscObject obj; 245497c42addSMatthew G. Knepley PetscClassId id; 2455aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 24567c927364SMatthew G. Knepley 245797c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 245897c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 245997c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 246097c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 246197c42addSMatthew G. Knepley 24627c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 24637c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 24647c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 246597c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 246697c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 246797c42addSMatthew G. Knepley PetscDualSpace Q; 246897c42addSMatthew G. Knepley 246997c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 247097c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 247197c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 247297c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 247397c42addSMatthew G. Knepley } 2474d172c84bSMatthew G. Knepley fTotDim += fNb; 24757c927364SMatthew G. Knepley } 2476e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 24777c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 24787c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 24797c927364SMatthew G. Knepley PetscFE feC; 248097c42addSMatthew G. Knepley PetscFV fvC; 24817c927364SMatthew G. Knepley PetscDualSpace QF, QC; 2482d172c84bSMatthew G. Knepley PetscInt order = -1, NcF, NcC, fpdim, cpdim; 24837c927364SMatthew G. Knepley 248497c42addSMatthew G. Knepley if (feRef[field]) { 2485e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 24867c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 24877c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 24887c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 2489d172c84bSMatthew G. Knepley ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr); 24907c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 24917c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 24927c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 249397c42addSMatthew G. Knepley } else { 249497c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 249597c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 249697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 249797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 249897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 249997c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 250097c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 250197c42addSMatthew G. Knepley } 250297c42addSMatthew G. Knepley if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC); 25037c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 25047c927364SMatthew G. Knepley PetscQuadrature cfunc; 2505d172c84bSMatthew G. Knepley const PetscReal *cqpoints, *cqweights; 2506d172c84bSMatthew G. Knepley PetscInt NqcC, NpC; 250797c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 25087c927364SMatthew G. Knepley 25097c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 2510d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr); 2511d172c84bSMatthew G. Knepley if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcC, NcC); 251297c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 25137c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 25147c927364SMatthew G. Knepley PetscQuadrature ffunc; 2515d172c84bSMatthew G. Knepley const PetscReal *fqpoints, *fqweights; 25167c927364SMatthew G. Knepley PetscReal sum = 0.0; 2517d172c84bSMatthew G. Knepley PetscInt NqcF, NpF; 25187c927364SMatthew G. Knepley 25197c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 2520d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr); 2521d172c84bSMatthew G. Knepley if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcF, NcF); 25227c927364SMatthew G. Knepley if (NpC != NpF) continue; 25237c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 25247c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 2525d172c84bSMatthew G. Knepley for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]); 2526d172c84bSMatthew G. Knepley if (sum < 1.0e-9) continue; 2527d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+f; 252897c42addSMatthew G. Knepley found = PETSC_TRUE; 25297c927364SMatthew G. Knepley break; 25307c927364SMatthew G. Knepley } 253197c42addSMatthew G. Knepley if (!found) { 253297c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 2533d172c84bSMatthew G. Knepley if (fvRef[field] || (feRef[field] && order == 0)) { 2534d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+0; 253597c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 253697c42addSMatthew G. Knepley } 25377c927364SMatthew G. Knepley } 2538d172c84bSMatthew G. Knepley offsetC += cpdim; 2539d172c84bSMatthew G. Knepley offsetF += fpdim; 25407c927364SMatthew G. Knepley } 254197c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 254297c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 25437c927364SMatthew G. Knepley 25447c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 25457c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 25460bd915a7SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr); 25477c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 25487c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 2549aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 2550aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 25517c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 25527c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 25537c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 25547c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 25550bd915a7SMatthew G. Knepley if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue; 25567c927364SMatthew G. Knepley if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]); 25577c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 25587c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 25597c927364SMatthew G. Knepley } 25607c927364SMatthew G. Knepley } 25617c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 25627c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 25637c927364SMatthew G. Knepley 25647c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 25657c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 25667c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 25677c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 25687c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 25697c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 25707c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 257175a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2572cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 2573cb1e1211SMatthew G Knepley } 2574