1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2da97024aSMatthew G. Knepley #include <petscsf.h> 3cb1e1211SMatthew G Knepley 4e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h> 5af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 6af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h> 7a0845e3aSMatthew G. Knepley 82f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy) 92f856554SMatthew G. Knepley { 102f856554SMatthew G. Knepley PetscBool isPlex; 112f856554SMatthew G. Knepley PetscErrorCode ierr; 122f856554SMatthew G. Knepley 132f856554SMatthew G. Knepley PetscFunctionBegin; 142f856554SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr); 152f856554SMatthew G. Knepley if (isPlex) { 162f856554SMatthew G. Knepley *plex = dm; 172f856554SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 182f856554SMatthew G. Knepley } else { 192f856554SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr); 202f856554SMatthew G. Knepley if (!*plex) { 212f856554SMatthew G. Knepley ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 222f856554SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr); 232f856554SMatthew G. Knepley if (copy) { 242f856554SMatthew G. Knepley const char *comps[3] = {"A", "dmAux"}; 252f856554SMatthew G. Knepley PetscObject obj; 262f856554SMatthew G. Knepley PetscInt i; 272f856554SMatthew G. Knepley 282f856554SMatthew G. Knepley { 292f856554SMatthew G. Knepley /* Run the subdomain hook (this will copy the DMSNES/DMTS) */ 302f856554SMatthew G. Knepley DMSubDomainHookLink link; 312f856554SMatthew G. Knepley for (link = dm->subdomainhook; link; link = link->next) { 322f856554SMatthew G. Knepley if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);} 332f856554SMatthew G. Knepley } 342f856554SMatthew G. Knepley } 352f856554SMatthew G. Knepley for (i = 0; i < 3; i++) { 362f856554SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr); 372f856554SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr); 382f856554SMatthew G. Knepley } 392f856554SMatthew G. Knepley } 402f856554SMatthew G. Knepley } else { 412f856554SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr); 422f856554SMatthew G. Knepley } 432f856554SMatthew G. Knepley } 442f856554SMatthew G. Knepley PetscFunctionReturn(0); 452f856554SMatthew G. Knepley } 462f856554SMatthew G. Knepley 479b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx) 489b6f715bSMatthew G. Knepley { 499b6f715bSMatthew G. Knepley PetscFEGeom *geom = (PetscFEGeom *) ctx; 509b6f715bSMatthew G. Knepley PetscErrorCode ierr; 519b6f715bSMatthew G. Knepley 529b6f715bSMatthew G. Knepley PetscFunctionBegin; 539b6f715bSMatthew G. Knepley ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr); 549b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 559b6f715bSMatthew G. Knepley } 569b6f715bSMatthew G. Knepley 579b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 589b6f715bSMatthew G. Knepley { 599b6f715bSMatthew G. Knepley char composeStr[33] = {0}; 609b6f715bSMatthew G. Knepley PetscObjectId id; 619b6f715bSMatthew G. Knepley PetscContainer container; 629b6f715bSMatthew G. Knepley PetscErrorCode ierr; 639b6f715bSMatthew G. Knepley 649b6f715bSMatthew G. Knepley PetscFunctionBegin; 659b6f715bSMatthew G. Knepley ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr); 669b6f715bSMatthew G. Knepley ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr); 679b6f715bSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr); 689b6f715bSMatthew G. Knepley if (container) { 699b6f715bSMatthew G. Knepley ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr); 709b6f715bSMatthew G. Knepley } else { 719b6f715bSMatthew G. Knepley ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr); 729b6f715bSMatthew G. Knepley ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 739b6f715bSMatthew G. Knepley ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr); 749b6f715bSMatthew G. Knepley ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr); 759b6f715bSMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr); 769b6f715bSMatthew G. Knepley ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 779b6f715bSMatthew G. Knepley } 789b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 799b6f715bSMatthew G. Knepley } 809b6f715bSMatthew G. Knepley 819b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 829b6f715bSMatthew G. Knepley { 839b6f715bSMatthew G. Knepley PetscFunctionBegin; 849b6f715bSMatthew G. Knepley *geom = NULL; 859b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 869b6f715bSMatthew G. Knepley } 879b6f715bSMatthew G. Knepley 8846fa42a0SMatthew G. Knepley /*@ 8946fa42a0SMatthew G. Knepley DMPlexGetScale - Get the scale for the specified fundamental unit 9046fa42a0SMatthew G. Knepley 9146fa42a0SMatthew G. Knepley Not collective 9246fa42a0SMatthew G. Knepley 9346fa42a0SMatthew G. Knepley Input Arguments: 9446fa42a0SMatthew G. Knepley + dm - the DM 9546fa42a0SMatthew G. Knepley - unit - The SI unit 9646fa42a0SMatthew G. Knepley 9746fa42a0SMatthew G. Knepley Output Argument: 9846fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit 9946fa42a0SMatthew G. Knepley 10046fa42a0SMatthew G. Knepley Level: advanced 10146fa42a0SMatthew G. Knepley 10246fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit 10346fa42a0SMatthew G. Knepley @*/ 104cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 105cb1e1211SMatthew G Knepley { 106cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 107cb1e1211SMatthew G Knepley 108cb1e1211SMatthew G Knepley PetscFunctionBegin; 109cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 110cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 111cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 112cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 113cb1e1211SMatthew G Knepley } 114cb1e1211SMatthew G Knepley 11546fa42a0SMatthew G. Knepley /*@ 11646fa42a0SMatthew G. Knepley DMPlexSetScale - Set the scale for the specified fundamental unit 11746fa42a0SMatthew G. Knepley 11846fa42a0SMatthew G. Knepley Not collective 11946fa42a0SMatthew G. Knepley 12046fa42a0SMatthew G. Knepley Input Arguments: 12146fa42a0SMatthew G. Knepley + dm - the DM 12246fa42a0SMatthew G. Knepley . unit - The SI unit 12346fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit 12446fa42a0SMatthew G. Knepley 12546fa42a0SMatthew G. Knepley Level: advanced 12646fa42a0SMatthew G. Knepley 12746fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit 12846fa42a0SMatthew G. Knepley @*/ 129cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 130cb1e1211SMatthew G Knepley { 131cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 132cb1e1211SMatthew G Knepley 133cb1e1211SMatthew G Knepley PetscFunctionBegin; 134cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 135cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 136cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 137cb1e1211SMatthew G Knepley } 138cb1e1211SMatthew G Knepley 139327779f2SMatthew Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nc, PetscScalar *mode, void *ctx) 140026175e5SToby Isaac { 14112adca46SMatthew 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}}}; 142026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *) ctx; 143ad917190SMatthew G. Knepley PetscInt dim2 = ctxInt[0]; 144026175e5SToby Isaac PetscInt d = ctxInt[1]; 145026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 146026175e5SToby Isaac 147ad917190SMatthew G. Knepley PetscFunctionBegin; 148ff1e0c32SBarry Smith if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %D does not match context dimension %D", dim, dim2); 149026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 150026175e5SToby Isaac if (d < dim) { 15112adca46SMatthew G. Knepley mode[d] = 1.; /* Translation along axis d */ 152ad917190SMatthew G. Knepley } else { 153026175e5SToby Isaac for (i = 0; i < dim; i++) { 154026175e5SToby Isaac for (j = 0; j < dim; j++) { 15512adca46SMatthew G. Knepley mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */ 156026175e5SToby Isaac } 157026175e5SToby Isaac } 158026175e5SToby Isaac } 159ad917190SMatthew G. Knepley PetscFunctionReturn(0); 160026175e5SToby Isaac } 161026175e5SToby Isaac 162cc4e42d9SMartin Diehl /*@ 16312adca46SMatthew G. Knepley DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation 164cb1e1211SMatthew G Knepley 165d083f849SBarry Smith Collective on dm 166cb1e1211SMatthew G Knepley 167cb1e1211SMatthew G Knepley Input Arguments: 168*56cf3b9cSMatthew G. Knepley + dm - the DM 169*56cf3b9cSMatthew G. Knepley - field - The field number for the rigid body space, or 0 for the default 170cb1e1211SMatthew G Knepley 171cb1e1211SMatthew G Knepley Output Argument: 172cb1e1211SMatthew G Knepley . sp - the null space 173cb1e1211SMatthew G Knepley 17412adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 175cb1e1211SMatthew G Knepley 176cb1e1211SMatthew G Knepley Level: advanced 177cb1e1211SMatthew G Knepley 17812adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG 179cb1e1211SMatthew G Knepley @*/ 180*56cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscInt field, MatNullSpace *sp) 181cb1e1211SMatthew G Knepley { 182*56cf3b9cSMatthew G. Knepley PetscErrorCode (**func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *); 183cb1e1211SMatthew G Knepley MPI_Comm comm; 184026175e5SToby Isaac Vec mode[6]; 185026175e5SToby Isaac PetscSection section, globalSection; 186*56cf3b9cSMatthew G. Knepley PetscInt dim, dimEmbed, Nf, n, m, mmin, d, i, j; 187cb1e1211SMatthew G Knepley PetscErrorCode ierr; 188cb1e1211SMatthew G Knepley 189cb1e1211SMatthew G Knepley PetscFunctionBegin; 190cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr); 191c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1929d8fbdeaSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 193*56cf3b9cSMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 194*56cf3b9cSMatthew G. Knepley if (Nf && (field < 0 || field >= Nf)) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, Nf)", field, Nf); 195*56cf3b9cSMatthew G. Knepley if (dim == 1 && Nf < 2) { 196cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 197cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 198cb1e1211SMatthew G Knepley } 19992fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 200e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 201cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 202*56cf3b9cSMatthew G. Knepley ierr = PetscCalloc1(Nf, &func);CHKERRQ(ierr); 203b247467aSMatthew G. Knepley m = (dim*(dim+1))/2; 204cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 205cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 206cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 207b247467aSMatthew G. Knepley ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr); 208b247467aSMatthew G. Knepley mmin = PetscMin(m, n); 209*56cf3b9cSMatthew G. Knepley func[field] = DMPlexProjectRigidBody_Private; 210cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 211026175e5SToby Isaac for (d = 0; d < m; d++) { 212330485fdSToby Isaac PetscInt ctx[2]; 213026175e5SToby Isaac void *voidctx = (void *) (&ctx[0]); 214cb1e1211SMatthew G Knepley 2159d8fbdeaSMatthew G. Knepley ctx[0] = dimEmbed; 216330485fdSToby Isaac ctx[1] = d; 217*56cf3b9cSMatthew G. Knepley ierr = DMProjectFunction(dm, 0.0, func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 218cb1e1211SMatthew G Knepley } 2193b2202bfSJacob Faibussowitsch /* Orthonormalize system */ 220b50a2c0aSJacob Faibussowitsch for (i = 0; i < mmin; ++i) { 221b77f2eeeSJacob Faibussowitsch PetscScalar dots[6]; 222b50a2c0aSJacob Faibussowitsch 2239019a6d7SJacob Faibussowitsch ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 224b77f2eeeSJacob Faibussowitsch ierr = VecMDot(mode[i], mmin-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr); 225b50a2c0aSJacob Faibussowitsch for (j = i+1; j < mmin; ++j) { 226b77f2eeeSJacob Faibussowitsch dots[j] *= -1.0; 227b77f2eeeSJacob Faibussowitsch ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr); 228b50a2c0aSJacob Faibussowitsch } 229cb1e1211SMatthew G Knepley } 230b247467aSMatthew G. Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr); 231b247467aSMatthew G. Knepley for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 232*56cf3b9cSMatthew G. Knepley ierr = PetscFree(func);CHKERRQ(ierr); 233cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 234cb1e1211SMatthew G Knepley } 235cb1e1211SMatthew G Knepley 236cc4e42d9SMartin Diehl /*@ 23712adca46SMatthew G. Knepley DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation 23812adca46SMatthew G. Knepley 239d083f849SBarry Smith Collective on dm 24012adca46SMatthew G. Knepley 24112adca46SMatthew G. Knepley Input Arguments: 24212adca46SMatthew G. Knepley + dm - the DM 24312adca46SMatthew G. Knepley . nb - The number of bodies 24412adca46SMatthew G. Knepley . label - The DMLabel marking each domain 24512adca46SMatthew G. Knepley . nids - The number of ids per body 24612adca46SMatthew G. Knepley - ids - An array of the label ids in sequence for each domain 24712adca46SMatthew G. Knepley 24812adca46SMatthew G. Knepley Output Argument: 24912adca46SMatthew G. Knepley . sp - the null space 25012adca46SMatthew G. Knepley 25112adca46SMatthew G. Knepley Note: This is necessary to provide a suitable coarse space for algebraic multigrid 25212adca46SMatthew G. Knepley 25312adca46SMatthew G. Knepley Level: advanced 25412adca46SMatthew G. Knepley 25512adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate() 25612adca46SMatthew G. Knepley @*/ 25712adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp) 25812adca46SMatthew G. Knepley { 25912adca46SMatthew G. Knepley MPI_Comm comm; 26012adca46SMatthew G. Knepley PetscSection section, globalSection; 26112adca46SMatthew G. Knepley Vec *mode; 26212adca46SMatthew G. Knepley PetscScalar *dots; 26312adca46SMatthew G. Knepley PetscInt dim, dimEmbed, n, m, b, d, i, j, off; 26412adca46SMatthew G. Knepley PetscErrorCode ierr; 26512adca46SMatthew G. Knepley 26612adca46SMatthew G. Knepley PetscFunctionBegin; 26712adca46SMatthew G. Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 26812adca46SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 26912adca46SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 27092fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 271e87a4003SBarry Smith ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 27212adca46SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 27312adca46SMatthew G. Knepley m = nb * (dim*(dim+1))/2; 27412adca46SMatthew G. Knepley ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr); 27512adca46SMatthew G. Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 27612adca46SMatthew G. Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 27712adca46SMatthew G. Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 27812adca46SMatthew G. Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 27912adca46SMatthew G. Knepley for (b = 0, off = 0; b < nb; ++b) { 28012adca46SMatthew G. Knepley for (d = 0; d < m/nb; ++d) { 28112adca46SMatthew G. Knepley PetscInt ctx[2]; 28212adca46SMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private; 28312adca46SMatthew G. Knepley void *voidctx = (void *) (&ctx[0]); 28412adca46SMatthew G. Knepley 28512adca46SMatthew G. Knepley ctx[0] = dimEmbed; 28612adca46SMatthew G. Knepley ctx[1] = d; 28712adca46SMatthew G. Knepley ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 28812adca46SMatthew G. Knepley off += nids[b]; 28912adca46SMatthew G. Knepley } 29012adca46SMatthew G. Knepley } 2913b2202bfSJacob Faibussowitsch /* Orthonormalize system */ 292606c1a1cSJacob Faibussowitsch for (i = 0; i < m; ++i) { 293b77f2eeeSJacob Faibussowitsch PetscScalar dots[6]; 2945a0e29b9SJacob Faibussowitsch 2959019a6d7SJacob Faibussowitsch ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 296b77f2eeeSJacob Faibussowitsch ierr = VecMDot(mode[i], m-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr); 2975a0e29b9SJacob Faibussowitsch for (j = i+1; j < m; ++j) { 298b77f2eeeSJacob Faibussowitsch dots[j] *= -1.0; 299b77f2eeeSJacob Faibussowitsch ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr); 3005a0e29b9SJacob Faibussowitsch } 30112adca46SMatthew G. Knepley } 30212adca46SMatthew G. Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 30312adca46SMatthew G. Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 30412adca46SMatthew G. Knepley ierr = PetscFree2(mode, dots);CHKERRQ(ierr); 30512adca46SMatthew G. Knepley PetscFunctionReturn(0); 30612adca46SMatthew G. Knepley } 30712adca46SMatthew G. Knepley 308b29cfa1cSToby Isaac /*@ 309b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 310b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 311b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 312b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 313b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 314b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 315b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 316b29cfa1cSToby Isaac 317b29cfa1cSToby Isaac Input Parameters: 318b29cfa1cSToby Isaac + dm - the DMPlex object 319b29cfa1cSToby Isaac - height - the maximum projection height >= 0 320b29cfa1cSToby Isaac 321b29cfa1cSToby Isaac Level: advanced 322b29cfa1cSToby Isaac 3234d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 324b29cfa1cSToby Isaac @*/ 325b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 326b29cfa1cSToby Isaac { 327b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 328b29cfa1cSToby Isaac 329b29cfa1cSToby Isaac PetscFunctionBegin; 330b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 331b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 332b29cfa1cSToby Isaac PetscFunctionReturn(0); 333b29cfa1cSToby Isaac } 334b29cfa1cSToby Isaac 335b29cfa1cSToby Isaac /*@ 336b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 337b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 338b29cfa1cSToby Isaac 339b29cfa1cSToby Isaac Input Parameters: 340b29cfa1cSToby Isaac . dm - the DMPlex object 341b29cfa1cSToby Isaac 342b29cfa1cSToby Isaac Output Parameters: 343b29cfa1cSToby Isaac . height - the maximum projection height 344b29cfa1cSToby Isaac 345b29cfa1cSToby Isaac Level: intermediate 346b29cfa1cSToby Isaac 3474d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 348b29cfa1cSToby Isaac @*/ 349b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 350b29cfa1cSToby Isaac { 351b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 352b29cfa1cSToby Isaac 353b29cfa1cSToby Isaac PetscFunctionBegin; 354b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 355b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 356b29cfa1cSToby Isaac PetscFunctionReturn(0); 357b29cfa1cSToby Isaac } 358b29cfa1cSToby Isaac 359ca3d3a14SMatthew G. Knepley typedef struct { 360ca3d3a14SMatthew G. Knepley PetscReal alpha; /* The first Euler angle, and in 2D the only one */ 361ca3d3a14SMatthew G. Knepley PetscReal beta; /* The second Euler angle */ 362ca3d3a14SMatthew G. Knepley PetscReal gamma; /* The third Euler angle */ 363ca3d3a14SMatthew G. Knepley PetscInt dim; /* The dimension of R */ 364ca3d3a14SMatthew G. Knepley PetscScalar *R; /* The rotation matrix, transforming a vector in the local basis to the global basis */ 365ca3d3a14SMatthew G. Knepley PetscScalar *RT; /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */ 366ca3d3a14SMatthew G. Knepley } RotCtx; 367ca3d3a14SMatthew G. Knepley 368ca3d3a14SMatthew G. Knepley /* 369ca3d3a14SMatthew G. Knepley Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that 370ca3d3a14SMatthew G. Knepley we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows: 371ca3d3a14SMatthew G. Knepley $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis. 372ca3d3a14SMatthew G. Knepley $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis. 373ca3d3a14SMatthew G. Knepley $ The XYZ system rotates a third time about the z axis by gamma. 374ca3d3a14SMatthew G. Knepley */ 375ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx) 376ca3d3a14SMatthew G. Knepley { 377ca3d3a14SMatthew G. Knepley RotCtx *rc = (RotCtx *) ctx; 378ca3d3a14SMatthew G. Knepley PetscInt dim = rc->dim; 379ca3d3a14SMatthew G. Knepley PetscReal c1, s1, c2, s2, c3, s3; 380ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 381ca3d3a14SMatthew G. Knepley 382ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 383ca3d3a14SMatthew G. Knepley ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr); 384ca3d3a14SMatthew G. Knepley switch (dim) { 385ca3d3a14SMatthew G. Knepley case 2: 386ca3d3a14SMatthew G. Knepley c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha); 387ca3d3a14SMatthew G. Knepley rc->R[0] = c1;rc->R[1] = s1; 388ca3d3a14SMatthew G. Knepley rc->R[2] = -s1;rc->R[3] = c1; 389580bdb30SBarry Smith ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr); 390ca3d3a14SMatthew G. Knepley DMPlex_Transpose2D_Internal(rc->RT);break; 391ca3d3a14SMatthew G. Knepley break; 392ca3d3a14SMatthew G. Knepley case 3: 393ca3d3a14SMatthew G. Knepley c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha); 394ca3d3a14SMatthew G. Knepley c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta); 395ca3d3a14SMatthew G. Knepley c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma); 396ca3d3a14SMatthew G. Knepley rc->R[0] = c1*c3 - c2*s1*s3;rc->R[1] = c3*s1 + c1*c2*s3;rc->R[2] = s2*s3; 397ca3d3a14SMatthew G. Knepley rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] = c1*c2*c3 - s1*s3; rc->R[5] = c3*s2; 398ca3d3a14SMatthew G. Knepley rc->R[6] = s1*s2; rc->R[7] = -c1*s2; rc->R[8] = c2; 399580bdb30SBarry Smith ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr); 400ca3d3a14SMatthew G. Knepley DMPlex_Transpose3D_Internal(rc->RT);break; 401ca3d3a14SMatthew G. Knepley break; 402ca3d3a14SMatthew G. Knepley default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim); 403ca3d3a14SMatthew G. Knepley } 404ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 405ca3d3a14SMatthew G. Knepley } 406ca3d3a14SMatthew G. Knepley 407ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx) 408ca3d3a14SMatthew G. Knepley { 409ca3d3a14SMatthew G. Knepley RotCtx *rc = (RotCtx *) ctx; 410ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 411ca3d3a14SMatthew G. Knepley 412ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 413ca3d3a14SMatthew G. Knepley ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr); 414ca3d3a14SMatthew G. Knepley ierr = PetscFree(rc);CHKERRQ(ierr); 415ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 416ca3d3a14SMatthew G. Knepley } 417ca3d3a14SMatthew G. Knepley 418ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx) 419ca3d3a14SMatthew G. Knepley { 420ca3d3a14SMatthew G. Knepley RotCtx *rc = (RotCtx *) ctx; 421ca3d3a14SMatthew G. Knepley 422ca3d3a14SMatthew G. Knepley PetscFunctionBeginHot; 423ca3d3a14SMatthew G. Knepley PetscValidPointer(ctx, 5); 424ca3d3a14SMatthew G. Knepley if (l2g) {*A = rc->R;} 425ca3d3a14SMatthew G. Knepley else {*A = rc->RT;} 426ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 427ca3d3a14SMatthew G. Knepley } 428ca3d3a14SMatthew G. Knepley 429ab6a9622SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApplyReal_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscReal *y, PetscReal *z, void *ctx) 430ab6a9622SMatthew G. Knepley { 431ec277c0fSMatthew G. Knepley PetscErrorCode ierr; 432ec277c0fSMatthew G. Knepley 433ec277c0fSMatthew G. Knepley PetscFunctionBegin; 434ab6a9622SMatthew G. Knepley #if defined(PETSC_USE_COMPLEX) 435ab6a9622SMatthew G. Knepley switch (dim) { 436ab6a9622SMatthew G. Knepley case 2: 437ab6a9622SMatthew G. Knepley { 438ab6a9622SMatthew G. Knepley PetscScalar yt[2], zt[2]; 439ab6a9622SMatthew G. Knepley 440ab6a9622SMatthew G. Knepley yt[0] = y[0]; yt[1] = y[1]; 441ec277c0fSMatthew G. Knepley ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr); 442ab6a9622SMatthew G. Knepley z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); 443ab6a9622SMatthew G. Knepley } 4449b4815dcSMatthew G. Knepley break; 445ab6a9622SMatthew G. Knepley case 3: 446ab6a9622SMatthew G. Knepley { 447ab6a9622SMatthew G. Knepley PetscScalar yt[3], zt[3]; 448ab6a9622SMatthew G. Knepley 449ab6a9622SMatthew G. Knepley yt[0] = y[0]; yt[1] = y[1]; yt[2] = y[2]; 450ec277c0fSMatthew G. Knepley ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr); 451ab6a9622SMatthew G. Knepley z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); z[2] = PetscRealPart(zt[2]); 452ab6a9622SMatthew G. Knepley } 4539b4815dcSMatthew G. Knepley break; 454ab6a9622SMatthew G. Knepley } 455ab6a9622SMatthew G. Knepley #else 456ab6a9622SMatthew G. Knepley ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, y, z, ctx);CHKERRQ(ierr); 457ab6a9622SMatthew G. Knepley #endif 458ec277c0fSMatthew G. Knepley PetscFunctionReturn(0); 459ab6a9622SMatthew G. Knepley } 460ab6a9622SMatthew G. Knepley 461ec277c0fSMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx) 462ca3d3a14SMatthew G. Knepley { 463ca3d3a14SMatthew G. Knepley const PetscScalar *A; 464ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 465ca3d3a14SMatthew G. Knepley 466ca3d3a14SMatthew G. Knepley PetscFunctionBeginHot; 467ca3d3a14SMatthew G. Knepley ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr); 468ca3d3a14SMatthew G. Knepley switch (dim) { 4691fba962aSMatthew G. Knepley case 2: DMPlex_Mult2D_Internal(A, 1, y, z);break; 4701fba962aSMatthew G. Knepley case 3: DMPlex_Mult3D_Internal(A, 1, y, z);break; 471ca3d3a14SMatthew G. Knepley } 472ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 473ca3d3a14SMatthew G. Knepley } 474ca3d3a14SMatthew G. Knepley 475ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a) 476ca3d3a14SMatthew G. Knepley { 477ca3d3a14SMatthew G. Knepley PetscSection ts; 478ca3d3a14SMatthew G. Knepley const PetscScalar *ta, *tva; 479ca3d3a14SMatthew G. Knepley PetscInt dof; 480ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 481ca3d3a14SMatthew G. Knepley 482ca3d3a14SMatthew G. Knepley PetscFunctionBeginHot; 48392fd8e1eSJed Brown ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr); 484ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr); 485ca3d3a14SMatthew G. Knepley ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr); 486ca3d3a14SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, (void *) &tva);CHKERRQ(ierr); 487ca3d3a14SMatthew G. Knepley if (l2g) { 488ca3d3a14SMatthew G. Knepley switch (dof) { 4891fba962aSMatthew G. Knepley case 4: DMPlex_Mult2D_Internal(tva, 1, a, a);break; 4901fba962aSMatthew G. Knepley case 9: DMPlex_Mult3D_Internal(tva, 1, a, a);break; 491ca3d3a14SMatthew G. Knepley } 492ca3d3a14SMatthew G. Knepley } else { 493ca3d3a14SMatthew G. Knepley switch (dof) { 4941fba962aSMatthew G. Knepley case 4: DMPlex_MultTranspose2D_Internal(tva, 1, a, a);break; 4951fba962aSMatthew G. Knepley case 9: DMPlex_MultTranspose3D_Internal(tva, 1, a, a);break; 496ca3d3a14SMatthew G. Knepley } 497ca3d3a14SMatthew G. Knepley } 498ca3d3a14SMatthew G. Knepley ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr); 499ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 500ca3d3a14SMatthew G. Knepley } 501ca3d3a14SMatthew G. Knepley 502ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformFieldTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt pf, PetscInt f, PetscInt pg, PetscInt g, PetscBool l2g, PetscInt lda, PetscScalar *a) 503ca3d3a14SMatthew G. Knepley { 504ca3d3a14SMatthew G. Knepley PetscSection s, ts; 505ca3d3a14SMatthew G. Knepley const PetscScalar *ta, *tvaf, *tvag; 506ca3d3a14SMatthew G. Knepley PetscInt fdof, gdof, fpdof, gpdof; 507ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 508ca3d3a14SMatthew G. Knepley 509ca3d3a14SMatthew G. Knepley PetscFunctionBeginHot; 51092fd8e1eSJed Brown ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr); 51192fd8e1eSJed Brown ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr); 512ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr); 513ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr); 514ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr); 515ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr); 516ca3d3a14SMatthew G. Knepley ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr); 517ca3d3a14SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, (void *) &tvaf);CHKERRQ(ierr); 518ca3d3a14SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, (void *) &tvag);CHKERRQ(ierr); 519ca3d3a14SMatthew G. Knepley if (l2g) { 520ca3d3a14SMatthew G. Knepley switch (fdof) { 521ca3d3a14SMatthew G. Knepley case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break; 522ca3d3a14SMatthew G. Knepley case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break; 523ca3d3a14SMatthew G. Knepley } 524ca3d3a14SMatthew G. Knepley switch (gdof) { 525ca3d3a14SMatthew G. Knepley case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break; 526ca3d3a14SMatthew G. Knepley case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break; 527ca3d3a14SMatthew G. Knepley } 528ca3d3a14SMatthew G. Knepley } else { 529ca3d3a14SMatthew G. Knepley switch (fdof) { 530ca3d3a14SMatthew G. Knepley case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break; 531ca3d3a14SMatthew G. Knepley case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break; 532ca3d3a14SMatthew G. Knepley } 533ca3d3a14SMatthew G. Knepley switch (gdof) { 534ca3d3a14SMatthew G. Knepley case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break; 535ca3d3a14SMatthew G. Knepley case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break; 536ca3d3a14SMatthew G. Knepley } 537ca3d3a14SMatthew G. Knepley } 538ca3d3a14SMatthew G. Knepley ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr); 539ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 540ca3d3a14SMatthew G. Knepley } 541ca3d3a14SMatthew G. Knepley 542ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a) 543ca3d3a14SMatthew G. Knepley { 544ca3d3a14SMatthew G. Knepley PetscSection s; 545ca3d3a14SMatthew G. Knepley PetscSection clSection; 546ca3d3a14SMatthew G. Knepley IS clPoints; 547ca3d3a14SMatthew G. Knepley const PetscInt *clp; 548ca3d3a14SMatthew G. Knepley PetscInt *points = NULL; 549ca3d3a14SMatthew G. Knepley PetscInt Nf, f, Np, cp, dof, d = 0; 550ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 551ca3d3a14SMatthew G. Knepley 552ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 55392fd8e1eSJed Brown ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr); 554ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 555ca3d3a14SMatthew G. Knepley ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr); 556ca3d3a14SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 557ca3d3a14SMatthew G. Knepley for (cp = 0; cp < Np*2; cp += 2) { 558ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr); 559ca3d3a14SMatthew G. Knepley if (!dof) continue; 560ca3d3a14SMatthew G. Knepley if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);} 561ca3d3a14SMatthew G. Knepley d += dof; 562ca3d3a14SMatthew G. Knepley } 563ca3d3a14SMatthew G. Knepley } 564ca3d3a14SMatthew G. Knepley ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr); 565ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 566ca3d3a14SMatthew G. Knepley } 567ca3d3a14SMatthew G. Knepley 568ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a) 569ca3d3a14SMatthew G. Knepley { 570ca3d3a14SMatthew G. Knepley PetscSection s; 571ca3d3a14SMatthew G. Knepley PetscSection clSection; 572ca3d3a14SMatthew G. Knepley IS clPoints; 573ca3d3a14SMatthew G. Knepley const PetscInt *clp; 574ca3d3a14SMatthew G. Knepley PetscInt *points = NULL; 5758bdb3c71SMatthew G. Knepley PetscInt Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c = 0; 576ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 577ca3d3a14SMatthew G. Knepley 578ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 57992fd8e1eSJed Brown ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr); 580ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 581ca3d3a14SMatthew G. Knepley ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr); 582ca3d3a14SMatthew G. Knepley for (f = 0, r = 0; f < Nf; ++f) { 583ca3d3a14SMatthew G. Knepley for (cpf = 0; cpf < Np*2; cpf += 2) { 584ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr); 585ca3d3a14SMatthew G. Knepley for (g = 0, c = 0; g < Nf; ++g) { 586ca3d3a14SMatthew G. Knepley for (cpg = 0; cpg < Np*2; cpg += 2) { 587ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr); 588ca3d3a14SMatthew G. Knepley ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr); 589ca3d3a14SMatthew G. Knepley c += gdof; 590ca3d3a14SMatthew G. Knepley } 591ca3d3a14SMatthew G. Knepley } 592ca3d3a14SMatthew G. Knepley if (c != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda); 593ca3d3a14SMatthew G. Knepley r += fdof; 594ca3d3a14SMatthew G. Knepley } 595ca3d3a14SMatthew G. Knepley } 596ca3d3a14SMatthew G. Knepley if (r != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda); 597ca3d3a14SMatthew G. Knepley ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr); 598ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 599ca3d3a14SMatthew G. Knepley } 600ca3d3a14SMatthew G. Knepley 601ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g) 602ca3d3a14SMatthew G. Knepley { 603ca3d3a14SMatthew G. Knepley DM tdm; 604ca3d3a14SMatthew G. Knepley Vec tv; 605ca3d3a14SMatthew G. Knepley PetscSection ts, s; 606ca3d3a14SMatthew G. Knepley const PetscScalar *ta; 607ca3d3a14SMatthew G. Knepley PetscScalar *a, *va; 608ca3d3a14SMatthew G. Knepley PetscInt pStart, pEnd, p, Nf, f; 609ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 610ca3d3a14SMatthew G. Knepley 611ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 612ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 613ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 61492fd8e1eSJed Brown ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr); 61592fd8e1eSJed Brown ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr); 616ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr); 617ca3d3a14SMatthew G. Knepley ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr); 618ca3d3a14SMatthew G. Knepley ierr = VecGetArray(lv, &a);CHKERRQ(ierr); 619ca3d3a14SMatthew G. Knepley ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr); 620ca3d3a14SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 621ca3d3a14SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 622ca3d3a14SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, p, f, a, (void *) &va);CHKERRQ(ierr); 623ca3d3a14SMatthew G. Knepley ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr); 624ca3d3a14SMatthew G. Knepley } 625ca3d3a14SMatthew G. Knepley } 626ca3d3a14SMatthew G. Knepley ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr); 627ca3d3a14SMatthew G. Knepley ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr); 628ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 629ca3d3a14SMatthew G. Knepley } 630ca3d3a14SMatthew G. Knepley 631ca3d3a14SMatthew G. Knepley /*@ 632ca3d3a14SMatthew G. Knepley DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis 633ca3d3a14SMatthew G. Knepley 634ca3d3a14SMatthew G. Knepley Input Parameters: 635ca3d3a14SMatthew G. Knepley + dm - The DM 636ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis 637ca3d3a14SMatthew G. Knepley 638ca3d3a14SMatthew G. Knepley Output Parameters: 639ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis 640ca3d3a14SMatthew G. Knepley 641c0f8e1fdSMatthew G. Knepley Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user will have a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal. 642c0f8e1fdSMatthew G. Knepley 643ca3d3a14SMatthew G. Knepley Level: developer 644ca3d3a14SMatthew G. Knepley 645436bc73aSJed Brown .seealso: DMPlexLocalToGlobalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation() 646ca3d3a14SMatthew G. Knepley @*/ 647ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv) 648ca3d3a14SMatthew G. Knepley { 649ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 650ca3d3a14SMatthew G. Knepley 651ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 652ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 653ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(lv, VEC_CLASSID, 2); 654ca3d3a14SMatthew G. Knepley ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr); 655ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 656ca3d3a14SMatthew G. Knepley } 657ca3d3a14SMatthew G. Knepley 658ca3d3a14SMatthew G. Knepley /*@ 659ca3d3a14SMatthew G. Knepley DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis 660ca3d3a14SMatthew G. Knepley 661ca3d3a14SMatthew G. Knepley Input Parameters: 662ca3d3a14SMatthew G. Knepley + dm - The DM 663ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis 664ca3d3a14SMatthew G. Knepley 665ca3d3a14SMatthew G. Knepley Output Parameters: 666ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis 667ca3d3a14SMatthew G. Knepley 668c0f8e1fdSMatthew G. Knepley Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user would want a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal. 669c0f8e1fdSMatthew G. Knepley 670ca3d3a14SMatthew G. Knepley Level: developer 671ca3d3a14SMatthew G. Knepley 672436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation() 673ca3d3a14SMatthew G. Knepley @*/ 674ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv) 675ca3d3a14SMatthew G. Knepley { 676ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 677ca3d3a14SMatthew G. Knepley 678ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 679ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 680ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(lv, VEC_CLASSID, 2); 681ca3d3a14SMatthew G. Knepley ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr); 682ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 683ca3d3a14SMatthew G. Knepley } 684ca3d3a14SMatthew G. Knepley 685ca3d3a14SMatthew G. Knepley /*@ 686ca3d3a14SMatthew G. Knepley DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions 687ca3d3a14SMatthew G. Knepley and global solutions, to a local basis, appropriate for discretization integrals and assembly. 688ca3d3a14SMatthew G. Knepley 689ca3d3a14SMatthew G. Knepley Input Parameters: 690ca3d3a14SMatthew G. Knepley + dm - The DM 691ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one 692ca3d3a14SMatthew G. Knepley . beta - The second Euler angle 693f0fc11ceSJed Brown - gamma - The third Euler angle 694ca3d3a14SMatthew G. Knepley 695ca3d3a14SMatthew G. Knepley Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that 696ca3d3a14SMatthew G. Knepley we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows: 697ca3d3a14SMatthew G. Knepley $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis. 698ca3d3a14SMatthew G. Knepley $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis. 699ca3d3a14SMatthew G. Knepley $ The XYZ system rotates a third time about the z axis by gamma. 700ca3d3a14SMatthew G. Knepley 701ca3d3a14SMatthew G. Knepley Level: developer 702ca3d3a14SMatthew G. Knepley 703ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis() 704ca3d3a14SMatthew G. Knepley @*/ 705ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma) 706ca3d3a14SMatthew G. Knepley { 707ca3d3a14SMatthew G. Knepley RotCtx *rc; 708ca3d3a14SMatthew G. Knepley PetscInt cdim; 709ca3d3a14SMatthew G. Knepley PetscErrorCode ierr; 710ca3d3a14SMatthew G. Knepley 711ca3d3a14SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr); 712ca3d3a14SMatthew G. Knepley ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr); 713ca3d3a14SMatthew G. Knepley dm->transformCtx = rc; 714ca3d3a14SMatthew G. Knepley dm->transformSetUp = DMPlexBasisTransformSetUp_Rotation_Internal; 715ca3d3a14SMatthew G. Knepley dm->transformDestroy = DMPlexBasisTransformDestroy_Rotation_Internal; 716ca3d3a14SMatthew G. Knepley dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal; 717ca3d3a14SMatthew G. Knepley rc->dim = cdim; 718ca3d3a14SMatthew G. Knepley rc->alpha = alpha; 719ca3d3a14SMatthew G. Knepley rc->beta = beta; 720ca3d3a14SMatthew G. Knepley rc->gamma = gamma; 721ca3d3a14SMatthew G. Knepley ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr); 722ca3d3a14SMatthew G. Knepley ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr); 723ca3d3a14SMatthew G. Knepley PetscFunctionReturn(0); 724ca3d3a14SMatthew G. Knepley } 725ca3d3a14SMatthew G. Knepley 726b278463cSMatthew G. Knepley /*@C 727ece3a9fcSMatthew G. Knepley DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector using a function of the coordinates 728b278463cSMatthew G. Knepley 729b278463cSMatthew G. Knepley Input Parameters: 730b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 731b278463cSMatthew G. Knepley . time - The time 732b278463cSMatthew G. Knepley . field - The field to constrain 7331c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 7341c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 735b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 736b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 737b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 738b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 739b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 740b278463cSMatthew G. Knepley 741b278463cSMatthew G. Knepley Output Parameter: 742b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 743b278463cSMatthew G. Knepley 744b278463cSMatthew G. Knepley Level: developer 745b278463cSMatthew G. Knepley 746ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary() 747b278463cSMatthew G. Knepley @*/ 7481c531cf8SMatthew 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) 74955f2e967SMatthew G. Knepley { 7500163fd50SMatthew G. Knepley PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 75155f2e967SMatthew G. Knepley void **ctxs; 752d7ddef95SMatthew G. Knepley PetscInt numFields; 753d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 754d7ddef95SMatthew G. Knepley 755d7ddef95SMatthew G. Knepley PetscFunctionBegin; 756d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 757d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 758d7ddef95SMatthew G. Knepley funcs[field] = func; 759d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 7601c531cf8SMatthew G. Knepley ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 761d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 762d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 763d7ddef95SMatthew G. Knepley } 764d7ddef95SMatthew G. Knepley 765b278463cSMatthew G. Knepley /*@C 766ece3a9fcSMatthew G. Knepley DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector using a function of the coordinates and field data 767b278463cSMatthew G. Knepley 768b278463cSMatthew G. Knepley Input Parameters: 769b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 770b278463cSMatthew G. Knepley . time - The time 771b278463cSMatthew G. Knepley . locU - A local vector with the input solution values 772b278463cSMatthew G. Knepley . field - The field to constrain 7731c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 7741c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 775b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 776b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 777b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 778b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 779b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 780b278463cSMatthew G. Knepley 781b278463cSMatthew G. Knepley Output Parameter: 782b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 783b278463cSMatthew G. Knepley 784b278463cSMatthew G. Knepley Level: developer 785b278463cSMatthew G. Knepley 786ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary() 787b278463cSMatthew G. Knepley @*/ 7881c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], 789c60e475cSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 790c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 791c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 79297b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], 793b278463cSMatthew G. Knepley PetscScalar[]), 794b278463cSMatthew G. Knepley void *ctx, Vec locX) 795c60e475cSMatthew G. Knepley { 796c60e475cSMatthew G. Knepley void (**funcs)(PetscInt, PetscInt, PetscInt, 797c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 798c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 79997b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]); 800c60e475cSMatthew G. Knepley void **ctxs; 801c60e475cSMatthew G. Knepley PetscInt numFields; 802c60e475cSMatthew G. Knepley PetscErrorCode ierr; 803c60e475cSMatthew G. Knepley 804c60e475cSMatthew G. Knepley PetscFunctionBegin; 805c60e475cSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 806c60e475cSMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 807c60e475cSMatthew G. Knepley funcs[field] = func; 808c60e475cSMatthew G. Knepley ctxs[field] = ctx; 8091c531cf8SMatthew G. Knepley ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 810c60e475cSMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 811c60e475cSMatthew G. Knepley PetscFunctionReturn(0); 812c60e475cSMatthew G. Knepley } 813c60e475cSMatthew G. Knepley 814b278463cSMatthew G. Knepley /*@C 815ece3a9fcSMatthew G. Knepley DMPlexInsertBoundaryValuesEssentialBdField - Insert boundary values into a local vector using a function of the coodinates and boundary field data 816ece3a9fcSMatthew G. Knepley 817ece3a9fcSMatthew G. Knepley Collective on dm 818ece3a9fcSMatthew G. Knepley 819ece3a9fcSMatthew G. Knepley Input Parameters: 820ece3a9fcSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 821ece3a9fcSMatthew G. Knepley . time - The time 822ece3a9fcSMatthew G. Knepley . locU - A local vector with the input solution values 823ece3a9fcSMatthew G. Knepley . field - The field to constrain 824ece3a9fcSMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 825ece3a9fcSMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 826ece3a9fcSMatthew G. Knepley . label - The DMLabel defining constrained points 827ece3a9fcSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 828ece3a9fcSMatthew G. Knepley . ids - An array of ids for constrained points 829ece3a9fcSMatthew G. Knepley . func - A pointwise function giving boundary values, the calling sequence is given in DMProjectBdFieldLabelLocal() 830ece3a9fcSMatthew G. Knepley - ctx - An optional user context for bcFunc 831ece3a9fcSMatthew G. Knepley 832ece3a9fcSMatthew G. Knepley Output Parameter: 833ece3a9fcSMatthew G. Knepley . locX - A local vector to receive the boundary values 834ece3a9fcSMatthew G. Knepley 835ece3a9fcSMatthew G. Knepley Level: developer 836ece3a9fcSMatthew G. Knepley 837ece3a9fcSMatthew G. Knepley .seealso: DMProjectBdFieldLabelLocal(), DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 838ece3a9fcSMatthew G. Knepley @*/ 839ece3a9fcSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialBdField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], 840ece3a9fcSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 841ece3a9fcSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 842ece3a9fcSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 843ece3a9fcSMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], 844ece3a9fcSMatthew G. Knepley PetscScalar[]), 845ece3a9fcSMatthew G. Knepley void *ctx, Vec locX) 846ece3a9fcSMatthew G. Knepley { 847ece3a9fcSMatthew G. Knepley void (**funcs)(PetscInt, PetscInt, PetscInt, 848ece3a9fcSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 849ece3a9fcSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 850ece3a9fcSMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]); 851ece3a9fcSMatthew G. Knepley void **ctxs; 852ece3a9fcSMatthew G. Knepley PetscInt numFields; 853ece3a9fcSMatthew G. Knepley PetscErrorCode ierr; 854ece3a9fcSMatthew G. Knepley 855ece3a9fcSMatthew G. Knepley PetscFunctionBegin; 856ece3a9fcSMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 857ece3a9fcSMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 858ece3a9fcSMatthew G. Knepley funcs[field] = func; 859ece3a9fcSMatthew G. Knepley ctxs[field] = ctx; 860ece3a9fcSMatthew G. Knepley ierr = DMProjectBdFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 861ece3a9fcSMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 862ece3a9fcSMatthew G. Knepley PetscFunctionReturn(0); 863ece3a9fcSMatthew G. Knepley } 864ece3a9fcSMatthew G. Knepley 865ece3a9fcSMatthew G. Knepley /*@C 866b278463cSMatthew G. Knepley DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector 867b278463cSMatthew G. Knepley 868b278463cSMatthew G. Knepley Input Parameters: 869b278463cSMatthew G. Knepley + dm - The DM, with a PetscDS that matches the problem being constrained 870b278463cSMatthew G. Knepley . time - The time 871b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information 872b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information 873b278463cSMatthew G. Knepley . Grad - A vector with the FVM cell gradient information 874b278463cSMatthew G. Knepley . field - The field to constrain 8751c531cf8SMatthew G. Knepley . Nc - The number of constrained field components, or 0 for all components 8761c531cf8SMatthew G. Knepley . comps - An array of constrained component numbers, or NULL for all components 877b278463cSMatthew G. Knepley . label - The DMLabel defining constrained points 878b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points 879b278463cSMatthew G. Knepley . ids - An array of ids for constrained points 880b278463cSMatthew G. Knepley . func - A pointwise function giving boundary values 881b278463cSMatthew G. Knepley - ctx - An optional user context for bcFunc 882b278463cSMatthew G. Knepley 883b278463cSMatthew G. Knepley Output Parameter: 884b278463cSMatthew G. Knepley . locX - A local vector to receives the boundary values 885b278463cSMatthew G. Knepley 886b278463cSMatthew G. Knepley Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary() 887b278463cSMatthew G. Knepley 888b278463cSMatthew G. Knepley Level: developer 889b278463cSMatthew G. Knepley 890b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary() 891b278463cSMatthew G. Knepley @*/ 8921c531cf8SMatthew 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[], 893b278463cSMatthew G. Knepley PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX) 894d7ddef95SMatthew G. Knepley { 89561f58d28SMatthew G. Knepley PetscDS prob; 896da97024aSMatthew G. Knepley PetscSF sf; 897d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 89820369375SToby Isaac const PetscScalar *facegeom, *cellgeom = NULL, *grad; 899da97024aSMatthew G. Knepley const PetscInt *leaves; 900d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 901520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 902e735a8a9SMatthew G. Knepley PetscErrorCode ierr, ierru = 0; 903d7ddef95SMatthew G. Knepley 904d7ddef95SMatthew G. Knepley PetscFunctionBegin; 905da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 906da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 907da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 908d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 909d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 91061f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 911d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 912d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 91320369375SToby Isaac if (cellGeometry) { 914d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 915d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 91620369375SToby Isaac } 917d7ddef95SMatthew G. Knepley if (Grad) { 918c0a6632aSMatthew G. Knepley PetscFV fv; 919c0a6632aSMatthew G. Knepley 920c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 921d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 922d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 923d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 92469291d52SBarry Smith ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 925d7ddef95SMatthew G. Knepley } 926d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 927d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 928d7ddef95SMatthew G. Knepley IS faceIS; 929d7ddef95SMatthew G. Knepley const PetscInt *faces; 930d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 931d7ddef95SMatthew G. Knepley 932d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 933d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 934d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 935d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 936d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 937d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 938640bce14SSatish Balay PetscFVFaceGeom *fg; 939d7ddef95SMatthew G. Knepley 940d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 941da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 942da97024aSMatthew G. Knepley if (loc >= 0) continue; 943d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 944d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 945d7ddef95SMatthew G. Knepley if (Grad) { 946640bce14SSatish Balay PetscFVCellGeom *cg; 947640bce14SSatish Balay PetscScalar *cx, *cgrad; 948d7ddef95SMatthew G. Knepley PetscScalar *xG; 949d7ddef95SMatthew G. Knepley PetscReal dx[3]; 950d7ddef95SMatthew G. Knepley PetscInt d; 951d7ddef95SMatthew G. Knepley 952d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 953d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 954d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 955520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 956d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 957d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 958e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx); 959e735a8a9SMatthew G. Knepley if (ierru) { 960e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 961e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 962e735a8a9SMatthew G. Knepley goto cleanup; 963e735a8a9SMatthew G. Knepley } 964d7ddef95SMatthew G. Knepley } else { 965640bce14SSatish Balay PetscScalar *xI; 966d7ddef95SMatthew G. Knepley PetscScalar *xG; 967d7ddef95SMatthew G. Knepley 968d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 969520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 970e735a8a9SMatthew G. Knepley ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx); 971e735a8a9SMatthew G. Knepley if (ierru) { 972e735a8a9SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 973e735a8a9SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 974e735a8a9SMatthew G. Knepley goto cleanup; 975e735a8a9SMatthew G. Knepley } 976d7ddef95SMatthew G. Knepley } 977d7ddef95SMatthew G. Knepley } 978d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 979d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 980d7ddef95SMatthew G. Knepley } 981e735a8a9SMatthew G. Knepley cleanup: 982d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 983d7ddef95SMatthew G. Knepley if (Grad) { 98469291d52SBarry Smith ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr); 985d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 986d7ddef95SMatthew G. Knepley } 987e735a8a9SMatthew G. Knepley if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);} 988d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 989e735a8a9SMatthew G. Knepley CHKERRQ(ierru); 990d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 991d7ddef95SMatthew G. Knepley } 992d7ddef95SMatthew G. Knepley 9930c364540SMatthew G. Knepley static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 9940c364540SMatthew G. Knepley { 9950c364540SMatthew G. Knepley PetscInt c; 9960c364540SMatthew G. Knepley for (c = 0; c < Nc; ++c) u[c] = 0.0; 9970c364540SMatthew G. Knepley return 0; 9980c364540SMatthew G. Knepley } 9990c364540SMatthew G. Knepley 1000f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 1001d7ddef95SMatthew G. Knepley { 10020c364540SMatthew G. Knepley PetscObject isZero; 1003e5e52638SMatthew G. Knepley PetscDS prob; 1004d7ddef95SMatthew G. Knepley PetscInt numBd, b; 100555f2e967SMatthew G. Knepley PetscErrorCode ierr; 100655f2e967SMatthew G. Knepley 100755f2e967SMatthew G. Knepley PetscFunctionBegin; 1008e5e52638SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1009e5e52638SMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 10100c364540SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr); 101155f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 1012f971fd6bSMatthew G. Knepley DMBoundaryConditionType type; 1013648eda8cSMatthew G. Knepley const char *name, *labelname; 1014d7ddef95SMatthew G. Knepley DMLabel label; 10151c531cf8SMatthew G. Knepley PetscInt field, Nc; 10161c531cf8SMatthew G. Knepley const PetscInt *comps; 1017d7ddef95SMatthew G. Knepley PetscObject obj; 1018d7ddef95SMatthew G. Knepley PetscClassId id; 1019a30ec4eaSSatish Balay void (*func)(void); 1020d7ddef95SMatthew G. Knepley PetscInt numids; 1021d7ddef95SMatthew G. Knepley const PetscInt *ids; 102255f2e967SMatthew G. Knepley void *ctx; 102355f2e967SMatthew G. Knepley 1024*56cf3b9cSMatthew G. Knepley ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, &func, NULL, &numids, &ids, &ctx);CHKERRQ(ierr); 1025f971fd6bSMatthew G. Knepley if (insertEssential != (type & DM_BC_ESSENTIAL)) continue; 1026c58f1c22SToby Isaac ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 1027648eda8cSMatthew G. Knepley if (!label) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "Label %s for boundary condition %s does not exist in the DM", labelname, name); 102844a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 1029d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1030d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1031c60e475cSMatthew G. Knepley switch (type) { 1032c60e475cSMatthew G. Knepley /* for FEM, there is no insertion to be done for non-essential boundary conditions */ 1033c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL: 10340c364540SMatthew G. Knepley if (isZero) func = (void (*)(void)) zero; 1035092e5057SToby Isaac ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 10361c531cf8SMatthew 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); 1037092e5057SToby Isaac ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 1038c60e475cSMatthew G. Knepley break; 1039c60e475cSMatthew G. Knepley case DM_BC_ESSENTIAL_FIELD: 1040c60e475cSMatthew G. Knepley ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 10411c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, 1042b278463cSMatthew G. Knepley (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1043c60e475cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 104497b6e6e8SMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr); 1045c60e475cSMatthew G. Knepley ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 1046c60e475cSMatthew G. Knepley break; 1047c60e475cSMatthew G. Knepley default: break; 1048c60e475cSMatthew G. Knepley } 1049d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 105043ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 10511c531cf8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids, 1052b278463cSMatthew G. Knepley (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 1053ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 105455f2e967SMatthew G. Knepley } 105555f2e967SMatthew G. Knepley PetscFunctionReturn(0); 105655f2e967SMatthew G. Knepley } 105755f2e967SMatthew G. Knepley 1058*56cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 1059*56cf3b9cSMatthew G. Knepley { 1060*56cf3b9cSMatthew G. Knepley PetscObject isZero; 1061*56cf3b9cSMatthew G. Knepley PetscDS prob; 1062*56cf3b9cSMatthew G. Knepley PetscInt numBd, b; 1063*56cf3b9cSMatthew G. Knepley PetscErrorCode ierr; 1064*56cf3b9cSMatthew G. Knepley 1065*56cf3b9cSMatthew G. Knepley PetscFunctionBegin; 1066*56cf3b9cSMatthew G. Knepley if (!locX) PetscFunctionReturn(0); 1067*56cf3b9cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1068*56cf3b9cSMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 1069*56cf3b9cSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr); 1070*56cf3b9cSMatthew G. Knepley for (b = 0; b < numBd; ++b) { 1071*56cf3b9cSMatthew G. Knepley DMBoundaryConditionType type; 1072*56cf3b9cSMatthew G. Knepley const char *name, *labelname; 1073*56cf3b9cSMatthew G. Knepley DMLabel label; 1074*56cf3b9cSMatthew G. Knepley PetscInt field, Nc; 1075*56cf3b9cSMatthew G. Knepley const PetscInt *comps; 1076*56cf3b9cSMatthew G. Knepley PetscObject obj; 1077*56cf3b9cSMatthew G. Knepley PetscClassId id; 1078*56cf3b9cSMatthew G. Knepley void (*func_t)(void); 1079*56cf3b9cSMatthew G. Knepley PetscInt numids; 1080*56cf3b9cSMatthew G. Knepley const PetscInt *ids; 1081*56cf3b9cSMatthew G. Knepley void *ctx; 1082*56cf3b9cSMatthew G. Knepley 1083*56cf3b9cSMatthew G. Knepley ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, NULL, &func_t, &numids, &ids, &ctx);CHKERRQ(ierr); 1084*56cf3b9cSMatthew G. Knepley if (!func_t) continue; 1085*56cf3b9cSMatthew G. Knepley if (insertEssential != (type & DM_BC_ESSENTIAL)) continue; 1086*56cf3b9cSMatthew G. Knepley ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 1087*56cf3b9cSMatthew G. Knepley if (!label) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "Label %s for boundary condition %s does not exist in the DM", labelname, name); 1088*56cf3b9cSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 1089*56cf3b9cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1090*56cf3b9cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1091*56cf3b9cSMatthew G. Knepley switch (type) { 1092*56cf3b9cSMatthew G. Knepley /* for FEM, there is no insertion to be done for non-essential boundary conditions */ 1093*56cf3b9cSMatthew G. Knepley case DM_BC_ESSENTIAL: 1094*56cf3b9cSMatthew G. Knepley if (isZero) func_t = (void (*)(void)) zero; 1095*56cf3b9cSMatthew G. Knepley ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 1096*56cf3b9cSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func_t, ctx, locX);CHKERRQ(ierr); 1097*56cf3b9cSMatthew G. Knepley ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 1098*56cf3b9cSMatthew G. Knepley break; 1099*56cf3b9cSMatthew G. Knepley case DM_BC_ESSENTIAL_FIELD: 1100*56cf3b9cSMatthew G. Knepley ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr); 1101*56cf3b9cSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, 1102*56cf3b9cSMatthew G. Knepley (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1103*56cf3b9cSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 1104*56cf3b9cSMatthew G. Knepley PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func_t, ctx, locX);CHKERRQ(ierr); 1105*56cf3b9cSMatthew G. Knepley ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr); 1106*56cf3b9cSMatthew G. Knepley break; 1107*56cf3b9cSMatthew G. Knepley default: break; 1108*56cf3b9cSMatthew G. Knepley } 1109*56cf3b9cSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 1110*56cf3b9cSMatthew G. Knepley } 1111*56cf3b9cSMatthew G. Knepley PetscFunctionReturn(0); 1112*56cf3b9cSMatthew G. Knepley } 1113*56cf3b9cSMatthew G. Knepley 1114f1d73a7aSMatthew G. Knepley /*@ 1115f1d73a7aSMatthew G. Knepley DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector 1116f1d73a7aSMatthew G. Knepley 1117f1d73a7aSMatthew G. Knepley Input Parameters: 1118f1d73a7aSMatthew G. Knepley + dm - The DM 1119f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions 1120f1d73a7aSMatthew G. Knepley . time - The time 1121f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations 1122f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations 1123f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations 1124f1d73a7aSMatthew G. Knepley 1125f1d73a7aSMatthew G. Knepley Output Parameters: 1126f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values 1127f1d73a7aSMatthew G. Knepley 1128f1d73a7aSMatthew G. Knepley Level: developer 1129f1d73a7aSMatthew G. Knepley 1130f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal() 1131f1d73a7aSMatthew G. Knepley @*/ 1132f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 1133f1d73a7aSMatthew G. Knepley { 1134f1d73a7aSMatthew G. Knepley PetscErrorCode ierr; 1135f1d73a7aSMatthew G. Knepley 1136f1d73a7aSMatthew G. Knepley PetscFunctionBegin; 1137f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1138f1d73a7aSMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 1139f1d73a7aSMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 1140f1d73a7aSMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 1141f1d73a7aSMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 1142f1d73a7aSMatthew G. Knepley ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr); 1143f1d73a7aSMatthew G. Knepley PetscFunctionReturn(0); 1144f1d73a7aSMatthew G. Knepley } 1145f1d73a7aSMatthew G. Knepley 1146*56cf3b9cSMatthew G. Knepley /*@ 1147*56cf3b9cSMatthew G. Knepley DMPlexInsertTimeDerivativeBoundaryValues - Puts coefficients which represent boundary values of the time derviative into the local solution vector 1148*56cf3b9cSMatthew G. Knepley 1149*56cf3b9cSMatthew G. Knepley Input Parameters: 1150*56cf3b9cSMatthew G. Knepley + dm - The DM 1151*56cf3b9cSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions 1152*56cf3b9cSMatthew G. Knepley . time - The time 1153*56cf3b9cSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations 1154*56cf3b9cSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations 1155*56cf3b9cSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations 1156*56cf3b9cSMatthew G. Knepley 1157*56cf3b9cSMatthew G. Knepley Output Parameters: 1158*56cf3b9cSMatthew G. Knepley . locX_t - Solution updated with boundary values 1159*56cf3b9cSMatthew G. Knepley 1160*56cf3b9cSMatthew G. Knepley Level: developer 1161*56cf3b9cSMatthew G. Knepley 1162*56cf3b9cSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal() 1163*56cf3b9cSMatthew G. Knepley @*/ 1164*56cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues(DM dm, PetscBool insertEssential, Vec locX_t, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 1165*56cf3b9cSMatthew G. Knepley { 1166*56cf3b9cSMatthew G. Knepley PetscErrorCode ierr; 1167*56cf3b9cSMatthew G. Knepley 1168*56cf3b9cSMatthew G. Knepley PetscFunctionBegin; 1169*56cf3b9cSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1170*56cf3b9cSMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 2);} 1171*56cf3b9cSMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 1172*56cf3b9cSMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 1173*56cf3b9cSMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 1174*56cf3b9cSMatthew G. Knepley ierr = PetscTryMethod(dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX_t,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr); 1175*56cf3b9cSMatthew G. Knepley PetscFunctionReturn(0); 1176*56cf3b9cSMatthew G. Knepley } 1177*56cf3b9cSMatthew G. Knepley 11780709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 1179cb1e1211SMatthew G Knepley { 1180574a98acSMatthew G. Knepley Vec localX; 1181574a98acSMatthew G. Knepley PetscErrorCode ierr; 1182574a98acSMatthew G. Knepley 1183574a98acSMatthew G. Knepley PetscFunctionBegin; 1184574a98acSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 11855d42b983SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr); 1186574a98acSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1187574a98acSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1188574a98acSMatthew G. Knepley ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr); 1189574a98acSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1190574a98acSMatthew G. Knepley PetscFunctionReturn(0); 1191574a98acSMatthew G. Knepley } 1192574a98acSMatthew G. Knepley 1193574a98acSMatthew G. Knepley /*@C 1194ca3d3a14SMatthew G. Knepley DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 1195574a98acSMatthew G. Knepley 1196d083f849SBarry Smith Collective on dm 1197c0f8e1fdSMatthew G. Knepley 1198574a98acSMatthew G. Knepley Input Parameters: 1199574a98acSMatthew G. Knepley + dm - The DM 1200574a98acSMatthew G. Knepley . time - The time 1201574a98acSMatthew G. Knepley . funcs - The functions to evaluate for each field component 1202574a98acSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 1203574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector 1204574a98acSMatthew G. Knepley 1205574a98acSMatthew G. Knepley Output Parameter: 1206574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2 1207574a98acSMatthew G. Knepley 1208574a98acSMatthew G. Knepley Level: developer 1209574a98acSMatthew G. Knepley 1210574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff() 1211574a98acSMatthew G. Knepley @*/ 1212574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff) 1213574a98acSMatthew G. Knepley { 12140f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 1215ca3d3a14SMatthew G. Knepley DM tdm; 1216ca3d3a14SMatthew G. Knepley Vec tv; 1217cb1e1211SMatthew G Knepley PetscSection section; 1218c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 12194bee2e38SMatthew G. Knepley PetscFEGeom fegeom; 122015496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 12214bee2e38SMatthew G. Knepley PetscReal *coords, *gcoords; 1222cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 12237318780aSToby Isaac const PetscReal *quadWeights; 1224412e9a14SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, c, field, fieldOffset; 1225ca3d3a14SMatthew G. Knepley PetscBool transform; 1226cb1e1211SMatthew G Knepley PetscErrorCode ierr; 1227cb1e1211SMatthew G Knepley 1228cb1e1211SMatthew G Knepley PetscFunctionBegin; 1229c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 12307318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 12312a4e142eSMatthew G. Knepley fegeom.dimEmbed = coordDim; 123292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 1233cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 1234ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 1235ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 1236ca3d3a14SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 1237cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 123815496722SMatthew G. Knepley PetscObject obj; 123915496722SMatthew G. Knepley PetscClassId id; 1240c5bbbd5bSMatthew G. Knepley PetscInt Nc; 1241c5bbbd5bSMatthew G. Knepley 124244a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 124315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 124415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 124515496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 124615496722SMatthew G. Knepley 12470f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 12480f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 124915496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 125015496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 125115496722SMatthew G. Knepley 125215496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 125315496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 1254ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 1255c5bbbd5bSMatthew G. Knepley numComponents += Nc; 1256cb1e1211SMatthew G Knepley } 12579c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr); 1258beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 12592a4e142eSMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr); 1260aed3cbd0SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 1261412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 1262cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 1263a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 1264cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 12659c3cf19fSMatthew G. Knepley PetscInt qc = 0; 1266cb1e1211SMatthew G Knepley 12672a4e142eSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 1268cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1269cb1e1211SMatthew G Knepley 127015496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 127115496722SMatthew G. Knepley PetscObject obj; 127215496722SMatthew G. Knepley PetscClassId id; 1273c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 127415496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 1275cb1e1211SMatthew G Knepley 127644a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 127715496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 127815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 127915496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1280ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 1281cb1e1211SMatthew G Knepley if (debug) { 1282cb1e1211SMatthew G Knepley char title[1024]; 1283ff1e0c32SBarry Smith ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr); 12844c848028SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 1285cb1e1211SMatthew G Knepley } 12867318780aSToby Isaac for (q = 0; q < Nq; ++q) { 12872a4e142eSMatthew G. Knepley PetscFEGeom qgeom; 12882a4e142eSMatthew G. Knepley 12892a4e142eSMatthew G. Knepley qgeom.dimEmbed = fegeom.dimEmbed; 12902a4e142eSMatthew G. Knepley qgeom.J = &fegeom.J[q*coordDim*coordDim]; 12912a4e142eSMatthew G. Knepley qgeom.invJ = &fegeom.invJ[q*coordDim*coordDim]; 12922a4e142eSMatthew G. Knepley qgeom.detJ = &fegeom.detJ[q]; 1293ff1e0c32SBarry Smith if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", (double)fegeom.detJ[q], c, q); 1294d3a7d86cSMatthew G. Knepley if (transform) { 1295d3a7d86cSMatthew G. Knepley gcoords = &coords[coordDim*Nq]; 1296d3a7d86cSMatthew G. Knepley ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr); 1297d3a7d86cSMatthew G. Knepley } else { 1298d3a7d86cSMatthew G. Knepley gcoords = &coords[coordDim*q]; 1299d3a7d86cSMatthew G. Knepley } 1300ca3d3a14SMatthew G. Knepley ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx); 1301e735a8a9SMatthew G. Knepley if (ierr) { 1302e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 1303e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 1304e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 13052a4e142eSMatthew G. Knepley ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2); 1306e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 1307e735a8a9SMatthew G. Knepley } 1308ca3d3a14SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);} 13092a4e142eSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);} 131015496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1311ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 131215496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1313beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 131480dbbc5dSMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %D field %D,%D point %g %g %g diff %g\n", c, field, fc, (double)(coordDim > 0 ? coords[coordDim*q] : 0.), (double)(coordDim > 1 ? coords[coordDim*q+1] : 0.),(double)(coordDim > 2 ? coords[coordDim*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);} 13154bee2e38SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]; 1316cb1e1211SMatthew G Knepley } 1317cb1e1211SMatthew G Knepley } 13189c3cf19fSMatthew G. Knepley fieldOffset += Nb; 1319beaa55a6SMatthew G. Knepley qc += Nc; 1320cb1e1211SMatthew G Knepley } 1321cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1322ff1e0c32SBarry Smith if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);} 1323cb1e1211SMatthew G Knepley localDiff += elemDiff; 1324cb1e1211SMatthew G Knepley } 13252a4e142eSMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr); 1326b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1327cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 1328cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1329cb1e1211SMatthew G Knepley } 1330cb1e1211SMatthew G Knepley 1331b698f381SToby 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) 1332cb1e1211SMatthew G Knepley { 13330f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 1334ca3d3a14SMatthew G. Knepley DM tdm; 1335cb1e1211SMatthew G Knepley PetscSection section; 133640e14135SMatthew G. Knepley PetscQuadrature quad; 1337ca3d3a14SMatthew G. Knepley Vec localX, tv; 13389c3cf19fSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 13392a4e142eSMatthew G. Knepley const PetscReal *quadWeights; 13404bee2e38SMatthew G. Knepley PetscFEGeom fegeom; 13414bee2e38SMatthew G. Knepley PetscReal *coords, *gcoords; 134240e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 1343485ad865SMatthew G. Knepley PetscInt dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset; 1344ca3d3a14SMatthew G. Knepley PetscBool transform; 1345cb1e1211SMatthew G Knepley PetscErrorCode ierr; 1346cb1e1211SMatthew G Knepley 1347cb1e1211SMatthew G Knepley PetscFunctionBegin; 1348c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 13497318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 13504bee2e38SMatthew G. Knepley fegeom.dimEmbed = coordDim; 135192fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 135240e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 135340e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 135440e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 135540e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1356ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 1357ca3d3a14SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 1358ca3d3a14SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 1359652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 13600f2d7e86SMatthew G. Knepley PetscFE fe; 136140e14135SMatthew G. Knepley PetscInt Nc; 1362652b88e8SMatthew G. Knepley 136344a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr); 13640f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 13650f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 136640e14135SMatthew G. Knepley numComponents += Nc; 1367652b88e8SMatthew G. Knepley } 13682a4e142eSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr); 1369beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 13704d6f44ffSToby Isaac /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 13714bee2e38SMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ,numComponents*coordDim,&interpolant,Nq,&fegeom.detJ);CHKERRQ(ierr); 1372412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 137340e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 137440e14135SMatthew G. Knepley PetscScalar *x = NULL; 137540e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 13769c3cf19fSMatthew G. Knepley PetscInt qc = 0; 1377652b88e8SMatthew G. Knepley 13784bee2e38SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 137940e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 138040e14135SMatthew G. Knepley 13819c3cf19fSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 13820f2d7e86SMatthew G. Knepley PetscFE fe; 138351259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 13849c3cf19fSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 138540e14135SMatthew G. Knepley 138644a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr); 13870f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 13889c3cf19fSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 138940e14135SMatthew G. Knepley if (debug) { 139040e14135SMatthew G. Knepley char title[1024]; 1391ff1e0c32SBarry Smith ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr); 13929c3cf19fSMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr); 1393652b88e8SMatthew G. Knepley } 13949c3cf19fSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 13952a4e142eSMatthew G. Knepley PetscFEGeom qgeom; 13962a4e142eSMatthew G. Knepley 13972a4e142eSMatthew G. Knepley qgeom.dimEmbed = fegeom.dimEmbed; 13982a4e142eSMatthew G. Knepley qgeom.J = &fegeom.J[q*coordDim*coordDim]; 13992a4e142eSMatthew G. Knepley qgeom.invJ = &fegeom.invJ[q*coordDim*coordDim]; 14002a4e142eSMatthew G. Knepley qgeom.detJ = &fegeom.detJ[q]; 1401ff1e0c32SBarry Smith if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], c, q); 1402d3a7d86cSMatthew G. Knepley if (transform) { 1403d3a7d86cSMatthew G. Knepley gcoords = &coords[coordDim*Nq]; 1404d3a7d86cSMatthew G. Knepley ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr); 1405d3a7d86cSMatthew G. Knepley } else { 1406d3a7d86cSMatthew G. Knepley gcoords = &coords[coordDim*q]; 1407d3a7d86cSMatthew G. Knepley } 14084bee2e38SMatthew G. Knepley ierr = (*funcs[field])(coordDim, time, gcoords, n, Nc, funcVal, ctx); 1409e735a8a9SMatthew G. Knepley if (ierr) { 1410e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 1411e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 1412e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 14134bee2e38SMatthew G. Knepley ierr2 = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr2); 1414e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 1415e735a8a9SMatthew G. Knepley } 1416ca3d3a14SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);} 14172a4e142eSMatthew G. Knepley ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr); 14184bee2e38SMatthew G. Knepley /* Overwrite with the dot product if the normal is given */ 14194bee2e38SMatthew G. Knepley if (n) { 14204bee2e38SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 14214bee2e38SMatthew G. Knepley PetscScalar sum = 0.0; 14224bee2e38SMatthew G. Knepley PetscInt d; 14234bee2e38SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += interpolant[fc*dim+d]*n[d]; 14244bee2e38SMatthew G. Knepley interpolant[fc] = sum; 14254bee2e38SMatthew G. Knepley } 14264bee2e38SMatthew G. Knepley } 14279c3cf19fSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1428beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 1429ff1e0c32SBarry Smith if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %D fieldDer %D,%D diff %g\n", c, field, fc, (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);} 14304bee2e38SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]; 143140e14135SMatthew G. Knepley } 143240e14135SMatthew G. Knepley } 14339c3cf19fSMatthew G. Knepley fieldOffset += Nb; 14349c3cf19fSMatthew G. Knepley qc += Nc; 143540e14135SMatthew G. Knepley } 143640e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1437ff1e0c32SBarry Smith if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);} 143840e14135SMatthew G. Knepley localDiff += elemDiff; 143940e14135SMatthew G. Knepley } 14404bee2e38SMatthew G. Knepley ierr = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr); 144140e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1442b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 144340e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 1444cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1445cb1e1211SMatthew G Knepley } 1446cb1e1211SMatthew G Knepley 1447c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 144873d901b8SMatthew G. Knepley { 14490f09c10fSMatthew G. Knepley const PetscInt debug = ((DM_Plex*)dm->data)->printL2; 1450ca3d3a14SMatthew G. Knepley DM tdm; 1451083401c6SMatthew G. Knepley DMLabel depthLabel; 145273d901b8SMatthew G. Knepley PetscSection section; 1453ca3d3a14SMatthew G. Knepley Vec localX, tv; 145473d901b8SMatthew G. Knepley PetscReal *localDiff; 1455083401c6SMatthew G. Knepley PetscInt dim, depth, dE, Nf, f, Nds, s; 1456ca3d3a14SMatthew G. Knepley PetscBool transform; 145773d901b8SMatthew G. Knepley PetscErrorCode ierr; 145873d901b8SMatthew G. Knepley 145973d901b8SMatthew G. Knepley PetscFunctionBegin; 1460c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1461083401c6SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr); 146292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 146373d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 1464083401c6SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 1465083401c6SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 1466083401c6SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 1467083401c6SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 1468083401c6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1469083401c6SMatthew G. Knepley ierr = DMLabelGetNumValues(depthLabel, &depth);CHKERRQ(ierr); 1470083401c6SMatthew G. Knepley 1471ca3d3a14SMatthew G. Knepley ierr = VecSet(localX, 0.0);CHKERRQ(ierr); 147273d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 147373d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1474ca3d3a14SMatthew G. Knepley ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 1475083401c6SMatthew G. Knepley ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr); 1476083401c6SMatthew G. Knepley ierr = PetscCalloc1(Nf, &localDiff);CHKERRQ(ierr); 1477083401c6SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 1478083401c6SMatthew G. Knepley PetscDS ds; 1479083401c6SMatthew G. Knepley DMLabel label; 1480083401c6SMatthew G. Knepley IS fieldIS, pointIS; 1481083401c6SMatthew G. Knepley const PetscInt *fields, *points = NULL; 1482083401c6SMatthew G. Knepley PetscQuadrature quad; 1483083401c6SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 1484083401c6SMatthew G. Knepley PetscFEGeom fegeom; 1485083401c6SMatthew G. Knepley PetscReal *coords, *gcoords; 1486083401c6SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 148796959cd1SMatthew G. Knepley PetscBool isHybrid; 1488083401c6SMatthew G. Knepley PetscInt qNc, Nq, totNc, cStart = 0, cEnd, c, dsNf; 148973d901b8SMatthew G. Knepley 1490083401c6SMatthew G. Knepley ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr); 1491083401c6SMatthew G. Knepley ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr); 149296959cd1SMatthew G. Knepley ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr); 1493083401c6SMatthew G. Knepley ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr); 1494083401c6SMatthew G. Knepley ierr = PetscDSGetTotalComponents(ds, &totNc);CHKERRQ(ierr); 1495083401c6SMatthew G. Knepley ierr = PetscDSGetQuadrature(ds, &quad);CHKERRQ(ierr); 14969c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 1497083401c6SMatthew G. Knepley if ((qNc != 1) && (qNc != totNc)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, totNc); 1498083401c6SMatthew G. Knepley ierr = PetscCalloc6(totNc, &funcVal, totNc, &interpolant, dE*(Nq+1), &coords,Nq, &fegeom.detJ, dE*dE*Nq, &fegeom.J, dE*dE*Nq, &fegeom.invJ);CHKERRQ(ierr); 1499083401c6SMatthew G. Knepley if (!label) { 1500412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1501083401c6SMatthew G. Knepley } else { 1502083401c6SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 1503083401c6SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &cEnd);CHKERRQ(ierr); 1504083401c6SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 1505083401c6SMatthew G. Knepley } 150673d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1507083401c6SMatthew G. Knepley const PetscInt cell = points ? points[c] : c; 150873d901b8SMatthew G. Knepley PetscScalar *x = NULL; 150996959cd1SMatthew G. Knepley PetscInt qc = 0, fOff = 0, dep, fStart = isHybrid ? dsNf-1 : 0; 151073d901b8SMatthew G. Knepley 1511083401c6SMatthew G. Knepley ierr = DMLabelGetValue(depthLabel, cell, &dep);CHKERRQ(ierr); 1512083401c6SMatthew G. Knepley if (dep != depth-1) continue; 151396959cd1SMatthew G. Knepley if (isHybrid) { 151496959cd1SMatthew G. Knepley const PetscInt *cone; 151596959cd1SMatthew G. Knepley 151696959cd1SMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr); 151796959cd1SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, cone[0], quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 151896959cd1SMatthew G. Knepley } else { 1519083401c6SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 152096959cd1SMatthew G. Knepley } 1521083401c6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr); 152296959cd1SMatthew G. Knepley for (f = fStart; f < dsNf; ++f) { 152315496722SMatthew G. Knepley PetscObject obj; 152415496722SMatthew G. Knepley PetscClassId id; 1525083401c6SMatthew G. Knepley void * const ctx = ctxs ? ctxs[fields[f]] : NULL; 152615496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 152715496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 152815496722SMatthew G. Knepley 1529083401c6SMatthew G. Knepley ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr); 153015496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 153115496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 153215496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1533083401c6SMatthew G. Knepley else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]); 153473d901b8SMatthew G. Knepley if (debug) { 153573d901b8SMatthew G. Knepley char title[1024]; 1536083401c6SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", fields[f]);CHKERRQ(ierr); 1537083401c6SMatthew G. Knepley ierr = DMPrintCellVector(cell, title, Nb, &x[fOff]);CHKERRQ(ierr); 153873d901b8SMatthew G. Knepley } 15397318780aSToby Isaac for (q = 0; q < Nq; ++q) { 15402a4e142eSMatthew G. Knepley PetscFEGeom qgeom; 15412a4e142eSMatthew G. Knepley 15422a4e142eSMatthew G. Knepley qgeom.dimEmbed = fegeom.dimEmbed; 1543083401c6SMatthew G. Knepley qgeom.J = &fegeom.J[q*dE*dE]; 1544083401c6SMatthew G. Knepley qgeom.invJ = &fegeom.invJ[q*dE*dE]; 15452a4e142eSMatthew G. Knepley qgeom.detJ = &fegeom.detJ[q]; 1546083401c6SMatthew G. Knepley if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for cell %D, quadrature point %D", (double)fegeom.detJ[q], cell, q); 1547d3a7d86cSMatthew G. Knepley if (transform) { 1548083401c6SMatthew G. Knepley gcoords = &coords[dE*Nq]; 1549083401c6SMatthew G. Knepley ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[dE*q], PETSC_TRUE, dE, &coords[dE*q], gcoords, dm->transformCtx);CHKERRQ(ierr); 1550d3a7d86cSMatthew G. Knepley } else { 1551083401c6SMatthew G. Knepley gcoords = &coords[dE*q]; 1552d3a7d86cSMatthew G. Knepley } 1553083401c6SMatthew G. Knepley ierr = (*funcs[fields[f]])(dE, time, gcoords, Nc, funcVal, ctx); 1554e735a8a9SMatthew G. Knepley if (ierr) { 1555e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 1556083401c6SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr2); 1557e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 1558083401c6SMatthew G. Knepley ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2); 1559e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 1560e735a8a9SMatthew G. Knepley } 1561083401c6SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[dE*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);} 156296959cd1SMatthew G. Knepley /* Call once for each face, except for lagrange field */ 1563083401c6SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fOff], &qgeom, q, interpolant);CHKERRQ(ierr);} 1564083401c6SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fOff], q, interpolant);CHKERRQ(ierr);} 1565083401c6SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]); 156615496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1567beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 1568083401c6SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " cell %D field %D,%D point %g %g %g diff %g\n", cell, fields[f], fc, (double)(dE > 0 ? coords[dE*q] : 0.), (double)(dE > 1 ? coords[dE*q+1] : 0.),(double)(dE > 2 ? coords[dE*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);} 15694bee2e38SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]; 157073d901b8SMatthew G. Knepley } 157173d901b8SMatthew G. Knepley } 1572083401c6SMatthew G. Knepley fOff += Nb; 15739c3cf19fSMatthew G. Knepley qc += Nc; 1574083401c6SMatthew G. Knepley localDiff[fields[f]] += elemDiff; 1575083401c6SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " cell %D field %D cum diff %g\n", cell, fields[f], (double)localDiff[fields[f]]);CHKERRQ(ierr);} 157673d901b8SMatthew G. Knepley } 1577083401c6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr); 1578083401c6SMatthew G. Knepley } 1579083401c6SMatthew G. Knepley if (label) { 1580083401c6SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 1581083401c6SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1582083401c6SMatthew G. Knepley } 1583083401c6SMatthew G. Knepley ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr); 1584083401c6SMatthew G. Knepley ierr = PetscFree6(funcVal, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ);CHKERRQ(ierr); 158573d901b8SMatthew G. Knepley } 158673d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1587083401c6SMatthew G. Knepley ierr = MPIU_Allreduce(localDiff, diff, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 1588083401c6SMatthew G. Knepley ierr = PetscFree(localDiff);CHKERRQ(ierr); 1589083401c6SMatthew G. Knepley for (f = 0; f < Nf; ++f) diff[f] = PetscSqrtReal(diff[f]); 159073d901b8SMatthew G. Knepley PetscFunctionReturn(0); 159173d901b8SMatthew G. Knepley } 159273d901b8SMatthew G. Knepley 1593e729f68cSMatthew G. Knepley /*@C 1594e729f68cSMatthew 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. 1595e729f68cSMatthew G. Knepley 1596d083f849SBarry Smith Collective on dm 1597c0f8e1fdSMatthew G. Knepley 1598e729f68cSMatthew G. Knepley Input Parameters: 1599e729f68cSMatthew G. Knepley + dm - The DM 16000163fd50SMatthew G. Knepley . time - The time 1601ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation 1602e729f68cSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 1603e729f68cSMatthew G. Knepley - X - The coefficient vector u_h 1604e729f68cSMatthew G. Knepley 1605e729f68cSMatthew G. Knepley Output Parameter: 1606e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell 1607e729f68cSMatthew G. Knepley 1608e729f68cSMatthew G. Knepley Level: developer 1609e729f68cSMatthew G. Knepley 1610b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 1611e729f68cSMatthew G. Knepley @*/ 16120163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D) 1613e729f68cSMatthew G. Knepley { 1614e729f68cSMatthew G. Knepley PetscSection section; 1615e729f68cSMatthew G. Knepley PetscQuadrature quad; 1616e729f68cSMatthew G. Knepley Vec localX; 16174bee2e38SMatthew G. Knepley PetscFEGeom fegeom; 1618e729f68cSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 16194bee2e38SMatthew G. Knepley PetscReal *coords; 1620e729f68cSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 1621485ad865SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, c, field, fieldOffset; 1622e729f68cSMatthew G. Knepley PetscErrorCode ierr; 1623e729f68cSMatthew G. Knepley 1624e729f68cSMatthew G. Knepley PetscFunctionBegin; 1625e729f68cSMatthew G. Knepley ierr = VecSet(D, 0.0);CHKERRQ(ierr); 1626e729f68cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 16277318780aSToby Isaac ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 162892fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 1629e729f68cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 1630e729f68cSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 1631bdd6f66aSToby Isaac ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 1632e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1633e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1634e729f68cSMatthew G. Knepley for (field = 0; field < numFields; ++field) { 1635e729f68cSMatthew G. Knepley PetscObject obj; 1636e729f68cSMatthew G. Knepley PetscClassId id; 1637e729f68cSMatthew G. Knepley PetscInt Nc; 1638e729f68cSMatthew G. Knepley 163944a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 1640e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1641e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1642e729f68cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1643e729f68cSMatthew G. Knepley 1644e729f68cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 1645e729f68cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 1646e729f68cSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1647e729f68cSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 1648e729f68cSMatthew G. Knepley 1649e729f68cSMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 1650e729f68cSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 1651ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 1652e729f68cSMatthew G. Knepley numComponents += Nc; 1653e729f68cSMatthew G. Knepley } 16549c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 1655beaa55a6SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 16562a4e142eSMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr); 1657412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1658e729f68cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1659e729f68cSMatthew G. Knepley PetscScalar *x = NULL; 16606f288a59SMatthew G. Knepley PetscScalar elemDiff = 0.0; 16619c3cf19fSMatthew G. Knepley PetscInt qc = 0; 1662e729f68cSMatthew G. Knepley 16632a4e142eSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 1664e729f68cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1665e729f68cSMatthew G. Knepley 1666e729f68cSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 1667e729f68cSMatthew G. Knepley PetscObject obj; 1668e729f68cSMatthew G. Knepley PetscClassId id; 1669e729f68cSMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 1670e729f68cSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 1671e729f68cSMatthew G. Knepley 167244a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 1673e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1674e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 1675e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1676ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 167723f34ed2SToby Isaac if (funcs[field]) { 16787318780aSToby Isaac for (q = 0; q < Nq; ++q) { 16792a4e142eSMatthew G. Knepley PetscFEGeom qgeom; 16802a4e142eSMatthew G. Knepley 16812a4e142eSMatthew G. Knepley qgeom.dimEmbed = fegeom.dimEmbed; 16822a4e142eSMatthew G. Knepley qgeom.J = &fegeom.J[q*coordDim*coordDim]; 16832a4e142eSMatthew G. Knepley qgeom.invJ = &fegeom.invJ[q*coordDim*coordDim]; 16842a4e142eSMatthew G. Knepley qgeom.detJ = &fegeom.detJ[q]; 16854bee2e38SMatthew G. Knepley if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], c, q); 1686274e8aaeSMatthew G. Knepley ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx); 1687e735a8a9SMatthew G. Knepley if (ierr) { 1688e735a8a9SMatthew G. Knepley PetscErrorCode ierr2; 1689e735a8a9SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2); 16902a4e142eSMatthew G. Knepley ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2); 1691e735a8a9SMatthew G. Knepley ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2); 1692e735a8a9SMatthew G. Knepley CHKERRQ(ierr); 1693e735a8a9SMatthew G. Knepley } 16942a4e142eSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);} 1695e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1696ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 1697e729f68cSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1698beaa55a6SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)]; 16994bee2e38SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]; 1700e729f68cSMatthew G. Knepley } 1701e729f68cSMatthew G. Knepley } 170223f34ed2SToby Isaac } 1703beaa55a6SMatthew G. Knepley fieldOffset += Nb; 17049c3cf19fSMatthew G. Knepley qc += Nc; 1705e729f68cSMatthew G. Knepley } 1706e729f68cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 170723f34ed2SToby Isaac ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr); 1708e729f68cSMatthew G. Knepley } 17092a4e142eSMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr); 1710e729f68cSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1711e729f68cSMatthew G. Knepley ierr = VecSqrtAbs(D);CHKERRQ(ierr); 1712e729f68cSMatthew G. Knepley PetscFunctionReturn(0); 1713e729f68cSMatthew G. Knepley } 1714e729f68cSMatthew G. Knepley 17151555c271SMatthew G. Knepley /*@C 17161555c271SMatthew 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. 17171555c271SMatthew G. Knepley 1718d083f849SBarry Smith Collective on dm 1719c0f8e1fdSMatthew G. Knepley 17201555c271SMatthew G. Knepley Input Parameters: 17211555c271SMatthew G. Knepley + dm - The DM 17221555c271SMatthew G. Knepley - LocX - The coefficient vector u_h 17231555c271SMatthew G. Knepley 17241555c271SMatthew G. Knepley Output Parameter: 17251555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient 17261555c271SMatthew G. Knepley 172795452b02SPatrick Sanan Notes: 172895452b02SPatrick Sanan Add citation to (Clement, 1975) and definition of the interpolant 17291555c271SMatthew 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 17301555c271SMatthew G. Knepley 17311555c271SMatthew G. Knepley Level: developer 17321555c271SMatthew G. Knepley 17331555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff() 17341555c271SMatthew G. Knepley @*/ 17351555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC) 17361555c271SMatthew G. Knepley { 1737db1066baSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 1738db1066baSMatthew G. Knepley PetscInt debug = mesh->printFEM; 17391555c271SMatthew G. Knepley DM dmC; 17401555c271SMatthew G. Knepley PetscSection section; 17411555c271SMatthew G. Knepley PetscQuadrature quad; 17421555c271SMatthew G. Knepley PetscScalar *interpolant, *gradsum; 17434bee2e38SMatthew G. Knepley PetscFEGeom fegeom; 17444bee2e38SMatthew G. Knepley PetscReal *coords; 17451555c271SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 1746485ad865SMatthew G. Knepley PetscInt dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, vStart, vEnd, v, field, fieldOffset; 17471555c271SMatthew G. Knepley PetscErrorCode ierr; 17481555c271SMatthew G. Knepley 17491555c271SMatthew G. Knepley PetscFunctionBegin; 17501555c271SMatthew G. Knepley ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr); 17511555c271SMatthew G. Knepley ierr = VecSet(locC, 0.0);CHKERRQ(ierr); 17521555c271SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 17531555c271SMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr); 17544bee2e38SMatthew G. Knepley fegeom.dimEmbed = coordDim; 175592fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 17561555c271SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 17571555c271SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 17581555c271SMatthew G. Knepley PetscObject obj; 17591555c271SMatthew G. Knepley PetscClassId id; 17601555c271SMatthew G. Knepley PetscInt Nc; 17611555c271SMatthew G. Knepley 176244a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 17631555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 17641555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 17651555c271SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 17661555c271SMatthew G. Knepley 17671555c271SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 17681555c271SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 17691555c271SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 17701555c271SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 17711555c271SMatthew G. Knepley 17721555c271SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 17731555c271SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 1774ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 17751555c271SMatthew G. Knepley numComponents += Nc; 17761555c271SMatthew G. Knepley } 17771555c271SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr); 17781555c271SMatthew G. Knepley if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents); 17794bee2e38SMatthew G. Knepley ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr); 17801555c271SMatthew G. Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 1781412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 17821555c271SMatthew G. Knepley for (v = vStart; v < vEnd; ++v) { 17831555c271SMatthew G. Knepley PetscScalar volsum = 0.0; 17841555c271SMatthew G. Knepley PetscInt *star = NULL; 17851555c271SMatthew G. Knepley PetscInt starSize, st, d, fc; 17861555c271SMatthew G. Knepley 1787580bdb30SBarry Smith ierr = PetscArrayzero(gradsum, coordDim*numComponents);CHKERRQ(ierr); 17881555c271SMatthew G. Knepley ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 17891555c271SMatthew G. Knepley for (st = 0; st < starSize*2; st += 2) { 17901555c271SMatthew G. Knepley const PetscInt cell = star[st]; 17911555c271SMatthew G. Knepley PetscScalar *grad = &gradsum[coordDim*numComponents]; 17921555c271SMatthew G. Knepley PetscScalar *x = NULL; 17931555c271SMatthew G. Knepley PetscReal vol = 0.0; 17941555c271SMatthew G. Knepley 17951555c271SMatthew G. Knepley if ((cell < cStart) || (cell >= cEnd)) continue; 17964bee2e38SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr); 17971555c271SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 17981555c271SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 17991555c271SMatthew G. Knepley PetscObject obj; 18001555c271SMatthew G. Knepley PetscClassId id; 18011555c271SMatthew G. Knepley PetscInt Nb, Nc, q, qc = 0; 18021555c271SMatthew G. Knepley 1803580bdb30SBarry Smith ierr = PetscArrayzero(grad, coordDim*numComponents);CHKERRQ(ierr); 180444a7f3ddSMatthew G. Knepley ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr); 18051555c271SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 18061555c271SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 18071555c271SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1808ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 18091555c271SMatthew G. Knepley for (q = 0; q < Nq; ++q) { 18102a4e142eSMatthew G. Knepley PetscFEGeom qgeom; 18112a4e142eSMatthew G. Knepley 18122a4e142eSMatthew G. Knepley qgeom.dimEmbed = fegeom.dimEmbed; 18132a4e142eSMatthew G. Knepley qgeom.J = &fegeom.J[q*coordDim*coordDim]; 18142a4e142eSMatthew G. Knepley qgeom.invJ = &fegeom.invJ[q*coordDim*coordDim]; 18152a4e142eSMatthew G. Knepley qgeom.detJ = &fegeom.detJ[q]; 18164bee2e38SMatthew G. Knepley if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], cell, q); 18171555c271SMatthew G. Knepley if (ierr) { 18181555c271SMatthew G. Knepley PetscErrorCode ierr2; 18191555c271SMatthew G. Knepley ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2); 18201555c271SMatthew G. Knepley ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2); 18214bee2e38SMatthew G. Knepley ierr2 = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2); 18221555c271SMatthew G. Knepley CHKERRQ(ierr); 18231555c271SMatthew G. Knepley } 18242a4e142eSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);} 1825ff1e0c32SBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field); 18261555c271SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 18271555c271SMatthew G. Knepley const PetscReal wt = quadWeights[q*qNc+qc+fc]; 18281555c271SMatthew G. Knepley 18294bee2e38SMatthew G. Knepley for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*fegeom.detJ[q]; 18301555c271SMatthew G. Knepley } 18314bee2e38SMatthew G. Knepley vol += quadWeights[q*qNc]*fegeom.detJ[q]; 18321555c271SMatthew G. Knepley } 18331555c271SMatthew G. Knepley fieldOffset += Nb; 18341555c271SMatthew G. Knepley qc += Nc; 18351555c271SMatthew G. Knepley } 18361555c271SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr); 1837f8527842SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 1838f8527842SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 1839f8527842SMatthew G. Knepley gradsum[fc*coordDim+d] += grad[fc*coordDim+d]; 1840f8527842SMatthew G. Knepley } 1841f8527842SMatthew G. Knepley } 1842f8527842SMatthew G. Knepley volsum += vol; 1843db1066baSMatthew G. Knepley if (debug) { 18446d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr); 18451555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 18461555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) { 18471555c271SMatthew G. Knepley if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);} 18486d20ae03SJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr); 18491555c271SMatthew G. Knepley } 18501555c271SMatthew G. Knepley } 18511555c271SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr); 1852db1066baSMatthew G. Knepley } 18531555c271SMatthew G. Knepley } 18541555c271SMatthew G. Knepley for (fc = 0; fc < numComponents; ++fc) { 18551555c271SMatthew G. Knepley for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum; 18561555c271SMatthew G. Knepley } 18571555c271SMatthew G. Knepley ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 18581555c271SMatthew G. Knepley ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr); 18591555c271SMatthew G. Knepley } 18604bee2e38SMatthew G. Knepley ierr = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr); 18611555c271SMatthew G. Knepley PetscFunctionReturn(0); 18621555c271SMatthew G. Knepley } 18631555c271SMatthew G. Knepley 1864338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user) 186573d901b8SMatthew G. Knepley { 1866338f77d5SMatthew G. Knepley DM dmAux = NULL; 186761aaff12SToby Isaac PetscDS prob, probAux = NULL; 186873d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1869338f77d5SMatthew G. Knepley Vec locX, locA; 1870c330f8ffSToby Isaac PetscInt dim, numCells = cEnd - cStart, c, f; 1871c330f8ffSToby Isaac PetscBool useFVM = PETSC_FALSE; 1872338f77d5SMatthew G. Knepley /* DS */ 1873338f77d5SMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x, numConstants; 1874338f77d5SMatthew G. Knepley PetscInt NfAux, totDimAux, *aOff; 1875338f77d5SMatthew G. Knepley PetscScalar *u, *a; 1876338f77d5SMatthew G. Knepley const PetscScalar *constants; 1877338f77d5SMatthew G. Knepley /* Geometry */ 1878c330f8ffSToby Isaac PetscFEGeom *cgeomFEM; 1879338f77d5SMatthew G. Knepley DM dmGrad; 1880c330f8ffSToby Isaac PetscQuadrature affineQuad = NULL; 1881338f77d5SMatthew G. Knepley Vec cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL; 1882b5a3613cSMatthew G. Knepley PetscFVCellGeom *cgeomFVM; 1883338f77d5SMatthew G. Knepley const PetscScalar *lgrad; 1884b7260050SToby Isaac PetscInt maxDegree; 1885c330f8ffSToby Isaac DMField coordField; 1886c330f8ffSToby Isaac IS cellIS; 188773d901b8SMatthew G. Knepley PetscErrorCode ierr; 188873d901b8SMatthew G. Knepley 188973d901b8SMatthew G. Knepley PetscFunctionBegin; 1890338f77d5SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1891c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 189292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 189373d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1894338f77d5SMatthew G. Knepley /* Determine which discretizations we have */ 1895b5a3613cSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1896b5a3613cSMatthew G. Knepley PetscObject obj; 1897b5a3613cSMatthew G. Knepley PetscClassId id; 1898b5a3613cSMatthew G. Knepley 1899b5a3613cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1900b5a3613cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1901338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE; 1902338f77d5SMatthew G. Knepley } 1903338f77d5SMatthew G. Knepley /* Get local solution with boundary values */ 1904338f77d5SMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 1905338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1906338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1907338f77d5SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 1908338f77d5SMatthew G. Knepley /* Read DS information */ 1909338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 1910338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 1911338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 1912c330f8ffSToby Isaac ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr); 1913338f77d5SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 1914338f77d5SMatthew G. Knepley /* Read Auxiliary DS information */ 1915338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1916338f77d5SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 1917338f77d5SMatthew G. Knepley if (dmAux) { 1918338f77d5SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1919338f77d5SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 192092fd8e1eSJed Brown ierr = DMGetLocalSection(dmAux, §ionAux);CHKERRQ(ierr); 1921338f77d5SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 1922338f77d5SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 1923338f77d5SMatthew G. Knepley } 1924338f77d5SMatthew G. Knepley /* Allocate data arrays */ 1925338f77d5SMatthew G. Knepley ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr); 1926338f77d5SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1927338f77d5SMatthew G. Knepley /* Read out geometry */ 1928c330f8ffSToby Isaac ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr); 1929b7260050SToby Isaac ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 1930b7260050SToby Isaac if (maxDegree <= 1) { 1931c330f8ffSToby Isaac ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 1932c330f8ffSToby Isaac if (affineQuad) { 1933c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 1934338f77d5SMatthew G. Knepley } 1935b5a3613cSMatthew G. Knepley } 1936b5a3613cSMatthew G. Knepley if (useFVM) { 1937338f77d5SMatthew G. Knepley PetscFV fv = NULL; 1938b5a3613cSMatthew G. Knepley Vec grad; 1939b5a3613cSMatthew G. Knepley PetscInt fStart, fEnd; 1940b5a3613cSMatthew G. Knepley PetscBool compGrad; 1941b5a3613cSMatthew G. Knepley 1942338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1943338f77d5SMatthew G. Knepley PetscObject obj; 1944338f77d5SMatthew G. Knepley PetscClassId id; 1945338f77d5SMatthew G. Knepley 1946338f77d5SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1947338f77d5SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1948338f77d5SMatthew G. Knepley if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;} 1949338f77d5SMatthew G. Knepley } 1950338f77d5SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr); 1951338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr); 1952b5a3613cSMatthew G. Knepley ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr); 1953338f77d5SMatthew G. Knepley ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr); 1954338f77d5SMatthew G. Knepley ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr); 1955b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 1956b5a3613cSMatthew G. Knepley /* Reconstruct and limit cell gradients */ 1957b5a3613cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 1958b5a3613cSMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1959338f77d5SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 1960b5a3613cSMatthew G. Knepley /* Communicate gradient values */ 1961b5a3613cSMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 1962b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1963b5a3613cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 1964b5a3613cSMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 1965b5a3613cSMatthew G. Knepley /* Handle non-essential (e.g. outflow) boundary values */ 1966338f77d5SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 1967b5a3613cSMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 1968b5a3613cSMatthew G. Knepley } 1969338f77d5SMatthew G. Knepley /* Read out data from inputs */ 197073d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 197173d901b8SMatthew G. Knepley PetscScalar *x = NULL; 197273d901b8SMatthew G. Knepley PetscInt i; 197373d901b8SMatthew G. Knepley 1974338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 19750f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 1976338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr); 197773d901b8SMatthew G. Knepley if (dmAux) { 1978338f77d5SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 19790f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 1980338f77d5SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr); 198173d901b8SMatthew G. Knepley } 198273d901b8SMatthew G. Knepley } 1983338f77d5SMatthew G. Knepley /* Do integration for each field */ 198473d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1985c1f031eeSMatthew G. Knepley PetscObject obj; 1986c1f031eeSMatthew G. Knepley PetscClassId id; 1987c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 198873d901b8SMatthew G. Knepley 1989c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1990c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1991c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1992c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1993c1f031eeSMatthew G. Knepley PetscQuadrature q; 1994c330f8ffSToby Isaac PetscFEGeom *chunkGeom = NULL; 1995c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1996c1f031eeSMatthew G. Knepley 19970f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1998c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 19999c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 2000c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 2001c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 200273d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 20030f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 200473d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 200573d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 200673d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 200773d901b8SMatthew G. Knepley offset = numCells - Nr; 2008c330f8ffSToby Isaac if (!affineQuad) { 2009c330f8ffSToby Isaac ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 2010c330f8ffSToby Isaac } 2011c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 20124bee2e38SMatthew G. Knepley ierr = PetscFEIntegrate(prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr); 2013c330f8ffSToby Isaac ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 20144bee2e38SMatthew G. Knepley ierr = PetscFEIntegrate(prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr); 2015c330f8ffSToby Isaac ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr); 2016c330f8ffSToby Isaac if (!affineQuad) { 2017c330f8ffSToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 2018c330f8ffSToby Isaac } 2019c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 2020c1f031eeSMatthew G. Knepley PetscInt foff; 2021420e96edSMatthew G. Knepley PetscPointFunc obj_func; 2022b69edc29SMatthew G. Knepley PetscScalar lint; 2023c1f031eeSMatthew G. Knepley 2024c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 2025c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 2026c1f031eeSMatthew G. Knepley if (obj_func) { 2027c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 2028b5a3613cSMatthew G. Knepley PetscScalar *u_x; 2029b5a3613cSMatthew G. Knepley 2030b5a3613cSMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr); 2031338f77d5SMatthew 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); 2032338f77d5SMatthew G. Knepley cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume; 203373d901b8SMatthew G. Knepley } 2034c1f031eeSMatthew G. Knepley } 2035ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f); 2036c1f031eeSMatthew G. Knepley } 2037338f77d5SMatthew G. Knepley /* Cleanup data arrays */ 2038b5a3613cSMatthew G. Knepley if (useFVM) { 2039b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 2040b5a3613cSMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 2041b5a3613cSMatthew G. Knepley ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 2042b5a3613cSMatthew G. Knepley ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr); 2043b5a3613cSMatthew G. Knepley ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr); 2044b5a3613cSMatthew G. Knepley ierr = DMDestroy(&dmGrad);CHKERRQ(ierr); 2045b5a3613cSMatthew G. Knepley } 204673d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 2047338f77d5SMatthew G. Knepley ierr = PetscFree(u);CHKERRQ(ierr); 2048338f77d5SMatthew G. Knepley /* Cleanup */ 2049f99c8401SToby Isaac if (affineQuad) { 2050f99c8401SToby Isaac ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr); 2051f99c8401SToby Isaac } 2052f99c8401SToby Isaac ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 2053c330f8ffSToby Isaac ierr = ISDestroy(&cellIS);CHKERRQ(ierr); 2054338f77d5SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 2055338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 2056338f77d5SMatthew G. Knepley } 2057338f77d5SMatthew G. Knepley 2058338f77d5SMatthew G. Knepley /*@ 2059338f77d5SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user 2060338f77d5SMatthew G. Knepley 2061338f77d5SMatthew G. Knepley Input Parameters: 2062338f77d5SMatthew G. Knepley + dm - The mesh 2063338f77d5SMatthew G. Knepley . X - Global input vector 2064338f77d5SMatthew G. Knepley - user - The user context 2065338f77d5SMatthew G. Knepley 2066338f77d5SMatthew G. Knepley Output Parameter: 2067338f77d5SMatthew G. Knepley . integral - Integral for each field 2068338f77d5SMatthew G. Knepley 2069338f77d5SMatthew G. Knepley Level: developer 2070338f77d5SMatthew G. Knepley 2071338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 2072338f77d5SMatthew G. Knepley @*/ 2073b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user) 2074338f77d5SMatthew G. Knepley { 2075338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2076b8feb594SMatthew G. Knepley PetscScalar *cintegral, *lintegral; 2077412e9a14SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cell; 2078338f77d5SMatthew G. Knepley PetscErrorCode ierr; 2079338f77d5SMatthew G. Knepley 2080338f77d5SMatthew G. Knepley PetscFunctionBegin; 2081338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2082338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 2083338f77d5SMatthew G. Knepley PetscValidPointer(integral, 3); 2084338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 2085338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2086338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 2087412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 2088338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 2089338f77d5SMatthew G. Knepley ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 2090338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 2091338f77d5SMatthew G. Knepley /* Sum up values */ 2092338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2093338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 2094338f77d5SMatthew G. Knepley 2095338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 2096b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f]; 2097338f77d5SMatthew G. Knepley } 2098b8feb594SMatthew G. Knepley ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 209973d901b8SMatthew G. Knepley if (mesh->printFEM) { 2100338f77d5SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr); 2101b8feb594SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);} 210273d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 210373d901b8SMatthew G. Knepley } 2104338f77d5SMatthew G. Knepley ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr); 2105338f77d5SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 2106338f77d5SMatthew G. Knepley PetscFunctionReturn(0); 2107338f77d5SMatthew G. Knepley } 2108338f77d5SMatthew G. Knepley 2109338f77d5SMatthew G. Knepley /*@ 2110338f77d5SMatthew G. Knepley DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user 2111338f77d5SMatthew G. Knepley 2112338f77d5SMatthew G. Knepley Input Parameters: 2113338f77d5SMatthew G. Knepley + dm - The mesh 2114338f77d5SMatthew G. Knepley . X - Global input vector 2115338f77d5SMatthew G. Knepley - user - The user context 2116338f77d5SMatthew G. Knepley 2117338f77d5SMatthew G. Knepley Output Parameter: 2118338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field 2119338f77d5SMatthew G. Knepley 2120338f77d5SMatthew G. Knepley Level: developer 2121338f77d5SMatthew G. Knepley 2122338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 2123338f77d5SMatthew G. Knepley @*/ 2124338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user) 2125338f77d5SMatthew G. Knepley { 2126338f77d5SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 2127338f77d5SMatthew G. Knepley DM dmF; 2128338f77d5SMatthew G. Knepley PetscSection sectionF; 2129338f77d5SMatthew G. Knepley PetscScalar *cintegral, *af; 2130412e9a14SMatthew G. Knepley PetscInt Nf, f, cellHeight, cStart, cEnd, cell; 2131338f77d5SMatthew G. Knepley PetscErrorCode ierr; 2132338f77d5SMatthew G. Knepley 2133338f77d5SMatthew G. Knepley PetscFunctionBegin; 2134338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2135338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 2136338f77d5SMatthew G. Knepley PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 2137338f77d5SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 2138338f77d5SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 2139338f77d5SMatthew G. Knepley ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr); 2140412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr); 2141338f77d5SMatthew G. Knepley /* TODO Introduce a loop over large chunks (right now this is a single chunk) */ 2142338f77d5SMatthew G. Knepley ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr); 2143338f77d5SMatthew G. Knepley ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr); 2144338f77d5SMatthew G. Knepley /* Put values in F*/ 2145338f77d5SMatthew G. Knepley ierr = VecGetDM(F, &dmF);CHKERRQ(ierr); 214692fd8e1eSJed Brown ierr = DMGetLocalSection(dmF, §ionF);CHKERRQ(ierr); 2147338f77d5SMatthew G. Knepley ierr = VecGetArray(F, &af);CHKERRQ(ierr); 2148338f77d5SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2149338f77d5SMatthew G. Knepley const PetscInt c = cell - cStart; 2150338f77d5SMatthew G. Knepley PetscInt dof, off; 2151338f77d5SMatthew G. Knepley 2152338f77d5SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);} 2153338f77d5SMatthew G. Knepley ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr); 2154338f77d5SMatthew G. Knepley ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr); 2155338f77d5SMatthew G. Knepley if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf); 2156338f77d5SMatthew G. Knepley for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f]; 2157338f77d5SMatthew G. Knepley } 2158338f77d5SMatthew G. Knepley ierr = VecRestoreArray(F, &af);CHKERRQ(ierr); 2159338f77d5SMatthew G. Knepley ierr = PetscFree(cintegral);CHKERRQ(ierr); 2160c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 216173d901b8SMatthew G. Knepley PetscFunctionReturn(0); 216273d901b8SMatthew G. Knepley } 216373d901b8SMatthew G. Knepley 21649b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS, 21659b6f715bSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 216664c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 216764c72086SMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 216864c72086SMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), 216964c72086SMatthew G. Knepley PetscScalar *fintegral, void *user) 217064c72086SMatthew G. Knepley { 21719b6f715bSMatthew G. Knepley DM plex = NULL, plexA = NULL; 2172a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 21739b6f715bSMatthew G. Knepley PetscDS prob, probAux = NULL; 21749b6f715bSMatthew G. Knepley PetscSection section, sectionAux = NULL; 21759b6f715bSMatthew G. Knepley Vec locA = NULL; 21769b6f715bSMatthew G. Knepley DMField coordField; 21779b6f715bSMatthew G. Knepley PetscInt Nf, totDim, *uOff, *uOff_x; 21789b6f715bSMatthew G. Knepley PetscInt NfAux = 0, totDimAux = 0, *aOff = NULL; 21799b6f715bSMatthew G. Knepley PetscScalar *u, *a = NULL; 218064c72086SMatthew G. Knepley const PetscScalar *constants; 21819b6f715bSMatthew G. Knepley PetscInt numConstants, f; 218264c72086SMatthew G. Knepley PetscErrorCode ierr; 218364c72086SMatthew G. Knepley 218464c72086SMatthew G. Knepley PetscFunctionBegin; 21859b6f715bSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 21869b6f715bSMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 218764c72086SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 218892fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 218964c72086SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 219064c72086SMatthew G. Knepley /* Determine which discretizations we have */ 21919b6f715bSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 219264c72086SMatthew G. Knepley PetscObject obj; 219364c72086SMatthew G. Knepley PetscClassId id; 219464c72086SMatthew G. Knepley 21959b6f715bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 219664c72086SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 21979b6f715bSMatthew G. Knepley if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f); 219864c72086SMatthew G. Knepley } 219964c72086SMatthew G. Knepley /* Read DS information */ 220064c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 220164c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr); 220264c72086SMatthew G. Knepley ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr); 220364c72086SMatthew G. Knepley ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr); 220464c72086SMatthew G. Knepley /* Read Auxiliary DS information */ 220564c72086SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 22069b6f715bSMatthew G. Knepley if (locA) { 22079b6f715bSMatthew G. Knepley DM dmAux; 22089b6f715bSMatthew G. Knepley 22099b6f715bSMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 2210a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 22119b6f715bSMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 221264c72086SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 221364c72086SMatthew G. Knepley ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr); 221492fd8e1eSJed Brown ierr = DMGetLocalSection(dmAux, §ionAux);CHKERRQ(ierr); 221564c72086SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 221664c72086SMatthew G. Knepley ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr); 221764c72086SMatthew G. Knepley } 22189b6f715bSMatthew G. Knepley /* Integrate over points */ 22199b6f715bSMatthew G. Knepley { 22209b6f715bSMatthew G. Knepley PetscFEGeom *fgeom, *chunkGeom = NULL; 2221b7260050SToby Isaac PetscInt maxDegree; 22229b6f715bSMatthew G. Knepley PetscQuadrature qGeom = NULL; 22239b6f715bSMatthew G. Knepley const PetscInt *points; 22249b6f715bSMatthew G. Knepley PetscInt numFaces, face, Nq, field; 22259b6f715bSMatthew G. Knepley PetscInt numChunks, chunkSize, chunk, Nr, offset; 222664c72086SMatthew G. Knepley 22279b6f715bSMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 22289b6f715bSMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 22299b6f715bSMatthew G. Knepley ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 2230b7260050SToby Isaac ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr); 223164c72086SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 223264c72086SMatthew G. Knepley PetscFE fe; 223364c72086SMatthew G. Knepley 223464c72086SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 2235b7260050SToby Isaac if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);} 22369b6f715bSMatthew G. Knepley if (!qGeom) { 22379b6f715bSMatthew G. Knepley ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 22389b6f715bSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr); 22399b6f715bSMatthew G. Knepley } 22409b6f715bSMatthew G. Knepley ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 22419b6f715bSMatthew G. Knepley ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr); 22429b6f715bSMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 22439b6f715bSMatthew G. Knepley const PetscInt point = points[face], *support, *cone; 22449b6f715bSMatthew G. Knepley PetscScalar *x = NULL; 22459b6f715bSMatthew G. Knepley PetscInt i, coneSize, faceLoc; 22469b6f715bSMatthew G. Knepley 22479b6f715bSMatthew G. Knepley ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 22489b6f715bSMatthew G. Knepley ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 22499b6f715bSMatthew G. Knepley ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 22509b6f715bSMatthew G. Knepley for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 22519b6f715bSMatthew 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]); 22529b6f715bSMatthew G. Knepley fgeom->face[face][0] = faceLoc; 22539b6f715bSMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 22549b6f715bSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 22559b6f715bSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 22569b6f715bSMatthew G. Knepley if (locA) { 22579b6f715bSMatthew G. Knepley PetscInt subp; 2258a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr); 22599b6f715bSMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 22609b6f715bSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i]; 22619b6f715bSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 22629b6f715bSMatthew G. Knepley } 22639b6f715bSMatthew G. Knepley } 22649b6f715bSMatthew G. Knepley /* Get blocking */ 22659b6f715bSMatthew G. Knepley { 22669b6f715bSMatthew G. Knepley PetscQuadrature q; 22679b6f715bSMatthew G. Knepley PetscInt numBatches, batchSize, numBlocks, blockSize; 22689b6f715bSMatthew G. Knepley PetscInt Nq, Nb; 22699b6f715bSMatthew G. Knepley 227064c72086SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 227164c72086SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 227264c72086SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 227364c72086SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 227464c72086SMatthew G. Knepley blockSize = Nb*Nq; 227564c72086SMatthew G. Knepley batchSize = numBlocks * blockSize; 22769b6f715bSMatthew G. Knepley chunkSize = numBatches*batchSize; 227764c72086SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 22789b6f715bSMatthew G. Knepley numChunks = numFaces / chunkSize; 22799b6f715bSMatthew G. Knepley Nr = numFaces % chunkSize; 228064c72086SMatthew G. Knepley offset = numFaces - Nr; 228164c72086SMatthew G. Knepley } 22829b6f715bSMatthew G. Knepley /* Do integration for each field */ 22839b6f715bSMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 22849b6f715bSMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr); 22854bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBd(prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr); 22869b6f715bSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr); 228764c72086SMatthew G. Knepley } 22889b6f715bSMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr); 22894bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateBd(prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr); 22909b6f715bSMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr); 229164c72086SMatthew G. Knepley /* Cleanup data arrays */ 22929b6f715bSMatthew G. Knepley ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr); 22939b6f715bSMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 22949b6f715bSMatthew G. Knepley ierr = PetscFree2(u, a);CHKERRQ(ierr); 22959b6f715bSMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 229664c72086SMatthew G. Knepley } 229764c72086SMatthew G. Knepley } 22989b6f715bSMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 22999b6f715bSMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 23009b6f715bSMatthew G. Knepley PetscFunctionReturn(0); 23019b6f715bSMatthew G. Knepley } 23029b6f715bSMatthew G. Knepley 23039b6f715bSMatthew G. Knepley /*@ 23049b6f715bSMatthew G. Knepley DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user 23059b6f715bSMatthew G. Knepley 23069b6f715bSMatthew G. Knepley Input Parameters: 23079b6f715bSMatthew G. Knepley + dm - The mesh 23089b6f715bSMatthew G. Knepley . X - Global input vector 23099b6f715bSMatthew G. Knepley . label - The boundary DMLabel 23109b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values 23119b6f715bSMatthew G. Knepley . vals - The label values to use, or PETSC_NULL for all values 23129b6f715bSMatthew G. Knepley . func = The function to integrate along the boundary 23139b6f715bSMatthew G. Knepley - user - The user context 23149b6f715bSMatthew G. Knepley 23159b6f715bSMatthew G. Knepley Output Parameter: 23169b6f715bSMatthew G. Knepley . integral - Integral for each field 23179b6f715bSMatthew G. Knepley 23189b6f715bSMatthew G. Knepley Level: developer 23199b6f715bSMatthew G. Knepley 23209b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM() 23219b6f715bSMatthew G. Knepley @*/ 23229b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[], 23239b6f715bSMatthew G. Knepley void (*func)(PetscInt, PetscInt, PetscInt, 23249b6f715bSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 23259b6f715bSMatthew G. Knepley const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], 23269b6f715bSMatthew G. Knepley PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), 23279b6f715bSMatthew G. Knepley PetscScalar *integral, void *user) 23289b6f715bSMatthew G. Knepley { 23299b6f715bSMatthew G. Knepley Vec locX; 23309b6f715bSMatthew G. Knepley PetscSection section; 23319b6f715bSMatthew G. Knepley DMLabel depthLabel; 23329b6f715bSMatthew G. Knepley IS facetIS; 23339b6f715bSMatthew G. Knepley PetscInt dim, Nf, f, v; 23349b6f715bSMatthew G. Knepley PetscErrorCode ierr; 23359b6f715bSMatthew G. Knepley 23369b6f715bSMatthew G. Knepley PetscFunctionBegin; 23379b6f715bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 23389b6f715bSMatthew G. Knepley PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 23399b6f715bSMatthew G. Knepley PetscValidPointer(label, 3); 23409b6f715bSMatthew G. Knepley if (vals) PetscValidPointer(vals, 5); 23419b6f715bSMatthew G. Knepley PetscValidPointer(integral, 6); 23429b6f715bSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 23439b6f715bSMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 23449b6f715bSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 23459b6f715bSMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 234692fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 23479b6f715bSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 23489b6f715bSMatthew G. Knepley /* Get local solution with boundary values */ 23499b6f715bSMatthew G. Knepley ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr); 23509b6f715bSMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 23519b6f715bSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 23529b6f715bSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr); 23539b6f715bSMatthew G. Knepley /* Loop over label values */ 2354580bdb30SBarry Smith ierr = PetscArrayzero(integral, Nf);CHKERRQ(ierr); 23559b6f715bSMatthew G. Knepley for (v = 0; v < numVals; ++v) { 23569b6f715bSMatthew G. Knepley IS pointIS; 23579b6f715bSMatthew G. Knepley PetscInt numFaces, face; 23589b6f715bSMatthew G. Knepley PetscScalar *fintegral; 23599b6f715bSMatthew G. Knepley 23609b6f715bSMatthew G. Knepley ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr); 23619b6f715bSMatthew G. Knepley if (!pointIS) continue; /* No points with that id on this process */ 23629b6f715bSMatthew G. Knepley { 23639b6f715bSMatthew G. Knepley IS isectIS; 23649b6f715bSMatthew G. Knepley 23659b6f715bSMatthew G. Knepley /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 23669b6f715bSMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr); 23679b6f715bSMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 23689b6f715bSMatthew G. Knepley pointIS = isectIS; 23699b6f715bSMatthew G. Knepley } 23709b6f715bSMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 23719b6f715bSMatthew G. Knepley ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr); 23729b6f715bSMatthew G. Knepley ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr); 23739b6f715bSMatthew G. Knepley /* Sum point contributions into integral */ 23749b6f715bSMatthew G. Knepley for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f]; 23759b6f715bSMatthew G. Knepley ierr = PetscFree(fintegral);CHKERRQ(ierr); 23769b6f715bSMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 23779b6f715bSMatthew G. Knepley } 237864c72086SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr); 23799b6f715bSMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 23809b6f715bSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 238164c72086SMatthew G. Knepley PetscFunctionReturn(0); 238264c72086SMatthew G. Knepley } 238364c72086SMatthew G. Knepley 2384d69c5d34SMatthew G. Knepley /*@ 2385cf51de39SMatthew G. Knepley DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to a uniformly refined DM. 2386d69c5d34SMatthew G. Knepley 2387d69c5d34SMatthew G. Knepley Input Parameters: 2388cf51de39SMatthew G. Knepley + dmc - The coarse mesh 2389cf51de39SMatthew G. Knepley . dmf - The fine mesh 2390cf51de39SMatthew G. Knepley . isRefined - Flag indicating regular refinement, rather than the same topology 2391d69c5d34SMatthew G. Knepley - user - The user context 2392d69c5d34SMatthew G. Knepley 2393d69c5d34SMatthew G. Knepley Output Parameter: 2394934789fcSMatthew G. Knepley . In - The interpolation matrix 2395d69c5d34SMatthew G. Knepley 2396d69c5d34SMatthew G. Knepley Level: developer 2397d69c5d34SMatthew G. Knepley 239868132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 2399d69c5d34SMatthew G. Knepley @*/ 2400cf51de39SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, PetscBool isRefined, Mat In, void *user) 2401d69c5d34SMatthew G. Knepley { 2402d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 2403d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 24047c2b9c7aSMatthew G. Knepley PetscDS cds, rds; 2405d69c5d34SMatthew G. Knepley PetscFE *feRef; 240697c42addSMatthew G. Knepley PetscFV *fvRef; 2407d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 2408d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 2409d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 2410485ad865SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c; 24110f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 2412d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 2413d69c5d34SMatthew G. Knepley 2414d69c5d34SMatthew G. Knepley PetscFunctionBegin; 2415d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2416c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 241792fd8e1eSJed Brown ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr); 2418e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 241992fd8e1eSJed Brown ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr); 2420e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 2421d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 2422412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 24237c2b9c7aSMatthew G. Knepley ierr = DMGetDS(dmc, &cds);CHKERRQ(ierr); 24247c2b9c7aSMatthew G. Knepley ierr = DMGetDS(dmf, &rds);CHKERRQ(ierr); 242597c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf, &feRef, Nf, &fvRef);CHKERRQ(ierr); 2426d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 242797c42addSMatthew G. Knepley PetscObject obj; 242897c42addSMatthew G. Knepley PetscClassId id; 2429aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 2430d69c5d34SMatthew G. Knepley 24317c2b9c7aSMatthew G. Knepley ierr = PetscDSGetDiscretization(rds, f, &obj);CHKERRQ(ierr); 243297c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 243397c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 243497c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 243597c42addSMatthew G. Knepley 2436cf51de39SMatthew G. Knepley if (isRefined) { 24370f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 2438cf51de39SMatthew G. Knepley } else { 2439cf51de39SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) fe);CHKERRQ(ierr); 2440cf51de39SMatthew G. Knepley feRef[f] = fe; 2441cf51de39SMatthew G. Knepley } 2442d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 24430f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 244497c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 244597c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 244697c42addSMatthew G. Knepley PetscDualSpace Q; 244797c42addSMatthew G. Knepley 2448cf51de39SMatthew G. Knepley if (isRefined) { 244997c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 2450cf51de39SMatthew G. Knepley } else { 2451cf51de39SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) fv);CHKERRQ(ierr); 2452cf51de39SMatthew G. Knepley fvRef[f] = fv; 2453cf51de39SMatthew G. Knepley } 245497c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 245597c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 245697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 245797c42addSMatthew G. Knepley } 24589c3cf19fSMatthew G. Knepley rTotDim += rNb; 2459d69c5d34SMatthew G. Knepley } 24607c2b9c7aSMatthew G. Knepley ierr = PetscDSGetTotalDimension(cds, &cTotDim);CHKERRQ(ierr); 24610f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 2462580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, rTotDim*cTotDim);CHKERRQ(ierr); 2463d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 2464d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 2465d69c5d34SMatthew G. Knepley PetscQuadrature f; 2466d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 2467d69c5d34SMatthew G. Knepley PetscReal *points; 2468d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 2469d69c5d34SMatthew G. Knepley 2470d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 247197c42addSMatthew G. Knepley if (feRef[fieldI]) { 2472d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 24730f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 247497c42addSMatthew G. Knepley } else { 247597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 247697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 247797c42addSMatthew G. Knepley } 2478d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 2479d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 2480d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 24819c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 2482d69c5d34SMatthew G. Knepley npoints += Np; 2483d69c5d34SMatthew G. Knepley } 2484d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 2485d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 2486d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 24879c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 2488d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 2489d69c5d34SMatthew G. Knepley } 2490d69c5d34SMatthew G. Knepley 2491d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 249297c42addSMatthew G. Knepley PetscObject obj; 249397c42addSMatthew G. Knepley PetscClassId id; 24949c3cf19fSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j, qNc; 2495d69c5d34SMatthew G. Knepley 24967c2b9c7aSMatthew G. Knepley ierr = PetscDSGetDiscretization(cds, fieldJ, &obj);CHKERRQ(ierr); 249797c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 249897c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 249997c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2500ef0bb6c7SMatthew G. Knepley PetscTabulation T = NULL; 2501d69c5d34SMatthew G. Knepley 2502d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 25030f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 25040f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 2505ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 2506ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 25079c3cf19fSMatthew 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); 2508ef0bb6c7SMatthew G. Knepley ierr = PetscFECreateTabulation(fe, 1, npoints, points, 0, &T);CHKERRQ(ierr); 2509d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 2510d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 25119c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 25129c3cf19fSMatthew 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); 2513d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 251436a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2515d172c84bSMatthew G. Knepley /* 2516d172c84bSMatthew G. Knepley cTotDim: Total columns in element interpolation matrix, sum of number of dual basis functionals in each field 2517d172c84bSMatthew G. Knepley offsetI, offsetJ: Offsets into the larger element interpolation matrix for different fields 2518d172c84bSMatthew G. Knepley fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals 2519d172c84bSMatthew G. Knepley qNC, Nc, Ncj, c: Number of components in this field 2520d172c84bSMatthew G. Knepley Np, p: Number of quad points in the fine grid functional i 2521d172c84bSMatthew G. Knepley k: i*Np + p, overall point number for the interpolation 2522d172c84bSMatthew G. Knepley */ 2523ef0bb6c7SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += T->T[0][k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c]; 252436a6d9c0SMatthew G. Knepley } 2525d69c5d34SMatthew G. Knepley } 2526d69c5d34SMatthew G. Knepley } 2527ef0bb6c7SMatthew G. Knepley ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);CHKERRQ(ierr); 2528ffe73a53SMatthew G. Knepley } 252997c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 253097c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 253197c42addSMatthew G. Knepley 253297c42addSMatthew G. Knepley /* Evaluate constant function at points */ 253397c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 253497c42addSMatthew G. Knepley cpdim = 1; 253597c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 253697c42addSMatthew G. Knepley if (fieldI == fieldJ) { 2537ff1e0c32SBarry Smith 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); 253897c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 253997c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 25409c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr); 25419c3cf19fSMatthew 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); 254297c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 254397c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2544458eb97cSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c]; 254597c42addSMatthew G. Knepley } 254697c42addSMatthew G. Knepley } 254797c42addSMatthew G. Knepley } 254897c42addSMatthew G. Knepley } 254997c42addSMatthew G. Knepley } 2550d172c84bSMatthew G. Knepley offsetJ += cpdim; 2551d69c5d34SMatthew G. Knepley } 2552d172c84bSMatthew G. Knepley offsetI += fpdim; 2553549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 2554d69c5d34SMatthew G. Knepley } 25550f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 25567f5b169aSMatthew G. Knepley /* Preallocate matrix */ 25577f5b169aSMatthew G. Knepley { 2558c094ef40SMatthew G. Knepley Mat preallocator; 2559c094ef40SMatthew G. Knepley PetscScalar *vals; 2560c094ef40SMatthew G. Knepley PetscInt *cellCIndices, *cellFIndices; 2561c094ef40SMatthew G. Knepley PetscInt locRows, locCols, cell; 25627f5b169aSMatthew G. Knepley 2563c094ef40SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr); 2564c094ef40SMatthew G. Knepley ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr); 2565c094ef40SMatthew G. Knepley ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr); 2566c094ef40SMatthew G. Knepley ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr); 2567c094ef40SMatthew G. Knepley ierr = MatSetUp(preallocator);CHKERRQ(ierr); 2568c094ef40SMatthew G. Knepley ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 25697f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2570cf51de39SMatthew G. Knepley if (isRefined) { 25717f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 2572c094ef40SMatthew G. Knepley ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr); 2573cf51de39SMatthew G. Knepley } else { 2574cf51de39SMatthew G. Knepley ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, preallocator, cell, vals, INSERT_VALUES);CHKERRQ(ierr); 2575cf51de39SMatthew G. Knepley } 25767f5b169aSMatthew G. Knepley } 2577c094ef40SMatthew G. Knepley ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr); 2578c094ef40SMatthew G. Knepley ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2579c094ef40SMatthew G. Knepley ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2580c094ef40SMatthew G. Knepley ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr); 2581c094ef40SMatthew G. Knepley ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 25827f5b169aSMatthew G. Knepley } 25837f5b169aSMatthew G. Knepley /* Fill matrix */ 25847f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 2585d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2586cf51de39SMatthew G. Knepley if (isRefined) { 2587934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 2588cf51de39SMatthew G. Knepley } else { 2589cf51de39SMatthew G. Knepley ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 2590cf51de39SMatthew G. Knepley } 2591d69c5d34SMatthew G. Knepley } 2592549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 259397c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 2594549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 2595934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2596934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2597d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 2598825f8a23SLisandro Dalcin ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr); 2599934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 2600825f8a23SLisandro Dalcin ierr = MatView(In, NULL);CHKERRQ(ierr); 2601d69c5d34SMatthew G. Knepley } 2602d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 2603d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 2604d69c5d34SMatthew G. Knepley } 26056c73c22cSMatthew G. Knepley 2606bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user) 2607bd041c0cSMatthew G. Knepley { 2608bd041c0cSMatthew G. Knepley SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness"); 2609bd041c0cSMatthew G. Knepley } 2610bd041c0cSMatthew G. Knepley 261168132eb9SMatthew G. Knepley /*@ 261268132eb9SMatthew G. Knepley DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM. 261368132eb9SMatthew G. Knepley 261468132eb9SMatthew G. Knepley Input Parameters: 261568132eb9SMatthew G. Knepley + dmf - The fine mesh 261668132eb9SMatthew G. Knepley . dmc - The coarse mesh 261768132eb9SMatthew G. Knepley - user - The user context 261868132eb9SMatthew G. Knepley 261968132eb9SMatthew G. Knepley Output Parameter: 262068132eb9SMatthew G. Knepley . In - The interpolation matrix 262168132eb9SMatthew G. Knepley 262268132eb9SMatthew G. Knepley Level: developer 262368132eb9SMatthew G. Knepley 262468132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 262568132eb9SMatthew G. Knepley @*/ 262668132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user) 26274ef9d792SMatthew G. Knepley { 262864e98e1dSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 262964e98e1dSMatthew G. Knepley const char *name = "Interpolator"; 26304ef9d792SMatthew G. Knepley PetscDS prob; 26314ef9d792SMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 2632e8f14785SLisandro Dalcin PetscHSetIJ ht; 26334ef9d792SMatthew G. Knepley PetscLayout rLayout; 26344ef9d792SMatthew G. Knepley PetscInt *dnz, *onz; 26354ef9d792SMatthew G. Knepley PetscInt locRows, rStart, rEnd; 26364ef9d792SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 26374ef9d792SMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 26384ef9d792SMatthew G. Knepley PetscScalar *elemMat; 26394ef9d792SMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 26404ef9d792SMatthew G. Knepley PetscErrorCode ierr; 26414ef9d792SMatthew G. Knepley 26424ef9d792SMatthew G. Knepley PetscFunctionBegin; 264377711781SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 26444ef9d792SMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 26454ef9d792SMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 26464bee2e38SMatthew G. Knepley ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr); 26474ef9d792SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 26484ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 26494ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 265092fd8e1eSJed Brown ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr); 2651e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 265292fd8e1eSJed Brown ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr); 2653e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 26544ef9d792SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 26554ef9d792SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 26569c3cf19fSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 26574ef9d792SMatthew G. Knepley 26584ef9d792SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 26594ef9d792SMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 26604ef9d792SMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 26614ef9d792SMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 26624ef9d792SMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 26634ef9d792SMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 26644ef9d792SMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 26654ef9d792SMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 2666e8f14785SLisandro Dalcin ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr); 26674ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 26684ef9d792SMatthew G. Knepley PetscObject obj; 26694ef9d792SMatthew G. Knepley PetscClassId id; 2670c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 26714ef9d792SMatthew G. Knepley PetscQuadrature f; 267217f047d8SMatthew G. Knepley const PetscReal *qpoints; 267317f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 26744ef9d792SMatthew G. Knepley 26754ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 26764ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 26774ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 26784ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 26794ef9d792SMatthew G. Knepley 26804ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 26814ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 26824ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 26834ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 26844ef9d792SMatthew G. Knepley 26854ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 26864ef9d792SMatthew G. Knepley Nc = 1; 26874ef9d792SMatthew G. Knepley } 26884ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 26894ef9d792SMatthew G. Knepley /* For each fine grid cell */ 26904ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 26914ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 26924ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 26934ef9d792SMatthew G. Knepley 269471f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 26954ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2696ff1e0c32SBarry Smith if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim); 26974ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 26984ef9d792SMatthew G. Knepley Vec pointVec; 26994ef9d792SMatthew G. Knepley PetscScalar *pV; 27003a93e3b7SToby Isaac PetscSF coarseCellSF = NULL; 27013a93e3b7SToby Isaac const PetscSFNode *coarseCells; 27029c3cf19fSMatthew G. Knepley PetscInt numCoarseCells, q, c; 27034ef9d792SMatthew G. Knepley 27044ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 27054ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 27069c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 27074ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 27084ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 27094ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 27104ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 2711c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2712c330f8ffSToby Isaac 27134ef9d792SMatthew G. Knepley /* Transform point to real space */ 2714c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 27154ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 27164ef9d792SMatthew G. Knepley } 27174ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 27184ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 27191555c271SMatthew G. Knepley /* OPT: Pack all quad points from fine cell */ 272062a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 27213a93e3b7SToby Isaac ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 27224ef9d792SMatthew G. Knepley /* Update preallocation info */ 27233a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 27243a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 27259c3cf19fSMatthew G. Knepley { 2726e8f14785SLisandro Dalcin PetscHashIJKey key; 2727e8f14785SLisandro Dalcin PetscBool missing; 27284ef9d792SMatthew G. Knepley 2729e8f14785SLisandro Dalcin key.i = findices[i]; 2730e8f14785SLisandro Dalcin if (key.i >= 0) { 27314ef9d792SMatthew G. Knepley /* Get indices for coarse elements */ 27324ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 273371f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 27344ef9d792SMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 2735e8f14785SLisandro Dalcin key.j = cindices[c]; 2736e8f14785SLisandro Dalcin if (key.j < 0) continue; 2737e8f14785SLisandro Dalcin ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr); 27384ef9d792SMatthew G. Knepley if (missing) { 2739e8f14785SLisandro Dalcin if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart]; 2740e8f14785SLisandro Dalcin else ++onz[key.i-rStart]; 27414ef9d792SMatthew G. Knepley } 27424ef9d792SMatthew G. Knepley } 274371f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 27444ef9d792SMatthew G. Knepley } 27454ef9d792SMatthew G. Knepley } 27468c543595SMatthew G. Knepley } 27473a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 27484ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 27494ef9d792SMatthew G. Knepley } 275071f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 27514ef9d792SMatthew G. Knepley } 27524ef9d792SMatthew G. Knepley } 2753e8f14785SLisandro Dalcin ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr); 27544ef9d792SMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 27554ef9d792SMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 27564ef9d792SMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 27574ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 27584ef9d792SMatthew G. Knepley PetscObject obj; 27594ef9d792SMatthew G. Knepley PetscClassId id; 2760c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 2761ef0bb6c7SMatthew G. Knepley PetscTabulation T = NULL; 27624ef9d792SMatthew G. Knepley PetscQuadrature f; 27634ef9d792SMatthew G. Knepley const PetscReal *qpoints, *qweights; 27649c3cf19fSMatthew G. Knepley PetscInt Nc, qNc, Np, fpdim, i, d; 27654ef9d792SMatthew G. Knepley 27664ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 27674ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 27684ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 27694ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 27704ef9d792SMatthew G. Knepley 27714ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 27724ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 2773ef0bb6c7SMatthew G. Knepley ierr = PetscFECreateTabulation(fe, 1, 1, x, 0, &T);CHKERRQ(ierr); 27744ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 27754ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 27764ef9d792SMatthew G. Knepley 27774ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 27784ef9d792SMatthew G. Knepley Nc = 1; 2779ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",field); 27804ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 27814ef9d792SMatthew G. Knepley /* For each fine grid cell */ 27824ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 27834ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 27844ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 27854ef9d792SMatthew G. Knepley 278671f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 27874ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2788ff1e0c32SBarry Smith if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim); 27894ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 27904ef9d792SMatthew G. Knepley Vec pointVec; 27914ef9d792SMatthew G. Knepley PetscScalar *pV; 279212111d7cSToby Isaac PetscSF coarseCellSF = NULL; 27933a93e3b7SToby Isaac const PetscSFNode *coarseCells; 279417f047d8SMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 27954ef9d792SMatthew G. Knepley 27964ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 27974ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 27989c3cf19fSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr); 27999c3cf19fSMatthew 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); 28004ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 28014ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 28024ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 28034ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 2804c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2805c330f8ffSToby Isaac 28064ef9d792SMatthew G. Knepley /* Transform point to real space */ 2807c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 28084ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 28094ef9d792SMatthew G. Knepley } 28104ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 28114ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 28121555c271SMatthew G. Knepley /* OPT: Read this out from preallocation information */ 281362a38674SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 28144ef9d792SMatthew G. Knepley /* Update preallocation info */ 28153a93e3b7SToby Isaac ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 28163a93e3b7SToby Isaac if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 28174ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 28184ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 2819826eb36dSMatthew G. Knepley PetscReal pVReal[3]; 2820c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2821826eb36dSMatthew G. Knepley 282271f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 28234ef9d792SMatthew G. Knepley /* Transform points from real space to coarse reference space */ 28243a93e3b7SToby Isaac ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 2825e2d86523SMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 2826c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 28274ef9d792SMatthew G. Knepley 28284ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 28294ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 28304ef9d792SMatthew G. Knepley 28314ef9d792SMatthew G. Knepley /* Evaluate coarse basis on contained point */ 28324ef9d792SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 2833ef0bb6c7SMatthew G. Knepley ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr); 2834580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr); 28354ef9d792SMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 28364ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 2837ef0bb6c7SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*qweights[ccell*qNc + c]; 28384ef9d792SMatthew G. Knepley } 28394ef9d792SMatthew G. Knepley } else { 28404ef9d792SMatthew G. Knepley cpdim = 1; 28414ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 28429c3cf19fSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c]; 28434ef9d792SMatthew G. Knepley } 28444ef9d792SMatthew G. Knepley } 28454ef9d792SMatthew G. Knepley /* Update interpolator */ 28469c3cf19fSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 28479c3cf19fSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 28489c3cf19fSMatthew G. Knepley ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr); 284971f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 28504ef9d792SMatthew G. Knepley } 28514ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 28523a93e3b7SToby Isaac ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 28534ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 28544ef9d792SMatthew G. Knepley } 285571f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 28564ef9d792SMatthew G. Knepley } 2857ef0bb6c7SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);} 28584ef9d792SMatthew G. Knepley } 28594ef9d792SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 28604ef9d792SMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 28614ef9d792SMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 28624ef9d792SMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28634ef9d792SMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 286477711781SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 28654ef9d792SMatthew G. Knepley PetscFunctionReturn(0); 28664ef9d792SMatthew G. Knepley } 28674ef9d792SMatthew G. Knepley 286846fa42a0SMatthew G. Knepley /*@ 2869bd041c0cSMatthew G. Knepley DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM. 2870bd041c0cSMatthew G. Knepley 2871bd041c0cSMatthew G. Knepley Input Parameters: 2872bd041c0cSMatthew G. Knepley + dmf - The fine mesh 2873bd041c0cSMatthew G. Knepley . dmc - The coarse mesh 2874bd041c0cSMatthew G. Knepley - user - The user context 2875bd041c0cSMatthew G. Knepley 2876bd041c0cSMatthew G. Knepley Output Parameter: 2877bd041c0cSMatthew G. Knepley . mass - The mass matrix 2878bd041c0cSMatthew G. Knepley 2879bd041c0cSMatthew G. Knepley Level: developer 2880bd041c0cSMatthew G. Knepley 2881bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 2882bd041c0cSMatthew G. Knepley @*/ 2883bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user) 2884bd041c0cSMatthew G. Knepley { 2885bd041c0cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 2886bd041c0cSMatthew G. Knepley const char *name = "Mass Matrix"; 2887bd041c0cSMatthew G. Knepley PetscDS prob; 2888bd041c0cSMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 2889e8f14785SLisandro Dalcin PetscHSetIJ ht; 2890bd041c0cSMatthew G. Knepley PetscLayout rLayout; 2891bd041c0cSMatthew G. Knepley PetscInt *dnz, *onz; 2892bd041c0cSMatthew G. Knepley PetscInt locRows, rStart, rEnd; 2893bd041c0cSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 2894bd041c0cSMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 2895bd041c0cSMatthew G. Knepley PetscScalar *elemMat; 2896bd041c0cSMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 2897bd041c0cSMatthew G. Knepley PetscErrorCode ierr; 2898bd041c0cSMatthew G. Knepley 2899bd041c0cSMatthew G. Knepley PetscFunctionBegin; 2900bd041c0cSMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 2901bd041c0cSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 29024bee2e38SMatthew G. Knepley ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr); 2903bd041c0cSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 2904bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 2905bd041c0cSMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 290692fd8e1eSJed Brown ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr); 2907e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 290892fd8e1eSJed Brown ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr); 2909e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 2910bd041c0cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 2911bd041c0cSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 2912bd041c0cSMatthew G. Knepley ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr); 2913bd041c0cSMatthew G. Knepley 2914bd041c0cSMatthew G. Knepley ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr); 2915bd041c0cSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr); 2916bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 2917bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 2918bd041c0cSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 2919bd041c0cSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 2920bd041c0cSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 2921bd041c0cSMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 2922e8f14785SLisandro Dalcin ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr); 2923bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2924bd041c0cSMatthew G. Knepley PetscObject obj; 2925bd041c0cSMatthew G. Knepley PetscClassId id; 2926bd041c0cSMatthew G. Knepley PetscQuadrature quad; 2927bd041c0cSMatthew G. Knepley const PetscReal *qpoints; 2928bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 2929bd041c0cSMatthew G. Knepley 2930bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 2931bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2932bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);} 2933bd041c0cSMatthew G. Knepley else {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);} 2934bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr); 2935bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 2936bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 2937bd041c0cSMatthew G. Knepley Vec pointVec; 2938bd041c0cSMatthew G. Knepley PetscScalar *pV; 2939bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 2940bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 2941bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, q, c; 2942bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 2943bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 2944bd041c0cSMatthew G. Knepley 294571f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 2946bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 2947bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 2948bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 2949bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 2950bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 2951bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 2952c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 2953c330f8ffSToby Isaac 2954bd041c0cSMatthew G. Knepley /* Transform point to real space */ 2955c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 2956bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 2957bd041c0cSMatthew G. Knepley } 2958bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 2959bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 2960bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 2961bd041c0cSMatthew G. Knepley ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr); 2962bd041c0cSMatthew G. Knepley /* Update preallocation info */ 2963bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 2964bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 2965bd041c0cSMatthew G. Knepley { 2966e8f14785SLisandro Dalcin PetscHashIJKey key; 2967e8f14785SLisandro Dalcin PetscBool missing; 2968bd041c0cSMatthew G. Knepley 2969bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 2970e8f14785SLisandro Dalcin key.i = findices[i]; 2971e8f14785SLisandro Dalcin if (key.i >= 0) { 2972bd041c0cSMatthew G. Knepley /* Get indices for coarse elements */ 2973bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 297471f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 2975bd041c0cSMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 2976e8f14785SLisandro Dalcin key.j = cindices[c]; 2977e8f14785SLisandro Dalcin if (key.j < 0) continue; 2978e8f14785SLisandro Dalcin ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr); 2979bd041c0cSMatthew G. Knepley if (missing) { 2980e8f14785SLisandro Dalcin if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart]; 2981e8f14785SLisandro Dalcin else ++onz[key.i-rStart]; 2982bd041c0cSMatthew G. Knepley } 2983bd041c0cSMatthew G. Knepley } 298471f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 2985bd041c0cSMatthew G. Knepley } 2986bd041c0cSMatthew G. Knepley } 2987bd041c0cSMatthew G. Knepley } 2988bd041c0cSMatthew G. Knepley } 2989bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 2990bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 299171f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 2992bd041c0cSMatthew G. Knepley } 2993bd041c0cSMatthew G. Knepley } 2994e8f14785SLisandro Dalcin ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr); 2995bd041c0cSMatthew G. Knepley ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 2996bd041c0cSMatthew G. Knepley ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 2997bd041c0cSMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 2998bd041c0cSMatthew G. Knepley for (field = 0; field < Nf; ++field) { 2999bd041c0cSMatthew G. Knepley PetscObject obj; 3000bd041c0cSMatthew G. Knepley PetscClassId id; 3001ef0bb6c7SMatthew G. Knepley PetscTabulation T, Tfine; 3002bd041c0cSMatthew G. Knepley PetscQuadrature quad; 3003bd041c0cSMatthew G. Knepley const PetscReal *qpoints, *qweights; 3004bd041c0cSMatthew G. Knepley PetscInt Nq, Nc, i, d; 3005bd041c0cSMatthew G. Knepley 3006bd041c0cSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 3007bd041c0cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 3008ef0bb6c7SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 3009ef0bb6c7SMatthew G. Knepley ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr); 3010ef0bb6c7SMatthew G. Knepley ierr = PetscFEGetCellTabulation((PetscFE) obj, &Tfine);CHKERRQ(ierr); 3011ef0bb6c7SMatthew G. Knepley ierr = PetscFECreateTabulation((PetscFE) obj, 1, 1, x, 0, &T);CHKERRQ(ierr); 3012ef0bb6c7SMatthew G. Knepley } else { 3013ef0bb6c7SMatthew G. Knepley ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr); 3014ef0bb6c7SMatthew G. Knepley } 3015bd041c0cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr); 3016bd041c0cSMatthew G. Knepley /* For each fine grid cell */ 3017bd041c0cSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 3018bd041c0cSMatthew G. Knepley Vec pointVec; 3019bd041c0cSMatthew G. Knepley PetscScalar *pV; 3020bd041c0cSMatthew G. Knepley PetscSF coarseCellSF = NULL; 3021bd041c0cSMatthew G. Knepley const PetscSFNode *coarseCells; 3022bd041c0cSMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 3023bd041c0cSMatthew G. Knepley PetscInt *findices, *cindices; 3024bd041c0cSMatthew G. Knepley PetscInt numFIndices, numCIndices; 3025bd041c0cSMatthew G. Knepley 302671f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 3027bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 3028bd041c0cSMatthew G. Knepley /* Get points from the quadrature */ 3029bd041c0cSMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr); 3030bd041c0cSMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 3031bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 3032bd041c0cSMatthew G. Knepley for (q = 0; q < Nq; ++q) { 3033c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 3034c330f8ffSToby Isaac 3035bd041c0cSMatthew G. Knepley /* Transform point to real space */ 3036c330f8ffSToby Isaac CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x); 3037bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 3038bd041c0cSMatthew G. Knepley } 3039bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 3040bd041c0cSMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 3041bd041c0cSMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr); 3042bd041c0cSMatthew G. Knepley /* Update matrix */ 3043bd041c0cSMatthew G. Knepley ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr); 3044bd041c0cSMatthew G. Knepley if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located"); 3045bd041c0cSMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 3046bd041c0cSMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 3047bd041c0cSMatthew G. Knepley PetscReal pVReal[3]; 3048c330f8ffSToby Isaac const PetscReal xi0[3] = {-1., -1., -1.}; 3049c330f8ffSToby Isaac 3050bd041c0cSMatthew G. Knepley 305171f0bbf9SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 3052bd041c0cSMatthew G. Knepley /* Transform points from real space to coarse reference space */ 3053bd041c0cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 3054bd041c0cSMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 3055c330f8ffSToby Isaac CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x); 3056bd041c0cSMatthew G. Knepley 3057bd041c0cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 3058bd041c0cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 3059bd041c0cSMatthew G. Knepley 3060bd041c0cSMatthew G. Knepley /* Evaluate coarse basis on contained point */ 3061bd041c0cSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 3062ef0bb6c7SMatthew G. Knepley ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr); 3063bd041c0cSMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 3064bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 3065580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr); 3066bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 3067ef0bb6c7SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*Tfine->T[0][(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ; 3068bd041c0cSMatthew G. Knepley } 3069bd041c0cSMatthew G. Knepley /* Update interpolator */ 3070bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 3071bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 3072bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 3073bd041c0cSMatthew G. Knepley } 3074bd041c0cSMatthew G. Knepley } else { 3075bd041c0cSMatthew G. Knepley cpdim = 1; 3076bd041c0cSMatthew G. Knepley for (i = 0; i < numFIndices; ++i) { 3077580bdb30SBarry Smith ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr); 3078bd041c0cSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 3079bd041c0cSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ; 3080bd041c0cSMatthew G. Knepley } 3081bd041c0cSMatthew G. Knepley /* Update interpolator */ 3082bd041c0cSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);} 3083ff1e0c32SBarry Smith ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %D %D Nf: %D %D Nc: %D %D\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr); 3084bd041c0cSMatthew G. Knepley if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim); 3085bd041c0cSMatthew G. Knepley ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr); 3086bd041c0cSMatthew G. Knepley } 3087bd041c0cSMatthew G. Knepley } 308871f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr); 3089bd041c0cSMatthew G. Knepley } 3090bd041c0cSMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 3091bd041c0cSMatthew G. Knepley ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr); 3092bd041c0cSMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 309371f0bbf9SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr); 3094bd041c0cSMatthew G. Knepley } 3095ef0bb6c7SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);} 3096bd041c0cSMatthew G. Knepley } 3097bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 3098bd041c0cSMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 3099bd041c0cSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 3100bd041c0cSMatthew G. Knepley ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3101bd041c0cSMatthew G. Knepley ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3102bd041c0cSMatthew G. Knepley PetscFunctionReturn(0); 3103bd041c0cSMatthew G. Knepley } 3104bd041c0cSMatthew G. Knepley 3105bd041c0cSMatthew G. Knepley /*@ 310646fa42a0SMatthew G. Knepley DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns 310746fa42a0SMatthew G. Knepley 310846fa42a0SMatthew G. Knepley Input Parameters: 310946fa42a0SMatthew G. Knepley + dmc - The coarse mesh 311046fa42a0SMatthew G. Knepley - dmf - The fine mesh 311146fa42a0SMatthew G. Knepley - user - The user context 311246fa42a0SMatthew G. Knepley 311346fa42a0SMatthew G. Knepley Output Parameter: 311446fa42a0SMatthew G. Knepley . sc - The mapping 311546fa42a0SMatthew G. Knepley 311646fa42a0SMatthew G. Knepley Level: developer 311746fa42a0SMatthew G. Knepley 311846fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 311946fa42a0SMatthew G. Knepley @*/ 31207c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 31217c927364SMatthew G. Knepley { 3122e9d4ef1bSMatthew G. Knepley PetscDS prob; 31237c927364SMatthew G. Knepley PetscFE *feRef; 312497c42addSMatthew G. Knepley PetscFV *fvRef; 31257c927364SMatthew G. Knepley Vec fv, cv; 31267c927364SMatthew G. Knepley IS fis, cis; 31277c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 31287c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 3129485ad865SMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, endC, offsetC, offsetF, m; 31306f3d3cbcSMatthew G. Knepley PetscBool *needAvg; 31317c927364SMatthew G. Knepley PetscErrorCode ierr; 31327c927364SMatthew G. Knepley 31337c927364SMatthew G. Knepley PetscFunctionBegin; 313475a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 3135c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 313692fd8e1eSJed Brown ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr); 3137e87a4003SBarry Smith ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 313892fd8e1eSJed Brown ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr); 3139e87a4003SBarry Smith ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 31407c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 3141412e9a14SMatthew G. Knepley ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 3142e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 31436f3d3cbcSMatthew G. Knepley ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr); 31447c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 314597c42addSMatthew G. Knepley PetscObject obj; 314697c42addSMatthew G. Knepley PetscClassId id; 3147aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 31487c927364SMatthew G. Knepley 314997c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 315097c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 315197c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 315297c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 31536f3d3cbcSMatthew G. Knepley PetscSpace sp; 31549b2fc754SMatthew G. Knepley PetscInt maxDegree; 315597c42addSMatthew G. Knepley 31567c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 31577c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 31587c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 31596f3d3cbcSMatthew G. Knepley ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr); 31609b2fc754SMatthew G. Knepley ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr); 31619b2fc754SMatthew G. Knepley if (!maxDegree) needAvg[f] = PETSC_TRUE; 316297c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 316397c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 316497c42addSMatthew G. Knepley PetscDualSpace Q; 316597c42addSMatthew G. Knepley 316697c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 316797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 316897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 316997c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 31706f3d3cbcSMatthew G. Knepley needAvg[f] = PETSC_TRUE; 317197c42addSMatthew G. Knepley } 3172d172c84bSMatthew G. Knepley fTotDim += fNb; 31737c927364SMatthew G. Knepley } 3174e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 31757c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 31767c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 31777c927364SMatthew G. Knepley PetscFE feC; 317897c42addSMatthew G. Knepley PetscFV fvC; 31797c927364SMatthew G. Knepley PetscDualSpace QF, QC; 3180d172c84bSMatthew G. Knepley PetscInt order = -1, NcF, NcC, fpdim, cpdim; 31817c927364SMatthew G. Knepley 318297c42addSMatthew G. Knepley if (feRef[field]) { 3183e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 31847c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 31857c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 31867c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 3187d172c84bSMatthew G. Knepley ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr); 31887c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 31897c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 31907c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 319197c42addSMatthew G. Knepley } else { 319297c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 319397c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 319497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 319597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 319697c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 319797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 319897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 319997c42addSMatthew G. Knepley } 3200ff1e0c32SBarry Smith 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); 32017c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 32027c927364SMatthew G. Knepley PetscQuadrature cfunc; 3203d172c84bSMatthew G. Knepley const PetscReal *cqpoints, *cqweights; 3204d172c84bSMatthew G. Knepley PetscInt NqcC, NpC; 320597c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 32067c927364SMatthew G. Knepley 32077c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 3208d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr); 3209ff1e0c32SBarry Smith if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcC, NcC); 321097c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 32117c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 32127c927364SMatthew G. Knepley PetscQuadrature ffunc; 3213d172c84bSMatthew G. Knepley const PetscReal *fqpoints, *fqweights; 32147c927364SMatthew G. Knepley PetscReal sum = 0.0; 3215d172c84bSMatthew G. Knepley PetscInt NqcF, NpF; 32167c927364SMatthew G. Knepley 32177c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 3218d172c84bSMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr); 3219ff1e0c32SBarry Smith if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcF, NcF); 32207c927364SMatthew G. Knepley if (NpC != NpF) continue; 32217c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 32227c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 3223d172c84bSMatthew G. Knepley for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]); 3224d172c84bSMatthew G. Knepley if (sum < 1.0e-9) continue; 3225d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+f; 322697c42addSMatthew G. Knepley found = PETSC_TRUE; 32277c927364SMatthew G. Knepley break; 32287c927364SMatthew G. Knepley } 322997c42addSMatthew G. Knepley if (!found) { 323097c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 3231d172c84bSMatthew G. Knepley if (fvRef[field] || (feRef[field] && order == 0)) { 3232d172c84bSMatthew G. Knepley cmap[offsetC+c] = offsetF+0; 323397c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 323497c42addSMatthew G. Knepley } 32357c927364SMatthew G. Knepley } 3236d172c84bSMatthew G. Knepley offsetC += cpdim; 3237d172c84bSMatthew G. Knepley offsetF += fpdim; 32387c927364SMatthew G. Knepley } 323997c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 32406f3d3cbcSMatthew G. Knepley ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr); 32417c927364SMatthew G. Knepley 32427c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 32437c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 32440bd915a7SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr); 32457c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 32467c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 3247aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 3248aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 32497c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 32507c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 32517c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 32527c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 32530bd915a7SMatthew G. Knepley if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue; 3254ff1e0c32SBarry Smith 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]]); 32557c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 32567c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 32577c927364SMatthew G. Knepley } 32587c927364SMatthew G. Knepley } 32597c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 32607c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 32617c927364SMatthew G. Knepley 32627c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 32637c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 32649448b7f1SJunchao Zhang ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 32657c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 32667c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 32677c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 32687c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 326975a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 3270cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 3271cb1e1211SMatthew G Knepley } 3272a1cf66bbSMatthew G. Knepley 32732f856554SMatthew G. Knepley /*@C 32742f856554SMatthew G. Knepley DMPlexGetCellFields - Retrieve the field values values for a chunk of cells 32752f856554SMatthew G. Knepley 32762f856554SMatthew G. Knepley Input Parameters: 32772f856554SMatthew G. Knepley + dm - The DM 32782f856554SMatthew G. Knepley . cellIS - The cells to include 32792f856554SMatthew G. Knepley . locX - A local vector with the solution fields 32802f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 32812f856554SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 32822f856554SMatthew G. Knepley 32832f856554SMatthew G. Knepley Output Parameters: 32842f856554SMatthew G. Knepley + u - The field coefficients 32852f856554SMatthew G. Knepley . u_t - The fields derivative coefficients 32862f856554SMatthew G. Knepley - a - The auxiliary field coefficients 32872f856554SMatthew G. Knepley 32882f856554SMatthew G. Knepley Level: developer 32892f856554SMatthew G. Knepley 32902f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 32912f856554SMatthew G. Knepley @*/ 32922f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 32932f856554SMatthew G. Knepley { 32942f856554SMatthew G. Knepley DM plex, plexA = NULL; 3295a6e0b375SMatthew G. Knepley DMEnclosureType encAux; 32962f856554SMatthew G. Knepley PetscSection section, sectionAux; 32972f856554SMatthew G. Knepley PetscDS prob; 32982f856554SMatthew G. Knepley const PetscInt *cells; 32992f856554SMatthew G. Knepley PetscInt cStart, cEnd, numCells, totDim, totDimAux, c; 33002f856554SMatthew G. Knepley PetscErrorCode ierr; 33012f856554SMatthew G. Knepley 33022f856554SMatthew G. Knepley PetscFunctionBegin; 33032f856554SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 33042f856554SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 33052f856554SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 33062f856554SMatthew G. Knepley if (locA) {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);} 33072f856554SMatthew G. Knepley PetscValidPointer(u, 7); 33082f856554SMatthew G. Knepley PetscValidPointer(u_t, 8); 33092f856554SMatthew G. Knepley PetscValidPointer(a, 9); 33102f856554SMatthew G. Knepley ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr); 33112f856554SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 331292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 3313083401c6SMatthew G. Knepley ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr); 33142f856554SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 33152f856554SMatthew G. Knepley if (locA) { 33162f856554SMatthew G. Knepley DM dmAux; 33172f856554SMatthew G. Knepley PetscDS probAux; 33182f856554SMatthew G. Knepley 33192f856554SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 3320a6e0b375SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 33212f856554SMatthew G. Knepley ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr); 332292fd8e1eSJed Brown ierr = DMGetLocalSection(dmAux, §ionAux);CHKERRQ(ierr); 33232f856554SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 33242f856554SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 33252f856554SMatthew G. Knepley } 33262f856554SMatthew G. Knepley numCells = cEnd - cStart; 33272f856554SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr); 33282f856554SMatthew G. Knepley if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;} 33292f856554SMatthew G. Knepley if (locA) {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;} 33302f856554SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 33312f856554SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 33322f856554SMatthew G. Knepley const PetscInt cind = c - cStart; 33332f856554SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a; 33342f856554SMatthew G. Knepley PetscInt i; 33352f856554SMatthew G. Knepley 33362f856554SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr); 33372f856554SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i]; 33382f856554SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr); 33392f856554SMatthew G. Knepley if (locX_t) { 33402f856554SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr); 33412f856554SMatthew G. Knepley for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i]; 33422f856554SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr); 33432f856554SMatthew G. Knepley } 33442f856554SMatthew G. Knepley if (locA) { 33452f856554SMatthew G. Knepley PetscInt subcell; 3346a6e0b375SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, cell, &subcell);CHKERRQ(ierr); 33472f856554SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr); 33482f856554SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i]; 33492f856554SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr); 33502f856554SMatthew G. Knepley } 33512f856554SMatthew G. Knepley } 33522f856554SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 33532f856554SMatthew G. Knepley if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 33542f856554SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 33552f856554SMatthew G. Knepley PetscFunctionReturn(0); 33562f856554SMatthew G. Knepley } 33572f856554SMatthew G. Knepley 33582f856554SMatthew G. Knepley /*@C 33592f856554SMatthew G. Knepley DMPlexRestoreCellFields - Restore the field values values for a chunk of cells 33602f856554SMatthew G. Knepley 33612f856554SMatthew G. Knepley Input Parameters: 33622f856554SMatthew G. Knepley + dm - The DM 33632f856554SMatthew G. Knepley . cellIS - The cells to include 33642f856554SMatthew G. Knepley . locX - A local vector with the solution fields 33652f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 33662f856554SMatthew G. Knepley - locA - A local vector with auxiliary fields, or NULL 33672f856554SMatthew G. Knepley 33682f856554SMatthew G. Knepley Output Parameters: 33692f856554SMatthew G. Knepley + u - The field coefficients 33702f856554SMatthew G. Knepley . u_t - The fields derivative coefficients 33712f856554SMatthew G. Knepley - a - The auxiliary field coefficients 33722f856554SMatthew G. Knepley 33732f856554SMatthew G. Knepley Level: developer 33742f856554SMatthew G. Knepley 33752f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 33762f856554SMatthew G. Knepley @*/ 33772f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a) 33782f856554SMatthew G. Knepley { 33792f856554SMatthew G. Knepley PetscErrorCode ierr; 33802f856554SMatthew G. Knepley 33812f856554SMatthew G. Knepley PetscFunctionBegin; 33822f856554SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr); 33832f856554SMatthew G. Knepley if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);} 33842f856554SMatthew G. Knepley if (locA) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);} 33852f856554SMatthew G. Knepley PetscFunctionReturn(0); 33862f856554SMatthew G. Knepley } 33872f856554SMatthew G. Knepley 33882f856554SMatthew G. Knepley /*@C 33892f856554SMatthew G. Knepley DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces 33902f856554SMatthew G. Knepley 33912f856554SMatthew G. Knepley Input Parameters: 33922f856554SMatthew G. Knepley + dm - The DM 33932f856554SMatthew G. Knepley . fStart - The first face to include 33942f856554SMatthew G. Knepley . fEnd - The first face to exclude 33952f856554SMatthew G. Knepley . locX - A local vector with the solution fields 33962f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 33972f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry 33982f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 33992f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 34002f856554SMatthew G. Knepley 34012f856554SMatthew G. Knepley Output Parameters: 34022f856554SMatthew G. Knepley + Nface - The number of faces with field values 34032f856554SMatthew G. Knepley . uL - The field values at the left side of the face 34042f856554SMatthew G. Knepley - uR - The field values at the right side of the face 34052f856554SMatthew G. Knepley 34062f856554SMatthew G. Knepley Level: developer 34072f856554SMatthew G. Knepley 34082f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields() 34092f856554SMatthew G. Knepley @*/ 34102f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR) 34112f856554SMatthew G. Knepley { 34122f856554SMatthew G. Knepley DM dmFace, dmCell, dmGrad = NULL; 34132f856554SMatthew G. Knepley PetscSection section; 34142f856554SMatthew G. Knepley PetscDS prob; 34152f856554SMatthew G. Knepley DMLabel ghostLabel; 34162f856554SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *x, *lgrad; 34172f856554SMatthew G. Knepley PetscBool *isFE; 34182f856554SMatthew G. Knepley PetscInt dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face; 34192f856554SMatthew G. Knepley PetscErrorCode ierr; 34202f856554SMatthew G. Knepley 34212f856554SMatthew G. Knepley PetscFunctionBegin; 34222f856554SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 34232f856554SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 4); 34242f856554SMatthew G. Knepley if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);} 34252f856554SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6); 34262f856554SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7); 34272f856554SMatthew G. Knepley if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);} 34282f856554SMatthew G. Knepley PetscValidPointer(uL, 9); 34292f856554SMatthew G. Knepley PetscValidPointer(uR, 10); 34302f856554SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 34312f856554SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 343292fd8e1eSJed Brown ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 34332f856554SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 34342f856554SMatthew G. Knepley ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr); 34352f856554SMatthew G. Knepley ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr); 34362f856554SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 34372f856554SMatthew G. Knepley PetscObject obj; 34382f856554SMatthew G. Knepley PetscClassId id; 34392f856554SMatthew G. Knepley 34402f856554SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 34412f856554SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 34422f856554SMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[f] = PETSC_TRUE;} 34432f856554SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;} 34442f856554SMatthew G. Knepley else {isFE[f] = PETSC_FALSE;} 34452f856554SMatthew G. Knepley } 34462f856554SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 34472f856554SMatthew G. Knepley ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr); 34482f856554SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 34492f856554SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 34502f856554SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 34512f856554SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 34522f856554SMatthew G. Knepley if (locGrad) { 34532f856554SMatthew G. Knepley ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr); 34542f856554SMatthew G. Knepley ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 34552f856554SMatthew G. Knepley } 34562f856554SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr); 34572f856554SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr); 34582f856554SMatthew G. Knepley /* Right now just eat the extra work for FE (could make a cell loop) */ 34592f856554SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 34602f856554SMatthew G. Knepley const PetscInt *cells; 34612f856554SMatthew G. Knepley PetscFVFaceGeom *fg; 34622f856554SMatthew G. Knepley PetscFVCellGeom *cgL, *cgR; 34632f856554SMatthew G. Knepley PetscScalar *xL, *xR, *gL, *gR; 34642f856554SMatthew G. Knepley PetscScalar *uLl = *uL, *uRl = *uR; 34652f856554SMatthew G. Knepley PetscInt ghost, nsupp, nchild; 34662f856554SMatthew G. Knepley 34672f856554SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 34682f856554SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 34692f856554SMatthew G. Knepley ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 34702f856554SMatthew G. Knepley if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 34712f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 34722f856554SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 34732f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 34742f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 34752f856554SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 34762f856554SMatthew G. Knepley PetscInt off; 34772f856554SMatthew G. Knepley 34782f856554SMatthew G. Knepley ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr); 34792f856554SMatthew G. Knepley if (isFE[f]) { 34802f856554SMatthew G. Knepley const PetscInt *cone; 34812f856554SMatthew G. Knepley PetscInt comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d; 34822f856554SMatthew G. Knepley 34832f856554SMatthew G. Knepley xL = xR = NULL; 34842f856554SMatthew G. Knepley ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 34852f856554SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 34862f856554SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 34872f856554SMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr); 34882f856554SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr); 34892f856554SMatthew G. Knepley for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break; 34902f856554SMatthew G. Knepley ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr); 34912f856554SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr); 34922f856554SMatthew G. Knepley for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break; 3493ff1e0c32SBarry Smith if (faceLocL == coneSizeL && faceLocR == coneSizeR) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of cell %D or cell %D", face, cells[0], cells[1]); 34942f856554SMatthew G. Knepley /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */ 34952f856554SMatthew G. Knepley /* TODO: this is a hack that might not be right for nonconforming */ 34962f856554SMatthew G. Knepley if (faceLocL < coneSizeL) { 3497a8f1f9e5SMatthew G. Knepley ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr); 3498a8f1f9e5SMatthew G. Knepley if (rdof == ldof && faceLocR < coneSizeR) {ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);} 34992f856554SMatthew G. Knepley else {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];} 35002f856554SMatthew G. Knepley } 35012f856554SMatthew G. Knepley else { 3502a8f1f9e5SMatthew G. Knepley ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr); 35032f856554SMatthew G. Knepley ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); 35042f856554SMatthew G. Knepley for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d]; 35052f856554SMatthew G. Knepley } 35062f856554SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr); 35072f856554SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr); 35082f856554SMatthew G. Knepley } else { 35092f856554SMatthew G. Knepley PetscFV fv; 35102f856554SMatthew G. Knepley PetscInt numComp, c; 35112f856554SMatthew G. Knepley 35122f856554SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr); 35132f856554SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr); 35142f856554SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr); 35152f856554SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr); 35162f856554SMatthew G. Knepley if (dmGrad) { 35172f856554SMatthew G. Knepley PetscReal dxL[3], dxR[3]; 35182f856554SMatthew G. Knepley 35192f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr); 35202f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr); 35212f856554SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL); 35222f856554SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR); 35232f856554SMatthew G. Knepley for (c = 0; c < numComp; ++c) { 35242f856554SMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL); 35252f856554SMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR); 35262f856554SMatthew G. Knepley } 35272f856554SMatthew G. Knepley } else { 35282f856554SMatthew G. Knepley for (c = 0; c < numComp; ++c) { 35292f856554SMatthew G. Knepley uLl[iface*Nc+off+c] = xL[c]; 35302f856554SMatthew G. Knepley uRl[iface*Nc+off+c] = xR[c]; 35312f856554SMatthew G. Knepley } 35322f856554SMatthew G. Knepley } 35332f856554SMatthew G. Knepley } 35342f856554SMatthew G. Knepley } 35352f856554SMatthew G. Knepley ++iface; 35362f856554SMatthew G. Knepley } 35372f856554SMatthew G. Knepley *Nface = iface; 35382f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr); 35392f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 35402f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 35412f856554SMatthew G. Knepley if (locGrad) { 35422f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr); 35432f856554SMatthew G. Knepley } 35442f856554SMatthew G. Knepley ierr = PetscFree(isFE);CHKERRQ(ierr); 35452f856554SMatthew G. Knepley PetscFunctionReturn(0); 35462f856554SMatthew G. Knepley } 35472f856554SMatthew G. Knepley 35482f856554SMatthew G. Knepley /*@C 35492f856554SMatthew G. Knepley DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces 35502f856554SMatthew G. Knepley 35512f856554SMatthew G. Knepley Input Parameters: 35522f856554SMatthew G. Knepley + dm - The DM 35532f856554SMatthew G. Knepley . fStart - The first face to include 35542f856554SMatthew G. Knepley . fEnd - The first face to exclude 35552f856554SMatthew G. Knepley . locX - A local vector with the solution fields 35562f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL 35572f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry 35582f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry 35592f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL 35602f856554SMatthew G. Knepley 35612f856554SMatthew G. Knepley Output Parameters: 35622f856554SMatthew G. Knepley + Nface - The number of faces with field values 35632f856554SMatthew G. Knepley . uL - The field values at the left side of the face 35642f856554SMatthew G. Knepley - uR - The field values at the right side of the face 35652f856554SMatthew G. Knepley 35662f856554SMatthew G. Knepley Level: developer 35672f856554SMatthew G. Knepley 35682f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 35692f856554SMatthew G. Knepley @*/ 35702f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR) 35712f856554SMatthew G. Knepley { 35722f856554SMatthew G. Knepley PetscErrorCode ierr; 35732f856554SMatthew G. Knepley 35742f856554SMatthew G. Knepley PetscFunctionBegin; 35752f856554SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr); 35762f856554SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr); 35772f856554SMatthew G. Knepley PetscFunctionReturn(0); 35782f856554SMatthew G. Knepley } 35792f856554SMatthew G. Knepley 35802f856554SMatthew G. Knepley /*@C 35812f856554SMatthew G. Knepley DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces 35822f856554SMatthew G. Knepley 35832f856554SMatthew G. Knepley Input Parameters: 35842f856554SMatthew G. Knepley + dm - The DM 35852f856554SMatthew G. Knepley . fStart - The first face to include 35862f856554SMatthew G. Knepley . fEnd - The first face to exclude 35872f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry 35882f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 35892f856554SMatthew G. Knepley 35902f856554SMatthew G. Knepley Output Parameters: 35912f856554SMatthew G. Knepley + Nface - The number of faces with field values 35922f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal 35932f856554SMatthew G. Knepley - vol - The cell volume 35942f856554SMatthew G. Knepley 35952f856554SMatthew G. Knepley Level: developer 35962f856554SMatthew G. Knepley 35972f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields() 35982f856554SMatthew G. Knepley @*/ 35992f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 36002f856554SMatthew G. Knepley { 36012f856554SMatthew G. Knepley DM dmFace, dmCell; 36022f856554SMatthew G. Knepley DMLabel ghostLabel; 36032f856554SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom; 36042f856554SMatthew G. Knepley PetscInt dim, numFaces = fEnd - fStart, iface, face; 36052f856554SMatthew G. Knepley PetscErrorCode ierr; 36062f856554SMatthew G. Knepley 36072f856554SMatthew G. Knepley PetscFunctionBegin; 36082f856554SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36092f856554SMatthew G. Knepley PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4); 36102f856554SMatthew G. Knepley PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5); 36112f856554SMatthew G. Knepley PetscValidPointer(fgeom, 6); 36122f856554SMatthew G. Knepley PetscValidPointer(vol, 7); 36132f856554SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 36142f856554SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 36152f856554SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 36162f856554SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 36172f856554SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 36182f856554SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 36192f856554SMatthew G. Knepley ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr); 36202f856554SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr); 36212f856554SMatthew G. Knepley for (face = fStart, iface = 0; face < fEnd; ++face) { 36222f856554SMatthew G. Knepley const PetscInt *cells; 36232f856554SMatthew G. Knepley PetscFVFaceGeom *fg; 36242f856554SMatthew G. Knepley PetscFVCellGeom *cgL, *cgR; 36252f856554SMatthew G. Knepley PetscFVFaceGeom *fgeoml = *fgeom; 36262f856554SMatthew G. Knepley PetscReal *voll = *vol; 36272f856554SMatthew G. Knepley PetscInt ghost, d, nchild, nsupp; 36282f856554SMatthew G. Knepley 36292f856554SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 36302f856554SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 36312f856554SMatthew G. Knepley ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 36322f856554SMatthew G. Knepley if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 36332f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 36342f856554SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 36352f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr); 36362f856554SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr); 36372f856554SMatthew G. Knepley for (d = 0; d < dim; ++d) { 36382f856554SMatthew G. Knepley fgeoml[iface].centroid[d] = fg->centroid[d]; 36392f856554SMatthew G. Knepley fgeoml[iface].normal[d] = fg->normal[d]; 36402f856554SMatthew G. Knepley } 36412f856554SMatthew G. Knepley voll[iface*2+0] = cgL->volume; 36422f856554SMatthew G. Knepley voll[iface*2+1] = cgR->volume; 36432f856554SMatthew G. Knepley ++iface; 36442f856554SMatthew G. Knepley } 36452f856554SMatthew G. Knepley *Nface = iface; 36462f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 36472f856554SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 36482f856554SMatthew G. Knepley PetscFunctionReturn(0); 36492f856554SMatthew G. Knepley } 36502f856554SMatthew G. Knepley 36512f856554SMatthew G. Knepley /*@C 36522f856554SMatthew G. Knepley DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces 36532f856554SMatthew G. Knepley 36542f856554SMatthew G. Knepley Input Parameters: 36552f856554SMatthew G. Knepley + dm - The DM 36562f856554SMatthew G. Knepley . fStart - The first face to include 36572f856554SMatthew G. Knepley . fEnd - The first face to exclude 36582f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry 36592f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry 36602f856554SMatthew G. Knepley 36612f856554SMatthew G. Knepley Output Parameters: 36622f856554SMatthew G. Knepley + Nface - The number of faces with field values 36632f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal 36642f856554SMatthew G. Knepley - vol - The cell volume 36652f856554SMatthew G. Knepley 36662f856554SMatthew G. Knepley Level: developer 36672f856554SMatthew G. Knepley 36682f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields() 36692f856554SMatthew G. Knepley @*/ 36702f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol) 36712f856554SMatthew G. Knepley { 36722f856554SMatthew G. Knepley PetscErrorCode ierr; 36732f856554SMatthew G. Knepley 36742f856554SMatthew G. Knepley PetscFunctionBegin; 36752f856554SMatthew G. Knepley ierr = PetscFree(*fgeom);CHKERRQ(ierr); 36762f856554SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr); 36772f856554SMatthew G. Knepley PetscFunctionReturn(0); 36782f856554SMatthew G. Knepley } 36792f856554SMatthew G. Knepley 3680a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 3681a1cf66bbSMatthew G. Knepley { 3682a1cf66bbSMatthew G. Knepley char composeStr[33] = {0}; 3683a1cf66bbSMatthew G. Knepley PetscObjectId id; 3684a1cf66bbSMatthew G. Knepley PetscContainer container; 3685a1cf66bbSMatthew G. Knepley PetscErrorCode ierr; 3686a1cf66bbSMatthew G. Knepley 3687a1cf66bbSMatthew G. Knepley PetscFunctionBegin; 3688a1cf66bbSMatthew G. Knepley ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr); 3689a1cf66bbSMatthew G. Knepley ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr); 3690a1cf66bbSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr); 3691a1cf66bbSMatthew G. Knepley if (container) { 3692a1cf66bbSMatthew G. Knepley ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr); 3693a1cf66bbSMatthew G. Knepley } else { 3694a1cf66bbSMatthew G. Knepley ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr); 3695a1cf66bbSMatthew G. Knepley ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 3696a1cf66bbSMatthew G. Knepley ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr); 3697a1cf66bbSMatthew G. Knepley ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr); 3698a1cf66bbSMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr); 3699a1cf66bbSMatthew G. Knepley ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 3700a1cf66bbSMatthew G. Knepley } 3701a1cf66bbSMatthew G. Knepley PetscFunctionReturn(0); 3702a1cf66bbSMatthew G. Knepley } 3703a1cf66bbSMatthew G. Knepley 3704a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom) 3705a1cf66bbSMatthew G. Knepley { 3706a1cf66bbSMatthew G. Knepley PetscFunctionBegin; 3707a1cf66bbSMatthew G. Knepley *geom = NULL; 3708a1cf66bbSMatthew G. Knepley PetscFunctionReturn(0); 3709a1cf66bbSMatthew G. Knepley } 3710a1cf66bbSMatthew G. Knepley 371192d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user) 371292d50984SMatthew G. Knepley { 371392d50984SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 371492d50984SMatthew G. Knepley const char *name = "Residual"; 371592d50984SMatthew G. Knepley DM dmAux = NULL; 371692d50984SMatthew G. Knepley DMLabel ghostLabel = NULL; 371792d50984SMatthew G. Knepley PetscDS prob = NULL; 371892d50984SMatthew G. Knepley PetscDS probAux = NULL; 371992d50984SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 372092d50984SMatthew G. Knepley PetscBool isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 372192d50984SMatthew G. Knepley DMField coordField = NULL; 3722c0006e53SPatrick Farrell Vec locA; 3723c0006e53SPatrick Farrell PetscScalar *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL; 372492d50984SMatthew G. Knepley IS chunkIS; 372592d50984SMatthew G. Knepley const PetscInt *cells; 372692d50984SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 3727364207b6SKarl Rupp PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd; 372892d50984SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 372992d50984SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 373092d50984SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 373192d50984SMatthew G. Knepley PetscErrorCode ierr; 373292d50984SMatthew G. Knepley 373392d50984SMatthew G. Knepley PetscFunctionBegin; 373492d50984SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 373592d50984SMatthew G. Knepley /* FEM+FVM */ 373692d50984SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 373792d50984SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 373892d50984SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 373992d50984SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 374092d50984SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 374192d50984SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 374292d50984SMatthew G. Knepley if (locA) { 374392d50984SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 374492d50984SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 374592d50984SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 374692d50984SMatthew G. Knepley } 374792d50984SMatthew G. Knepley /* 2: Get geometric data */ 374892d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 374992d50984SMatthew G. Knepley PetscObject obj; 375092d50984SMatthew G. Knepley PetscClassId id; 375192d50984SMatthew G. Knepley PetscBool fimp; 375292d50984SMatthew G. Knepley 375392d50984SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 375492d50984SMatthew G. Knepley if (isImplicit != fimp) continue; 375592d50984SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 375692d50984SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 375792d50984SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 3758364207b6SKarl Rupp if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented"); 375992d50984SMatthew G. Knepley } 376092d50984SMatthew G. Knepley if (useFEM) { 376192d50984SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 376292d50984SMatthew G. Knepley ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 376392d50984SMatthew G. Knepley if (maxDegree <= 1) { 376492d50984SMatthew G. Knepley ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 376592d50984SMatthew G. Knepley if (affineQuad) { 376692d50984SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 376792d50984SMatthew G. Knepley } 376892d50984SMatthew G. Knepley } else { 376992d50984SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 377092d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 377192d50984SMatthew G. Knepley PetscObject obj; 377292d50984SMatthew G. Knepley PetscClassId id; 377392d50984SMatthew G. Knepley PetscBool fimp; 377492d50984SMatthew G. Knepley 377592d50984SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 377692d50984SMatthew G. Knepley if (isImplicit != fimp) continue; 377792d50984SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 377892d50984SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 377992d50984SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 378092d50984SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 378192d50984SMatthew G. Knepley 378292d50984SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 378392d50984SMatthew G. Knepley ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr); 378492d50984SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 378592d50984SMatthew G. Knepley } 378692d50984SMatthew G. Knepley } 378792d50984SMatthew G. Knepley } 378892d50984SMatthew G. Knepley } 378992d50984SMatthew G. Knepley /* Loop over chunks */ 379092d50984SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 379192d50984SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 379292d50984SMatthew G. Knepley if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);} 379392d50984SMatthew G. Knepley numCells = cEnd - cStart; 379492d50984SMatthew G. Knepley numChunks = 1; 379592d50984SMatthew G. Knepley cellChunkSize = numCells/numChunks; 379692d50984SMatthew G. Knepley numChunks = PetscMin(1,numCells); 379792d50984SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 3798c0006e53SPatrick Farrell PetscScalar *elemVec, *fluxL = NULL, *fluxR = NULL; 3799c0006e53SPatrick Farrell PetscReal *vol = NULL; 3800c0006e53SPatrick Farrell PetscFVFaceGeom *fgeom = NULL; 380192d50984SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 3802c0006e53SPatrick Farrell PetscInt numFaces = 0; 380392d50984SMatthew G. Knepley 380492d50984SMatthew G. Knepley /* Extract field coefficients */ 380592d50984SMatthew G. Knepley if (useFEM) { 380692d50984SMatthew G. Knepley ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr); 380792d50984SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 380892d50984SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 3809580bdb30SBarry Smith ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr); 381092d50984SMatthew G. Knepley } 381192d50984SMatthew G. Knepley /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 381292d50984SMatthew G. Knepley /* Loop over fields */ 381392d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 381492d50984SMatthew G. Knepley PetscObject obj; 381592d50984SMatthew G. Knepley PetscClassId id; 381692d50984SMatthew G. Knepley PetscBool fimp; 381792d50984SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 381892d50984SMatthew G. Knepley 381992d50984SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 382092d50984SMatthew G. Knepley if (isImplicit != fimp) continue; 382192d50984SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 382292d50984SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 382392d50984SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 382492d50984SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 382592d50984SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 382692d50984SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 382792d50984SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 382892d50984SMatthew G. Knepley PetscInt Nq, Nb; 382992d50984SMatthew G. Knepley 383092d50984SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 383192d50984SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 383292d50984SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 383392d50984SMatthew G. Knepley blockSize = Nb; 383492d50984SMatthew G. Knepley batchSize = numBlocks * blockSize; 383592d50984SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 383692d50984SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 383792d50984SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 383892d50984SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 383992d50984SMatthew G. Knepley offset = numCells - Nr; 384092d50984SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 384192d50984SMatthew G. Knepley /* For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */ 384292d50984SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 38434bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 384492d50984SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 38454bee2e38SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 384692d50984SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 384792d50984SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 384892d50984SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 384992d50984SMatthew G. Knepley 385092d50984SMatthew G. Knepley Ne = numFaces; 385192d50984SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 385292d50984SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 385392d50984SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 3854ff1e0c32SBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f); 385592d50984SMatthew G. Knepley } 385692d50984SMatthew G. Knepley /* Loop over domain */ 385792d50984SMatthew G. Knepley if (useFEM) { 385892d50984SMatthew G. Knepley /* Add elemVec to locX */ 385992d50984SMatthew G. Knepley for (c = cS; c < cE; ++c) { 386092d50984SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 386192d50984SMatthew G. Knepley const PetscInt cind = c - cStart; 386292d50984SMatthew G. Knepley 386392d50984SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 386492d50984SMatthew G. Knepley if (ghostLabel) { 386592d50984SMatthew G. Knepley PetscInt ghostVal; 386692d50984SMatthew G. Knepley 386792d50984SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 386892d50984SMatthew G. Knepley if (ghostVal > 0) continue; 386992d50984SMatthew G. Knepley } 387092d50984SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 387192d50984SMatthew G. Knepley } 387292d50984SMatthew G. Knepley } 387392d50984SMatthew G. Knepley /* Handle time derivative */ 387492d50984SMatthew G. Knepley if (locX_t) { 387592d50984SMatthew G. Knepley PetscScalar *x_t, *fa; 387692d50984SMatthew G. Knepley 387792d50984SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 387892d50984SMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 387992d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 388092d50984SMatthew G. Knepley PetscFV fv; 388192d50984SMatthew G. Knepley PetscObject obj; 388292d50984SMatthew G. Knepley PetscClassId id; 388392d50984SMatthew G. Knepley PetscInt pdim, d; 388492d50984SMatthew G. Knepley 388592d50984SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 388692d50984SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 388792d50984SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 388892d50984SMatthew G. Knepley fv = (PetscFV) obj; 388992d50984SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 389092d50984SMatthew G. Knepley for (c = cS; c < cE; ++c) { 389192d50984SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 389292d50984SMatthew G. Knepley PetscScalar *u_t, *r; 389392d50984SMatthew G. Knepley 389492d50984SMatthew G. Knepley if (ghostLabel) { 389592d50984SMatthew G. Knepley PetscInt ghostVal; 389692d50984SMatthew G. Knepley 389792d50984SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr); 389892d50984SMatthew G. Knepley if (ghostVal > 0) continue; 389992d50984SMatthew G. Knepley } 390092d50984SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 390192d50984SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 390292d50984SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 390392d50984SMatthew G. Knepley } 390492d50984SMatthew G. Knepley } 390592d50984SMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 390692d50984SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 390792d50984SMatthew G. Knepley } 390892d50984SMatthew G. Knepley if (useFEM) { 390992d50984SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 391092d50984SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 391192d50984SMatthew G. Knepley } 391292d50984SMatthew G. Knepley } 391392d50984SMatthew G. Knepley if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);} 391492d50984SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 391592d50984SMatthew G. Knepley /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */ 391692d50984SMatthew G. Knepley if (useFEM) { 391792d50984SMatthew G. Knepley if (maxDegree <= 1) { 391892d50984SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 391992d50984SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 392092d50984SMatthew G. Knepley } else { 392192d50984SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 392292d50984SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 392392d50984SMatthew G. Knepley ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr); 392492d50984SMatthew G. Knepley } 392592d50984SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 392692d50984SMatthew G. Knepley } 392792d50984SMatthew G. Knepley } 392892d50984SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 392992d50984SMatthew G. Knepley PetscFunctionReturn(0); 393092d50984SMatthew G. Knepley } 393192d50984SMatthew G. Knepley 3932a1cf66bbSMatthew G. Knepley /* 3933a1cf66bbSMatthew G. Knepley We always assemble JacP, and if the matrix is different from Jac and two different sets of point functions are provided, we also assemble Jac 3934a1cf66bbSMatthew G. Knepley 3935a1cf66bbSMatthew G. Knepley X - The local solution vector 3936a1cf66bbSMatthew G. Knepley X_t - The local solution time derviative vector, or NULL 3937a1cf66bbSMatthew G. Knepley */ 3938a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS, 3939a1cf66bbSMatthew G. Knepley PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx) 3940a1cf66bbSMatthew G. Knepley { 3941a1cf66bbSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 3942a1cf66bbSMatthew G. Knepley const char *name = "Jacobian", *nameP = "JacobianPre"; 3943a1cf66bbSMatthew G. Knepley DM dmAux = NULL; 3944a1cf66bbSMatthew G. Knepley PetscDS prob, probAux = NULL; 3945a1cf66bbSMatthew G. Knepley PetscSection sectionAux = NULL; 3946a1cf66bbSMatthew G. Knepley Vec A; 3947a1cf66bbSMatthew G. Knepley DMField coordField; 3948a1cf66bbSMatthew G. Knepley PetscFEGeom *cgeomFEM; 3949a1cf66bbSMatthew G. Knepley PetscQuadrature qGeom = NULL; 3950a1cf66bbSMatthew G. Knepley Mat J = Jac, JP = JacP; 3951a1cf66bbSMatthew G. Knepley PetscScalar *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL; 39529b2fc754SMatthew G. Knepley PetscBool hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE; 3953a1cf66bbSMatthew G. Knepley const PetscInt *cells; 39549b2fc754SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0; 3955a1cf66bbSMatthew G. Knepley PetscErrorCode ierr; 3956a1cf66bbSMatthew G. Knepley 3957a1cf66bbSMatthew G. Knepley PetscFunctionBegin; 3958a1cf66bbSMatthew G. Knepley CHKMEMQ; 3959a1cf66bbSMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 3960a1cf66bbSMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 3961a1cf66bbSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 3962a1cf66bbSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 3963a1cf66bbSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 3964a1cf66bbSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 3965a1cf66bbSMatthew G. Knepley if (dmAux) { 396692fd8e1eSJed Brown ierr = DMGetLocalSection(dmAux, §ionAux);CHKERRQ(ierr); 3967a1cf66bbSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 3968a1cf66bbSMatthew G. Knepley } 3969a1cf66bbSMatthew G. Knepley /* Get flags */ 3970a1cf66bbSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 3971a1cf66bbSMatthew G. Knepley ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr); 3972a1cf66bbSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 3973a1cf66bbSMatthew G. Knepley PetscObject disc; 3974a1cf66bbSMatthew G. Knepley PetscClassId id; 3975a1cf66bbSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr); 3976a1cf66bbSMatthew G. Knepley ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr); 3977a1cf66bbSMatthew G. Knepley if (id == PETSCFE_CLASSID) {isFE[fieldI] = PETSC_TRUE;} 3978a1cf66bbSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;} 3979a1cf66bbSMatthew G. Knepley } 3980a1cf66bbSMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 3981a1cf66bbSMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 3982a1cf66bbSMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 3983a1cf66bbSMatthew G. Knepley assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE; 3984a1cf66bbSMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 3985a1cf66bbSMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATIS, &isMatIS);CHKERRQ(ierr); 3986a1cf66bbSMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 3987a1cf66bbSMatthew G. Knepley /* Setup input data and temp arrays (should be DMGetWorkArray) */ 3988a1cf66bbSMatthew G. Knepley if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);} 3989a1cf66bbSMatthew G. Knepley if (isMatIS) {ierr = MatISGetLocalMat(Jac, &J);CHKERRQ(ierr);} 3990a1cf66bbSMatthew G. Knepley if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);} 3991a1cf66bbSMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);} /* No allocated space for FV stuff, so ignore the zero entries */ 3992a1cf66bbSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 3993a1cf66bbSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 3994a1cf66bbSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 3995a1cf66bbSMatthew G. Knepley if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);} 3996a1cf66bbSMatthew G. Knepley CHKMEMQ; 3997a1cf66bbSMatthew G. Knepley /* Compute batch sizes */ 3998a1cf66bbSMatthew G. Knepley if (isFE[0]) { 3999a1cf66bbSMatthew G. Knepley PetscFE fe; 4000a1cf66bbSMatthew G. Knepley PetscQuadrature q; 4001a1cf66bbSMatthew G. Knepley PetscInt numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb; 4002a1cf66bbSMatthew G. Knepley 4003a1cf66bbSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr); 4004a1cf66bbSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 4005a1cf66bbSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 4006a1cf66bbSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 4007a1cf66bbSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 4008a1cf66bbSMatthew G. Knepley blockSize = Nb*numQuadPoints; 4009a1cf66bbSMatthew G. Knepley batchSize = numBlocks * blockSize; 4010a1cf66bbSMatthew G. Knepley chunkSize = numBatches * batchSize; 4011a1cf66bbSMatthew G. Knepley numChunks = numCells / chunkSize + numCells % chunkSize; 4012a1cf66bbSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 4013a1cf66bbSMatthew G. Knepley } else { 4014a1cf66bbSMatthew G. Knepley chunkSize = numCells; 4015a1cf66bbSMatthew G. Knepley numChunks = 1; 4016a1cf66bbSMatthew G. Knepley } 4017a1cf66bbSMatthew G. Knepley /* Get work space */ 4018a1cf66bbSMatthew G. Knepley wsz = (((X?1:0) + (X_t?1:0) + (dmAux?1:0))*totDim + ((hasJac?1:0) + (hasPrec?1:0) + (hasDyn?1:0))*totDim*totDim)*chunkSize; 4019a1cf66bbSMatthew G. Knepley ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr); 4020580bdb30SBarry Smith ierr = PetscArrayzero(work, wsz);CHKERRQ(ierr); 4021a1cf66bbSMatthew G. Knepley off = 0; 4022a1cf66bbSMatthew G. Knepley u = X ? (sz = chunkSize*totDim, off += sz, work+off-sz) : NULL; 4023a1cf66bbSMatthew G. Knepley u_t = X_t ? (sz = chunkSize*totDim, off += sz, work+off-sz) : NULL; 4024a1cf66bbSMatthew G. Knepley a = dmAux ? (sz = chunkSize*totDimAux, off += sz, work+off-sz) : NULL; 4025a1cf66bbSMatthew G. Knepley elemMat = hasJac ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL; 4026a1cf66bbSMatthew G. Knepley elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL; 4027a1cf66bbSMatthew G. Knepley elemMatD = hasDyn ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL; 4028a1cf66bbSMatthew G. Knepley if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz); 4029a1cf66bbSMatthew G. Knepley /* Setup geometry */ 4030a1cf66bbSMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 40319b2fc754SMatthew G. Knepley ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr); 40329b2fc754SMatthew G. Knepley if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);} 4033a1cf66bbSMatthew G. Knepley if (!qGeom) { 4034a1cf66bbSMatthew G. Knepley PetscFE fe; 4035a1cf66bbSMatthew G. Knepley 4036a1cf66bbSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr); 4037a1cf66bbSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr); 4038a1cf66bbSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr); 4039a1cf66bbSMatthew G. Knepley } 4040a1cf66bbSMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr); 4041a1cf66bbSMatthew G. Knepley /* Compute volume integrals */ 4042a1cf66bbSMatthew G. Knepley if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);} 4043a1cf66bbSMatthew G. Knepley ierr = MatZeroEntries(JP);CHKERRQ(ierr); 4044a1cf66bbSMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) { 4045a1cf66bbSMatthew G. Knepley const PetscInt Ncell = PetscMin(chunkSize, numCells - offCell); 4046a1cf66bbSMatthew G. Knepley PetscInt c; 4047a1cf66bbSMatthew G. Knepley 4048a1cf66bbSMatthew G. Knepley /* Extract values */ 4049a1cf66bbSMatthew G. Knepley for (c = 0; c < Ncell; ++c) { 4050a1cf66bbSMatthew G. Knepley const PetscInt cell = cells ? cells[c+offCell] : c+offCell; 4051a1cf66bbSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 4052a1cf66bbSMatthew G. Knepley PetscInt i; 4053a1cf66bbSMatthew G. Knepley 4054a1cf66bbSMatthew G. Knepley if (X) { 4055a1cf66bbSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 4056a1cf66bbSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 4057a1cf66bbSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 4058a1cf66bbSMatthew G. Knepley } 4059a1cf66bbSMatthew G. Knepley if (X_t) { 4060a1cf66bbSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 4061a1cf66bbSMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 4062a1cf66bbSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 4063a1cf66bbSMatthew G. Knepley } 4064a1cf66bbSMatthew G. Knepley if (dmAux) { 4065a1cf66bbSMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 4066a1cf66bbSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 4067a1cf66bbSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr); 4068a1cf66bbSMatthew G. Knepley } 4069a1cf66bbSMatthew G. Knepley } 4070a1cf66bbSMatthew G. Knepley CHKMEMQ; 4071a1cf66bbSMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 4072a1cf66bbSMatthew G. Knepley PetscFE fe; 4073a1cf66bbSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 4074a1cf66bbSMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 40754bee2e38SMatthew G. Knepley if (hasJac) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);} 40764bee2e38SMatthew G. Knepley if (hasPrec) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);} 40774bee2e38SMatthew G. Knepley if (hasDyn) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);} 4078a1cf66bbSMatthew G. Knepley } 4079a1cf66bbSMatthew G. Knepley /* For finite volume, add the identity */ 4080a1cf66bbSMatthew G. Knepley if (!isFE[fieldI]) { 4081a1cf66bbSMatthew G. Knepley PetscFV fv; 4082a1cf66bbSMatthew G. Knepley PetscInt eOffset = 0, Nc, fc, foff; 4083a1cf66bbSMatthew G. Knepley 4084a1cf66bbSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr); 4085a1cf66bbSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 4086a1cf66bbSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 4087a1cf66bbSMatthew G. Knepley for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) { 4088a1cf66bbSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 4089a1cf66bbSMatthew G. Knepley const PetscInt i = foff + fc; 4090a1cf66bbSMatthew G. Knepley if (hasJac) {elemMat [eOffset+i*totDim+i] = 1.0;} 4091a1cf66bbSMatthew G. Knepley if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;} 4092a1cf66bbSMatthew G. Knepley } 4093a1cf66bbSMatthew G. Knepley } 4094a1cf66bbSMatthew G. Knepley } 4095a1cf66bbSMatthew G. Knepley } 4096a1cf66bbSMatthew G. Knepley CHKMEMQ; 4097a1cf66bbSMatthew G. Knepley /* Add contribution from X_t */ 4098a1cf66bbSMatthew G. Knepley if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];} 4099a1cf66bbSMatthew G. Knepley /* Insert values into matrix */ 4100a1cf66bbSMatthew G. Knepley for (c = 0; c < Ncell; ++c) { 4101a1cf66bbSMatthew G. Knepley const PetscInt cell = cells ? cells[c+offCell] : c+offCell; 4102a1cf66bbSMatthew G. Knepley if (mesh->printFEM > 1) { 4103a1cf66bbSMatthew G. Knepley if (hasJac) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 4104a1cf66bbSMatthew G. Knepley if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);} 4105a1cf66bbSMatthew G. Knepley } 4106a1cf66bbSMatthew G. Knepley if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);} 4107a1cf66bbSMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 4108a1cf66bbSMatthew G. Knepley } 4109a1cf66bbSMatthew G. Knepley CHKMEMQ; 4110a1cf66bbSMatthew G. Knepley } 4111a1cf66bbSMatthew G. Knepley /* Cleanup */ 4112a1cf66bbSMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr); 4113a1cf66bbSMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 4114a1cf66bbSMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 4115a1cf66bbSMatthew G. Knepley ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr); 4116a1cf66bbSMatthew G. Knepley ierr = DMRestoreWorkArray(dm, ((1 + (X_t?1:0) + (dmAux?1:0))*totDim + ((hasJac?1:0) + (hasPrec?1:0) + (hasDyn?1:0))*totDim*totDim)*chunkSize, MPIU_SCALAR, &work);CHKERRQ(ierr); 4117a1cf66bbSMatthew G. Knepley /* Compute boundary integrals */ 4118a1cf66bbSMatthew G. Knepley /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */ 4119a1cf66bbSMatthew G. Knepley /* Assemble matrix */ 4120a1cf66bbSMatthew G. Knepley if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);} 4121a1cf66bbSMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4122a1cf66bbSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 4123a1cf66bbSMatthew G. Knepley CHKMEMQ; 4124a1cf66bbSMatthew G. Knepley PetscFunctionReturn(0); 4125a1cf66bbSMatthew G. Knepley } 41263e9753d6SMatthew G. Knepley 41273e9753d6SMatthew G. Knepley /******** FEM Assembly Function ********/ 41283e9753d6SMatthew G. Knepley 41293e9753d6SMatthew G. Knepley static PetscErrorCode DMConvertPlex_Internal(DM dm, DM *plex, PetscBool copy) 41303e9753d6SMatthew G. Knepley { 41313e9753d6SMatthew G. Knepley PetscBool isPlex; 41323e9753d6SMatthew G. Knepley PetscErrorCode ierr; 41333e9753d6SMatthew G. Knepley 41343e9753d6SMatthew G. Knepley PetscFunctionBegin; 41353e9753d6SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr); 41363e9753d6SMatthew G. Knepley if (isPlex) { 41373e9753d6SMatthew G. Knepley *plex = dm; 41383e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); 41393e9753d6SMatthew G. Knepley } else { 41403e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr); 41413e9753d6SMatthew G. Knepley if (!*plex) { 41423e9753d6SMatthew G. Knepley ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr); 41433e9753d6SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr); 41443e9753d6SMatthew G. Knepley if (copy) { 41453e9753d6SMatthew G. Knepley const char *comps[] = {"A", "dmAux"}; 41463e9753d6SMatthew G. Knepley PetscObject obj; 41473e9753d6SMatthew G. Knepley PetscInt i; 41483e9753d6SMatthew G. Knepley 41493e9753d6SMatthew G. Knepley for (i = 0; i < 2; ++i) { 41503e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr); 41513e9753d6SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr); 41523e9753d6SMatthew G. Knepley } 41533e9753d6SMatthew G. Knepley } 41543e9753d6SMatthew G. Knepley } else { 41553e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr); 41563e9753d6SMatthew G. Knepley } 41573e9753d6SMatthew G. Knepley } 41583e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 41593e9753d6SMatthew G. Knepley } 41603e9753d6SMatthew G. Knepley 41613e9753d6SMatthew G. Knepley /*@ 41623e9753d6SMatthew G. Knepley DMPlexGetGeometryFVM - Return precomputed geometric data 41633e9753d6SMatthew G. Knepley 41643e9753d6SMatthew G. Knepley Collective on DM 41653e9753d6SMatthew G. Knepley 41663e9753d6SMatthew G. Knepley Input Parameter: 41673e9753d6SMatthew G. Knepley . dm - The DM 41683e9753d6SMatthew G. Knepley 41693e9753d6SMatthew G. Knepley Output Parameters: 41703e9753d6SMatthew G. Knepley + facegeom - The values precomputed from face geometry 41713e9753d6SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry 41723e9753d6SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell 41733e9753d6SMatthew G. Knepley 41743e9753d6SMatthew G. Knepley Level: developer 41753e9753d6SMatthew G. Knepley 41763e9753d6SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal() 41773e9753d6SMatthew G. Knepley @*/ 41783e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius) 41793e9753d6SMatthew G. Knepley { 41803e9753d6SMatthew G. Knepley DM plex; 41813e9753d6SMatthew G. Knepley PetscErrorCode ierr; 41823e9753d6SMatthew G. Knepley 41833e9753d6SMatthew G. Knepley PetscFunctionBegin; 41843e9753d6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 41853e9753d6SMatthew G. Knepley ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 41863e9753d6SMatthew G. Knepley ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr); 41873e9753d6SMatthew G. Knepley if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);} 41883e9753d6SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 41893e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 41903e9753d6SMatthew G. Knepley } 41913e9753d6SMatthew G. Knepley 41923e9753d6SMatthew G. Knepley /*@ 41933e9753d6SMatthew G. Knepley DMPlexGetGradientDM - Return gradient data layout 41943e9753d6SMatthew G. Knepley 41953e9753d6SMatthew G. Knepley Collective on DM 41963e9753d6SMatthew G. Knepley 41973e9753d6SMatthew G. Knepley Input Parameters: 41983e9753d6SMatthew G. Knepley + dm - The DM 41993e9753d6SMatthew G. Knepley - fv - The PetscFV 42003e9753d6SMatthew G. Knepley 42013e9753d6SMatthew G. Knepley Output Parameter: 42023e9753d6SMatthew G. Knepley . dmGrad - The layout for gradient values 42033e9753d6SMatthew G. Knepley 42043e9753d6SMatthew G. Knepley Level: developer 42053e9753d6SMatthew G. Knepley 42063e9753d6SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM() 42073e9753d6SMatthew G. Knepley @*/ 42083e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGradientDM(DM dm, PetscFV fv, DM *dmGrad) 42093e9753d6SMatthew G. Knepley { 42103e9753d6SMatthew G. Knepley DM plex; 42113e9753d6SMatthew G. Knepley PetscBool computeGradients; 42123e9753d6SMatthew G. Knepley PetscErrorCode ierr; 42133e9753d6SMatthew G. Knepley 42143e9753d6SMatthew G. Knepley PetscFunctionBegin; 42153e9753d6SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 42163e9753d6SMatthew G. Knepley PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2); 42173e9753d6SMatthew G. Knepley PetscValidPointer(dmGrad,3); 42183e9753d6SMatthew G. Knepley ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr); 42193e9753d6SMatthew G. Knepley if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);} 42203e9753d6SMatthew G. Knepley ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr); 42213e9753d6SMatthew G. Knepley ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr); 42223e9753d6SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 42233e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 42243e9753d6SMatthew G. Knepley } 42253e9753d6SMatthew G. Knepley 42263e9753d6SMatthew G. Knepley static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS) 42273e9753d6SMatthew G. Knepley { 42283e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 42293e9753d6SMatthew G. Knepley DM plex = NULL, plexA = NULL; 42303e9753d6SMatthew G. Knepley DMEnclosureType encAux; 42313e9753d6SMatthew G. Knepley PetscDS prob, probAux = NULL; 42323e9753d6SMatthew G. Knepley PetscSection section, sectionAux = NULL; 42333e9753d6SMatthew G. Knepley Vec locA = NULL; 42343e9753d6SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL; 42353e9753d6SMatthew G. Knepley PetscInt v; 42363e9753d6SMatthew G. Knepley PetscInt totDim, totDimAux = 0; 42373e9753d6SMatthew G. Knepley PetscErrorCode ierr; 42383e9753d6SMatthew G. Knepley 42393e9753d6SMatthew G. Knepley PetscFunctionBegin; 42403e9753d6SMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 42413e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 42423e9753d6SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 42433e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 42443e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 42453e9753d6SMatthew G. Knepley if (locA) { 42463e9753d6SMatthew G. Knepley DM dmAux; 42473e9753d6SMatthew G. Knepley 42483e9753d6SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 42493e9753d6SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 42503e9753d6SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 42513e9753d6SMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 42523e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 42533e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(plexA, §ionAux);CHKERRQ(ierr); 42543e9753d6SMatthew G. Knepley } 42553e9753d6SMatthew G. Knepley for (v = 0; v < numValues; ++v) { 42563e9753d6SMatthew G. Knepley PetscFEGeom *fgeom; 42573e9753d6SMatthew G. Knepley PetscInt maxDegree; 42583e9753d6SMatthew G. Knepley PetscQuadrature qGeom = NULL; 42593e9753d6SMatthew G. Knepley IS pointIS; 42603e9753d6SMatthew G. Knepley const PetscInt *points; 42613e9753d6SMatthew G. Knepley PetscInt numFaces, face, Nq; 42623e9753d6SMatthew G. Knepley 42633e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 42643e9753d6SMatthew G. Knepley if (!pointIS) continue; /* No points with that id on this process */ 42653e9753d6SMatthew G. Knepley { 42663e9753d6SMatthew G. Knepley IS isectIS; 42673e9753d6SMatthew G. Knepley 42683e9753d6SMatthew G. Knepley /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */ 42693e9753d6SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 42703e9753d6SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 42713e9753d6SMatthew G. Knepley pointIS = isectIS; 42723e9753d6SMatthew G. Knepley } 42733e9753d6SMatthew G. Knepley ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr); 42743e9753d6SMatthew G. Knepley ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr); 42753e9753d6SMatthew G. Knepley ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 42763e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 42773e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 42783e9753d6SMatthew G. Knepley ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 42793e9753d6SMatthew G. Knepley } 42803e9753d6SMatthew G. Knepley if (!qGeom) { 42813e9753d6SMatthew G. Knepley PetscFE fe; 42823e9753d6SMatthew G. Knepley 42833e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 42843e9753d6SMatthew G. Knepley ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 42853e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 42863e9753d6SMatthew G. Knepley } 42873e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 42883e9753d6SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 42893e9753d6SMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 42903e9753d6SMatthew G. Knepley const PetscInt point = points[face], *support, *cone; 42913e9753d6SMatthew G. Knepley PetscScalar *x = NULL; 42923e9753d6SMatthew G. Knepley PetscInt i, coneSize, faceLoc; 42933e9753d6SMatthew G. Knepley 42943e9753d6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 42953e9753d6SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 42963e9753d6SMatthew G. Knepley ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 42973e9753d6SMatthew G. Knepley for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 42983e9753d6SMatthew G. Knepley if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]); 42993e9753d6SMatthew G. Knepley fgeom->face[face][0] = faceLoc; 43003e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 43013e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 43023e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 43033e9753d6SMatthew G. Knepley if (locX_t) { 43043e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 43053e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 43063e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 43073e9753d6SMatthew G. Knepley } 43083e9753d6SMatthew G. Knepley if (locA) { 43093e9753d6SMatthew G. Knepley PetscInt subp; 43103e9753d6SMatthew G. Knepley 43113e9753d6SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr); 43123e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 43133e9753d6SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 43143e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 43153e9753d6SMatthew G. Knepley } 43163e9753d6SMatthew G. Knepley } 43173e9753d6SMatthew G. Knepley ierr = PetscArrayzero(elemVec, numFaces*totDim);CHKERRQ(ierr); 43183e9753d6SMatthew G. Knepley { 43193e9753d6SMatthew G. Knepley PetscFE fe; 43203e9753d6SMatthew G. Knepley PetscInt Nb; 43213e9753d6SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 43223e9753d6SMatthew G. Knepley /* Conforming batches */ 43233e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 43243e9753d6SMatthew G. Knepley /* Remainder */ 43253e9753d6SMatthew G. Knepley PetscInt Nr, offset; 43263e9753d6SMatthew G. Knepley 43273e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr); 43283e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 43293e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 43303e9753d6SMatthew G. Knepley /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */ 43313e9753d6SMatthew G. Knepley blockSize = Nb; 43323e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 43333e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 43343e9753d6SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 43353e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 43363e9753d6SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 43373e9753d6SMatthew G. Knepley offset = numFaces - Nr; 43383e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 43393e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 43403e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr); 43413e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 43423e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr); 43433e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 43443e9753d6SMatthew G. Knepley } 43453e9753d6SMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 43463e9753d6SMatthew G. Knepley const PetscInt point = points[face], *support; 43473e9753d6SMatthew G. Knepley 43483e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);} 43493e9753d6SMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 43503e9753d6SMatthew G. Knepley ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 43513e9753d6SMatthew G. Knepley } 43523e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 43533e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 43543e9753d6SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 43553e9753d6SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 43563e9753d6SMatthew G. Knepley ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr); 43573e9753d6SMatthew G. Knepley } 43583e9753d6SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 43593e9753d6SMatthew G. Knepley ierr = DMDestroy(&plexA);CHKERRQ(ierr); 43603e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 43613e9753d6SMatthew G. Knepley } 43623e9753d6SMatthew G. Knepley 43633e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF) 43643e9753d6SMatthew G. Knepley { 43653e9753d6SMatthew G. Knepley DMField coordField; 43663e9753d6SMatthew G. Knepley DMLabel depthLabel; 43673e9753d6SMatthew G. Knepley IS facetIS; 43683e9753d6SMatthew G. Knepley PetscInt dim; 43693e9753d6SMatthew G. Knepley PetscErrorCode ierr; 43703e9753d6SMatthew G. Knepley 43713e9753d6SMatthew G. Knepley PetscFunctionBegin; 43723e9753d6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 43733e9753d6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 43743e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 43753e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 43763e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 43773e9753d6SMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 43783e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 43793e9753d6SMatthew G. Knepley } 43803e9753d6SMatthew G. Knepley 43813e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 43823e9753d6SMatthew G. Knepley { 43833e9753d6SMatthew G. Knepley PetscDS prob; 43843e9753d6SMatthew G. Knepley PetscInt numBd, bd; 43853e9753d6SMatthew G. Knepley DMField coordField = NULL; 43863e9753d6SMatthew G. Knepley IS facetIS = NULL; 43873e9753d6SMatthew G. Knepley DMLabel depthLabel; 43883e9753d6SMatthew G. Knepley PetscInt dim; 43893e9753d6SMatthew G. Knepley PetscErrorCode ierr; 43903e9753d6SMatthew G. Knepley 43913e9753d6SMatthew G. Knepley PetscFunctionBegin; 43923e9753d6SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 43933e9753d6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 43943e9753d6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 43953e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr); 43963e9753d6SMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 43973e9753d6SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 43983e9753d6SMatthew G. Knepley DMBoundaryConditionType type; 43993e9753d6SMatthew G. Knepley const char *bdLabel; 44003e9753d6SMatthew G. Knepley DMLabel label; 44013e9753d6SMatthew G. Knepley const PetscInt *values; 44023e9753d6SMatthew G. Knepley PetscInt field, numValues; 44033e9753d6SMatthew G. Knepley PetscObject obj; 44043e9753d6SMatthew G. Knepley PetscClassId id; 44053e9753d6SMatthew G. Knepley 4406*56cf3b9cSMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 44073e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 44083e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 44093e9753d6SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 44103e9753d6SMatthew G. Knepley if (!facetIS) { 44113e9753d6SMatthew G. Knepley DMLabel depthLabel; 44123e9753d6SMatthew G. Knepley PetscInt dim; 44133e9753d6SMatthew G. Knepley 44143e9753d6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 44153e9753d6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 44163e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr); 44173e9753d6SMatthew G. Knepley } 44183e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 44193e9753d6SMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 44203e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr); 44213e9753d6SMatthew G. Knepley } 44223e9753d6SMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 44233e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 44243e9753d6SMatthew G. Knepley } 44253e9753d6SMatthew G. Knepley 44263e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 44273e9753d6SMatthew G. Knepley { 44283e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 44293e9753d6SMatthew G. Knepley const char *name = "Residual"; 44303e9753d6SMatthew G. Knepley DM dmAux = NULL; 44313e9753d6SMatthew G. Knepley DM dmGrad = NULL; 44323e9753d6SMatthew G. Knepley DMLabel ghostLabel = NULL; 44333e9753d6SMatthew G. Knepley PetscDS prob = NULL; 44343e9753d6SMatthew G. Knepley PetscDS probAux = NULL; 44353e9753d6SMatthew G. Knepley PetscSection section = NULL; 44363e9753d6SMatthew G. Knepley PetscBool useFEM = PETSC_FALSE; 44373e9753d6SMatthew G. Knepley PetscBool useFVM = PETSC_FALSE; 44383e9753d6SMatthew G. Knepley PetscBool isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE; 44393e9753d6SMatthew G. Knepley PetscFV fvm = NULL; 44403e9753d6SMatthew G. Knepley PetscFVCellGeom *cgeomFVM = NULL; 44413e9753d6SMatthew G. Knepley PetscFVFaceGeom *fgeomFVM = NULL; 44423e9753d6SMatthew G. Knepley DMField coordField = NULL; 44433e9753d6SMatthew G. Knepley Vec locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL; 44443e9753d6SMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a, *uL, *uR; 44453e9753d6SMatthew G. Knepley IS chunkIS; 44463e9753d6SMatthew G. Knepley const PetscInt *cells; 44473e9753d6SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 44483e9753d6SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd; 44493e9753d6SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 44503e9753d6SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 44513e9753d6SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 44523e9753d6SMatthew G. Knepley PetscErrorCode ierr; 44533e9753d6SMatthew G. Knepley 44543e9753d6SMatthew G. Knepley PetscFunctionBegin; 44553e9753d6SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 44563e9753d6SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 44573e9753d6SMatthew G. Knepley /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */ 44583e9753d6SMatthew G. Knepley /* FEM+FVM */ 44593e9753d6SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 44603e9753d6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 44613e9753d6SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 44623e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 44633e9753d6SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 44643e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr); 44653e9753d6SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 44663e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 44673e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 44683e9753d6SMatthew G. Knepley if (locA) { 44693e9753d6SMatthew G. Knepley PetscInt subcell; 44703e9753d6SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 44713e9753d6SMatthew G. Knepley ierr = DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);CHKERRQ(ierr); 44723e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dmAux, subcell, &probAux);CHKERRQ(ierr); 44733e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 44743e9753d6SMatthew G. Knepley } 44753e9753d6SMatthew G. Knepley /* 2: Get geometric data */ 44763e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 44773e9753d6SMatthew G. Knepley PetscObject obj; 44783e9753d6SMatthew G. Knepley PetscClassId id; 44793e9753d6SMatthew G. Knepley PetscBool fimp; 44803e9753d6SMatthew G. Knepley 44813e9753d6SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 44823e9753d6SMatthew G. Knepley if (isImplicit != fimp) continue; 44833e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 44843e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 44853e9753d6SMatthew G. Knepley if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;} 44863e9753d6SMatthew G. Knepley if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;} 44873e9753d6SMatthew G. Knepley } 44883e9753d6SMatthew G. Knepley if (useFEM) { 44893e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 44903e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 44913e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 44923e9753d6SMatthew G. Knepley ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr); 44933e9753d6SMatthew G. Knepley if (affineQuad) { 44943e9753d6SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 44953e9753d6SMatthew G. Knepley } 44963e9753d6SMatthew G. Knepley } else { 44973e9753d6SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 44983e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 44993e9753d6SMatthew G. Knepley PetscObject obj; 45003e9753d6SMatthew G. Knepley PetscClassId id; 45013e9753d6SMatthew G. Knepley PetscBool fimp; 45023e9753d6SMatthew G. Knepley 45033e9753d6SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 45043e9753d6SMatthew G. Knepley if (isImplicit != fimp) continue; 45053e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 45063e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 45073e9753d6SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 45083e9753d6SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 45093e9753d6SMatthew G. Knepley 45103e9753d6SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 45113e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr); 45123e9753d6SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 45133e9753d6SMatthew G. Knepley } 45143e9753d6SMatthew G. Knepley } 45153e9753d6SMatthew G. Knepley } 45163e9753d6SMatthew G. Knepley } 45173e9753d6SMatthew G. Knepley if (useFVM) { 45183e9753d6SMatthew G. Knepley ierr = DMPlexGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr); 45193e9753d6SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr); 45203e9753d6SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr); 45213e9753d6SMatthew G. Knepley /* Reconstruct and limit cell gradients */ 45223e9753d6SMatthew G. Knepley ierr = DMPlexGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr); 45233e9753d6SMatthew G. Knepley if (dmGrad) { 45243e9753d6SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 45253e9753d6SMatthew G. Knepley ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 45263e9753d6SMatthew G. Knepley ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr); 45273e9753d6SMatthew G. Knepley /* Communicate gradient values */ 45283e9753d6SMatthew G. Knepley ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr); 45293e9753d6SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 45303e9753d6SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr); 45313e9753d6SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr); 45323e9753d6SMatthew G. Knepley } 45333e9753d6SMatthew G. Knepley /* Handle non-essential (e.g. outflow) boundary values */ 45343e9753d6SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr); 45353e9753d6SMatthew G. Knepley } 45363e9753d6SMatthew G. Knepley /* Loop over chunks */ 45373e9753d6SMatthew G. Knepley if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);} 45383e9753d6SMatthew G. Knepley numCells = cEnd - cStart; 45393e9753d6SMatthew G. Knepley numChunks = 1; 45403e9753d6SMatthew G. Knepley cellChunkSize = numCells/numChunks; 45413e9753d6SMatthew G. Knepley faceChunkSize = (fEnd - fStart)/numChunks; 45423e9753d6SMatthew G. Knepley numChunks = PetscMin(1,numCells); 45433e9753d6SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 45443e9753d6SMatthew G. Knepley PetscScalar *elemVec, *fluxL, *fluxR; 45453e9753d6SMatthew G. Knepley PetscReal *vol; 45463e9753d6SMatthew G. Knepley PetscFVFaceGeom *fgeom; 45473e9753d6SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 45483e9753d6SMatthew G. Knepley PetscInt fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face; 45493e9753d6SMatthew G. Knepley 45503e9753d6SMatthew G. Knepley /* Extract field coefficients */ 45513e9753d6SMatthew G. Knepley if (useFEM) { 45523e9753d6SMatthew G. Knepley ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr); 45533e9753d6SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 45543e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 45553e9753d6SMatthew G. Knepley ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr); 45563e9753d6SMatthew G. Knepley } 45573e9753d6SMatthew G. Knepley if (useFVM) { 45583e9753d6SMatthew G. Knepley ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 45593e9753d6SMatthew G. Knepley ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 45603e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 45613e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 45623e9753d6SMatthew G. Knepley ierr = PetscArrayzero(fluxL, numFaces*totDim);CHKERRQ(ierr); 45633e9753d6SMatthew G. Knepley ierr = PetscArrayzero(fluxR, numFaces*totDim);CHKERRQ(ierr); 45643e9753d6SMatthew G. Knepley } 45653e9753d6SMatthew G. Knepley /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */ 45663e9753d6SMatthew G. Knepley /* Loop over fields */ 45673e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 45683e9753d6SMatthew G. Knepley PetscObject obj; 45693e9753d6SMatthew G. Knepley PetscClassId id; 45703e9753d6SMatthew G. Knepley PetscBool fimp; 45713e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 45723e9753d6SMatthew G. Knepley 45733e9753d6SMatthew G. Knepley ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr); 45743e9753d6SMatthew G. Knepley if (isImplicit != fimp) continue; 45753e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 45763e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 45773e9753d6SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 45783e9753d6SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 45793e9753d6SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 45803e9753d6SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 45813e9753d6SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 45823e9753d6SMatthew G. Knepley PetscInt Nq, Nb; 45833e9753d6SMatthew G. Knepley 45843e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 45853e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 45863e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 45873e9753d6SMatthew G. Knepley blockSize = Nb; 45883e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 45893e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 45903e9753d6SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 45913e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 45923e9753d6SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 45933e9753d6SMatthew G. Knepley offset = numCells - Nr; 45943e9753d6SMatthew G. Knepley /* Integrate FE residual to get elemVec (need fields at quadrature points) */ 45953e9753d6SMatthew G. Knepley /* For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */ 45963e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 45973e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 45983e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 45993e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 46003e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 46013e9753d6SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 46023e9753d6SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 46033e9753d6SMatthew G. Knepley 46043e9753d6SMatthew G. Knepley Ne = numFaces; 46053e9753d6SMatthew G. Knepley /* Riemann solve over faces (need fields at face centroids) */ 46063e9753d6SMatthew G. Knepley /* We need to evaluate FE fields at those coordinates */ 46073e9753d6SMatthew G. Knepley ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr); 46083e9753d6SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f); 46093e9753d6SMatthew G. Knepley } 46103e9753d6SMatthew G. Knepley /* Loop over domain */ 46113e9753d6SMatthew G. Knepley if (useFEM) { 46123e9753d6SMatthew G. Knepley /* Add elemVec to locX */ 46133e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 46143e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 46153e9753d6SMatthew G. Knepley const PetscInt cind = c - cStart; 46163e9753d6SMatthew G. Knepley 46173e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 46183e9753d6SMatthew G. Knepley if (ghostLabel) { 46193e9753d6SMatthew G. Knepley PetscInt ghostVal; 46203e9753d6SMatthew G. Knepley 46213e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 46223e9753d6SMatthew G. Knepley if (ghostVal > 0) continue; 46233e9753d6SMatthew G. Knepley } 46243e9753d6SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 46253e9753d6SMatthew G. Knepley } 46263e9753d6SMatthew G. Knepley } 46273e9753d6SMatthew G. Knepley if (useFVM) { 46283e9753d6SMatthew G. Knepley PetscScalar *fa; 46293e9753d6SMatthew G. Knepley PetscInt iface; 46303e9753d6SMatthew G. Knepley 46313e9753d6SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 46323e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 46333e9753d6SMatthew G. Knepley PetscFV fv; 46343e9753d6SMatthew G. Knepley PetscObject obj; 46353e9753d6SMatthew G. Knepley PetscClassId id; 46363e9753d6SMatthew G. Knepley PetscInt foff, pdim; 46373e9753d6SMatthew G. Knepley 46383e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 46393e9753d6SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 46403e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 46413e9753d6SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 46423e9753d6SMatthew G. Knepley fv = (PetscFV) obj; 46433e9753d6SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 46443e9753d6SMatthew G. Knepley /* Accumulate fluxes to cells */ 46453e9753d6SMatthew G. Knepley for (face = fS, iface = 0; face < fE; ++face) { 46463e9753d6SMatthew G. Knepley const PetscInt *scells; 46473e9753d6SMatthew G. Knepley PetscScalar *fL = NULL, *fR = NULL; 46483e9753d6SMatthew G. Knepley PetscInt ghost, d, nsupp, nchild; 46493e9753d6SMatthew G. Knepley 46503e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr); 46513e9753d6SMatthew G. Knepley ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr); 46523e9753d6SMatthew G. Knepley ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr); 46533e9753d6SMatthew G. Knepley if (ghost >= 0 || nsupp > 2 || nchild > 0) continue; 46543e9753d6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr); 46553e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr); 46563e9753d6SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);} 46573e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr); 46583e9753d6SMatthew G. Knepley if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);} 46593e9753d6SMatthew G. Knepley for (d = 0; d < pdim; ++d) { 46603e9753d6SMatthew G. Knepley if (fL) fL[d] -= fluxL[iface*totDim+foff+d]; 46613e9753d6SMatthew G. Knepley if (fR) fR[d] += fluxR[iface*totDim+foff+d]; 46623e9753d6SMatthew G. Knepley } 46633e9753d6SMatthew G. Knepley ++iface; 46643e9753d6SMatthew G. Knepley } 46653e9753d6SMatthew G. Knepley } 46663e9753d6SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 46673e9753d6SMatthew G. Knepley } 46683e9753d6SMatthew G. Knepley /* Handle time derivative */ 46693e9753d6SMatthew G. Knepley if (locX_t) { 46703e9753d6SMatthew G. Knepley PetscScalar *x_t, *fa; 46713e9753d6SMatthew G. Knepley 46723e9753d6SMatthew G. Knepley ierr = VecGetArray(locF, &fa);CHKERRQ(ierr); 46733e9753d6SMatthew G. Knepley ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr); 46743e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 46753e9753d6SMatthew G. Knepley PetscFV fv; 46763e9753d6SMatthew G. Knepley PetscObject obj; 46773e9753d6SMatthew G. Knepley PetscClassId id; 46783e9753d6SMatthew G. Knepley PetscInt pdim, d; 46793e9753d6SMatthew G. Knepley 46803e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 46813e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 46823e9753d6SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 46833e9753d6SMatthew G. Knepley fv = (PetscFV) obj; 46843e9753d6SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 46853e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 46863e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 46873e9753d6SMatthew G. Knepley PetscScalar *u_t, *r; 46883e9753d6SMatthew G. Knepley 46893e9753d6SMatthew G. Knepley if (ghostLabel) { 46903e9753d6SMatthew G. Knepley PetscInt ghostVal; 46913e9753d6SMatthew G. Knepley 46923e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr); 46933e9753d6SMatthew G. Knepley if (ghostVal > 0) continue; 46943e9753d6SMatthew G. Knepley } 46953e9753d6SMatthew G. Knepley ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr); 46963e9753d6SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr); 46973e9753d6SMatthew G. Knepley for (d = 0; d < pdim; ++d) r[d] += u_t[d]; 46983e9753d6SMatthew G. Knepley } 46993e9753d6SMatthew G. Knepley } 47003e9753d6SMatthew G. Knepley ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr); 47013e9753d6SMatthew G. Knepley ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr); 47023e9753d6SMatthew G. Knepley } 47033e9753d6SMatthew G. Knepley if (useFEM) { 47043e9753d6SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 47053e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 47063e9753d6SMatthew G. Knepley } 47073e9753d6SMatthew G. Knepley if (useFVM) { 47083e9753d6SMatthew G. Knepley ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr); 47093e9753d6SMatthew G. Knepley ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr); 47103e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr); 47113e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr); 47123e9753d6SMatthew G. Knepley if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);} 47133e9753d6SMatthew G. Knepley } 47143e9753d6SMatthew G. Knepley } 47153e9753d6SMatthew G. Knepley if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);} 47163e9753d6SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 47173e9753d6SMatthew G. Knepley 47183e9753d6SMatthew G. Knepley if (useFEM) { 47193e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr); 47203e9753d6SMatthew G. Knepley 47213e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 47223e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 47233e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 47243e9753d6SMatthew G. Knepley } else { 47253e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 47263e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr); 47273e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr); 47283e9753d6SMatthew G. Knepley } 47293e9753d6SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 47303e9753d6SMatthew G. Knepley } 47313e9753d6SMatthew G. Knepley } 47323e9753d6SMatthew G. Knepley 47333e9753d6SMatthew G. Knepley /* FEM */ 47343e9753d6SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 47353e9753d6SMatthew G. Knepley /* 2: Get geometric data */ 47363e9753d6SMatthew G. Knepley /* 3: Handle boundary values */ 47373e9753d6SMatthew G. Knepley /* 4: Loop over domain */ 47383e9753d6SMatthew G. Knepley /* Extract coefficients */ 47393e9753d6SMatthew G. Knepley /* Loop over fields */ 47403e9753d6SMatthew G. Knepley /* Set tiling for FE*/ 47413e9753d6SMatthew G. Knepley /* Integrate FE residual to get elemVec */ 47423e9753d6SMatthew G. Knepley /* Loop over subdomain */ 47433e9753d6SMatthew G. Knepley /* Loop over quad points */ 47443e9753d6SMatthew G. Knepley /* Transform coords to real space */ 47453e9753d6SMatthew G. Knepley /* Evaluate field and aux fields at point */ 47463e9753d6SMatthew G. Knepley /* Evaluate residual at point */ 47473e9753d6SMatthew G. Knepley /* Transform residual to real space */ 47483e9753d6SMatthew G. Knepley /* Add residual to elemVec */ 47493e9753d6SMatthew G. Knepley /* Loop over domain */ 47503e9753d6SMatthew G. Knepley /* Add elemVec to locX */ 47513e9753d6SMatthew G. Knepley 47523e9753d6SMatthew G. Knepley /* FVM */ 47533e9753d6SMatthew G. Knepley /* Get geometric data */ 47543e9753d6SMatthew G. Knepley /* If using gradients */ 47553e9753d6SMatthew G. Knepley /* Compute gradient data */ 47563e9753d6SMatthew G. Knepley /* Loop over domain faces */ 47573e9753d6SMatthew G. Knepley /* Count computational faces */ 47583e9753d6SMatthew G. Knepley /* Reconstruct cell gradient */ 47593e9753d6SMatthew G. Knepley /* Loop over domain cells */ 47603e9753d6SMatthew G. Knepley /* Limit cell gradients */ 47613e9753d6SMatthew G. Knepley /* Handle boundary values */ 47623e9753d6SMatthew G. Knepley /* Loop over domain faces */ 47633e9753d6SMatthew G. Knepley /* Read out field, centroid, normal, volume for each side of face */ 47643e9753d6SMatthew G. Knepley /* Riemann solve over faces */ 47653e9753d6SMatthew G. Knepley /* Loop over domain faces */ 47663e9753d6SMatthew G. Knepley /* Accumulate fluxes to cells */ 47673e9753d6SMatthew G. Knepley /* TODO Change printFEM to printDisc here */ 47683e9753d6SMatthew G. Knepley if (mesh->printFEM) { 47693e9753d6SMatthew G. Knepley Vec locFbc; 47703e9753d6SMatthew G. Knepley PetscInt pStart, pEnd, p, maxDof; 47713e9753d6SMatthew G. Knepley PetscScalar *zeroes; 47723e9753d6SMatthew G. Knepley 47733e9753d6SMatthew G. Knepley ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr); 47743e9753d6SMatthew G. Knepley ierr = VecCopy(locF,locFbc);CHKERRQ(ierr); 47753e9753d6SMatthew G. Knepley ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr); 47763e9753d6SMatthew G. Knepley ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr); 47773e9753d6SMatthew G. Knepley ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr); 47783e9753d6SMatthew G. Knepley for (p = pStart; p < pEnd; p++) { 47793e9753d6SMatthew G. Knepley ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr); 47803e9753d6SMatthew G. Knepley } 47813e9753d6SMatthew G. Knepley ierr = PetscFree(zeroes);CHKERRQ(ierr); 47823e9753d6SMatthew G. Knepley ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr); 47833e9753d6SMatthew G. Knepley ierr = VecDestroy(&locFbc);CHKERRQ(ierr); 47843e9753d6SMatthew G. Knepley } 47853e9753d6SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 47863e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 47873e9753d6SMatthew G. Knepley } 47883e9753d6SMatthew G. Knepley 47893e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user) 47903e9753d6SMatthew G. Knepley { 47913e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 47923e9753d6SMatthew G. Knepley const char *name = "Hybrid Residual"; 47933e9753d6SMatthew G. Knepley DM dmAux = NULL; 47943e9753d6SMatthew G. Knepley DMLabel ghostLabel = NULL; 47953e9753d6SMatthew G. Knepley PetscDS prob = NULL; 47963e9753d6SMatthew G. Knepley PetscDS probAux = NULL; 47973e9753d6SMatthew G. Knepley PetscSection section = NULL; 47983e9753d6SMatthew G. Knepley DMField coordField = NULL; 47993e9753d6SMatthew G. Knepley Vec locA; 48003e9753d6SMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a; 48013e9753d6SMatthew G. Knepley PetscScalar *elemVec; 48023e9753d6SMatthew G. Knepley IS chunkIS; 48033e9753d6SMatthew G. Knepley const PetscInt *cells; 48043e9753d6SMatthew G. Knepley PetscInt *faces; 48053e9753d6SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 48063e9753d6SMatthew G. Knepley PetscInt Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk; 48073e9753d6SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 48083e9753d6SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 48093e9753d6SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 48103e9753d6SMatthew G. Knepley PetscErrorCode ierr; 48113e9753d6SMatthew G. Knepley 48123e9753d6SMatthew G. Knepley PetscFunctionBegin; 48133e9753d6SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 48143e9753d6SMatthew G. Knepley /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */ 48153e9753d6SMatthew G. Knepley /* FEM */ 48163e9753d6SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 48173e9753d6SMatthew G. Knepley /* 1: Get sizes from dm and dmAux */ 48183e9753d6SMatthew G. Knepley ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 48193e9753d6SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 48203e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 48213e9753d6SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 48223e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 48233e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 48243e9753d6SMatthew G. Knepley if (locA) { 48253e9753d6SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 48263e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr); 48273e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 48283e9753d6SMatthew G. Knepley } 48293e9753d6SMatthew G. Knepley /* 2: Setup geometric data */ 48303e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 48313e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr); 48323e9753d6SMatthew G. Knepley if (maxDegree > 1) { 48333e9753d6SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 48343e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 48353e9753d6SMatthew G. Knepley PetscFE fe; 48363e9753d6SMatthew G. Knepley 48373e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 48383e9753d6SMatthew G. Knepley if (fe) { 48393e9753d6SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 48403e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr); 48413e9753d6SMatthew G. Knepley } 48423e9753d6SMatthew G. Knepley } 48433e9753d6SMatthew G. Knepley } 48443e9753d6SMatthew G. Knepley /* Loop over chunks */ 48453e9753d6SMatthew G. Knepley numCells = cEnd - cStart; 48463e9753d6SMatthew G. Knepley cellChunkSize = numCells; 48473e9753d6SMatthew G. Knepley numChunks = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize); 48483e9753d6SMatthew G. Knepley ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr); 48493e9753d6SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr); 48503e9753d6SMatthew G. Knepley /* Extract field coefficients */ 48513e9753d6SMatthew G. Knepley /* NOTE This needs the end cap faces to have identical orientations */ 48523e9753d6SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 48533e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 48543e9753d6SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 48553e9753d6SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 48563e9753d6SMatthew G. Knepley 48573e9753d6SMatthew G. Knepley ierr = PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 48583e9753d6SMatthew G. Knepley /* Get faces */ 48593e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 48603e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 48613e9753d6SMatthew G. Knepley const PetscInt *cone; 48623e9753d6SMatthew G. Knepley ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr); 48633e9753d6SMatthew G. Knepley faces[(c-cS)*2+0] = cone[0]; 48643e9753d6SMatthew G. Knepley faces[(c-cS)*2+1] = cone[1]; 48653e9753d6SMatthew G. Knepley } 48663e9753d6SMatthew G. Knepley ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr); 48673e9753d6SMatthew G. Knepley /* Get geometric data */ 48683e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 48693e9753d6SMatthew G. Knepley if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);} 48703e9753d6SMatthew G. Knepley if (affineQuad) {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);} 48713e9753d6SMatthew G. Knepley } else { 48723e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 48733e9753d6SMatthew G. Knepley if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);} 48743e9753d6SMatthew G. Knepley } 48753e9753d6SMatthew G. Knepley } 48763e9753d6SMatthew G. Knepley /* Loop over fields */ 48773e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 48783e9753d6SMatthew G. Knepley PetscFE fe; 48793e9753d6SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[f]; 48803e9753d6SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 48813e9753d6SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; 48823e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb; 48833e9753d6SMatthew G. Knepley 48843e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 48853e9753d6SMatthew G. Knepley if (!fe) continue; 48863e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 48873e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 48883e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 48893e9753d6SMatthew G. Knepley blockSize = Nb; 48903e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 48913e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 48923e9753d6SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 48933e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 48943e9753d6SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 48953e9753d6SMatthew G. Knepley offset = numCells - Nr; 48963e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 48973e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr); 48983e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 48993e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr); 49003e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr); 49013e9753d6SMatthew G. Knepley } 49023e9753d6SMatthew G. Knepley /* Add elemVec to locX */ 49033e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 49043e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 49053e9753d6SMatthew G. Knepley const PetscInt cind = c - cStart; 49063e9753d6SMatthew G. Knepley 49073e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);} 49083e9753d6SMatthew G. Knepley if (ghostLabel) { 49093e9753d6SMatthew G. Knepley PetscInt ghostVal; 49103e9753d6SMatthew G. Knepley 49113e9753d6SMatthew G. Knepley ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr); 49123e9753d6SMatthew G. Knepley if (ghostVal > 0) continue; 49133e9753d6SMatthew G. Knepley } 49143e9753d6SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr); 49153e9753d6SMatthew G. Knepley } 49163e9753d6SMatthew G. Knepley } 49173e9753d6SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 49183e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr); 49193e9753d6SMatthew G. Knepley ierr = PetscFree(faces);CHKERRQ(ierr); 49203e9753d6SMatthew G. Knepley ierr = ISDestroy(&chunkIS);CHKERRQ(ierr); 49213e9753d6SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 49223e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 49233e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 49243e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 49253e9753d6SMatthew G. Knepley } else { 49263e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 49273e9753d6SMatthew G. Knepley if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);} 49283e9753d6SMatthew G. Knepley if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);} 49293e9753d6SMatthew G. Knepley } 49303e9753d6SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 49313e9753d6SMatthew G. Knepley } 49323e9753d6SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 49333e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 49343e9753d6SMatthew G. Knepley } 49353e9753d6SMatthew G. Knepley 49363e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS) 49373e9753d6SMatthew G. Knepley { 49383e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 49393e9753d6SMatthew G. Knepley DM plex = NULL, plexA = NULL, tdm; 49403e9753d6SMatthew G. Knepley DMEnclosureType encAux; 49413e9753d6SMatthew G. Knepley PetscDS prob, probAux = NULL; 49423e9753d6SMatthew G. Knepley PetscSection section, sectionAux = NULL; 49433e9753d6SMatthew G. Knepley PetscSection globalSection, subSection = NULL; 49443e9753d6SMatthew G. Knepley Vec locA = NULL, tv; 49453e9753d6SMatthew G. Knepley PetscScalar *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL; 49463e9753d6SMatthew G. Knepley PetscInt v; 49473e9753d6SMatthew G. Knepley PetscInt Nf, totDim, totDimAux = 0; 49483e9753d6SMatthew G. Knepley PetscBool isMatISP, transform; 49493e9753d6SMatthew G. Knepley PetscErrorCode ierr; 49503e9753d6SMatthew G. Knepley 49513e9753d6SMatthew G. Knepley PetscFunctionBegin; 49523e9753d6SMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 49533e9753d6SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 49543e9753d6SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 49553e9753d6SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 49563e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 49573e9753d6SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 49583e9753d6SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 49593e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 49603e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 49613e9753d6SMatthew G. Knepley if (locA) { 49623e9753d6SMatthew G. Knepley DM dmAux; 49633e9753d6SMatthew G. Knepley 49643e9753d6SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 49653e9753d6SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 49663e9753d6SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 49673e9753d6SMatthew G. Knepley ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr); 49683e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 49693e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(plexA, §ionAux);CHKERRQ(ierr); 49703e9753d6SMatthew G. Knepley } 49713e9753d6SMatthew G. Knepley 49723e9753d6SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 49733e9753d6SMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 49743e9753d6SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 49753e9753d6SMatthew G. Knepley for (v = 0; v < numValues; ++v) { 49763e9753d6SMatthew G. Knepley PetscFEGeom *fgeom; 49773e9753d6SMatthew G. Knepley PetscInt maxDegree; 49783e9753d6SMatthew G. Knepley PetscQuadrature qGeom = NULL; 49793e9753d6SMatthew G. Knepley IS pointIS; 49803e9753d6SMatthew G. Knepley const PetscInt *points; 49813e9753d6SMatthew G. Knepley PetscInt numFaces, face, Nq; 49823e9753d6SMatthew G. Knepley 49833e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 49843e9753d6SMatthew G. Knepley if (!pointIS) continue; /* No points with that id on this process */ 49853e9753d6SMatthew G. Knepley { 49863e9753d6SMatthew G. Knepley IS isectIS; 49873e9753d6SMatthew G. Knepley 49883e9753d6SMatthew G. Knepley /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */ 49893e9753d6SMatthew G. Knepley ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr); 49903e9753d6SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 49913e9753d6SMatthew G. Knepley pointIS = isectIS; 49923e9753d6SMatthew G. Knepley } 49933e9753d6SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr); 49943e9753d6SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 49953e9753d6SMatthew G. Knepley ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr); 49963e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr); 49973e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 49983e9753d6SMatthew G. Knepley ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr); 49993e9753d6SMatthew G. Knepley } 50003e9753d6SMatthew G. Knepley if (!qGeom) { 50013e9753d6SMatthew G. Knepley PetscFE fe; 50023e9753d6SMatthew G. Knepley 50033e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 50043e9753d6SMatthew G. Knepley ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr); 50053e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 50063e9753d6SMatthew G. Knepley } 50073e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 50083e9753d6SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 50093e9753d6SMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 50103e9753d6SMatthew G. Knepley const PetscInt point = points[face], *support, *cone; 50113e9753d6SMatthew G. Knepley PetscScalar *x = NULL; 50123e9753d6SMatthew G. Knepley PetscInt i, coneSize, faceLoc; 50133e9753d6SMatthew G. Knepley 50143e9753d6SMatthew G. Knepley ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 50153e9753d6SMatthew G. Knepley ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr); 50163e9753d6SMatthew G. Knepley ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr); 50173e9753d6SMatthew G. Knepley for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break; 50183e9753d6SMatthew G. Knepley if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]); 50193e9753d6SMatthew G. Knepley fgeom->face[face][0] = faceLoc; 50203e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 50213e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i]; 50223e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr); 50233e9753d6SMatthew G. Knepley if (locX_t) { 50243e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 50253e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i]; 50263e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr); 50273e9753d6SMatthew G. Knepley } 50283e9753d6SMatthew G. Knepley if (locA) { 50293e9753d6SMatthew G. Knepley PetscInt subp; 50303e9753d6SMatthew G. Knepley ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr); 50313e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 50323e9753d6SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i]; 50333e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr); 50343e9753d6SMatthew G. Knepley } 50353e9753d6SMatthew G. Knepley } 50363e9753d6SMatthew G. Knepley ierr = PetscArrayzero(elemMat, numFaces*totDim*totDim);CHKERRQ(ierr); 50373e9753d6SMatthew G. Knepley { 50383e9753d6SMatthew G. Knepley PetscFE fe; 50393e9753d6SMatthew G. Knepley PetscInt Nb; 50403e9753d6SMatthew G. Knepley /* Conforming batches */ 50413e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 50423e9753d6SMatthew G. Knepley /* Remainder */ 50433e9753d6SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL; 50443e9753d6SMatthew G. Knepley PetscInt fieldJ, Nr, offset; 50453e9753d6SMatthew G. Knepley 50463e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 50473e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 50483e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 50493e9753d6SMatthew G. Knepley blockSize = Nb; 50503e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 50513e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 50523e9753d6SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 50533e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 50543e9753d6SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 50553e9753d6SMatthew G. Knepley offset = numFaces - Nr; 50563e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr); 50573e9753d6SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 50583e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 50593e9753d6SMatthew G. Knepley } 50603e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 50613e9753d6SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 50623e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 50633e9753d6SMatthew G. Knepley } 50643e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr); 50653e9753d6SMatthew G. Knepley } 50663e9753d6SMatthew G. Knepley for (face = 0; face < numFaces; ++face) { 50673e9753d6SMatthew G. Knepley const PetscInt point = points[face], *support; 50683e9753d6SMatthew G. Knepley 50693e9753d6SMatthew G. Knepley /* Transform to global basis before insertion in Jacobian */ 50703e9753d6SMatthew G. Knepley ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr); 50713e9753d6SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 50723e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);} 50733e9753d6SMatthew G. Knepley if (!isMatISP) { 50743e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 50753e9753d6SMatthew G. Knepley } else { 50763e9753d6SMatthew G. Knepley Mat lJ; 50773e9753d6SMatthew G. Knepley 50783e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr); 50793e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 50803e9753d6SMatthew G. Knepley } 50813e9753d6SMatthew G. Knepley } 50823e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr); 50833e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 50843e9753d6SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 50853e9753d6SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 50863e9753d6SMatthew G. Knepley ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr); 50873e9753d6SMatthew G. Knepley } 50883e9753d6SMatthew G. Knepley if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);} 50893e9753d6SMatthew G. Knepley if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 50903e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 50913e9753d6SMatthew G. Knepley } 50923e9753d6SMatthew G. Knepley 50933e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP) 50943e9753d6SMatthew G. Knepley { 50953e9753d6SMatthew G. Knepley DMField coordField; 50963e9753d6SMatthew G. Knepley DMLabel depthLabel; 50973e9753d6SMatthew G. Knepley IS facetIS; 50983e9753d6SMatthew G. Knepley PetscInt dim; 50993e9753d6SMatthew G. Knepley PetscErrorCode ierr; 51003e9753d6SMatthew G. Knepley 51013e9753d6SMatthew G. Knepley PetscFunctionBegin; 51023e9753d6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 51033e9753d6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 51043e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 51053e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 51063e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 51073e9753d6SMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 51083e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 51093e9753d6SMatthew G. Knepley } 51103e9753d6SMatthew G. Knepley 51113e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user) 51123e9753d6SMatthew G. Knepley { 51133e9753d6SMatthew G. Knepley PetscDS prob; 51143e9753d6SMatthew G. Knepley PetscInt dim, numBd, bd; 51153e9753d6SMatthew G. Knepley DMLabel depthLabel; 51163e9753d6SMatthew G. Knepley DMField coordField = NULL; 51173e9753d6SMatthew G. Knepley IS facetIS; 51183e9753d6SMatthew G. Knepley PetscErrorCode ierr; 51193e9753d6SMatthew G. Knepley 51203e9753d6SMatthew G. Knepley PetscFunctionBegin; 51213e9753d6SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 51223e9753d6SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 51233e9753d6SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 51243e9753d6SMatthew G. Knepley ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr); 51253e9753d6SMatthew G. Knepley ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr); 51263e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 51273e9753d6SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 51283e9753d6SMatthew G. Knepley DMBoundaryConditionType type; 51293e9753d6SMatthew G. Knepley const char *bdLabel; 51303e9753d6SMatthew G. Knepley DMLabel label; 51313e9753d6SMatthew G. Knepley const PetscInt *values; 51323e9753d6SMatthew G. Knepley PetscInt fieldI, numValues; 51333e9753d6SMatthew G. Knepley PetscObject obj; 51343e9753d6SMatthew G. Knepley PetscClassId id; 51353e9753d6SMatthew G. Knepley 5136*56cf3b9cSMatthew G. Knepley ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 51373e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr); 51383e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 51393e9753d6SMatthew G. Knepley if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue; 51403e9753d6SMatthew G. Knepley ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 51413e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr); 51423e9753d6SMatthew G. Knepley } 51433e9753d6SMatthew G. Knepley ierr = ISDestroy(&facetIS);CHKERRQ(ierr); 51443e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 51453e9753d6SMatthew G. Knepley } 51463e9753d6SMatthew G. Knepley 51473e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 51483e9753d6SMatthew G. Knepley { 51493e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 51503e9753d6SMatthew G. Knepley const char *name = "Jacobian"; 51513e9753d6SMatthew G. Knepley DM dmAux, plex, tdm; 51523e9753d6SMatthew G. Knepley DMEnclosureType encAux; 51533e9753d6SMatthew G. Knepley Vec A, tv; 51543e9753d6SMatthew G. Knepley DMField coordField; 51553e9753d6SMatthew G. Knepley PetscDS prob, probAux = NULL; 51563e9753d6SMatthew G. Knepley PetscSection section, globalSection, subSection, sectionAux; 51573e9753d6SMatthew G. Knepley PetscScalar *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL; 51583e9753d6SMatthew G. Knepley const PetscInt *cells; 51593e9753d6SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ; 51603e9753d6SMatthew G. Knepley PetscInt totDim, totDimAux, cStart, cEnd, numCells, c; 51613e9753d6SMatthew G. Knepley PetscBool isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform; 51623e9753d6SMatthew G. Knepley PetscErrorCode ierr; 51633e9753d6SMatthew G. Knepley 51643e9753d6SMatthew G. Knepley PetscFunctionBegin; 51653e9753d6SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 51663e9753d6SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 51673e9753d6SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 51683e9753d6SMatthew G. Knepley ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr); 51693e9753d6SMatthew G. Knepley ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr); 51703e9753d6SMatthew G. Knepley ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr); 51713e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(dm, §ion);CHKERRQ(ierr); 51723e9753d6SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 51733e9753d6SMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 51743e9753d6SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);} 51753e9753d6SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 51763e9753d6SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 51773e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr); 51783e9753d6SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 51793e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 51803e9753d6SMatthew G. Knepley ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr); 51813e9753d6SMatthew G. Knepley ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr); 51823e9753d6SMatthew G. Knepley /* user passed in the same matrix, avoid double contributions and 51833e9753d6SMatthew G. Knepley only assemble the Jacobian */ 51843e9753d6SMatthew G. Knepley if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE; 51853e9753d6SMatthew G. Knepley ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr); 51863e9753d6SMatthew G. Knepley hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE; 51873e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 51883e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 51893e9753d6SMatthew G. Knepley if (dmAux) { 51903e9753d6SMatthew G. Knepley ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr); 51913e9753d6SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr); 51923e9753d6SMatthew G. Knepley ierr = DMGetLocalSection(plex, §ionAux);CHKERRQ(ierr); 51933e9753d6SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 51943e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 51953e9753d6SMatthew G. Knepley } 51963e9753d6SMatthew G. Knepley ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr); 51973e9753d6SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 51983e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 51993e9753d6SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 52003e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 52013e9753d6SMatthew G. Knepley const PetscInt cind = c - cStart; 52023e9753d6SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 52033e9753d6SMatthew G. Knepley PetscInt i; 52043e9753d6SMatthew G. Knepley 52053e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 52063e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i]; 52073e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr); 52083e9753d6SMatthew G. Knepley if (X_t) { 52093e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 52103e9753d6SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i]; 52113e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr); 52123e9753d6SMatthew G. Knepley } 52133e9753d6SMatthew G. Knepley if (dmAux) { 52143e9753d6SMatthew G. Knepley PetscInt subcell; 52153e9753d6SMatthew G. Knepley ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr); 52163e9753d6SMatthew G. Knepley ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 52173e9753d6SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i]; 52183e9753d6SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr); 52193e9753d6SMatthew G. Knepley } 52203e9753d6SMatthew G. Knepley } 52213e9753d6SMatthew G. Knepley if (hasJac) {ierr = PetscArrayzero(elemMat, numCells*totDim*totDim);CHKERRQ(ierr);} 52223e9753d6SMatthew G. Knepley if (hasPrec) {ierr = PetscArrayzero(elemMatP, numCells*totDim*totDim);CHKERRQ(ierr);} 52233e9753d6SMatthew G. Knepley if (hasDyn) {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);} 52243e9753d6SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 52253e9753d6SMatthew G. Knepley PetscClassId id; 52263e9753d6SMatthew G. Knepley PetscFE fe; 52273e9753d6SMatthew G. Knepley PetscQuadrature qGeom = NULL; 52283e9753d6SMatthew G. Knepley PetscInt Nb; 52293e9753d6SMatthew G. Knepley /* Conforming batches */ 52303e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 52313e9753d6SMatthew G. Knepley /* Remainder */ 52323e9753d6SMatthew G. Knepley PetscInt Nr, offset, Nq; 52333e9753d6SMatthew G. Knepley PetscInt maxDegree; 52343e9753d6SMatthew G. Knepley PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL; 52353e9753d6SMatthew G. Knepley 52363e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 52373e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr); 52383e9753d6SMatthew G. Knepley if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;} 52393e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 52403e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 52413e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr); 52423e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 52433e9753d6SMatthew G. Knepley ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr); 52443e9753d6SMatthew G. Knepley } 52453e9753d6SMatthew G. Knepley if (!qGeom) { 52463e9753d6SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr); 52473e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr); 52483e9753d6SMatthew G. Knepley } 52493e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 52503e9753d6SMatthew G. Knepley ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 52513e9753d6SMatthew G. Knepley blockSize = Nb; 52523e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 52533e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 52543e9753d6SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 52553e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 52563e9753d6SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 52573e9753d6SMatthew G. Knepley offset = numCells - Nr; 52583e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 52593e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 52603e9753d6SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 52613e9753d6SMatthew G. Knepley if (hasJac) { 52623e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 52633e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 52643e9753d6SMatthew G. Knepley } 52653e9753d6SMatthew G. Knepley if (hasPrec) { 52663e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 52673e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 52683e9753d6SMatthew G. Knepley } 52693e9753d6SMatthew G. Knepley if (hasDyn) { 52703e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr); 52713e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr); 52723e9753d6SMatthew G. Knepley } 52733e9753d6SMatthew G. Knepley } 52743e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr); 52753e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr); 52763e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr); 52773e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr); 52783e9753d6SMatthew G. Knepley } 52793e9753d6SMatthew G. Knepley /* Add contribution from X_t */ 52803e9753d6SMatthew G. Knepley if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];} 52813e9753d6SMatthew G. Knepley if (hasFV) { 52823e9753d6SMatthew G. Knepley PetscClassId id; 52833e9753d6SMatthew G. Knepley PetscFV fv; 52843e9753d6SMatthew G. Knepley PetscInt offsetI, NcI, NbI = 1, fc, f; 52853e9753d6SMatthew G. Knepley 52863e9753d6SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 52873e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr); 52883e9753d6SMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr); 52893e9753d6SMatthew G. Knepley ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr); 52903e9753d6SMatthew G. Knepley if (id != PETSCFV_CLASSID) continue; 52913e9753d6SMatthew G. Knepley /* Put in the identity */ 52923e9753d6SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr); 52933e9753d6SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 52943e9753d6SMatthew G. Knepley const PetscInt cind = c - cStart; 52953e9753d6SMatthew G. Knepley const PetscInt eOffset = cind*totDim*totDim; 52963e9753d6SMatthew G. Knepley for (fc = 0; fc < NcI; ++fc) { 52973e9753d6SMatthew G. Knepley for (f = 0; f < NbI; ++f) { 52983e9753d6SMatthew G. Knepley const PetscInt i = offsetI + f*NcI+fc; 52993e9753d6SMatthew G. Knepley if (hasPrec) { 53003e9753d6SMatthew G. Knepley if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;} 53013e9753d6SMatthew G. Knepley elemMatP[eOffset+i*totDim+i] = 1.0; 53023e9753d6SMatthew G. Knepley } else {elemMat[eOffset+i*totDim+i] = 1.0;} 53033e9753d6SMatthew G. Knepley } 53043e9753d6SMatthew G. Knepley } 53053e9753d6SMatthew G. Knepley } 53063e9753d6SMatthew G. Knepley } 53073e9753d6SMatthew G. Knepley /* No allocated space for FV stuff, so ignore the zero entries */ 53083e9753d6SMatthew G. Knepley ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr); 53093e9753d6SMatthew G. Knepley } 53103e9753d6SMatthew G. Knepley /* Insert values into matrix */ 53113e9753d6SMatthew G. Knepley isMatIS = PETSC_FALSE; 53123e9753d6SMatthew G. Knepley if (hasPrec && hasJac) { 53133e9753d6SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr); 53143e9753d6SMatthew G. Knepley } 53153e9753d6SMatthew G. Knepley if (isMatIS && !subSection) { 53163e9753d6SMatthew G. Knepley ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr); 53173e9753d6SMatthew G. Knepley } 53183e9753d6SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 53193e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 53203e9753d6SMatthew G. Knepley const PetscInt cind = c - cStart; 53213e9753d6SMatthew G. Knepley 53223e9753d6SMatthew G. Knepley /* Transform to global basis before insertion in Jacobian */ 53233e9753d6SMatthew G. Knepley if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 53243e9753d6SMatthew G. Knepley if (hasPrec) { 53253e9753d6SMatthew G. Knepley if (hasJac) { 53263e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 53273e9753d6SMatthew G. Knepley if (!isMatIS) { 53283e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53293e9753d6SMatthew G. Knepley } else { 53303e9753d6SMatthew G. Knepley Mat lJ; 53313e9753d6SMatthew G. Knepley 53323e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 53333e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53343e9753d6SMatthew G. Knepley } 53353e9753d6SMatthew G. Knepley } 53363e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 53373e9753d6SMatthew G. Knepley if (!isMatISP) { 53383e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53393e9753d6SMatthew G. Knepley } else { 53403e9753d6SMatthew G. Knepley Mat lJ; 53413e9753d6SMatthew G. Knepley 53423e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 53433e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53443e9753d6SMatthew G. Knepley } 53453e9753d6SMatthew G. Knepley } else { 53463e9753d6SMatthew G. Knepley if (hasJac) { 53473e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 53483e9753d6SMatthew G. Knepley if (!isMatISP) { 53493e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53503e9753d6SMatthew G. Knepley } else { 53513e9753d6SMatthew G. Knepley Mat lJ; 53523e9753d6SMatthew G. Knepley 53533e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 53543e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 53553e9753d6SMatthew G. Knepley } 53563e9753d6SMatthew G. Knepley } 53573e9753d6SMatthew G. Knepley } 53583e9753d6SMatthew G. Knepley } 53593e9753d6SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 53603e9753d6SMatthew G. Knepley if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);} 53613e9753d6SMatthew G. Knepley ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr); 53623e9753d6SMatthew G. Knepley if (dmAux) { 53633e9753d6SMatthew G. Knepley ierr = PetscFree(a);CHKERRQ(ierr); 53643e9753d6SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 53653e9753d6SMatthew G. Knepley } 53663e9753d6SMatthew G. Knepley /* Compute boundary integrals */ 53673e9753d6SMatthew G. Knepley ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr); 53683e9753d6SMatthew G. Knepley /* Assemble matrix */ 53693e9753d6SMatthew G. Knepley if (hasJac && hasPrec) { 53703e9753d6SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 53713e9753d6SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 53723e9753d6SMatthew G. Knepley } 53733e9753d6SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 53743e9753d6SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 53753e9753d6SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 53763e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 53773e9753d6SMatthew G. Knepley } 53783e9753d6SMatthew G. Knepley 53793e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user) 53803e9753d6SMatthew G. Knepley { 53813e9753d6SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 53823e9753d6SMatthew G. Knepley const char *name = "Hybrid Jacobian"; 53833e9753d6SMatthew G. Knepley DM dmAux = NULL; 53843e9753d6SMatthew G. Knepley DM plex = NULL; 53853e9753d6SMatthew G. Knepley DM plexA = NULL; 53863e9753d6SMatthew G. Knepley DMLabel ghostLabel = NULL; 53873e9753d6SMatthew G. Knepley PetscDS prob = NULL; 53883e9753d6SMatthew G. Knepley PetscDS probAux = NULL; 53893e9753d6SMatthew G. Knepley PetscSection section = NULL; 53903e9753d6SMatthew G. Knepley DMField coordField = NULL; 53913e9753d6SMatthew G. Knepley Vec locA; 53923e9753d6SMatthew G. Knepley PetscScalar *u = NULL, *u_t, *a = NULL; 53933e9753d6SMatthew G. Knepley PetscScalar *elemMat, *elemMatP; 53943e9753d6SMatthew G. Knepley PetscSection globalSection, subSection, sectionAux; 53953e9753d6SMatthew G. Knepley IS chunkIS; 53963e9753d6SMatthew G. Knepley const PetscInt *cells; 53973e9753d6SMatthew G. Knepley PetscInt *faces; 53983e9753d6SMatthew G. Knepley PetscInt cStart, cEnd, numCells; 53993e9753d6SMatthew G. Knepley PetscInt Nf, fieldI, fieldJ, totDim, totDimAux, numChunks, cellChunkSize, chunk; 54003e9753d6SMatthew G. Knepley PetscInt maxDegree = PETSC_MAX_INT; 54013e9753d6SMatthew G. Knepley PetscQuadrature affineQuad = NULL, *quads = NULL; 54023e9753d6SMatthew G. Knepley PetscFEGeom *affineGeom = NULL, **geoms = NULL; 54033e9753d6SMatthew G. Knepley PetscBool isMatIS = PETSC_FALSE, isMatISP = PETSC_FALSE, hasBdJac, hasBdPrec; 54043e9753d6SMatthew G. Knepley PetscErrorCode ierr; 54053e9753d6SMatthew G. Knepley 54063e9753d6SMatthew G. Knepley PetscFunctionBegin; 54073e9753d6SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 54083e9753d6SMatthew G. Knepley ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr); 54093e9753d6SMatthew G. Knepley ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 54103e9753d6SMatthew G. Knepley ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr); 54113e9753d6SMatthew G. Knepley ierr = DMGetSection(dm, §ion);CHKERRQ(ierr); 54123e9753d6SMatthew G. Knepley ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr); 54133e9753d6SMatthew G. Knepley ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr); 54143e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr); 54153e9753d6SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 54163e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 54173e9753d6SMatthew G. Knepley ierr = PetscDSHasBdJacobian(prob, &hasBdJac);CHKERRQ(ierr); 54183e9753d6SMatthew G. Knepley ierr = PetscDSHasBdJacobianPreconditioner(prob, &hasBdPrec);CHKERRQ(ierr); 54193e9753d6SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr); 54203e9753d6SMatthew G. Knepley if (isMatISP) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);} 54213e9753d6SMatthew G. Knepley if (hasBdPrec && hasBdJac) {ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);} 54223e9753d6SMatthew G. Knepley if (isMatIS && !subSection) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);} 54233e9753d6SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr); 54243e9753d6SMatthew G. Knepley if (locA) { 54253e9753d6SMatthew G. Knepley ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr); 54263e9753d6SMatthew G. Knepley ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr); 54273e9753d6SMatthew G. Knepley ierr = DMGetSection(dmAux, §ionAux);CHKERRQ(ierr); 54283e9753d6SMatthew G. Knepley ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr); 54293e9753d6SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 54303e9753d6SMatthew G. Knepley } 54313e9753d6SMatthew G. Knepley ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr); 54323e9753d6SMatthew G. Knepley ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr); 54333e9753d6SMatthew G. Knepley if (maxDegree > 1) { 54343e9753d6SMatthew G. Knepley PetscInt f; 54353e9753d6SMatthew G. Knepley ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr); 54363e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 54373e9753d6SMatthew G. Knepley PetscFE fe; 54383e9753d6SMatthew G. Knepley 54393e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 54403e9753d6SMatthew G. Knepley if (fe) { 54413e9753d6SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr); 54423e9753d6SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr); 54433e9753d6SMatthew G. Knepley } 54443e9753d6SMatthew G. Knepley } 54453e9753d6SMatthew G. Knepley } 54463e9753d6SMatthew G. Knepley cellChunkSize = numCells; 54473e9753d6SMatthew G. Knepley numChunks = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize); 54483e9753d6SMatthew G. Knepley ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr); 54493e9753d6SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr); 54503e9753d6SMatthew G. Knepley ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 54513e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, hasBdJac ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr); 54523e9753d6SMatthew G. Knepley ierr = DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr); 54533e9753d6SMatthew G. Knepley for (chunk = 0; chunk < numChunks; ++chunk) { 54543e9753d6SMatthew G. Knepley PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c; 54553e9753d6SMatthew G. Knepley 54563e9753d6SMatthew G. Knepley if (hasBdJac) {ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 54573e9753d6SMatthew G. Knepley if (hasBdPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);} 54583e9753d6SMatthew G. Knepley /* Get faces */ 54593e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 54603e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 54613e9753d6SMatthew G. Knepley const PetscInt *cone; 54623e9753d6SMatthew G. Knepley ierr = DMPlexGetCone(plex, cell, &cone);CHKERRQ(ierr); 54633e9753d6SMatthew G. Knepley faces[(c-cS)*2+0] = cone[0]; 54643e9753d6SMatthew G. Knepley faces[(c-cS)*2+1] = cone[1]; 54653e9753d6SMatthew G. Knepley } 54663e9753d6SMatthew G. Knepley ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr); 54673e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 54683e9753d6SMatthew G. Knepley if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);} 54693e9753d6SMatthew G. Knepley if (affineQuad) {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);} 54703e9753d6SMatthew G. Knepley } else { 54713e9753d6SMatthew G. Knepley PetscInt f; 54723e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 54733e9753d6SMatthew G. Knepley if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);} 54743e9753d6SMatthew G. Knepley } 54753e9753d6SMatthew G. Knepley } 54763e9753d6SMatthew G. Knepley 54773e9753d6SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 54783e9753d6SMatthew G. Knepley PetscFE feI; 54793e9753d6SMatthew G. Knepley PetscFEGeom *geom = affineGeom ? affineGeom : geoms[fieldI]; 54803e9753d6SMatthew G. Knepley PetscFEGeom *chunkGeom = NULL, *remGeom = NULL; 54813e9753d6SMatthew G. Knepley PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI]; 54823e9753d6SMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb; 54833e9753d6SMatthew G. Knepley 54843e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &feI);CHKERRQ(ierr); 54853e9753d6SMatthew G. Knepley if (!feI) continue; 54863e9753d6SMatthew G. Knepley ierr = PetscFEGetTileSizes(feI, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 54873e9753d6SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 54883e9753d6SMatthew G. Knepley ierr = PetscFEGetDimension(feI, &Nb);CHKERRQ(ierr); 54893e9753d6SMatthew G. Knepley blockSize = Nb; 54903e9753d6SMatthew G. Knepley batchSize = numBlocks * blockSize; 54913e9753d6SMatthew G. Knepley ierr = PetscFESetTileSizes(feI, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 54923e9753d6SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 54933e9753d6SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 54943e9753d6SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 54953e9753d6SMatthew G. Knepley offset = numCells - Nr; 54963e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 54973e9753d6SMatthew G. Knepley ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr); 54983e9753d6SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 54993e9753d6SMatthew G. Knepley PetscFE feJ; 55003e9753d6SMatthew G. Knepley 55013e9753d6SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &feJ);CHKERRQ(ierr); 55023e9753d6SMatthew G. Knepley if (!feJ) continue; 55033e9753d6SMatthew G. Knepley if (hasBdJac) { 55043e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr); 55053e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 55063e9753d6SMatthew G. Knepley } 55073e9753d6SMatthew G. Knepley if (hasBdPrec) { 55083e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr); 55093e9753d6SMatthew G. Knepley ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr); 55103e9753d6SMatthew G. Knepley } 55113e9753d6SMatthew G. Knepley } 55123e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr); 55133e9753d6SMatthew G. Knepley ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr); 55143e9753d6SMatthew G. Knepley } 55153e9753d6SMatthew G. Knepley /* Insert values into matrix */ 55163e9753d6SMatthew G. Knepley for (c = cS; c < cE; ++c) { 55173e9753d6SMatthew G. Knepley const PetscInt cell = cells ? cells[c] : c; 55183e9753d6SMatthew G. Knepley const PetscInt cind = c - cS; 55193e9753d6SMatthew G. Knepley 55203e9753d6SMatthew G. Knepley if (hasBdPrec) { 55213e9753d6SMatthew G. Knepley if (hasBdJac) { 55223e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 55233e9753d6SMatthew G. Knepley if (!isMatIS) { 55243e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55253e9753d6SMatthew G. Knepley } else { 55263e9753d6SMatthew G. Knepley Mat lJ; 55273e9753d6SMatthew G. Knepley 55283e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr); 55293e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55303e9753d6SMatthew G. Knepley } 55313e9753d6SMatthew G. Knepley } 55323e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);} 55333e9753d6SMatthew G. Knepley if (!isMatISP) { 55343e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55353e9753d6SMatthew G. Knepley } else { 55363e9753d6SMatthew G. Knepley Mat lJ; 55373e9753d6SMatthew G. Knepley 55383e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 55393e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55403e9753d6SMatthew G. Knepley } 55413e9753d6SMatthew G. Knepley } else if (hasBdJac) { 55423e9753d6SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);} 55433e9753d6SMatthew G. Knepley if (!isMatISP) { 55443e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55453e9753d6SMatthew G. Knepley } else { 55463e9753d6SMatthew G. Knepley Mat lJ; 55473e9753d6SMatthew G. Knepley 55483e9753d6SMatthew G. Knepley ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr); 55493e9753d6SMatthew G. Knepley ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 55503e9753d6SMatthew G. Knepley } 55513e9753d6SMatthew G. Knepley } 55523e9753d6SMatthew G. Knepley } 55533e9753d6SMatthew G. Knepley } 55543e9753d6SMatthew G. Knepley ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr); 55553e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, hasBdJac ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr); 55563e9753d6SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr); 55573e9753d6SMatthew G. Knepley ierr = PetscFree(faces);CHKERRQ(ierr); 55583e9753d6SMatthew G. Knepley ierr = ISDestroy(&chunkIS);CHKERRQ(ierr); 55593e9753d6SMatthew G. Knepley ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr); 55603e9753d6SMatthew G. Knepley if (maxDegree <= 1) { 55613e9753d6SMatthew G. Knepley ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr); 55623e9753d6SMatthew G. Knepley ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr); 55633e9753d6SMatthew G. Knepley } else { 55643e9753d6SMatthew G. Knepley PetscInt f; 55653e9753d6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 55663e9753d6SMatthew G. Knepley if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);CHKERRQ(ierr);} 55673e9753d6SMatthew G. Knepley if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);} 55683e9753d6SMatthew G. Knepley } 55693e9753d6SMatthew G. Knepley ierr = PetscFree2(quads,geoms);CHKERRQ(ierr); 55703e9753d6SMatthew G. Knepley } 55713e9753d6SMatthew G. Knepley if (dmAux) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);} 55723e9753d6SMatthew G. Knepley ierr = DMDestroy(&plex);CHKERRQ(ierr); 55733e9753d6SMatthew G. Knepley /* Assemble matrix */ 55743e9753d6SMatthew G. Knepley if (hasBdJac && hasBdPrec) { 55753e9753d6SMatthew G. Knepley ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 55763e9753d6SMatthew G. Knepley ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 55773e9753d6SMatthew G. Knepley } 55783e9753d6SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 55793e9753d6SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 55803e9753d6SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 55813e9753d6SMatthew G. Knepley PetscFunctionReturn(0); 55823e9753d6SMatthew G. Knepley } 5583