1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2cb1e1211SMatthew G Knepley 30f2d7e86SMatthew G. Knepley #include <petsc-private/petscfeimpl.h> 4f62f30faSMatthew G. Knepley #include <petscfv.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); 118cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(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" 186a18a7fb9SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 187a18a7fb9SMatthew G. Knepley { 188a18a7fb9SMatthew G. Knepley PetscDualSpace *sp; 189a18a7fb9SMatthew G. Knepley PetscSection section; 190a18a7fb9SMatthew G. Knepley PetscScalar *values; 191a18a7fb9SMatthew G. Knepley PetscReal *v0, *J, detJ; 192ad96f515SMatthew G. Knepley PetscBool *fieldActive; 193a18a7fb9SMatthew G. Knepley PetscInt numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, f, d, v, i, comp; 194a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 195a18a7fb9SMatthew G. Knepley 196a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 197a18a7fb9SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 198a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 199a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 200a18a7fb9SMatthew G. Knepley ierr = PetscMalloc3(numFields,&sp,dim,&v0,dim*dim,&J);CHKERRQ(ierr); 201a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 202a18a7fb9SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr); 203a18a7fb9SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 204a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 205a18a7fb9SMatthew G. Knepley totDim += spDim*numComp; 206a18a7fb9SMatthew G. Knepley } 207a18a7fb9SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 208a18a7fb9SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 209a18a7fb9SMatthew 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); 210a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 211ad96f515SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 212ad96f515SMatthew G. Knepley for (f = 0; f < numFields; ++f) fieldActive[f] = funcs[f] ? PETSC_TRUE : PETSC_FALSE; 213a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 214a18a7fb9SMatthew G. Knepley IS pointIS; 215a18a7fb9SMatthew G. Knepley const PetscInt *points; 216a18a7fb9SMatthew G. Knepley PetscInt n, p; 217a18a7fb9SMatthew G. Knepley 218a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 219a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 220a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 221a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 222a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 223a18a7fb9SMatthew G. Knepley PetscCellGeometry geom; 224a18a7fb9SMatthew G. Knepley 225a18a7fb9SMatthew G. Knepley if ((point < cStart) || (point >= cEnd)) continue; 226a18a7fb9SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, v0, J, NULL, &detJ);CHKERRQ(ierr); 227a18a7fb9SMatthew G. Knepley geom.v0 = v0; 228a18a7fb9SMatthew G. Knepley geom.J = J; 229a18a7fb9SMatthew G. Knepley geom.detJ = &detJ; 230a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 231a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 232a18a7fb9SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 233a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 234a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 235a18a7fb9SMatthew G. Knepley if (funcs[f]) { 236a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr); 237a18a7fb9SMatthew G. Knepley } else { 238a18a7fb9SMatthew G. Knepley for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0; 239a18a7fb9SMatthew G. Knepley } 240a18a7fb9SMatthew G. Knepley v += numComp; 241a18a7fb9SMatthew G. Knepley } 242a18a7fb9SMatthew G. Knepley } 243ad96f515SMatthew G. Knepley ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr); 244a18a7fb9SMatthew G. Knepley } 245a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 246a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 247a18a7fb9SMatthew G. Knepley } 248a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 249ad96f515SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 250a18a7fb9SMatthew G. Knepley ierr = PetscFree3(sp,v0,J);CHKERRQ(ierr); 251a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 252a18a7fb9SMatthew G. Knepley } 253a18a7fb9SMatthew G. Knepley 254a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 255cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 2560f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 257cb1e1211SMatthew G Knepley { 25872f94c41SMatthew G. Knepley PetscDualSpace *sp; 25972f94c41SMatthew G. Knepley PetscSection section; 26072f94c41SMatthew G. Knepley PetscScalar *values; 26172f94c41SMatthew G. Knepley PetscReal *v0, *J, detJ; 262120386c5SMatthew G. Knepley PetscInt numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp; 263cb1e1211SMatthew G Knepley PetscErrorCode ierr; 264cb1e1211SMatthew G Knepley 265cb1e1211SMatthew G Knepley PetscFunctionBegin; 266cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 26772f94c41SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 268785e854fSJed Brown ierr = PetscMalloc1(numFields, &sp);CHKERRQ(ierr); 26972f94c41SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 2700f2d7e86SMatthew G. Knepley PetscFE fe; 2710f2d7e86SMatthew G. Knepley 2720f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr); 2730f2d7e86SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 2740f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 27572f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 27672f94c41SMatthew G. Knepley totDim += spDim*numComp; 277cb1e1211SMatthew G Knepley } 27872f94c41SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 27972f94c41SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 28072f94c41SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 28172f94c41SMatthew 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); 28272f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 283dcca6d9dSJed Brown ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr); 28472f94c41SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 28572f94c41SMatthew G. Knepley PetscCellGeometry geom; 286cb1e1211SMatthew G Knepley 287cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr); 28872f94c41SMatthew G. Knepley geom.v0 = v0; 28972f94c41SMatthew G. Knepley geom.J = J; 29072f94c41SMatthew G. Knepley geom.detJ = &detJ; 29172f94c41SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 2920f2d7e86SMatthew G. Knepley PetscFE fe; 293c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[f] : NULL; 2940f2d7e86SMatthew G. Knepley 2950f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr); 2960f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp);CHKERRQ(ierr); 29772f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 29872f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 299120386c5SMatthew G. Knepley if (funcs[f]) { 300c110b1eeSGeoffrey Irving ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr); 301120386c5SMatthew G. Knepley } else { 302120386c5SMatthew G. Knepley for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0; 303120386c5SMatthew G. Knepley } 30472f94c41SMatthew G. Knepley v += numComp; 305cb1e1211SMatthew G Knepley } 306cb1e1211SMatthew G Knepley } 30772f94c41SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 308cb1e1211SMatthew G Knepley } 30972f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 3101f2da991SMatthew G. Knepley ierr = PetscFree2(v0,J);CHKERRQ(ierr); 31172f94c41SMatthew G. Knepley ierr = PetscFree(sp);CHKERRQ(ierr); 312cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 313cb1e1211SMatthew G Knepley } 314cb1e1211SMatthew G Knepley 315cb1e1211SMatthew G Knepley #undef __FUNCT__ 316cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction" 317cb1e1211SMatthew G Knepley /*@C 318cb1e1211SMatthew G Knepley DMPlexProjectFunction - This projects the given function into the function space provided. 319cb1e1211SMatthew G Knepley 320cb1e1211SMatthew G Knepley Input Parameters: 321cb1e1211SMatthew G Knepley + dm - The DM 32272f94c41SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field 323c110b1eeSGeoffrey Irving . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 324cb1e1211SMatthew G Knepley - mode - The insertion mode for values 325cb1e1211SMatthew G Knepley 326cb1e1211SMatthew G Knepley Output Parameter: 327cb1e1211SMatthew G Knepley . X - vector 328cb1e1211SMatthew G Knepley 329cb1e1211SMatthew G Knepley Level: developer 330cb1e1211SMatthew G Knepley 331878cb397SSatish Balay .seealso: DMPlexComputeL2Diff() 332878cb397SSatish Balay @*/ 3330f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) 334cb1e1211SMatthew G Knepley { 335cb1e1211SMatthew G Knepley Vec localX; 336cb1e1211SMatthew G Knepley PetscErrorCode ierr; 337cb1e1211SMatthew G Knepley 338cb1e1211SMatthew G Knepley PetscFunctionBegin; 3399a800dd8SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 340cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 3410f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr); 342cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 343cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 344cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 345cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 346cb1e1211SMatthew G Knepley } 347cb1e1211SMatthew G Knepley 34855f2e967SMatthew G. Knepley #undef __FUNCT__ 3490f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal" 3503bc3b0a0SMatthew 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) 3510f2d7e86SMatthew G. Knepley { 3520f2d7e86SMatthew G. Knepley DM dmAux; 3532764a2aaSMatthew G. Knepley PetscDS prob, probAux; 3540f2d7e86SMatthew G. Knepley Vec A; 355326413afSMatthew G. Knepley PetscSection section, sectionAux; 356326413afSMatthew G. Knepley PetscScalar *values, *u, *u_x, *a, *a_x; 3570f2d7e86SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ, **basisField, **basisFieldDer, **basisFieldAux, **basisFieldDerAux; 358326413afSMatthew G. Knepley PetscInt Nf, dim, spDim, totDim, numValues, cStart, cEnd, c, f, d, v, comp; 3590f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 3600f2d7e86SMatthew G. Knepley 3610f2d7e86SMatthew G. Knepley PetscFunctionBegin; 3622764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 3630f2d7e86SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 3640f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 3650f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 3660f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 3672764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 3682764a2aaSMatthew G. Knepley ierr = PetscDSGetTabulation(prob, &basisField, &basisFieldDer);CHKERRQ(ierr); 3692764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr); 3702764a2aaSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 3710f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 3720f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 3730f2d7e86SMatthew G. Knepley if (dmAux) { 3742764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 375326413afSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 3762764a2aaSMatthew G. Knepley ierr = PetscDSGetTabulation(prob, &basisFieldAux, &basisFieldDerAux);CHKERRQ(ierr); 3772764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr); 3780f2d7e86SMatthew G. Knepley } 3790f2d7e86SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, localU);CHKERRQ(ierr); 3800f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 3810f2d7e86SMatthew 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); 3820f2d7e86SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 3830f2d7e86SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 3840f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 385326413afSMatthew G. Knepley PetscScalar *coefficients = NULL, *coefficientsAux = NULL; 386326413afSMatthew G. Knepley 3870f2d7e86SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 388326413afSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 389326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 3900f2d7e86SMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 3913113607cSMatthew G. Knepley PetscFE fe; 3923113607cSMatthew G. Knepley PetscDualSpace sp; 3933113607cSMatthew G. Knepley PetscInt Ncf; 3943113607cSMatthew G. Knepley 3952764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 3963113607cSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp);CHKERRQ(ierr); 3973113607cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncf);CHKERRQ(ierr); 3983113607cSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp, &spDim);CHKERRQ(ierr); 3990f2d7e86SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 4000f2d7e86SMatthew G. Knepley PetscQuadrature quad; 4010f2d7e86SMatthew G. Knepley const PetscReal *points, *weights; 4020f2d7e86SMatthew G. Knepley PetscInt numPoints, q; 4030f2d7e86SMatthew G. Knepley 4040f2d7e86SMatthew G. Knepley if (funcs[f]) { 4053113607cSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(sp, d, &quad);CHKERRQ(ierr); 4060f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr); 4073113607cSMatthew G. Knepley ierr = PetscFEGetTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr); 4080f2d7e86SMatthew G. Knepley for (q = 0; q < numPoints; ++q) { 4090f2d7e86SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x); 410326413afSMatthew G. Knepley ierr = EvaluateFieldJets(prob, PETSC_FALSE, q, invJ, coefficients, NULL, u, u_x, NULL);CHKERRQ(ierr); 411326413afSMatthew G. Knepley ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr); 4123bc3b0a0SMatthew G. Knepley (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]); 4130f2d7e86SMatthew G. Knepley } 4143113607cSMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, numPoints, points, &basisField[f], &basisFieldDer[f], NULL);CHKERRQ(ierr); 4150f2d7e86SMatthew G. Knepley } else { 4160f2d7e86SMatthew G. Knepley for (comp = 0; comp < Ncf; ++comp) values[v+comp] = 0.0; 4170f2d7e86SMatthew G. Knepley } 4180f2d7e86SMatthew G. Knepley v += Ncf; 4190f2d7e86SMatthew G. Knepley } 4200f2d7e86SMatthew G. Knepley } 421326413afSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 422326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 4230f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 4240f2d7e86SMatthew G. Knepley } 4250f2d7e86SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4260f2d7e86SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 4270f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 4280f2d7e86SMatthew G. Knepley } 4290f2d7e86SMatthew G. Knepley 4300f2d7e86SMatthew G. Knepley #undef __FUNCT__ 4310f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectField" 4320f2d7e86SMatthew G. Knepley /*@C 4330f2d7e86SMatthew G. Knepley DMPlexProjectField - This projects the given function of the fields into the function space provided. 4340f2d7e86SMatthew G. Knepley 4350f2d7e86SMatthew G. Knepley Input Parameters: 4360f2d7e86SMatthew G. Knepley + dm - The DM 4370f2d7e86SMatthew G. Knepley . U - The input field vector 4380f2d7e86SMatthew G. Knepley . funcs - The functions to evaluate, one per field 4390f2d7e86SMatthew G. Knepley - mode - The insertion mode for values 4400f2d7e86SMatthew G. Knepley 4410f2d7e86SMatthew G. Knepley Output Parameter: 4420f2d7e86SMatthew G. Knepley . X - The output vector 4430f2d7e86SMatthew G. Knepley 4440f2d7e86SMatthew G. Knepley Level: developer 4450f2d7e86SMatthew G. Knepley 4460f2d7e86SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 4470f2d7e86SMatthew G. Knepley @*/ 4483bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectField(DM dm, Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X) 4490f2d7e86SMatthew G. Knepley { 4500f2d7e86SMatthew G. Knepley Vec localX, localU; 4510f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 4520f2d7e86SMatthew G. Knepley 4530f2d7e86SMatthew G. Knepley PetscFunctionBegin; 4540f2d7e86SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4550f2d7e86SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 4560f2d7e86SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr); 4570f2d7e86SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr); 4580f2d7e86SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr); 4593113607cSMatthew G. Knepley ierr = DMPlexProjectFieldLocal(dm, localU, funcs, mode, localX);CHKERRQ(ierr); 4600f2d7e86SMatthew G. Knepley ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 4610f2d7e86SMatthew G. Knepley ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 4620f2d7e86SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 4630f2d7e86SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr); 4640f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 4650f2d7e86SMatthew G. Knepley } 4660f2d7e86SMatthew G. Knepley 4670f2d7e86SMatthew G. Knepley #undef __FUNCT__ 4683351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM" 4693351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX) 47055f2e967SMatthew G. Knepley { 47155f2e967SMatthew G. Knepley void (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx); 47255f2e967SMatthew G. Knepley void **ctxs; 47355f2e967SMatthew G. Knepley PetscFE *fe; 47455f2e967SMatthew G. Knepley PetscInt numFields, f, numBd, b; 47555f2e967SMatthew G. Knepley PetscErrorCode ierr; 47655f2e967SMatthew G. Knepley 47755f2e967SMatthew G. Knepley PetscFunctionBegin; 47855f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 47955f2e967SMatthew G. Knepley PetscValidHeaderSpecific(localX, VEC_CLASSID, 2); 48055f2e967SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 48155f2e967SMatthew G. Knepley ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 48255f2e967SMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);} 48355f2e967SMatthew G. Knepley /* OPT: Could attempt to do multiple BCs at once */ 48455f2e967SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 48555f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 486a18a7fb9SMatthew G. Knepley DMLabel label; 48755f2e967SMatthew G. Knepley const PetscInt *ids; 48863d5297fSMatthew G. Knepley const char *labelname; 48955f2e967SMatthew G. Knepley PetscInt numids, field; 49055f2e967SMatthew G. Knepley PetscBool isEssential; 49155f2e967SMatthew G. Knepley void (*func)(); 49255f2e967SMatthew G. Knepley void *ctx; 49355f2e967SMatthew G. Knepley 49463d5297fSMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 49563d5297fSMatthew G. Knepley ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr); 49655f2e967SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 49755f2e967SMatthew G. Knepley funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL; 49855f2e967SMatthew G. Knepley ctxs[f] = field == f ? ctx : NULL; 49955f2e967SMatthew G. Knepley } 500a18a7fb9SMatthew G. Knepley ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 50155f2e967SMatthew G. Knepley } 50255f2e967SMatthew G. Knepley ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr); 50355f2e967SMatthew G. Knepley PetscFunctionReturn(0); 50455f2e967SMatthew G. Knepley } 50555f2e967SMatthew G. Knepley 506cb1e1211SMatthew G Knepley #undef __FUNCT__ 507cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 508cb1e1211SMatthew G Knepley /*@C 509cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 510cb1e1211SMatthew G Knepley 511cb1e1211SMatthew G Knepley Input Parameters: 512cb1e1211SMatthew G Knepley + dm - The DM 513cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 51451259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 515cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 516cb1e1211SMatthew G Knepley 517cb1e1211SMatthew G Knepley Output Parameter: 518cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 519cb1e1211SMatthew G Knepley 520cb1e1211SMatthew G Knepley Level: developer 521cb1e1211SMatthew G Knepley 52223d86601SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2GradientDiff() 523878cb397SSatish Balay @*/ 5240f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 525cb1e1211SMatthew G Knepley { 526cb1e1211SMatthew G Knepley const PetscInt debug = 0; 527cb1e1211SMatthew G Knepley PetscSection section; 528c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 529cb1e1211SMatthew G Knepley Vec localX; 53072f94c41SMatthew G. Knepley PetscScalar *funcVal; 531cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 532cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 533cb1e1211SMatthew G Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 534cb1e1211SMatthew G Knepley PetscErrorCode ierr; 535cb1e1211SMatthew G Knepley 536cb1e1211SMatthew G Knepley PetscFunctionBegin; 537cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 538cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 539cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 540cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 541cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 542cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 543cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 5440f2d7e86SMatthew G. Knepley PetscFE fe; 545c5bbbd5bSMatthew G. Knepley PetscInt Nc; 546c5bbbd5bSMatthew G. Knepley 5470f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 5480f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 5490f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 550c5bbbd5bSMatthew G. Knepley numComponents += Nc; 551cb1e1211SMatthew G Knepley } 5520f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 553dcca6d9dSJed Brown ierr = PetscMalloc5(numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 554cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 555cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 556a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 557cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 558cb1e1211SMatthew G Knepley 559cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 560cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 561cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 562cb1e1211SMatthew G Knepley 563cb1e1211SMatthew G Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 5640f2d7e86SMatthew G. Knepley PetscFE fe; 565c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 56621454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 567c5bbbd5bSMatthew G. Knepley PetscReal *basis; 56821454ff5SMatthew G. Knepley PetscInt numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f; 569cb1e1211SMatthew G Knepley 5700f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 57121454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 5720f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr); 5730f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr); 5740f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr); 575cb1e1211SMatthew G Knepley if (debug) { 576cb1e1211SMatthew G Knepley char title[1024]; 577cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 578cb1e1211SMatthew G Knepley ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr); 579cb1e1211SMatthew G Knepley } 580cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 581cb1e1211SMatthew G Knepley for (d = 0; d < dim; d++) { 582cb1e1211SMatthew G Knepley coords[d] = v0[d]; 583cb1e1211SMatthew G Knepley for (e = 0; e < dim; e++) { 584cb1e1211SMatthew G Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 585cb1e1211SMatthew G Knepley } 586cb1e1211SMatthew G Knepley } 587c110b1eeSGeoffrey Irving (*funcs[field])(coords, funcVal, ctx); 588cb1e1211SMatthew G Knepley for (fc = 0; fc < numBasisComps; ++fc) { 589a1d24da5SMatthew G. Knepley PetscScalar interpolant = 0.0; 590a1d24da5SMatthew G. Knepley 591cb1e1211SMatthew G Knepley for (f = 0; f < numBasisFuncs; ++f) { 592cb1e1211SMatthew G Knepley const PetscInt fidx = f*numBasisComps+fc; 593a1d24da5SMatthew G. Knepley interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx]; 594cb1e1211SMatthew G Knepley } 59572f94c41SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 59672f94c41SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 597cb1e1211SMatthew G Knepley } 598cb1e1211SMatthew G Knepley } 599cb1e1211SMatthew G Knepley comp += numBasisComps; 600cb1e1211SMatthew G Knepley fieldOffset += numBasisFuncs*numBasisComps; 601cb1e1211SMatthew G Knepley } 602cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 603cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 604cb1e1211SMatthew G Knepley localDiff += elemDiff; 605cb1e1211SMatthew G Knepley } 60672f94c41SMatthew G. Knepley ierr = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr); 607cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 60886a74ee0SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 609cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 610cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 611cb1e1211SMatthew G Knepley } 612cb1e1211SMatthew G Knepley 613cb1e1211SMatthew G Knepley #undef __FUNCT__ 61440e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 61540e14135SMatthew G. Knepley /*@C 61640e14135SMatthew 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. 61740e14135SMatthew G. Knepley 61840e14135SMatthew G. Knepley Input Parameters: 61940e14135SMatthew G. Knepley + dm - The DM 62040e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 62151259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 62240e14135SMatthew G. Knepley . X - The coefficient vector u_h 62340e14135SMatthew G. Knepley - n - The vector to project along 62440e14135SMatthew G. Knepley 62540e14135SMatthew G. Knepley Output Parameter: 62640e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 62740e14135SMatthew G. Knepley 62840e14135SMatthew G. Knepley Level: developer 62940e14135SMatthew G. Knepley 63040e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 63140e14135SMatthew G. Knepley @*/ 6320f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 633cb1e1211SMatthew G Knepley { 63440e14135SMatthew G. Knepley const PetscInt debug = 0; 635cb1e1211SMatthew G Knepley PetscSection section; 63640e14135SMatthew G. Knepley PetscQuadrature quad; 63740e14135SMatthew G. Knepley Vec localX; 63840e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 63940e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 64040e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 64140e14135SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 642cb1e1211SMatthew G Knepley PetscErrorCode ierr; 643cb1e1211SMatthew G Knepley 644cb1e1211SMatthew G Knepley PetscFunctionBegin; 64540e14135SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 64640e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 64740e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 64840e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 64940e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 65040e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 651652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 6520f2d7e86SMatthew G. Knepley PetscFE fe; 65340e14135SMatthew G. Knepley PetscInt Nc; 654652b88e8SMatthew G. Knepley 6550f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 6560f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 6570f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 65840e14135SMatthew G. Knepley numComponents += Nc; 659652b88e8SMatthew G. Knepley } 66040e14135SMatthew G. Knepley /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 66140e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 66240e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 66340e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 66440e14135SMatthew G. Knepley PetscScalar *x = NULL; 66540e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 666652b88e8SMatthew G. Knepley 66740e14135SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 66840e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 66940e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 67040e14135SMatthew G. Knepley 67140e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 6720f2d7e86SMatthew G. Knepley PetscFE fe; 67351259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 67421454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 67540e14135SMatthew G. Knepley PetscReal *basisDer; 67621454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 67740e14135SMatthew G. Knepley 6780f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 67921454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 6800f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 6810f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr); 6820f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 68340e14135SMatthew G. Knepley if (debug) { 68440e14135SMatthew G. Knepley char title[1024]; 68540e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 68640e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 687652b88e8SMatthew G. Knepley } 68840e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 68940e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 69040e14135SMatthew G. Knepley coords[d] = v0[d]; 69140e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 69240e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 693652b88e8SMatthew G. Knepley } 69440e14135SMatthew G. Knepley } 69551259fa3SMatthew G. Knepley (*funcs[field])(coords, n, funcVal, ctx); 69640e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 69740e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 69840e14135SMatthew G. Knepley 69940e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 70040e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 70140e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 70240e14135SMatthew G. Knepley 70340e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 70440e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 70540e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 70640e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 70740e14135SMatthew G. Knepley } 70840e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 70940e14135SMatthew G. Knepley } 71040e14135SMatthew G. Knepley } 71140e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 71240e14135SMatthew 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);} 71340e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 71440e14135SMatthew G. Knepley } 71540e14135SMatthew G. Knepley } 71640e14135SMatthew G. Knepley comp += Ncomp; 71740e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 71840e14135SMatthew G. Knepley } 71940e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 72040e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 72140e14135SMatthew G. Knepley localDiff += elemDiff; 72240e14135SMatthew G. Knepley } 72340e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 72440e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 72540e14135SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 72640e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 727cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 728cb1e1211SMatthew G Knepley } 729cb1e1211SMatthew G Knepley 730a0845e3aSMatthew G. Knepley #undef __FUNCT__ 73173d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff" 7320f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 73373d901b8SMatthew G. Knepley { 73473d901b8SMatthew G. Knepley const PetscInt debug = 0; 73573d901b8SMatthew G. Knepley PetscSection section; 73673d901b8SMatthew G. Knepley PetscQuadrature quad; 73773d901b8SMatthew G. Knepley Vec localX; 73873d901b8SMatthew G. Knepley PetscScalar *funcVal; 73973d901b8SMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 74073d901b8SMatthew G. Knepley PetscReal *localDiff; 74173d901b8SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 74273d901b8SMatthew G. Knepley PetscErrorCode ierr; 74373d901b8SMatthew G. Knepley 74473d901b8SMatthew G. Knepley PetscFunctionBegin; 74573d901b8SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 74673d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 74773d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 74873d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 74973d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 75073d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 75173d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 7520f2d7e86SMatthew G. Knepley PetscFE fe; 75373d901b8SMatthew G. Knepley PetscInt Nc; 75473d901b8SMatthew G. Knepley 7550f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 7560f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7570f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 75873d901b8SMatthew G. Knepley numComponents += Nc; 75973d901b8SMatthew G. Knepley } 7600f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 76173d901b8SMatthew G. Knepley ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 76273d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 76373d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 76473d901b8SMatthew G. Knepley PetscScalar *x = NULL; 76573d901b8SMatthew G. Knepley 76673d901b8SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 76773d901b8SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 76873d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 76973d901b8SMatthew G. Knepley 77073d901b8SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 7710f2d7e86SMatthew G. Knepley PetscFE fe; 77273d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 77373d901b8SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 77473d901b8SMatthew G. Knepley PetscReal *basis, elemDiff = 0.0; 77573d901b8SMatthew G. Knepley PetscInt numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f; 77673d901b8SMatthew G. Knepley 7770f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 77873d901b8SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 7790f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &numBasisFuncs);CHKERRQ(ierr); 7800f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numBasisComps);CHKERRQ(ierr); 7810f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, &basis, NULL, NULL);CHKERRQ(ierr); 78273d901b8SMatthew G. Knepley if (debug) { 78373d901b8SMatthew G. Knepley char title[1024]; 78473d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 78573d901b8SMatthew G. Knepley ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr); 78673d901b8SMatthew G. Knepley } 78773d901b8SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 78873d901b8SMatthew G. Knepley for (d = 0; d < dim; d++) { 78973d901b8SMatthew G. Knepley coords[d] = v0[d]; 79073d901b8SMatthew G. Knepley for (e = 0; e < dim; e++) { 79173d901b8SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 79273d901b8SMatthew G. Knepley } 79373d901b8SMatthew G. Knepley } 79473d901b8SMatthew G. Knepley (*funcs[field])(coords, funcVal, ctx); 79573d901b8SMatthew G. Knepley for (fc = 0; fc < numBasisComps; ++fc) { 79673d901b8SMatthew G. Knepley PetscScalar interpolant = 0.0; 79773d901b8SMatthew G. Knepley 79873d901b8SMatthew G. Knepley for (f = 0; f < numBasisFuncs; ++f) { 79973d901b8SMatthew G. Knepley const PetscInt fidx = f*numBasisComps+fc; 80073d901b8SMatthew G. Knepley interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx]; 80173d901b8SMatthew G. Knepley } 80273d901b8SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 80373d901b8SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 80473d901b8SMatthew G. Knepley } 80573d901b8SMatthew G. Knepley } 80673d901b8SMatthew G. Knepley comp += numBasisComps; 80773d901b8SMatthew G. Knepley fieldOffset += numBasisFuncs*numBasisComps; 80873d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 80973d901b8SMatthew G. Knepley } 81073d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 81173d901b8SMatthew G. Knepley } 81273d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 81373d901b8SMatthew G. Knepley ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 81473d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 81573d901b8SMatthew G. Knepley ierr = PetscFree6(localDiff,funcVal,coords,v0,J,invJ);CHKERRQ(ierr); 81673d901b8SMatthew G. Knepley PetscFunctionReturn(0); 81773d901b8SMatthew G. Knepley } 81873d901b8SMatthew G. Knepley 81973d901b8SMatthew G. Knepley #undef __FUNCT__ 82073d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM" 82173d901b8SMatthew G. Knepley /*@ 82273d901b8SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user 82373d901b8SMatthew G. Knepley 82473d901b8SMatthew G. Knepley Input Parameters: 82573d901b8SMatthew G. Knepley + dm - The mesh 82673d901b8SMatthew G. Knepley . X - Local input vector 82773d901b8SMatthew G. Knepley - user - The user context 82873d901b8SMatthew G. Knepley 82973d901b8SMatthew G. Knepley Output Parameter: 83073d901b8SMatthew G. Knepley . integral - Local integral for each field 83173d901b8SMatthew G. Knepley 83273d901b8SMatthew G. Knepley Level: developer 83373d901b8SMatthew G. Knepley 83473d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 83573d901b8SMatthew G. Knepley @*/ 8360f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user) 83773d901b8SMatthew G. Knepley { 83873d901b8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 83973d901b8SMatthew G. Knepley DM dmAux; 84073d901b8SMatthew G. Knepley Vec localX, A; 8412764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 84273d901b8SMatthew G. Knepley PetscQuadrature q; 84373d901b8SMatthew G. Knepley PetscCellGeometry geom; 84473d901b8SMatthew G. Knepley PetscSection section, sectionAux; 84573d901b8SMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 84673d901b8SMatthew G. Knepley PetscScalar *u, *a = NULL; 8470f2d7e86SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 8480f2d7e86SMatthew G. Knepley PetscInt totDim, totDimAux; 84973d901b8SMatthew G. Knepley PetscErrorCode ierr; 85073d901b8SMatthew G. Knepley 85173d901b8SMatthew G. Knepley PetscFunctionBegin; 85273d901b8SMatthew G. Knepley /*ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/ 85373d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 85473d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 85573d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 85673d901b8SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 85773d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 8582764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 8592764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 86073d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 86173d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 86273d901b8SMatthew G. Knepley numCells = cEnd - cStart; 8630f2d7e86SMatthew G. Knepley for (f = 0; f < Nf; ++f) {integral[f] = 0.0;} 86473d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 86573d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 86673d901b8SMatthew G. Knepley if (dmAux) { 86773d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 8682764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 8692764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 87073d901b8SMatthew G. Knepley } 87173d901b8SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, localX);CHKERRQ(ierr); 8720f2d7e86SMatthew G. Knepley ierr = PetscMalloc5(numCells*totDim,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ);CHKERRQ(ierr); 8730f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 87473d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 87573d901b8SMatthew G. Knepley PetscScalar *x = NULL; 87673d901b8SMatthew G. Knepley PetscInt i; 87773d901b8SMatthew G. Knepley 87873d901b8SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 87973d901b8SMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 88073d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 8810f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 88273d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 88373d901b8SMatthew G. Knepley if (dmAux) { 88473d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 8850f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 88673d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 88773d901b8SMatthew G. Knepley } 88873d901b8SMatthew G. Knepley } 88973d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 8900f2d7e86SMatthew G. Knepley PetscFE fe; 89173d901b8SMatthew G. Knepley PetscInt numQuadPoints, Nb; 89273d901b8SMatthew G. Knepley /* Conforming batches */ 89373d901b8SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 89473d901b8SMatthew G. Knepley /* Remainder */ 89573d901b8SMatthew G. Knepley PetscInt Nr, offset; 89673d901b8SMatthew G. Knepley 8972764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 8980f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 8990f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 9000f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 90173d901b8SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 90273d901b8SMatthew G. Knepley blockSize = Nb*numQuadPoints; 90373d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 9040f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 90573d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 90673d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 90773d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 90873d901b8SMatthew G. Knepley offset = numCells - Nr; 90973d901b8SMatthew G. Knepley geom.v0 = v0; 91073d901b8SMatthew G. Knepley geom.J = J; 91173d901b8SMatthew G. Knepley geom.invJ = invJ; 91273d901b8SMatthew G. Knepley geom.detJ = detJ; 9130f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Ne, geom, u, probAux, a, integral);CHKERRQ(ierr); 91473d901b8SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 91573d901b8SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 91673d901b8SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 91773d901b8SMatthew G. Knepley geom.detJ = &detJ[offset]; 9180f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Nr, geom, &u[offset*totDim], probAux, &a[offset*totDimAux], integral);CHKERRQ(ierr); 91973d901b8SMatthew G. Knepley } 92073d901b8SMatthew G. Knepley ierr = PetscFree5(u,v0,J,invJ,detJ);CHKERRQ(ierr); 92173d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 92273d901b8SMatthew G. Knepley if (mesh->printFEM) { 92373d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr); 92473d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", integral[f]);CHKERRQ(ierr);} 92573d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 92673d901b8SMatthew G. Knepley } 92773d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 92873d901b8SMatthew G. Knepley /* TODO: Allreduce for integral */ 92973d901b8SMatthew G. Knepley /*ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);*/ 93073d901b8SMatthew G. Knepley PetscFunctionReturn(0); 93173d901b8SMatthew G. Knepley } 93273d901b8SMatthew G. Knepley 93373d901b8SMatthew G. Knepley #undef __FUNCT__ 9340f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal" 9350f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user) 9360f2d7e86SMatthew G. Knepley { 9370f2d7e86SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 9380f2d7e86SMatthew G. Knepley const char *name = "Residual"; 9390f2d7e86SMatthew G. Knepley DM dmAux; 9400f2d7e86SMatthew G. Knepley DMLabel depth; 9410f2d7e86SMatthew G. Knepley Vec A; 9422764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 9430f2d7e86SMatthew G. Knepley PetscQuadrature q; 9440f2d7e86SMatthew G. Knepley PetscCellGeometry geom; 9450f2d7e86SMatthew G. Knepley PetscSection section, sectionAux; 9460f2d7e86SMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 9470f2d7e86SMatthew G. Knepley PetscScalar *elemVec, *u, *u_t, *a = NULL; 9480f2d7e86SMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd; 9490f2d7e86SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux; 9500f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 9510f2d7e86SMatthew G. Knepley 9520f2d7e86SMatthew G. Knepley PetscFunctionBegin; 9530f2d7e86SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 9540f2d7e86SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 9550f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 9562764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 9572764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 9582764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 9590f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 9600f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 9610f2d7e86SMatthew G. Knepley numCells = cEnd - cStart; 9620f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 9630f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 9640f2d7e86SMatthew G. Knepley if (dmAux) { 9650f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 9662764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 9672764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 9680f2d7e86SMatthew G. Knepley } 9690f2d7e86SMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 9700f2d7e86SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 9710f2d7e86SMatthew G. Knepley ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr); 9720f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 9730f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 9740f2d7e86SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 9750f2d7e86SMatthew G. Knepley PetscInt i; 9760f2d7e86SMatthew G. Knepley 9770f2d7e86SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 9780f2d7e86SMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 9790f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 9800f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 9810f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 9820f2d7e86SMatthew G. Knepley if (X_t) { 9830f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 9840f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 9850f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 9860f2d7e86SMatthew G. Knepley } 9870f2d7e86SMatthew G. Knepley if (dmAux) { 9880f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 9890f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 9900f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 9910f2d7e86SMatthew G. Knepley } 9920f2d7e86SMatthew G. Knepley } 9930f2d7e86SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 9940f2d7e86SMatthew G. Knepley PetscFE fe; 9950f2d7e86SMatthew G. Knepley PetscInt numQuadPoints, Nb; 9960f2d7e86SMatthew G. Knepley /* Conforming batches */ 9970f2d7e86SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 9980f2d7e86SMatthew G. Knepley /* Remainder */ 9990f2d7e86SMatthew G. Knepley PetscInt Nr, offset; 10000f2d7e86SMatthew G. Knepley 10012764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 10020f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 10030f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 10040f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 10050f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 10060f2d7e86SMatthew G. Knepley blockSize = Nb*numQuadPoints; 10070f2d7e86SMatthew G. Knepley batchSize = numBlocks * blockSize; 10080f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 10090f2d7e86SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 10100f2d7e86SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 10110f2d7e86SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 10120f2d7e86SMatthew G. Knepley offset = numCells - Nr; 10130f2d7e86SMatthew G. Knepley geom.v0 = v0; 10140f2d7e86SMatthew G. Knepley geom.J = J; 10150f2d7e86SMatthew G. Knepley geom.invJ = invJ; 10160f2d7e86SMatthew G. Knepley geom.detJ = detJ; 10170f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 10180f2d7e86SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 10190f2d7e86SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 10200f2d7e86SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 10210f2d7e86SMatthew G. Knepley geom.detJ = &detJ[offset]; 10220f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 10230f2d7e86SMatthew G. Knepley } 10240f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 10250f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);} 10260f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 10270f2d7e86SMatthew G. Knepley } 10280f2d7e86SMatthew G. Knepley ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 10290f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 10300f2d7e86SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 10310f2d7e86SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 10320f2d7e86SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 10330f2d7e86SMatthew G. Knepley const char *bdLabel; 10340f2d7e86SMatthew G. Knepley DMLabel label; 10350f2d7e86SMatthew G. Knepley IS pointIS; 10360f2d7e86SMatthew G. Knepley const PetscInt *points; 10370f2d7e86SMatthew G. Knepley const PetscInt *values; 10380f2d7e86SMatthew G. Knepley PetscReal *n; 10390f2d7e86SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 10400f2d7e86SMatthew G. Knepley PetscBool isEssential; 10410f2d7e86SMatthew G. Knepley 10420f2d7e86SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 10430f2d7e86SMatthew G. Knepley if (isEssential) continue; 10440f2d7e86SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 10450f2d7e86SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 10460f2d7e86SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 10470f2d7e86SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 10480f2d7e86SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 10490f2d7e86SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 10500f2d7e86SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 10510f2d7e86SMatthew G. Knepley if (dep == dim-1) ++numFaces; 10520f2d7e86SMatthew G. Knepley } 10530f2d7e86SMatthew G. Knepley ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd,&elemVec);CHKERRQ(ierr); 10540f2d7e86SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 10550f2d7e86SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 10560f2d7e86SMatthew G. Knepley const PetscInt point = points[p]; 10570f2d7e86SMatthew G. Knepley PetscScalar *x = NULL; 10580f2d7e86SMatthew G. Knepley PetscInt i; 10590f2d7e86SMatthew G. Knepley 10600f2d7e86SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 10610f2d7e86SMatthew G. Knepley if (dep != dim-1) continue; 10620f2d7e86SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr); 10630f2d7e86SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]); 10640f2d7e86SMatthew G. Knepley if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point); 10650f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 10660f2d7e86SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 10670f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 10680f2d7e86SMatthew G. Knepley if (X_t) { 10690f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 10700f2d7e86SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 10710f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 10720f2d7e86SMatthew G. Knepley } 10730f2d7e86SMatthew G. Knepley ++f; 10740f2d7e86SMatthew G. Knepley } 10750f2d7e86SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 10760f2d7e86SMatthew G. Knepley PetscFE fe; 10770f2d7e86SMatthew G. Knepley PetscInt numQuadPoints, Nb; 10780f2d7e86SMatthew G. Knepley /* Conforming batches */ 10790f2d7e86SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 10800f2d7e86SMatthew G. Knepley /* Remainder */ 10810f2d7e86SMatthew G. Knepley PetscInt Nr, offset; 10820f2d7e86SMatthew G. Knepley 10832764a2aaSMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 10840f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 10850f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 10860f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 10870f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 10880f2d7e86SMatthew G. Knepley blockSize = Nb*numQuadPoints; 10890f2d7e86SMatthew G. Knepley batchSize = numBlocks * blockSize; 10900f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 10910f2d7e86SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 10920f2d7e86SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 10930f2d7e86SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 10940f2d7e86SMatthew G. Knepley offset = numFaces - Nr; 10950f2d7e86SMatthew G. Knepley geom.v0 = v0; 10960f2d7e86SMatthew G. Knepley geom.n = n; 10970f2d7e86SMatthew G. Knepley geom.J = J; 10980f2d7e86SMatthew G. Knepley geom.invJ = invJ; 10990f2d7e86SMatthew G. Knepley geom.detJ = detJ; 11000f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr); 11010f2d7e86SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 11020f2d7e86SMatthew G. Knepley geom.n = &n[offset*dim]; 11030f2d7e86SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 11040f2d7e86SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 11050f2d7e86SMatthew G. Knepley geom.detJ = &detJ[offset]; 11060f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr); 11070f2d7e86SMatthew G. Knepley } 11080f2d7e86SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 11090f2d7e86SMatthew G. Knepley const PetscInt point = points[p]; 11100f2d7e86SMatthew G. Knepley 11110f2d7e86SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 11120f2d7e86SMatthew G. Knepley if (dep != dim-1) continue; 11130f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);} 11140f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr); 11150f2d7e86SMatthew G. Knepley ++f; 11160f2d7e86SMatthew G. Knepley } 11170f2d7e86SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 11180f2d7e86SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 11190f2d7e86SMatthew G. Knepley ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr); 11200f2d7e86SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 11210f2d7e86SMatthew G. Knepley } 11220f2d7e86SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);} 11230f2d7e86SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 11240f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 11250f2d7e86SMatthew G. Knepley } 11260f2d7e86SMatthew G. Knepley 11270f2d7e86SMatthew G. Knepley #undef __FUNCT__ 112808f11eefSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check" 112908f11eefSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check(DM dm, Vec X, Vec X_t, Vec F, void *user) 113008f11eefSMatthew G. Knepley { 113108f11eefSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 113208f11eefSMatthew G. Knepley DM dmCh, dmAux; 113308f11eefSMatthew G. Knepley Vec A; 1134*6f4eac71SMatthew G. Knepley PetscDS prob, probCh, probAux = NULL; 113508f11eefSMatthew G. Knepley PetscQuadrature q; 113608f11eefSMatthew G. Knepley PetscCellGeometry geom; 113708f11eefSMatthew G. Knepley PetscSection section, sectionAux; 113808f11eefSMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 113908f11eefSMatthew G. Knepley PetscScalar *elemVec, *elemVecCh, *u, *u_t, *a = NULL; 114008f11eefSMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, c; 114108f11eefSMatthew G. Knepley PetscInt totDim, totDimAux, diffCell = 0; 114208f11eefSMatthew G. Knepley PetscErrorCode ierr; 114308f11eefSMatthew G. Knepley 114408f11eefSMatthew G. Knepley PetscFunctionBegin; 114508f11eefSMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 114608f11eefSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1147*6f4eac71SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 1148*6f4eac71SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 114908f11eefSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 115008f11eefSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 115108f11eefSMatthew G. Knepley numCells = cEnd - cStart; 115208f11eefSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr); 1153*6f4eac71SMatthew G. Knepley ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr); 115408f11eefSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 115508f11eefSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 115608f11eefSMatthew G. Knepley if (dmAux) { 115708f11eefSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1158*6f4eac71SMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 1159*6f4eac71SMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 116008f11eefSMatthew G. Knepley } 116108f11eefSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 116208f11eefSMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 116308f11eefSMatthew G. Knepley ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr); 116408f11eefSMatthew G. Knepley ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr); 116508f11eefSMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 116608f11eefSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 116708f11eefSMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 116808f11eefSMatthew G. Knepley PetscInt i; 116908f11eefSMatthew G. Knepley 117008f11eefSMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 117108f11eefSMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 117208f11eefSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 117308f11eefSMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 117408f11eefSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 117508f11eefSMatthew G. Knepley if (X_t) { 117608f11eefSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 117708f11eefSMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 117808f11eefSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 117908f11eefSMatthew G. Knepley } 118008f11eefSMatthew G. Knepley if (dmAux) { 118108f11eefSMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 118208f11eefSMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 118308f11eefSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 118408f11eefSMatthew G. Knepley } 118508f11eefSMatthew G. Knepley } 118608f11eefSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 118708f11eefSMatthew G. Knepley PetscFE fe, feCh; 118808f11eefSMatthew G. Knepley PetscInt numQuadPoints, Nb; 118908f11eefSMatthew G. Knepley /* Conforming batches */ 119008f11eefSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 119108f11eefSMatthew G. Knepley /* Remainder */ 119208f11eefSMatthew G. Knepley PetscInt Nr, offset; 119308f11eefSMatthew G. Knepley 1194*6f4eac71SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 1195*6f4eac71SMatthew G. Knepley ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr); 119608f11eefSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 119708f11eefSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 119808f11eefSMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 119908f11eefSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 120008f11eefSMatthew G. Knepley blockSize = Nb*numQuadPoints; 120108f11eefSMatthew G. Knepley batchSize = numBlocks * blockSize; 120208f11eefSMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 120308f11eefSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 120408f11eefSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 120508f11eefSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 120608f11eefSMatthew G. Knepley offset = numCells - Nr; 120708f11eefSMatthew G. Knepley geom.v0 = v0; 120808f11eefSMatthew G. Knepley geom.J = J; 120908f11eefSMatthew G. Knepley geom.invJ = invJ; 121008f11eefSMatthew G. Knepley geom.detJ = detJ; 121108f11eefSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr); 121208f11eefSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, geom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr); 121308f11eefSMatthew G. Knepley geom.v0 = &v0[offset*dim]; 121408f11eefSMatthew G. Knepley geom.J = &J[offset*dim*dim]; 121508f11eefSMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 121608f11eefSMatthew G. Knepley geom.detJ = &detJ[offset]; 121708f11eefSMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr); 121808f11eefSMatthew G. Knepley ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr); 121908f11eefSMatthew G. Knepley } 122008f11eefSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 122108f11eefSMatthew G. Knepley PetscBool diff = PETSC_FALSE; 122208f11eefSMatthew G. Knepley PetscInt d; 122308f11eefSMatthew G. Knepley 122408f11eefSMatthew G. Knepley for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;} 122508f11eefSMatthew G. Knepley if (diff) { 122608f11eefSMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr); 122708f11eefSMatthew G. Knepley ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr); 122808f11eefSMatthew G. Knepley ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr); 122908f11eefSMatthew G. Knepley ++diffCell; 123008f11eefSMatthew G. Knepley } 123108f11eefSMatthew G. Knepley if (diffCell > 9) break; 123208f11eefSMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr); 123308f11eefSMatthew G. Knepley } 123408f11eefSMatthew G. Knepley ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 123508f11eefSMatthew G. Knepley ierr = PetscFree(elemVecCh);CHKERRQ(ierr); 123608f11eefSMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 123708f11eefSMatthew G. Knepley PetscFunctionReturn(0); 123808f11eefSMatthew G. Knepley } 123908f11eefSMatthew G. Knepley 124008f11eefSMatthew G. Knepley #undef __FUNCT__ 12410f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM" 1242a0845e3aSMatthew G. Knepley /*@ 12430f2d7e86SMatthew G. Knepley DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 1244a0845e3aSMatthew G. Knepley 1245a0845e3aSMatthew G. Knepley Input Parameters: 1246a0845e3aSMatthew G. Knepley + dm - The mesh 12470f2d7e86SMatthew G. Knepley . X - Local solution 1248a0845e3aSMatthew G. Knepley - user - The user context 1249a0845e3aSMatthew G. Knepley 1250a0845e3aSMatthew G. Knepley Output Parameter: 1251a0845e3aSMatthew G. Knepley . F - Local output vector 1252a0845e3aSMatthew G. Knepley 1253a0845e3aSMatthew G. Knepley Level: developer 1254a0845e3aSMatthew G. Knepley 1255a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 1256a0845e3aSMatthew G. Knepley @*/ 12570f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 1258a0845e3aSMatthew G. Knepley { 125908f11eefSMatthew G. Knepley PetscObject check; 1260a0845e3aSMatthew G. Knepley PetscErrorCode ierr; 1261a0845e3aSMatthew G. Knepley 1262a0845e3aSMatthew G. Knepley PetscFunctionBegin; 126308f11eefSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr); 126408f11eefSMatthew G. Knepley if (check) {ierr = DMPlexComputeResidualFEM_Check(dm, X, NULL, F, user);CHKERRQ(ierr);} 126508f11eefSMatthew G. Knepley else {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);} 12660f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 12670f2d7e86SMatthew G. Knepley } 12680f2d7e86SMatthew G. Knepley 12690f2d7e86SMatthew G. Knepley #undef __FUNCT__ 1270adbe6fbbSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM" 1271adbe6fbbSMatthew G. Knepley /*@ 1272adbe6fbbSMatthew G. Knepley DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user 1273adbe6fbbSMatthew G. Knepley 1274adbe6fbbSMatthew G. Knepley Input Parameters: 1275adbe6fbbSMatthew G. Knepley + dm - The mesh 1276adbe6fbbSMatthew G. Knepley . t - The time 1277adbe6fbbSMatthew G. Knepley . X - Local solution 1278adbe6fbbSMatthew G. Knepley . X_t - Local solution time derivative, or NULL 1279adbe6fbbSMatthew G. Knepley - user - The user context 1280adbe6fbbSMatthew G. Knepley 1281adbe6fbbSMatthew G. Knepley Output Parameter: 1282adbe6fbbSMatthew G. Knepley . F - Local output vector 1283adbe6fbbSMatthew G. Knepley 1284adbe6fbbSMatthew G. Knepley Level: developer 1285adbe6fbbSMatthew G. Knepley 1286adbe6fbbSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 1287adbe6fbbSMatthew G. Knepley @*/ 1288adbe6fbbSMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec X, Vec X_t, Vec F, void *user) 1289adbe6fbbSMatthew G. Knepley { 1290adbe6fbbSMatthew G. Knepley PetscErrorCode ierr; 1291adbe6fbbSMatthew G. Knepley 1292adbe6fbbSMatthew G. Knepley PetscFunctionBegin; 1293adbe6fbbSMatthew G. Knepley ierr = DMPlexComputeResidualFEM_Internal(dm, X, X_t, F, user);CHKERRQ(ierr); 1294adbe6fbbSMatthew G. Knepley PetscFunctionReturn(0); 1295adbe6fbbSMatthew G. Knepley } 1296adbe6fbbSMatthew G. Knepley 1297adbe6fbbSMatthew G. Knepley #undef __FUNCT__ 12980f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal" 12990f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user) 13000f2d7e86SMatthew G. Knepley { 13010f2d7e86SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 13020f2d7e86SMatthew G. Knepley const char *name = "Jacobian"; 13030f2d7e86SMatthew G. Knepley DM dmAux; 13040f2d7e86SMatthew G. Knepley DMLabel depth; 13050f2d7e86SMatthew G. Knepley Vec A; 13062764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 13070f2d7e86SMatthew G. Knepley PetscQuadrature quad; 13080f2d7e86SMatthew G. Knepley PetscCellGeometry geom; 13090f2d7e86SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 13100f2d7e86SMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 13110f2d7e86SMatthew G. Knepley PetscScalar *elemMat, *u, *u_t, *a = NULL; 13120f2d7e86SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c; 13130f2d7e86SMatthew G. Knepley PetscInt totDim, totDimBd, totDimAux, numBd, bd; 13140f2d7e86SMatthew G. Knepley PetscBool isShell; 13150f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 13160f2d7e86SMatthew G. Knepley 13170f2d7e86SMatthew G. Knepley PetscFunctionBegin; 13180f2d7e86SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 1319a0845e3aSMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1320a0845e3aSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 13210f2d7e86SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 13222764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 13232764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 13242764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr); 13259a559087SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1326a0845e3aSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1327a0845e3aSMatthew G. Knepley numCells = cEnd - cStart; 13289a559087SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 13299a559087SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 13309a559087SMatthew G. Knepley if (dmAux) { 13319a559087SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 13322764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 13332764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 13349a559087SMatthew G. Knepley } 13353351dd3dSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 13360f2d7e86SMatthew G. Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 13370f2d7e86SMatthew G. Knepley ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr); 13380f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1339a0845e3aSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 13400f2d7e86SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 1341a0845e3aSMatthew G. Knepley PetscInt i; 1342a0845e3aSMatthew G. Knepley 1343a0845e3aSMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 1344a0845e3aSMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 1345a0845e3aSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 13460f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 1347a0845e3aSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 13480f2d7e86SMatthew G. Knepley if (X_t) { 13490f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 13500f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i]; 13510f2d7e86SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 13520f2d7e86SMatthew G. Knepley } 13539a559087SMatthew G. Knepley if (dmAux) { 13549a559087SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 13550f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 13569a559087SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 1357a0845e3aSMatthew G. Knepley } 13589a559087SMatthew G. Knepley } 13590f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr); 13600f2d7e86SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 13610f2d7e86SMatthew G. Knepley PetscFE fe; 136221454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1363a0845e3aSMatthew G. Knepley /* Conforming batches */ 1364f30c5766SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1365a0845e3aSMatthew G. Knepley /* Remainder */ 1366a0845e3aSMatthew G. Knepley PetscInt Nr, offset; 1367a0845e3aSMatthew G. Knepley 13682764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 13690f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 13700f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 13710f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 13720f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 137321454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 1374a0845e3aSMatthew G. Knepley batchSize = numBlocks * blockSize; 13750f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1376a0845e3aSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 1377a0845e3aSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1378a0845e3aSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 1379a0845e3aSMatthew G. Knepley offset = numCells - Nr; 13800f2d7e86SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 1381a0845e3aSMatthew G. Knepley geom.v0 = v0; 1382a0845e3aSMatthew G. Knepley geom.J = J; 1383a0845e3aSMatthew G. Knepley geom.invJ = invJ; 1384a0845e3aSMatthew G. Knepley geom.detJ = detJ; 13850f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr); 1386a0845e3aSMatthew G. Knepley geom.v0 = &v0[offset*dim]; 1387a0845e3aSMatthew G. Knepley geom.J = &J[offset*dim*dim]; 1388a0845e3aSMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 1389a0845e3aSMatthew G. Knepley geom.detJ = &detJ[offset]; 13900f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr); 13910f2d7e86SMatthew G. Knepley } 1392a0845e3aSMatthew G. Knepley } 1393a0845e3aSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 13940f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);} 13950f2d7e86SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr); 1396a0845e3aSMatthew G. Knepley } 13970f2d7e86SMatthew G. Knepley ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr); 13989a559087SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 13990f2d7e86SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 14000f2d7e86SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 14011c093863SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 14021c093863SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 14031c093863SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 14041c093863SMatthew G. Knepley const char *bdLabel; 14051c093863SMatthew G. Knepley DMLabel label; 14061c093863SMatthew G. Knepley IS pointIS; 14071c093863SMatthew G. Knepley const PetscInt *points; 14081c093863SMatthew G. Knepley const PetscInt *values; 14091c093863SMatthew G. Knepley PetscReal *n; 14101c093863SMatthew G. Knepley PetscInt field, numValues, numPoints, p, dep, numFaces; 14111c093863SMatthew G. Knepley PetscBool isEssential; 14121c093863SMatthew G. Knepley 141363d5297fSMatthew G. Knepley ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr); 14140f2d7e86SMatthew G. Knepley if (isEssential) continue; 14151c093863SMatthew G. Knepley if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this"); 14161c093863SMatthew G. Knepley ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr); 14171c093863SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 14181c093863SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 14191c093863SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 1420075da914SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 1421075da914SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1422075da914SMatthew G. Knepley if (dep == dim-1) ++numFaces; 1423075da914SMatthew G. Knepley } 14240f2d7e86SMatthew G. Knepley ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr); 14250f2d7e86SMatthew G. Knepley if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);} 1426075da914SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 1427f1ea0e2fSMatthew G. Knepley const PetscInt point = points[p]; 1428f1ea0e2fSMatthew G. Knepley PetscScalar *x = NULL; 1429f1ea0e2fSMatthew G. Knepley PetscInt i; 1430f1ea0e2fSMatthew G. Knepley 1431075da914SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1432075da914SMatthew G. Knepley if (dep != dim-1) continue; 1433075da914SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr); 1434a8007bbfSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]); 1435075da914SMatthew G. Knepley if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point); 1436f1ea0e2fSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 14370f2d7e86SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i]; 1438f1ea0e2fSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 14390f2d7e86SMatthew G. Knepley if (X_t) { 1440af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 14410f2d7e86SMatthew G. Knepley for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i]; 1442af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 14430f2d7e86SMatthew G. Knepley } 1444af1eca97SMatthew G. Knepley ++f; 1445af1eca97SMatthew G. Knepley } 14460f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr); 14470f2d7e86SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 14480f2d7e86SMatthew G. Knepley PetscFE fe; 1449af1eca97SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1450af1eca97SMatthew G. Knepley /* Conforming batches */ 1451af1eca97SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1452af1eca97SMatthew G. Knepley /* Remainder */ 1453af1eca97SMatthew G. Knepley PetscInt Nr, offset; 1454af1eca97SMatthew G. Knepley 14552764a2aaSMatthew G. Knepley ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr); 14560f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 14570f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 14580f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 145921454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 146021454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 14610483ade4SMatthew G. Knepley batchSize = numBlocks * blockSize; 14620f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 14630f2d7e86SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 14640483ade4SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 14650f2d7e86SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 14660f2d7e86SMatthew G. Knepley offset = numFaces - Nr; 14670f2d7e86SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 14680483ade4SMatthew G. Knepley geom.v0 = v0; 14690f2d7e86SMatthew G. Knepley geom.n = n; 14700483ade4SMatthew G. Knepley geom.J = J; 14710483ade4SMatthew G. Knepley geom.invJ = invJ; 14720483ade4SMatthew G. Knepley geom.detJ = detJ; 14730f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr); 14740483ade4SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 14750f2d7e86SMatthew G. Knepley geom.n = &n[offset*dim]; 14760483ade4SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 14770483ade4SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 14780483ade4SMatthew G. Knepley geom.detJ = &detJ[offset]; 14790f2d7e86SMatthew G. Knepley ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr); 1480cb1e1211SMatthew G Knepley } 1481cb1e1211SMatthew G Knepley } 14820f2d7e86SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 14830f2d7e86SMatthew G. Knepley const PetscInt point = points[p]; 1484cb1e1211SMatthew G Knepley 14850f2d7e86SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 14860f2d7e86SMatthew G. Knepley if (dep != dim-1) continue; 14870f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);} 14880f2d7e86SMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr); 14890f2d7e86SMatthew G. Knepley ++f; 1490cb1e1211SMatthew G Knepley } 14910f2d7e86SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 14920f2d7e86SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 14930f2d7e86SMatthew G. Knepley ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr); 14940f2d7e86SMatthew G. Knepley if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);} 1495cb1e1211SMatthew G Knepley } 14960f2d7e86SMatthew G. Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14970f2d7e86SMatthew G. Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14980f2d7e86SMatthew G. Knepley if (mesh->printFEM) { 14990f2d7e86SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 15000f2d7e86SMatthew G. Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 15010f2d7e86SMatthew G. Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 15020f2d7e86SMatthew G. Knepley } 15030f2d7e86SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 15040f2d7e86SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 15050f2d7e86SMatthew G. Knepley if (isShell) { 15060f2d7e86SMatthew G. Knepley JacActionCtx *jctx; 15070f2d7e86SMatthew G. Knepley 15080f2d7e86SMatthew G. Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 15090f2d7e86SMatthew G. Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 15100f2d7e86SMatthew G. Knepley } 1511cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1512cb1e1211SMatthew G Knepley } 1513cb1e1211SMatthew G Knepley 1514cb1e1211SMatthew G Knepley #undef __FUNCT__ 15150f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM" 1516cb1e1211SMatthew G Knepley /*@ 15170f2d7e86SMatthew G. Knepley DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 1518cb1e1211SMatthew G Knepley 1519cb1e1211SMatthew G Knepley Input Parameters: 1520cb1e1211SMatthew G Knepley + dm - The mesh 1521cb1e1211SMatthew G Knepley . X - Local input vector 1522cb1e1211SMatthew G Knepley - user - The user context 1523cb1e1211SMatthew G Knepley 1524cb1e1211SMatthew G Knepley Output Parameter: 1525cb1e1211SMatthew G Knepley . Jac - Jacobian matrix 1526cb1e1211SMatthew G Knepley 1527cb1e1211SMatthew G Knepley Note: 15288026896cSMatthew G. Knepley The first member of the user context must be an FEMContext. 1529cb1e1211SMatthew G Knepley 1530cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1531cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 1532cb1e1211SMatthew G Knepley 15330059ad2aSSatish Balay Level: developer 15340059ad2aSSatish Balay 1535cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal() 1536878cb397SSatish Balay @*/ 15370f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 1538cb1e1211SMatthew G Knepley { 1539cb1e1211SMatthew G Knepley PetscErrorCode ierr; 1540cb1e1211SMatthew G Knepley 1541cb1e1211SMatthew G Knepley PetscFunctionBegin; 15420f2d7e86SMatthew G. Knepley ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr); 1543cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1544cb1e1211SMatthew G Knepley } 1545bceba477SMatthew G. Knepley 1546d69c5d34SMatthew G. Knepley #undef __FUNCT__ 1547d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM" 1548d69c5d34SMatthew G. Knepley /*@ 1549d69c5d34SMatthew G. Knepley DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1550d69c5d34SMatthew G. Knepley 1551d69c5d34SMatthew G. Knepley Input Parameters: 1552d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1553d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1554d69c5d34SMatthew G. Knepley - user - The user context 1555d69c5d34SMatthew G. Knepley 1556d69c5d34SMatthew G. Knepley Output Parameter: 1557934789fcSMatthew G. Knepley . In - The interpolation matrix 1558d69c5d34SMatthew G. Knepley 1559d69c5d34SMatthew G. Knepley Note: 1560d69c5d34SMatthew G. Knepley The first member of the user context must be an FEMContext. 1561d69c5d34SMatthew G. Knepley 1562d69c5d34SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1563d69c5d34SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 1564d69c5d34SMatthew G. Knepley 1565d69c5d34SMatthew G. Knepley Level: developer 1566d69c5d34SMatthew G. Knepley 1567d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM() 1568d69c5d34SMatthew G. Knepley @*/ 1569934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user) 1570d69c5d34SMatthew G. Knepley { 1571d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1572d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 15732764a2aaSMatthew G. Knepley PetscDS prob; 1574d69c5d34SMatthew G. Knepley PetscFE *feRef; 1575d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1576d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1577d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 1578942a7a06SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c; 15790f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1580d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1581d69c5d34SMatthew G. Knepley 1582d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1583d69c5d34SMatthew G. Knepley #if 0 1584d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1585d69c5d34SMatthew G. Knepley #endif 1586d69c5d34SMatthew G. Knepley ierr = DMPlexGetDimension(dmf, &dim);CHKERRQ(ierr); 1587d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1588d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1589d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1590d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1591d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1592d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 15932764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 1594d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr); 1595d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 15960f2d7e86SMatthew G. Knepley PetscFE fe; 15970f2d7e86SMatthew G. Knepley PetscInt rNb, Nc; 1598d69c5d34SMatthew G. Knepley 15992764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr); 16000f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1601d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 16020f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 16030f2d7e86SMatthew G. Knepley rTotDim += rNb*Nc; 1604d69c5d34SMatthew G. Knepley } 16052764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 1606934789fcSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 16070f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 16080f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1609d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1610d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1611d69c5d34SMatthew G. Knepley PetscQuadrature f; 1612d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1613d69c5d34SMatthew G. Knepley PetscReal *points; 1614d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1615d69c5d34SMatthew G. Knepley 1616d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 1617d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 16180f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 1619d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1620d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1621d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1622d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1623d69c5d34SMatthew G. Knepley npoints += Np; 1624d69c5d34SMatthew G. Knepley } 1625d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1626d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1627d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1628d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1629d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1630d69c5d34SMatthew G. Knepley } 1631d69c5d34SMatthew G. Knepley 1632d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 16330f2d7e86SMatthew G. Knepley PetscFE fe; 1634d69c5d34SMatthew G. Knepley PetscReal *B; 163536a6d9c0SMatthew G. Knepley PetscInt NcJ, cpdim, j; 1636d69c5d34SMatthew G. Knepley 1637d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 16382764a2aaSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &fe);CHKERRQ(ierr); 16390f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 164036a6d9c0SMatthew 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); 16410f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1642ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1643ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 16440f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1645d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1646d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1647d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1648d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 164936a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 16500f2d7e86SMatthew 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]; 165136a6d9c0SMatthew G. Knepley } 1652d69c5d34SMatthew G. Knepley } 1653d69c5d34SMatthew G. Knepley } 16540f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1655ffe73a53SMatthew G. Knepley } 165636a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1657d69c5d34SMatthew G. Knepley } 1658d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1659549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1660d69c5d34SMatthew G. Knepley } 16610f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 1662d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1663934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1664d69c5d34SMatthew G. Knepley } 1665549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 1666d69c5d34SMatthew G. Knepley ierr = PetscFree(feRef);CHKERRQ(ierr); 1667549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1668934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1669934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1670d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1671d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1672934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1673934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1674d69c5d34SMatthew G. Knepley } 1675d69c5d34SMatthew G. Knepley #if 0 1676d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1677d69c5d34SMatthew G. Knepley #endif 1678d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1679d69c5d34SMatthew G. Knepley } 16806c73c22cSMatthew G. Knepley 16816c73c22cSMatthew G. Knepley #undef __FUNCT__ 16828e136ac0SMatthew G. Knepley #define __FUNCT__ "BoundaryDuplicate" 16838e136ac0SMatthew G. Knepley static PetscErrorCode BoundaryDuplicate(DMBoundary bd, DMBoundary *boundary) 16848e136ac0SMatthew G. Knepley { 16858e136ac0SMatthew G. Knepley DMBoundary b = bd, b2, bold = NULL; 16868e136ac0SMatthew G. Knepley PetscErrorCode ierr; 16878e136ac0SMatthew G. Knepley 16888e136ac0SMatthew G. Knepley PetscFunctionBegin; 16898e136ac0SMatthew G. Knepley *boundary = NULL; 16908e136ac0SMatthew G. Knepley for (; b; b = b->next, bold = b2) { 16918e136ac0SMatthew G. Knepley ierr = PetscNew(&b2);CHKERRQ(ierr); 16928e136ac0SMatthew G. Knepley ierr = PetscStrallocpy(b->name, (char **) &b2->name);CHKERRQ(ierr); 16938e136ac0SMatthew G. Knepley ierr = PetscStrallocpy(b->labelname, (char **) &b2->labelname);CHKERRQ(ierr); 16948e136ac0SMatthew G. Knepley ierr = PetscMalloc1(b->numids, &b2->ids);CHKERRQ(ierr); 16958e136ac0SMatthew G. Knepley ierr = PetscMemcpy(b2->ids, b->ids, b->numids*sizeof(PetscInt));CHKERRQ(ierr); 16968e136ac0SMatthew G. Knepley b2->label = NULL; 16978e136ac0SMatthew G. Knepley b2->essential = b->essential; 16988e136ac0SMatthew G. Knepley b2->field = b->field; 16998e136ac0SMatthew G. Knepley b2->func = b->func; 17008e136ac0SMatthew G. Knepley b2->numids = b->numids; 17018e136ac0SMatthew G. Knepley b2->ctx = b->ctx; 17028e136ac0SMatthew G. Knepley b2->next = NULL; 17038e136ac0SMatthew G. Knepley if (!*boundary) *boundary = b2; 17048e136ac0SMatthew G. Knepley if (bold) bold->next = b2; 17058e136ac0SMatthew G. Knepley } 17068e136ac0SMatthew G. Knepley PetscFunctionReturn(0); 17078e136ac0SMatthew G. Knepley } 17088e136ac0SMatthew G. Knepley 17098e136ac0SMatthew G. Knepley #undef __FUNCT__ 17108e136ac0SMatthew G. Knepley #define __FUNCT__ "DMPlexCopyBoundary" 17118e136ac0SMatthew G. Knepley PetscErrorCode DMPlexCopyBoundary(DM dm, DM dmNew) 17128e136ac0SMatthew G. Knepley { 17138e136ac0SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 17148e136ac0SMatthew G. Knepley DM_Plex *meshNew = (DM_Plex *) dmNew->data; 17158e136ac0SMatthew G. Knepley DMBoundary b; 17168e136ac0SMatthew G. Knepley PetscErrorCode ierr; 17178e136ac0SMatthew G. Knepley 17188e136ac0SMatthew G. Knepley PetscFunctionBegin; 17198e136ac0SMatthew G. Knepley ierr = BoundaryDuplicate(mesh->boundary, &meshNew->boundary);CHKERRQ(ierr); 17208e136ac0SMatthew G. Knepley for (b = meshNew->boundary; b; b = b->next) { 17218e136ac0SMatthew G. Knepley if (b->labelname) { 17228e136ac0SMatthew G. Knepley ierr = DMPlexGetLabel(dmNew, b->labelname, &b->label);CHKERRQ(ierr); 17238e136ac0SMatthew G. Knepley if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname); 17248e136ac0SMatthew G. Knepley } 17258e136ac0SMatthew G. Knepley } 17268e136ac0SMatthew G. Knepley PetscFunctionReturn(0); 17278e136ac0SMatthew G. Knepley } 17288e136ac0SMatthew G. Knepley 17298e136ac0SMatthew G. Knepley #undef __FUNCT__ 17306c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexAddBoundary" 17316c73c22cSMatthew G. Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */ 173263d5297fSMatthew G. Knepley PetscErrorCode DMPlexAddBoundary(DM dm, PetscBool isEssential, const char name[], const char labelname[], PetscInt field, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx) 17336c73c22cSMatthew G. Knepley { 17346c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 17356c73c22cSMatthew G. Knepley DMBoundary b; 17366c73c22cSMatthew G. Knepley PetscErrorCode ierr; 17376c73c22cSMatthew G. Knepley 17386c73c22cSMatthew G. Knepley PetscFunctionBegin; 173963d5297fSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17406c73c22cSMatthew G. Knepley ierr = PetscNew(&b);CHKERRQ(ierr); 17416c73c22cSMatthew G. Knepley ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr); 174263d5297fSMatthew G. Knepley ierr = PetscStrallocpy(labelname, (char **) &b->labelname);CHKERRQ(ierr); 17436c73c22cSMatthew G. Knepley ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr); 17446c73c22cSMatthew G. Knepley ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr); 174563d5297fSMatthew G. Knepley if (b->labelname) { 174663d5297fSMatthew G. Knepley ierr = DMPlexGetLabel(dm, b->labelname, &b->label);CHKERRQ(ierr); 174763d5297fSMatthew G. Knepley if (!b->label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s does not exist in this DM", b->labelname); 174863d5297fSMatthew G. Knepley } 17496c73c22cSMatthew G. Knepley b->essential = isEssential; 17506c73c22cSMatthew G. Knepley b->field = field; 17516c73c22cSMatthew G. Knepley b->func = bcFunc; 17526c73c22cSMatthew G. Knepley b->numids = numids; 17536c73c22cSMatthew G. Knepley b->ctx = ctx; 17546c73c22cSMatthew G. Knepley b->next = mesh->boundary; 17556c73c22cSMatthew G. Knepley mesh->boundary = b; 17566c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 17576c73c22cSMatthew G. Knepley } 17586c73c22cSMatthew G. Knepley 17596c73c22cSMatthew G. Knepley #undef __FUNCT__ 17606c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumBoundary" 17616c73c22cSMatthew G. Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd) 17626c73c22cSMatthew G. Knepley { 17636c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 17646c73c22cSMatthew G. Knepley DMBoundary b = mesh->boundary; 17656c73c22cSMatthew G. Knepley 17666c73c22cSMatthew G. Knepley PetscFunctionBegin; 176763d5297fSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 176863d5297fSMatthew G. Knepley PetscValidPointer(numBd, 2); 17696c73c22cSMatthew G. Knepley *numBd = 0; 17706c73c22cSMatthew G. Knepley while (b) {++(*numBd); b = b->next;} 17716c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 17726c73c22cSMatthew G. Knepley } 17736c73c22cSMatthew G. Knepley 17746c73c22cSMatthew G. Knepley #undef __FUNCT__ 17756c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetBoundary" 177663d5297fSMatthew G. Knepley PetscErrorCode DMPlexGetBoundary(DM dm, PetscInt bd, PetscBool *isEssential, const char **name, const char **labelname, PetscInt *field, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx) 17776c73c22cSMatthew G. Knepley { 17786c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 17796c73c22cSMatthew G. Knepley DMBoundary b = mesh->boundary; 17806c73c22cSMatthew G. Knepley PetscInt n = 0; 17816c73c22cSMatthew G. Knepley 17826c73c22cSMatthew G. Knepley PetscFunctionBegin; 178363d5297fSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17846c73c22cSMatthew G. Knepley while (b) { 17856c73c22cSMatthew G. Knepley if (n == bd) break; 17866c73c22cSMatthew G. Knepley b = b->next; 17876c73c22cSMatthew G. Knepley ++n; 17886c73c22cSMatthew G. Knepley } 17896c73c22cSMatthew G. Knepley if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n); 17906c73c22cSMatthew G. Knepley if (isEssential) { 17916c73c22cSMatthew G. Knepley PetscValidPointer(isEssential, 3); 17926c73c22cSMatthew G. Knepley *isEssential = b->essential; 17936c73c22cSMatthew G. Knepley } 17946c73c22cSMatthew G. Knepley if (name) { 17956c73c22cSMatthew G. Knepley PetscValidPointer(name, 4); 17966c73c22cSMatthew G. Knepley *name = b->name; 17976c73c22cSMatthew G. Knepley } 179863d5297fSMatthew G. Knepley if (labelname) { 179963d5297fSMatthew G. Knepley PetscValidPointer(labelname, 5); 180063d5297fSMatthew G. Knepley *labelname = b->labelname; 180163d5297fSMatthew G. Knepley } 18026c73c22cSMatthew G. Knepley if (field) { 180363d5297fSMatthew G. Knepley PetscValidPointer(field, 6); 18046c73c22cSMatthew G. Knepley *field = b->field; 18056c73c22cSMatthew G. Knepley } 18066c73c22cSMatthew G. Knepley if (func) { 180763d5297fSMatthew G. Knepley PetscValidPointer(func, 7); 18086c73c22cSMatthew G. Knepley *func = b->func; 18096c73c22cSMatthew G. Knepley } 18106c73c22cSMatthew G. Knepley if (numids) { 181163d5297fSMatthew G. Knepley PetscValidPointer(numids, 8); 18126c73c22cSMatthew G. Knepley *numids = b->numids; 18136c73c22cSMatthew G. Knepley } 18146c73c22cSMatthew G. Knepley if (ids) { 181563d5297fSMatthew G. Knepley PetscValidPointer(ids, 9); 18166c73c22cSMatthew G. Knepley *ids = b->ids; 18176c73c22cSMatthew G. Knepley } 18186c73c22cSMatthew G. Knepley if (ctx) { 181963d5297fSMatthew G. Knepley PetscValidPointer(ctx, 10); 18206c73c22cSMatthew G. Knepley *ctx = b->ctx; 18216c73c22cSMatthew G. Knepley } 18226c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 18236c73c22cSMatthew G. Knepley } 18240225b034SMatthew G. Knepley 18250225b034SMatthew G. Knepley #undef __FUNCT__ 18260225b034SMatthew G. Knepley #define __FUNCT__ "DMPlexIsBoundaryPoint" 18270225b034SMatthew G. Knepley PetscErrorCode DMPlexIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd) 18280225b034SMatthew G. Knepley { 18290225b034SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 18300225b034SMatthew G. Knepley DMBoundary b = mesh->boundary; 18310225b034SMatthew G. Knepley PetscErrorCode ierr; 18320225b034SMatthew G. Knepley 18330225b034SMatthew G. Knepley PetscFunctionBegin; 18340225b034SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 18350225b034SMatthew G. Knepley PetscValidPointer(isBd, 3); 18360225b034SMatthew G. Knepley *isBd = PETSC_FALSE; 18370225b034SMatthew G. Knepley while (b && !(*isBd)) { 18380225b034SMatthew G. Knepley if (b->label) { 18390225b034SMatthew G. Knepley PetscInt i; 18400225b034SMatthew G. Knepley 18410225b034SMatthew G. Knepley for (i = 0; i < b->numids && !(*isBd); ++i) { 18420225b034SMatthew G. Knepley ierr = DMLabelStratumHasPoint(b->label, b->ids[i], point, isBd);CHKERRQ(ierr); 18430225b034SMatthew G. Knepley } 18440225b034SMatthew G. Knepley } 18450225b034SMatthew G. Knepley b = b->next; 18460225b034SMatthew G. Knepley } 18470225b034SMatthew G. Knepley PetscFunctionReturn(0); 18480225b034SMatthew G. Knepley } 1849