1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2da97024aSMatthew G. Knepley #include <petscsf.h> 3cb1e1211SMatthew G Knepley 4af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h> 5af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h> 6a0845e3aSMatthew G. Knepley 7cb1e1211SMatthew G Knepley #undef __FUNCT__ 8cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexGetScale" 9cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale) 10cb1e1211SMatthew G Knepley { 11cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 12cb1e1211SMatthew G Knepley 13cb1e1211SMatthew G Knepley PetscFunctionBegin; 14cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 15cb1e1211SMatthew G Knepley PetscValidPointer(scale, 3); 16cb1e1211SMatthew G Knepley *scale = mesh->scale[unit]; 17cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 18cb1e1211SMatthew G Knepley } 19cb1e1211SMatthew G Knepley 20cb1e1211SMatthew G Knepley #undef __FUNCT__ 21cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexSetScale" 22cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale) 23cb1e1211SMatthew G Knepley { 24cb1e1211SMatthew G Knepley DM_Plex *mesh = (DM_Plex*) dm->data; 25cb1e1211SMatthew G Knepley 26cb1e1211SMatthew G Knepley PetscFunctionBegin; 27cb1e1211SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 28cb1e1211SMatthew G Knepley mesh->scale[unit] = scale; 29cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 30cb1e1211SMatthew G Knepley } 31cb1e1211SMatthew G Knepley 32cb1e1211SMatthew G Knepley PETSC_STATIC_INLINE PetscInt epsilon(PetscInt i, PetscInt j, PetscInt k) 33cb1e1211SMatthew G Knepley { 34cb1e1211SMatthew G Knepley switch (i) { 35cb1e1211SMatthew G Knepley case 0: 36cb1e1211SMatthew G Knepley switch (j) { 37cb1e1211SMatthew G Knepley case 0: return 0; 38cb1e1211SMatthew G Knepley case 1: 39cb1e1211SMatthew G Knepley switch (k) { 40cb1e1211SMatthew G Knepley case 0: return 0; 41cb1e1211SMatthew G Knepley case 1: return 0; 42cb1e1211SMatthew G Knepley case 2: return 1; 43cb1e1211SMatthew G Knepley } 44cb1e1211SMatthew G Knepley case 2: 45cb1e1211SMatthew G Knepley switch (k) { 46cb1e1211SMatthew G Knepley case 0: return 0; 47cb1e1211SMatthew G Knepley case 1: return -1; 48cb1e1211SMatthew G Knepley case 2: return 0; 49cb1e1211SMatthew G Knepley } 50cb1e1211SMatthew G Knepley } 51cb1e1211SMatthew G Knepley case 1: 52cb1e1211SMatthew G Knepley switch (j) { 53cb1e1211SMatthew G Knepley case 0: 54cb1e1211SMatthew G Knepley switch (k) { 55cb1e1211SMatthew G Knepley case 0: return 0; 56cb1e1211SMatthew G Knepley case 1: return 0; 57cb1e1211SMatthew G Knepley case 2: return -1; 58cb1e1211SMatthew G Knepley } 59cb1e1211SMatthew G Knepley case 1: return 0; 60cb1e1211SMatthew G Knepley case 2: 61cb1e1211SMatthew G Knepley switch (k) { 62cb1e1211SMatthew G Knepley case 0: return 1; 63cb1e1211SMatthew G Knepley case 1: return 0; 64cb1e1211SMatthew G Knepley case 2: return 0; 65cb1e1211SMatthew G Knepley } 66cb1e1211SMatthew G Knepley } 67cb1e1211SMatthew G Knepley case 2: 68cb1e1211SMatthew G Knepley switch (j) { 69cb1e1211SMatthew G Knepley case 0: 70cb1e1211SMatthew G Knepley switch (k) { 71cb1e1211SMatthew G Knepley case 0: return 0; 72cb1e1211SMatthew G Knepley case 1: return 1; 73cb1e1211SMatthew G Knepley case 2: return 0; 74cb1e1211SMatthew G Knepley } 75cb1e1211SMatthew G Knepley case 1: 76cb1e1211SMatthew G Knepley switch (k) { 77cb1e1211SMatthew G Knepley case 0: return -1; 78cb1e1211SMatthew G Knepley case 1: return 0; 79cb1e1211SMatthew G Knepley case 2: return 0; 80cb1e1211SMatthew G Knepley } 81cb1e1211SMatthew G Knepley case 2: return 0; 82cb1e1211SMatthew G Knepley } 83cb1e1211SMatthew G Knepley } 84cb1e1211SMatthew G Knepley return 0; 85cb1e1211SMatthew G Knepley } 86cb1e1211SMatthew G Knepley 87cb1e1211SMatthew G Knepley #undef __FUNCT__ 88026175e5SToby Isaac #define __FUNCT__ "DMPlexProjectRigidBody" 89ad917190SMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody(PetscInt dim, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx) 90026175e5SToby Isaac { 91026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *) ctx; 92ad917190SMatthew G. Knepley PetscInt dim2 = ctxInt[0]; 93026175e5SToby Isaac PetscInt d = ctxInt[1]; 94026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 95026175e5SToby Isaac 96ad917190SMatthew G. Knepley PetscFunctionBegin; 97ad917190SMatthew G. Knepley if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2); 98026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 99026175e5SToby Isaac if (d < dim) { 100026175e5SToby Isaac mode[d] = 1.; 101ad917190SMatthew G. Knepley } else { 102026175e5SToby Isaac for (i = 0; i < dim; i++) { 103026175e5SToby Isaac for (j = 0; j < dim; j++) { 104026175e5SToby Isaac mode[j] += epsilon(i, j, k)*X[i]; 105026175e5SToby Isaac } 106026175e5SToby Isaac } 107026175e5SToby Isaac } 108ad917190SMatthew G. Knepley PetscFunctionReturn(0); 109026175e5SToby Isaac } 110026175e5SToby Isaac 111026175e5SToby Isaac #undef __FUNCT__ 112cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 113cb1e1211SMatthew G Knepley /*@C 114026175e5SToby Isaac DMPlexCreateRigidBody - for the default global section, create rigid body modes from coordinates 115cb1e1211SMatthew G Knepley 116cb1e1211SMatthew G Knepley Collective on DM 117cb1e1211SMatthew G Knepley 118cb1e1211SMatthew G Knepley Input Arguments: 119026175e5SToby Isaac . dm - the DM 120cb1e1211SMatthew G Knepley 121cb1e1211SMatthew G Knepley Output Argument: 122cb1e1211SMatthew G Knepley . sp - the null space 123cb1e1211SMatthew G Knepley 124cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 125cb1e1211SMatthew G Knepley 126cb1e1211SMatthew G Knepley Level: advanced 127cb1e1211SMatthew G Knepley 128cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 129cb1e1211SMatthew G Knepley @*/ 130026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp) 131cb1e1211SMatthew G Knepley { 132cb1e1211SMatthew G Knepley MPI_Comm comm; 133026175e5SToby Isaac Vec mode[6]; 134026175e5SToby Isaac PetscSection section, globalSection; 1359d8fbdeaSMatthew G. Knepley PetscInt dim, dimEmbed, n, m, d, i, j; 136cb1e1211SMatthew G Knepley PetscErrorCode ierr; 137cb1e1211SMatthew G Knepley 138cb1e1211SMatthew G Knepley PetscFunctionBegin; 139cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 140c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1419d8fbdeaSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 142cb1e1211SMatthew G Knepley if (dim == 1) { 143cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 144cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 145cb1e1211SMatthew G Knepley } 146026175e5SToby Isaac ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 147026175e5SToby Isaac ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 148cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 149cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 150cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 151cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 152cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 153cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 154026175e5SToby Isaac for (d = 0; d < m; d++) { 155330485fdSToby Isaac PetscInt ctx[2]; 156026175e5SToby Isaac void *voidctx = (void *) (&ctx[0]); 157ad917190SMatthew G. Knepley PetscErrorCode (*func)(PetscInt, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody; 158cb1e1211SMatthew G Knepley 1599d8fbdeaSMatthew G. Knepley ctx[0] = dimEmbed; 160330485fdSToby Isaac ctx[1] = d; 161*4d6f44ffSToby Isaac ierr = DMProjectFunction(dm, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 162cb1e1211SMatthew G Knepley } 163cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 164cb1e1211SMatthew G Knepley /* Orthonormalize system */ 165cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 166cb1e1211SMatthew G Knepley PetscScalar dots[6]; 167cb1e1211SMatthew G Knepley 168cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 169cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 170cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 171cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 172cb1e1211SMatthew G Knepley } 173cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 174cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 175cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 176cb1e1211SMatthew G Knepley } 177cb1e1211SMatthew G Knepley 178cb1e1211SMatthew G Knepley #undef __FUNCT__ 179b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight" 180b29cfa1cSToby Isaac /*@ 181b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 182b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 183b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 184b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 185b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 186b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 187b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 188b29cfa1cSToby Isaac 189b29cfa1cSToby Isaac Input Parameters: 190b29cfa1cSToby Isaac + dm - the DMPlex object 191b29cfa1cSToby Isaac - height - the maximum projection height >= 0 192b29cfa1cSToby Isaac 193b29cfa1cSToby Isaac Level: advanced 194b29cfa1cSToby Isaac 195*4d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 196b29cfa1cSToby Isaac @*/ 197b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 198b29cfa1cSToby Isaac { 199b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 200b29cfa1cSToby Isaac 201b29cfa1cSToby Isaac PetscFunctionBegin; 202b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 203b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 204b29cfa1cSToby Isaac PetscFunctionReturn(0); 205b29cfa1cSToby Isaac } 206b29cfa1cSToby Isaac 207b29cfa1cSToby Isaac #undef __FUNCT__ 208b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight" 209b29cfa1cSToby Isaac /*@ 210b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 211b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 212b29cfa1cSToby Isaac 213b29cfa1cSToby Isaac Input Parameters: 214b29cfa1cSToby Isaac . dm - the DMPlex object 215b29cfa1cSToby Isaac 216b29cfa1cSToby Isaac Output Parameters: 217b29cfa1cSToby Isaac . height - the maximum projection height 218b29cfa1cSToby Isaac 219b29cfa1cSToby Isaac Level: intermediate 220b29cfa1cSToby Isaac 221*4d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal() 222b29cfa1cSToby Isaac @*/ 223b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 224b29cfa1cSToby Isaac { 225b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 226b29cfa1cSToby Isaac 227b29cfa1cSToby Isaac PetscFunctionBegin; 228b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 229b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 230b29cfa1cSToby Isaac PetscFunctionReturn(0); 231b29cfa1cSToby Isaac } 232b29cfa1cSToby Isaac 233b29cfa1cSToby Isaac #undef __FUNCT__ 234*4d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal_plex" 235*4d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal_plex(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 236a18a7fb9SMatthew G. Knepley { 2377d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 238dda36da7SMatthew G. Knepley PetscInt *numComp; 239a18a7fb9SMatthew G. Knepley PetscSection section; 240a18a7fb9SMatthew G. Knepley PetscScalar *values; 241ad96f515SMatthew G. Knepley PetscBool *fieldActive; 242dda36da7SMatthew G. Knepley PetscInt numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h; 243a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 244a18a7fb9SMatthew G. Knepley 245a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 2469ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2479ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2489ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2499ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 250c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2517a1a1bd4SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 252a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 253a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 254dda36da7SMatthew G. Knepley ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr); 2557d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 2567d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 2579ac3fadcSMatthew G. Knepley if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);} 2589ac3fadcSMatthew G. Knepley else {cellsp = sp;} 2597d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 2607d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 261fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 2627d1dd11eSToby Isaac if (pEnd <= pStart) continue; 2637d1dd11eSToby Isaac totDim = 0; 264a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 2659ac3fadcSMatthew G. Knepley PetscObject obj; 2669ac3fadcSMatthew G. Knepley PetscClassId id; 2679ac3fadcSMatthew G. Knepley 2689ac3fadcSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 2699ac3fadcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2709ac3fadcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 2719ac3fadcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2729ac3fadcSMatthew G. Knepley 273dda36da7SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 2747d1dd11eSToby Isaac if (!h) { 275ee2838f6SToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 2767d1dd11eSToby Isaac sp[f] = cellsp[f]; 2779ac3fadcSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2789ac3fadcSMatthew G. Knepley } else { 2797d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 2807d1dd11eSToby Isaac if (!sp[f]) continue; 2817d1dd11eSToby Isaac } 2829ac3fadcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 2839ac3fadcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 2849ac3fadcSMatthew G. Knepley 285dda36da7SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 286302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 287e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2889ac3fadcSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 289a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 290dda36da7SMatthew G. Knepley totDim += spDim*numComp[f]; 291a18a7fb9SMatthew G. Knepley } 2927d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 2937d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 294d374af2bSToby Isaac if (!totDim) continue; 295a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 296ad96f515SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 297ee2838f6SToby Isaac for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE; 298a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 299a18a7fb9SMatthew G. Knepley IS pointIS; 300a18a7fb9SMatthew G. Knepley const PetscInt *points; 301a18a7fb9SMatthew G. Knepley PetscInt n, p; 302a18a7fb9SMatthew G. Knepley 303a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 304a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 305a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 306a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 307a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 308e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 309a18a7fb9SMatthew G. Knepley 3107d1dd11eSToby Isaac if ((point < pStart) || (point >= pEnd)) continue; 311e1d0b1adSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 312ee2838f6SToby Isaac geom.dim = dim - h; 3137a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 314a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 315a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 316bf3434eeSMatthew G. Knepley 3177d1dd11eSToby Isaac if (!sp[f]) continue; 318a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 319a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 320a18a7fb9SMatthew G. Knepley if (funcs[f]) { 321dda36da7SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 322a18a7fb9SMatthew G. Knepley } else { 323dda36da7SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 324a18a7fb9SMatthew G. Knepley } 325dda36da7SMatthew G. Knepley v += numComp[f]; 326a18a7fb9SMatthew G. Knepley } 327a18a7fb9SMatthew G. Knepley } 328ad96f515SMatthew G. Knepley ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr); 329a18a7fb9SMatthew G. Knepley } 330a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 331a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 332a18a7fb9SMatthew G. Knepley } 3337d1dd11eSToby Isaac if (h) { 3347d1dd11eSToby Isaac for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 3357d1dd11eSToby Isaac } 3367d1dd11eSToby Isaac } 337a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 338ad96f515SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 3399ac3fadcSMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 340dda36da7SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 3417d1dd11eSToby Isaac if (maxHeight > 0) { 3427d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 3437d1dd11eSToby Isaac } 344a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 345a18a7fb9SMatthew G. Knepley } 346a18a7fb9SMatthew G. Knepley 347a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 348*4d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal_plex" 349*4d6f44ffSToby Isaac PetscErrorCode DMProjectFunctionLocal_plex(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 350cb1e1211SMatthew G Knepley { 3517d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 35215496722SMatthew G. Knepley PetscInt *numComp; 35372f94c41SMatthew G. Knepley PetscSection section; 35472f94c41SMatthew G. Knepley PetscScalar *values; 355b793a5ffSMatthew G. Knepley PetscInt Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight; 356cb1e1211SMatthew G Knepley PetscErrorCode ierr; 357cb1e1211SMatthew G Knepley 358cb1e1211SMatthew G Knepley PetscFunctionBegin; 3599ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 3609ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 3619ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 3629ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 363cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 364b793a5ffSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 365b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &numComp);CHKERRQ(ierr); 3667d1dd11eSToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 367ee2838f6SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 3687d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 3697d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 3707d1dd11eSToby Isaac if (maxHeight > 0) { 371b793a5ffSMatthew G. Knepley ierr = PetscMalloc1(Nf,&cellsp);CHKERRQ(ierr); 3727d1dd11eSToby Isaac } 373048b7b1eSToby Isaac else { 374048b7b1eSToby Isaac cellsp = sp; 375048b7b1eSToby Isaac } 3767d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 3777d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 378fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 379048b7b1eSToby Isaac if (pEnd <= pStart) continue; 3807d1dd11eSToby Isaac totDim = 0; 381b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 38215496722SMatthew G. Knepley PetscObject obj; 38315496722SMatthew G. Knepley PetscClassId id; 3840f2d7e86SMatthew G. Knepley 38515496722SMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 38615496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 38715496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 38815496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 38915496722SMatthew G. Knepley 39015496722SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 3917d1dd11eSToby Isaac if (!h) { 3927d1dd11eSToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 3937d1dd11eSToby Isaac sp[f] = cellsp[f]; 39415496722SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 3957d1dd11eSToby Isaac } 3967d1dd11eSToby Isaac else { 3977d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 3987d1dd11eSToby Isaac if (!sp[f]) { 3997d1dd11eSToby Isaac continue; 4007d1dd11eSToby Isaac } 4017d1dd11eSToby Isaac } 40215496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 40315496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 40415496722SMatthew G. Knepley 405976fa17dSToby Isaac if (!h) { 406976fa17dSToby Isaac ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 407976fa17dSToby Isaac ierr = PetscFVGetDualSpace(fv, &cellsp[f]);CHKERRQ(ierr); 408976fa17dSToby Isaac ierr = PetscObjectReference((PetscObject) cellsp[f]);CHKERRQ(ierr); 409976fa17dSToby Isaac sp[f] = cellsp[f]; 410976fa17dSToby Isaac } 411976fa17dSToby Isaac else { 41277d9c660SToby Isaac sp[f] = NULL; 41377d9c660SToby Isaac continue; /* FV doesn't have fields that can be evaluated at faces, corners, etc. */ 41477d9c660SToby Isaac } 41515496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 41672f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 41715496722SMatthew G. Knepley totDim += spDim*numComp[f]; 418cb1e1211SMatthew G Knepley } 4197d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 4207d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 421d374af2bSToby Isaac if (!totDim) continue; 42272f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4237d1dd11eSToby Isaac for (p = pStart; p < pEnd; ++p) { 424e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 425cb1e1211SMatthew G Knepley 42631383a9bSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 427910c25dcSToby Isaac geom.dim = dim - h; 4287a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 429b793a5ffSMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 430c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[f] : NULL; 4310f2d7e86SMatthew G. Knepley 4327d1dd11eSToby Isaac if (!sp[f]) continue; 43372f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 43472f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 435120386c5SMatthew G. Knepley if (funcs[f]) { 436e1d0b1adSMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 437120386c5SMatthew G. Knepley } else { 43815496722SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 439120386c5SMatthew G. Knepley } 44015496722SMatthew G. Knepley v += numComp[f]; 441cb1e1211SMatthew G Knepley } 442cb1e1211SMatthew G Knepley } 4437d1dd11eSToby Isaac ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr); 444cb1e1211SMatthew G Knepley } 44572f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 446ee2838f6SToby Isaac if (h || !maxHeight) { 447b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 4487d1dd11eSToby Isaac } 4497d1dd11eSToby Isaac } 45015496722SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 4517d1dd11eSToby Isaac if (maxHeight > 0) { 452b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);} 4537d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 4547d1dd11eSToby Isaac } 455cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 456cb1e1211SMatthew G Knepley } 457cb1e1211SMatthew G Knepley 458cb1e1211SMatthew G Knepley #undef __FUNCT__ 4590f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal" 4603bc3b0a0SMatthew G. Knepley PetscErrorCode DMPlexProjectFieldLocal(DM dm, Vec localU, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec localX) 4610f2d7e86SMatthew G. Knepley { 4620f2d7e86SMatthew G. Knepley DM dmAux; 463d5396abcSMatthew G. Knepley PetscDS prob, probAux = NULL; 4640f2d7e86SMatthew G. Knepley Vec A; 465d5396abcSMatthew G. Knepley PetscSection section, sectionAux = NULL; 466b793a5ffSMatthew G. Knepley PetscDualSpace *sp; 467b793a5ffSMatthew G. Knepley PetscInt *Ncf; 468326413afSMatthew G. Knepley PetscScalar *values, *u, *u_x, *a, *a_x; 469d5396abcSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 4709ac3fadcSMatthew G. Knepley PetscInt Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight; 4710f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 4720f2d7e86SMatthew G. Knepley 4730f2d7e86SMatthew G. Knepley PetscFunctionBegin; 474048b7b1eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 475048b7b1eSToby Isaac if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");} 4762764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 477c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 4780f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 4790f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 480b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr); 4810f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 4822764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 4832764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr); 4842764a2aaSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 4850f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 4860f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 4870f2d7e86SMatthew G. Knepley if (dmAux) { 4882764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 489326413afSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 4902764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr); 4910f2d7e86SMatthew G. Knepley } 492d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 4930f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 4940f2d7e86SMatthew 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); 4950f2d7e86SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4960f2d7e86SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 4979ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 4989ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 4990f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 500326413afSMatthew G. Knepley PetscScalar *coefficients = NULL, *coefficientsAux = NULL; 501326413afSMatthew G. Knepley 5028e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 503326413afSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 504326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5050f2d7e86SMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 506d5396abcSMatthew G. Knepley PetscObject obj; 507d5396abcSMatthew G. Knepley PetscClassId id; 5083113607cSMatthew G. Knepley 509d5396abcSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 510d5396abcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 511d5396abcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 512d5396abcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 513d5396abcSMatthew G. Knepley 514b793a5ffSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 515b793a5ffSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 516b793a5ffSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr); 517d5396abcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 518d5396abcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 519d5396abcSMatthew G. Knepley 520b793a5ffSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr); 521302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 522e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 523d5396abcSMatthew G. Knepley } 524b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 5250f2d7e86SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 5260f2d7e86SMatthew G. Knepley PetscQuadrature quad; 5270f2d7e86SMatthew G. Knepley const PetscReal *points, *weights; 5280f2d7e86SMatthew G. Knepley PetscInt numPoints, q; 5290f2d7e86SMatthew G. Knepley 5300f2d7e86SMatthew G. Knepley if (funcs[f]) { 531b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr); 5320f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr); 5330f2d7e86SMatthew G. Knepley for (q = 0; q < numPoints; ++q) { 5340f2d7e86SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x); 535326413afSMatthew G. Knepley ierr = EvaluateFieldJets(prob, PETSC_FALSE, q, invJ, coefficients, NULL, u, u_x, NULL);CHKERRQ(ierr); 536326413afSMatthew G. Knepley ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr); 5373bc3b0a0SMatthew G. Knepley (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]); 5380f2d7e86SMatthew G. Knepley } 5390f2d7e86SMatthew G. Knepley } else { 540b793a5ffSMatthew G. Knepley for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0; 5410f2d7e86SMatthew G. Knepley } 542b793a5ffSMatthew G. Knepley v += Ncf[f]; 5430f2d7e86SMatthew G. Knepley } 5440f2d7e86SMatthew G. Knepley } 545326413afSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 546326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5470f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 5480f2d7e86SMatthew G. Knepley } 5490f2d7e86SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 5500f2d7e86SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 551b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 552b793a5ffSMatthew G. Knepley ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr); 5530f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 5540f2d7e86SMatthew G. Knepley } 5550f2d7e86SMatthew G. Knepley 5560f2d7e86SMatthew G. Knepley #undef __FUNCT__ 557d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal" 558ad917190SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX) 55955f2e967SMatthew G. Knepley { 560ad917190SMatthew G. Knepley PetscErrorCode (**funcs)(PetscInt, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 56155f2e967SMatthew G. Knepley void **ctxs; 562d7ddef95SMatthew G. Knepley PetscInt numFields; 563d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 564d7ddef95SMatthew G. Knepley 565d7ddef95SMatthew G. Knepley PetscFunctionBegin; 566d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 567d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 568d7ddef95SMatthew G. Knepley funcs[field] = func; 569d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 570*4d6f44ffSToby Isaac ierr = DMProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 571d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 572d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 573d7ddef95SMatthew G. Knepley } 574d7ddef95SMatthew G. Knepley 575d7ddef95SMatthew G. Knepley #undef __FUNCT__ 576d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal" 5770e0f25f2SMatthew G. Knepley /* This ignores numcomps/comps */ 578d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, 579d7ddef95SMatthew G. Knepley PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX) 580d7ddef95SMatthew G. Knepley { 58161f58d28SMatthew G. Knepley PetscDS prob; 582da97024aSMatthew G. Knepley PetscSF sf; 583d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 584d7ddef95SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *grad; 585da97024aSMatthew G. Knepley const PetscInt *leaves; 586d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 587520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 588d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 589d7ddef95SMatthew G. Knepley 590d7ddef95SMatthew G. Knepley PetscFunctionBegin; 591da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 592da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 593da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 594d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 595d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 59661f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 597d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 598d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 599d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 600d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 601d7ddef95SMatthew G. Knepley if (Grad) { 602c0a6632aSMatthew G. Knepley PetscFV fv; 603c0a6632aSMatthew G. Knepley 604c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 605d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 606d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 607d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 608d7ddef95SMatthew G. Knepley ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 609d7ddef95SMatthew G. Knepley } 610d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 611d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 612d7ddef95SMatthew G. Knepley IS faceIS; 613d7ddef95SMatthew G. Knepley const PetscInt *faces; 614d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 615d7ddef95SMatthew G. Knepley 616d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 617d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 618d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 619d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 620d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 621d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 622d7ddef95SMatthew G. Knepley const PetscFVFaceGeom *fg; 623d7ddef95SMatthew G. Knepley 624d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 625da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 626da97024aSMatthew G. Knepley if (loc >= 0) continue; 627d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 628d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 629d7ddef95SMatthew G. Knepley if (Grad) { 630d7ddef95SMatthew G. Knepley const PetscFVCellGeom *cg; 631d7ddef95SMatthew G. Knepley const PetscScalar *cx, *cgrad; 632d7ddef95SMatthew G. Knepley PetscScalar *xG; 633d7ddef95SMatthew G. Knepley PetscReal dx[3]; 634d7ddef95SMatthew G. Knepley PetscInt d; 635d7ddef95SMatthew G. Knepley 636d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 637d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 638d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 639520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 640d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 641d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 642520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr); 643d7ddef95SMatthew G. Knepley } else { 644d7ddef95SMatthew G. Knepley const PetscScalar *xI; 645d7ddef95SMatthew G. Knepley PetscScalar *xG; 646d7ddef95SMatthew G. Knepley 647d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 648520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 649520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr); 650d7ddef95SMatthew G. Knepley } 651d7ddef95SMatthew G. Knepley } 652d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 653d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 654d7ddef95SMatthew G. Knepley } 655d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 656d7ddef95SMatthew G. Knepley if (Grad) { 657d7ddef95SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 658d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 659d7ddef95SMatthew G. Knepley } 660d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 661d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 662d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 663d7ddef95SMatthew G. Knepley } 664d7ddef95SMatthew G. Knepley 665d7ddef95SMatthew G. Knepley #undef __FUNCT__ 666d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues" 667d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 668d7ddef95SMatthew G. Knepley { 669d7ddef95SMatthew G. Knepley PetscInt numBd, b; 67055f2e967SMatthew G. Knepley PetscErrorCode ierr; 67155f2e967SMatthew G. Knepley 67255f2e967SMatthew G. Knepley PetscFunctionBegin; 67355f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 674d7ddef95SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 675d7ddef95SMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 676d7ddef95SMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 677d7ddef95SMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 678a6ba4734SToby Isaac ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 67955f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 68055f2e967SMatthew G. Knepley PetscBool isEssential; 681d7ddef95SMatthew G. Knepley const char *labelname; 682d7ddef95SMatthew G. Knepley DMLabel label; 683d7ddef95SMatthew G. Knepley PetscInt field; 684d7ddef95SMatthew G. Knepley PetscObject obj; 685d7ddef95SMatthew G. Knepley PetscClassId id; 68655f2e967SMatthew G. Knepley void (*func)(); 687d7ddef95SMatthew G. Knepley PetscInt numids; 688d7ddef95SMatthew G. Knepley const PetscInt *ids; 68955f2e967SMatthew G. Knepley void *ctx; 69055f2e967SMatthew G. Knepley 691a6ba4734SToby Isaac ierr = DMGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, NULL, NULL, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 692d7ddef95SMatthew G. Knepley if (!isEssential) continue; 693c58f1c22SToby Isaac ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 694d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 695d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 696d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 697ad917190SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (PetscErrorCode (*)(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr); 698d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 69943ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 700d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, 701d7ddef95SMatthew G. Knepley field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 702d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 70355f2e967SMatthew G. Knepley } 70455f2e967SMatthew G. Knepley PetscFunctionReturn(0); 70555f2e967SMatthew G. Knepley } 70655f2e967SMatthew G. Knepley 707cb1e1211SMatthew G Knepley #undef __FUNCT__ 708cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 709cb1e1211SMatthew G Knepley /*@C 710cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 711cb1e1211SMatthew G Knepley 712cb1e1211SMatthew G Knepley Input Parameters: 713cb1e1211SMatthew G Knepley + dm - The DM 714cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 71551259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 716cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 717cb1e1211SMatthew G Knepley 718cb1e1211SMatthew G Knepley Output Parameter: 719cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 720cb1e1211SMatthew G Knepley 721cb1e1211SMatthew G Knepley Level: developer 722cb1e1211SMatthew G Knepley 723*4d6f44ffSToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 724878cb397SSatish Balay @*/ 725ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 726cb1e1211SMatthew G Knepley { 727cb1e1211SMatthew G Knepley const PetscInt debug = 0; 728cb1e1211SMatthew G Knepley PetscSection section; 729c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 730cb1e1211SMatthew G Knepley Vec localX; 73115496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 732cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 733cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 73415496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7359ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 736cb1e1211SMatthew G Knepley PetscErrorCode ierr; 737cb1e1211SMatthew G Knepley 738cb1e1211SMatthew G Knepley PetscFunctionBegin; 739c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 740cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 741cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 742cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 743cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 744cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 745cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 74615496722SMatthew G. Knepley PetscObject obj; 74715496722SMatthew G. Knepley PetscClassId id; 748c5bbbd5bSMatthew G. Knepley PetscInt Nc; 749c5bbbd5bSMatthew G. Knepley 75015496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 75115496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 75215496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 75315496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 75415496722SMatthew G. Knepley 7550f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7560f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 75715496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 75815496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 75915496722SMatthew G. Knepley 76015496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 76115496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 76215496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 763c5bbbd5bSMatthew G. Knepley numComponents += Nc; 764cb1e1211SMatthew G Knepley } 76515496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 766*4d6f44ffSToby Isaac ierr = DMProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 76715496722SMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 768cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 7699ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 7709ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 771cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 772a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 773cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 774cb1e1211SMatthew G Knepley 7758e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 776cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 777cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 778cb1e1211SMatthew G Knepley 77915496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 78015496722SMatthew G. Knepley PetscObject obj; 78115496722SMatthew G. Knepley PetscClassId id; 782c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 78315496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 784cb1e1211SMatthew G Knepley 78515496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 78615496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 78715496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 78815496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 78915496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 790cb1e1211SMatthew G Knepley if (debug) { 791cb1e1211SMatthew G Knepley char title[1024]; 792cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 79315496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 794cb1e1211SMatthew G Knepley } 795cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 79615496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 797be7a662bSMatthew G. Knepley ierr = (*funcs[field])(dim, coords, Nc, funcVal, ctx);CHKERRQ(ierr); 79815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 79915496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 80015496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 80115496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 80215496722SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 80315496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 804cb1e1211SMatthew G Knepley } 805cb1e1211SMatthew G Knepley } 80615496722SMatthew G. Knepley fieldOffset += Nb*Nc; 807cb1e1211SMatthew G Knepley } 808cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 809cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 810cb1e1211SMatthew G Knepley localDiff += elemDiff; 811cb1e1211SMatthew G Knepley } 81215496722SMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 813cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 814b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 815cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 816cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 817cb1e1211SMatthew G Knepley } 818cb1e1211SMatthew G Knepley 819cb1e1211SMatthew G Knepley #undef __FUNCT__ 82040e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 82140e14135SMatthew G. Knepley /*@C 82240e14135SMatthew 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. 82340e14135SMatthew G. Knepley 82440e14135SMatthew G. Knepley Input Parameters: 82540e14135SMatthew G. Knepley + dm - The DM 82640e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 82751259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 82840e14135SMatthew G. Knepley . X - The coefficient vector u_h 82940e14135SMatthew G. Knepley - n - The vector to project along 83040e14135SMatthew G. Knepley 83140e14135SMatthew G. Knepley Output Parameter: 83240e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 83340e14135SMatthew G. Knepley 83440e14135SMatthew G. Knepley Level: developer 83540e14135SMatthew G. Knepley 836*4d6f44ffSToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2Diff() 83740e14135SMatthew G. Knepley @*/ 838ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 839cb1e1211SMatthew G Knepley { 84040e14135SMatthew G. Knepley const PetscInt debug = 0; 841cb1e1211SMatthew G Knepley PetscSection section; 84240e14135SMatthew G. Knepley PetscQuadrature quad; 84340e14135SMatthew G. Knepley Vec localX; 84440e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 84540e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 84640e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 8479ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp; 848cb1e1211SMatthew G Knepley PetscErrorCode ierr; 849cb1e1211SMatthew G Knepley 850cb1e1211SMatthew G Knepley PetscFunctionBegin; 851c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 85240e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 85340e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 85440e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 85540e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 85640e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 857652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 8580f2d7e86SMatthew G. Knepley PetscFE fe; 85940e14135SMatthew G. Knepley PetscInt Nc; 860652b88e8SMatthew G. Knepley 8610f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 8620f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8630f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 86440e14135SMatthew G. Knepley numComponents += Nc; 865652b88e8SMatthew G. Knepley } 866*4d6f44ffSToby Isaac /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 86740e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 86840e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 8699ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 8709ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 87140e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 87240e14135SMatthew G. Knepley PetscScalar *x = NULL; 87340e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 874652b88e8SMatthew G. Knepley 8758e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 87640e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 87740e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 87840e14135SMatthew G. Knepley 87940e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 8800f2d7e86SMatthew G. Knepley PetscFE fe; 88151259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 88221454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 88340e14135SMatthew G. Knepley PetscReal *basisDer; 88421454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 88540e14135SMatthew G. Knepley 8860f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 88721454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 8880f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 8890f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr); 8900f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 89140e14135SMatthew G. Knepley if (debug) { 89240e14135SMatthew G. Knepley char title[1024]; 89340e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 89440e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 895652b88e8SMatthew G. Knepley } 89640e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 89740e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 89840e14135SMatthew G. Knepley coords[d] = v0[d]; 89940e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 90040e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 901652b88e8SMatthew G. Knepley } 90240e14135SMatthew G. Knepley } 903ad917190SMatthew G. Knepley ierr = (*funcs[field])(dim, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr); 90440e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 90540e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 90640e14135SMatthew G. Knepley 90740e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 90840e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 90940e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 91040e14135SMatthew G. Knepley 91140e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 91240e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 91340e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 91440e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 91540e14135SMatthew G. Knepley } 91640e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 91740e14135SMatthew G. Knepley } 91840e14135SMatthew G. Knepley } 91940e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 92040e14135SMatthew 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);} 92140e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 92240e14135SMatthew G. Knepley } 92340e14135SMatthew G. Knepley } 92440e14135SMatthew G. Knepley comp += Ncomp; 92540e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 92640e14135SMatthew G. Knepley } 92740e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 92840e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 92940e14135SMatthew G. Knepley localDiff += elemDiff; 93040e14135SMatthew G. Knepley } 93140e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 93240e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 933b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 93440e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 935cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 936cb1e1211SMatthew G Knepley } 937cb1e1211SMatthew G Knepley 938a0845e3aSMatthew G. Knepley #undef __FUNCT__ 93973d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff" 94015496722SMatthew G. Knepley /*@C 94115496722SMatthew G. Knepley DMPlexComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components. 94215496722SMatthew G. Knepley 94315496722SMatthew G. Knepley Input Parameters: 94415496722SMatthew G. Knepley + dm - The DM 94515496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component 94615496722SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 94715496722SMatthew G. Knepley - X - The coefficient vector u_h 94815496722SMatthew G. Knepley 94915496722SMatthew G. Knepley Output Parameter: 95015496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2 95115496722SMatthew G. Knepley 95215496722SMatthew G. Knepley Level: developer 95315496722SMatthew G. Knepley 954*4d6f44ffSToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff() 95515496722SMatthew G. Knepley @*/ 956ad917190SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 95773d901b8SMatthew G. Knepley { 95873d901b8SMatthew G. Knepley const PetscInt debug = 0; 95973d901b8SMatthew G. Knepley PetscSection section; 96073d901b8SMatthew G. Knepley PetscQuadrature quad; 96173d901b8SMatthew G. Knepley Vec localX; 96215496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 96373d901b8SMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 96473d901b8SMatthew G. Knepley PetscReal *localDiff; 96515496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 9669ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 96773d901b8SMatthew G. Knepley PetscErrorCode ierr; 96873d901b8SMatthew G. Knepley 96973d901b8SMatthew G. Knepley PetscFunctionBegin; 970c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 97173d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 97273d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 97373d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 97473d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 97573d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 97673d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 97715496722SMatthew G. Knepley PetscObject obj; 97815496722SMatthew G. Knepley PetscClassId id; 97973d901b8SMatthew G. Knepley PetscInt Nc; 98073d901b8SMatthew G. Knepley 98115496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 98215496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 98315496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 98415496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 98515496722SMatthew G. Knepley 9860f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 9870f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 98815496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 98915496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 99015496722SMatthew G. Knepley 99115496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 99215496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 99315496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 99473d901b8SMatthew G. Knepley numComponents += Nc; 99573d901b8SMatthew G. Knepley } 99615496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 997*4d6f44ffSToby Isaac ierr = DMProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 99815496722SMatthew G. Knepley ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 99973d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 10009ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 10019ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 100273d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 100373d901b8SMatthew G. Knepley PetscScalar *x = NULL; 100473d901b8SMatthew G. Knepley 10058e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 100673d901b8SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 100773d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 100873d901b8SMatthew G. Knepley 100915496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 101015496722SMatthew G. Knepley PetscObject obj; 101115496722SMatthew G. Knepley PetscClassId id; 101273d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 101315496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 101473d901b8SMatthew G. Knepley 101515496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 101615496722SMatthew G. Knepley 101715496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 101815496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 101915496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 102015496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 102115496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 102273d901b8SMatthew G. Knepley if (debug) { 102373d901b8SMatthew G. Knepley char title[1024]; 102473d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 102515496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 102673d901b8SMatthew G. Knepley } 102773d901b8SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 102815496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 1029ad917190SMatthew G. Knepley ierr = (*funcs[field])(dim, coords, numFields, funcVal, ctx);CHKERRQ(ierr); 103015496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 103115496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 103215496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 103315496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 103415496722SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ);CHKERRQ(ierr);} 103515496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 103673d901b8SMatthew G. Knepley } 103773d901b8SMatthew G. Knepley } 103815496722SMatthew G. Knepley fieldOffset += Nb*Nc; 103973d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 104073d901b8SMatthew G. Knepley } 104173d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 104273d901b8SMatthew G. Knepley } 104373d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1044b2566f29SBarry Smith ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 104573d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 104615496722SMatthew G. Knepley ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 104773d901b8SMatthew G. Knepley PetscFunctionReturn(0); 104873d901b8SMatthew G. Knepley } 104973d901b8SMatthew G. Knepley 105073d901b8SMatthew G. Knepley #undef __FUNCT__ 1051e729f68cSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2DiffVec" 1052e729f68cSMatthew G. Knepley /*@C 1053e729f68cSMatthew G. Knepley DMPlexComputeL2DiffVec - This function computes the cellwise L_2 difference between a function u and an FEM interpolant solution u_h, and stores it in a Vec. 1054e729f68cSMatthew G. Knepley 1055e729f68cSMatthew G. Knepley Input Parameters: 1056e729f68cSMatthew G. Knepley + dm - The DM 1057e729f68cSMatthew G. Knepley . funcs - The functions to evaluate for each field component 1058e729f68cSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 1059e729f68cSMatthew G. Knepley - X - The coefficient vector u_h 1060e729f68cSMatthew G. Knepley 1061e729f68cSMatthew G. Knepley Output Parameter: 1062e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell 1063e729f68cSMatthew G. Knepley 1064e729f68cSMatthew G. Knepley Level: developer 1065e729f68cSMatthew G. Knepley 1066*4d6f44ffSToby Isaac .seealso: DMProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 1067e729f68cSMatthew G. Knepley @*/ 1068e729f68cSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscErrorCode (**funcs)(PetscInt, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D) 1069e729f68cSMatthew G. Knepley { 1070e729f68cSMatthew G. Knepley PetscSection section; 1071e729f68cSMatthew G. Knepley PetscQuadrature quad; 1072e729f68cSMatthew G. Knepley Vec localX; 1073e729f68cSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 1074e729f68cSMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 1075e729f68cSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 1076e729f68cSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 1077e729f68cSMatthew G. Knepley PetscErrorCode ierr; 1078e729f68cSMatthew G. Knepley 1079e729f68cSMatthew G. Knepley PetscFunctionBegin; 1080e729f68cSMatthew G. Knepley ierr = VecSet(D, 0.0);CHKERRQ(ierr); 1081e729f68cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1082e729f68cSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1083e729f68cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 1084e729f68cSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 1085e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1086e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1087e729f68cSMatthew G. Knepley for (field = 0; field < numFields; ++field) { 1088e729f68cSMatthew G. Knepley PetscObject obj; 1089e729f68cSMatthew G. Knepley PetscClassId id; 1090e729f68cSMatthew G. Knepley PetscInt Nc; 1091e729f68cSMatthew G. Knepley 1092e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1093e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1094e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1095e729f68cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1096e729f68cSMatthew G. Knepley 1097e729f68cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 1098e729f68cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 1099e729f68cSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1100e729f68cSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 1101e729f68cSMatthew G. Knepley 1102e729f68cSMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 1103e729f68cSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 1104e729f68cSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1105e729f68cSMatthew G. Knepley numComponents += Nc; 1106e729f68cSMatthew G. Knepley } 1107e729f68cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 1108*4d6f44ffSToby Isaac ierr = DMProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 1109e729f68cSMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 1110e729f68cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1111e729f68cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1112e729f68cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1113e729f68cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1114e729f68cSMatthew G. Knepley PetscScalar *x = NULL; 11156f288a59SMatthew G. Knepley PetscScalar elemDiff = 0.0; 1116e729f68cSMatthew G. Knepley 1117e729f68cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 1118e729f68cSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 1119e729f68cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1120e729f68cSMatthew G. Knepley 1121e729f68cSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 1122e729f68cSMatthew G. Knepley PetscObject obj; 1123e729f68cSMatthew G. Knepley PetscClassId id; 1124e729f68cSMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 1125e729f68cSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 1126e729f68cSMatthew G. Knepley 1127e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1128e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1129e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 1130e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1131e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1132e729f68cSMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 1133e729f68cSMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 1134e729f68cSMatthew G. Knepley ierr = (*funcs[field])(dim, coords, Nc, funcVal, ctx);CHKERRQ(ierr); 1135e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1136e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1137e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1138e729f68cSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1139e729f68cSMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 1140e729f68cSMatthew G. Knepley } 1141e729f68cSMatthew G. Knepley } 1142e729f68cSMatthew G. Knepley fieldOffset += Nb*Nc; 1143e729f68cSMatthew G. Knepley } 1144e729f68cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1145e729f68cSMatthew G. Knepley ierr = VecSetValuesSection(D, section, c, &elemDiff, ADD_VALUES);CHKERRQ(ierr); 1146e729f68cSMatthew G. Knepley } 1147e729f68cSMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 1148e729f68cSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1149e729f68cSMatthew G. Knepley ierr = VecSqrtAbs(D);CHKERRQ(ierr); 1150e729f68cSMatthew G. Knepley PetscFunctionReturn(0); 1151e729f68cSMatthew G. Knepley } 1152e729f68cSMatthew G. Knepley 1153e729f68cSMatthew G. Knepley #undef __FUNCT__ 115473d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM" 115573d901b8SMatthew G. Knepley /*@ 115673d901b8SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user 115773d901b8SMatthew G. Knepley 115873d901b8SMatthew G. Knepley Input Parameters: 115973d901b8SMatthew G. Knepley + dm - The mesh 116073d901b8SMatthew G. Knepley . X - Local input vector 116173d901b8SMatthew G. Knepley - user - The user context 116273d901b8SMatthew G. Knepley 116373d901b8SMatthew G. Knepley Output Parameter: 116473d901b8SMatthew G. Knepley . integral - Local integral for each field 116573d901b8SMatthew G. Knepley 116673d901b8SMatthew G. Knepley Level: developer 116773d901b8SMatthew G. Knepley 116873d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 116973d901b8SMatthew G. Knepley @*/ 11700f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user) 117173d901b8SMatthew G. Knepley { 117273d901b8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 117373d901b8SMatthew G. Knepley DM dmAux; 117473d901b8SMatthew G. Knepley Vec localX, A; 11752764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 117673d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1177bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 117873d901b8SMatthew G. Knepley PetscScalar *u, *a = NULL; 1179c1f031eeSMatthew G. Knepley PetscReal *lintegral, *vol; 11809ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c; 11810f2d7e86SMatthew G. Knepley PetscInt totDim, totDimAux; 118273d901b8SMatthew G. Knepley PetscErrorCode ierr; 118373d901b8SMatthew G. Knepley 118473d901b8SMatthew G. Knepley PetscFunctionBegin; 1185c1f031eeSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 118673d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 118773d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 118873d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1189c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 119073d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 11912764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 11922764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 119373d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 119473d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 11959ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 11969ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 119773d901b8SMatthew G. Knepley numCells = cEnd - cStart; 119873d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 119973d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 120073d901b8SMatthew G. Knepley if (dmAux) { 120173d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 12022764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 12032764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 120473d901b8SMatthew G. Knepley } 1205d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1206c1f031eeSMatthew G. Knepley ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr); 12070f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1208c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;} 120973d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 121073d901b8SMatthew G. Knepley PetscScalar *x = NULL; 121173d901b8SMatthew G. Knepley PetscInt i; 121273d901b8SMatthew G. Knepley 1213bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr); 1214c1f031eeSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr); 1215bbce034cSMatthew G. Knepley if (cgeom[c].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", cgeom[c].detJ, c); 121673d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 12170f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 121873d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 121973d901b8SMatthew G. Knepley if (dmAux) { 122073d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 12210f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 122273d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 122373d901b8SMatthew G. Knepley } 122473d901b8SMatthew G. Knepley } 122573d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1226c1f031eeSMatthew G. Knepley PetscObject obj; 1227c1f031eeSMatthew G. Knepley PetscClassId id; 1228c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 122973d901b8SMatthew G. Knepley 1230c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1231c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1232c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1233c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1234c1f031eeSMatthew G. Knepley PetscQuadrature q; 1235c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1236c1f031eeSMatthew G. Knepley 12370f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1238c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1239c1f031eeSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1240c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1241c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 124273d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 12430f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 124473d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 124573d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 124673d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 124773d901b8SMatthew G. Knepley offset = numCells - Nr; 1248c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr); 1249c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr); 1250c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1251b69edc29SMatthew G. Knepley /* PetscFV fv = (PetscFV) obj; */ 1252c1f031eeSMatthew G. Knepley PetscInt foff; 1253420e96edSMatthew G. Knepley PetscPointFunc obj_func; 1254b69edc29SMatthew G. Knepley PetscScalar lint; 1255c1f031eeSMatthew G. Knepley 1256c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 1257c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1258c1f031eeSMatthew G. Knepley if (obj_func) { 1259c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 1260c1f031eeSMatthew G. Knepley /* TODO: Need full pointwise interpolation and get centroid for x argument */ 126130b9ff8bSMatthew G. Knepley obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint); 1262b69edc29SMatthew G. Knepley lintegral[f] = PetscRealPart(lint)*vol[c]; 126373d901b8SMatthew G. Knepley } 1264c1f031eeSMatthew G. Knepley } 1265c1f031eeSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1266c1f031eeSMatthew G. Knepley } 126773d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 126873d901b8SMatthew G. Knepley if (mesh->printFEM) { 126973d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr); 1270c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);} 127173d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 127273d901b8SMatthew G. Knepley } 127373d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1274b2566f29SBarry Smith ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 1275c1f031eeSMatthew G. Knepley ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr); 1276c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 127773d901b8SMatthew G. Knepley PetscFunctionReturn(0); 127873d901b8SMatthew G. Knepley } 127973d901b8SMatthew G. Knepley 128073d901b8SMatthew G. Knepley #undef __FUNCT__ 128168132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorNested" 1282d69c5d34SMatthew G. Knepley /*@ 128368132eb9SMatthew G. Knepley DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1284d69c5d34SMatthew G. Knepley 1285d69c5d34SMatthew G. Knepley Input Parameters: 1286d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1287d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1288d69c5d34SMatthew G. Knepley - user - The user context 1289d69c5d34SMatthew G. Knepley 1290d69c5d34SMatthew G. Knepley Output Parameter: 1291934789fcSMatthew G. Knepley . In - The interpolation matrix 1292d69c5d34SMatthew G. Knepley 1293d69c5d34SMatthew G. Knepley Level: developer 1294d69c5d34SMatthew G. Knepley 129568132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 1296d69c5d34SMatthew G. Knepley @*/ 129768132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user) 1298d69c5d34SMatthew G. Knepley { 1299d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1300d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 13012764a2aaSMatthew G. Knepley PetscDS prob; 1302d69c5d34SMatthew G. Knepley PetscFE *feRef; 130397c42addSMatthew G. Knepley PetscFV *fvRef; 1304d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1305d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1306d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 13079ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c; 13080f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1309d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1310d69c5d34SMatthew G. Knepley 1311d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1312d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1313c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1314d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1315d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1316d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1317d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1318d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1319d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 13209ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 13219ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 13222764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 132397c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 1324d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 132597c42addSMatthew G. Knepley PetscObject obj; 132697c42addSMatthew G. Knepley PetscClassId id; 1327aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 1328d69c5d34SMatthew G. Knepley 132997c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 133097c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 133197c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 133297c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 133397c42addSMatthew G. Knepley 13340f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1335d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 13360f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 133797c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 133897c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 133997c42addSMatthew G. Knepley PetscDualSpace Q; 134097c42addSMatthew G. Knepley 134197c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 134297c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 134397c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 134497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 134597c42addSMatthew G. Knepley } 13460f2d7e86SMatthew G. Knepley rTotDim += rNb*Nc; 1347d69c5d34SMatthew G. Knepley } 13482764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 13490f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 13500f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1351d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1352d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1353d69c5d34SMatthew G. Knepley PetscQuadrature f; 1354d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1355d69c5d34SMatthew G. Knepley PetscReal *points; 1356d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1357d69c5d34SMatthew G. Knepley 1358d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 135997c42addSMatthew G. Knepley if (feRef[fieldI]) { 1360d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 13610f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 136297c42addSMatthew G. Knepley } else { 136397c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 136497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 136597c42addSMatthew G. Knepley } 1366d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1367d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1368d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1369d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1370d69c5d34SMatthew G. Knepley npoints += Np; 1371d69c5d34SMatthew G. Knepley } 1372d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1373d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1374d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1375d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1376d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1377d69c5d34SMatthew G. Knepley } 1378d69c5d34SMatthew G. Knepley 1379d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 138097c42addSMatthew G. Knepley PetscObject obj; 138197c42addSMatthew G. Knepley PetscClassId id; 1382d69c5d34SMatthew G. Knepley PetscReal *B; 1383aa7890ccSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j; 1384d69c5d34SMatthew G. Knepley 138597c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr); 138697c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 138797c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 138897c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1389d69c5d34SMatthew G. Knepley 1390d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 13910f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 13920f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1393ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1394ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 13957c927364SMatthew 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); 13960f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1397d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1398d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1399d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1400d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 140136a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 14020f2d7e86SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p]; 140336a6d9c0SMatthew G. Knepley } 1404d69c5d34SMatthew G. Knepley } 1405d69c5d34SMatthew G. Knepley } 14060f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1407ffe73a53SMatthew G. Knepley } 140897c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 140997c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 141097c42addSMatthew G. Knepley 141197c42addSMatthew G. Knepley /* Evaluate constant function at points */ 141297c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 141397c42addSMatthew G. Knepley cpdim = 1; 141497c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 141597c42addSMatthew G. Knepley if (fieldI == fieldJ) { 141697c42addSMatthew 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); 141797c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 141897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 141997c42addSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 142097c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 142197c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 142297c42addSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p]; 142397c42addSMatthew G. Knepley } 142497c42addSMatthew G. Knepley } 142597c42addSMatthew G. Knepley } 142697c42addSMatthew G. Knepley } 142797c42addSMatthew G. Knepley } 142836a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1429d69c5d34SMatthew G. Knepley } 1430d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1431549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1432d69c5d34SMatthew G. Knepley } 14330f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 14347f5b169aSMatthew G. Knepley /* Preallocate matrix */ 14357f5b169aSMatthew G. Knepley { 14367f5b169aSMatthew G. Knepley PetscHashJK ht; 14377f5b169aSMatthew G. Knepley PetscLayout rLayout; 14387f5b169aSMatthew G. Knepley PetscInt *dnz, *onz, *cellCIndices, *cellFIndices; 14397f5b169aSMatthew G. Knepley PetscInt locRows, rStart, rEnd, cell, r; 14407f5b169aSMatthew G. Knepley 14417f5b169aSMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 14427f5b169aSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 14437f5b169aSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 14447f5b169aSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 14457f5b169aSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 14467f5b169aSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 14477f5b169aSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 14487f5b169aSMatthew G. Knepley ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 14497f5b169aSMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 14507f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 14517f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 14527f5b169aSMatthew G. Knepley for (r = 0; r < rTotDim; ++r) { 14537f5b169aSMatthew G. Knepley PetscHashJKKey key; 14547f5b169aSMatthew G. Knepley PetscHashJKIter missing, iter; 14557f5b169aSMatthew G. Knepley 14567f5b169aSMatthew G. Knepley key.j = cellFIndices[r]; 14577f5b169aSMatthew G. Knepley if (key.j < 0) continue; 14587f5b169aSMatthew G. Knepley for (c = 0; c < cTotDim; ++c) { 14597f5b169aSMatthew G. Knepley key.k = cellCIndices[c]; 14607f5b169aSMatthew G. Knepley if (key.k < 0) continue; 14617f5b169aSMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 14627f5b169aSMatthew G. Knepley if (missing) { 14637f5b169aSMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 14647f5b169aSMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 14657f5b169aSMatthew G. Knepley else ++onz[key.j-rStart]; 14667f5b169aSMatthew G. Knepley } 14677f5b169aSMatthew G. Knepley } 14687f5b169aSMatthew G. Knepley } 14697f5b169aSMatthew G. Knepley } 14707f5b169aSMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 14717f5b169aSMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 14727f5b169aSMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 14737f5b169aSMatthew G. Knepley ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr); 14747f5b169aSMatthew G. Knepley } 14757f5b169aSMatthew G. Knepley /* Fill matrix */ 14767f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1477d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1478934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1479d69c5d34SMatthew G. Knepley } 1480549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 148197c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 1482549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1483934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1484934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1485d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1486d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1487934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1488934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1489d69c5d34SMatthew G. Knepley } 1490d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1491d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1492d69c5d34SMatthew G. Knepley } 14936c73c22cSMatthew G. Knepley 14946c73c22cSMatthew G. Knepley #undef __FUNCT__ 149568132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorGeneral" 149668132eb9SMatthew G. Knepley /*@ 149768132eb9SMatthew G. Knepley DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM. 149868132eb9SMatthew G. Knepley 149968132eb9SMatthew G. Knepley Input Parameters: 150068132eb9SMatthew G. Knepley + dmf - The fine mesh 150168132eb9SMatthew G. Knepley . dmc - The coarse mesh 150268132eb9SMatthew G. Knepley - user - The user context 150368132eb9SMatthew G. Knepley 150468132eb9SMatthew G. Knepley Output Parameter: 150568132eb9SMatthew G. Knepley . In - The interpolation matrix 150668132eb9SMatthew G. Knepley 150768132eb9SMatthew G. Knepley Level: developer 150868132eb9SMatthew G. Knepley 150968132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 151068132eb9SMatthew G. Knepley @*/ 151168132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user) 15124ef9d792SMatthew G. Knepley { 15134ef9d792SMatthew G. Knepley PetscDS prob; 15144ef9d792SMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 15154ef9d792SMatthew G. Knepley PetscHashJK ht; 15164ef9d792SMatthew G. Knepley PetscLayout rLayout; 15174ef9d792SMatthew G. Knepley PetscInt *dnz, *onz; 15184ef9d792SMatthew G. Knepley PetscInt locRows, rStart, rEnd; 15194ef9d792SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 15204ef9d792SMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 15214ef9d792SMatthew G. Knepley PetscScalar *elemMat; 15224ef9d792SMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 15234ef9d792SMatthew G. Knepley PetscErrorCode ierr; 15244ef9d792SMatthew G. Knepley 15254ef9d792SMatthew G. Knepley PetscFunctionBegin; 15264ef9d792SMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 15274ef9d792SMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 15284ef9d792SMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 15294ef9d792SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 15304ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 15314ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 15324ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 15334ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 15344ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 15354ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 15364ef9d792SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 15374ef9d792SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 15384ef9d792SMatthew G. Knepley ierr = PetscMalloc1(totDim*totDim, &elemMat);CHKERRQ(ierr); 15394ef9d792SMatthew G. Knepley 15404ef9d792SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 15414ef9d792SMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 15424ef9d792SMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 15434ef9d792SMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 15444ef9d792SMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 15454ef9d792SMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 15464ef9d792SMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 15474ef9d792SMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 15484ef9d792SMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 15494ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 15504ef9d792SMatthew G. Knepley PetscObject obj; 15514ef9d792SMatthew G. Knepley PetscClassId id; 1552c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 15534ef9d792SMatthew G. Knepley PetscQuadrature f; 155417f047d8SMatthew G. Knepley const PetscReal *qpoints; 155517f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 15564ef9d792SMatthew G. Knepley 15574ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 15584ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 15594ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 15604ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 15614ef9d792SMatthew G. Knepley 15624ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 15634ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 15644ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 15654ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 15664ef9d792SMatthew G. Knepley 15674ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 15684ef9d792SMatthew G. Knepley Nc = 1; 15694ef9d792SMatthew G. Knepley } 15704ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 15714ef9d792SMatthew G. Knepley /* For each fine grid cell */ 15724ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 15734ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 15744ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 15754ef9d792SMatthew G. Knepley 15764ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 15774ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 15784ef9d792SMatthew G. Knepley if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc); 15794ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 15804ef9d792SMatthew G. Knepley Vec pointVec; 15814ef9d792SMatthew G. Knepley PetscScalar *pV; 15824ef9d792SMatthew G. Knepley IS coarseCellIS; 15834ef9d792SMatthew G. Knepley const PetscInt *coarseCells; 15844ef9d792SMatthew G. Knepley PetscInt numCoarseCells, q, r, c; 15854ef9d792SMatthew G. Knepley 15864ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 15874ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 15884ef9d792SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 15894ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 15904ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 15914ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 15924ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 15934ef9d792SMatthew G. Knepley /* Transform point to real space */ 15944ef9d792SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x); 15954ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 15964ef9d792SMatthew G. Knepley } 15974ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 15984ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 15994ef9d792SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr); 16004ef9d792SMatthew G. Knepley ierr = ISViewFromOptions(coarseCellIS, NULL, "-interp_is_view");CHKERRQ(ierr); 16014ef9d792SMatthew G. Knepley /* Update preallocation info */ 16024ef9d792SMatthew G. Knepley ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr); 16034ef9d792SMatthew G. Knepley ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 16044ef9d792SMatthew G. Knepley for (r = 0; r < Nc; ++r) { 16054ef9d792SMatthew G. Knepley PetscHashJKKey key; 16064ef9d792SMatthew G. Knepley PetscHashJKIter missing, iter; 16074ef9d792SMatthew G. Knepley 16084ef9d792SMatthew G. Knepley key.j = findices[i*Nc+r]; 16094ef9d792SMatthew G. Knepley if (key.j < 0) continue; 16104ef9d792SMatthew G. Knepley /* Get indices for coarse elements */ 16114ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 16124ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 16134ef9d792SMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 16144ef9d792SMatthew G. Knepley key.k = cindices[c]; 16154ef9d792SMatthew G. Knepley if (key.k < 0) continue; 16164ef9d792SMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 16174ef9d792SMatthew G. Knepley if (missing) { 16184ef9d792SMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 16194ef9d792SMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 16204ef9d792SMatthew G. Knepley else ++onz[key.j-rStart]; 16214ef9d792SMatthew G. Knepley } 16224ef9d792SMatthew G. Knepley } 16234ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 16244ef9d792SMatthew G. Knepley } 16254ef9d792SMatthew G. Knepley } 16264ef9d792SMatthew G. Knepley ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 16274ef9d792SMatthew G. Knepley ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr); 16284ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 16294ef9d792SMatthew G. Knepley } 16304ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 16314ef9d792SMatthew G. Knepley } 16324ef9d792SMatthew G. Knepley } 16334ef9d792SMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 16344ef9d792SMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 16354ef9d792SMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 16364ef9d792SMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 16374ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 16384ef9d792SMatthew G. Knepley PetscObject obj; 16394ef9d792SMatthew G. Knepley PetscClassId id; 1640c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 16414ef9d792SMatthew G. Knepley PetscQuadrature f; 16424ef9d792SMatthew G. Knepley const PetscReal *qpoints, *qweights; 164317f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 16444ef9d792SMatthew G. Knepley 16454ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 16464ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 16474ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 16484ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 16494ef9d792SMatthew G. Knepley 16504ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 16514ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 16524ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 16534ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 16544ef9d792SMatthew G. Knepley 16554ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 16564ef9d792SMatthew G. Knepley Nc = 1; 16574ef9d792SMatthew G. Knepley } 16584ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 16594ef9d792SMatthew G. Knepley /* For each fine grid cell */ 16604ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 16614ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 16624ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 16634ef9d792SMatthew G. Knepley 16644ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 16654ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 16664ef9d792SMatthew G. Knepley if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc); 16674ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 16684ef9d792SMatthew G. Knepley Vec pointVec; 16694ef9d792SMatthew G. Knepley PetscScalar *pV; 16704ef9d792SMatthew G. Knepley IS coarseCellIS; 16714ef9d792SMatthew G. Knepley const PetscInt *coarseCells; 167217f047d8SMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 16734ef9d792SMatthew G. Knepley 16744ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 16754ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 16764ef9d792SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, &qweights);CHKERRQ(ierr); 16774ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 16784ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 16794ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 16804ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 16814ef9d792SMatthew G. Knepley /* Transform point to real space */ 16824ef9d792SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x); 16834ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 16844ef9d792SMatthew G. Knepley } 16854ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 16864ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 16874ef9d792SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr); 16884ef9d792SMatthew G. Knepley /* Update preallocation info */ 16894ef9d792SMatthew G. Knepley ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr); 16904ef9d792SMatthew G. Knepley ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 16914ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 16924ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 1693826eb36dSMatthew G. Knepley PetscReal pVReal[3]; 1694826eb36dSMatthew G. Knepley 16954ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 16964ef9d792SMatthew G. Knepley /* Transform points from real space to coarse reference space */ 1697f1ac73f2SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell], NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 1698826eb36dSMatthew G. Knepley CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x); 1699826eb36dSMatthew G. Knepley for (d = 0; d < dim; ++d) pV[ccell*dim+d] = pVReal[d]; 17004ef9d792SMatthew G. Knepley 17014ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 17024ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 17034ef9d792SMatthew G. Knepley PetscReal *B; 17044ef9d792SMatthew G. Knepley 17054ef9d792SMatthew G. Knepley /* Evaluate coarse basis on contained point */ 17064ef9d792SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 17074ef9d792SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 17084ef9d792SMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 17094ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 17104ef9d792SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = B[j*Nc + c]*qweights[ccell]; 17114ef9d792SMatthew G. Knepley } 17124ef9d792SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 17134ef9d792SMatthew G. Knepley } else { 17144ef9d792SMatthew G. Knepley cpdim = 1; 17154ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 17164ef9d792SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = 1.0*qweights[ccell]; 17174ef9d792SMatthew G. Knepley } 17184ef9d792SMatthew G. Knepley } 17194ef9d792SMatthew G. Knepley /* Update interpolator */ 17204ef9d792SMatthew G. Knepley ierr = MatSetValues(In, Nc, &findices[i*Nc], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr); 17214ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 17224ef9d792SMatthew G. Knepley } 17234ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 17244ef9d792SMatthew G. Knepley ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 17254ef9d792SMatthew G. Knepley ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr); 17264ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 17274ef9d792SMatthew G. Knepley } 17284ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 17294ef9d792SMatthew G. Knepley } 17304ef9d792SMatthew G. Knepley } 17314ef9d792SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 17324ef9d792SMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 17334ef9d792SMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 17344ef9d792SMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17354ef9d792SMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17364ef9d792SMatthew G. Knepley PetscFunctionReturn(0); 17374ef9d792SMatthew G. Knepley } 17384ef9d792SMatthew G. Knepley 17394ef9d792SMatthew G. Knepley #undef __FUNCT__ 17407c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM" 17417c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 17427c927364SMatthew G. Knepley { 1743e9d4ef1bSMatthew G. Knepley PetscDS prob; 17447c927364SMatthew G. Knepley PetscFE *feRef; 174597c42addSMatthew G. Knepley PetscFV *fvRef; 17467c927364SMatthew G. Knepley Vec fv, cv; 17477c927364SMatthew G. Knepley IS fis, cis; 17487c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 17497c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 17509ac3fadcSMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, offsetC, offsetF, m; 17517c927364SMatthew G. Knepley PetscErrorCode ierr; 17527c927364SMatthew G. Knepley 17537c927364SMatthew G. Knepley PetscFunctionBegin; 175475a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1755c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 17567c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 17577c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 17587c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 17597c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 17607c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 17617c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 17629ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 17639ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1764e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 176597c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 17667c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 176797c42addSMatthew G. Knepley PetscObject obj; 176897c42addSMatthew G. Knepley PetscClassId id; 1769aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 17707c927364SMatthew G. Knepley 177197c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 177297c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 177397c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 177497c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 177597c42addSMatthew G. Knepley 17767c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 17777c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 17787c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 177997c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 178097c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 178197c42addSMatthew G. Knepley PetscDualSpace Q; 178297c42addSMatthew G. Knepley 178397c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 178497c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 178597c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 178697c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 178797c42addSMatthew G. Knepley } 17887c927364SMatthew G. Knepley fTotDim += fNb*Nc; 17897c927364SMatthew G. Knepley } 1790e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 17917c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 17927c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 17937c927364SMatthew G. Knepley PetscFE feC; 179497c42addSMatthew G. Knepley PetscFV fvC; 17957c927364SMatthew G. Knepley PetscDualSpace QF, QC; 17967c927364SMatthew G. Knepley PetscInt NcF, NcC, fpdim, cpdim; 17977c927364SMatthew G. Knepley 179897c42addSMatthew G. Knepley if (feRef[field]) { 1799e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 18007c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 18017c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 18027c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 18037c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 18047c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 18057c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 180697c42addSMatthew G. Knepley } else { 180797c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 180897c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 180997c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 181097c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 181197c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 181297c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 181397c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 181497c42addSMatthew G. Knepley } 181597c42addSMatthew G. Knepley if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC); 18167c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 18177c927364SMatthew G. Knepley PetscQuadrature cfunc; 18187c927364SMatthew G. Knepley const PetscReal *cqpoints; 18197c927364SMatthew G. Knepley PetscInt NpC; 182097c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 18217c927364SMatthew G. Knepley 18227c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 18237c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr); 182497c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 18257c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 18267c927364SMatthew G. Knepley PetscQuadrature ffunc; 18277c927364SMatthew G. Knepley const PetscReal *fqpoints; 18287c927364SMatthew G. Knepley PetscReal sum = 0.0; 18297c927364SMatthew G. Knepley PetscInt NpF, comp; 18307c927364SMatthew G. Knepley 18317c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 18327c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr); 18337c927364SMatthew G. Knepley if (NpC != NpF) continue; 18347c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 18357c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 18367c927364SMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 18377c927364SMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp; 18387c927364SMatthew G. Knepley } 183997c42addSMatthew G. Knepley found = PETSC_TRUE; 18407c927364SMatthew G. Knepley break; 18417c927364SMatthew G. Knepley } 184297c42addSMatthew G. Knepley if (!found) { 184397c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 184497c42addSMatthew G. Knepley if (fvRef[field]) { 184597c42addSMatthew G. Knepley PetscInt comp; 184697c42addSMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 184797c42addSMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp; 184897c42addSMatthew G. Knepley } 184997c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 185097c42addSMatthew G. Knepley } 18517c927364SMatthew G. Knepley } 18527c927364SMatthew G. Knepley offsetC += cpdim*NcC; 18537c927364SMatthew G. Knepley offsetF += fpdim*NcF; 18547c927364SMatthew G. Knepley } 185597c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 185697c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 18577c927364SMatthew G. Knepley 18587c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 18597c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 18607c927364SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr); 18617c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 18627c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 1863aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 1864aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 18657c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 18667c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 18677c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 18687c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 18697c927364SMatthew G. Knepley if (cellCIndices[d] < 0) continue; 18707c927364SMatthew G. Knepley if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]); 18717c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 18727c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 18737c927364SMatthew G. Knepley } 18747c927364SMatthew G. Knepley } 18757c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 18767c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 18777c927364SMatthew G. Knepley 18787c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 18797c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 18807c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 18817c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 18827c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 18837c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 18847c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 188575a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1886cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1887cb1e1211SMatthew G Knepley } 1888