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" 89026175e5SToby Isaac static void DMPlexProjectRigidBody(const PetscReal X[], PetscScalar *mode, void *ctx) 90026175e5SToby Isaac { 91026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *)ctx; 92026175e5SToby Isaac PetscInt dim = ctxInt[0]; 93026175e5SToby Isaac PetscInt d = ctxInt[1]; 94026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 95026175e5SToby Isaac 96026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 97026175e5SToby Isaac if (d < dim) { 98026175e5SToby Isaac mode[d] = 1.; 99026175e5SToby Isaac } 100026175e5SToby Isaac else { 101026175e5SToby Isaac for (i = 0; i < dim; i++) { 102026175e5SToby Isaac for (j = 0; j < dim; j++) { 103026175e5SToby Isaac mode[j] += epsilon(i, j, k)*X[i]; 104026175e5SToby Isaac } 105026175e5SToby Isaac } 106026175e5SToby Isaac } 107026175e5SToby Isaac } 108026175e5SToby Isaac 109026175e5SToby Isaac #undef __FUNCT__ 110cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 111cb1e1211SMatthew G Knepley /*@C 112026175e5SToby Isaac DMPlexCreateRigidBody - for the default global section, create rigid body modes from coordinates 113cb1e1211SMatthew G Knepley 114cb1e1211SMatthew G Knepley Collective on DM 115cb1e1211SMatthew G Knepley 116cb1e1211SMatthew G Knepley Input Arguments: 117026175e5SToby Isaac . dm - the DM 118cb1e1211SMatthew G Knepley 119cb1e1211SMatthew G Knepley Output Argument: 120cb1e1211SMatthew G Knepley . sp - the null space 121cb1e1211SMatthew G Knepley 122cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 123cb1e1211SMatthew G Knepley 124cb1e1211SMatthew G Knepley Level: advanced 125cb1e1211SMatthew G Knepley 126cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 127cb1e1211SMatthew G Knepley @*/ 128026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp) 129cb1e1211SMatthew G Knepley { 130cb1e1211SMatthew G Knepley MPI_Comm comm; 131026175e5SToby Isaac Vec mode[6]; 132026175e5SToby Isaac PetscSection section, globalSection; 133026175e5SToby Isaac PetscInt dim, n, m, d, i, j; 134cb1e1211SMatthew G Knepley PetscErrorCode ierr; 135cb1e1211SMatthew G Knepley 136cb1e1211SMatthew G Knepley PetscFunctionBegin; 137cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 138c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 139cb1e1211SMatthew G Knepley if (dim == 1) { 140cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 141cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 142cb1e1211SMatthew G Knepley } 143026175e5SToby Isaac ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 144026175e5SToby Isaac ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 145cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 146cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 147cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 148cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 149cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 150cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 151026175e5SToby Isaac for (d = 0; d < m; d++) { 152330485fdSToby Isaac PetscInt ctx[2]; 153026175e5SToby Isaac void *voidctx = (void *)(&ctx[0]); 154026175e5SToby Isaac void (*func)(const PetscReal *,PetscScalar *,void *) = DMPlexProjectRigidBody; 155cb1e1211SMatthew G Knepley 156330485fdSToby Isaac ctx[0] = dim; 157330485fdSToby Isaac ctx[1] = d; 158026175e5SToby Isaac ierr = DMPlexProjectFunction(dm,&func,&voidctx,INSERT_VALUES,mode[d]);CHKERRQ(ierr); 159cb1e1211SMatthew G Knepley } 160cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 161cb1e1211SMatthew G Knepley /* Orthonormalize system */ 162cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 163cb1e1211SMatthew G Knepley PetscScalar dots[6]; 164cb1e1211SMatthew G Knepley 165cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 166cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 167cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 168cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 169cb1e1211SMatthew G Knepley } 170cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 171cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 172cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 173cb1e1211SMatthew G Knepley } 174cb1e1211SMatthew G Knepley 175cb1e1211SMatthew G Knepley #undef __FUNCT__ 176b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight" 177b29cfa1cSToby Isaac /*@ 178b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 179b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 180b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 181b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 182b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 183b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 184b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 185b29cfa1cSToby Isaac 186b29cfa1cSToby Isaac Input Parameters: 187b29cfa1cSToby Isaac + dm - the DMPlex object 188b29cfa1cSToby Isaac - height - the maximum projection height >= 0 189b29cfa1cSToby Isaac 190b29cfa1cSToby Isaac Level: advanced 191b29cfa1cSToby Isaac 192048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal() 193b29cfa1cSToby Isaac @*/ 194b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 195b29cfa1cSToby Isaac { 196b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 197b29cfa1cSToby Isaac 198b29cfa1cSToby Isaac PetscFunctionBegin; 199b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 200b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 201b29cfa1cSToby Isaac PetscFunctionReturn(0); 202b29cfa1cSToby Isaac } 203b29cfa1cSToby Isaac 204b29cfa1cSToby Isaac #undef __FUNCT__ 205b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight" 206b29cfa1cSToby Isaac /*@ 207b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 208b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 209b29cfa1cSToby Isaac 210b29cfa1cSToby Isaac Input Parameters: 211b29cfa1cSToby Isaac . dm - the DMPlex object 212b29cfa1cSToby Isaac 213b29cfa1cSToby Isaac Output Parameters: 214b29cfa1cSToby Isaac . height - the maximum projection height 215b29cfa1cSToby Isaac 216b29cfa1cSToby Isaac Level: intermediate 217b29cfa1cSToby Isaac 218048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal() 219b29cfa1cSToby Isaac @*/ 220b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 221b29cfa1cSToby Isaac { 222b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 223b29cfa1cSToby Isaac 224b29cfa1cSToby Isaac PetscFunctionBegin; 225b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 226b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 227b29cfa1cSToby Isaac PetscFunctionReturn(0); 228b29cfa1cSToby Isaac } 229b29cfa1cSToby Isaac 230b29cfa1cSToby Isaac #undef __FUNCT__ 231a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal" 232bf3434eeSMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, DMLabel label, PetscInt numIds, const PetscInt ids[], void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 233a18a7fb9SMatthew G. Knepley { 2347d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 235dda36da7SMatthew G. Knepley PetscInt *numComp; 236a18a7fb9SMatthew G. Knepley PetscSection section; 237a18a7fb9SMatthew G. Knepley PetscScalar *values; 238ad96f515SMatthew G. Knepley PetscBool *fieldActive; 239dda36da7SMatthew G. Knepley PetscInt numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h; 240a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 241a18a7fb9SMatthew G. Knepley 242a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 2439ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2449ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2459ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2469ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 247c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2487a1a1bd4SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 249a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 250a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 251dda36da7SMatthew G. Knepley ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr); 2527d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 2537d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 2549ac3fadcSMatthew G. Knepley if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);} 2559ac3fadcSMatthew G. Knepley else {cellsp = sp;} 2567d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 2577d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 258fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 2597d1dd11eSToby Isaac if (pEnd <= pStart) continue; 2607d1dd11eSToby Isaac totDim = 0; 261a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 2629ac3fadcSMatthew G. Knepley PetscObject obj; 2639ac3fadcSMatthew G. Knepley PetscClassId id; 2649ac3fadcSMatthew G. Knepley 2659ac3fadcSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 2669ac3fadcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2679ac3fadcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 2689ac3fadcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2699ac3fadcSMatthew G. Knepley 270dda36da7SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 2717d1dd11eSToby Isaac if (!h) { 272ee2838f6SToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 2737d1dd11eSToby Isaac sp[f] = cellsp[f]; 2749ac3fadcSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2759ac3fadcSMatthew G. Knepley } else { 2767d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 2777d1dd11eSToby Isaac if (!sp[f]) continue; 2787d1dd11eSToby Isaac } 2799ac3fadcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 2809ac3fadcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 2819ac3fadcSMatthew G. Knepley 282dda36da7SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 283302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 284e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2859ac3fadcSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 286a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 287dda36da7SMatthew G. Knepley totDim += spDim*numComp[f]; 288a18a7fb9SMatthew G. Knepley } 2897d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 2907d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 291d374af2bSToby Isaac if (!totDim) continue; 292a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 293ad96f515SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 294ee2838f6SToby Isaac for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE; 295a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 296a18a7fb9SMatthew G. Knepley IS pointIS; 297a18a7fb9SMatthew G. Knepley const PetscInt *points; 298a18a7fb9SMatthew G. Knepley PetscInt n, p; 299a18a7fb9SMatthew G. Knepley 300a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 301a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 302a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 303a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 304a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 305e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 306a18a7fb9SMatthew G. Knepley 3077d1dd11eSToby Isaac if ((point < pStart) || (point >= pEnd)) continue; 308e1d0b1adSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 309ee2838f6SToby Isaac geom.dim = dim - h; 3107a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 311a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 312a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 313bf3434eeSMatthew G. Knepley 3147d1dd11eSToby Isaac if (!sp[f]) continue; 315a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 316a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 317a18a7fb9SMatthew G. Knepley if (funcs[f]) { 318dda36da7SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 319a18a7fb9SMatthew G. Knepley } else { 320dda36da7SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 321a18a7fb9SMatthew G. Knepley } 322dda36da7SMatthew G. Knepley v += numComp[f]; 323a18a7fb9SMatthew G. Knepley } 324a18a7fb9SMatthew G. Knepley } 325ad96f515SMatthew G. Knepley ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr); 326a18a7fb9SMatthew G. Knepley } 327a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 328a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 329a18a7fb9SMatthew G. Knepley } 3307d1dd11eSToby Isaac if (h) { 3317d1dd11eSToby Isaac for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 3327d1dd11eSToby Isaac } 3337d1dd11eSToby Isaac } 334a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 335ad96f515SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 3369ac3fadcSMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 337dda36da7SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 3387d1dd11eSToby Isaac if (maxHeight > 0) { 3397d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 3407d1dd11eSToby Isaac } 341a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 342a18a7fb9SMatthew G. Knepley } 343a18a7fb9SMatthew G. Knepley 344a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 345cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 3460f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 347cb1e1211SMatthew G Knepley { 3487d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 34915496722SMatthew G. Knepley PetscInt *numComp; 35072f94c41SMatthew G. Knepley PetscSection section; 35172f94c41SMatthew G. Knepley PetscScalar *values; 352b793a5ffSMatthew G. Knepley PetscInt Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight; 353cb1e1211SMatthew G Knepley PetscErrorCode ierr; 354cb1e1211SMatthew G Knepley 355cb1e1211SMatthew G Knepley PetscFunctionBegin; 3569ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 3579ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 3589ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 3599ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 360cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 361b793a5ffSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 362b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &numComp);CHKERRQ(ierr); 3637d1dd11eSToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 364ee2838f6SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 3657d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 3667d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 3677d1dd11eSToby Isaac if (maxHeight > 0) { 368b793a5ffSMatthew G. Knepley ierr = PetscMalloc1(Nf,&cellsp);CHKERRQ(ierr); 3697d1dd11eSToby Isaac } 370048b7b1eSToby Isaac else { 371048b7b1eSToby Isaac cellsp = sp; 372048b7b1eSToby Isaac } 3737d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 3747d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 375fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 376048b7b1eSToby Isaac if (pEnd <= pStart) continue; 3777d1dd11eSToby Isaac totDim = 0; 378b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 37915496722SMatthew G. Knepley PetscObject obj; 38015496722SMatthew G. Knepley PetscClassId id; 3810f2d7e86SMatthew G. Knepley 38215496722SMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 38315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 38415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 38515496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 38615496722SMatthew G. Knepley 38715496722SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 3887d1dd11eSToby Isaac if (!h) { 3897d1dd11eSToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 3907d1dd11eSToby Isaac sp[f] = cellsp[f]; 39115496722SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 3927d1dd11eSToby Isaac } 3937d1dd11eSToby Isaac else { 3947d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 3957d1dd11eSToby Isaac if (!sp[f]) { 3967d1dd11eSToby Isaac continue; 3977d1dd11eSToby Isaac } 3987d1dd11eSToby Isaac } 39915496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 40015496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 40115496722SMatthew G. Knepley 402*77d9c660SToby Isaac cellsp[f] = NULL; 403*77d9c660SToby Isaac if (h) { 404*77d9c660SToby Isaac sp[f] = NULL; 405*77d9c660SToby Isaac continue; /* FV doesn't have fields that can be evaluated at faces, corners, etc. */ 406*77d9c660SToby Isaac } 40715496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 408302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 409e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 41015496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 41172f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 41215496722SMatthew G. Knepley totDim += spDim*numComp[f]; 413cb1e1211SMatthew G Knepley } 4147d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 4157d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 416d374af2bSToby Isaac if (!totDim) continue; 41772f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4187d1dd11eSToby Isaac for (p = pStart; p < pEnd; ++p) { 419e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 420cb1e1211SMatthew G Knepley 42131383a9bSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 422910c25dcSToby Isaac geom.dim = dim - h; 4237a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 424b793a5ffSMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 425c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[f] : NULL; 4260f2d7e86SMatthew G. Knepley 4277d1dd11eSToby Isaac if (!sp[f]) continue; 42872f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 42972f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 430120386c5SMatthew G. Knepley if (funcs[f]) { 431e1d0b1adSMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 432120386c5SMatthew G. Knepley } else { 43315496722SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 434120386c5SMatthew G. Knepley } 43515496722SMatthew G. Knepley v += numComp[f]; 436cb1e1211SMatthew G Knepley } 437cb1e1211SMatthew G Knepley } 4387d1dd11eSToby Isaac ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr); 439cb1e1211SMatthew G Knepley } 44072f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 441ee2838f6SToby Isaac if (h || !maxHeight) { 442b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 4437d1dd11eSToby Isaac } 4447d1dd11eSToby Isaac } 44515496722SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 4467d1dd11eSToby Isaac if (maxHeight > 0) { 447b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);} 4487d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 4497d1dd11eSToby Isaac } 450cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 451cb1e1211SMatthew G Knepley } 452cb1e1211SMatthew G Knepley 453cb1e1211SMatthew G Knepley #undef __FUNCT__ 4548040c1f3SToby Isaac #define __FUNCT__ "DMPlexProjectFunction" 4558040c1f3SToby Isaac /*@C 4568040c1f3SToby Isaac DMPlexProjectFunction - This projects the given function into the function space provided. 4578040c1f3SToby Isaac 4588040c1f3SToby Isaac Input Parameters: 4598040c1f3SToby Isaac + dm - The DM 4608040c1f3SToby Isaac . funcs - The coordinate functions to evaluate, one per field 4618040c1f3SToby Isaac . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 4628040c1f3SToby Isaac - mode - The insertion mode for values 4638040c1f3SToby Isaac 4648040c1f3SToby Isaac Output Parameter: 4658040c1f3SToby Isaac . X - vector 4668040c1f3SToby Isaac 4678040c1f3SToby Isaac Level: developer 4688040c1f3SToby Isaac 4698040c1f3SToby Isaac .seealso: DMPlexComputeL2Diff() 4708040c1f3SToby Isaac @*/ 4718040c1f3SToby Isaac PetscErrorCode DMPlexProjectFunction(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) 4728040c1f3SToby Isaac { 4738040c1f3SToby Isaac Vec localX; 4748040c1f3SToby Isaac PetscErrorCode ierr; 4758040c1f3SToby Isaac 4768040c1f3SToby Isaac PetscFunctionBegin; 4778040c1f3SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4788040c1f3SToby Isaac ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 4798040c1f3SToby Isaac ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, mode, localX);CHKERRQ(ierr); 4808040c1f3SToby Isaac ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 4818040c1f3SToby Isaac ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 4828040c1f3SToby Isaac ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 483cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 484cb1e1211SMatthew G Knepley } 485cb1e1211SMatthew G Knepley 486cb1e1211SMatthew G Knepley #undef __FUNCT__ 4870f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal" 4883bc3b0a0SMatthew 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) 4890f2d7e86SMatthew G. Knepley { 4900f2d7e86SMatthew G. Knepley DM dmAux; 491d5396abcSMatthew G. Knepley PetscDS prob, probAux = NULL; 4920f2d7e86SMatthew G. Knepley Vec A; 493d5396abcSMatthew G. Knepley PetscSection section, sectionAux = NULL; 494b793a5ffSMatthew G. Knepley PetscDualSpace *sp; 495b793a5ffSMatthew G. Knepley PetscInt *Ncf; 496326413afSMatthew G. Knepley PetscScalar *values, *u, *u_x, *a, *a_x; 497d5396abcSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 4989ac3fadcSMatthew G. Knepley PetscInt Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight; 4990f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 5000f2d7e86SMatthew G. Knepley 5010f2d7e86SMatthew G. Knepley PetscFunctionBegin; 502048b7b1eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 503048b7b1eSToby Isaac if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");} 5042764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 505c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 5060f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 5070f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 508b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr); 5090f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 5102764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 5112764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr); 5122764a2aaSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 5130f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 5140f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 5150f2d7e86SMatthew G. Knepley if (dmAux) { 5162764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 517326413afSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 5182764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr); 5190f2d7e86SMatthew G. Knepley } 520d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 5210f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 5220f2d7e86SMatthew 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); 5230f2d7e86SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 5240f2d7e86SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 5259ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 5269ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 5270f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 528326413afSMatthew G. Knepley PetscScalar *coefficients = NULL, *coefficientsAux = NULL; 529326413afSMatthew G. Knepley 5308e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 531326413afSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 532326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5330f2d7e86SMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 534d5396abcSMatthew G. Knepley PetscObject obj; 535d5396abcSMatthew G. Knepley PetscClassId id; 5363113607cSMatthew G. Knepley 537d5396abcSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 538d5396abcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 539d5396abcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 540d5396abcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 541d5396abcSMatthew G. Knepley 542b793a5ffSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 543b793a5ffSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 544b793a5ffSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr); 545d5396abcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 546d5396abcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 547d5396abcSMatthew G. Knepley 548b793a5ffSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr); 549302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 550e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 551d5396abcSMatthew G. Knepley } 552b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 5530f2d7e86SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 5540f2d7e86SMatthew G. Knepley PetscQuadrature quad; 5550f2d7e86SMatthew G. Knepley const PetscReal *points, *weights; 5560f2d7e86SMatthew G. Knepley PetscInt numPoints, q; 5570f2d7e86SMatthew G. Knepley 5580f2d7e86SMatthew G. Knepley if (funcs[f]) { 559b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr); 5600f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr); 5610f2d7e86SMatthew G. Knepley for (q = 0; q < numPoints; ++q) { 5620f2d7e86SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x); 563326413afSMatthew G. Knepley ierr = EvaluateFieldJets(prob, PETSC_FALSE, q, invJ, coefficients, NULL, u, u_x, NULL);CHKERRQ(ierr); 564326413afSMatthew G. Knepley ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr); 5653bc3b0a0SMatthew G. Knepley (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]); 5660f2d7e86SMatthew G. Knepley } 5670f2d7e86SMatthew G. Knepley } else { 568b793a5ffSMatthew G. Knepley for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0; 5690f2d7e86SMatthew G. Knepley } 570b793a5ffSMatthew G. Knepley v += Ncf[f]; 5710f2d7e86SMatthew G. Knepley } 5720f2d7e86SMatthew G. Knepley } 573326413afSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 574326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5750f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 5760f2d7e86SMatthew G. Knepley } 5770f2d7e86SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 5780f2d7e86SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 579b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 580b793a5ffSMatthew G. Knepley ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr); 5810f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 5820f2d7e86SMatthew G. Knepley } 5830f2d7e86SMatthew G. Knepley 5840f2d7e86SMatthew G. Knepley #undef __FUNCT__ 585d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal" 586d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], void (*func)(const PetscReal[], PetscScalar *, void *), void *ctx, Vec locX) 58755f2e967SMatthew G. Knepley { 58855f2e967SMatthew G. Knepley void (**funcs)(const PetscReal x[], PetscScalar *u, void *ctx); 58955f2e967SMatthew G. Knepley void **ctxs; 590d7ddef95SMatthew G. Knepley PetscInt numFields; 591d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 592d7ddef95SMatthew G. Knepley 593d7ddef95SMatthew G. Knepley PetscFunctionBegin; 594d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 595d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 596d7ddef95SMatthew G. Knepley funcs[field] = func; 597d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 598d7ddef95SMatthew G. Knepley ierr = DMPlexProjectFunctionLabelLocal(dm, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 599d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 600d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 601d7ddef95SMatthew G. Knepley } 602d7ddef95SMatthew G. Knepley 603d7ddef95SMatthew G. Knepley #undef __FUNCT__ 604d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal" 605d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, 606d7ddef95SMatthew 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) 607d7ddef95SMatthew G. Knepley { 60861f58d28SMatthew G. Knepley PetscDS prob; 609da97024aSMatthew G. Knepley PetscSF sf; 610d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 611d7ddef95SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *grad; 612da97024aSMatthew G. Knepley const PetscInt *leaves; 613d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 614520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 615d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 616d7ddef95SMatthew G. Knepley 617d7ddef95SMatthew G. Knepley PetscFunctionBegin; 618da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 619da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 620da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 621d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 622d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 62361f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 624d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 625d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 626d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 627d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 628d7ddef95SMatthew G. Knepley if (Grad) { 629c0a6632aSMatthew G. Knepley PetscFV fv; 630c0a6632aSMatthew G. Knepley 631c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 632d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 633d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 634d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 635d7ddef95SMatthew G. Knepley ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 636d7ddef95SMatthew G. Knepley } 637d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 638d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 639d7ddef95SMatthew G. Knepley IS faceIS; 640d7ddef95SMatthew G. Knepley const PetscInt *faces; 641d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 642d7ddef95SMatthew G. Knepley 643d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 644d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 645d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 646d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 647d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 648d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 649d7ddef95SMatthew G. Knepley const PetscFVFaceGeom *fg; 650d7ddef95SMatthew G. Knepley 651d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 652da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 653da97024aSMatthew G. Knepley if (loc >= 0) continue; 654d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 655d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 656d7ddef95SMatthew G. Knepley if (Grad) { 657d7ddef95SMatthew G. Knepley const PetscFVCellGeom *cg; 658d7ddef95SMatthew G. Knepley const PetscScalar *cx, *cgrad; 659d7ddef95SMatthew G. Knepley PetscScalar *xG; 660d7ddef95SMatthew G. Knepley PetscReal dx[3]; 661d7ddef95SMatthew G. Knepley PetscInt d; 662d7ddef95SMatthew G. Knepley 663d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 664d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 665d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 666520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 667d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 668d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 669520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr); 670d7ddef95SMatthew G. Knepley } else { 671d7ddef95SMatthew G. Knepley const PetscScalar *xI; 672d7ddef95SMatthew G. Knepley PetscScalar *xG; 673d7ddef95SMatthew G. Knepley 674d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 675520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 676520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr); 677d7ddef95SMatthew G. Knepley } 678d7ddef95SMatthew G. Knepley } 679d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 680d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 681d7ddef95SMatthew G. Knepley } 682d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 683d7ddef95SMatthew G. Knepley if (Grad) { 684d7ddef95SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 685d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 686d7ddef95SMatthew G. Knepley } 687d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 688d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 689d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 690d7ddef95SMatthew G. Knepley } 691d7ddef95SMatthew G. Knepley 692d7ddef95SMatthew G. Knepley #undef __FUNCT__ 693d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues" 694d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 695d7ddef95SMatthew G. Knepley { 696d7ddef95SMatthew G. Knepley PetscInt numBd, b; 69755f2e967SMatthew G. Knepley PetscErrorCode ierr; 69855f2e967SMatthew G. Knepley 69955f2e967SMatthew G. Knepley PetscFunctionBegin; 70055f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 701d7ddef95SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 702d7ddef95SMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 703d7ddef95SMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 704d7ddef95SMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 70555f2e967SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 70655f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 70755f2e967SMatthew G. Knepley PetscBool isEssential; 708d7ddef95SMatthew G. Knepley const char *labelname; 709d7ddef95SMatthew G. Knepley DMLabel label; 710d7ddef95SMatthew G. Knepley PetscInt field; 711d7ddef95SMatthew G. Knepley PetscObject obj; 712d7ddef95SMatthew G. Knepley PetscClassId id; 71355f2e967SMatthew G. Knepley void (*func)(); 714d7ddef95SMatthew G. Knepley PetscInt numids; 715d7ddef95SMatthew G. Knepley const PetscInt *ids; 71655f2e967SMatthew G. Knepley void *ctx; 71755f2e967SMatthew G. Knepley 71863d5297fSMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 719d7ddef95SMatthew G. Knepley if (!isEssential) continue; 72063d5297fSMatthew G. Knepley ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr); 721d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 722d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 723d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 724d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, field, label, numids, ids, (void (*)(const PetscReal[], PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr); 725d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 72643ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 727d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, 728d7ddef95SMatthew G. Knepley field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 729d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 73055f2e967SMatthew G. Knepley } 73155f2e967SMatthew G. Knepley PetscFunctionReturn(0); 73255f2e967SMatthew G. Knepley } 73355f2e967SMatthew G. Knepley 734cb1e1211SMatthew G Knepley #undef __FUNCT__ 735cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 736cb1e1211SMatthew G Knepley /*@C 737cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 738cb1e1211SMatthew G Knepley 739cb1e1211SMatthew G Knepley Input Parameters: 740cb1e1211SMatthew G Knepley + dm - The DM 741cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 74251259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 743cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 744cb1e1211SMatthew G Knepley 745cb1e1211SMatthew G Knepley Output Parameter: 746cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 747cb1e1211SMatthew G Knepley 748cb1e1211SMatthew G Knepley Level: developer 749cb1e1211SMatthew G Knepley 75015496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 751878cb397SSatish Balay @*/ 7520f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 753cb1e1211SMatthew G Knepley { 754cb1e1211SMatthew G Knepley const PetscInt debug = 0; 755cb1e1211SMatthew G Knepley PetscSection section; 756c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 757cb1e1211SMatthew G Knepley Vec localX; 75815496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 759cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 760cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 76115496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7629ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 763cb1e1211SMatthew G Knepley PetscErrorCode ierr; 764cb1e1211SMatthew G Knepley 765cb1e1211SMatthew G Knepley PetscFunctionBegin; 766c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 767cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 768cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 769cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 770cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 771cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 772cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 77315496722SMatthew G. Knepley PetscObject obj; 77415496722SMatthew G. Knepley PetscClassId id; 775c5bbbd5bSMatthew G. Knepley PetscInt Nc; 776c5bbbd5bSMatthew G. Knepley 77715496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 77815496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 77915496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 78015496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 78115496722SMatthew G. Knepley 7820f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 7830f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 78415496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 78515496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 78615496722SMatthew G. Knepley 78715496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 78815496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 78915496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 790c5bbbd5bSMatthew G. Knepley numComponents += Nc; 791cb1e1211SMatthew G Knepley } 79215496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 7930f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 79415496722SMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 795cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 7969ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 7979ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 798cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 799a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 800cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 801cb1e1211SMatthew G Knepley 8028e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 803cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 804cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 805cb1e1211SMatthew G Knepley 80615496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 80715496722SMatthew G. Knepley PetscObject obj; 80815496722SMatthew G. Knepley PetscClassId id; 809c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 81015496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 811cb1e1211SMatthew G Knepley 81215496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 81315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 81415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 81515496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 81615496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 817cb1e1211SMatthew G Knepley if (debug) { 818cb1e1211SMatthew G Knepley char title[1024]; 819cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 82015496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 821cb1e1211SMatthew G Knepley } 822cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 82315496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 824c110b1eeSGeoffrey Irving (*funcs[field])(coords, funcVal, ctx); 82515496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 82615496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 82715496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 82815496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 82915496722SMatthew 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);} 83015496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 831cb1e1211SMatthew G Knepley } 832cb1e1211SMatthew G Knepley } 83315496722SMatthew G. Knepley fieldOffset += Nb*Nc; 834cb1e1211SMatthew G Knepley } 835cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 836cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 837cb1e1211SMatthew G Knepley localDiff += elemDiff; 838cb1e1211SMatthew G Knepley } 83915496722SMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 840cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 841a529e722SBarry Smith ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 842cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 843cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 844cb1e1211SMatthew G Knepley } 845cb1e1211SMatthew G Knepley 846cb1e1211SMatthew G Knepley #undef __FUNCT__ 84740e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 84840e14135SMatthew G. Knepley /*@C 84940e14135SMatthew 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. 85040e14135SMatthew G. Knepley 85140e14135SMatthew G. Knepley Input Parameters: 85240e14135SMatthew G. Knepley + dm - The DM 85340e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 85451259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 85540e14135SMatthew G. Knepley . X - The coefficient vector u_h 85640e14135SMatthew G. Knepley - n - The vector to project along 85740e14135SMatthew G. Knepley 85840e14135SMatthew G. Knepley Output Parameter: 85940e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 86040e14135SMatthew G. Knepley 86140e14135SMatthew G. Knepley Level: developer 86240e14135SMatthew G. Knepley 86340e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 86440e14135SMatthew G. Knepley @*/ 8650f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, void (**funcs)(const PetscReal [], const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 866cb1e1211SMatthew G Knepley { 86740e14135SMatthew G. Knepley const PetscInt debug = 0; 868cb1e1211SMatthew G Knepley PetscSection section; 86940e14135SMatthew G. Knepley PetscQuadrature quad; 87040e14135SMatthew G. Knepley Vec localX; 87140e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 87240e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 87340e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 8749ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp; 875cb1e1211SMatthew G Knepley PetscErrorCode ierr; 876cb1e1211SMatthew G Knepley 877cb1e1211SMatthew G Knepley PetscFunctionBegin; 878c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 87940e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 88040e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 88140e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 88240e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 88340e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 884652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 8850f2d7e86SMatthew G. Knepley PetscFE fe; 88640e14135SMatthew G. Knepley PetscInt Nc; 887652b88e8SMatthew G. Knepley 8880f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 8890f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8900f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 89140e14135SMatthew G. Knepley numComponents += Nc; 892652b88e8SMatthew G. Knepley } 89340e14135SMatthew G. Knepley /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 89440e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 89540e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 8969ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 8979ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 89840e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 89940e14135SMatthew G. Knepley PetscScalar *x = NULL; 90040e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 901652b88e8SMatthew G. Knepley 9028e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 90340e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 90440e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 90540e14135SMatthew G. Knepley 90640e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 9070f2d7e86SMatthew G. Knepley PetscFE fe; 90851259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 90921454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 91040e14135SMatthew G. Knepley PetscReal *basisDer; 91121454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 91240e14135SMatthew G. Knepley 9130f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 91421454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 9150f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 9160f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr); 9170f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 91840e14135SMatthew G. Knepley if (debug) { 91940e14135SMatthew G. Knepley char title[1024]; 92040e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 92140e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 922652b88e8SMatthew G. Knepley } 92340e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 92440e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 92540e14135SMatthew G. Knepley coords[d] = v0[d]; 92640e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 92740e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 928652b88e8SMatthew G. Knepley } 92940e14135SMatthew G. Knepley } 93051259fa3SMatthew G. Knepley (*funcs[field])(coords, n, funcVal, ctx); 93140e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 93240e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 93340e14135SMatthew G. Knepley 93440e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 93540e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 93640e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 93740e14135SMatthew G. Knepley 93840e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 93940e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 94040e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 94140e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 94240e14135SMatthew G. Knepley } 94340e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 94440e14135SMatthew G. Knepley } 94540e14135SMatthew G. Knepley } 94640e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 94740e14135SMatthew 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);} 94840e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 94940e14135SMatthew G. Knepley } 95040e14135SMatthew G. Knepley } 95140e14135SMatthew G. Knepley comp += Ncomp; 95240e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 95340e14135SMatthew G. Knepley } 95440e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 95540e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 95640e14135SMatthew G. Knepley localDiff += elemDiff; 95740e14135SMatthew G. Knepley } 95840e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 95940e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 960a529e722SBarry Smith ierr = MPI_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 96140e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 962cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 963cb1e1211SMatthew G Knepley } 964cb1e1211SMatthew G Knepley 965a0845e3aSMatthew G. Knepley #undef __FUNCT__ 96673d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff" 96715496722SMatthew G. Knepley /*@C 96815496722SMatthew 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. 96915496722SMatthew G. Knepley 97015496722SMatthew G. Knepley Input Parameters: 97115496722SMatthew G. Knepley + dm - The DM 97215496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component 97315496722SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 97415496722SMatthew G. Knepley - X - The coefficient vector u_h 97515496722SMatthew G. Knepley 97615496722SMatthew G. Knepley Output Parameter: 97715496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2 97815496722SMatthew G. Knepley 97915496722SMatthew G. Knepley Level: developer 98015496722SMatthew G. Knepley 98115496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff() 98215496722SMatthew G. Knepley @*/ 9830f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, void (**funcs)(const PetscReal [], PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 98473d901b8SMatthew G. Knepley { 98573d901b8SMatthew G. Knepley const PetscInt debug = 0; 98673d901b8SMatthew G. Knepley PetscSection section; 98773d901b8SMatthew G. Knepley PetscQuadrature quad; 98873d901b8SMatthew G. Knepley Vec localX; 98915496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 99073d901b8SMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 99173d901b8SMatthew G. Knepley PetscReal *localDiff; 99215496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 9939ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 99473d901b8SMatthew G. Knepley PetscErrorCode ierr; 99573d901b8SMatthew G. Knepley 99673d901b8SMatthew G. Knepley PetscFunctionBegin; 997c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 99873d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 99973d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 100073d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 100173d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 100273d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 100373d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 100415496722SMatthew G. Knepley PetscObject obj; 100515496722SMatthew G. Knepley PetscClassId id; 100673d901b8SMatthew G. Knepley PetscInt Nc; 100773d901b8SMatthew G. Knepley 100815496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 100915496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 101015496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 101115496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 101215496722SMatthew G. Knepley 10130f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 10140f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 101515496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 101615496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 101715496722SMatthew G. Knepley 101815496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 101915496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 102015496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 102173d901b8SMatthew G. Knepley numComponents += Nc; 102273d901b8SMatthew G. Knepley } 102315496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 10240f2d7e86SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 102515496722SMatthew G. Knepley ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 102673d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 10279ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 10289ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 102973d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 103073d901b8SMatthew G. Knepley PetscScalar *x = NULL; 103173d901b8SMatthew G. Knepley 10328e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 103373d901b8SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 103473d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 103573d901b8SMatthew G. Knepley 103615496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 103715496722SMatthew G. Knepley PetscObject obj; 103815496722SMatthew G. Knepley PetscClassId id; 103973d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 104015496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 104173d901b8SMatthew G. Knepley 104215496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 104315496722SMatthew G. Knepley 104415496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 104515496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 104615496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 104715496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 104815496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 104973d901b8SMatthew G. Knepley if (debug) { 105073d901b8SMatthew G. Knepley char title[1024]; 105173d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 105215496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 105373d901b8SMatthew G. Knepley } 105473d901b8SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 105515496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 105673d901b8SMatthew G. Knepley (*funcs[field])(coords, funcVal, ctx); 105715496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 105815496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 105915496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 106015496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 106115496722SMatthew 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);} 106215496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 106373d901b8SMatthew G. Knepley } 106473d901b8SMatthew G. Knepley } 106515496722SMatthew G. Knepley fieldOffset += Nb*Nc; 106673d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 106773d901b8SMatthew G. Knepley } 106873d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 106973d901b8SMatthew G. Knepley } 107073d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1071a529e722SBarry Smith ierr = MPI_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 107273d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 107315496722SMatthew G. Knepley ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 107473d901b8SMatthew G. Knepley PetscFunctionReturn(0); 107573d901b8SMatthew G. Knepley } 107673d901b8SMatthew G. Knepley 107773d901b8SMatthew G. Knepley #undef __FUNCT__ 107873d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM" 107973d901b8SMatthew G. Knepley /*@ 108073d901b8SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user 108173d901b8SMatthew G. Knepley 108273d901b8SMatthew G. Knepley Input Parameters: 108373d901b8SMatthew G. Knepley + dm - The mesh 108473d901b8SMatthew G. Knepley . X - Local input vector 108573d901b8SMatthew G. Knepley - user - The user context 108673d901b8SMatthew G. Knepley 108773d901b8SMatthew G. Knepley Output Parameter: 108873d901b8SMatthew G. Knepley . integral - Local integral for each field 108973d901b8SMatthew G. Knepley 109073d901b8SMatthew G. Knepley Level: developer 109173d901b8SMatthew G. Knepley 109273d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 109373d901b8SMatthew G. Knepley @*/ 10940f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user) 109573d901b8SMatthew G. Knepley { 109673d901b8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 109773d901b8SMatthew G. Knepley DM dmAux; 109873d901b8SMatthew G. Knepley Vec localX, A; 10992764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 110073d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1101bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 110273d901b8SMatthew G. Knepley PetscScalar *u, *a = NULL; 1103c1f031eeSMatthew G. Knepley PetscReal *lintegral, *vol; 11049ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c; 11050f2d7e86SMatthew G. Knepley PetscInt totDim, totDimAux; 110673d901b8SMatthew G. Knepley PetscErrorCode ierr; 110773d901b8SMatthew G. Knepley 110873d901b8SMatthew G. Knepley PetscFunctionBegin; 1109c1f031eeSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 111073d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 111173d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 111273d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1113c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 111473d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 11152764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 11162764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 111773d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 111873d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 11199ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 11209ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 112173d901b8SMatthew G. Knepley numCells = cEnd - cStart; 112273d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 112373d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 112473d901b8SMatthew G. Knepley if (dmAux) { 112573d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 11262764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 11272764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 112873d901b8SMatthew G. Knepley } 1129d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1130c1f031eeSMatthew G. Knepley ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr); 11310f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1132c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;} 113373d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 113473d901b8SMatthew G. Knepley PetscScalar *x = NULL; 113573d901b8SMatthew G. Knepley PetscInt i; 113673d901b8SMatthew G. Knepley 1137bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr); 1138c1f031eeSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr); 1139bbce034cSMatthew 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); 114073d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 11410f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 114273d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 114373d901b8SMatthew G. Knepley if (dmAux) { 114473d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 11450f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 114673d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 114773d901b8SMatthew G. Knepley } 114873d901b8SMatthew G. Knepley } 114973d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1150c1f031eeSMatthew G. Knepley PetscObject obj; 1151c1f031eeSMatthew G. Knepley PetscClassId id; 1152c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 115373d901b8SMatthew G. Knepley 1154c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1155c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1156c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1157c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1158c1f031eeSMatthew G. Knepley PetscQuadrature q; 1159c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1160c1f031eeSMatthew G. Knepley 11610f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1162c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1163c1f031eeSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1164c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1165c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 116673d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 11670f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 116873d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 116973d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 117073d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 117173d901b8SMatthew G. Knepley offset = numCells - Nr; 1172c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr); 1173c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr); 1174c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1175b69edc29SMatthew G. Knepley /* PetscFV fv = (PetscFV) obj; */ 1176c1f031eeSMatthew G. Knepley PetscInt foff; 1177420e96edSMatthew G. Knepley PetscPointFunc obj_func; 1178b69edc29SMatthew G. Knepley PetscScalar lint; 1179c1f031eeSMatthew G. Knepley 1180c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 1181c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1182c1f031eeSMatthew G. Knepley if (obj_func) { 1183c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 1184c1f031eeSMatthew G. Knepley /* TODO: Need full pointwise interpolation and get centroid for x argument */ 118530b9ff8bSMatthew G. Knepley obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint); 1186b69edc29SMatthew G. Knepley lintegral[f] = PetscRealPart(lint)*vol[c]; 118773d901b8SMatthew G. Knepley } 1188c1f031eeSMatthew G. Knepley } 1189c1f031eeSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1190c1f031eeSMatthew G. Knepley } 119173d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 119273d901b8SMatthew G. Knepley if (mesh->printFEM) { 119373d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr); 1194c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);} 119573d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 119673d901b8SMatthew G. Knepley } 119773d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1198a529e722SBarry Smith ierr = MPI_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 1199c1f031eeSMatthew G. Knepley ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr); 1200c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 120173d901b8SMatthew G. Knepley PetscFunctionReturn(0); 120273d901b8SMatthew G. Knepley } 120373d901b8SMatthew G. Knepley 120473d901b8SMatthew G. Knepley #undef __FUNCT__ 1205d69c5d34SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorFEM" 1206d69c5d34SMatthew G. Knepley /*@ 1207d69c5d34SMatthew G. Knepley DMPlexComputeInterpolatorFEM - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1208d69c5d34SMatthew G. Knepley 1209d69c5d34SMatthew G. Knepley Input Parameters: 1210d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1211d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1212d69c5d34SMatthew G. Knepley - user - The user context 1213d69c5d34SMatthew G. Knepley 1214d69c5d34SMatthew G. Knepley Output Parameter: 1215934789fcSMatthew G. Knepley . In - The interpolation matrix 1216d69c5d34SMatthew G. Knepley 1217d69c5d34SMatthew G. Knepley Note: 1218d69c5d34SMatthew G. Knepley The first member of the user context must be an FEMContext. 1219d69c5d34SMatthew G. Knepley 1220d69c5d34SMatthew G. Knepley We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator, 1221d69c5d34SMatthew G. Knepley like a GPU, or vectorize on a multicore machine. 1222d69c5d34SMatthew G. Knepley 1223d69c5d34SMatthew G. Knepley Level: developer 1224d69c5d34SMatthew G. Knepley 1225d69c5d34SMatthew G. Knepley .seealso: DMPlexComputeJacobianFEM() 1226d69c5d34SMatthew G. Knepley @*/ 1227934789fcSMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorFEM(DM dmc, DM dmf, Mat In, void *user) 1228d69c5d34SMatthew G. Knepley { 1229d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1230d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 12312764a2aaSMatthew G. Knepley PetscDS prob; 1232d69c5d34SMatthew G. Knepley PetscFE *feRef; 123397c42addSMatthew G. Knepley PetscFV *fvRef; 1234d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1235d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1236d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 12379ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c; 12380f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1239d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1240d69c5d34SMatthew G. Knepley 1241d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1242d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1243c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1244d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1245d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1246d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1247d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1248d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1249d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 12509ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 12519ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 12522764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 125397c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 1254d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 125597c42addSMatthew G. Knepley PetscObject obj; 125697c42addSMatthew G. Knepley PetscClassId id; 1257aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 1258d69c5d34SMatthew G. Knepley 125997c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 126097c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 126197c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 126297c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 126397c42addSMatthew G. Knepley 12640f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1265d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 12660f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 126797c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 126897c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 126997c42addSMatthew G. Knepley PetscDualSpace Q; 127097c42addSMatthew G. Knepley 127197c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 127297c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 127397c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 127497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 127597c42addSMatthew G. Knepley } 12760f2d7e86SMatthew G. Knepley rTotDim += rNb*Nc; 1277d69c5d34SMatthew G. Knepley } 12782764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 12790f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 12800f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1281d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1282d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1283d69c5d34SMatthew G. Knepley PetscQuadrature f; 1284d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1285d69c5d34SMatthew G. Knepley PetscReal *points; 1286d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1287d69c5d34SMatthew G. Knepley 1288d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 128997c42addSMatthew G. Knepley if (feRef[fieldI]) { 1290d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 12910f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 129297c42addSMatthew G. Knepley } else { 129397c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 129497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 129597c42addSMatthew G. Knepley } 1296d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1297d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1298d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1299d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1300d69c5d34SMatthew G. Knepley npoints += Np; 1301d69c5d34SMatthew G. Knepley } 1302d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1303d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1304d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1305d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1306d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1307d69c5d34SMatthew G. Knepley } 1308d69c5d34SMatthew G. Knepley 1309d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 131097c42addSMatthew G. Knepley PetscObject obj; 131197c42addSMatthew G. Knepley PetscClassId id; 1312d69c5d34SMatthew G. Knepley PetscReal *B; 1313aa7890ccSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j; 1314d69c5d34SMatthew G. Knepley 131597c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr); 131697c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 131797c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 131897c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1319d69c5d34SMatthew G. Knepley 1320d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 13210f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 13220f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1323ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1324ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 13257c927364SMatthew 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); 13260f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1327d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1328d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1329d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1330d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 133136a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 13320f2d7e86SMatthew 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]; 133336a6d9c0SMatthew G. Knepley } 1334d69c5d34SMatthew G. Knepley } 1335d69c5d34SMatthew G. Knepley } 13360f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1337ffe73a53SMatthew G. Knepley } 133897c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 133997c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 134097c42addSMatthew G. Knepley 134197c42addSMatthew G. Knepley /* Evaluate constant function at points */ 134297c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 134397c42addSMatthew G. Knepley cpdim = 1; 134497c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 134597c42addSMatthew G. Knepley if (fieldI == fieldJ) { 134697c42addSMatthew 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); 134797c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 134897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 134997c42addSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 135097c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 135197c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 135297c42addSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p]; 135397c42addSMatthew G. Knepley } 135497c42addSMatthew G. Knepley } 135597c42addSMatthew G. Knepley } 135697c42addSMatthew G. Knepley } 135797c42addSMatthew G. Knepley } 135836a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1359d69c5d34SMatthew G. Knepley } 1360d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1361549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1362d69c5d34SMatthew G. Knepley } 13630f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 13647f5b169aSMatthew G. Knepley /* Preallocate matrix */ 13657f5b169aSMatthew G. Knepley { 13667f5b169aSMatthew G. Knepley PetscHashJK ht; 13677f5b169aSMatthew G. Knepley PetscLayout rLayout; 13687f5b169aSMatthew G. Knepley PetscInt *dnz, *onz, *cellCIndices, *cellFIndices; 13697f5b169aSMatthew G. Knepley PetscInt locRows, rStart, rEnd, cell, r; 13707f5b169aSMatthew G. Knepley 13717f5b169aSMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 13727f5b169aSMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 13737f5b169aSMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 13747f5b169aSMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 13757f5b169aSMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 13767f5b169aSMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 13777f5b169aSMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 13787f5b169aSMatthew G. Knepley ierr = PetscCalloc4(locRows,&dnz,locRows,&onz,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 13797f5b169aSMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 13807f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 13817f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 13827f5b169aSMatthew G. Knepley for (r = 0; r < rTotDim; ++r) { 13837f5b169aSMatthew G. Knepley PetscHashJKKey key; 13847f5b169aSMatthew G. Knepley PetscHashJKIter missing, iter; 13857f5b169aSMatthew G. Knepley 13867f5b169aSMatthew G. Knepley key.j = cellFIndices[r]; 13877f5b169aSMatthew G. Knepley if (key.j < 0) continue; 13887f5b169aSMatthew G. Knepley for (c = 0; c < cTotDim; ++c) { 13897f5b169aSMatthew G. Knepley key.k = cellCIndices[c]; 13907f5b169aSMatthew G. Knepley if (key.k < 0) continue; 13917f5b169aSMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 13927f5b169aSMatthew G. Knepley if (missing) { 13937f5b169aSMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 13947f5b169aSMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 13957f5b169aSMatthew G. Knepley else ++onz[key.j-rStart]; 13967f5b169aSMatthew G. Knepley } 13977f5b169aSMatthew G. Knepley } 13987f5b169aSMatthew G. Knepley } 13997f5b169aSMatthew G. Knepley } 14007f5b169aSMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 14017f5b169aSMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 14027f5b169aSMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 14037f5b169aSMatthew G. Knepley ierr = PetscFree4(dnz,onz,cellCIndices,cellFIndices);CHKERRQ(ierr); 14047f5b169aSMatthew G. Knepley } 14057f5b169aSMatthew G. Knepley /* Fill matrix */ 14067f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1407d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1408934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1409d69c5d34SMatthew G. Knepley } 1410549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 141197c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 1412549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1413934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1414934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1415d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1416d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1417934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1418934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1419d69c5d34SMatthew G. Knepley } 1420d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1421d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1422d69c5d34SMatthew G. Knepley } 14236c73c22cSMatthew G. Knepley 14246c73c22cSMatthew G. Knepley #undef __FUNCT__ 14257c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM" 14267c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 14277c927364SMatthew G. Knepley { 1428e9d4ef1bSMatthew G. Knepley PetscDS prob; 14297c927364SMatthew G. Knepley PetscFE *feRef; 143097c42addSMatthew G. Knepley PetscFV *fvRef; 14317c927364SMatthew G. Knepley Vec fv, cv; 14327c927364SMatthew G. Knepley IS fis, cis; 14337c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 14347c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 14359ac3fadcSMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, offsetC, offsetF, m; 14367c927364SMatthew G. Knepley PetscErrorCode ierr; 14377c927364SMatthew G. Knepley 14387c927364SMatthew G. Knepley PetscFunctionBegin; 143975a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1440c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 14417c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 14427c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 14437c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 14447c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 14457c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 14467c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 14479ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 14489ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1449e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 145097c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 14517c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 145297c42addSMatthew G. Knepley PetscObject obj; 145397c42addSMatthew G. Knepley PetscClassId id; 1454aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 14557c927364SMatthew G. Knepley 145697c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 145797c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 145897c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 145997c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 146097c42addSMatthew G. Knepley 14617c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 14627c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 14637c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 146497c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 146597c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 146697c42addSMatthew G. Knepley PetscDualSpace Q; 146797c42addSMatthew G. Knepley 146897c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 146997c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 147097c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 147197c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 147297c42addSMatthew G. Knepley } 14737c927364SMatthew G. Knepley fTotDim += fNb*Nc; 14747c927364SMatthew G. Knepley } 1475e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 14767c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 14777c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 14787c927364SMatthew G. Knepley PetscFE feC; 147997c42addSMatthew G. Knepley PetscFV fvC; 14807c927364SMatthew G. Knepley PetscDualSpace QF, QC; 14817c927364SMatthew G. Knepley PetscInt NcF, NcC, fpdim, cpdim; 14827c927364SMatthew G. Knepley 148397c42addSMatthew G. Knepley if (feRef[field]) { 1484e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 14857c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 14867c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 14877c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 14887c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 14897c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 14907c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 149197c42addSMatthew G. Knepley } else { 149297c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 149397c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 149497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 149597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 149697c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 149797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 149897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 149997c42addSMatthew G. Knepley } 150097c42addSMatthew 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); 15017c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 15027c927364SMatthew G. Knepley PetscQuadrature cfunc; 15037c927364SMatthew G. Knepley const PetscReal *cqpoints; 15047c927364SMatthew G. Knepley PetscInt NpC; 150597c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 15067c927364SMatthew G. Knepley 15077c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 15087c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr); 150997c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 15107c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 15117c927364SMatthew G. Knepley PetscQuadrature ffunc; 15127c927364SMatthew G. Knepley const PetscReal *fqpoints; 15137c927364SMatthew G. Knepley PetscReal sum = 0.0; 15147c927364SMatthew G. Knepley PetscInt NpF, comp; 15157c927364SMatthew G. Knepley 15167c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 15177c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr); 15187c927364SMatthew G. Knepley if (NpC != NpF) continue; 15197c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 15207c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 15217c927364SMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 15227c927364SMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp; 15237c927364SMatthew G. Knepley } 152497c42addSMatthew G. Knepley found = PETSC_TRUE; 15257c927364SMatthew G. Knepley break; 15267c927364SMatthew G. Knepley } 152797c42addSMatthew G. Knepley if (!found) { 152897c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 152997c42addSMatthew G. Knepley if (fvRef[field]) { 153097c42addSMatthew G. Knepley PetscInt comp; 153197c42addSMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 153297c42addSMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp; 153397c42addSMatthew G. Knepley } 153497c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 153597c42addSMatthew G. Knepley } 15367c927364SMatthew G. Knepley } 15377c927364SMatthew G. Knepley offsetC += cpdim*NcC; 15387c927364SMatthew G. Knepley offsetF += fpdim*NcF; 15397c927364SMatthew G. Knepley } 154097c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 154197c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 15427c927364SMatthew G. Knepley 15437c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 15447c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 15457c927364SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, NULL);CHKERRQ(ierr); 15467c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 15477c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 1548aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 1549aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 15507c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 15517c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 15527c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 15537c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 15547c927364SMatthew G. Knepley if (cellCIndices[d] < 0) continue; 15557c927364SMatthew 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]]); 15567c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 15577c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 15587c927364SMatthew G. Knepley } 15597c927364SMatthew G. Knepley } 15607c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 15617c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 15627c927364SMatthew G. Knepley 15637c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 15647c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 15657c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 15667c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 15677c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 15687c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 15697c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 157075a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1571cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1572cb1e1211SMatthew G Knepley } 1573