1cb1e1211SMatthew G Knepley #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2cb1e1211SMatthew G Knepley 3a0845e3aSMatthew G. Knepley #include <petscfe.h> 4a0845e3aSMatthew G. Knepley 5cb1e1211SMatthew G Knepley #undef __FUNCT__ 6cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale" 7cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 8cb1e1211SMatthew G Knepley { 9cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 10cb1e1211SMatthew G Knepley 11cb1e1211SMatthew G Knepley PetscFunctionBegin; 12cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 13cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 14cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 15cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 16cb1e1211SMatthew G Knepley } 17cb1e1211SMatthew G Knepley 18cb1e1211SMatthew G Knepley #undef __FUNCT__ 19cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale" 20cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 21cb1e1211SMatthew G Knepley { 22cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 23cb1e1211SMatthew G Knepley 24cb1e1211SMatthew G Knepley PetscFunctionBegin; 25cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 26cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 27cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 28cb1e1211SMatthew G Knepley } 29cb1e1211SMatthew G Knepley 30cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k) 31cb1e1211SMatthew G Knepley { 32cb1e1211SMatthew G Knepley switch (i) { 33cb1e1211SMatthew G Knepley case 0: 34cb1e1211SMatthew G Knepley switch (j) { 35cb1e1211SMatthew G Knepley case 0: return 0; 36cb1e1211SMatthew G Knepley case 1: 37cb1e1211SMatthew G Knepley switch (k) { 38cb1e1211SMatthew G Knepley case 0: return 0; 39cb1e1211SMatthew G Knepley case 1: return 0; 40cb1e1211SMatthew G Knepley case 2: return 1; 41cb1e1211SMatthew G Knepley } 42cb1e1211SMatthew G Knepley case 2: 43cb1e1211SMatthew G Knepley switch (k) { 44cb1e1211SMatthew G Knepley case 0: return 0; 45cb1e1211SMatthew G Knepley case 1: return -1; 46cb1e1211SMatthew G Knepley case 2: return 0; 47cb1e1211SMatthew G Knepley } 48cb1e1211SMatthew G Knepley } 49cb1e1211SMatthew G Knepley case 1: 50cb1e1211SMatthew G Knepley switch (j) { 51cb1e1211SMatthew G Knepley case 0: 52cb1e1211SMatthew G Knepley switch (k) { 53cb1e1211SMatthew G Knepley case 0: return 0; 54cb1e1211SMatthew G Knepley case 1: return 0; 55cb1e1211SMatthew G Knepley case 2: return -1; 56cb1e1211SMatthew G Knepley } 57cb1e1211SMatthew G Knepley case 1: return 0; 58cb1e1211SMatthew G Knepley case 2: 59cb1e1211SMatthew G Knepley switch (k) { 60cb1e1211SMatthew G Knepley case 0: return 1; 61cb1e1211SMatthew G Knepley case 1: return 0; 62cb1e1211SMatthew G Knepley case 2: return 0; 63cb1e1211SMatthew G Knepley } 64cb1e1211SMatthew G Knepley } 65cb1e1211SMatthew G Knepley case 2: 66cb1e1211SMatthew G Knepley switch (j) { 67cb1e1211SMatthew G Knepley case 0: 68cb1e1211SMatthew G Knepley switch (k) { 69cb1e1211SMatthew G Knepley case 0: return 0; 70cb1e1211SMatthew G Knepley case 1: return 1; 71cb1e1211SMatthew G Knepley case 2: return 0; 72cb1e1211SMatthew G Knepley } 73cb1e1211SMatthew G Knepley case 1: 74cb1e1211SMatthew G Knepley switch (k) { 75cb1e1211SMatthew G Knepley case 0: return -1; 76cb1e1211SMatthew G Knepley case 1: return 0; 77cb1e1211SMatthew G Knepley case 2: return 0; 78cb1e1211SMatthew G Knepley } 79cb1e1211SMatthew G Knepley case 2: return 0; 80cb1e1211SMatthew G Knepley } 81cb1e1211SMatthew G Knepley } 82cb1e1211SMatthew G Knepley return 0; 83cb1e1211SMatthew G Knepley } 84cb1e1211SMatthew G Knepley 85cb1e1211SMatthew G Knepley #undef __FUNCT__ 86cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 87cb1e1211SMatthew G Knepley /*@C 88cb1e1211SMatthew G Knepley DMPlexCreateRigidBody - create rigid body modes from coordinates 89cb1e1211SMatthew G Knepley 90cb1e1211SMatthew G Knepley Collective on DM 91cb1e1211SMatthew G Knepley 92cb1e1211SMatthew G Knepley Input Arguments: 93cb1e1211SMatthew G Knepley + dm - the DM 94cb1e1211SMatthew G Knepley . section - the local section associated with the rigid field, or NULL for the default section 95cb1e1211SMatthew G Knepley - globalSection - the global section associated with the rigid field, or NULL for the default section 96cb1e1211SMatthew G Knepley 97cb1e1211SMatthew G Knepley Output Argument: 98cb1e1211SMatthew G Knepley . sp - the null space 99cb1e1211SMatthew G Knepley 100cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 101cb1e1211SMatthew G Knepley 102cb1e1211SMatthew G Knepley Level: advanced 103cb1e1211SMatthew G Knepley 104cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 105cb1e1211SMatthew G Knepley @*/ 106cb1e1211SMatthew G Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscSection section, PetscSection globalSection, MatNullSpace *sp) 107cb1e1211SMatthew G Knepley { 108cb1e1211SMatthew G Knepley MPI_Comm comm; 109cb1e1211SMatthew G Knepley Vec coordinates, localMode, mode[6]; 110cb1e1211SMatthew G Knepley PetscSection coordSection; 111cb1e1211SMatthew G Knepley PetscScalar *coords; 112cb1e1211SMatthew G Knepley PetscInt dim, vStart, vEnd, v, n, m, d, i, j; 113cb1e1211SMatthew G Knepley PetscErrorCode ierr; 114cb1e1211SMatthew G Knepley 115cb1e1211SMatthew G Knepley PetscFunctionBegin; 116cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 117cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 118cb1e1211SMatthew G Knepley if (dim == 1) { 119cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 120cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 121cb1e1211SMatthew G Knepley } 122cb1e1211SMatthew G Knepley if (!section) {ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr);} 123cb1e1211SMatthew G Knepley if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);} 124cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 125cb1e1211SMatthew G Knepley ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 12669d8a9ceSMatthew G. Knepley ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 127cb1e1211SMatthew G Knepley ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 128cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 129cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 130cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 131cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 132cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 133cb1e1211SMatthew G Knepley /* Assume P1 */ 134cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localMode);CHKERRQ(ierr); 135cb1e1211SMatthew G Knepley for (d = 0; d < dim; ++d) { 136cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 137cb1e1211SMatthew G Knepley 138cb1e1211SMatthew G Knepley values[d] = 1.0; 139cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 140cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 141cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 142cb1e1211SMatthew G Knepley } 143cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 144cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 145cb1e1211SMatthew G Knepley } 146cb1e1211SMatthew G Knepley ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 147cb1e1211SMatthew G Knepley for (d = dim; d < dim*(dim+1)/2; ++d) { 148cb1e1211SMatthew G Knepley PetscInt i, j, k = dim > 2 ? d - dim : d; 149cb1e1211SMatthew G Knepley 150cb1e1211SMatthew G Knepley ierr = VecSet(localMode, 0.0);CHKERRQ(ierr); 151cb1e1211SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 152cb1e1211SMatthew G Knepley PetscScalar values[3] = {0.0, 0.0, 0.0}; 153cb1e1211SMatthew G Knepley PetscInt off; 154cb1e1211SMatthew G Knepley 155cb1e1211SMatthew G Knepley ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 156cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) { 157cb1e1211SMatthew G Knepley for (j = 0; j < dim; ++j) { 158cb1e1211SMatthew G Knepley values[j] += epsilon(i, j, k)*PetscRealPart(coords[off+i]); 159cb1e1211SMatthew G Knepley } 160cb1e1211SMatthew G Knepley } 161cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, section, localMode, v, values, INSERT_VALUES);CHKERRQ(ierr); 162cb1e1211SMatthew G Knepley } 163cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 164cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localMode, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 165cb1e1211SMatthew G Knepley } 166cb1e1211SMatthew G Knepley ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 167cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localMode);CHKERRQ(ierr); 168cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 169cb1e1211SMatthew G Knepley /* Orthonormalize system */ 170cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 171cb1e1211SMatthew G Knepley PetscScalar dots[6]; 172cb1e1211SMatthew G Knepley 173cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 174cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 175cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 176cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 177cb1e1211SMatthew G Knepley } 178cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 179cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 180cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 181cb1e1211SMatthew G Knepley } 182cb1e1211SMatthew G Knepley 183cb1e1211SMatthew G Knepley #undef __FUNCT__ 184a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal" 185a18a7fb9SMatthew 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) 186a18a7fb9SMatthew G. Knepley { 187a18a7fb9SMatthew G. Knepley PetscDualSpace *sp; 188a18a7fb9SMatthew G. Knepley PetscSection section; 189a18a7fb9SMatthew G. Knepley PetscScalar *values; 190a18a7fb9SMatthew G. Knepley PetscReal *v0, *J, detJ; 191a18a7fb9SMatthew G. Knepley PetscInt numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, f, d, v, i, comp; 192a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 193a18a7fb9SMatthew G. Knepley 194a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 195a18a7fb9SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 196a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 197a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 198a18a7fb9SMatthew G. Knepley ierr = PetscMalloc3(numFields,&sp,dim,&v0,dim*dim,&J);CHKERRQ(ierr); 199a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 200a18a7fb9SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr); 201a18a7fb9SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 202a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 203a18a7fb9SMatthew G. Knepley totDim += spDim*numComp; 204a18a7fb9SMatthew G. Knepley } 205a18a7fb9SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 206a18a7fb9SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 207a18a7fb9SMatthew 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); 208a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 209a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 210a18a7fb9SMatthew G. Knepley IS pointIS; 211a18a7fb9SMatthew G. Knepley const PetscInt *points; 212a18a7fb9SMatthew G. Knepley PetscInt n, p; 213a18a7fb9SMatthew G. Knepley 214a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 215a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 216a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 217a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 218a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 219a18a7fb9SMatthew G. Knepley PetscCellGeometry geom; 220a18a7fb9SMatthew G. Knepley 221a18a7fb9SMatthew G. Knepley if ((point < cStart) || (point >= cEnd)) continue; 222a18a7fb9SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, v0, J, NULL, &detJ);CHKERRQ(ierr); 223a18a7fb9SMatthew G. Knepley geom.v0 = v0; 224a18a7fb9SMatthew G. Knepley geom.J = J; 225a18a7fb9SMatthew G. Knepley geom.detJ = &detJ; 226a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 227a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 228a18a7fb9SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 229a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 230a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 231a18a7fb9SMatthew G. Knepley if (funcs[f]) { 232a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr); 233a18a7fb9SMatthew G. Knepley } else { 234a18a7fb9SMatthew G. Knepley for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0; 235a18a7fb9SMatthew G. Knepley } 236a18a7fb9SMatthew G. Knepley v += numComp; 237a18a7fb9SMatthew G. Knepley } 238a18a7fb9SMatthew G. Knepley } 239a18a7fb9SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, point, values, mode);CHKERRQ(ierr); 240a18a7fb9SMatthew G. Knepley } 241a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 242a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 243a18a7fb9SMatthew G. Knepley } 244a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 245a18a7fb9SMatthew G. Knepley ierr = PetscFree3(sp,v0,J);CHKERRQ(ierr); 246a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 247a18a7fb9SMatthew G. Knepley } 248a18a7fb9SMatthew G. Knepley 249a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 250cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 251c110b1eeSGeoffrey Irving PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 252cb1e1211SMatthew G Knepley { 25372f94c41SMatthew G. Knepley PetscDualSpace *sp; 25472f94c41SMatthew G. Knepley PetscSection section; 25572f94c41SMatthew G. Knepley PetscScalar *values; 25672f94c41SMatthew G. Knepley PetscReal *v0, *J, detJ; 257120386c5SMatthew G. Knepley PetscInt numFields, numComp, dim, spDim, totDim = 0, numValues, cStart, cEnd, c, f, d, v, comp; 258cb1e1211SMatthew G Knepley PetscErrorCode ierr; 259cb1e1211SMatthew G Knepley 260cb1e1211SMatthew G Knepley PetscFunctionBegin; 261cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 26272f94c41SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 263785e854fSJed Brown ierr = PetscMalloc1(numFields, &sp);CHKERRQ(ierr); 26472f94c41SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 26572f94c41SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe[f], &sp[f]);CHKERRQ(ierr); 26672f94c41SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 26772f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 26872f94c41SMatthew G. Knepley totDim += spDim*numComp; 269cb1e1211SMatthew G Knepley } 27072f94c41SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 27172f94c41SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 27272f94c41SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 27372f94c41SMatthew 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); 27472f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 275dcca6d9dSJed Brown ierr = PetscMalloc2(dim,&v0,dim*dim,&J);CHKERRQ(ierr); 27672f94c41SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 27772f94c41SMatthew G. Knepley PetscCellGeometry geom; 278cb1e1211SMatthew G Knepley 279cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, NULL, &detJ);CHKERRQ(ierr); 28072f94c41SMatthew G. Knepley geom.v0 = v0; 28172f94c41SMatthew G. Knepley geom.J = J; 28272f94c41SMatthew G. Knepley geom.detJ = &detJ; 28372f94c41SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 284c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[f] : NULL; 28572f94c41SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &numComp);CHKERRQ(ierr); 28672f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 28772f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 288120386c5SMatthew G. Knepley if (funcs[f]) { 289c110b1eeSGeoffrey Irving ierr = PetscDualSpaceApply(sp[f], d, geom, numComp, funcs[f], ctx, &values[v]);CHKERRQ(ierr); 290120386c5SMatthew G. Knepley } else { 291120386c5SMatthew G. Knepley for (comp = 0; comp < numComp; ++comp) values[v+comp] = 0.0; 292120386c5SMatthew G. Knepley } 29372f94c41SMatthew G. Knepley v += numComp; 294cb1e1211SMatthew G Knepley } 295cb1e1211SMatthew G Knepley } 29672f94c41SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 297cb1e1211SMatthew G Knepley } 29872f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 2991f2da991SMatthew G. Knepley ierr = PetscFree2(v0,J);CHKERRQ(ierr); 30072f94c41SMatthew G. Knepley ierr = PetscFree(sp);CHKERRQ(ierr); 301cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 302cb1e1211SMatthew G Knepley } 303cb1e1211SMatthew G Knepley 304cb1e1211SMatthew G Knepley #undef __FUNCT__ 305cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunction" 306cb1e1211SMatthew G Knepley /*@C 307cb1e1211SMatthew G Knepley DMPlexProjectFunction - This projects the given function into the function space provided. 308cb1e1211SMatthew G Knepley 309cb1e1211SMatthew G Knepley Input Parameters: 310cb1e1211SMatthew G Knepley + dm - The DM 31172f94c41SMatthew G. Knepley . fe - The PetscFE associated with the field 31272f94c41SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field 313c110b1eeSGeoffrey Irving . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 314cb1e1211SMatthew G Knepley - mode - The insertion mode for values 315cb1e1211SMatthew G Knepley 316cb1e1211SMatthew G Knepley Output Parameter: 317cb1e1211SMatthew G Knepley . X - vector 318cb1e1211SMatthew G Knepley 319cb1e1211SMatthew G Knepley Level: developer 320cb1e1211SMatthew G Knepley 321878cb397SSatish Balay .seealso: DMPlexComputeL2Diff() 322878cb397SSatish Balay @*/ 323c110b1eeSGeoffrey Irving PetscErrorCode DMPlexProjectFunction(DM dm, PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) 324cb1e1211SMatthew G Knepley { 325cb1e1211SMatthew G Knepley Vec localX; 326cb1e1211SMatthew G Knepley PetscErrorCode ierr; 327cb1e1211SMatthew G Knepley 328cb1e1211SMatthew G Knepley PetscFunctionBegin; 3299a800dd8SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 330cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 331c110b1eeSGeoffrey Irving ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, ctxs, mode, localX);CHKERRQ(ierr); 332cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 333cb1e1211SMatthew G Knepley ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 334cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 335cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 336cb1e1211SMatthew G Knepley } 337cb1e1211SMatthew G Knepley 33855f2e967SMatthew G. Knepley #undef __FUNCT__ 3393351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFEM" 3403351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFEM(DM dm, Vec localX) 34155f2e967SMatthew G. Knepley { 34255f2e967SMatthew G. Knepley void (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx); 34355f2e967SMatthew G. Knepley void **ctxs; 34455f2e967SMatthew G. Knepley PetscFE *fe; 34555f2e967SMatthew G. Knepley PetscInt numFields, f, numBd, b; 34655f2e967SMatthew G. Knepley PetscErrorCode ierr; 34755f2e967SMatthew G. Knepley 34855f2e967SMatthew G. Knepley PetscFunctionBegin; 34955f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 35055f2e967SMatthew G. Knepley PetscValidHeaderSpecific(localX, VEC_CLASSID, 2); 35155f2e967SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 35255f2e967SMatthew G. Knepley ierr = PetscMalloc3(numFields,&fe,numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 35355f2e967SMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = DMGetField(dm, f, (PetscObject *) &fe[f]);CHKERRQ(ierr);} 35455f2e967SMatthew G. Knepley /* OPT: Could attempt to do multiple BCs at once */ 35555f2e967SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 35655f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 357a18a7fb9SMatthew G. Knepley DMLabel label; 35855f2e967SMatthew G. Knepley const PetscInt *ids; 359a18a7fb9SMatthew G. Knepley const char *name; 36055f2e967SMatthew G. Knepley PetscInt numids, field; 36155f2e967SMatthew G. Knepley PetscBool isEssential; 36255f2e967SMatthew G. Knepley void (*func)(); 36355f2e967SMatthew G. Knepley void *ctx; 36455f2e967SMatthew G. Knepley 36555f2e967SMatthew G. Knepley /* TODO: We need to set only the part indicated by the ids */ 366a18a7fb9SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, &isEssential, &name, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 367a18a7fb9SMatthew G. Knepley ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr); 36855f2e967SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 36955f2e967SMatthew G. Knepley funcs[f] = field == f ? (void (*)(const PetscReal[], PetscScalar *, void *)) func : NULL; 37055f2e967SMatthew G. Knepley ctxs[f] = field == f ? ctx : NULL; 37155f2e967SMatthew G. Knepley } 372a18a7fb9SMatthew G. Knepley ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 37355f2e967SMatthew G. Knepley } 37455f2e967SMatthew G. Knepley ierr = PetscFree3(fe,funcs,ctxs);CHKERRQ(ierr); 37555f2e967SMatthew G. Knepley PetscFunctionReturn(0); 37655f2e967SMatthew G. Knepley } 37755f2e967SMatthew G. Knepley 3783351dd3dSMatthew G. Knepley /* Assuming dim == 3 */ 3793351dd3dSMatthew G. Knepley typedef struct { 3803351dd3dSMatthew G. Knepley PetscScalar normal[3]; /* Area-scaled normals */ 3813351dd3dSMatthew G. Knepley PetscScalar centroid[3]; /* Location of centroid (quadrature point) */ 3823351dd3dSMatthew G. Knepley PetscScalar grad[2][3]; /* Face contribution to gradient in left and right cell */ 3833351dd3dSMatthew G. Knepley } FaceGeom; 3843351dd3dSMatthew G. Knepley 3853351dd3dSMatthew G. Knepley #undef __FUNCT__ 3863351dd3dSMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValuesFVM" 3873351dd3dSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesFVM(DM dm, PetscReal time, Vec locX) 3883351dd3dSMatthew G. Knepley { 3893351dd3dSMatthew G. Knepley DM dmFace; 3903351dd3dSMatthew G. Knepley Vec faceGeometry; 3913351dd3dSMatthew G. Knepley DMLabel label; 3923351dd3dSMatthew G. Knepley const PetscScalar *facegeom; 3933351dd3dSMatthew G. Knepley PetscScalar *x; 394*ede43f4cSMatthew G. Knepley PetscInt numBd, b, fStart, fEnd; 3953351dd3dSMatthew G. Knepley PetscErrorCode ierr; 3963351dd3dSMatthew G. Knepley 3973351dd3dSMatthew G. Knepley PetscFunctionBegin; 3988817705eSMatthew G. Knepley /* TODO Pull this geometry calculation into the library */ 3993351dd3dSMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "FaceGeometry", (PetscObject *) &faceGeometry);CHKERRQ(ierr); 4003351dd3dSMatthew G. Knepley ierr = DMPlexGetLabel(dm, "Face Sets", &label);CHKERRQ(ierr); 401*ede43f4cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 4023351dd3dSMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 4033351dd3dSMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 4043351dd3dSMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 4053351dd3dSMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 4063351dd3dSMatthew G. Knepley for (b = 0; b < numBd; ++b) { 4071fc84671SMatthew G. Knepley PetscErrorCode (*func)(PetscReal,const PetscScalar*,const PetscScalar*,const PetscScalar*,PetscScalar*,void*); 4083351dd3dSMatthew G. Knepley const PetscInt *ids; 4093351dd3dSMatthew G. Knepley PetscInt numids, i; 4103351dd3dSMatthew G. Knepley void *ctx; 4113351dd3dSMatthew G. Knepley 4123351dd3dSMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, NULL, NULL, NULL, (void (**)()) &func, &numids, &ids, &ctx);CHKERRQ(ierr); 4133351dd3dSMatthew G. Knepley for (i = 0; i < numids; ++i) { 4143351dd3dSMatthew G. Knepley IS faceIS; 4153351dd3dSMatthew G. Knepley const PetscInt *faces; 4163351dd3dSMatthew G. Knepley PetscInt numFaces, f; 4173351dd3dSMatthew G. Knepley 4183351dd3dSMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 4198817705eSMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 4203351dd3dSMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 4213351dd3dSMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 4223351dd3dSMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 4233351dd3dSMatthew G. Knepley const PetscInt face = faces[f], *cells; 4243351dd3dSMatthew G. Knepley const PetscScalar *xI; 4253351dd3dSMatthew G. Knepley PetscScalar *xG; 4263351dd3dSMatthew G. Knepley const FaceGeom *fg; 4273351dd3dSMatthew G. Knepley 428*ede43f4cSMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 4293351dd3dSMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 4303351dd3dSMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 4313351dd3dSMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 4323351dd3dSMatthew G. Knepley ierr = DMPlexPointLocalRef(dm, cells[1], x, &xG);CHKERRQ(ierr); 4333351dd3dSMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr); 4343351dd3dSMatthew G. Knepley } 4353351dd3dSMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 4363351dd3dSMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 4373351dd3dSMatthew G. Knepley } 4383351dd3dSMatthew G. Knepley } 4393351dd3dSMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 4403351dd3dSMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 4413351dd3dSMatthew G. Knepley PetscFunctionReturn(0); 4423351dd3dSMatthew G. Knepley } 4433351dd3dSMatthew G. Knepley 444cb1e1211SMatthew G Knepley #undef __FUNCT__ 445cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 446cb1e1211SMatthew G Knepley /*@C 447cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 448cb1e1211SMatthew G Knepley 449cb1e1211SMatthew G Knepley Input Parameters: 450cb1e1211SMatthew G Knepley + dm - The DM 451c5bbbd5bSMatthew G. Knepley . fe - The PetscFE object for each field 452cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 45351259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 454cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 455cb1e1211SMatthew G Knepley 456cb1e1211SMatthew G Knepley Output Parameter: 457cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 458cb1e1211SMatthew G Knepley 459cb1e1211SMatthew G Knepley Level: developer 460cb1e1211SMatthew G Knepley 46123d86601SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2GradientDiff() 462878cb397SSatish Balay @*/ 463c110b1eeSGeoffrey Irving PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscFE fe[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 464cb1e1211SMatthew G Knepley { 465cb1e1211SMatthew G Knepley const PetscInt debug = 0; 466cb1e1211SMatthew G Knepley PetscSection section; 467c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 468cb1e1211SMatthew G Knepley Vec localX; 46972f94c41SMatthew G. Knepley PetscScalar *funcVal; 470cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 471cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 472cb1e1211SMatthew G Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 473cb1e1211SMatthew G Knepley PetscErrorCode ierr; 474cb1e1211SMatthew G Knepley 475cb1e1211SMatthew G Knepley PetscFunctionBegin; 476cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 477cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 478cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 479cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 480cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 481cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 482cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 483c5bbbd5bSMatthew G. Knepley PetscInt Nc; 484c5bbbd5bSMatthew G. Knepley 485c5bbbd5bSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr); 486c5bbbd5bSMatthew G. Knepley numComponents += Nc; 487cb1e1211SMatthew G Knepley } 488c110b1eeSGeoffrey Irving ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 489dcca6d9dSJed Brown ierr = PetscMalloc5(numComponents,&funcVal,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 490cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 491c5bbbd5bSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[0], &quad);CHKERRQ(ierr); 492cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 493a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 494cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 495cb1e1211SMatthew G Knepley 496cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 497cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 498cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 499cb1e1211SMatthew G Knepley 500cb1e1211SMatthew G Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 501c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 50221454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 503c5bbbd5bSMatthew G. Knepley PetscReal *basis; 50421454ff5SMatthew G. Knepley PetscInt numQuadPoints, numBasisFuncs, numBasisComps, q, d, e, fc, f; 505cb1e1211SMatthew G Knepley 50621454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 507c5bbbd5bSMatthew G. Knepley ierr = PetscFEGetDimension(fe[field], &numBasisFuncs);CHKERRQ(ierr); 508c5bbbd5bSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[field], &numBasisComps);CHKERRQ(ierr); 509c5bbbd5bSMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe[field], &basis, NULL, NULL);CHKERRQ(ierr); 510cb1e1211SMatthew G Knepley if (debug) { 511cb1e1211SMatthew G Knepley char title[1024]; 512cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 513cb1e1211SMatthew G Knepley ierr = DMPrintCellVector(c, title, numBasisFuncs*numBasisComps, &x[fieldOffset]);CHKERRQ(ierr); 514cb1e1211SMatthew G Knepley } 515cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 516cb1e1211SMatthew G Knepley for (d = 0; d < dim; d++) { 517cb1e1211SMatthew G Knepley coords[d] = v0[d]; 518cb1e1211SMatthew G Knepley for (e = 0; e < dim; e++) { 519cb1e1211SMatthew G Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 520cb1e1211SMatthew G Knepley } 521cb1e1211SMatthew G Knepley } 522c110b1eeSGeoffrey Irving (*funcs[field])(coords, funcVal, ctx); 523cb1e1211SMatthew G Knepley for (fc = 0; fc < numBasisComps; ++fc) { 524a1d24da5SMatthew G. Knepley PetscScalar interpolant = 0.0; 525a1d24da5SMatthew G. Knepley 526cb1e1211SMatthew G Knepley for (f = 0; f < numBasisFuncs; ++f) { 527cb1e1211SMatthew G Knepley const PetscInt fidx = f*numBasisComps+fc; 528a1d24da5SMatthew G. Knepley interpolant += x[fieldOffset+fidx]*basis[q*numBasisFuncs*numBasisComps+fidx]; 529cb1e1211SMatthew G Knepley } 53072f94c41SMatthew 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);} 53172f94c41SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 532cb1e1211SMatthew G Knepley } 533cb1e1211SMatthew G Knepley } 534cb1e1211SMatthew G Knepley comp += numBasisComps; 535cb1e1211SMatthew G Knepley fieldOffset += numBasisFuncs*numBasisComps; 536cb1e1211SMatthew G Knepley } 537cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 538cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 539cb1e1211SMatthew G Knepley localDiff += elemDiff; 540cb1e1211SMatthew G Knepley } 54172f94c41SMatthew G. Knepley ierr = PetscFree5(funcVal,coords,v0,J,invJ);CHKERRQ(ierr); 542cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 54386a74ee0SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 544cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 545cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 546cb1e1211SMatthew G Knepley } 547cb1e1211SMatthew G Knepley 548cb1e1211SMatthew G Knepley #undef __FUNCT__ 54940e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 55040e14135SMatthew G. Knepley /*@C 55140e14135SMatthew 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. 55240e14135SMatthew G. Knepley 55340e14135SMatthew G. Knepley Input Parameters: 55440e14135SMatthew G. Knepley + dm - The DM 55540e14135SMatthew G. Knepley . fe - The PetscFE object for each field 55640e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 55751259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 55840e14135SMatthew G. Knepley . X - The coefficient vector u_h 55940e14135SMatthew G. Knepley - n - The vector to project along 56040e14135SMatthew G. Knepley 56140e14135SMatthew G. Knepley Output Parameter: 56240e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 56340e14135SMatthew G. Knepley 56440e14135SMatthew G. Knepley Level: developer 56540e14135SMatthew G. Knepley 56640e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 56740e14135SMatthew G. Knepley @*/ 56851259fa3SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, PetscFE fe[], void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 569cb1e1211SMatthew G Knepley { 57040e14135SMatthew G. Knepley const PetscInt debug = 0; 571cb1e1211SMatthew G Knepley PetscSection section; 57240e14135SMatthew G. Knepley PetscQuadrature quad; 57340e14135SMatthew G. Knepley Vec localX; 57440e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 57540e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 57640e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 57740e14135SMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset, comp; 578cb1e1211SMatthew G Knepley PetscErrorCode ierr; 579cb1e1211SMatthew G Knepley 580cb1e1211SMatthew G Knepley PetscFunctionBegin; 58140e14135SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 58240e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 58340e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 58440e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 58540e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 58640e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 587652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 58840e14135SMatthew G. Knepley PetscInt Nc; 589652b88e8SMatthew G. Knepley 59040e14135SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr); 59140e14135SMatthew G. Knepley numComponents += Nc; 592652b88e8SMatthew G. Knepley } 59340e14135SMatthew G. Knepley /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 59440e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 59540e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 59640e14135SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[0], &quad);CHKERRQ(ierr); 59740e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 59840e14135SMatthew G. Knepley PetscScalar *x = NULL; 59940e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 600652b88e8SMatthew G. Knepley 60140e14135SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, v0, J, invJ, &detJ);CHKERRQ(ierr); 60240e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 60340e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 60440e14135SMatthew G. Knepley 60540e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 60651259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 60721454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 60840e14135SMatthew G. Knepley PetscReal *basisDer; 60921454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 61040e14135SMatthew G. Knepley 61121454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 61240e14135SMatthew G. Knepley ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr); 61340e14135SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[field], &Ncomp);CHKERRQ(ierr); 61440e14135SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe[field], NULL, &basisDer, NULL);CHKERRQ(ierr); 61540e14135SMatthew G. Knepley if (debug) { 61640e14135SMatthew G. Knepley char title[1024]; 61740e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 61840e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 619652b88e8SMatthew G. Knepley } 62040e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 62140e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 62240e14135SMatthew G. Knepley coords[d] = v0[d]; 62340e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 62440e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 625652b88e8SMatthew G. Knepley } 62640e14135SMatthew G. Knepley } 62751259fa3SMatthew G. Knepley (*funcs[field])(coords, n, funcVal, ctx); 62840e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 62940e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 63040e14135SMatthew G. Knepley 63140e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 63240e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 63340e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 63440e14135SMatthew G. Knepley 63540e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 63640e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 63740e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 63840e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 63940e14135SMatthew G. Knepley } 64040e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 64140e14135SMatthew G. Knepley } 64240e14135SMatthew G. Knepley } 64340e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 64440e14135SMatthew 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);} 64540e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 64640e14135SMatthew G. Knepley } 64740e14135SMatthew G. Knepley } 64840e14135SMatthew G. Knepley comp += Ncomp; 64940e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 65040e14135SMatthew G. Knepley } 65140e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 65240e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 65340e14135SMatthew G. Knepley localDiff += elemDiff; 65440e14135SMatthew G. Knepley } 65540e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 65640e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 65740e14135SMatthew G. Knepley ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 65840e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 659cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 660cb1e1211SMatthew G Knepley } 661cb1e1211SMatthew G Knepley 662a0845e3aSMatthew G. Knepley #undef __FUNCT__ 663a0845e3aSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM" 664a0845e3aSMatthew G. Knepley /*@ 665a0845e3aSMatthew G. Knepley DMPlexComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user 666a0845e3aSMatthew G. Knepley 667a0845e3aSMatthew G. Knepley Input Parameters: 668a0845e3aSMatthew G. Knepley + dm - The mesh 669a0845e3aSMatthew G. Knepley . X - Local input vector 670a0845e3aSMatthew G. Knepley - user - The user context 671a0845e3aSMatthew G. Knepley 672a0845e3aSMatthew G. Knepley Output Parameter: 673a0845e3aSMatthew G. Knepley . F - Local output vector 674a0845e3aSMatthew G. Knepley 675a0845e3aSMatthew G. Knepley Note: 6768026896cSMatthew G. Knepley The first member of the user context must be an FEMContext. 677a0845e3aSMatthew G. Knepley 678a0845e3aSMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 679a0845e3aSMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 680a0845e3aSMatthew G. Knepley 681a0845e3aSMatthew G. Knepley Level: developer 682a0845e3aSMatthew G. Knepley 683a0845e3aSMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM() 684a0845e3aSMatthew G. Knepley @*/ 685a0845e3aSMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM(DM dm, Vec X, Vec F, void *user) 686a0845e3aSMatthew G. Knepley { 687a0845e3aSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 6889a559087SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 689a0845e3aSMatthew G. Knepley PetscFE *fe = fem->fe; 6909a559087SMatthew G. Knepley PetscFE *feAux = fem->feAux; 691f1ea0e2fSMatthew G. Knepley PetscFE *feBd = fem->feBd; 692a0845e3aSMatthew G. Knepley const char *name = "Residual"; 6939a559087SMatthew G. Knepley DM dmAux; 6949a559087SMatthew G. Knepley Vec A; 695a0845e3aSMatthew G. Knepley PetscQuadrature q; 696a0845e3aSMatthew G. Knepley PetscCellGeometry geom; 6979a559087SMatthew G. Knepley PetscSection section, sectionAux; 698a0845e3aSMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 69901599b20SMatthew G. Knepley PetscScalar *elemVec, *u, *a = NULL; 7009a559087SMatthew G. Knepley PetscInt dim, Nf, NfAux = 0, f, numCells, cStart, cEnd, c; 701a0845e3aSMatthew G. Knepley PetscInt cellDof = 0, numComponents = 0; 7029a559087SMatthew G. Knepley PetscInt cellDofAux = 0, numComponentsAux = 0; 703a0845e3aSMatthew G. Knepley PetscErrorCode ierr; 704a0845e3aSMatthew G. Knepley 705a0845e3aSMatthew G. Knepley PetscFunctionBegin; 706a0845e3aSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 707a0845e3aSMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 708a0845e3aSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 7099a559087SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 710a0845e3aSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 711a0845e3aSMatthew G. Knepley numCells = cEnd - cStart; 7129a559087SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 713a0845e3aSMatthew G. Knepley PetscInt Nb, Nc; 714a0845e3aSMatthew G. Knepley 715a0845e3aSMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr); 716a0845e3aSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr); 717a0845e3aSMatthew G. Knepley cellDof += Nb*Nc; 718a0845e3aSMatthew G. Knepley numComponents += Nc; 719a0845e3aSMatthew G. Knepley } 7209a559087SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 7219a559087SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 7229a559087SMatthew G. Knepley if (dmAux) { 7239a559087SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 7249a559087SMatthew G. Knepley ierr = PetscSectionGetNumFields(sectionAux, &NfAux);CHKERRQ(ierr); 7259a559087SMatthew G. Knepley } 7269a559087SMatthew G. Knepley for (f = 0; f < NfAux; ++f) { 7279a559087SMatthew G. Knepley PetscInt Nb, Nc; 7289a559087SMatthew G. Knepley 7299a559087SMatthew G. Knepley ierr = PetscFEGetDimension(feAux[f], &Nb);CHKERRQ(ierr); 7309a559087SMatthew G. Knepley ierr = PetscFEGetNumComponents(feAux[f], &Nc);CHKERRQ(ierr); 7319a559087SMatthew G. Knepley cellDofAux += Nb*Nc; 7329a559087SMatthew G. Knepley numComponentsAux += Nc; 7339a559087SMatthew G. Knepley } 7343351dd3dSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 735a0845e3aSMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 736dcca6d9dSJed Brown ierr = PetscMalloc6(numCells*cellDof,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*cellDof,&elemVec);CHKERRQ(ierr); 737785e854fSJed Brown if (dmAux) {ierr = PetscMalloc1(numCells*cellDofAux, &a);CHKERRQ(ierr);} 738a0845e3aSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 739a0845e3aSMatthew G. Knepley PetscScalar *x = NULL; 740a0845e3aSMatthew G. Knepley PetscInt i; 741a0845e3aSMatthew G. Knepley 742a0845e3aSMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 743a0845e3aSMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 744a0845e3aSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 745a0845e3aSMatthew G. Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 746a0845e3aSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 7479a559087SMatthew G. Knepley if (dmAux) { 7489a559087SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 7499a559087SMatthew G. Knepley for (i = 0; i < cellDofAux; ++i) a[c*cellDofAux+i] = x[i]; 7509a559087SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 751a0845e3aSMatthew G. Knepley } 7529a559087SMatthew G. Knepley } 7539a559087SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 754c012ea0aSMatthew G. Knepley void (*f0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->f0Funcs[f]; 755c012ea0aSMatthew G. Knepley void (*f1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->f1Funcs[f]; 75621454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb; 757a0845e3aSMatthew G. Knepley /* Conforming batches */ 758f30c5766SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 759a0845e3aSMatthew G. Knepley /* Remainder */ 760a0845e3aSMatthew G. Knepley PetscInt Nr, offset; 761a0845e3aSMatthew G. Knepley 762a0845e3aSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[f], &q);CHKERRQ(ierr); 763a0845e3aSMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr); 764f30c5766SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe[f], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 76521454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 76621454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 767a0845e3aSMatthew G. Knepley batchSize = numBlocks * blockSize; 768f30c5766SMatthew G. Knepley ierr = PetscFESetTileSizes(fe[f], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 769a0845e3aSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 770a0845e3aSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 771a0845e3aSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 772a0845e3aSMatthew G. Knepley offset = numCells - Nr; 773a0845e3aSMatthew G. Knepley geom.v0 = v0; 774a0845e3aSMatthew G. Knepley geom.J = J; 775a0845e3aSMatthew G. Knepley geom.invJ = invJ; 776a0845e3aSMatthew G. Knepley geom.detJ = detJ; 7779a559087SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe[f], Ne, Nf, fe, f, geom, u, NfAux, feAux, a, f0, f1, elemVec);CHKERRQ(ierr); 778a0845e3aSMatthew G. Knepley geom.v0 = &v0[offset*dim]; 779a0845e3aSMatthew G. Knepley geom.J = &J[offset*dim*dim]; 780a0845e3aSMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 781a0845e3aSMatthew G. Knepley geom.detJ = &detJ[offset]; 7829a559087SMatthew G. Knepley ierr = PetscFEIntegrateResidual(fe[f], Nr, Nf, fe, f, geom, &u[offset*cellDof], NfAux, feAux, &a[offset*cellDofAux], f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 783a0845e3aSMatthew G. Knepley } 784a0845e3aSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 785a0845e3aSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 786a0845e3aSMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 787a0845e3aSMatthew G. Knepley } 788a0845e3aSMatthew G. Knepley ierr = PetscFree6(u,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 7899a559087SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 790f1ea0e2fSMatthew G. Knepley if (feBd) { 791075da914SMatthew G. Knepley DMLabel label, depth; 792f1ea0e2fSMatthew G. Knepley IS pointIS; 793f1ea0e2fSMatthew G. Knepley const PetscInt *points; 794075da914SMatthew G. Knepley PetscInt dep, numPoints, p, numFaces; 795f1ea0e2fSMatthew G. Knepley PetscReal *n; 796f1ea0e2fSMatthew G. Knepley 797f1ea0e2fSMatthew G. Knepley ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr); 798075da914SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 799f1ea0e2fSMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 800f1ea0e2fSMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 801f1ea0e2fSMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 802f1ea0e2fSMatthew G. Knepley for (f = 0, cellDof = 0, numComponents = 0; f < Nf; ++f) { 803f1ea0e2fSMatthew G. Knepley PetscInt Nb, Nc; 804f1ea0e2fSMatthew G. Knepley 805f1ea0e2fSMatthew G. Knepley ierr = PetscFEGetDimension(feBd[f], &Nb);CHKERRQ(ierr); 806f1ea0e2fSMatthew G. Knepley ierr = PetscFEGetNumComponents(feBd[f], &Nc);CHKERRQ(ierr); 807f1ea0e2fSMatthew G. Knepley cellDof += Nb*Nc; 808f1ea0e2fSMatthew G. Knepley numComponents += Nc; 809f1ea0e2fSMatthew G. Knepley } 810075da914SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 811075da914SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 812075da914SMatthew G. Knepley if (dep == dim-1) ++numFaces; 813075da914SMatthew G. Knepley } 814dcca6d9dSJed Brown ierr = PetscMalloc7(numFaces*cellDof,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*cellDof,&elemVec);CHKERRQ(ierr); 815075da914SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 816f1ea0e2fSMatthew G. Knepley const PetscInt point = points[p]; 817f1ea0e2fSMatthew G. Knepley PetscScalar *x = NULL; 818f1ea0e2fSMatthew G. Knepley PetscInt i; 819f1ea0e2fSMatthew G. Knepley 820075da914SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 821075da914SMatthew G. Knepley if (dep != dim-1) continue; 822075da914SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr); 823a8007bbfSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]); 824075da914SMatthew G. Knepley if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point); 825f1ea0e2fSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 826075da914SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u[f*cellDof+i] = x[i]; 827f1ea0e2fSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 828075da914SMatthew G. Knepley ++f; 829f1ea0e2fSMatthew G. Knepley } 830f1ea0e2fSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 831f1ea0e2fSMatthew G. Knepley void (*f0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f0BdFuncs[f]; 832f1ea0e2fSMatthew G. Knepley void (*f1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f1BdFuncs[f]; 83321454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb; 834f1ea0e2fSMatthew G. Knepley /* Conforming batches */ 835f1ea0e2fSMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 836f1ea0e2fSMatthew G. Knepley /* Remainder */ 837f1ea0e2fSMatthew G. Knepley PetscInt Nr, offset; 838f1ea0e2fSMatthew G. Knepley 839f1ea0e2fSMatthew G. Knepley ierr = PetscFEGetQuadrature(feBd[f], &q);CHKERRQ(ierr); 840f1ea0e2fSMatthew G. Knepley ierr = PetscFEGetDimension(feBd[f], &Nb);CHKERRQ(ierr); 841f1ea0e2fSMatthew G. Knepley ierr = PetscFEGetTileSizes(feBd[f], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 84221454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 84321454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 844f1ea0e2fSMatthew G. Knepley batchSize = numBlocks * blockSize; 845f1ea0e2fSMatthew G. Knepley ierr = PetscFESetTileSizes(feBd[f], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 846075da914SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 847f1ea0e2fSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 848075da914SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 849075da914SMatthew G. Knepley offset = numFaces - Nr; 850f1ea0e2fSMatthew G. Knepley geom.v0 = v0; 851f1ea0e2fSMatthew G. Knepley geom.n = n; 852f1ea0e2fSMatthew G. Knepley geom.J = J; 853f1ea0e2fSMatthew G. Knepley geom.invJ = invJ; 854f1ea0e2fSMatthew G. Knepley geom.detJ = detJ; 855f1ea0e2fSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(feBd[f], Ne, Nf, feBd, f, geom, u, 0, NULL, NULL, f0, f1, elemVec);CHKERRQ(ierr); 856f1ea0e2fSMatthew G. Knepley geom.v0 = &v0[offset*dim]; 857f1ea0e2fSMatthew G. Knepley geom.n = &n[offset*dim]; 858f1ea0e2fSMatthew G. Knepley geom.J = &J[offset*dim*dim]; 859f1ea0e2fSMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 860f1ea0e2fSMatthew G. Knepley geom.detJ = &detJ[offset]; 861f1ea0e2fSMatthew G. Knepley ierr = PetscFEIntegrateBdResidual(feBd[f], Nr, Nf, feBd, f, geom, &u[offset*cellDof], 0, NULL, NULL, f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 862f1ea0e2fSMatthew G. Knepley } 863075da914SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 864f1ea0e2fSMatthew G. Knepley const PetscInt point = points[p]; 865f1ea0e2fSMatthew G. Knepley 866075da914SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 867075da914SMatthew G. Knepley if (dep != dim-1) continue; 868075da914SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", cellDof, &elemVec[f*cellDof]);CHKERRQ(ierr);} 869075da914SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*cellDof], ADD_VALUES);CHKERRQ(ierr); 870075da914SMatthew G. Knepley ++f; 871f1ea0e2fSMatthew G. Knepley } 872f1ea0e2fSMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 873f1ea0e2fSMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 874f1ea0e2fSMatthew G. Knepley ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr); 875f1ea0e2fSMatthew G. Knepley } 8766113b454SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);} 877a0845e3aSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 878a0845e3aSMatthew G. Knepley PetscFunctionReturn(0); 879a0845e3aSMatthew G. Knepley } 880a0845e3aSMatthew G. Knepley 881cb1e1211SMatthew G Knepley #undef __FUNCT__ 882af1eca97SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIFunctionFEM" 883af1eca97SMatthew G. Knepley /*@ 884af1eca97SMatthew G. Knepley DMPlexComputeIFunctionFEM - Form the local implicit function F from the local input X, X_t using pointwise functions specified by the user 885af1eca97SMatthew G. Knepley 886af1eca97SMatthew G. Knepley Input Parameters: 887af1eca97SMatthew G. Knepley + dm - The mesh 8881c41a8caSMatthew G. Knepley . time - The current time 889af1eca97SMatthew G. Knepley . X - Local input vector 890af1eca97SMatthew G. Knepley . X_t - Time derivative of the local input vector 891af1eca97SMatthew G. Knepley - user - The user context 892af1eca97SMatthew G. Knepley 893af1eca97SMatthew G. Knepley Output Parameter: 894af1eca97SMatthew G. Knepley . F - Local output vector 895af1eca97SMatthew G. Knepley 896af1eca97SMatthew G. Knepley Note: 897af1eca97SMatthew G. Knepley The first member of the user context must be an FEMContext. 898af1eca97SMatthew G. Knepley 899af1eca97SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 900af1eca97SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 901af1eca97SMatthew G. Knepley 902af1eca97SMatthew G. Knepley Level: developer 903af1eca97SMatthew G. Knepley 904af1eca97SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 905af1eca97SMatthew G. Knepley @*/ 9061c41a8caSMatthew G. Knepley PetscErrorCode DMPlexComputeIFunctionFEM(DM dm, PetscReal time, Vec X, Vec X_t, Vec F, void *user) 907af1eca97SMatthew G. Knepley { 908af1eca97SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 909af1eca97SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 910af1eca97SMatthew G. Knepley PetscFE *fe = fem->fe; 911af1eca97SMatthew G. Knepley PetscFE *feAux = fem->feAux; 912af1eca97SMatthew G. Knepley PetscFE *feBd = fem->feBd; 913af1eca97SMatthew G. Knepley const char *name = "Residual"; 914af1eca97SMatthew G. Knepley DM dmAux; 915af1eca97SMatthew G. Knepley Vec A; 916af1eca97SMatthew G. Knepley PetscQuadrature q; 917af1eca97SMatthew G. Knepley PetscCellGeometry geom; 918af1eca97SMatthew G. Knepley PetscSection section, sectionAux; 919af1eca97SMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 920af1eca97SMatthew G. Knepley PetscScalar *elemVec, *u, *u_t, *a = NULL; 921af1eca97SMatthew G. Knepley PetscInt dim, Nf, NfAux = 0, f, numCells, cStart, cEnd, c; 922af1eca97SMatthew G. Knepley PetscInt cellDof = 0, numComponents = 0; 923af1eca97SMatthew G. Knepley PetscInt cellDofAux = 0, numComponentsAux = 0; 924af1eca97SMatthew G. Knepley PetscErrorCode ierr; 925af1eca97SMatthew G. Knepley 926af1eca97SMatthew G. Knepley PetscFunctionBegin; 927af1eca97SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 928af1eca97SMatthew G. Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 929af1eca97SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 930af1eca97SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 931af1eca97SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 932af1eca97SMatthew G. Knepley numCells = cEnd - cStart; 933af1eca97SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 934af1eca97SMatthew G. Knepley PetscInt Nb, Nc; 935af1eca97SMatthew G. Knepley 936af1eca97SMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr); 937af1eca97SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr); 938af1eca97SMatthew G. Knepley cellDof += Nb*Nc; 939af1eca97SMatthew G. Knepley numComponents += Nc; 940af1eca97SMatthew G. Knepley } 941af1eca97SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 942af1eca97SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 943af1eca97SMatthew G. Knepley if (dmAux) { 944af1eca97SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 945af1eca97SMatthew G. Knepley ierr = PetscSectionGetNumFields(sectionAux, &NfAux);CHKERRQ(ierr); 946af1eca97SMatthew G. Knepley } 947af1eca97SMatthew G. Knepley for (f = 0; f < NfAux; ++f) { 948af1eca97SMatthew G. Knepley PetscInt Nb, Nc; 949af1eca97SMatthew G. Knepley 950af1eca97SMatthew G. Knepley ierr = PetscFEGetDimension(feAux[f], &Nb);CHKERRQ(ierr); 951af1eca97SMatthew G. Knepley ierr = PetscFEGetNumComponents(feAux[f], &Nc);CHKERRQ(ierr); 952af1eca97SMatthew G. Knepley cellDofAux += Nb*Nc; 953af1eca97SMatthew G. Knepley numComponentsAux += Nc; 954af1eca97SMatthew G. Knepley } 9553351dd3dSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 956af1eca97SMatthew G. Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 957af1eca97SMatthew G. Knepley ierr = PetscMalloc7(numCells*cellDof,&u,numCells*cellDof,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*cellDof,&elemVec);CHKERRQ(ierr); 958af1eca97SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*cellDofAux, &a);CHKERRQ(ierr);} 959af1eca97SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 960af1eca97SMatthew G. Knepley PetscScalar *x = NULL, *x_t = NULL; 961af1eca97SMatthew G. Knepley PetscInt i; 962af1eca97SMatthew G. Knepley 963af1eca97SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 964af1eca97SMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 965af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 966af1eca97SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 967af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 968af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 969af1eca97SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u_t[c*cellDof+i] = x_t[i]; 970af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr); 971af1eca97SMatthew G. Knepley if (dmAux) { 972af1eca97SMatthew G. Knepley PetscScalar *x_a = NULL; 973af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x_a);CHKERRQ(ierr); 974af1eca97SMatthew G. Knepley for (i = 0; i < cellDofAux; ++i) a[c*cellDofAux+i] = x_a[i]; 975af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x_a);CHKERRQ(ierr); 976af1eca97SMatthew G. Knepley } 977af1eca97SMatthew G. Knepley } 978af1eca97SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 979af1eca97SMatthew G. Knepley void (*f0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->f0IFuncs[f]; 980af1eca97SMatthew G. Knepley void (*f1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->f1IFuncs[f]; 981af1eca97SMatthew G. Knepley PetscInt numQuadPoints, Nb; 982af1eca97SMatthew G. Knepley /* Conforming batches */ 983af1eca97SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 984af1eca97SMatthew G. Knepley /* Remainder */ 985af1eca97SMatthew G. Knepley PetscInt Nr, offset; 986af1eca97SMatthew G. Knepley 987af1eca97SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[f], &q);CHKERRQ(ierr); 988af1eca97SMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr); 989af1eca97SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe[f], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 990af1eca97SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 991af1eca97SMatthew G. Knepley blockSize = Nb*numQuadPoints; 992af1eca97SMatthew G. Knepley batchSize = numBlocks * blockSize; 993af1eca97SMatthew G. Knepley ierr = PetscFESetTileSizes(fe[f], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 994af1eca97SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 995af1eca97SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 996af1eca97SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 997af1eca97SMatthew G. Knepley offset = numCells - Nr; 998af1eca97SMatthew G. Knepley geom.v0 = v0; 999af1eca97SMatthew G. Knepley geom.J = J; 1000af1eca97SMatthew G. Knepley geom.invJ = invJ; 1001af1eca97SMatthew G. Knepley geom.detJ = detJ; 1002af1eca97SMatthew G. Knepley ierr = PetscFEIntegrateIFunction(fe[f], Ne, Nf, fe, f, geom, u, u_t, NfAux, feAux, a, f0, f1, elemVec);CHKERRQ(ierr); 1003af1eca97SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 1004af1eca97SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 1005af1eca97SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 1006af1eca97SMatthew G. Knepley geom.detJ = &detJ[offset]; 1007af1eca97SMatthew G. Knepley ierr = PetscFEIntegrateIFunction(fe[f], Nr, Nf, fe, f, geom, &u[offset*cellDof], &u_t[offset*cellDof], NfAux, feAux, &a[offset*cellDofAux], f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 1008af1eca97SMatthew G. Knepley } 1009af1eca97SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1010af1eca97SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 1011af1eca97SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 1012af1eca97SMatthew G. Knepley } 1013af1eca97SMatthew G. Knepley ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 1014af1eca97SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 1015af1eca97SMatthew G. Knepley if (feBd) { 1016af1eca97SMatthew G. Knepley DMLabel label, depth; 1017af1eca97SMatthew G. Knepley IS pointIS; 1018af1eca97SMatthew G. Knepley const PetscInt *points; 1019af1eca97SMatthew G. Knepley PetscInt dep, numPoints, p, numFaces; 1020af1eca97SMatthew G. Knepley PetscReal *n; 1021af1eca97SMatthew G. Knepley 1022af1eca97SMatthew G. Knepley ierr = DMPlexGetLabel(dm, "boundary", &label);CHKERRQ(ierr); 1023af1eca97SMatthew G. Knepley ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr); 1024af1eca97SMatthew G. Knepley ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr); 1025af1eca97SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr); 1026af1eca97SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 1027af1eca97SMatthew G. Knepley for (f = 0, cellDof = 0, numComponents = 0; f < Nf; ++f) { 1028af1eca97SMatthew G. Knepley PetscInt Nb, Nc; 1029af1eca97SMatthew G. Knepley 1030af1eca97SMatthew G. Knepley ierr = PetscFEGetDimension(feBd[f], &Nb);CHKERRQ(ierr); 1031af1eca97SMatthew G. Knepley ierr = PetscFEGetNumComponents(feBd[f], &Nc);CHKERRQ(ierr); 1032af1eca97SMatthew G. Knepley cellDof += Nb*Nc; 1033af1eca97SMatthew G. Knepley numComponents += Nc; 1034af1eca97SMatthew G. Knepley } 1035af1eca97SMatthew G. Knepley for (p = 0, numFaces = 0; p < numPoints; ++p) { 1036af1eca97SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1037af1eca97SMatthew G. Knepley if (dep == dim-1) ++numFaces; 1038af1eca97SMatthew G. Knepley } 1039af1eca97SMatthew G. Knepley ierr = PetscMalloc7(numFaces*cellDof,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*cellDof,&elemVec);CHKERRQ(ierr); 1040af1eca97SMatthew G. Knepley ierr = PetscMalloc1(numFaces*cellDof,&u_t);CHKERRQ(ierr); 1041af1eca97SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 1042af1eca97SMatthew G. Knepley const PetscInt point = points[p]; 1043af1eca97SMatthew G. Knepley PetscScalar *x = NULL; 1044af1eca97SMatthew G. Knepley PetscInt i; 1045af1eca97SMatthew G. Knepley 1046af1eca97SMatthew G. Knepley ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr); 1047af1eca97SMatthew G. Knepley if (dep != dim-1) continue; 1048af1eca97SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dm, point, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr); 1049af1eca97SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]); 1050af1eca97SMatthew G. Knepley if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point); 1051af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 1052af1eca97SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u[f*cellDof+i] = x[i]; 1053af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr); 1054af1eca97SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 1055af1eca97SMatthew G. Knepley for (i = 0; i < cellDof; ++i) u_t[f*cellDof+i] = x[i]; 1056af1eca97SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr); 1057af1eca97SMatthew G. Knepley ++f; 1058af1eca97SMatthew G. Knepley } 1059af1eca97SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1060af1eca97SMatthew G. Knepley void (*f0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f0BdIFuncs[f]; 1061af1eca97SMatthew G. Knepley void (*f1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], const PetscReal[], PetscScalar[]) = fem->f1BdIFuncs[f]; 1062af1eca97SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1063af1eca97SMatthew G. Knepley /* Conforming batches */ 1064af1eca97SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1065af1eca97SMatthew G. Knepley /* Remainder */ 1066af1eca97SMatthew G. Knepley PetscInt Nr, offset; 1067af1eca97SMatthew G. Knepley 1068af1eca97SMatthew G. Knepley ierr = PetscFEGetQuadrature(feBd[f], &q);CHKERRQ(ierr); 1069af1eca97SMatthew G. Knepley ierr = PetscFEGetDimension(feBd[f], &Nb);CHKERRQ(ierr); 1070af1eca97SMatthew G. Knepley ierr = PetscFEGetTileSizes(feBd[f], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1071af1eca97SMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 1072af1eca97SMatthew G. Knepley blockSize = Nb*numQuadPoints; 1073af1eca97SMatthew G. Knepley batchSize = numBlocks * blockSize; 1074af1eca97SMatthew G. Knepley ierr = PetscFESetTileSizes(feBd[f], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1075af1eca97SMatthew G. Knepley numChunks = numFaces / (numBatches*batchSize); 1076af1eca97SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1077af1eca97SMatthew G. Knepley Nr = numFaces % (numBatches*batchSize); 1078af1eca97SMatthew G. Knepley offset = numFaces - Nr; 1079af1eca97SMatthew G. Knepley geom.v0 = v0; 1080af1eca97SMatthew G. Knepley geom.n = n; 1081af1eca97SMatthew G. Knepley geom.J = J; 1082af1eca97SMatthew G. Knepley geom.invJ = invJ; 1083af1eca97SMatthew G. Knepley geom.detJ = detJ; 1084af1eca97SMatthew G. Knepley ierr = PetscFEIntegrateBdIFunction(feBd[f], Ne, Nf, feBd, f, geom, u, u_t, 0, NULL, NULL, f0, f1, elemVec);CHKERRQ(ierr); 1085af1eca97SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 1086af1eca97SMatthew G. Knepley geom.n = &n[offset*dim]; 1087af1eca97SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 1088af1eca97SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 1089af1eca97SMatthew G. Knepley geom.detJ = &detJ[offset]; 1090af1eca97SMatthew G. Knepley ierr = PetscFEIntegrateBdIFunction(feBd[f], Nr, Nf, feBd, f, geom, &u[offset*cellDof], &u_t[offset*cellDof], 0, NULL, NULL, f0, f1, &elemVec[offset*cellDof]);CHKERRQ(ierr); 1091af1eca97SMatthew G. Knepley } 1092af1eca97SMatthew G. Knepley for (p = 0, f = 0; p < numPoints; ++p) { 1093af1eca97SMatthew G. Knepley const PetscInt point = points[p]; 1094af1eca97SMatthew G. Knepley 1095af1eca97SMatthew G. Knepley ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr); 1096af1eca97SMatthew G. Knepley if (dep != dim-1) continue; 1097af1eca97SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", cellDof, &elemVec[f*cellDof]);CHKERRQ(ierr);} 1098af1eca97SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*cellDof], ADD_VALUES);CHKERRQ(ierr); 1099af1eca97SMatthew G. Knepley ++f; 1100af1eca97SMatthew G. Knepley } 1101af1eca97SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 1102af1eca97SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 1103af1eca97SMatthew G. Knepley ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr); 1104af1eca97SMatthew G. Knepley ierr = PetscFree(u_t);CHKERRQ(ierr); 1105af1eca97SMatthew G. Knepley } 1106af1eca97SMatthew G. Knepley if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);} 1107af1eca97SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr); 1108af1eca97SMatthew G. Knepley PetscFunctionReturn(0); 1109af1eca97SMatthew G. Knepley } 1110af1eca97SMatthew G. Knepley 1111af1eca97SMatthew G. Knepley #undef __FUNCT__ 1112cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeJacobianActionFEM" 1113cb1e1211SMatthew G Knepley /*@C 1114cb1e1211SMatthew G Knepley DMPlexComputeJacobianActionFEM - Form the local action of Jacobian J(u) on the local input X using pointwise functions specified by the user 1115cb1e1211SMatthew G Knepley 1116cb1e1211SMatthew G Knepley Input Parameters: 1117cb1e1211SMatthew G Knepley + dm - The mesh 1118cb1e1211SMatthew G Knepley . J - The Jacobian shell matrix 1119cb1e1211SMatthew G Knepley . X - Local input vector 1120cb1e1211SMatthew G Knepley - user - The user context 1121cb1e1211SMatthew G Knepley 1122cb1e1211SMatthew G Knepley Output Parameter: 1123cb1e1211SMatthew G Knepley . F - Local output vector 1124cb1e1211SMatthew G Knepley 1125cb1e1211SMatthew G Knepley Note: 11268026896cSMatthew G. Knepley The first member of the user context must be an FEMContext. 1127cb1e1211SMatthew G Knepley 1128cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1129cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 1130cb1e1211SMatthew G Knepley 11310059ad2aSSatish Balay Level: developer 11320059ad2aSSatish Balay 1133cb1e1211SMatthew G Knepley .seealso: DMPlexComputeResidualFEM() 1134878cb397SSatish Balay @*/ 1135cb1e1211SMatthew G Knepley PetscErrorCode DMPlexComputeJacobianActionFEM(DM dm, Mat Jac, Vec X, Vec F, void *user) 1136cb1e1211SMatthew G Knepley { 1137cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 11389a559087SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 11390483ade4SMatthew G. Knepley PetscFE *fe = fem->fe; 11400483ade4SMatthew G. Knepley PetscQuadrature quad; 11410483ade4SMatthew G. Knepley PetscCellGeometry geom; 1142cb1e1211SMatthew G Knepley PetscSection section; 1143cb1e1211SMatthew G Knepley JacActionCtx *jctx; 1144cb1e1211SMatthew G Knepley PetscReal *v0, *J, *invJ, *detJ; 1145cb1e1211SMatthew G Knepley PetscScalar *elemVec, *u, *a; 11460483ade4SMatthew G. Knepley PetscInt dim, numFields, field, numCells, cStart, cEnd, c; 1147cb1e1211SMatthew G Knepley PetscInt cellDof = 0; 1148cb1e1211SMatthew G Knepley PetscErrorCode ierr; 1149cb1e1211SMatthew G Knepley 1150cb1e1211SMatthew G Knepley PetscFunctionBegin; 11510483ade4SMatthew G. Knepley /* ierr = PetscLogEventBegin(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */ 1152cb1e1211SMatthew G Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 1153cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1154cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1155cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 1156cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1157cb1e1211SMatthew G Knepley numCells = cEnd - cStart; 1158cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 11590483ade4SMatthew G. Knepley PetscInt Nb, Nc; 11600483ade4SMatthew G. Knepley 11610483ade4SMatthew G. Knepley ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr); 11620483ade4SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[field], &Nc);CHKERRQ(ierr); 11630483ade4SMatthew G. Knepley cellDof += Nb*Nc; 1164cb1e1211SMatthew G Knepley } 1165cb1e1211SMatthew G Knepley ierr = VecSet(F, 0.0);CHKERRQ(ierr); 1166dcca6d9dSJed Brown ierr = PetscMalloc7(numCells*cellDof,&u,numCells*cellDof,&a,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*cellDof,&elemVec);CHKERRQ(ierr); 1167cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 1168a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 1169cb1e1211SMatthew G Knepley PetscInt i; 1170cb1e1211SMatthew G Knepley 1171cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 1172cb1e1211SMatthew G Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 1173cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 1174cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 1175cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, jctx->u, c, NULL, &x);CHKERRQ(ierr); 1176cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 1177cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) a[c*cellDof+i] = x[i]; 1178cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, X, c, NULL, &x);CHKERRQ(ierr); 1179cb1e1211SMatthew G Knepley } 1180cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 118121454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1182cb1e1211SMatthew G Knepley /* Conforming batches */ 1183cb1e1211SMatthew G Knepley PetscInt numBlocks = 1; 11840483ade4SMatthew G. Knepley PetscInt numBatches = 1; 11850483ade4SMatthew G. Knepley PetscInt numChunks, Ne, blockSize, batchSize; 1186cb1e1211SMatthew G Knepley /* Remainder */ 11870483ade4SMatthew G. Knepley PetscInt Nr, offset; 1188cb1e1211SMatthew G Knepley 11890483ade4SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[field], &quad);CHKERRQ(ierr); 11900483ade4SMatthew G. Knepley ierr = PetscFEGetDimension(fe[field], &Nb);CHKERRQ(ierr); 119121454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 119221454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 11930483ade4SMatthew G. Knepley batchSize = numBlocks * blockSize; 11940483ade4SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 11950483ade4SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 11960483ade4SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 11970483ade4SMatthew G. Knepley offset = numCells - Nr; 11980483ade4SMatthew G. Knepley geom.v0 = v0; 11990483ade4SMatthew G. Knepley geom.J = J; 12000483ade4SMatthew G. Knepley geom.invJ = invJ; 12010483ade4SMatthew G. Knepley geom.detJ = detJ; 12020483ade4SMatthew G. Knepley ierr = PetscFEIntegrateJacobianAction(fe[field], Ne, numFields, fe, field, geom, u, a, fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, elemVec);CHKERRQ(ierr); 12030483ade4SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 12040483ade4SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 12050483ade4SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 12060483ade4SMatthew G. Knepley geom.detJ = &detJ[offset]; 12070483ade4SMatthew G. Knepley ierr = PetscFEIntegrateJacobianAction(fe[field], Nr, numFields, fe, field, geom, &u[offset*cellDof], &a[offset*cellDof], 1208cb1e1211SMatthew G Knepley fem->g0Funcs, fem->g1Funcs, fem->g2Funcs, fem->g3Funcs, &elemVec[offset*cellDof]);CHKERRQ(ierr); 1209cb1e1211SMatthew G Knepley } 1210cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 1211cb1e1211SMatthew G Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, "Jacobian Action", cellDof, &elemVec[c*cellDof]);CHKERRQ(ierr);} 1212cb1e1211SMatthew G Knepley ierr = DMPlexVecSetClosure(dm, NULL, F, c, &elemVec[c*cellDof], ADD_VALUES);CHKERRQ(ierr); 1213cb1e1211SMatthew G Knepley } 1214cb1e1211SMatthew G Knepley ierr = PetscFree7(u,a,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr); 1215cb1e1211SMatthew G Knepley if (mesh->printFEM) { 1216cb1e1211SMatthew G Knepley PetscMPIInt rank, numProcs; 1217cb1e1211SMatthew G Knepley PetscInt p; 1218cb1e1211SMatthew G Knepley 1219cb1e1211SMatthew G Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 1220cb1e1211SMatthew G Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &numProcs);CHKERRQ(ierr); 122186a74ee0SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Jacobian Action:\n");CHKERRQ(ierr); 1222cb1e1211SMatthew G Knepley for (p = 0; p < numProcs; ++p) { 1223cb1e1211SMatthew G Knepley if (p == rank) {ierr = VecView(F, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);} 1224cb1e1211SMatthew G Knepley ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 1225cb1e1211SMatthew G Knepley } 1226cb1e1211SMatthew G Knepley } 12270483ade4SMatthew G. Knepley /* ierr = PetscLogEventEnd(DMPLEX_JacobianActionFEM,dm,0,0,0);CHKERRQ(ierr); */ 1228cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1229cb1e1211SMatthew G Knepley } 1230cb1e1211SMatthew G Knepley 1231cb1e1211SMatthew G Knepley #undef __FUNCT__ 1232cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM" 1233cb1e1211SMatthew G Knepley /*@ 1234cb1e1211SMatthew G Knepley DMPlexComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user. 1235cb1e1211SMatthew G Knepley 1236cb1e1211SMatthew G Knepley Input Parameters: 1237cb1e1211SMatthew G Knepley + dm - The mesh 1238cb1e1211SMatthew G Knepley . X - Local input vector 1239cb1e1211SMatthew G Knepley - user - The user context 1240cb1e1211SMatthew G Knepley 1241cb1e1211SMatthew G Knepley Output Parameter: 1242cb1e1211SMatthew G Knepley . Jac - Jacobian matrix 1243cb1e1211SMatthew G Knepley 1244cb1e1211SMatthew G Knepley Note: 12458026896cSMatthew G. Knepley The first member of the user context must be an FEMContext. 1246cb1e1211SMatthew G Knepley 1247cb1e1211SMatthew G Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1248cb1e1211SMatthew G Knepley like a GPU, or vectorize on a multicore machine. 1249cb1e1211SMatthew G Knepley 12500059ad2aSSatish Balay Level: developer 12510059ad2aSSatish Balay 1252cb1e1211SMatthew G Knepley .seealso: FormFunctionLocal() 1253878cb397SSatish Balay @*/ 12540405ed22SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user) 1255cb1e1211SMatthew G Knepley { 1256cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 12579a559087SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 1258a319912fSMatthew G. Knepley PetscFE *fe = fem->fe; 1259754551f4SMatthew G. Knepley PetscFE *feAux = fem->feAux; 1260a319912fSMatthew G. Knepley const char *name = "Jacobian"; 1261754551f4SMatthew G. Knepley DM dmAux; 1262754551f4SMatthew G. Knepley Vec A; 1263a319912fSMatthew G. Knepley PetscQuadrature quad; 1264a319912fSMatthew G. Knepley PetscCellGeometry geom; 1265754551f4SMatthew G. Knepley PetscSection section, globalSection, sectionAux; 1266cb1e1211SMatthew G Knepley PetscReal *v0, *J, *invJ, *detJ; 1267754551f4SMatthew G. Knepley PetscScalar *elemMat, *u, *a; 1268754551f4SMatthew G. Knepley PetscInt dim, Nf, NfAux = 0, f, fieldI, fieldJ, numCells, cStart, cEnd, c; 1269cb1e1211SMatthew G Knepley PetscInt cellDof = 0, numComponents = 0; 1270754551f4SMatthew G. Knepley PetscInt cellDofAux = 0, numComponentsAux = 0; 1271cb1e1211SMatthew G Knepley PetscBool isShell; 1272cb1e1211SMatthew G Knepley PetscErrorCode ierr; 1273cb1e1211SMatthew G Knepley 1274cb1e1211SMatthew G Knepley PetscFunctionBegin; 1275a319912fSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 1276cb1e1211SMatthew G Knepley ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1277cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1278a319912fSMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 1279754551f4SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 1280cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1281cb1e1211SMatthew G Knepley numCells = cEnd - cStart; 1282754551f4SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1283a319912fSMatthew G. Knepley PetscInt Nb, Nc; 1284a319912fSMatthew G. Knepley 1285a319912fSMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &Nb);CHKERRQ(ierr); 1286a319912fSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr); 1287a319912fSMatthew G. Knepley cellDof += Nb*Nc; 1288a319912fSMatthew G. Knepley numComponents += Nc; 1289cb1e1211SMatthew G Knepley } 1290754551f4SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 1291754551f4SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 1292754551f4SMatthew G. Knepley if (dmAux) { 1293754551f4SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 1294754551f4SMatthew G. Knepley ierr = PetscSectionGetNumFields(sectionAux, &NfAux);CHKERRQ(ierr); 1295754551f4SMatthew G. Knepley } 1296754551f4SMatthew G. Knepley for (f = 0; f < NfAux; ++f) { 1297754551f4SMatthew G. Knepley PetscInt Nb, Nc; 1298754551f4SMatthew G. Knepley 1299754551f4SMatthew G. Knepley ierr = PetscFEGetDimension(feAux[f], &Nb);CHKERRQ(ierr); 1300754551f4SMatthew G. Knepley ierr = PetscFEGetNumComponents(feAux[f], &Nc);CHKERRQ(ierr); 1301754551f4SMatthew G. Knepley cellDofAux += Nb*Nc; 1302754551f4SMatthew G. Knepley numComponentsAux += Nc; 1303754551f4SMatthew G. Knepley } 13043351dd3dSMatthew G. Knepley ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr); 1305cb1e1211SMatthew G Knepley ierr = MatZeroEntries(JacP);CHKERRQ(ierr); 1306dcca6d9dSJed Brown ierr = PetscMalloc6(numCells*cellDof,&u,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*cellDof*cellDof,&elemMat);CHKERRQ(ierr); 1307785e854fSJed Brown if (dmAux) {ierr = PetscMalloc1(numCells*cellDofAux, &a);CHKERRQ(ierr);} 1308cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 1309a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 1310cb1e1211SMatthew G Knepley PetscInt i; 1311cb1e1211SMatthew G Knepley 1312cb1e1211SMatthew G Knepley ierr = DMPlexComputeCellGeometry(dm, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 1313cb1e1211SMatthew G Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 1314a319912fSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 1315cb1e1211SMatthew G Knepley for (i = 0; i < cellDof; ++i) u[c*cellDof+i] = x[i]; 1316a319912fSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr); 1317754551f4SMatthew G. Knepley if (dmAux) { 1318754551f4SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 1319754551f4SMatthew G. Knepley for (i = 0; i < cellDofAux; ++i) a[c*cellDofAux+i] = x[i]; 1320754551f4SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 1321754551f4SMatthew G. Knepley } 1322cb1e1211SMatthew G Knepley } 1323cb1e1211SMatthew G Knepley ierr = PetscMemzero(elemMat, numCells*cellDof*cellDof * sizeof(PetscScalar));CHKERRQ(ierr); 1324754551f4SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 132521454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1326cb1e1211SMatthew G Knepley /* Conforming batches */ 1327754551f4SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1328cb1e1211SMatthew G Knepley /* Remainder */ 1329a319912fSMatthew G. Knepley PetscInt Nr, offset; 1330cb1e1211SMatthew G Knepley 1331754551f4SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[fieldI], &quad);CHKERRQ(ierr); 1332754551f4SMatthew G. Knepley ierr = PetscFEGetDimension(fe[fieldI], &Nb);CHKERRQ(ierr); 1333754551f4SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe[fieldI], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 133421454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 133521454ff5SMatthew G. Knepley blockSize = Nb*numQuadPoints; 1336a319912fSMatthew G. Knepley batchSize = numBlocks * blockSize; 1337754551f4SMatthew G. Knepley ierr = PetscFESetTileSizes(fe[fieldI], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1338a319912fSMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 1339a319912fSMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1340a319912fSMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 1341a319912fSMatthew G. Knepley offset = numCells - Nr; 1342754551f4SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 1343754551f4SMatthew G. Knepley void (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g0Funcs[fieldI*Nf+fieldJ]; 1344754551f4SMatthew G. Knepley void (*g1)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g1Funcs[fieldI*Nf+fieldJ]; 1345754551f4SMatthew G. Knepley void (*g2)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g2Funcs[fieldI*Nf+fieldJ]; 1346754551f4SMatthew G. Knepley void (*g3)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g3Funcs[fieldI*Nf+fieldJ]; 1347754551f4SMatthew G. Knepley 1348a319912fSMatthew G. Knepley geom.v0 = v0; 1349a319912fSMatthew G. Knepley geom.J = J; 1350a319912fSMatthew G. Knepley geom.invJ = invJ; 1351a319912fSMatthew G. Knepley geom.detJ = detJ; 1352754551f4SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe[fieldI], Ne, Nf, fe, fieldI, fieldJ, geom, u, NfAux, feAux, a, g0, g1, g2, g3, elemMat);CHKERRQ(ierr); 1353a319912fSMatthew G. Knepley geom.v0 = &v0[offset*dim]; 1354a319912fSMatthew G. Knepley geom.J = &J[offset*dim*dim]; 1355a319912fSMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 1356a319912fSMatthew G. Knepley geom.detJ = &detJ[offset]; 1357754551f4SMatthew G. Knepley ierr = PetscFEIntegrateJacobian(fe[fieldI], Nr, Nf, fe, fieldI, fieldJ, geom, &u[offset*cellDof], NfAux, feAux, &a[offset*cellDofAux], g0, g1, g2, g3, &elemMat[offset*cellDof*cellDof]);CHKERRQ(ierr); 1358cb1e1211SMatthew G Knepley } 1359cb1e1211SMatthew G Knepley } 1360cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 1361a319912fSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, cellDof, cellDof, &elemMat[c*cellDof*cellDof]);CHKERRQ(ierr);} 1362a319912fSMatthew G. Knepley ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*cellDof*cellDof], ADD_VALUES);CHKERRQ(ierr); 1363cb1e1211SMatthew G Knepley } 1364cb1e1211SMatthew G Knepley ierr = PetscFree6(u,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr); 1365754551f4SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 1366cb1e1211SMatthew G Knepley ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1367cb1e1211SMatthew G Knepley ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1368cb1e1211SMatthew G Knepley if (mesh->printFEM) { 1369a319912fSMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1370cb1e1211SMatthew G Knepley ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr); 1371cb1e1211SMatthew G Knepley ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1372cb1e1211SMatthew G Knepley } 1373a319912fSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr); 1374cb1e1211SMatthew G Knepley ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr); 1375cb1e1211SMatthew G Knepley if (isShell) { 1376cb1e1211SMatthew G Knepley JacActionCtx *jctx; 1377cb1e1211SMatthew G Knepley 1378cb1e1211SMatthew G Knepley ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr); 1379cb1e1211SMatthew G Knepley ierr = VecCopy(X, jctx->u);CHKERRQ(ierr); 1380cb1e1211SMatthew G Knepley } 1381cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1382cb1e1211SMatthew G Knepley } 1383bceba477SMatthew G. Knepley 1384d69c5d34SMatthew G. Knepley #if 0 1385d69c5d34SMatthew G. Knepley 138632356553SMatthew G. Knepley static void g0_identity_1d_static(const PetscScalar u[], const PetscScalar gradU[], const PetscScalar a[], const PetscScalar gradA[], const PetscReal x[], PetscScalar g3[]) 138732356553SMatthew G. Knepley { 138832356553SMatthew G. Knepley g3[0] = 1.0; 138932356553SMatthew G. Knepley } 139032356553SMatthew G. Knepley 139132356553SMatthew G. Knepley static void g0_identity_2d_static(const PetscScalar u[], const PetscScalar gradU[], const PetscScalar a[], const PetscScalar gradA[], const PetscReal x[], PetscScalar g3[]) 139232356553SMatthew G. Knepley { 139332356553SMatthew G. Knepley g3[0] = g3[3] = 1.0; 139432356553SMatthew G. Knepley } 139532356553SMatthew G. Knepley 139632356553SMatthew G. Knepley static void g0_identity_3d_static(const PetscScalar u[], const PetscScalar gradU[], const PetscScalar a[], const PetscScalar gradA[], const PetscReal x[], PetscScalar g3[]) 139732356553SMatthew G. Knepley { 139832356553SMatthew G. Knepley g3[0] = g3[4] = g3[8] = 1.0; 139932356553SMatthew G. Knepley } 140032356553SMatthew G. Knepley 1401bceba477SMatthew G. Knepley #undef __FUNCT__ 1402d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEMBroken" 1403d69c5d34SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEMBroken(DM dmc, DM dmf, Mat I, void *user) 1404bceba477SMatthew G. Knepley { 1405bceba477SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1406bceba477SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 1407bceba477SMatthew G. Knepley PetscFE *fe = fem->fe; 1408bceba477SMatthew G. Knepley const char *name = "Interpolator"; 140932356553SMatthew G. Knepley PetscFE *feRef; 1410bceba477SMatthew G. Knepley PetscQuadrature quad, quadOld; 1411bceba477SMatthew G. Knepley PetscCellGeometry geom; 141232356553SMatthew G. Knepley PetscSection fsection, fglobalSection; 141332356553SMatthew G. Knepley PetscSection csection, cglobalSection; 1414bceba477SMatthew G. Knepley PetscReal *v0, *J, *invJ, *detJ; 1415bceba477SMatthew G. Knepley PetscScalar *elemMat; 1416bceba477SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c; 141732356553SMatthew G. Knepley PetscInt rCellDof = 0, cCellDof = 0, numComponents = 0; 1418bceba477SMatthew G. Knepley PetscErrorCode ierr; 1419bceba477SMatthew G. Knepley 1420bceba477SMatthew G. Knepley PetscFunctionBegin; 1421bceba477SMatthew G. Knepley #if 0 1422bceba477SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1423bceba477SMatthew G. Knepley #endif 1424bceba477SMatthew G. Knepley ierr = DMPlexGetDimension(dmf, &dim);CHKERRQ(ierr); 142532356553SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 142632356553SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 142732356553SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 142832356553SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 142932356553SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1430bceba477SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 1431bceba477SMatthew G. Knepley numCells = cEnd - cStart; 143232356553SMatthew G. Knepley ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr); 143332356553SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 143432356553SMatthew G. Knepley ierr = PetscFERefine(fe[fieldI], &feRef[fieldI]);CHKERRQ(ierr); 143532356553SMatthew G. Knepley } 1436bceba477SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 143732356553SMatthew G. Knepley PetscInt rNb, cNb, Nc; 1438bceba477SMatthew G. Knepley 143932356553SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 144032356553SMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &cNb);CHKERRQ(ierr); 1441bceba477SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr); 1442bceba477SMatthew G. Knepley numComponents += Nc; 144332356553SMatthew G. Knepley rCellDof += rNb*Nc; 144432356553SMatthew G. Knepley cCellDof += cNb*Nc; 1445bceba477SMatthew G. Knepley } 1446bceba477SMatthew G. Knepley ierr = MatZeroEntries(I);CHKERRQ(ierr); 144732356553SMatthew G. Knepley ierr = PetscMalloc5(numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*rCellDof*cCellDof,&elemMat);CHKERRQ(ierr); 1448bceba477SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1449bceba477SMatthew G. Knepley ierr = DMPlexComputeCellGeometry(dmc, c, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr); 1450bceba477SMatthew G. Knepley if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c); 1451bceba477SMatthew G. Knepley } 145232356553SMatthew G. Knepley ierr = PetscMemzero(elemMat, numCells*rCellDof*cCellDof * sizeof(PetscScalar));CHKERRQ(ierr); 1453bceba477SMatthew G. Knepley for (fieldI = 0; fieldI < Nf; ++fieldI) { 1454bceba477SMatthew G. Knepley PetscInt numQuadPoints, Nb; 1455bceba477SMatthew G. Knepley /* Conforming batches */ 1456bceba477SMatthew G. Knepley PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize; 1457bceba477SMatthew G. Knepley /* Remainder */ 1458bceba477SMatthew G. Knepley PetscInt Nr, offset; 1459bceba477SMatthew G. Knepley 1460bceba477SMatthew G. Knepley /* Make new fine FE which refines the ref cell and the quadrature rule */ 146132356553SMatthew G. Knepley ierr = PetscFEGetQuadrature(feRef[fieldI], &quad);CHKERRQ(ierr); 146232356553SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[fieldI], &Nb);CHKERRQ(ierr); 146332356553SMatthew G. Knepley ierr = PetscFEGetTileSizes(feRef[fieldI], NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1464bceba477SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr); 1465bceba477SMatthew G. Knepley blockSize = Nb*numQuadPoints; 1466bceba477SMatthew G. Knepley batchSize = numBlocks * blockSize; 146732356553SMatthew G. Knepley ierr = PetscFESetTileSizes(feRef[fieldI], blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 1468bceba477SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 1469bceba477SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 1470bceba477SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 1471bceba477SMatthew G. Knepley offset = numCells - Nr; 1472d69c5d34SMatthew G. Knepley 1473bceba477SMatthew G. Knepley for (fieldJ = 0; fieldJ < Nf; ++fieldJ) { 147432356553SMatthew G. Knepley /* void (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = fem->g0Funcs[fieldI*Nf+fieldJ]; */ 147532356553SMatthew G. Knepley void (*g0)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal[], PetscScalar[]) = g0_identity_2d_static; 1476bceba477SMatthew G. Knepley 1477bceba477SMatthew G. Knepley /* Replace quadrature in coarse FE with refined quadrature */ 1478bceba477SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe[fieldJ], &quadOld);CHKERRQ(ierr); 1479bceba477SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) quadOld);CHKERRQ(ierr); 1480bceba477SMatthew G. Knepley ierr = PetscFESetQuadrature(fe[fieldJ], quad);CHKERRQ(ierr); 1481bceba477SMatthew G. Knepley geom.v0 = v0; 1482bceba477SMatthew G. Knepley geom.J = J; 1483bceba477SMatthew G. Knepley geom.invJ = invJ; 1484bceba477SMatthew G. Knepley geom.detJ = detJ; 148532356553SMatthew G. Knepley ierr = PetscFEIntegrateInterpolator_Basic(feRef[fieldI], Ne, Nf, feRef, fieldI, fe, fieldJ, geom, g0, elemMat);CHKERRQ(ierr); 1486bceba477SMatthew G. Knepley geom.v0 = &v0[offset*dim]; 1487bceba477SMatthew G. Knepley geom.J = &J[offset*dim*dim]; 1488bceba477SMatthew G. Knepley geom.invJ = &invJ[offset*dim*dim]; 1489bceba477SMatthew G. Knepley geom.detJ = &detJ[offset]; 149032356553SMatthew G. Knepley ierr = PetscFEIntegrateInterpolator_Basic(feRef[fieldI], Nr, Nf, feRef, fieldI, fe, fieldJ, geom, g0, &elemMat[offset*rCellDof*cCellDof]);CHKERRQ(ierr); 1491bceba477SMatthew G. Knepley ierr = PetscFESetQuadrature(fe[fieldJ], quadOld);CHKERRQ(ierr); 1492bceba477SMatthew G. Knepley } 1493bceba477SMatthew G. Knepley } 1494bceba477SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 149532356553SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, rCellDof, cCellDof, &elemMat[c*rCellDof*cCellDof]);CHKERRQ(ierr);} 149632356553SMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, I, c, &elemMat[c*rCellDof*cCellDof], ADD_VALUES);CHKERRQ(ierr); 1497bceba477SMatthew G. Knepley } 1498bceba477SMatthew G. Knepley ierr = PetscFree5(v0,J,invJ,detJ,elemMat);CHKERRQ(ierr); 149932356553SMatthew G. Knepley ierr = PetscFree(feRef);CHKERRQ(ierr); 1500bceba477SMatthew G. Knepley ierr = MatAssemblyBegin(I, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1501bceba477SMatthew G. Knepley ierr = MatAssemblyEnd(I, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1502bceba477SMatthew G. Knepley if (mesh->printFEM) { 1503bceba477SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1504bceba477SMatthew G. Knepley ierr = MatChop(I, 1.0e-10);CHKERRQ(ierr); 1505bceba477SMatthew G. Knepley ierr = MatView(I, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1506bceba477SMatthew G. Knepley } 1507bceba477SMatthew G. Knepley #if 0 1508bceba477SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1509bceba477SMatthew G. Knepley #endif 1510bceba477SMatthew G. Knepley PetscFunctionReturn(0); 1511bceba477SMatthew G. Knepley } 1512d69c5d34SMatthew G. Knepley #endif 1513d69c5d34SMatthew G. Knepley 1514d69c5d34SMatthew G. Knepley #undef __FUNCT__ 1515d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM" 1516d69c5d34SMatthew G. Knepley /*@ 1517d69c5d34SMatthew G. Knepley DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1518d69c5d34SMatthew G. Knepley 1519d69c5d34SMatthew G. Knepley Input Parameters: 1520d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1521d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1522d69c5d34SMatthew G. Knepley - user - The user context 1523d69c5d34SMatthew G. Knepley 1524d69c5d34SMatthew G. Knepley Output Parameter: 1525934789fcSMatthew G. Knepley . In - The interpolation matrix 1526d69c5d34SMatthew G. Knepley 1527d69c5d34SMatthew G. Knepley Note: 1528d69c5d34SMatthew G. Knepley The first member of the user context must be an FEMContext. 1529d69c5d34SMatthew G. Knepley 1530d69c5d34SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1531d69c5d34SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 1532d69c5d34SMatthew G. Knepley 1533d69c5d34SMatthew G. Knepley Level: developer 1534d69c5d34SMatthew G. Knepley 1535d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM() 1536d69c5d34SMatthew G. Knepley @*/ 1537934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user) 1538d69c5d34SMatthew G. Knepley { 1539d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1540d69c5d34SMatthew G. Knepley PetscFEM *fem = (PetscFEM *) user; 1541d69c5d34SMatthew G. Knepley PetscFE *fe = fem->fe; 1542d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 1543d69c5d34SMatthew G. Knepley PetscFE *feRef; 1544d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1545d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1546d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 1547942a7a06SMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c; 1548d69c5d34SMatthew G. Knepley PetscInt rCellDof = 0, cCellDof = 0; 1549d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1550d69c5d34SMatthew G. Knepley 1551d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1552d69c5d34SMatthew G. Knepley #if 0 1553d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1554d69c5d34SMatthew G. Knepley #endif 1555d69c5d34SMatthew G. Knepley ierr = DMPlexGetDimension(dmf, &dim);CHKERRQ(ierr); 1556d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1557d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1558d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1559d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1560d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1561d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 1562d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(Nf,&feRef);CHKERRQ(ierr); 1563d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1564d69c5d34SMatthew G. Knepley PetscInt rNb, cNb, Nc; 1565d69c5d34SMatthew G. Knepley 1566d69c5d34SMatthew G. Knepley ierr = PetscFERefine(fe[f], &feRef[f]);CHKERRQ(ierr); 1567d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 1568d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(fe[f], &cNb);CHKERRQ(ierr); 1569d69c5d34SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[f], &Nc);CHKERRQ(ierr); 1570d69c5d34SMatthew G. Knepley rCellDof += rNb*Nc; 1571d69c5d34SMatthew G. Knepley cCellDof += cNb*Nc; 1572d69c5d34SMatthew G. Knepley } 1573934789fcSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1574d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(rCellDof*cCellDof,&elemMat);CHKERRQ(ierr); 1575d69c5d34SMatthew G. Knepley ierr = PetscMemzero(elemMat, rCellDof*cCellDof * sizeof(PetscScalar));CHKERRQ(ierr); 1576d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1577d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1578d69c5d34SMatthew G. Knepley PetscQuadrature f; 1579d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1580d69c5d34SMatthew G. Knepley PetscReal *points; 1581d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1582d69c5d34SMatthew G. Knepley 1583d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 1584d69c5d34SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[fieldI], &Nc);CHKERRQ(ierr); 1585d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 1586d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1587d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1588d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1589d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1590d69c5d34SMatthew G. Knepley npoints += Np; 1591d69c5d34SMatthew G. Knepley } 1592d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1593d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1594d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1595d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1596d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1597d69c5d34SMatthew G. Knepley } 1598d69c5d34SMatthew G. Knepley 1599d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 1600d69c5d34SMatthew G. Knepley PetscReal *B; 160136a6d9c0SMatthew G. Knepley PetscInt NcJ, cpdim, j; 1602d69c5d34SMatthew G. Knepley 1603d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 160436a6d9c0SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe[fieldJ], &NcJ);CHKERRQ(ierr); 160536a6d9c0SMatthew 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); 1606d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(fe[fieldJ], &cpdim);CHKERRQ(ierr); 1607ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1608ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 1609d69c5d34SMatthew G. Knepley ierr = PetscFEGetTabulation(fe[fieldJ], npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1610d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1611d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1612d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1613d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 161436a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 161536a6d9c0SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cCellDof + offsetJ + j*NcJ + c] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p]; 161636a6d9c0SMatthew G. Knepley } 1617d69c5d34SMatthew G. Knepley } 1618d69c5d34SMatthew G. Knepley } 1619d69c5d34SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe[fieldJ], npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1620ffe73a53SMatthew G. Knepley } 162136a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1622d69c5d34SMatthew G. Knepley } 1623d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1624549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1625d69c5d34SMatthew G. Knepley } 1626d69c5d34SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rCellDof, cCellDof, elemMat);CHKERRQ(ierr);} 1627d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1628934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1629d69c5d34SMatthew G. Knepley } 1630549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 1631d69c5d34SMatthew G. Knepley ierr = PetscFree(feRef);CHKERRQ(ierr); 1632549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1633934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1634934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1635d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1636d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1637934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1638934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1639d69c5d34SMatthew G. Knepley } 1640d69c5d34SMatthew G. Knepley #if 0 1641d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1642d69c5d34SMatthew G. Knepley #endif 1643d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1644d69c5d34SMatthew G. Knepley } 16456c73c22cSMatthew G. Knepley 16466c73c22cSMatthew G. Knepley #undef __FUNCT__ 16476c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexAddBoundary" 16486c73c22cSMatthew G. Knepley /* The ids can be overridden by the command line option -bc_<boundary name> */ 16496c73c22cSMatthew G. Knepley PetscErrorCode DMPlexAddBoundary(DM dm, PetscBool isEssential, const char name[], PetscInt field, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx) 16506c73c22cSMatthew G. Knepley { 16516c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 16526c73c22cSMatthew G. Knepley DMBoundary b; 16536c73c22cSMatthew G. Knepley PetscErrorCode ierr; 16546c73c22cSMatthew G. Knepley 16556c73c22cSMatthew G. Knepley PetscFunctionBegin; 16566c73c22cSMatthew G. Knepley ierr = PetscNew(&b);CHKERRQ(ierr); 16576c73c22cSMatthew G. Knepley ierr = PetscStrallocpy(name, (char **) &b->name);CHKERRQ(ierr); 16586c73c22cSMatthew G. Knepley ierr = PetscMalloc1(numids, &b->ids);CHKERRQ(ierr); 16596c73c22cSMatthew G. Knepley ierr = PetscMemcpy(b->ids, ids, numids*sizeof(PetscInt));CHKERRQ(ierr); 16606c73c22cSMatthew G. Knepley b->essential = isEssential; 16616c73c22cSMatthew G. Knepley b->field = field; 16626c73c22cSMatthew G. Knepley b->func = bcFunc; 16636c73c22cSMatthew G. Knepley b->numids = numids; 16646c73c22cSMatthew G. Knepley b->ctx = ctx; 16656c73c22cSMatthew G. Knepley b->next = mesh->boundary; 16666c73c22cSMatthew G. Knepley mesh->boundary = b; 16676c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 16686c73c22cSMatthew G. Knepley } 16696c73c22cSMatthew G. Knepley 16706c73c22cSMatthew G. Knepley #undef __FUNCT__ 16716c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumBoundary" 16726c73c22cSMatthew G. Knepley PetscErrorCode DMPlexGetNumBoundary(DM dm, PetscInt *numBd) 16736c73c22cSMatthew G. Knepley { 16746c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 16756c73c22cSMatthew G. Knepley DMBoundary b = mesh->boundary; 16766c73c22cSMatthew G. Knepley 16776c73c22cSMatthew G. Knepley PetscFunctionBegin; 16786c73c22cSMatthew G. Knepley *numBd = 0; 16796c73c22cSMatthew G. Knepley while (b) {++(*numBd); b = b->next;} 16806c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 16816c73c22cSMatthew G. Knepley } 16826c73c22cSMatthew G. Knepley 16836c73c22cSMatthew G. Knepley #undef __FUNCT__ 16846c73c22cSMatthew G. Knepley #define __FUNCT__ "DMPlexGetBoundary" 16856c73c22cSMatthew G. Knepley PetscErrorCode DMPlexGetBoundary(DM dm, PetscInt bd, PetscBool *isEssential, const char **name, PetscInt *field, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx) 16866c73c22cSMatthew G. Knepley { 16876c73c22cSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 16886c73c22cSMatthew G. Knepley DMBoundary b = mesh->boundary; 16896c73c22cSMatthew G. Knepley PetscInt n = 0; 16906c73c22cSMatthew G. Knepley 16916c73c22cSMatthew G. Knepley PetscFunctionBegin; 16926c73c22cSMatthew G. Knepley while (b) { 16936c73c22cSMatthew G. Knepley if (n == bd) break; 16946c73c22cSMatthew G. Knepley b = b->next; 16956c73c22cSMatthew G. Knepley ++n; 16966c73c22cSMatthew G. Knepley } 16976c73c22cSMatthew G. Knepley if (n != bd) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boundary %d is not in [0, %d)", bd, n); 16986c73c22cSMatthew G. Knepley if (isEssential) { 16996c73c22cSMatthew G. Knepley PetscValidPointer(isEssential, 3); 17006c73c22cSMatthew G. Knepley *isEssential = b->essential; 17016c73c22cSMatthew G. Knepley } 17026c73c22cSMatthew G. Knepley if (name) { 17036c73c22cSMatthew G. Knepley PetscValidPointer(name, 4); 17046c73c22cSMatthew G. Knepley *name = b->name; 17056c73c22cSMatthew G. Knepley } 17066c73c22cSMatthew G. Knepley if (field) { 17076c73c22cSMatthew G. Knepley PetscValidPointer(field, 5); 17086c73c22cSMatthew G. Knepley *field = b->field; 17096c73c22cSMatthew G. Knepley } 17106c73c22cSMatthew G. Knepley if (func) { 17116c73c22cSMatthew G. Knepley PetscValidPointer(func, 6); 17126c73c22cSMatthew G. Knepley *func = b->func; 17136c73c22cSMatthew G. Knepley } 17146c73c22cSMatthew G. Knepley if (numids) { 17156c73c22cSMatthew G. Knepley PetscValidPointer(numids, 7); 17166c73c22cSMatthew G. Knepley *numids = b->numids; 17176c73c22cSMatthew G. Knepley } 17186c73c22cSMatthew G. Knepley if (ids) { 17196c73c22cSMatthew G. Knepley PetscValidPointer(ids, 8); 17206c73c22cSMatthew G. Knepley *ids = b->ids; 17216c73c22cSMatthew G. Knepley } 17226c73c22cSMatthew G. Knepley if (ctx) { 17236c73c22cSMatthew G. Knepley PetscValidPointer(ctx, 9); 17246c73c22cSMatthew G. Knepley *ctx = b->ctx; 17256c73c22cSMatthew G. Knepley } 17266c73c22cSMatthew G. Knepley PetscFunctionReturn(0); 17276c73c22cSMatthew G. Knepley } 1728