1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2da97024aSMatthew G. Knepley #include <petscsf.h> 3cb1e1211SMatthew G Knepley 4af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 5af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h> 6a0845e3aSMatthew G. Knepley 746fa42a0SMatthew G. Knepley /*@ 846fa42a0SMatthew G. Knepley DMPlexGetScale - Get the scale for the specified fundamental unit 946fa42a0SMatthew G. Knepley 1046fa42a0SMatthew G. Knepley Not collective 1146fa42a0SMatthew G. Knepley 1246fa42a0SMatthew G. Knepley Input Arguments: 1346fa42a0SMatthew G. Knepley + dm - the DM 1446fa42a0SMatthew G. Knepley - unit - The SI unit 1546fa42a0SMatthew G. Knepley 1646fa42a0SMatthew G. Knepley Output Argument: 1746fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit 1846fa42a0SMatthew G. Knepley 1946fa42a0SMatthew G. Knepley Level: advanced 2046fa42a0SMatthew G. Knepley 2146fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit 2246fa42a0SMatthew G. Knepley @*/ 23cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 24cb1e1211SMatthew G Knepley { 25cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 26cb1e1211SMatthew G Knepley 27cb1e1211SMatthew G Knepley PetscFunctionBegin; 28cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 29cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 30cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 31cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 32cb1e1211SMatthew G Knepley } 33cb1e1211SMatthew G Knepley 3446fa42a0SMatthew G. Knepley /*@ 3546fa42a0SMatthew G. Knepley DMPlexSetScale - Set the scale for the specified fundamental unit 3646fa42a0SMatthew G. Knepley 3746fa42a0SMatthew G. Knepley Not collective 3846fa42a0SMatthew G. Knepley 3946fa42a0SMatthew G. Knepley Input Arguments: 4046fa42a0SMatthew G. Knepley + dm - the DM 4146fa42a0SMatthew G. Knepley . unit - The SI unit 4246fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit 4346fa42a0SMatthew G. Knepley 4446fa42a0SMatthew G. Knepley Level: advanced 4546fa42a0SMatthew G. Knepley 4646fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit 4746fa42a0SMatthew G. Knepley @*/ 48cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 49cb1e1211SMatthew G Knepley { 50cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 51cb1e1211SMatthew G Knepley 52cb1e1211SMatthew G Knepley PetscFunctionBegin; 53cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 54cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 55cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 56cb1e1211SMatthew G Knepley } 57cb1e1211SMatthew G Knepley 58d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx) 59026175e5SToby Isaac { 6012adca46SMatthew 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}}}; 61026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *) ctx; 62ad917190SMatthew G. Knepley PetscInt dim2 = ctxInt[0]; 63026175e5SToby Isaac PetscInt d = ctxInt[1]; 64026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 65026175e5SToby Isaac 66ad917190SMatthew G. Knepley PetscFunctionBegin; 67ad917190SMatthew G. Knepley if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2); 68026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 69026175e5SToby Isaac if (d < dim) { 7012adca46SMatthew G. Knepley mode[d] = 1.; /* Translation along axis d */ 71ad917190SMatthew G. Knepley } else { 72026175e5SToby Isaac for (i = 0; i < dim; i++) { 73026175e5SToby Isaac for (j = 0; j < dim; j++) { 7412adca46SMatthew G. Knepley mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */ 75026175e5SToby Isaac } 76026175e5SToby Isaac } 77026175e5SToby Isaac } 78ad917190SMatthew G. Knepley PetscFunctionReturn(0); 79026175e5SToby Isaac } 80026175e5SToby Isaac 81cb1e1211SMatthew G Knepley /*@C 8212adca46SMatthew G. Knepley DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation 83cb1e1211SMatthew G Knepley 84cb1e1211SMatthew G Knepley Collective on DM 85cb1e1211SMatthew G Knepley 86cb1e1211SMatthew G Knepley Input Arguments: 87026175e5SToby Isaac . dm - the DM 88cb1e1211SMatthew G Knepley 89cb1e1211SMatthew G Knepley Output Argument: 90cb1e1211SMatthew G Knepley . sp - the null space 91cb1e1211SMatthew G Knepley 9212adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 93cb1e1211SMatthew G Knepley 94cb1e1211SMatthew G Knepley Level: advanced 95cb1e1211SMatthew G Knepley 9612adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG 97cb1e1211SMatthew G Knepley @*/ 98026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp) 99cb1e1211SMatthew G Knepley { 100cb1e1211SMatthew G Knepley MPI_Comm comm; 101026175e5SToby Isaac Vec mode[6]; 102026175e5SToby Isaac PetscSection section, globalSection; 1039d8fbdeaSMatthew G. Knepley PetscInt dim, dimEmbed, n, m, d, i, j; 104cb1e1211SMatthew G Knepley PetscErrorCode ierr; 105cb1e1211SMatthew G Knepley 106cb1e1211SMatthew G Knepley PetscFunctionBegin; 107cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 108c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1099d8fbdeaSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 110cb1e1211SMatthew G Knepley if (dim == 1) { 111cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 112cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 113cb1e1211SMatthew G Knepley } 114026175e5SToby Isaac ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 115026175e5SToby Isaac ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 116cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 117cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 118cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 119cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 120cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 121cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 122026175e5SToby Isaac for (d = 0; d < m; d++) { 123330485fdSToby Isaac PetscInt ctx[2]; 124d1828a1cSMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private; 125026175e5SToby Isaac void *voidctx = (void *) (&ctx[0]); 126cb1e1211SMatthew G Knepley 1279d8fbdeaSMatthew G. Knepley ctx[0] = dimEmbed; 128330485fdSToby Isaac ctx[1] = d; 1290709b2feSToby Isaac ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 130cb1e1211SMatthew G Knepley } 131cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 132cb1e1211SMatthew G Knepley /* Orthonormalize system */ 133cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 134cb1e1211SMatthew G Knepley PetscScalar dots[6]; 135cb1e1211SMatthew G Knepley 136cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 137cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 138cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 139cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 140cb1e1211SMatthew G Knepley } 141cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 142cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 143cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 144cb1e1211SMatthew G Knepley } 145cb1e1211SMatthew G Knepley 14612adca46SMatthew G. Knepley /*@C 14712adca46SMatthew G. Knepley DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation 14812adca46SMatthew G. Knepley 14912adca46SMatthew G. Knepley Collective on DM 15012adca46SMatthew G. Knepley 15112adca46SMatthew G. Knepley Input Arguments: 15212adca46SMatthew G. Knepley + dm - the DM 15312adca46SMatthew G. Knepley . nb - The number of bodies 15412adca46SMatthew G. Knepley . label - The DMLabel marking each domain 15512adca46SMatthew G. Knepley . nids - The number of ids per body 15612adca46SMatthew G. Knepley - ids - An array of the label ids in sequence for each domain 15712adca46SMatthew G. Knepley 15812adca46SMatthew G. Knepley Output Argument: 15912adca46SMatthew G. Knepley . sp - the null space 16012adca46SMatthew G. Knepley 16112adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 16212adca46SMatthew G. Knepley 16312adca46SMatthew G. Knepley Level: advanced 16412adca46SMatthew G. Knepley 16512adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate() 16612adca46SMatthew G. Knepley @*/ 16712adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp) 16812adca46SMatthew G. Knepley { 16912adca46SMatthew G. Knepley MPI_Comm comm; 17012adca46SMatthew G. Knepley PetscSection section, globalSection; 17112adca46SMatthew G. Knepley Vec *mode; 17212adca46SMatthew G. Knepley PetscScalar *dots; 17312adca46SMatthew G. Knepley PetscInt dim, dimEmbed, n, m, b, d, i, j, off; 17412adca46SMatthew G. Knepley PetscErrorCode ierr; 17512adca46SMatthew G. Knepley 17612adca46SMatthew G. Knepley PetscFunctionBegin; 17712adca46SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 17812adca46SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 17912adca46SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 18012adca46SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 18112adca46SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 18212adca46SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 18312adca46SMatthew G. Knepley m = nb * (dim*(dim+1))/2; 18412adca46SMatthew G. Knepley ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr); 18512adca46SMatthew G. Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 18612adca46SMatthew G. Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 18712adca46SMatthew G. Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 18812adca46SMatthew G. Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 18912adca46SMatthew G. Knepley for (b = 0, off = 0; b < nb; ++b) { 19012adca46SMatthew G. Knepley for (d = 0; d < m/nb; ++d) { 19112adca46SMatthew G. Knepley PetscInt ctx[2]; 19212adca46SMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private; 19312adca46SMatthew G. Knepley void *voidctx = (void *) (&ctx[0]); 19412adca46SMatthew G. Knepley 19512adca46SMatthew G. Knepley ctx[0] = dimEmbed; 19612adca46SMatthew G. Knepley ctx[1] = d; 19712adca46SMatthew G. Knepley ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 19812adca46SMatthew G. Knepley off += nids[b]; 19912adca46SMatthew G. Knepley } 20012adca46SMatthew G. Knepley } 20112adca46SMatthew G. Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 20212adca46SMatthew G. Knepley /* Orthonormalize system */ 20312adca46SMatthew G. Knepley for (i = 0; i < m; ++i) { 20412adca46SMatthew G. Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 20512adca46SMatthew G. Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 20612adca46SMatthew G. Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 20712adca46SMatthew G. Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 20812adca46SMatthew G. Knepley } 20912adca46SMatthew G. Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 21012adca46SMatthew G. Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 21112adca46SMatthew G. Knepley ierr = PetscFree2(mode, dots);CHKERRQ(ierr); 21212adca46SMatthew G. Knepley PetscFunctionReturn(0); 21312adca46SMatthew G. Knepley } 21412adca46SMatthew G. Knepley 215b29cfa1cSToby Isaac /*@ 216b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 217b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 218b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 219b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 220b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 221b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 222b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 223b29cfa1cSToby Isaac 224b29cfa1cSToby Isaac Input Parameters: 225b29cfa1cSToby Isaac + dm - the DMPlex object 226b29cfa1cSToby Isaac - height - the maximum projection height >= 0 227b29cfa1cSToby Isaac 228b29cfa1cSToby Isaac Level: advanced 229b29cfa1cSToby Isaac 2304d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 231b29cfa1cSToby Isaac @*/ 232b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 233b29cfa1cSToby Isaac { 234b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 235b29cfa1cSToby Isaac 236b29cfa1cSToby Isaac PetscFunctionBegin; 237b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 238b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 239b29cfa1cSToby Isaac PetscFunctionReturn(0); 240b29cfa1cSToby Isaac } 241b29cfa1cSToby Isaac 242b29cfa1cSToby Isaac /*@ 243b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 244b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 245b29cfa1cSToby Isaac 246b29cfa1cSToby Isaac Input Parameters: 247b29cfa1cSToby Isaac . dm - the DMPlex object 248b29cfa1cSToby Isaac 249b29cfa1cSToby Isaac Output Parameters: 250b29cfa1cSToby Isaac . height - the maximum projection height 251b29cfa1cSToby Isaac 252b29cfa1cSToby Isaac Level: intermediate 253b29cfa1cSToby Isaac 2544d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 255b29cfa1cSToby Isaac @*/ 256b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 257b29cfa1cSToby Isaac { 258b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 259b29cfa1cSToby Isaac 260b29cfa1cSToby Isaac PetscFunctionBegin; 261b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 262b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 263b29cfa1cSToby Isaac PetscFunctionReturn(0); 264b29cfa1cSToby Isaac } 265b29cfa1cSToby Isaac 266b278463cSMatthew G. Knepley /*@C 267b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector 268b278463cSMatthew G. Knepley 269b278463cSMatthew G. Knepley Input Parameters: 270b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 271b278463cSMatthew G. Knepley . time - The time 272b278463cSMatthew G. Knepley . field - The field to constrain 2731c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 2741c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 275b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 276b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 277b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 278b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 279b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 280b278463cSMatthew G. Knepley 281b278463cSMatthew G. Knepley Output Parameter: 282b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 283b278463cSMatthew G. Knepley 284b278463cSMatthew G. Knepley Level: developer 285b278463cSMatthew G. Knepley 286b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 287b278463cSMatthew G. Knepley @*/ 2881c531cf8SMatthew 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) 28955f2e967SMatthew G. Knepley { 2900163fd50SMatthew G. Knepley PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 29155f2e967SMatthew G. Knepley void **ctxs; 292d7ddef95SMatthew G. Knepley PetscInt numFields; 293d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 294d7ddef95SMatthew G. Knepley 295d7ddef95SMatthew G. Knepley PetscFunctionBegin; 296d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 297d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 298d7ddef95SMatthew G. Knepley funcs[field] = func; 299d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 3001c531cf8SMatthew G. Knepley ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 301d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 302d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 303d7ddef95SMatthew G. Knepley } 304d7ddef95SMatthew G. Knepley 305b278463cSMatthew G. Knepley /*@C 306b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector 307b278463cSMatthew G. Knepley 308b278463cSMatthew G. Knepley Input Parameters: 309b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 310b278463cSMatthew G. Knepley . time - The time 311b278463cSMatthew G. Knepley . locU - A local vector with the input solution values 312b278463cSMatthew G. Knepley . field - The field to constrain 3131c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 3141c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 315b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 316b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 317b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 318b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 319b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 320b278463cSMatthew G. Knepley 321b278463cSMatthew G. Knepley Output Parameter: 322b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 323b278463cSMatthew G. Knepley 324b278463cSMatthew G. Knepley Level: developer 325b278463cSMatthew G. Knepley 326b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary() 327b278463cSMatthew G. Knepley @*/ 3281c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], 329c60e475cSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 330c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 331c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 33297b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], 333b278463cSMatthew G. Knepley PetscScalar[]), 334b278463cSMatthew G. Knepley void *ctx, Vec locX) 335c60e475cSMatthew G. Knepley { 336c60e475cSMatthew G. Knepley void (**funcs)(PetscInt, PetscInt, PetscInt, 337c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 338c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 33997b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]); 340c60e475cSMatthew G. Knepley void **ctxs; 341c60e475cSMatthew G. Knepley PetscInt numFields; 342c60e475cSMatthew G. Knepley PetscErrorCode ierr; 343c60e475cSMatthew G. Knepley 344c60e475cSMatthew G. Knepley PetscFunctionBegin; 345c60e475cSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 346c60e475cSMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 347c60e475cSMatthew G. Knepley funcs[field] = func; 348c60e475cSMatthew G. Knepley ctxs[field] = ctx; 3491c531cf8SMatthew G. Knepley ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 350c60e475cSMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 351c60e475cSMatthew G. Knepley PetscFunctionReturn(0); 352c60e475cSMatthew G. Knepley } 353c60e475cSMatthew G. Knepley 354b278463cSMatthew G. Knepley /*@C 355b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector 356b278463cSMatthew G. Knepley 357b278463cSMatthew G. Knepley Input Parameters: 358b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 359b278463cSMatthew G. Knepley . time - The time 360b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information 361b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information 362b278463cSMatthew G. Knepley . Grad - A vector with the FVM cell gradient information 363b278463cSMatthew G. Knepley . field - The field to constrain 3641c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 3651c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 366b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 367b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 368b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 369b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 370b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 371b278463cSMatthew G. Knepley 372b278463cSMatthew G. Knepley Output Parameter: 373b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 374b278463cSMatthew G. Knepley 375b278463cSMatthew G. Knepley Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary() 376b278463cSMatthew G. Knepley 377b278463cSMatthew G. Knepley Level: developer 378b278463cSMatthew G. Knepley 379b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 380b278463cSMatthew G. Knepley @*/ 3811c531cf8SMatthew 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[], 382b278463cSMatthew G. Knepley PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX) 383d7ddef95SMatthew G. Knepley { 38461f58d28SMatthew G. Knepley PetscDS prob; 385da97024aSMatthew G. Knepley PetscSF sf; 386d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 38720369375SToby Isaac const PetscScalar *facegeom, *cellgeom = NULL, *grad; 388da97024aSMatthew G. Knepley const PetscInt *leaves; 389d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 390520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 391e735a8a9SMatthew G. Knepley PetscErrorCode ierr, ierru = 0; 392d7ddef95SMatthew G. Knepley 393d7ddef95SMatthew G. Knepley PetscFunctionBegin; 394da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 395da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 396da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 397d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 398d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 39961f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 400d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 401d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 40220369375SToby Isaac if (cellGeometry) { 403d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 404d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 40520369375SToby Isaac } 406d7ddef95SMatthew G. Knepley if (Grad) { 407c0a6632aSMatthew G. Knepley PetscFV fv; 408c0a6632aSMatthew G. Knepley 409c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 410d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 411d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 412d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 41369291d52SBarry Smith ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 414d7ddef95SMatthew G. Knepley } 415d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 416d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 417d7ddef95SMatthew G. Knepley IS faceIS; 418d7ddef95SMatthew G. Knepley const PetscInt *faces; 419d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 420d7ddef95SMatthew G. Knepley 421d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 422d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 423d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 424d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 425d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 426d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 427640bce14SSatish Balay PetscFVFaceGeom *fg; 428d7ddef95SMatthew G. Knepley 429d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 430da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 431da97024aSMatthew G. Knepley if (loc >= 0) continue; 432d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 433d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 434d7ddef95SMatthew G. Knepley if (Grad) { 435640bce14SSatish Balay PetscFVCellGeom *cg; 436640bce14SSatish Balay PetscScalar *cx, *cgrad; 437d7ddef95SMatthew G. Knepley PetscScalar *xG; 438d7ddef95SMatthew G. Knepley PetscReal dx[3]; 439d7ddef95SMatthew G. Knepley PetscInt d; 440d7ddef95SMatthew G. Knepley 441d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 442d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 443d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 444520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 445d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 446d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 447e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx); 448e735a8a9SMatthew G. Knepley if (ierru) { 449e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 450e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 451e735a8a9SMatthew G. Knepley goto cleanup; 452e735a8a9SMatthew G. Knepley } 453d7ddef95SMatthew G. Knepley } else { 454640bce14SSatish Balay PetscScalar *xI; 455d7ddef95SMatthew G. Knepley PetscScalar *xG; 456d7ddef95SMatthew G. Knepley 457d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 458520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 459e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx); 460e735a8a9SMatthew G. Knepley if (ierru) { 461e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 462e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 463e735a8a9SMatthew G. Knepley goto cleanup; 464e735a8a9SMatthew G. Knepley } 465d7ddef95SMatthew G. Knepley } 466d7ddef95SMatthew G. Knepley } 467d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 468d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 469d7ddef95SMatthew G. Knepley } 470e735a8a9SMatthew G. Knepley cleanup: 471d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 472d7ddef95SMatthew G. Knepley if (Grad) { 47369291d52SBarry Smith ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 474d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 475d7ddef95SMatthew G. Knepley } 476e735a8a9SMatthew G. Knepley if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);} 477d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 478e735a8a9SMatthew G. Knepley CHKERRQ(ierru); 479d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 480d7ddef95SMatthew G. Knepley } 481d7ddef95SMatthew G. Knepley 482f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 483d7ddef95SMatthew G. Knepley { 484d7ddef95SMatthew G. Knepley PetscInt numBd, b; 48555f2e967SMatthew G. Knepley PetscErrorCode ierr; 48655f2e967SMatthew G. Knepley 48755f2e967SMatthew G. Knepley PetscFunctionBegin; 488dff059c6SToby Isaac ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr); 48955f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 490f971fd6bSMatthew G. Knepley DMBoundaryConditionType type; 491d7ddef95SMatthew G. Knepley const char *labelname; 492d7ddef95SMatthew G. Knepley DMLabel label; 4931c531cf8SMatthew G. Knepley PetscInt field, Nc; 4941c531cf8SMatthew G. Knepley const PetscInt *comps; 495d7ddef95SMatthew G. Knepley PetscObject obj; 496d7ddef95SMatthew G. Knepley PetscClassId id; 497a30ec4eaSSatish Balay void (*func)(void); 498d7ddef95SMatthew G. Knepley PetscInt numids; 499d7ddef95SMatthew G. Knepley const PetscInt *ids; 50055f2e967SMatthew G. Knepley void *ctx; 50155f2e967SMatthew G. Knepley 5021c531cf8SMatthew G. Knepley ierr = DMGetBoundary(dm, b, &type, NULL, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 503f971fd6bSMatthew G. Knepley if (insertEssential != (type & DM_BC_ESSENTIAL)) continue; 504c58f1c22SToby Isaac ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 505d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 506d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 507d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 508c60e475cSMatthew G. Knepley switch (type) { 509c60e475cSMatthew G. Knepley /* for FEM, there is no insertion to be done for non-essential boundary conditions */ 510c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL: 511092e5057SToby Isaac ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 5121c531cf8SMatthew 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); 513092e5057SToby Isaac ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 514c60e475cSMatthew G. Knepley break; 515c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL_FIELD: 516c60e475cSMatthew G. Knepley ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 5171c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, 518b278463cSMatthew G. Knepley (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 519c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 52097b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr); 521c60e475cSMatthew G. Knepley ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 522c60e475cSMatthew G. Knepley break; 523c60e475cSMatthew G. Knepley default: break; 524c60e475cSMatthew G. Knepley } 525d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 52643ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 5271c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids, 528b278463cSMatthew G. Knepley (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 529d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 53055f2e967SMatthew G. Knepley } 53155f2e967SMatthew G. Knepley PetscFunctionReturn(0); 53255f2e967SMatthew G. Knepley } 53355f2e967SMatthew G. Knepley 534f1d73a7aSMatthew G. Knepley /*@ 535f1d73a7aSMatthew G. Knepley DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector 536f1d73a7aSMatthew G. Knepley 537f1d73a7aSMatthew G. Knepley Input Parameters: 538f1d73a7aSMatthew G. Knepley + dm - The DM 539f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions 540f1d73a7aSMatthew G. Knepley . time - The time 541f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations 542f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations 543f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations 544f1d73a7aSMatthew G. Knepley 545f1d73a7aSMatthew G. Knepley Output Parameters: 546f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values 547f1d73a7aSMatthew G. Knepley 548f1d73a7aSMatthew G. Knepley Level: developer 549f1d73a7aSMatthew G. Knepley 550f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal() 551f1d73a7aSMatthew G. Knepley @*/ 552f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 553f1d73a7aSMatthew G. Knepley { 554f1d73a7aSMatthew G. Knepley PetscErrorCode ierr; 555f1d73a7aSMatthew G. Knepley 556f1d73a7aSMatthew G. Knepley PetscFunctionBegin; 557f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 558f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 559f1d73a7aSMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 560f1d73a7aSMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 561f1d73a7aSMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 562f1d73a7aSMatthew G. Knepley ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr); 563f1d73a7aSMatthew G. Knepley PetscFunctionReturn(0); 564f1d73a7aSMatthew G. Knepley } 565f1d73a7aSMatthew G. Knepley 5660709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 567cb1e1211SMatthew G Knepley { 568574a98acSMatthew G. Knepley Vec localX; 569574a98acSMatthew G. Knepley PetscErrorCode ierr; 570574a98acSMatthew G. Knepley 571574a98acSMatthew G. Knepley PetscFunctionBegin; 572574a98acSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 5735d42b983SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr); 574574a98acSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 575574a98acSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 576574a98acSMatthew G. Knepley ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr); 577574a98acSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 578574a98acSMatthew G. Knepley PetscFunctionReturn(0); 579574a98acSMatthew G. Knepley } 580574a98acSMatthew G. Knepley 581574a98acSMatthew G. Knepley /*@C 582574a98acSMatthew G. Knepley DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 583574a98acSMatthew G. Knepley 584574a98acSMatthew G. Knepley Input Parameters: 585574a98acSMatthew G. Knepley + dm - The DM 586574a98acSMatthew G. Knepley . time - The time 587574a98acSMatthew G. Knepley . funcs - The functions to evaluate for each field component 588574a98acSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 589574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector 590574a98acSMatthew G. Knepley 591574a98acSMatthew G. Knepley Output Parameter: 592574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2 593574a98acSMatthew G. Knepley 594574a98acSMatthew G. Knepley Level: developer 595574a98acSMatthew G. Knepley 596574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff() 597574a98acSMatthew G. Knepley @*/ 598574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff) 599574a98acSMatthew G. Knepley { 600cb1e1211SMatthew G Knepley const PetscInt debug = 0; 601cb1e1211SMatthew G Knepley PetscSection section; 602c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 60315496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 6047318780aSToby Isaac PetscReal *coords, *detJ, *J; 605cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 6067318780aSToby Isaac const PetscReal *quadWeights; 607aed3cbd0SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset; 608cb1e1211SMatthew G Knepley PetscErrorCode ierr; 609cb1e1211SMatthew G Knepley 610cb1e1211SMatthew G Knepley PetscFunctionBegin; 611c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 6127318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 613cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 614cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 615cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 61615496722SMatthew G. Knepley PetscObject obj; 61715496722SMatthew G. Knepley PetscClassId id; 618c5bbbd5bSMatthew G. Knepley PetscInt Nc; 619c5bbbd5bSMatthew G. Knepley 62015496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 62115496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 62215496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 62315496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 62415496722SMatthew G. Knepley 6250f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 6260f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 62715496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 62815496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 62915496722SMatthew G. Knepley 63015496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 63115496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 63215496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 633c5bbbd5bSMatthew G. Knepley numComponents += Nc; 634cb1e1211SMatthew G Knepley } 6359c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr); 636beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 6377318780aSToby Isaac ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 638aed3cbd0SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 639aed3cbd0SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 6409ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 6419ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 642cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 643a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 644cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 6459c3cf19fSMatthew G. Knepley PetscInt qc = 0; 646cb1e1211SMatthew G Knepley 6477318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 648cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 649cb1e1211SMatthew G Knepley 65015496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 65115496722SMatthew G. Knepley PetscObject obj; 65215496722SMatthew G. Knepley PetscClassId id; 653c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 65415496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 655cb1e1211SMatthew G Knepley 65615496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 65715496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 65815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 65915496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 66015496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 661cb1e1211SMatthew G Knepley if (debug) { 662cb1e1211SMatthew G Knepley char title[1024]; 663cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 6644c848028SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 665cb1e1211SMatthew G Knepley } 6667318780aSToby Isaac for (q = 0; q < Nq; ++q) { 6677318780aSToby 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); 6687318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[coordDim * q], Nc, funcVal, ctx); 669e735a8a9SMatthew G. Knepley if (ierr) { 670e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 671e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 672e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 6737318780aSToby Isaac ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 674e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 675e735a8a9SMatthew G. Knepley } 67615496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 67715496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 67815496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 67915496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 680beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 681beaa55a6SMatthew 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);} 682beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 683cb1e1211SMatthew G Knepley } 684cb1e1211SMatthew G Knepley } 6859c3cf19fSMatthew G. Knepley fieldOffset += Nb; 686beaa55a6SMatthew G. Knepley qc += Nc; 687cb1e1211SMatthew G Knepley } 688cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 689cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 690cb1e1211SMatthew G Knepley localDiff += elemDiff; 691cb1e1211SMatthew G Knepley } 6927318780aSToby Isaac ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 693b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 694cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 695cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 696cb1e1211SMatthew G Knepley } 697cb1e1211SMatthew G Knepley 698b698f381SToby 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) 699cb1e1211SMatthew G Knepley { 70040e14135SMatthew G. Knepley const PetscInt debug = 0; 701cb1e1211SMatthew G Knepley PetscSection section; 70240e14135SMatthew G. Knepley PetscQuadrature quad; 70340e14135SMatthew G. Knepley Vec localX; 7049c3cf19fSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 7059c3cf19fSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7067318780aSToby Isaac PetscReal *coords, *realSpaceDer, *J, *invJ, *detJ; 70740e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 7089c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset; 709cb1e1211SMatthew G Knepley PetscErrorCode ierr; 710cb1e1211SMatthew G Knepley 711cb1e1211SMatthew G Knepley PetscFunctionBegin; 712c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 7137318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 71440e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 71540e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 71640e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 71740e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 71840e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 719652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 7200f2d7e86SMatthew G. Knepley PetscFE fe; 72140e14135SMatthew G. Knepley PetscInt Nc; 722652b88e8SMatthew G. Knepley 7230f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7240f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7250f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 72640e14135SMatthew G. Knepley numComponents += Nc; 727652b88e8SMatthew G. Knepley } 7289c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 729beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 7304d6f44ffSToby Isaac /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 7319c3cf19fSMatthew 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); 73240e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 7339ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 7349ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 73540e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 73640e14135SMatthew G. Knepley PetscScalar *x = NULL; 73740e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 7389c3cf19fSMatthew G. Knepley PetscInt qc = 0; 739652b88e8SMatthew G. Knepley 7407318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr); 74140e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 74240e14135SMatthew G. Knepley 7439c3cf19fSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 7440f2d7e86SMatthew G. Knepley PetscFE fe; 74551259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 74640e14135SMatthew G. Knepley PetscReal *basisDer; 7479c3cf19fSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 74840e14135SMatthew G. Knepley 7490f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7500f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 7519c3cf19fSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 7520f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 75340e14135SMatthew G. Knepley if (debug) { 75440e14135SMatthew G. Knepley char title[1024]; 75540e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 7569c3cf19fSMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 757652b88e8SMatthew G. Knepley } 7589c3cf19fSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 7597318780aSToby 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); 7607318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx); 761e735a8a9SMatthew G. Knepley if (ierr) { 762e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 763e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 764e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 7659c3cf19fSMatthew G. Knepley ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2); 766e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 767e735a8a9SMatthew G. Knepley } 7689c3cf19fSMatthew G. Knepley ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr); 7699c3cf19fSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 770beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 771beaa55a6SMatthew 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);} 772beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 77340e14135SMatthew G. Knepley } 77440e14135SMatthew G. Knepley } 7759c3cf19fSMatthew G. Knepley fieldOffset += Nb; 7769c3cf19fSMatthew G. Knepley qc += Nc; 77740e14135SMatthew G. Knepley } 77840e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 77940e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 78040e14135SMatthew G. Knepley localDiff += elemDiff; 78140e14135SMatthew G. Knepley } 7829c3cf19fSMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr); 78340e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 784b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 78540e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 786cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 787cb1e1211SMatthew G Knepley } 788cb1e1211SMatthew G Knepley 789c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 79073d901b8SMatthew G. Knepley { 79173d901b8SMatthew G. Knepley const PetscInt debug = 0; 79273d901b8SMatthew G. Knepley PetscSection section; 79373d901b8SMatthew G. Knepley PetscQuadrature quad; 79473d901b8SMatthew G. Knepley Vec localX; 79515496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 7967318780aSToby Isaac PetscReal *coords, *detJ, *J; 79773d901b8SMatthew G. Knepley PetscReal *localDiff; 79815496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7999c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset; 80073d901b8SMatthew G. Knepley PetscErrorCode ierr; 80173d901b8SMatthew G. Knepley 80273d901b8SMatthew G. Knepley PetscFunctionBegin; 803c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 8047318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 80573d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 80673d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 80773d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 808bdd6f66aSToby Isaac ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 80973d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 81073d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 81173d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 81215496722SMatthew G. Knepley PetscObject obj; 81315496722SMatthew G. Knepley PetscClassId id; 81473d901b8SMatthew G. Knepley PetscInt Nc; 81573d901b8SMatthew G. Knepley 81615496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 81715496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 81815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 81915496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 82015496722SMatthew G. Knepley 8210f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8220f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 82315496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 82415496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 82515496722SMatthew G. Knepley 82615496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 82715496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 82815496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 82973d901b8SMatthew G. Knepley numComponents += Nc; 83073d901b8SMatthew G. Knepley } 8319c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 832beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 8337318780aSToby Isaac ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 83473d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 8359ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 8369ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 83773d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 83873d901b8SMatthew G. Knepley PetscScalar *x = NULL; 8399c3cf19fSMatthew G. Knepley PetscInt qc = 0; 84073d901b8SMatthew G. Knepley 8417318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 84273d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 84373d901b8SMatthew G. Knepley 84415496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 84515496722SMatthew G. Knepley PetscObject obj; 84615496722SMatthew G. Knepley PetscClassId id; 84773d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 84815496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 84973d901b8SMatthew G. Knepley 85015496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 85115496722SMatthew G. Knepley 85215496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 85315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 85415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 85515496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 85615496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 85773d901b8SMatthew G. Knepley if (debug) { 85873d901b8SMatthew G. Knepley char title[1024]; 85973d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 86015496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 86173d901b8SMatthew G. Knepley } 8627318780aSToby Isaac for (q = 0; q < Nq; ++q) { 8637318780aSToby 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); 8647318780aSToby Isaac ierr = (*funcs[field])(coordDim, time, &coords[coordDim*q], numFields, funcVal, ctx); 865e735a8a9SMatthew G. Knepley if (ierr) { 866e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 867e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 868e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 8697318780aSToby Isaac ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 870e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 871e735a8a9SMatthew G. Knepley } 87215496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 87315496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 87415496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 87515496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 876beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 877beaa55a6SMatthew 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);} 878beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 87973d901b8SMatthew G. Knepley } 88073d901b8SMatthew G. Knepley } 881beaa55a6SMatthew G. Knepley fieldOffset += Nb; 8829c3cf19fSMatthew G. Knepley qc += Nc; 88373d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 88473d901b8SMatthew G. Knepley } 88573d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 88673d901b8SMatthew G. Knepley } 88773d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 888b2566f29SBarry Smith ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 88973d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 8907318780aSToby Isaac ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 89173d901b8SMatthew G. Knepley PetscFunctionReturn(0); 89273d901b8SMatthew G. Knepley } 89373d901b8SMatthew G. Knepley 894e729f68cSMatthew G. Knepley /*@C 895e729f68cSMatthew 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. 896e729f68cSMatthew G. Knepley 897e729f68cSMatthew G. Knepley Input Parameters: 898e729f68cSMatthew G. Knepley + dm - The DM 8990163fd50SMatthew G. Knepley . time - The time 900ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation 901e729f68cSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 902e729f68cSMatthew G. Knepley - X - The coefficient vector u_h 903e729f68cSMatthew G. Knepley 904e729f68cSMatthew G. Knepley Output Parameter: 905e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell 906e729f68cSMatthew G. Knepley 907e729f68cSMatthew G. Knepley Level: developer 908e729f68cSMatthew G. Knepley 909b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 910e729f68cSMatthew G. Knepley @*/ 9110163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D) 912e729f68cSMatthew G. Knepley { 913e729f68cSMatthew G. Knepley PetscSection section; 914e729f68cSMatthew G. Knepley PetscQuadrature quad; 915e729f68cSMatthew G. Knepley Vec localX; 916e729f68cSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 9177318780aSToby Isaac PetscReal *coords, *detJ, *J; 918e729f68cSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 9199c3cf19fSMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset; 920e729f68cSMatthew G. Knepley PetscErrorCode ierr; 921e729f68cSMatthew G. Knepley 922e729f68cSMatthew G. Knepley PetscFunctionBegin; 923e729f68cSMatthew G. Knepley ierr = VecSet(D, 0.0);CHKERRQ(ierr); 924e729f68cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 9257318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 926e729f68cSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 927e729f68cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 928e729f68cSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 929bdd6f66aSToby Isaac ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 930e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 931e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 932e729f68cSMatthew G. Knepley for (field = 0; field < numFields; ++field) { 933e729f68cSMatthew G. Knepley PetscObject obj; 934e729f68cSMatthew G. Knepley PetscClassId id; 935e729f68cSMatthew G. Knepley PetscInt Nc; 936e729f68cSMatthew G. Knepley 937e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 938e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 939e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 940e729f68cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 941e729f68cSMatthew G. Knepley 942e729f68cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 943e729f68cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 944e729f68cSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 945e729f68cSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 946e729f68cSMatthew G. Knepley 947e729f68cSMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 948e729f68cSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 949e729f68cSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 950e729f68cSMatthew G. Knepley numComponents += Nc; 951e729f68cSMatthew G. Knepley } 9529c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 953beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 9547318780aSToby Isaac ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr); 955e729f68cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 956e729f68cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 957e729f68cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 958e729f68cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 959e729f68cSMatthew G. Knepley PetscScalar *x = NULL; 9606f288a59SMatthew G. Knepley PetscScalar elemDiff = 0.0; 9619c3cf19fSMatthew G. Knepley PetscInt qc = 0; 962e729f68cSMatthew G. Knepley 9637318780aSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr); 964e729f68cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 965e729f68cSMatthew G. Knepley 966e729f68cSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 967e729f68cSMatthew G. Knepley PetscObject obj; 968e729f68cSMatthew G. Knepley PetscClassId id; 969e729f68cSMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 970e729f68cSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 971e729f68cSMatthew G. Knepley 972e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 973e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 974e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 975e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 976e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 97723f34ed2SToby Isaac if (funcs[field]) { 9787318780aSToby Isaac for (q = 0; q < Nq; ++q) { 9797f79407eSBarry 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); 9807318780aSToby Isaac ierr = (*funcs[field])(dim, time, &coords[q*coordDim], Nc, funcVal, ctx); 981e735a8a9SMatthew G. Knepley if (ierr) { 982e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 983e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 9847f79407eSBarry Smith ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2); 985e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 986e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 987e735a8a9SMatthew G. Knepley } 988e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 989e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 990e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 991e729f68cSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 992beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 993beaa55a6SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]; 994e729f68cSMatthew G. Knepley } 995e729f68cSMatthew G. Knepley } 99623f34ed2SToby Isaac } 997beaa55a6SMatthew G. Knepley fieldOffset += Nb; 9989c3cf19fSMatthew G. Knepley qc += Nc; 999e729f68cSMatthew G. Knepley } 1000e729f68cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 100123f34ed2SToby Isaac ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr); 1002e729f68cSMatthew G. Knepley } 10037318780aSToby Isaac ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr); 1004e729f68cSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1005e729f68cSMatthew G. Knepley ierr = VecSqrtAbs(D);CHKERRQ(ierr); 1006e729f68cSMatthew G. Knepley PetscFunctionReturn(0); 1007e729f68cSMatthew G. Knepley } 1008e729f68cSMatthew G. Knepley 10091555c271SMatthew G. Knepley /*@C 10101555c271SMatthew 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. 10111555c271SMatthew G. Knepley 10121555c271SMatthew G. Knepley Input Parameters: 10131555c271SMatthew G. Knepley + dm - The DM 10141555c271SMatthew G. Knepley - LocX - The coefficient vector u_h 10151555c271SMatthew G. Knepley 10161555c271SMatthew G. Knepley Output Parameter: 10171555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient 10181555c271SMatthew G. Knepley 10191555c271SMatthew G. Knepley Notes: Add citation to (Clement, 1975) and definition of the interpolant 10201555c271SMatthew 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 10211555c271SMatthew G. Knepley 10221555c271SMatthew G. Knepley Level: developer 10231555c271SMatthew G. Knepley 10241555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 10251555c271SMatthew G. Knepley @*/ 10261555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC) 10271555c271SMatthew G. Knepley { 1028db1066baSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1029db1066baSMatthew G. Knepley PetscInt debug = mesh->printFEM; 10301555c271SMatthew G. Knepley DM dmC; 10311555c271SMatthew G. Knepley PetscSection section; 10321555c271SMatthew G. Knepley PetscQuadrature quad; 10331555c271SMatthew G. Knepley PetscScalar *interpolant, *gradsum; 10341555c271SMatthew G. Knepley PetscReal *coords, *detJ, *J, *invJ; 10351555c271SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 10361555c271SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset; 10371555c271SMatthew G. Knepley PetscErrorCode ierr; 10381555c271SMatthew G. Knepley 10391555c271SMatthew G. Knepley PetscFunctionBegin; 10401555c271SMatthew G. Knepley ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr); 10411555c271SMatthew G. Knepley ierr = VecSet(locC, 0.0);CHKERRQ(ierr); 10421555c271SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 10431555c271SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 10441555c271SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 10451555c271SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 10461555c271SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 10471555c271SMatthew G. Knepley PetscObject obj; 10481555c271SMatthew G. Knepley PetscClassId id; 10491555c271SMatthew G. Knepley PetscInt Nc; 10501555c271SMatthew G. Knepley 10511555c271SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 10521555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 10531555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 10541555c271SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 10551555c271SMatthew G. Knepley 10561555c271SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 10571555c271SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 10581555c271SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 10591555c271SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 10601555c271SMatthew G. Knepley 10611555c271SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 10621555c271SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 10631555c271SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 10641555c271SMatthew G. Knepley numComponents += Nc; 10651555c271SMatthew G. Knepley } 10661555c271SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 10671555c271SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 10681555c271SMatthew 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); 10691555c271SMatthew G. Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 10701555c271SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 10711555c271SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 10721555c271SMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 10731555c271SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 10741555c271SMatthew G. Knepley PetscScalar volsum = 0.0; 10751555c271SMatthew G. Knepley PetscInt *star = NULL; 10761555c271SMatthew G. Knepley PetscInt starSize, st, d, fc; 10771555c271SMatthew G. Knepley 10781555c271SMatthew G. Knepley ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr); 10791555c271SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 10801555c271SMatthew G. Knepley for (st = 0; st < starSize*2; st += 2) { 10811555c271SMatthew G. Knepley const PetscInt cell = star[st]; 10821555c271SMatthew G. Knepley PetscScalar *grad = &gradsum[coordDim*numComponents]; 10831555c271SMatthew G. Knepley PetscScalar *x = NULL; 10841555c271SMatthew G. Knepley PetscReal vol = 0.0; 10851555c271SMatthew G. Knepley 10861555c271SMatthew G. Knepley if ((cell < cStart) || (cell >= cEnd)) continue; 10871555c271SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr); 10881555c271SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 10891555c271SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 10901555c271SMatthew G. Knepley PetscObject obj; 10911555c271SMatthew G. Knepley PetscClassId id; 10921555c271SMatthew G. Knepley PetscInt Nb, Nc, q, qc = 0; 10931555c271SMatthew G. Knepley 10941555c271SMatthew G. Knepley ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr); 10951555c271SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 10961555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 10971555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 10981555c271SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 10991555c271SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 11001555c271SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 11011555c271SMatthew 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); 11021555c271SMatthew G. Knepley if (ierr) { 11031555c271SMatthew G. Knepley PetscErrorCode ierr2; 11041555c271SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2); 11051555c271SMatthew G. Knepley ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2); 11061555c271SMatthew G. Knepley ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2); 11071555c271SMatthew G. Knepley CHKERRQ(ierr); 11081555c271SMatthew G. Knepley } 11091555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);} 11101555c271SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 11111555c271SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 11121555c271SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+qc+fc]; 11131555c271SMatthew G. Knepley 11141555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q]; 11151555c271SMatthew G. Knepley } 11161555c271SMatthew G. Knepley vol += quadWeights[q*qNc]*detJ[q]; 11171555c271SMatthew G. Knepley } 11181555c271SMatthew G. Knepley fieldOffset += Nb; 11191555c271SMatthew G. Knepley qc += Nc; 11201555c271SMatthew G. Knepley } 11211555c271SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 1122f8527842SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 1123f8527842SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 1124f8527842SMatthew G. Knepley gradsum[fc*coordDim+d] += grad[fc*coordDim+d]; 1125f8527842SMatthew G. Knepley } 1126f8527842SMatthew G. Knepley } 1127f8527842SMatthew G. Knepley volsum += vol; 1128db1066baSMatthew G. Knepley if (debug) { 11296d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr); 11301555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 11311555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 11321555c271SMatthew G. Knepley if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);} 11336d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr); 11341555c271SMatthew G. Knepley } 11351555c271SMatthew G. Knepley } 11361555c271SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr); 1137db1066baSMatthew G. Knepley } 11381555c271SMatthew G. Knepley } 11391555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 11401555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum; 11411555c271SMatthew G. Knepley } 11421555c271SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 11431555c271SMatthew G. Knepley ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr); 11441555c271SMatthew G. Knepley } 11451555c271SMatthew G. Knepley ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr); 11461555c271SMatthew G. Knepley PetscFunctionReturn(0); 11471555c271SMatthew G. Knepley } 11481555c271SMatthew G. Knepley 1149338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user) 115073d901b8SMatthew G. Knepley { 1151338f77d5SMatthew G. Knepley DM dmAux = NULL; 115261aaff12SToby Isaac PetscDS prob, probAux = NULL; 115373d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1154338f77d5SMatthew G. Knepley Vec locX, locA; 1155c330f8ffSToby Isaac PetscInt dim, numCells = cEnd - cStart, c, f; 1156c330f8ffSToby Isaac PetscBool useFVM = PETSC_FALSE; 1157338f77d5SMatthew G. Knepley /* DS */ 1158338f77d5SMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x, numConstants; 1159338f77d5SMatthew G. Knepley PetscInt NfAux, totDimAux, *aOff; 1160338f77d5SMatthew G. Knepley PetscScalar *u, *a; 1161338f77d5SMatthew G. Knepley const PetscScalar *constants; 1162338f77d5SMatthew G. Knepley /* Geometry */ 1163c330f8ffSToby Isaac PetscFEGeom *cgeomFEM; 1164338f77d5SMatthew G. Knepley DM dmGrad; 1165c330f8ffSToby Isaac PetscQuadrature affineQuad = NULL; 1166338f77d5SMatthew G. Knepley Vec cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL; 1167b5a3613cSMatthew G. Knepley PetscFVCellGeom *cgeomFVM; 1168338f77d5SMatthew G. Knepley const PetscScalar *lgrad; 1169c330f8ffSToby Isaac PetscBool isAffine; 1170c330f8ffSToby Isaac DMField coordField; 1171c330f8ffSToby Isaac IS cellIS; 117273d901b8SMatthew G. Knepley PetscErrorCode ierr; 117373d901b8SMatthew G. Knepley 117473d901b8SMatthew G. Knepley PetscFunctionBegin; 1175338f77d5SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1176c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 117773d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 117873d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1179338f77d5SMatthew G. Knepley /* Determine which discretizations we have */ 1180b5a3613cSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1181b5a3613cSMatthew G. Knepley PetscObject obj; 1182b5a3613cSMatthew G. Knepley PetscClassId id; 1183b5a3613cSMatthew G. Knepley 1184b5a3613cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1185b5a3613cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1186338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE; 1187338f77d5SMatthew G. Knepley } 1188338f77d5SMatthew G. Knepley /* Get local solution with boundary values */ 1189338f77d5SMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 1190338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1191338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1192338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1193338f77d5SMatthew G. Knepley /* Read DS information */ 1194338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1195338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 1196338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 1197c330f8ffSToby Isaac ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr); 1198338f77d5SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 1199338f77d5SMatthew G. Knepley /* Read Auxiliary DS information */ 1200338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1201338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1202338f77d5SMatthew G. Knepley if (dmAux) { 1203338f77d5SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1204338f77d5SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 1205338f77d5SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1206338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1207338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 1208338f77d5SMatthew G. Knepley } 1209338f77d5SMatthew G. Knepley /* Allocate data arrays */ 1210338f77d5SMatthew G. Knepley ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr); 1211338f77d5SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1212338f77d5SMatthew G. Knepley /* Read out geometry */ 1213c330f8ffSToby Isaac ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr); 1214c330f8ffSToby Isaac ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr); 1215c330f8ffSToby Isaac if (isAffine) { 1216c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1217c330f8ffSToby Isaac if (affineQuad) { 1218c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 1219338f77d5SMatthew G. Knepley } 1220b5a3613cSMatthew G. Knepley } 1221b5a3613cSMatthew G. Knepley if (useFVM) { 1222338f77d5SMatthew G. Knepley PetscFV fv = NULL; 1223b5a3613cSMatthew G. Knepley Vec grad; 1224b5a3613cSMatthew G. Knepley PetscInt fStart, fEnd; 1225b5a3613cSMatthew G. Knepley PetscBool compGrad; 1226b5a3613cSMatthew G. Knepley 1227338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1228338f77d5SMatthew G. Knepley PetscObject obj; 1229338f77d5SMatthew G. Knepley PetscClassId id; 1230338f77d5SMatthew G. Knepley 1231338f77d5SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1232338f77d5SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1233338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;} 1234338f77d5SMatthew G. Knepley } 1235338f77d5SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr); 1236338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr); 1237b5a3613cSMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr); 1238338f77d5SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr); 1239338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr); 1240b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1241b5a3613cSMatthew G. Knepley /* Reconstruct and limit cell gradients */ 1242b5a3613cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1243b5a3613cSMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1244338f77d5SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1245b5a3613cSMatthew G. Knepley /* Communicate gradient values */ 1246b5a3613cSMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1247b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1248b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1249b5a3613cSMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1250b5a3613cSMatthew G. Knepley /* Handle non-essential (e.g. outflow) boundary values */ 1251338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 1252b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1253b5a3613cSMatthew G. Knepley } 1254338f77d5SMatthew G. Knepley /* Read out data from inputs */ 125573d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 125673d901b8SMatthew G. Knepley PetscScalar *x = NULL; 125773d901b8SMatthew G. Knepley PetscInt i; 125873d901b8SMatthew G. Knepley 1259338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 12600f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 1261338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 126273d901b8SMatthew G. Knepley if (dmAux) { 1263338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 12640f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 1265338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 126673d901b8SMatthew G. Knepley } 126773d901b8SMatthew G. Knepley } 1268338f77d5SMatthew G. Knepley /* Do integration for each field */ 126973d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1270c1f031eeSMatthew G. Knepley PetscObject obj; 1271c1f031eeSMatthew G. Knepley PetscClassId id; 1272c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 127373d901b8SMatthew G. Knepley 1274c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1275c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1276c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1277c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1278c1f031eeSMatthew G. Knepley PetscQuadrature q; 1279c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 1280c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1281c1f031eeSMatthew G. Knepley 12820f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1283c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 12849c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1285c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1286c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 128773d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 12880f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 128973d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 129073d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 129173d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 129273d901b8SMatthew G. Knepley offset = numCells - Nr; 1293c330f8ffSToby Isaac if (!affineQuad) { 1294c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 1295c330f8ffSToby Isaac } 1296c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 1297c330f8ffSToby Isaac ierr = PetscFEIntegrate(fe, prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr); 1298c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1299c330f8ffSToby Isaac ierr = PetscFEIntegrate(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr); 1300c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 1301c330f8ffSToby Isaac if (!affineQuad) { 1302c330f8ffSToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 1303c330f8ffSToby Isaac } 1304c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1305c1f031eeSMatthew G. Knepley PetscInt foff; 1306420e96edSMatthew G. Knepley PetscPointFunc obj_func; 1307b69edc29SMatthew G. Knepley PetscScalar lint; 1308c1f031eeSMatthew G. Knepley 1309c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 1310c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1311c1f031eeSMatthew G. Knepley if (obj_func) { 1312c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 1313b5a3613cSMatthew G. Knepley PetscScalar *u_x; 1314b5a3613cSMatthew G. Knepley 1315b5a3613cSMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr); 1316338f77d5SMatthew 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); 1317338f77d5SMatthew G. Knepley cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume; 131873d901b8SMatthew G. Knepley } 1319c1f031eeSMatthew G. Knepley } 1320c1f031eeSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1321c1f031eeSMatthew G. Knepley } 1322338f77d5SMatthew G. Knepley /* Cleanup data arrays */ 1323b5a3613cSMatthew G. Knepley if (useFVM) { 1324b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1325b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1326b5a3613cSMatthew G. Knepley ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1327b5a3613cSMatthew G. Knepley ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr); 1328b5a3613cSMatthew G. Knepley ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr); 1329b5a3613cSMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 1330b5a3613cSMatthew G. Knepley } 133173d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 1332338f77d5SMatthew G. Knepley ierr = PetscFree(u);CHKERRQ(ierr); 1333338f77d5SMatthew G. Knepley /* Cleanup */ 1334f99c8401SToby Isaac if (affineQuad) { 1335f99c8401SToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 1336f99c8401SToby Isaac } 1337f99c8401SToby Isaac ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 1338c330f8ffSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 1339338f77d5SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 1340338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 1341338f77d5SMatthew G. Knepley } 1342338f77d5SMatthew G. Knepley 1343338f77d5SMatthew G. Knepley /*@ 1344338f77d5SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user 1345338f77d5SMatthew G. Knepley 1346338f77d5SMatthew G. Knepley Input Parameters: 1347338f77d5SMatthew G. Knepley + dm - The mesh 1348338f77d5SMatthew G. Knepley . X - Global input vector 1349338f77d5SMatthew G. Knepley - user - The user context 1350338f77d5SMatthew G. Knepley 1351338f77d5SMatthew G. Knepley Output Parameter: 1352338f77d5SMatthew G. Knepley . integral - Integral for each field 1353338f77d5SMatthew G. Knepley 1354338f77d5SMatthew G. Knepley Level: developer 1355338f77d5SMatthew G. Knepley 1356338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 1357338f77d5SMatthew G. Knepley @*/ 1358b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user) 1359338f77d5SMatthew G. Knepley { 1360338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1361b8feb594SMatthew G. Knepley PetscScalar *cintegral, *lintegral; 1362338f77d5SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell; 1363338f77d5SMatthew G. Knepley PetscErrorCode ierr; 1364338f77d5SMatthew G. Knepley 1365338f77d5SMatthew G. Knepley PetscFunctionBegin; 1366338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1367338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 1368338f77d5SMatthew G. Knepley PetscValidPointer(integral, 3); 1369338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1370338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 1371338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 1372338f77d5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 1373338f77d5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr); 1374338f77d5SMatthew G. Knepley cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight]; 1375338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 1376338f77d5SMatthew G. Knepley ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 1377338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 1378338f77d5SMatthew G. Knepley /* Sum up values */ 1379338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 1380338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 1381338f77d5SMatthew G. Knepley 1382338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 1383b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f]; 1384338f77d5SMatthew G. Knepley } 1385b8feb594SMatthew G. Knepley ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 138673d901b8SMatthew G. Knepley if (mesh->printFEM) { 1387338f77d5SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr); 1388b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);} 138973d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 139073d901b8SMatthew G. Knepley } 1391338f77d5SMatthew G. Knepley ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr); 1392338f77d5SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1393338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 1394338f77d5SMatthew G. Knepley } 1395338f77d5SMatthew G. Knepley 1396338f77d5SMatthew G. Knepley /*@ 1397338f77d5SMatthew G. Knepley DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user 1398338f77d5SMatthew G. Knepley 1399338f77d5SMatthew G. Knepley Input Parameters: 1400338f77d5SMatthew G. Knepley + dm - The mesh 1401338f77d5SMatthew G. Knepley . X - Global input vector 1402338f77d5SMatthew G. Knepley - user - The user context 1403338f77d5SMatthew G. Knepley 1404338f77d5SMatthew G. Knepley Output Parameter: 1405338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field 1406338f77d5SMatthew G. Knepley 1407338f77d5SMatthew G. Knepley Level: developer 1408338f77d5SMatthew G. Knepley 1409338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 1410338f77d5SMatthew G. Knepley @*/ 1411338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user) 1412338f77d5SMatthew G. Knepley { 1413338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1414338f77d5SMatthew G. Knepley DM dmF; 1415338f77d5SMatthew G. Knepley PetscSection sectionF; 1416338f77d5SMatthew G. Knepley PetscScalar *cintegral, *af; 1417338f77d5SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell; 1418338f77d5SMatthew G. Knepley PetscErrorCode ierr; 1419338f77d5SMatthew G. Knepley 1420338f77d5SMatthew G. Knepley PetscFunctionBegin; 1421338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1422338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 1423338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 1424338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 1425338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 1426338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 1427338f77d5SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 1428338f77d5SMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr); 1429338f77d5SMatthew G. Knepley cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight]; 1430338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 1431338f77d5SMatthew G. Knepley ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 1432338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 1433338f77d5SMatthew G. Knepley /* Put values in F*/ 1434338f77d5SMatthew G. Knepley ierr = VecGetDM(F, &dmF);CHKERRQ(ierr); 1435338f77d5SMatthew G. Knepley ierr = DMGetDefaultSection(dmF, §ionF);CHKERRQ(ierr); 1436338f77d5SMatthew G. Knepley ierr = VecGetArray(F, &af);CHKERRQ(ierr); 1437338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 1438338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 1439338f77d5SMatthew G. Knepley PetscInt dof, off; 1440338f77d5SMatthew G. Knepley 1441338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 1442338f77d5SMatthew G. Knepley ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr); 1443338f77d5SMatthew G. Knepley ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr); 1444338f77d5SMatthew G. Knepley if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf); 1445338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f]; 1446338f77d5SMatthew G. Knepley } 1447338f77d5SMatthew G. Knepley ierr = VecRestoreArray(F, &af);CHKERRQ(ierr); 1448338f77d5SMatthew G. Knepley ierr = PetscFree(cintegral);CHKERRQ(ierr); 1449c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 145073d901b8SMatthew G. Knepley PetscFunctionReturn(0); 145173d901b8SMatthew G. Knepley } 145273d901b8SMatthew G. Knepley 1453*64c72086SMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[], 1454*64c72086SMatthew G. Knepley void (*obj_func)(PetscInt, PetscInt, PetscInt, 1455*64c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1456*64c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1457*64c72086SMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), 1458*64c72086SMatthew G. Knepley PetscScalar *fintegral, void *user) 1459*64c72086SMatthew G. Knepley { 1460*64c72086SMatthew G. Knepley DM dmAux = NULL; 1461*64c72086SMatthew G. Knepley PetscDS prob, probAux; 1462*64c72086SMatthew G. Knepley PetscSection section, sectionAux; 1463*64c72086SMatthew G. Knepley Vec locX, locA; 1464*64c72086SMatthew G. Knepley PetscInt dim, dimEmbed, field, f, v; 1465*64c72086SMatthew G. Knepley PetscBool useFEM = PETSC_TRUE; 1466*64c72086SMatthew G. Knepley /* DS */ 1467*64c72086SMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x, numConstants; 1468*64c72086SMatthew G. Knepley PetscInt NfAux, totDimAux, *aOff; 1469*64c72086SMatthew G. Knepley PetscScalar *u, *a; 1470*64c72086SMatthew G. Knepley const PetscScalar *constants; 1471*64c72086SMatthew G. Knepley /* Geometry */ 1472*64c72086SMatthew G. Knepley PetscFEFaceGeom *fgeom; 1473*64c72086SMatthew G. Knepley PetscErrorCode ierr; 1474*64c72086SMatthew G. Knepley 1475*64c72086SMatthew G. Knepley PetscFunctionBegin; 1476*64c72086SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1477*64c72086SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1478*64c72086SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 1479*64c72086SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1480*64c72086SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1481*64c72086SMatthew G. Knepley /* Determine which discretizations we have */ 1482*64c72086SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 1483*64c72086SMatthew G. Knepley PetscObject obj; 1484*64c72086SMatthew G. Knepley PetscClassId id; 1485*64c72086SMatthew G. Knepley 1486*64c72086SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 1487*64c72086SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1488*64c72086SMatthew G. Knepley if (id == PETSCFV_CLASSID) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM"); 1489*64c72086SMatthew G. Knepley } 1490*64c72086SMatthew G. Knepley /* Get local solution with boundary values */ 1491*64c72086SMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 1492*64c72086SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1493*64c72086SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1494*64c72086SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1495*64c72086SMatthew G. Knepley /* Read DS information */ 1496*64c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1497*64c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 1498*64c72086SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 1499*64c72086SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 1500*64c72086SMatthew G. Knepley /* Read Auxiliary DS information */ 1501*64c72086SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1502*64c72086SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1503*64c72086SMatthew G. Knepley if (dmAux) { 1504*64c72086SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1505*64c72086SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 1506*64c72086SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1507*64c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1508*64c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 1509*64c72086SMatthew G. Knepley } 1510*64c72086SMatthew G. Knepley if (useFEM) { 1511*64c72086SMatthew G. Knepley for (v = 0; v < numVals; ++v) { 1512*64c72086SMatthew G. Knepley IS faceIS; 1513*64c72086SMatthew G. Knepley const PetscInt *faces; 1514*64c72086SMatthew G. Knepley PetscInt numFaces; 1515*64c72086SMatthew G. Knepley 1516*64c72086SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, vals[v], &faceIS);CHKERRQ(ierr); 1517*64c72086SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 1518*64c72086SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 1519*64c72086SMatthew G. Knepley /* Allocate data arrays */ 1520*64c72086SMatthew G. Knepley ierr = PetscCalloc1(numFaces*totDim, &u);CHKERRQ(ierr); 1521*64c72086SMatthew G. Knepley if (useFEM) {ierr = PetscMalloc1(numFaces, &fgeom);CHKERRQ(ierr);} 1522*64c72086SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numFaces*totDimAux, &a);CHKERRQ(ierr);} 1523*64c72086SMatthew G. Knepley /* Read out geometry */ 1524*64c72086SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1525*64c72086SMatthew G. Knepley const PetscInt *support, *cone; 1526*64c72086SMatthew G. Knepley const PetscInt face = faces[f]; 1527*64c72086SMatthew G. Knepley PetscInt coneSize, faceLoc; 1528*64c72086SMatthew G. Knepley PetscReal dummyJ[9], dummyDetJ; 1529*64c72086SMatthew G. Knepley 1530*64c72086SMatthew G. Knepley fgeom[f].dim = dim-1; 1531*64c72086SMatthew G. Knepley fgeom[f].dimEmbed = dimEmbed; 1532*64c72086SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr); 1533*64c72086SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, support[0], NULL, NULL, dummyJ, fgeom[f].invJ[0], &dummyDetJ);CHKERRQ(ierr); 1534*64c72086SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, face, NULL, fgeom[f].v0, fgeom[f].J, NULL, &fgeom[f].detJ);CHKERRQ(ierr); 1535*64c72086SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, face, NULL, NULL, fgeom[f].n);CHKERRQ(ierr); 1536*64c72086SMatthew G. Knepley if (fgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", (double) fgeom[f].detJ, face); 1537*64c72086SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 1538*64c72086SMatthew G. Knepley ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 1539*64c72086SMatthew G. Knepley for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == face) break; 1540*64c72086SMatthew 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]); 1541*64c72086SMatthew G. Knepley fgeom[f].face[0] = faceLoc; 1542*64c72086SMatthew G. Knepley } 1543*64c72086SMatthew G. Knepley /* Read out data from inputs */ 1544*64c72086SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1545*64c72086SMatthew G. Knepley const PetscInt *support; 1546*64c72086SMatthew G. Knepley const PetscInt face = faces[f]; 1547*64c72086SMatthew G. Knepley PetscScalar *x = NULL; 1548*64c72086SMatthew G. Knepley PetscInt i; 1549*64c72086SMatthew G. Knepley 1550*64c72086SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr); 1551*64c72086SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1552*64c72086SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[f*totDim+i] = x[i]; 1553*64c72086SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 1554*64c72086SMatthew G. Knepley if (dmAux) { 1555*64c72086SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr); 1556*64c72086SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i]; 1557*64c72086SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr); 1558*64c72086SMatthew G. Knepley } 1559*64c72086SMatthew G. Knepley } 1560*64c72086SMatthew G. Knepley /* Do integration for each field */ 1561*64c72086SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1562*64c72086SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 1563*64c72086SMatthew G. Knepley PetscFE fe; 1564*64c72086SMatthew G. Knepley PetscQuadrature q; 1565*64c72086SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 1566*64c72086SMatthew G. Knepley PetscInt Nq, Nb; 1567*64c72086SMatthew G. Knepley 1568*64c72086SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 1569*64c72086SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1570*64c72086SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1571*64c72086SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1572*64c72086SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1573*64c72086SMatthew G. Knepley blockSize = Nb*Nq; 1574*64c72086SMatthew G. Knepley batchSize = numBlocks * blockSize; 1575*64c72086SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1576*64c72086SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 1577*64c72086SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1578*64c72086SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 1579*64c72086SMatthew G. Knepley offset = numFaces - Nr; 1580*64c72086SMatthew G. Knepley ierr = PetscFEIntegrateBd(fe, prob, field, obj_func, Ne, fgeom, u, probAux, a, fintegral);CHKERRQ(ierr); 1581*64c72086SMatthew G. Knepley ierr = PetscFEIntegrateBd(fe, prob, field, obj_func, Nr, &fgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], &fintegral[offset*Nf]);CHKERRQ(ierr); 1582*64c72086SMatthew G. Knepley } 1583*64c72086SMatthew G. Knepley } 1584*64c72086SMatthew G. Knepley /* Cleanup data arrays */ 1585*64c72086SMatthew G. Knepley ierr = PetscFree(u);CHKERRQ(ierr); 1586*64c72086SMatthew G. Knepley if (useFEM) {ierr = PetscFree(fgeom);CHKERRQ(ierr);} 1587*64c72086SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 1588*64c72086SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 1589*64c72086SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 1590*64c72086SMatthew G. Knepley } 1591*64c72086SMatthew G. Knepley } 1592*64c72086SMatthew G. Knepley /* Cleanup */ 1593*64c72086SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 1594*64c72086SMatthew G. Knepley PetscFunctionReturn(0); 1595*64c72086SMatthew G. Knepley } 1596*64c72086SMatthew G. Knepley 1597d69c5d34SMatthew G. Knepley /*@ 159868132eb9SMatthew G. Knepley DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1599d69c5d34SMatthew G. Knepley 1600d69c5d34SMatthew G. Knepley Input Parameters: 1601d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1602d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1603d69c5d34SMatthew G. Knepley - user - The user context 1604d69c5d34SMatthew G. Knepley 1605d69c5d34SMatthew G. Knepley Output Parameter: 1606934789fcSMatthew G. Knepley . In - The interpolation matrix 1607d69c5d34SMatthew G. Knepley 1608d69c5d34SMatthew G. Knepley Level: developer 1609d69c5d34SMatthew G. Knepley 161068132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 1611d69c5d34SMatthew G. Knepley @*/ 161268132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user) 1613d69c5d34SMatthew G. Knepley { 1614d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1615d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 16162764a2aaSMatthew G. Knepley PetscDS prob; 1617d69c5d34SMatthew G. Knepley PetscFE *feRef; 161897c42addSMatthew G. Knepley PetscFV *fvRef; 1619d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1620d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1621d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 16229ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c; 16230f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1624d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1625d69c5d34SMatthew G. Knepley 1626d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1627d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1628c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1629d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1630d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1631d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1632d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1633d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1634d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 16359ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 16369ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 16372764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 163897c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 1639d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 164097c42addSMatthew G. Knepley PetscObject obj; 164197c42addSMatthew G. Knepley PetscClassId id; 1642aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 1643d69c5d34SMatthew G. Knepley 164497c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 164597c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 164697c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 164797c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 164897c42addSMatthew G. Knepley 16490f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1650d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 16510f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 165297c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 165397c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 165497c42addSMatthew G. Knepley PetscDualSpace Q; 165597c42addSMatthew G. Knepley 165697c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 165797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 165897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 165997c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 166097c42addSMatthew G. Knepley } 16619c3cf19fSMatthew G. Knepley rTotDim += rNb; 1662d69c5d34SMatthew G. Knepley } 16632764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 16640f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 16650f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1666d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1667d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1668d69c5d34SMatthew G. Knepley PetscQuadrature f; 1669d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1670d69c5d34SMatthew G. Knepley PetscReal *points; 1671d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1672d69c5d34SMatthew G. Knepley 1673d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 167497c42addSMatthew G. Knepley if (feRef[fieldI]) { 1675d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 16760f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 167797c42addSMatthew G. Knepley } else { 167897c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 167997c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 168097c42addSMatthew G. Knepley } 1681d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1682d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1683d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 16849c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1685d69c5d34SMatthew G. Knepley npoints += Np; 1686d69c5d34SMatthew G. Knepley } 1687d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1688d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1689d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 16909c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1691d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1692d69c5d34SMatthew G. Knepley } 1693d69c5d34SMatthew G. Knepley 1694d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 169597c42addSMatthew G. Knepley PetscObject obj; 169697c42addSMatthew G. Knepley PetscClassId id; 1697d69c5d34SMatthew G. Knepley PetscReal *B; 16989c3cf19fSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j, qNc; 1699d69c5d34SMatthew G. Knepley 170097c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr); 170197c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 170297c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 170397c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1704d69c5d34SMatthew G. Knepley 1705d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 17060f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 17070f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1708ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1709ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 17109c3cf19fSMatthew 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); 17110f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1712d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1713d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 17149c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 17159c3cf19fSMatthew 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); 1716d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 171736a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 1718d172c84bSMatthew G. Knepley /* 1719d172c84bSMatthew G. Knepley cTotDim: Total columns in element interpolation matrix, sum of number of dual basis functionals in each field 1720d172c84bSMatthew G. Knepley offsetI, offsetJ: Offsets into the larger element interpolation matrix for different fields 1721d172c84bSMatthew G. Knepley fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals 1722d172c84bSMatthew G. Knepley qNC, Nc, Ncj, c: Number of components in this field 1723d172c84bSMatthew G. Knepley Np, p: Number of quad points in the fine grid functional i 1724d172c84bSMatthew G. Knepley k: i*Np + p, overall point number for the interpolation 1725d172c84bSMatthew G. Knepley */ 17269c3cf19fSMatthew 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]; 172736a6d9c0SMatthew G. Knepley } 1728d69c5d34SMatthew G. Knepley } 1729d69c5d34SMatthew G. Knepley } 17300f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1731ffe73a53SMatthew G. Knepley } 173297c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 173397c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 173497c42addSMatthew G. Knepley 173597c42addSMatthew G. Knepley /* Evaluate constant function at points */ 173697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 173797c42addSMatthew G. Knepley cpdim = 1; 173897c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 173997c42addSMatthew G. Knepley if (fieldI == fieldJ) { 174097c42addSMatthew 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); 174197c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 174297c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 17439c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 17449c3cf19fSMatthew 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); 174597c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 174697c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 17479c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p*qNc+c]; 174897c42addSMatthew G. Knepley } 174997c42addSMatthew G. Knepley } 175097c42addSMatthew G. Knepley } 175197c42addSMatthew G. Knepley } 175297c42addSMatthew G. Knepley } 1753d172c84bSMatthew G. Knepley offsetJ += cpdim; 1754d69c5d34SMatthew G. Knepley } 1755d172c84bSMatthew G. Knepley offsetI += fpdim; 1756549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1757d69c5d34SMatthew G. Knepley } 17580f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 17597f5b169aSMatthew G. Knepley /* Preallocate matrix */ 17607f5b169aSMatthew G. Knepley { 1761c094ef40SMatthew G. Knepley Mat preallocator; 1762c094ef40SMatthew G. Knepley PetscScalar *vals; 1763c094ef40SMatthew G. Knepley PetscInt *cellCIndices, *cellFIndices; 1764c094ef40SMatthew G. Knepley PetscInt locRows, locCols, cell; 17657f5b169aSMatthew G. Knepley 1766c094ef40SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr); 1767c094ef40SMatthew G. Knepley ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr); 1768c094ef40SMatthew G. Knepley ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr); 1769c094ef40SMatthew G. Knepley ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr); 1770c094ef40SMatthew G. Knepley ierr = MatSetUp(preallocator);CHKERRQ(ierr); 1771c094ef40SMatthew G. Knepley ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 17727f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 17737f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 1774c094ef40SMatthew G. Knepley ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr); 17757f5b169aSMatthew G. Knepley } 1776c094ef40SMatthew G. Knepley ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr); 1777c094ef40SMatthew G. Knepley ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1778c094ef40SMatthew G. Knepley ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1779c094ef40SMatthew G. Knepley ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr); 1780c094ef40SMatthew G. Knepley ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 17817f5b169aSMatthew G. Knepley } 17827f5b169aSMatthew G. Knepley /* Fill matrix */ 17837f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1784d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1785934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1786d69c5d34SMatthew G. Knepley } 1787549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 178897c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 1789549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1790934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1791934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1792d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1793d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1794934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1795934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1796d69c5d34SMatthew G. Knepley } 1797d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1798d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1799d69c5d34SMatthew G. Knepley } 18006c73c22cSMatthew G. Knepley 1801bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user) 1802bd041c0cSMatthew G. Knepley { 1803bd041c0cSMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness"); 1804bd041c0cSMatthew G. Knepley } 1805bd041c0cSMatthew G. Knepley 180668132eb9SMatthew G. Knepley /*@ 180768132eb9SMatthew G. Knepley DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM. 180868132eb9SMatthew G. Knepley 180968132eb9SMatthew G. Knepley Input Parameters: 181068132eb9SMatthew G. Knepley + dmf - The fine mesh 181168132eb9SMatthew G. Knepley . dmc - The coarse mesh 181268132eb9SMatthew G. Knepley - user - The user context 181368132eb9SMatthew G. Knepley 181468132eb9SMatthew G. Knepley Output Parameter: 181568132eb9SMatthew G. Knepley . In - The interpolation matrix 181668132eb9SMatthew G. Knepley 181768132eb9SMatthew G. Knepley Level: developer 181868132eb9SMatthew G. Knepley 181968132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 182068132eb9SMatthew G. Knepley @*/ 182168132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user) 18224ef9d792SMatthew G. Knepley { 182364e98e1dSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 182464e98e1dSMatthew G. Knepley const char *name = "Interpolator"; 18254ef9d792SMatthew G. Knepley PetscDS prob; 18264ef9d792SMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 18274ef9d792SMatthew G. Knepley PetscHashJK ht; 18284ef9d792SMatthew G. Knepley PetscLayout rLayout; 18294ef9d792SMatthew G. Knepley PetscInt *dnz, *onz; 18304ef9d792SMatthew G. Knepley PetscInt locRows, rStart, rEnd; 18314ef9d792SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 18324ef9d792SMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 18334ef9d792SMatthew G. Knepley PetscScalar *elemMat; 18344ef9d792SMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 18354ef9d792SMatthew G. Knepley PetscErrorCode ierr; 18364ef9d792SMatthew G. Knepley 18374ef9d792SMatthew G. Knepley PetscFunctionBegin; 183877711781SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 18394ef9d792SMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 18404ef9d792SMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 18414ef9d792SMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 18424ef9d792SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 18434ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 18444ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 18454ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 18464ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 18474ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 18484ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 18494ef9d792SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 18504ef9d792SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 18519c3cf19fSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 18524ef9d792SMatthew G. Knepley 18534ef9d792SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 18544ef9d792SMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 18554ef9d792SMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 18564ef9d792SMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 18574ef9d792SMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 18584ef9d792SMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 18594ef9d792SMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 18604ef9d792SMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 18614ef9d792SMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 18624ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 18634ef9d792SMatthew G. Knepley PetscObject obj; 18644ef9d792SMatthew G. Knepley PetscClassId id; 1865c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 18664ef9d792SMatthew G. Knepley PetscQuadrature f; 186717f047d8SMatthew G. Knepley const PetscReal *qpoints; 186817f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 18694ef9d792SMatthew G. Knepley 18704ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 18714ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 18724ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 18734ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 18744ef9d792SMatthew G. Knepley 18754ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 18764ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 18774ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 18784ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 18794ef9d792SMatthew G. Knepley 18804ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 18814ef9d792SMatthew G. Knepley Nc = 1; 18824ef9d792SMatthew G. Knepley } 18834ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 18844ef9d792SMatthew G. Knepley /* For each fine grid cell */ 18854ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 18864ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 18874ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 18884ef9d792SMatthew G. Knepley 18896ecaa68aSToby Isaac ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 18904ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 18919c3cf19fSMatthew G. Knepley if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim); 18924ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 18934ef9d792SMatthew G. Knepley Vec pointVec; 18944ef9d792SMatthew G. Knepley PetscScalar *pV; 18953a93e3b7SToby Isaac PetscSF coarseCellSF = NULL; 18963a93e3b7SToby Isaac const PetscSFNode *coarseCells; 18979c3cf19fSMatthew G. Knepley PetscInt numCoarseCells, q, c; 18984ef9d792SMatthew G. Knepley 18994ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 19004ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 19019c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 19024ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 19034ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 19044ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 19054ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 1906c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 1907c330f8ffSToby Isaac 19084ef9d792SMatthew G. Knepley /* Transform point to real space */ 1909c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 19104ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 19114ef9d792SMatthew G. Knepley } 19124ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 19134ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 19141555c271SMatthew G. Knepley /* OPT: Pack all quad points from fine cell */ 191562a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 19163a93e3b7SToby Isaac ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 19174ef9d792SMatthew G. Knepley /* Update preallocation info */ 19183a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 19193a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 19209c3cf19fSMatthew G. Knepley { 19214ef9d792SMatthew G. Knepley PetscHashJKKey key; 19224ef9d792SMatthew G. Knepley PetscHashJKIter missing, iter; 19234ef9d792SMatthew G. Knepley 19249c3cf19fSMatthew G. Knepley key.j = findices[i]; 19258c543595SMatthew G. Knepley if (key.j >= 0) { 19264ef9d792SMatthew G. Knepley /* Get indices for coarse elements */ 19274ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 19283a93e3b7SToby Isaac ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 19294ef9d792SMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 19304ef9d792SMatthew G. Knepley key.k = cindices[c]; 19314ef9d792SMatthew G. Knepley if (key.k < 0) continue; 19324ef9d792SMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 19334ef9d792SMatthew G. Knepley if (missing) { 19344ef9d792SMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 19354ef9d792SMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 19364ef9d792SMatthew G. Knepley else ++onz[key.j-rStart]; 19374ef9d792SMatthew G. Knepley } 19384ef9d792SMatthew G. Knepley } 19393a93e3b7SToby Isaac ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 19404ef9d792SMatthew G. Knepley } 19414ef9d792SMatthew G. Knepley } 19428c543595SMatthew G. Knepley } 19433a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 19444ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 19454ef9d792SMatthew G. Knepley } 194646bdb399SToby Isaac ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 19474ef9d792SMatthew G. Knepley } 19484ef9d792SMatthew G. Knepley } 19494ef9d792SMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 19504ef9d792SMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 19514ef9d792SMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 19524ef9d792SMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 19534ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 19544ef9d792SMatthew G. Knepley PetscObject obj; 19554ef9d792SMatthew G. Knepley PetscClassId id; 1956c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 19574ef9d792SMatthew G. Knepley PetscQuadrature f; 19584ef9d792SMatthew G. Knepley const PetscReal *qpoints, *qweights; 19599c3cf19fSMatthew G. Knepley PetscInt Nc, qNc, Np, fpdim, i, d; 19604ef9d792SMatthew G. Knepley 19614ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 19624ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 19634ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 19644ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 19654ef9d792SMatthew G. Knepley 19664ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 19674ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 19684ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 19694ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 19704ef9d792SMatthew G. Knepley 19714ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 19724ef9d792SMatthew G. Knepley Nc = 1; 197325ce1634SJed Brown } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %d",field); 19744ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 19754ef9d792SMatthew G. Knepley /* For each fine grid cell */ 19764ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 19774ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 19784ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 19794ef9d792SMatthew G. Knepley 19806ecaa68aSToby Isaac ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 19814ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 19829c3cf19fSMatthew G. Knepley if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim); 19834ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 19844ef9d792SMatthew G. Knepley Vec pointVec; 19854ef9d792SMatthew G. Knepley PetscScalar *pV; 198612111d7cSToby Isaac PetscSF coarseCellSF = NULL; 19873a93e3b7SToby Isaac const PetscSFNode *coarseCells; 198817f047d8SMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 19894ef9d792SMatthew G. Knepley 19904ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 19914ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 19929c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr); 19939c3cf19fSMatthew 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); 19944ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 19954ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 19964ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 19974ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 1998c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 1999c330f8ffSToby Isaac 20004ef9d792SMatthew G. Knepley /* Transform point to real space */ 2001c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 20024ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 20034ef9d792SMatthew G. Knepley } 20044ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 20054ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 20061555c271SMatthew G. Knepley /* OPT: Read this out from preallocation information */ 200762a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 20084ef9d792SMatthew G. Knepley /* Update preallocation info */ 20093a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 20103a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 20114ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 20124ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2013826eb36dSMatthew G. Knepley PetscReal pVReal[3]; 2014c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2015826eb36dSMatthew G. Knepley 20163a93e3b7SToby Isaac ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 20174ef9d792SMatthew G. Knepley /* Transform points from real space to coarse reference space */ 20183a93e3b7SToby Isaac ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 2019e2d86523SMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 2020c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 20214ef9d792SMatthew G. Knepley 20224ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 20234ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 20244ef9d792SMatthew G. Knepley PetscReal *B; 20254ef9d792SMatthew G. Knepley 20264ef9d792SMatthew G. Knepley /* Evaluate coarse basis on contained point */ 20274ef9d792SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 20284ef9d792SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 20299c3cf19fSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 20304ef9d792SMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 20314ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 20329c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c]; 20334ef9d792SMatthew G. Knepley } 20344ef9d792SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 20354ef9d792SMatthew G. Knepley } else { 20364ef9d792SMatthew G. Knepley cpdim = 1; 20374ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 20389c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c]; 20394ef9d792SMatthew G. Knepley } 20404ef9d792SMatthew G. Knepley } 20414ef9d792SMatthew G. Knepley /* Update interpolator */ 20429c3cf19fSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 20439c3cf19fSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 20449c3cf19fSMatthew G. Knepley ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr); 20453a93e3b7SToby Isaac ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 20464ef9d792SMatthew G. Knepley } 20474ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 20483a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 20494ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 20504ef9d792SMatthew G. Knepley } 205146bdb399SToby Isaac ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 20524ef9d792SMatthew G. Knepley } 20534ef9d792SMatthew G. Knepley } 20544ef9d792SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 20554ef9d792SMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 20564ef9d792SMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 20574ef9d792SMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20584ef9d792SMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 205977711781SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 20604ef9d792SMatthew G. Knepley PetscFunctionReturn(0); 20614ef9d792SMatthew G. Knepley } 20624ef9d792SMatthew G. Knepley 206346fa42a0SMatthew G. Knepley /*@ 2064bd041c0cSMatthew G. Knepley DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM. 2065bd041c0cSMatthew G. Knepley 2066bd041c0cSMatthew G. Knepley Input Parameters: 2067bd041c0cSMatthew G. Knepley + dmf - The fine mesh 2068bd041c0cSMatthew G. Knepley . dmc - The coarse mesh 2069bd041c0cSMatthew G. Knepley - user - The user context 2070bd041c0cSMatthew G. Knepley 2071bd041c0cSMatthew G. Knepley Output Parameter: 2072bd041c0cSMatthew G. Knepley . mass - The mass matrix 2073bd041c0cSMatthew G. Knepley 2074bd041c0cSMatthew G. Knepley Level: developer 2075bd041c0cSMatthew G. Knepley 2076bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 2077bd041c0cSMatthew G. Knepley @*/ 2078bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user) 2079bd041c0cSMatthew G. Knepley { 2080bd041c0cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 2081bd041c0cSMatthew G. Knepley const char *name = "Mass Matrix"; 2082bd041c0cSMatthew G. Knepley PetscDS prob; 2083bd041c0cSMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 2084bd041c0cSMatthew G. Knepley PetscHashJK ht; 2085bd041c0cSMatthew G. Knepley PetscLayout rLayout; 2086bd041c0cSMatthew G. Knepley PetscInt *dnz, *onz; 2087bd041c0cSMatthew G. Knepley PetscInt locRows, rStart, rEnd; 2088bd041c0cSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 2089bd041c0cSMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 2090bd041c0cSMatthew G. Knepley PetscScalar *elemMat; 2091bd041c0cSMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 2092bd041c0cSMatthew G. Knepley PetscErrorCode ierr; 2093bd041c0cSMatthew G. Knepley 2094bd041c0cSMatthew G. Knepley PetscFunctionBegin; 2095bd041c0cSMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 2096bd041c0cSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 2097bd041c0cSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 2098bd041c0cSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 2099bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 2100bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 2101bd041c0cSMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 2102bd041c0cSMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 2103bd041c0cSMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 2104bd041c0cSMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 2105bd041c0cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 2106bd041c0cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2107bd041c0cSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 2108bd041c0cSMatthew G. Knepley 2109bd041c0cSMatthew G. Knepley ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr); 2110bd041c0cSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr); 2111bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 2112bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 2113bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 2114bd041c0cSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 2115bd041c0cSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 2116bd041c0cSMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 2117bd041c0cSMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 2118bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2119bd041c0cSMatthew G. Knepley PetscObject obj; 2120bd041c0cSMatthew G. Knepley PetscClassId id; 2121bd041c0cSMatthew G. Knepley PetscQuadrature quad; 2122bd041c0cSMatthew G. Knepley const PetscReal *qpoints; 2123bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 2124bd041c0cSMatthew G. Knepley 2125bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 2126bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2127bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);} 2128bd041c0cSMatthew G. Knepley else {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);} 2129bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr); 2130bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 2131bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2132bd041c0cSMatthew G. Knepley Vec pointVec; 2133bd041c0cSMatthew G. Knepley PetscScalar *pV; 2134bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 2135bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 2136bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, q, c; 2137bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 2138bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 2139bd041c0cSMatthew G. Knepley 2140bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2141bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2142bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 2143bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 2144bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 2145bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2146bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 2147c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2148c330f8ffSToby Isaac 2149bd041c0cSMatthew G. Knepley /* Transform point to real space */ 2150c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 2151bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 2152bd041c0cSMatthew G. Knepley } 2153bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2154bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 2155bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 2156bd041c0cSMatthew G. Knepley ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 2157bd041c0cSMatthew G. Knepley /* Update preallocation info */ 2158bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 2159bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 2160bd041c0cSMatthew G. Knepley { 2161bd041c0cSMatthew G. Knepley PetscHashJKKey key; 2162bd041c0cSMatthew G. Knepley PetscHashJKIter missing, iter; 2163bd041c0cSMatthew G. Knepley 2164bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2165bd041c0cSMatthew G. Knepley key.j = findices[i]; 2166bd041c0cSMatthew G. Knepley if (key.j >= 0) { 2167bd041c0cSMatthew G. Knepley /* Get indices for coarse elements */ 2168bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2169bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2170bd041c0cSMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 2171bd041c0cSMatthew G. Knepley key.k = cindices[c]; 2172bd041c0cSMatthew G. Knepley if (key.k < 0) continue; 2173bd041c0cSMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 2174bd041c0cSMatthew G. Knepley if (missing) { 2175bd041c0cSMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 2176bd041c0cSMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 2177bd041c0cSMatthew G. Knepley else ++onz[key.j-rStart]; 2178bd041c0cSMatthew G. Knepley } 2179bd041c0cSMatthew G. Knepley } 2180bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2181bd041c0cSMatthew G. Knepley } 2182bd041c0cSMatthew G. Knepley } 2183bd041c0cSMatthew G. Knepley } 2184bd041c0cSMatthew G. Knepley } 2185bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 2186bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 2187bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2188bd041c0cSMatthew G. Knepley } 2189bd041c0cSMatthew G. Knepley } 2190bd041c0cSMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 2191bd041c0cSMatthew G. Knepley ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 2192bd041c0cSMatthew G. Knepley ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 2193bd041c0cSMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 2194bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2195bd041c0cSMatthew G. Knepley PetscObject obj; 2196bd041c0cSMatthew G. Knepley PetscClassId id; 2197bd041c0cSMatthew G. Knepley PetscQuadrature quad; 2198bd041c0cSMatthew G. Knepley PetscReal *Bfine; 2199bd041c0cSMatthew G. Knepley const PetscReal *qpoints, *qweights; 2200bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 2201bd041c0cSMatthew G. Knepley 2202bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 2203bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2204bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);} 2205bd041c0cSMatthew G. Knepley else {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);} 2206bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr); 2207bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 2208bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2209bd041c0cSMatthew G. Knepley Vec pointVec; 2210bd041c0cSMatthew G. Knepley PetscScalar *pV; 2211bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 2212bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 2213bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 2214bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 2215bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 2216bd041c0cSMatthew G. Knepley 2217bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2218bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2219bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 2220bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 2221bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 2222bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2223bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 2224c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2225c330f8ffSToby Isaac 2226bd041c0cSMatthew G. Knepley /* Transform point to real space */ 2227c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 2228bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 2229bd041c0cSMatthew G. Knepley } 2230bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2231bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 2232bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 2233bd041c0cSMatthew G. Knepley /* Update matrix */ 2234bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 2235bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 2236bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2237bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2238bd041c0cSMatthew G. Knepley PetscReal pVReal[3]; 2239c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2240c330f8ffSToby Isaac 2241bd041c0cSMatthew G. Knepley 2242bd041c0cSMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2243bd041c0cSMatthew G. Knepley /* Transform points from real space to coarse reference space */ 2244bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 2245bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 2246c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 2247bd041c0cSMatthew G. Knepley 2248bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 2249bd041c0cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2250bd041c0cSMatthew G. Knepley PetscReal *B; 2251bd041c0cSMatthew G. Knepley 2252bd041c0cSMatthew G. Knepley /* Evaluate coarse basis on contained point */ 2253bd041c0cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 2254bd041c0cSMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 2255bd041c0cSMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 2256bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2257bd041c0cSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 2258bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2259bd041c0cSMatthew 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; 2260bd041c0cSMatthew G. Knepley } 2261bd041c0cSMatthew G. Knepley /* Update interpolator */ 2262bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 2263bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 2264bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 2265bd041c0cSMatthew G. Knepley } 2266bd041c0cSMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 2267bd041c0cSMatthew G. Knepley } else { 2268bd041c0cSMatthew G. Knepley cpdim = 1; 2269bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2270bd041c0cSMatthew G. Knepley ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr); 2271bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2272bd041c0cSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ; 2273bd041c0cSMatthew G. Knepley } 2274bd041c0cSMatthew G. Knepley /* Update interpolator */ 2275bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 2276bd041c0cSMatthew 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); 2277bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 2278bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 2279bd041c0cSMatthew G. Knepley } 2280bd041c0cSMatthew G. Knepley } 2281bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr); 2282bd041c0cSMatthew G. Knepley } 2283bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2284bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 2285bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 2286bd041c0cSMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr); 2287bd041c0cSMatthew G. Knepley } 2288bd041c0cSMatthew G. Knepley } 2289bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 2290bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 2291bd041c0cSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 2292bd041c0cSMatthew G. Knepley ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2293bd041c0cSMatthew G. Knepley ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2294bd041c0cSMatthew G. Knepley PetscFunctionReturn(0); 2295bd041c0cSMatthew G. Knepley } 2296bd041c0cSMatthew G. Knepley 2297bd041c0cSMatthew G. Knepley /*@ 229846fa42a0SMatthew G. Knepley DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns 229946fa42a0SMatthew G. Knepley 230046fa42a0SMatthew G. Knepley Input Parameters: 230146fa42a0SMatthew G. Knepley + dmc - The coarse mesh 230246fa42a0SMatthew G. Knepley - dmf - The fine mesh 230346fa42a0SMatthew G. Knepley - user - The user context 230446fa42a0SMatthew G. Knepley 230546fa42a0SMatthew G. Knepley Output Parameter: 230646fa42a0SMatthew G. Knepley . sc - The mapping 230746fa42a0SMatthew G. Knepley 230846fa42a0SMatthew G. Knepley Level: developer 230946fa42a0SMatthew G. Knepley 231046fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 231146fa42a0SMatthew G. Knepley @*/ 23127c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 23137c927364SMatthew G. Knepley { 2314e9d4ef1bSMatthew G. Knepley PetscDS prob; 23157c927364SMatthew G. Knepley PetscFE *feRef; 231697c42addSMatthew G. Knepley PetscFV *fvRef; 23177c927364SMatthew G. Knepley Vec fv, cv; 23187c927364SMatthew G. Knepley IS fis, cis; 23197c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 23207c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 23210bd915a7SMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m; 23227c927364SMatthew G. Knepley PetscErrorCode ierr; 23237c927364SMatthew G. Knepley 23247c927364SMatthew G. Knepley PetscFunctionBegin; 232575a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2326c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 23277c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 23287c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 23297c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 23307c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 23317c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 23327c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 23339ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 23349ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2335e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 233697c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 23377c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 233897c42addSMatthew G. Knepley PetscObject obj; 233997c42addSMatthew G. Knepley PetscClassId id; 2340aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 23417c927364SMatthew G. Knepley 234297c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 234397c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 234497c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 234597c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 234697c42addSMatthew G. Knepley 23477c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 23487c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 23497c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 235097c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 235197c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 235297c42addSMatthew G. Knepley PetscDualSpace Q; 235397c42addSMatthew G. Knepley 235497c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 235597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 235697c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 235797c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 235897c42addSMatthew G. Knepley } 2359d172c84bSMatthew G. Knepley fTotDim += fNb; 23607c927364SMatthew G. Knepley } 2361e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 23627c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 23637c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 23647c927364SMatthew G. Knepley PetscFE feC; 236597c42addSMatthew G. Knepley PetscFV fvC; 23667c927364SMatthew G. Knepley PetscDualSpace QF, QC; 2367d172c84bSMatthew G. Knepley PetscInt order = -1, NcF, NcC, fpdim, cpdim; 23687c927364SMatthew G. Knepley 236997c42addSMatthew G. Knepley if (feRef[field]) { 2370e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 23717c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 23727c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 23737c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 2374d172c84bSMatthew G. Knepley ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr); 23757c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 23767c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 23777c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 237897c42addSMatthew G. Knepley } else { 237997c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 238097c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 238197c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 238297c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 238397c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 238497c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 238597c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 238697c42addSMatthew G. Knepley } 238797c42addSMatthew 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); 23887c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 23897c927364SMatthew G. Knepley PetscQuadrature cfunc; 2390d172c84bSMatthew G. Knepley const PetscReal *cqpoints, *cqweights; 2391d172c84bSMatthew G. Knepley PetscInt NqcC, NpC; 239297c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 23937c927364SMatthew G. Knepley 23947c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 2395d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr); 2396d172c84bSMatthew 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); 239797c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 23987c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 23997c927364SMatthew G. Knepley PetscQuadrature ffunc; 2400d172c84bSMatthew G. Knepley const PetscReal *fqpoints, *fqweights; 24017c927364SMatthew G. Knepley PetscReal sum = 0.0; 2402d172c84bSMatthew G. Knepley PetscInt NqcF, NpF; 24037c927364SMatthew G. Knepley 24047c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 2405d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr); 2406d172c84bSMatthew 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); 24077c927364SMatthew G. Knepley if (NpC != NpF) continue; 24087c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 24097c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 2410d172c84bSMatthew G. Knepley for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]); 2411d172c84bSMatthew G. Knepley if (sum < 1.0e-9) continue; 2412d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+f; 241397c42addSMatthew G. Knepley found = PETSC_TRUE; 24147c927364SMatthew G. Knepley break; 24157c927364SMatthew G. Knepley } 241697c42addSMatthew G. Knepley if (!found) { 241797c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 2418d172c84bSMatthew G. Knepley if (fvRef[field] || (feRef[field] && order == 0)) { 2419d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+0; 242097c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 242197c42addSMatthew G. Knepley } 24227c927364SMatthew G. Knepley } 2423d172c84bSMatthew G. Knepley offsetC += cpdim; 2424d172c84bSMatthew G. Knepley offsetF += fpdim; 24257c927364SMatthew G. Knepley } 242697c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 242797c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 24287c927364SMatthew G. Knepley 24297c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 24307c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 24310bd915a7SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr); 24327c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 24337c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 2434aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 2435aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 24367c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 24377c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 24387c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 24397c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 24400bd915a7SMatthew G. Knepley if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue; 24417c927364SMatthew 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]]); 24427c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 24437c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 24447c927364SMatthew G. Knepley } 24457c927364SMatthew G. Knepley } 24467c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 24477c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 24487c927364SMatthew G. Knepley 24497c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 24507c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 24517c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 24527c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 24537c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 24547c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 24557c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 245675a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2457cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 2458cb1e1211SMatthew G Knepley } 2459