1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2cb1e1211SMatthew G Knepley 30f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h> 415496722SMatthew G. Knepley #include <petsc-private/petscfvimpl.h> 5a0845e3aSMatthew G. Knepley 6cb1e1211SMatthew G Knepley #undef __FUNCT__ 7cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale" 8cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 9cb1e1211SMatthew G Knepley { 10cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 11cb1e1211SMatthew G Knepley 12cb1e1211SMatthew G Knepley PetscFunctionBegin; 13cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 15cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 16cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 17cb1e1211SMatthew G Knepley } 18cb1e1211SMatthew G Knepley 19cb1e1211SMatthew G Knepley #undef __FUNCT__ 20cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale" 21cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 22cb1e1211SMatthew G Knepley { 23cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 24cb1e1211SMatthew G Knepley 25cb1e1211SMatthew G Knepley PetscFunctionBegin; 26cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 27cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 28cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 29cb1e1211SMatthew G Knepley } 30cb1e1211SMatthew G Knepley 31cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k) 32cb1e1211SMatthew G Knepley { 33cb1e1211SMatthew G Knepley switch (i) { 34cb1e1211SMatthew G Knepley case 0: 35cb1e1211SMatthew G Knepley switch (j) { 36cb1e1211SMatthew G Knepley case 0: return 0; 37cb1e1211SMatthew G Knepley case 1: 38cb1e1211SMatthew G Knepley switch (k) { 39cb1e1211SMatthew G Knepley case 0: return 0; 40cb1e1211SMatthew G Knepley case 1: return 0; 41cb1e1211SMatthew G Knepley case 2: return 1; 42cb1e1211SMatthew G Knepley } 43cb1e1211SMatthew G Knepley case 2: 44cb1e1211SMatthew G Knepley switch (k) { 45cb1e1211SMatthew G Knepley case 0: return 0; 46cb1e1211SMatthew G Knepley case 1: return -1; 47cb1e1211SMatthew G Knepley case 2: return 0; 48cb1e1211SMatthew G Knepley } 49cb1e1211SMatthew G Knepley } 50cb1e1211SMatthew G Knepley case 1: 51cb1e1211SMatthew G Knepley switch (j) { 52cb1e1211SMatthew G Knepley case 0: 53cb1e1211SMatthew G Knepley switch (k) { 54cb1e1211SMatthew G Knepley case 0: return 0; 55cb1e1211SMatthew G Knepley case 1: return 0; 56cb1e1211SMatthew G Knepley case 2: return -1; 57cb1e1211SMatthew G Knepley } 58cb1e1211SMatthew G Knepley case 1: return 0; 59cb1e1211SMatthew G Knepley case 2: 60cb1e1211SMatthew G Knepley switch (k) { 61cb1e1211SMatthew G Knepley case 0: return 1; 62cb1e1211SMatthew G Knepley case 1: return 0; 63cb1e1211SMatthew G Knepley case 2: return 0; 64cb1e1211SMatthew G Knepley } 65cb1e1211SMatthew G Knepley } 66cb1e1211SMatthew G Knepley case 2: 67cb1e1211SMatthew G Knepley switch (j) { 68cb1e1211SMatthew G Knepley case 0: 69cb1e1211SMatthew G Knepley switch (k) { 70cb1e1211SMatthew G Knepley case 0: return 0; 71cb1e1211SMatthew G Knepley case 1: return 1; 72cb1e1211SMatthew G Knepley case 2: return 0; 73cb1e1211SMatthew G Knepley } 74cb1e1211SMatthew G Knepley case 1: 75cb1e1211SMatthew G Knepley switch (k) { 76cb1e1211SMatthew G Knepley case 0: return -1; 77cb1e1211SMatthew G Knepley case 1: return 0; 78cb1e1211SMatthew G Knepley case 2: return 0; 79cb1e1211SMatthew G Knepley } 80cb1e1211SMatthew G Knepley case 2: return 0; 81cb1e1211SMatthew G Knepley } 82cb1e1211SMatthew G Knepley } 83cb1e1211SMatthew G Knepley return 0; 84cb1e1211SMatthew G Knepley } 85cb1e1211SMatthew G Knepley 86cb1e1211SMatthew G Knepley #undef __FUNCT__ 87cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 88cb1e1211SMatthew G Knepley /*@C 89cb1e1211SMatthew G Knepley DMPlexCreateRigidBody - create rigid body modes from coordinates 90cb1e1211SMatthew G Knepley 91cb1e1211SMatthew G Knepley Collective on DM 92cb1e1211SMatthew G Knepley 93cb1e1211SMatthew G Knepley Input Arguments: 94cb1e1211SMatthew G Knepley + dm - the DM 95cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section 96cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section 97cb1e1211SMatthew G Knepley 98cb1e1211SMatthew G Knepley Output Argument: 99cb1e1211SMatthew G Knepley . sp - the null space 100cb1e1211SMatthew G Knepley 101cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 102cb1e1211SMatthew G Knepley 103cb1e1211SMatthew G Knepley Level: advanced 104cb1e1211SMatthew G Knepley 105cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 106cb1e1211SMatthew G Knepley @*/ 107cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp) 108cb1e1211SMatthew G Knepley { 109cb1e1211SMatthew G Knepley MPI_Comm comm; 110cb1e1211SMatthew G Knepley Vec coordinates, localMode, mode[6]; 111cb1e1211SMatthew G Knepley PetscSection coordSection; 112cb1e1211SMatthew G Knepley PetscScalar *coords; 113cb1e1211SMatthew G Knepley PetscInt dim, vStart, vEnd, v, n, m, d, i, j; 114cb1e1211SMatthew G Knepley PetscErrorCode ierr; 115cb1e1211SMatthew G Knepley 116cb1e1211SMatthew G Knepley PetscFunctionBegin; 117cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 118c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 119cb1e1211SMatthew G Knepley if (dim == 1) { 120cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 121cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 122cb1e1211SMatthew G Knepley } 123cb1e1211SMatthew G Knepley if (!section) {ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr);} 124cb1e1211SMatthew G Knepley if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);} 125cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 126cb1e1211SMatthew G Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 12769d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 128cb1e1211SMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 129cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 130cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 131cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 132cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 133cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 134cb1e1211SMatthew G Knepley /* Assume P1 */ 135cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr); 136cb1e1211SMatthew G Knepley for (d = 0; d < dim; ++d) { 137cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 138cb1e1211SMatthew G Knepley 139cb1e1211SMatthew G Knepley values[d] = 1.0; 140cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 141cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 142cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 143cb1e1211SMatthew G Knepley } 144cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 145cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 146cb1e1211SMatthew G Knepley } 147cb1e1211SMatthew G Knepley ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 148cb1e1211SMatthew G Knepley for (d = dim; d < dim*(dim+1)/2; ++d) { 149cb1e1211SMatthew G Knepley PetscInt i, j, k = dim > 2 ? d - dim : d; 150cb1e1211SMatthew G Knepley 151cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 152cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 153cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 154cb1e1211SMatthew G Knepley PetscInt off; 155cb1e1211SMatthew G Knepley 156cb1e1211SMatthew G Knepley ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 157cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) { 158cb1e1211SMatthew G Knepley for (j = 0; j < dim; ++j) { 159cb1e1211SMatthew G Knepley values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]); 160cb1e1211SMatthew G Knepley } 161cb1e1211SMatthew G Knepley } 162cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 163cb1e1211SMatthew G Knepley } 164cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 165cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 166cb1e1211SMatthew G Knepley } 167cb1e1211SMatthew G Knepley ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 168cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr); 169cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 170cb1e1211SMatthew G Knepley /* Orthonormalize system */ 171cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 172cb1e1211SMatthew G Knepley PetscScalar dots[6]; 173cb1e1211SMatthew G Knepley 174cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 175cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 176cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 177cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 178cb1e1211SMatthew G Knepley } 179cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 180cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 181cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 182cb1e1211SMatthew G Knepley } 183cb1e1211SMatthew G Knepley 184cb1e1211SMatthew G Knepley #undef __FUNCT__ 185a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal" 186bf3434eeSMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 187a18a7fb9SMatthew G. Knepley { 188bf3434eeSMatthew G. Knepley PetscFE fe; 189a18a7fb9SMatthew G. Knepley PetscDualSpace *sp; 190a18a7fb9SMatthew G. Knepley PetscSection section; 191a18a7fb9SMatthew G. Knepley PetscScalar *values; 192ad96f515SMatthew G. Knepley PetscBool *fieldActive; 193*7a1a1bd4SToby Isaac PetscInt numFields, numComp, dim, dimEmbed, spDim, totDim = 0, numValues, cStart, cEnd, f, d, v, i, comp; 194a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 195a18a7fb9SMatthew G. Knepley 196a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 19756ad4130SToby Isaac ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 19856ad4130SToby Isaac if (cEnd <= cStart) PetscFunctionReturn(0); 199c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 200*7a1a1bd4SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 201a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 202a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 203e1d0b1adSMatthew G. Knepley ierr = PetscMalloc1(numFields,&sp);CHKERRQ(ierr); 204a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 205bf3434eeSMatthew G. Knepley ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr); 206bf3434eeSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 207bf3434eeSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 208a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 209a18a7fb9SMatthew G. Knepley totDim += spDim*numComp; 210a18a7fb9SMatthew G. Knepley } 211a18a7fb9SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 212a18a7fb9SMatthew G. Knepley if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim); 213a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 214ad96f515SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 215ad96f515SMatthew G. Knepley for (f = 0; f < numFields; ++f) fieldActive[f] = funcs[f] ? PETSC_TRUE : PETSC_FALSE; 216a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 217a18a7fb9SMatthew G. Knepley IS pointIS; 218a18a7fb9SMatthew G. Knepley const PetscInt *points; 219a18a7fb9SMatthew G. Knepley PetscInt n, p; 220a18a7fb9SMatthew G. Knepley 221a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 222a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 223a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 224a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 225a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 226e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 227a18a7fb9SMatthew G. Knepley 228a18a7fb9SMatthew G. Knepley if ((point < cStart) || (point >= cEnd)) continue; 229e1d0b1adSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 230*7a1a1bd4SToby Isaac geom.dim = dim; 231*7a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 232a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 233a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 234bf3434eeSMatthew G. Knepley 235bf3434eeSMatthew G. Knepley ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr); 236bf3434eeSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 237a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 238a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 239a18a7fb9SMatthew G. Knepley if (funcs[f]) { 240e1d0b1adSMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr); 241a18a7fb9SMatthew G. Knepley } else { 242a18a7fb9SMatthew G. Knepley for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0; 243a18a7fb9SMatthew G. Knepley } 244a18a7fb9SMatthew G. Knepley v += numComp; 245a18a7fb9SMatthew G. Knepley } 246a18a7fb9SMatthew G. Knepley } 247ad96f515SMatthew G. Knepley ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr); 248a18a7fb9SMatthew G. Knepley } 249a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 250a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 251a18a7fb9SMatthew G. Knepley } 252a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 253ad96f515SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 254e1d0b1adSMatthew G. Knepley ierr = PetscFree(sp);CHKERRQ(ierr); 255a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 256a18a7fb9SMatthew G. Knepley } 257a18a7fb9SMatthew G. Knepley 258a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 259cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 2600f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 261cb1e1211SMatthew G Knepley { 26272f94c41SMatthew G. Knepley PetscDualSpace *sp; 26315496722SMatthew G. Knepley PetscInt *numComp; 26472f94c41SMatthew G. Knepley PetscSection section; 26572f94c41SMatthew G. Knepley PetscScalar *values; 266*7a1a1bd4SToby Isaac PetscInt numFields, dim, dimEmbed, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp; 267cb1e1211SMatthew G Knepley PetscErrorCode ierr; 268cb1e1211SMatthew G Knepley 269cb1e1211SMatthew G Knepley PetscFunctionBegin; 27056ad4130SToby Isaac ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 27156ad4130SToby Isaac if (cEnd <= cStart) PetscFunctionReturn(0); 272cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 27372f94c41SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 27415496722SMatthew G. Knepley ierr = PetscMalloc2(numFields, &sp, numFields, &numComp);CHKERRQ(ierr); 27572f94c41SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 27615496722SMatthew G. Knepley PetscObject obj; 27715496722SMatthew G. Knepley PetscClassId id; 2780f2d7e86SMatthew G. Knepley 27915496722SMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 28015496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 28115496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 28215496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 28315496722SMatthew G. Knepley 28415496722SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 2850f2d7e86SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 28615496722SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 28715496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 28815496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 28915496722SMatthew G. Knepley PetscQuadrature q; 29015496722SMatthew G. Knepley 29115496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 29215496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &q);CHKERRQ(ierr); 29315496722SMatthew G. Knepley ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject) fv), &sp[f]);CHKERRQ(ierr); 29415496722SMatthew G. Knepley ierr = PetscDualSpaceSetDM(sp[f], dm);CHKERRQ(ierr); 29515496722SMatthew G. Knepley ierr = PetscDualSpaceSetType(sp[f], PETSCDUALSPACESIMPLE);CHKERRQ(ierr); 29615496722SMatthew G. Knepley ierr = PetscDualSpaceSimpleSetDimension(sp[f], 1);CHKERRQ(ierr); 29715496722SMatthew G. Knepley ierr = PetscDualSpaceSimpleSetFunctional(sp[f], 0, q);CHKERRQ(ierr); 29815496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 29972f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 30015496722SMatthew G. Knepley totDim += spDim*numComp[f]; 301cb1e1211SMatthew G Knepley } 302c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 303*7a1a1bd4SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 30472f94c41SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 30572f94c41SMatthew G. Knepley if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim); 30672f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 30772f94c41SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 308e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 309cb1e1211SMatthew G Knepley 310e1d0b1adSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 311*7a1a1bd4SToby Isaac geom.dim = dim; 312*7a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 31372f94c41SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 314c110b1eeSGeoffrey Irving void *const ctx = ctxs ? ctxs[f] : NULL; 3150f2d7e86SMatthew G. Knepley 31672f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 31772f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 318120386c5SMatthew G. Knepley if (funcs[f]) { 319e1d0b1adSMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 320120386c5SMatthew G. Knepley } else { 32115496722SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 322120386c5SMatthew G. Knepley } 32315496722SMatthew G. Knepley v += numComp[f]; 324cb1e1211SMatthew G Knepley } 325cb1e1211SMatthew G Knepley } 32672f94c41SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 327cb1e1211SMatthew G Knepley } 32872f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 32915496722SMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 33015496722SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 331cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 332cb1e1211SMatthew G Knepley } 333cb1e1211SMatthew G Knepley 334cb1e1211SMatthew G Knepley #undef __FUNCT__ 3350f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal" 3363bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX) 3370f2d7e86SMatthew G. Knepley { 3380f2d7e86SMatthew G. Knepley DM dmAux; 3392764a2aaSMatthew G. Knepley PetscDS prob, probAux; 3400f2d7e86SMatthew G. Knepley Vec A; 341326413afSMatthew G. Knepley PetscSection section, sectionAux; 342326413afSMatthew G. Knepley PetscScalar *values, *u, *u_x, *a, *a_x; 3430f2d7e86SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux; 344326413afSMatthew G. Knepley PetscInt Nf, dim, spDim, totDim, numValues, cStart, cEnd, c, f, d, v, comp; 3450f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 3460f2d7e86SMatthew G. Knepley 3470f2d7e86SMatthew G. Knepley PetscFunctionBegin; 3482764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 349c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 3500f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 3510f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 3520f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 3532764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 3542764a2aaSMatthew G. Knepley ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr); 3552764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr); 3562764a2aaSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 3570f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 3580f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 3590f2d7e86SMatthew G. Knepley if (dmAux) { 3602764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 361326413afSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 3622764a2aaSMatthew G. Knepley ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr); 3632764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr); 3640f2d7e86SMatthew G. Knepley } 365d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 3660f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 3670f2d7e86SMatthew G. Knepley if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim); 3680f2d7e86SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 3690f2d7e86SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 3700f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 371326413afSMatthew G. Knepley PetscScalar *coefficients = NULL, *coefficientsAux = NULL; 372326413afSMatthew G. Knepley 3738e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 374326413afSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 375326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 3760f2d7e86SMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 3773113607cSMatthew G. Knepley PetscFE fe; 3783113607cSMatthew G. Knepley PetscDualSpace sp; 3793113607cSMatthew G. Knepley PetscInt Ncf; 3803113607cSMatthew G. Knepley 3812764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 3823113607cSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr); 3833113607cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr); 3843113607cSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr); 3850f2d7e86SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 3860f2d7e86SMatthew G. Knepley PetscQuadrature quad; 3870f2d7e86SMatthew G. Knepley const PetscReal *points, *weights; 3880f2d7e86SMatthew G. Knepley PetscInt numPoints, q; 3890f2d7e86SMatthew G. Knepley 3900f2d7e86SMatthew G. Knepley if (funcs[f]) { 3913113607cSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr); 3920f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr); 3933113607cSMatthew G. Knepley ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr); 3940f2d7e86SMatthew G. Knepley for (q = 0; q < numPoints; ++q) { 3950f2d7e86SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x); 396326413afSMatthew G. Knepley ierr = EvaluateFieldJets(prob, PETSC_FALSE, q, invJ, coefficients, NULL, u, u_x, NULL);CHKERRQ(ierr); 397326413afSMatthew G. Knepley ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr); 3983bc3b0a0SMatthew G. Knepley (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]); 3990f2d7e86SMatthew G. Knepley } 4003113607cSMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr); 4010f2d7e86SMatthew G. Knepley } else { 4020f2d7e86SMatthew G. Knepley for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0; 4030f2d7e86SMatthew G. Knepley } 4040f2d7e86SMatthew G. Knepley v += Ncf; 4050f2d7e86SMatthew G. Knepley } 4060f2d7e86SMatthew G. Knepley } 407326413afSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 408326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 4090f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 4100f2d7e86SMatthew G. Knepley } 4110f2d7e86SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4120f2d7e86SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 4130f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 4140f2d7e86SMatthew G. Knepley } 4150f2d7e86SMatthew G. Knepley 4160f2d7e86SMatthew G. Knepley #undef __FUNCT__ 417d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal" 418d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], void (*func)(const PetscReal[], PetscScalar *, void *), void *ctx, Vec locX) 41955f2e967SMatthew G. Knepley { 42055f2e967SMatthew G. Knepley void (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx); 42155f2e967SMatthew G. Knepley void **ctxs; 422d7ddef95SMatthew G. Knepley PetscInt numFields; 423d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 424d7ddef95SMatthew G. Knepley 425d7ddef95SMatthew G. Knepley PetscFunctionBegin; 426d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 427d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 428d7ddef95SMatthew G. Knepley funcs[field] = func; 429d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 430d7ddef95SMatthew G. Knepley ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 431d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 432d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 433d7ddef95SMatthew G. Knepley } 434d7ddef95SMatthew G. Knepley 435d7ddef95SMatthew G. Knepley #undef __FUNCT__ 436d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal" 437d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, 438d7ddef95SMatthew G. Knepley PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX) 439d7ddef95SMatthew G. Knepley { 440d7ddef95SMatthew G. Knepley PetscFV fv; 441d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 442d7ddef95SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *grad; 443d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 444d7ddef95SMatthew G. Knepley PetscInt dim, fStart, fEnd, pdim, i; 445d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 446d7ddef95SMatthew G. Knepley 447d7ddef95SMatthew G. Knepley PetscFunctionBegin; 448d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 449d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 450d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fv);CHKERRQ(ierr); 451d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 452d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 453d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 454d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 455d7ddef95SMatthew G. Knepley if (Grad) { 456d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 457d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 458d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 459d7ddef95SMatthew G. Knepley ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 460d7ddef95SMatthew G. Knepley } 461d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 462d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 463d7ddef95SMatthew G. Knepley IS faceIS; 464d7ddef95SMatthew G. Knepley const PetscInt *faces; 465d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 466d7ddef95SMatthew G. Knepley 467d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 468d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 469d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 470d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 471d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 472d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 473d7ddef95SMatthew G. Knepley const PetscFVFaceGeom *fg; 474d7ddef95SMatthew G. Knepley 475d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 476d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 477d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 478d7ddef95SMatthew G. Knepley if (Grad) { 479d7ddef95SMatthew G. Knepley const PetscFVCellGeom *cg; 480d7ddef95SMatthew G. Knepley const PetscScalar *cx, *cgrad; 481d7ddef95SMatthew G. Knepley PetscScalar *xG; 482d7ddef95SMatthew G. Knepley PetscReal dx[3]; 483d7ddef95SMatthew G. Knepley PetscInt d; 484d7ddef95SMatthew G. Knepley 485d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 486d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 487d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 488d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRef(dm, cells[1], x, &xG);CHKERRQ(ierr); 489d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 490d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 491d7ddef95SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr); 492d7ddef95SMatthew G. Knepley } else { 493d7ddef95SMatthew G. Knepley const PetscScalar *xI; 494d7ddef95SMatthew G. Knepley PetscScalar *xG; 495d7ddef95SMatthew G. Knepley 496d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 497d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRef(dm, cells[1], x, &xG);CHKERRQ(ierr); 498d7ddef95SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr); 499d7ddef95SMatthew G. Knepley } 500d7ddef95SMatthew G. Knepley } 501d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 502d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 503d7ddef95SMatthew G. Knepley } 504d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 505d7ddef95SMatthew G. Knepley if (Grad) { 506d7ddef95SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 507d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 508d7ddef95SMatthew G. Knepley } 509d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 510d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 511d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 512d7ddef95SMatthew G. Knepley } 513d7ddef95SMatthew G. Knepley 514d7ddef95SMatthew G. Knepley #undef __FUNCT__ 515d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues" 516d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 517d7ddef95SMatthew G. Knepley { 518d7ddef95SMatthew G. Knepley PetscInt numBd, b; 51955f2e967SMatthew G. Knepley PetscErrorCode ierr; 52055f2e967SMatthew G. Knepley 52155f2e967SMatthew G. Knepley PetscFunctionBegin; 52255f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 523d7ddef95SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 524d7ddef95SMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 525d7ddef95SMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 526d7ddef95SMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 52755f2e967SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 52855f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 52955f2e967SMatthew G. Knepley PetscBool isEssential; 530d7ddef95SMatthew G. Knepley const char *labelname; 531d7ddef95SMatthew G. Knepley DMLabel label; 532d7ddef95SMatthew G. Knepley PetscInt field; 533d7ddef95SMatthew G. Knepley PetscObject obj; 534d7ddef95SMatthew G. Knepley PetscClassId id; 53555f2e967SMatthew G. Knepley void (*func)(); 536d7ddef95SMatthew G. Knepley PetscInt numids; 537d7ddef95SMatthew G. Knepley const PetscInt *ids; 53855f2e967SMatthew G. Knepley void *ctx; 53955f2e967SMatthew G. Knepley 54063d5297fSMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 541d7ddef95SMatthew G. Knepley if (!isEssential) continue; 54263d5297fSMatthew G. Knepley ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr); 543d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 544d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 545d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 546d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (void (*)(const PetscReal[], PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr); 547d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 548d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, 549d7ddef95SMatthew G. Knepley field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 550d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 55155f2e967SMatthew G. Knepley } 55255f2e967SMatthew G. Knepley PetscFunctionReturn(0); 55355f2e967SMatthew G. Knepley } 55455f2e967SMatthew G. Knepley 555cb1e1211SMatthew G Knepley #undef __FUNCT__ 556cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 557cb1e1211SMatthew G Knepley /*@C 558cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 559cb1e1211SMatthew G Knepley 560cb1e1211SMatthew G Knepley Input Parameters: 561cb1e1211SMatthew G Knepley + dm - The DM 562cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 56351259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 564cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 565cb1e1211SMatthew G Knepley 566cb1e1211SMatthew G Knepley Output Parameter: 567cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 568cb1e1211SMatthew G Knepley 569cb1e1211SMatthew G Knepley Level: developer 570cb1e1211SMatthew G Knepley 57115496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 572878cb397SSatish Balay @*/ 5730f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 574cb1e1211SMatthew G Knepley { 575cb1e1211SMatthew G Knepley const PetscInt debug = 0; 576cb1e1211SMatthew G Knepley PetscSection section; 577c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 578cb1e1211SMatthew G Knepley Vec localX; 57915496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 580cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 581cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 58215496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 58315496722SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, c, field, fieldOffset; 584cb1e1211SMatthew G Knepley PetscErrorCode ierr; 585cb1e1211SMatthew G Knepley 586cb1e1211SMatthew G Knepley PetscFunctionBegin; 587c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 588cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 589cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 590cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 591cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 592cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 593cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 59415496722SMatthew G. Knepley PetscObject obj; 59515496722SMatthew G. Knepley PetscClassId id; 596c5bbbd5bSMatthew G. Knepley PetscInt Nc; 597c5bbbd5bSMatthew G. Knepley 59815496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 59915496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 60015496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 60115496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 60215496722SMatthew G. Knepley 6030f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 6040f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 60515496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 60615496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 60715496722SMatthew G. Knepley 60815496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 60915496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 61015496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 611c5bbbd5bSMatthew G. Knepley numComponents += Nc; 612cb1e1211SMatthew G Knepley } 61315496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 6140f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 61515496722SMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 616cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 617cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 618a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 619cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 620cb1e1211SMatthew G Knepley 6218e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 622cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 623cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 624cb1e1211SMatthew G Knepley 62515496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 62615496722SMatthew G. Knepley PetscObject obj; 62715496722SMatthew G. Knepley PetscClassId id; 628c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 62915496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 630cb1e1211SMatthew G Knepley 63115496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 63215496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 63315496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 63415496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 63515496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 636cb1e1211SMatthew G Knepley if (debug) { 637cb1e1211SMatthew G Knepley char title[1024]; 638cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 63915496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 640cb1e1211SMatthew G Knepley } 641cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 64215496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 643c110b1eeSGeoffrey Irving (*funcs[field])(coords, funcVal, ctx); 64415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 64515496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 64615496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 64715496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 64815496722SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 64915496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 650cb1e1211SMatthew G Knepley } 651cb1e1211SMatthew G Knepley } 65215496722SMatthew G. Knepley fieldOffset += Nb*Nc; 653cb1e1211SMatthew G Knepley } 654cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 655cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 656cb1e1211SMatthew G Knepley localDiff += elemDiff; 657cb1e1211SMatthew G Knepley } 65815496722SMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 659cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 66086a74ee0SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 661cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 662cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 663cb1e1211SMatthew G Knepley } 664cb1e1211SMatthew G Knepley 665cb1e1211SMatthew G Knepley #undef __FUNCT__ 66640e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 66740e14135SMatthew G. Knepley /*@C 66840e14135SMatthew G. Knepley DMPlexComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h. 66940e14135SMatthew G. Knepley 67040e14135SMatthew G. Knepley Input Parameters: 67140e14135SMatthew G. Knepley + dm - The DM 67240e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 67351259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 67440e14135SMatthew G. Knepley . X - The coefficient vector u_h 67540e14135SMatthew G. Knepley - n - The vector to project along 67640e14135SMatthew G. Knepley 67740e14135SMatthew G. Knepley Output Parameter: 67840e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 67940e14135SMatthew G. Knepley 68040e14135SMatthew G. Knepley Level: developer 68140e14135SMatthew G. Knepley 68240e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 68340e14135SMatthew G. Knepley @*/ 6840f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 685cb1e1211SMatthew G Knepley { 68640e14135SMatthew G. Knepley const PetscInt debug = 0; 687cb1e1211SMatthew G Knepley PetscSection section; 68840e14135SMatthew G. Knepley PetscQuadrature quad; 68940e14135SMatthew G. Knepley Vec localX; 69040e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 69140e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 69240e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 69340e14135SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 694cb1e1211SMatthew G Knepley PetscErrorCode ierr; 695cb1e1211SMatthew G Knepley 696cb1e1211SMatthew G Knepley PetscFunctionBegin; 697c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 69840e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 69940e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 70040e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 70140e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 70240e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 703652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 7040f2d7e86SMatthew G. Knepley PetscFE fe; 70540e14135SMatthew G. Knepley PetscInt Nc; 706652b88e8SMatthew G. Knepley 7070f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7080f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7090f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 71040e14135SMatthew G. Knepley numComponents += Nc; 711652b88e8SMatthew G. Knepley } 71240e14135SMatthew G. Knepley /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 71340e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 71440e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 71540e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 71640e14135SMatthew G. Knepley PetscScalar *x = NULL; 71740e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 718652b88e8SMatthew G. Knepley 7198e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 72040e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 72140e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 72240e14135SMatthew G. Knepley 72340e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 7240f2d7e86SMatthew G. Knepley PetscFE fe; 72551259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 72621454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 72740e14135SMatthew G. Knepley PetscReal *basisDer; 72821454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 72940e14135SMatthew G. Knepley 7300f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 73121454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 7320f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 7330f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr); 7340f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 73540e14135SMatthew G. Knepley if (debug) { 73640e14135SMatthew G. Knepley char title[1024]; 73740e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 73840e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 739652b88e8SMatthew G. Knepley } 74040e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 74140e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 74240e14135SMatthew G. Knepley coords[d] = v0[d]; 74340e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 74440e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 745652b88e8SMatthew G. Knepley } 74640e14135SMatthew G. Knepley } 74751259fa3SMatthew G. Knepley (*funcs[field])(coords, n, funcVal, ctx); 74840e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 74940e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 75040e14135SMatthew G. Knepley 75140e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 75240e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 75340e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 75440e14135SMatthew G. Knepley 75540e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 75640e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 75740e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 75840e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 75940e14135SMatthew G. Knepley } 76040e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 76140e14135SMatthew G. Knepley } 76240e14135SMatthew G. Knepley } 76340e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 76440e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 76540e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 76640e14135SMatthew G. Knepley } 76740e14135SMatthew G. Knepley } 76840e14135SMatthew G. Knepley comp += Ncomp; 76940e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 77040e14135SMatthew G. Knepley } 77140e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 77240e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 77340e14135SMatthew G. Knepley localDiff += elemDiff; 77440e14135SMatthew G. Knepley } 77540e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 77640e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 77740e14135SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 77840e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 779cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 780cb1e1211SMatthew G Knepley } 781cb1e1211SMatthew G Knepley 782a0845e3aSMatthew G. Knepley #undef __FUNCT__ 78373d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff" 78415496722SMatthew G. Knepley /*@C 78515496722SMatthew G. Knepley DMPlexComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components. 78615496722SMatthew G. Knepley 78715496722SMatthew G. Knepley Input Parameters: 78815496722SMatthew G. Knepley + dm - The DM 78915496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component 79015496722SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 79115496722SMatthew G. Knepley - X - The coefficient vector u_h 79215496722SMatthew G. Knepley 79315496722SMatthew G. Knepley Output Parameter: 79415496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2 79515496722SMatthew G. Knepley 79615496722SMatthew G. Knepley Level: developer 79715496722SMatthew G. Knepley 79815496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff() 79915496722SMatthew G. Knepley @*/ 8000f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 80173d901b8SMatthew G. Knepley { 80273d901b8SMatthew G. Knepley const PetscInt debug = 0; 80373d901b8SMatthew G. Knepley PetscSection section; 80473d901b8SMatthew G. Knepley PetscQuadrature quad; 80573d901b8SMatthew G. Knepley Vec localX; 80615496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 80773d901b8SMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 80873d901b8SMatthew G. Knepley PetscReal *localDiff; 80915496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 81015496722SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, c, field, fieldOffset; 81173d901b8SMatthew G. Knepley PetscErrorCode ierr; 81273d901b8SMatthew G. Knepley 81373d901b8SMatthew G. Knepley PetscFunctionBegin; 814c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 81573d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 81673d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 81773d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 81873d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 81973d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 82073d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 82115496722SMatthew G. Knepley PetscObject obj; 82215496722SMatthew G. Knepley PetscClassId id; 82373d901b8SMatthew G. Knepley PetscInt Nc; 82473d901b8SMatthew G. Knepley 82515496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 82615496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 82715496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 82815496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 82915496722SMatthew G. Knepley 8300f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8310f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 83215496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 83315496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 83415496722SMatthew G. Knepley 83515496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 83615496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 83715496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 83873d901b8SMatthew G. Knepley numComponents += Nc; 83973d901b8SMatthew G. Knepley } 84015496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 8410f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 84215496722SMatthew G. Knepley ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 84373d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 84473d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 84573d901b8SMatthew G. Knepley PetscScalar *x = NULL; 84673d901b8SMatthew G. Knepley 8478e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 84873d901b8SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 84973d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 85073d901b8SMatthew G. Knepley 85115496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 85215496722SMatthew G. Knepley PetscObject obj; 85315496722SMatthew G. Knepley PetscClassId id; 85473d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 85515496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 85673d901b8SMatthew G. Knepley 85715496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 85815496722SMatthew G. Knepley 85915496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 86015496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 86115496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 86215496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 86315496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 86473d901b8SMatthew G. Knepley if (debug) { 86573d901b8SMatthew G. Knepley char title[1024]; 86673d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 86715496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 86873d901b8SMatthew G. Knepley } 86973d901b8SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 87015496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 87173d901b8SMatthew G. Knepley (*funcs[field])(coords, funcVal, ctx); 87215496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 87315496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 87415496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 87515496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 87615496722SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 87715496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 87873d901b8SMatthew G. Knepley } 87973d901b8SMatthew G. Knepley } 88015496722SMatthew G. Knepley fieldOffset += Nb*Nc; 88173d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 88273d901b8SMatthew G. Knepley } 88373d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 88473d901b8SMatthew G. Knepley } 88573d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 88673d901b8SMatthew G. Knepley ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 88773d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 88815496722SMatthew G. Knepley ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 88973d901b8SMatthew G. Knepley PetscFunctionReturn(0); 89073d901b8SMatthew G. Knepley } 89173d901b8SMatthew G. Knepley 89273d901b8SMatthew G. Knepley #undef __FUNCT__ 89373d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM" 89473d901b8SMatthew G. Knepley /*@ 89573d901b8SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user 89673d901b8SMatthew G. Knepley 89773d901b8SMatthew G. Knepley Input Parameters: 89873d901b8SMatthew G. Knepley + dm - The mesh 89973d901b8SMatthew G. Knepley . X - Local input vector 90073d901b8SMatthew G. Knepley - user - The user context 90173d901b8SMatthew G. Knepley 90273d901b8SMatthew G. Knepley Output Parameter: 90373d901b8SMatthew G. Knepley . integral - Local integral for each field 90473d901b8SMatthew G. Knepley 90573d901b8SMatthew G. Knepley Level: developer 90673d901b8SMatthew G. Knepley 90773d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 90873d901b8SMatthew G. Knepley @*/ 9090f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user) 91073d901b8SMatthew G. Knepley { 91173d901b8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 91273d901b8SMatthew G. Knepley DM dmAux; 91373d901b8SMatthew G. Knepley Vec localX, A; 9142764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 91573d901b8SMatthew G. Knepley PetscQuadrature q; 91673d901b8SMatthew G. Knepley PetscSection section, sectionAux; 917bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 91873d901b8SMatthew G. Knepley PetscScalar *u, *a = NULL; 9190f2d7e86SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 9200f2d7e86SMatthew G. Knepley PetscInt totDim, totDimAux; 92173d901b8SMatthew G. Knepley PetscErrorCode ierr; 92273d901b8SMatthew G. Knepley 92373d901b8SMatthew G. Knepley PetscFunctionBegin; 92473d901b8SMatthew G. Knepley /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/ 92573d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 92673d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 92773d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 928c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 92973d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 9302764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 9312764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 93273d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 93373d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 93473d901b8SMatthew G. Knepley numCells = cEnd - cStart; 9350f2d7e86SMatthew G. Knepley for (f = 0; f < Nf; ++f) {integral[f] = 0.0;} 93673d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 93773d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 93873d901b8SMatthew G. Knepley if (dmAux) { 93973d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 9402764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 9412764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 94273d901b8SMatthew G. Knepley } 943d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 944bbce034cSMatthew G. Knepley ierr = PetscMalloc2(numCells*totDim,&u,numCells,&cgeom);CHKERRQ(ierr); 9450f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 94673d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 94773d901b8SMatthew G. Knepley PetscScalar *x = NULL; 94873d901b8SMatthew G. Knepley PetscInt i; 94973d901b8SMatthew G. Knepley 950bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr); 951bbce034cSMatthew G. Knepley if (cgeom[c].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cgeom[c].detJ, c); 95273d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 9530f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 95473d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 95573d901b8SMatthew G. Knepley if (dmAux) { 95673d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 9570f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 95873d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 95973d901b8SMatthew G. Knepley } 96073d901b8SMatthew G. Knepley } 96173d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 9620f2d7e86SMatthew G. Knepley PetscFE fe; 96373d901b8SMatthew G. Knepley PetscInt numQuadPoints, Nb; 96473d901b8SMatthew G. Knepley /* Conforming batches */ 96573d901b8SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 96673d901b8SMatthew G. Knepley /* Remainder */ 96773d901b8SMatthew G. Knepley PetscInt Nr, offset; 96873d901b8SMatthew G. Knepley 9692764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 9700f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 9710f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 9720f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 97373d901b8SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 97473d901b8SMatthew G. Knepley blockSize = Nb*numQuadPoints; 97573d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 9760f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 97773d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 97873d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 97973d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 98073d901b8SMatthew G. Knepley offset = numCells - Nr; 981bbce034cSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, integral);CHKERRQ(ierr); 982bbce034cSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr); 98373d901b8SMatthew G. Knepley } 984bbce034cSMatthew G. Knepley ierr = PetscFree2(u,cgeom);CHKERRQ(ierr); 98573d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 98673d901b8SMatthew G. Knepley if (mesh->printFEM) { 98773d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr); 98873d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);} 98973d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 99073d901b8SMatthew G. Knepley } 99173d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 99273d901b8SMatthew G. Knepley /* TODO: Allreduce for integral */ 99373d901b8SMatthew G. Knepley /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/ 99473d901b8SMatthew G. Knepley PetscFunctionReturn(0); 99573d901b8SMatthew G. Knepley } 99673d901b8SMatthew G. Knepley 99773d901b8SMatthew G. Knepley #undef __FUNCT__ 998d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM" 999d69c5d34SMatthew G. Knepley /*@ 1000d69c5d34SMatthew G. Knepley DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1001d69c5d34SMatthew G. Knepley 1002d69c5d34SMatthew G. Knepley Input Parameters: 1003d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1004d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1005d69c5d34SMatthew G. Knepley - user - The user context 1006d69c5d34SMatthew G. Knepley 1007d69c5d34SMatthew G. Knepley Output Parameter: 1008934789fcSMatthew G. Knepley . In - The interpolation matrix 1009d69c5d34SMatthew G. Knepley 1010d69c5d34SMatthew G. Knepley Note: 1011d69c5d34SMatthew G. Knepley The first member of the user context must be an FEMContext. 1012d69c5d34SMatthew G. Knepley 1013d69c5d34SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1014d69c5d34SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 1015d69c5d34SMatthew G. Knepley 1016d69c5d34SMatthew G. Knepley Level: developer 1017d69c5d34SMatthew G. Knepley 1018d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM() 1019d69c5d34SMatthew G. Knepley @*/ 1020934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user) 1021d69c5d34SMatthew G. Knepley { 1022d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1023d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 10242764a2aaSMatthew G. Knepley PetscDS prob; 1025d69c5d34SMatthew G. Knepley PetscFE *feRef; 1026d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1027d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1028d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 1029942a7a06SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c; 10300f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1031d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1032d69c5d34SMatthew G. Knepley 1033d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1034d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1035c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1036d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1037d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1038d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1039d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1040d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1041d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 10422764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 1043d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr); 1044d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 10450f2d7e86SMatthew G. Knepley PetscFE fe; 10460f2d7e86SMatthew G. Knepley PetscInt rNb, Nc; 1047d69c5d34SMatthew G. Knepley 10482764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 10490f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1050d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 10510f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 10520f2d7e86SMatthew G. Knepley rTotDim += rNb*Nc; 1053d69c5d34SMatthew G. Knepley } 10542764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 10550f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 10560f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1057d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1058d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1059d69c5d34SMatthew G. Knepley PetscQuadrature f; 1060d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1061d69c5d34SMatthew G. Knepley PetscReal *points; 1062d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1063d69c5d34SMatthew G. Knepley 1064d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 1065d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 10660f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 1067d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1068d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1069d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1070d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1071d69c5d34SMatthew G. Knepley npoints += Np; 1072d69c5d34SMatthew G. Knepley } 1073d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1074d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1075d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1076d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1077d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1078d69c5d34SMatthew G. Knepley } 1079d69c5d34SMatthew G. Knepley 1080d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 10810f2d7e86SMatthew G. Knepley PetscFE fe; 1082d69c5d34SMatthew G. Knepley PetscReal *B; 108336a6d9c0SMatthew G. Knepley PetscInt NcJ, cpdim, j; 1084d69c5d34SMatthew G. Knepley 1085d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 10862764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr); 10870f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 10880f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1089ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1090ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 10917c927364SMatthew 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); 10920f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1093d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1094d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1095d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1096d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 109736a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 10980f2d7e86SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p]; 109936a6d9c0SMatthew G. Knepley } 1100d69c5d34SMatthew G. Knepley } 1101d69c5d34SMatthew G. Knepley } 11020f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1103ffe73a53SMatthew G. Knepley } 110436a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1105d69c5d34SMatthew G. Knepley } 1106d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1107549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1108d69c5d34SMatthew G. Knepley } 11090f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 11107f5b169aSMatthew G. Knepley /* Preallocate matrix */ 11117f5b169aSMatthew G. Knepley { 11127f5b169aSMatthew G. Knepley PetscHashJK ht; 11137f5b169aSMatthew G. Knepley PetscLayout rLayout; 11147f5b169aSMatthew G. Knepley PetscInt *dnz, *onz, *cellCIndices, *cellFIndices; 11157f5b169aSMatthew G. Knepley PetscInt locRows, rStart, rEnd, cell, r; 11167f5b169aSMatthew G. Knepley 11177f5b169aSMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 11187f5b169aSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 11197f5b169aSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 11207f5b169aSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 11217f5b169aSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 11227f5b169aSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 11237f5b169aSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 11247f5b169aSMatthew G. Knepley ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 11257f5b169aSMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 11267f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 11277f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 11287f5b169aSMatthew G. Knepley for (r = 0; r < rTotDim; ++r) { 11297f5b169aSMatthew G. Knepley PetscHashJKKey key; 11307f5b169aSMatthew G. Knepley PetscHashJKIter missing, iter; 11317f5b169aSMatthew G. Knepley 11327f5b169aSMatthew G. Knepley key.j = cellFIndices[r]; 11337f5b169aSMatthew G. Knepley if (key.j < 0) continue; 11347f5b169aSMatthew G. Knepley for (c = 0; c < cTotDim; ++c) { 11357f5b169aSMatthew G. Knepley key.k = cellCIndices[c]; 11367f5b169aSMatthew G. Knepley if (key.k < 0) continue; 11377f5b169aSMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 11387f5b169aSMatthew G. Knepley if (missing) { 11397f5b169aSMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 11407f5b169aSMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 11417f5b169aSMatthew G. Knepley else ++onz[key.j-rStart]; 11427f5b169aSMatthew G. Knepley } 11437f5b169aSMatthew G. Knepley } 11447f5b169aSMatthew G. Knepley } 11457f5b169aSMatthew G. Knepley } 11467f5b169aSMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 11477f5b169aSMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 11487f5b169aSMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 11497f5b169aSMatthew G. Knepley ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr); 11507f5b169aSMatthew G. Knepley } 11517f5b169aSMatthew G. Knepley /* Fill matrix */ 11527f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1153d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1154934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1155d69c5d34SMatthew G. Knepley } 1156549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 1157d69c5d34SMatthew G. Knepley ierr = PetscFree(feRef);CHKERRQ(ierr); 1158549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1159934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1160934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1161d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1162d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1163934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1164934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1165d69c5d34SMatthew G. Knepley } 1166d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1167d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1168d69c5d34SMatthew G. Knepley } 11696c73c22cSMatthew G. Knepley 11706c73c22cSMatthew G. Knepley #undef __FUNCT__ 11717c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM" 11727c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 11737c927364SMatthew G. Knepley { 1174e9d4ef1bSMatthew G. Knepley PetscDS prob; 11757c927364SMatthew G. Knepley PetscFE *feRef; 11767c927364SMatthew G. Knepley Vec fv, cv; 11777c927364SMatthew G. Knepley IS fis, cis; 11787c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 11797c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 11807c927364SMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, offsetC, offsetF, m; 11817c927364SMatthew G. Knepley PetscErrorCode ierr; 11827c927364SMatthew G. Knepley 11837c927364SMatthew G. Knepley PetscFunctionBegin; 118475a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1185c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 11867c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 11877c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 11887c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 11897c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 11907c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 11917c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 1192e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 11937c927364SMatthew G. Knepley ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr); 11947c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 11957c927364SMatthew G. Knepley PetscFE fe; 11967c927364SMatthew G. Knepley PetscInt fNb, Nc; 11977c927364SMatthew G. Knepley 1198e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 11997c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 12007c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 12017c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 12027c927364SMatthew G. Knepley fTotDim += fNb*Nc; 12037c927364SMatthew G. Knepley } 1204e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 12057c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 12067c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 12077c927364SMatthew G. Knepley PetscFE feC; 12087c927364SMatthew G. Knepley PetscDualSpace QF, QC; 12097c927364SMatthew G. Knepley PetscInt NcF, NcC, fpdim, cpdim; 12107c927364SMatthew G. Knepley 1211e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 12127c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 12137c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 12147c927364SMatthew G. Knepley if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC); 12157c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 12167c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 12177c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 12187c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 12197c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 12207c927364SMatthew G. Knepley PetscQuadrature cfunc; 12217c927364SMatthew G. Knepley const PetscReal *cqpoints; 12227c927364SMatthew G. Knepley PetscInt NpC; 12237c927364SMatthew G. Knepley 12247c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 12257c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr); 12267c927364SMatthew G. Knepley if (NpC != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 12277c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 12287c927364SMatthew G. Knepley PetscQuadrature ffunc; 12297c927364SMatthew G. Knepley const PetscReal *fqpoints; 12307c927364SMatthew G. Knepley PetscReal sum = 0.0; 12317c927364SMatthew G. Knepley PetscInt NpF, comp; 12327c927364SMatthew G. Knepley 12337c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 12347c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr); 12357c927364SMatthew G. Knepley if (NpC != NpF) continue; 12367c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 12377c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 12387c927364SMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 12397c927364SMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp; 12407c927364SMatthew G. Knepley } 12417c927364SMatthew G. Knepley break; 12427c927364SMatthew G. Knepley } 12437c927364SMatthew G. Knepley } 12447c927364SMatthew G. Knepley offsetC += cpdim*NcC; 12457c927364SMatthew G. Knepley offsetF += fpdim*NcF; 12467c927364SMatthew G. Knepley } 12477c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 12487c927364SMatthew G. Knepley ierr = PetscFree(feRef);CHKERRQ(ierr); 12497c927364SMatthew G. Knepley 12507c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 12517c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 12527c927364SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr); 12537c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 12547c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 1255aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 1256aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 12577c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 12587c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 12597c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 12607c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 12617c927364SMatthew G. Knepley if (cellCIndices[d] < 0) continue; 12627c927364SMatthew G. Knepley if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]); 12637c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 12647c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 12657c927364SMatthew G. Knepley } 12667c927364SMatthew G. Knepley } 12677c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 12687c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 12697c927364SMatthew G. Knepley 12707c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 12717c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 12727c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 12737c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 12747c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 12757c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 12767c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 127775a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1278cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1279cb1e1211SMatthew G Knepley } 1280