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" 890163fd50SMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx) 90026175e5SToby Isaac { 91026175e5SToby Isaac PetscInt *ctxInt = (PetscInt *) ctx; 92ad917190SMatthew G. Knepley PetscInt dim2 = ctxInt[0]; 93026175e5SToby Isaac PetscInt d = ctxInt[1]; 94026175e5SToby Isaac PetscInt i, j, k = dim > 2 ? d - dim : d; 95026175e5SToby Isaac 96ad917190SMatthew G. Knepley PetscFunctionBegin; 97ad917190SMatthew G. Knepley if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2); 98026175e5SToby Isaac for (i = 0; i < dim; i++) mode[i] = 0.; 99026175e5SToby Isaac if (d < dim) { 100026175e5SToby Isaac mode[d] = 1.; 101ad917190SMatthew G. Knepley } else { 102026175e5SToby Isaac for (i = 0; i < dim; i++) { 103026175e5SToby Isaac for (j = 0; j < dim; j++) { 104026175e5SToby Isaac mode[j] += epsilon(i, j, k)*X[i]; 105026175e5SToby Isaac } 106026175e5SToby Isaac } 107026175e5SToby Isaac } 108ad917190SMatthew G. Knepley PetscFunctionReturn(0); 109026175e5SToby Isaac } 110026175e5SToby Isaac 111026175e5SToby Isaac #undef __FUNCT__ 112cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexCreateRigidBody" 113cb1e1211SMatthew G Knepley /*@C 114026175e5SToby Isaac DMPlexCreateRigidBody - for the default global section, create rigid body modes from coordinates 115cb1e1211SMatthew G Knepley 116cb1e1211SMatthew G Knepley Collective on DM 117cb1e1211SMatthew G Knepley 118cb1e1211SMatthew G Knepley Input Arguments: 119026175e5SToby Isaac . dm - the DM 120cb1e1211SMatthew G Knepley 121cb1e1211SMatthew G Knepley Output Argument: 122cb1e1211SMatthew G Knepley . sp - the null space 123cb1e1211SMatthew G Knepley 124cb1e1211SMatthew G Knepley Note: This is necessary to take account of Dirichlet conditions on the displacements 125cb1e1211SMatthew G Knepley 126cb1e1211SMatthew G Knepley Level: advanced 127cb1e1211SMatthew G Knepley 128cb1e1211SMatthew G Knepley .seealso: MatNullSpaceCreate() 129cb1e1211SMatthew G Knepley @*/ 130026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp) 131cb1e1211SMatthew G Knepley { 132cb1e1211SMatthew G Knepley MPI_Comm comm; 133026175e5SToby Isaac Vec mode[6]; 134026175e5SToby Isaac PetscSection section, globalSection; 1359d8fbdeaSMatthew G. Knepley PetscInt dim, dimEmbed, n, m, d, i, j; 136cb1e1211SMatthew G Knepley PetscErrorCode ierr; 137cb1e1211SMatthew G Knepley 138cb1e1211SMatthew G Knepley PetscFunctionBegin; 139cb1e1211SMatthew G Knepley ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 140c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1419d8fbdeaSMatthew G. Knepley ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 142cb1e1211SMatthew G Knepley if (dim == 1) { 143cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr); 144cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 145cb1e1211SMatthew G Knepley } 146026175e5SToby Isaac ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 147026175e5SToby Isaac ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr); 148cb1e1211SMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr); 149cb1e1211SMatthew G Knepley m = (dim*(dim+1))/2; 150cb1e1211SMatthew G Knepley ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr); 151cb1e1211SMatthew G Knepley ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr); 152cb1e1211SMatthew G Knepley ierr = VecSetUp(mode[0]);CHKERRQ(ierr); 153cb1e1211SMatthew G Knepley for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);} 154026175e5SToby Isaac for (d = 0; d < m; d++) { 155330485fdSToby Isaac PetscInt ctx[2]; 156026175e5SToby Isaac void *voidctx = (void *) (&ctx[0]); 1570163fd50SMatthew G. Knepley PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody; 158cb1e1211SMatthew G Knepley 1599d8fbdeaSMatthew G. Knepley ctx[0] = dimEmbed; 160330485fdSToby Isaac ctx[1] = d; 1610163fd50SMatthew G. Knepley ierr = DMPlexProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr); 162cb1e1211SMatthew G Knepley } 163cb1e1211SMatthew G Knepley for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);} 164cb1e1211SMatthew G Knepley /* Orthonormalize system */ 165cb1e1211SMatthew G Knepley for (i = dim; i < m; ++i) { 166cb1e1211SMatthew G Knepley PetscScalar dots[6]; 167cb1e1211SMatthew G Knepley 168cb1e1211SMatthew G Knepley ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr); 169cb1e1211SMatthew G Knepley for (j = 0; j < i; ++j) dots[j] *= -1.0; 170cb1e1211SMatthew G Knepley ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr); 171cb1e1211SMatthew G Knepley ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr); 172cb1e1211SMatthew G Knepley } 173cb1e1211SMatthew G Knepley ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr); 174cb1e1211SMatthew G Knepley for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);} 175cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 176cb1e1211SMatthew G Knepley } 177cb1e1211SMatthew G Knepley 178cb1e1211SMatthew G Knepley #undef __FUNCT__ 179b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexSetMaxProjectionHeight" 180b29cfa1cSToby Isaac /*@ 181b29cfa1cSToby Isaac DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs 182b29cfa1cSToby Isaac are computed by associating the basis function with one of the mesh points in its transitively-closed support, and 183b29cfa1cSToby Isaac evaluating the dual space basis of that point. A basis function is associated with the point in its 184b29cfa1cSToby Isaac transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum 185b29cfa1cSToby Isaac projection height, which is set with this function. By default, the maximum projection height is zero, which means 186b29cfa1cSToby Isaac that only mesh cells are used to project basis functions. A height of one, for example, evaluates a cell-interior 187b29cfa1cSToby Isaac basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face. 188b29cfa1cSToby Isaac 189b29cfa1cSToby Isaac Input Parameters: 190b29cfa1cSToby Isaac + dm - the DMPlex object 191b29cfa1cSToby Isaac - height - the maximum projection height >= 0 192b29cfa1cSToby Isaac 193b29cfa1cSToby Isaac Level: advanced 194b29cfa1cSToby Isaac 195048b7b1eSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal() 196b29cfa1cSToby Isaac @*/ 197b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height) 198b29cfa1cSToby Isaac { 199b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 200b29cfa1cSToby Isaac 201b29cfa1cSToby Isaac PetscFunctionBegin; 202b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 203b29cfa1cSToby Isaac plex->maxProjectionHeight = height; 204b29cfa1cSToby Isaac PetscFunctionReturn(0); 205b29cfa1cSToby Isaac } 206b29cfa1cSToby Isaac 207b29cfa1cSToby Isaac #undef __FUNCT__ 208b29cfa1cSToby Isaac #define __FUNCT__ "DMPlexGetMaxProjectionHeight" 209b29cfa1cSToby Isaac /*@ 210b29cfa1cSToby Isaac DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in 211b29cfa1cSToby Isaac DMPlexProjectXXXLocal() functions. 212b29cfa1cSToby Isaac 213b29cfa1cSToby Isaac Input Parameters: 214b29cfa1cSToby Isaac . dm - the DMPlex object 215b29cfa1cSToby Isaac 216b29cfa1cSToby Isaac Output Parameters: 217b29cfa1cSToby Isaac . height - the maximum projection height 218b29cfa1cSToby Isaac 219b29cfa1cSToby Isaac Level: intermediate 220b29cfa1cSToby Isaac 221048b7b1eSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMPlexProjectFunctionLocal(), DMPlexProjectFunctionLabelLocal() 222b29cfa1cSToby Isaac @*/ 223b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height) 224b29cfa1cSToby Isaac { 225b29cfa1cSToby Isaac DM_Plex *plex = (DM_Plex *) dm->data; 226b29cfa1cSToby Isaac 227b29cfa1cSToby Isaac PetscFunctionBegin; 228b29cfa1cSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 229b29cfa1cSToby Isaac *height = plex->maxProjectionHeight; 230b29cfa1cSToby Isaac PetscFunctionReturn(0); 231b29cfa1cSToby Isaac } 232b29cfa1cSToby Isaac 233b29cfa1cSToby Isaac #undef __FUNCT__ 234a18a7fb9SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFunctionLabelLocal" 2350163fd50SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 236a18a7fb9SMatthew G. Knepley { 2377d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 238dda36da7SMatthew G. Knepley PetscInt *numComp; 239a18a7fb9SMatthew G. Knepley PetscSection section; 240a18a7fb9SMatthew G. Knepley PetscScalar *values; 241ad96f515SMatthew G. Knepley PetscBool *fieldActive; 242dda36da7SMatthew G. Knepley PetscInt numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h; 243a18a7fb9SMatthew G. Knepley PetscErrorCode ierr; 244a18a7fb9SMatthew G. Knepley 245a18a7fb9SMatthew G. Knepley PetscFunctionBegin; 2469ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 2479ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 2489ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 2499ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 250c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 2517a1a1bd4SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 252a18a7fb9SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 253a18a7fb9SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 254dda36da7SMatthew G. Knepley ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr); 2557d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 2567d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 2579ac3fadcSMatthew G. Knepley if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);} 2589ac3fadcSMatthew G. Knepley else {cellsp = sp;} 2597d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 2607d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 261fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 2627d1dd11eSToby Isaac if (pEnd <= pStart) continue; 2637d1dd11eSToby Isaac totDim = 0; 264a18a7fb9SMatthew G. Knepley for (f = 0; f < numFields; ++f) { 2659ac3fadcSMatthew G. Knepley PetscObject obj; 2669ac3fadcSMatthew G. Knepley PetscClassId id; 2679ac3fadcSMatthew G. Knepley 2689ac3fadcSMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 2699ac3fadcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 2709ac3fadcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 2719ac3fadcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 2729ac3fadcSMatthew G. Knepley 273dda36da7SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 2747d1dd11eSToby Isaac if (!h) { 275ee2838f6SToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 2767d1dd11eSToby Isaac sp[f] = cellsp[f]; 2779ac3fadcSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2789ac3fadcSMatthew G. Knepley } else { 2797d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 2807d1dd11eSToby Isaac if (!sp[f]) continue; 2817d1dd11eSToby Isaac } 2829ac3fadcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 2839ac3fadcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 2849ac3fadcSMatthew G. Knepley 285dda36da7SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 286302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 287e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 2889ac3fadcSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 289a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 290dda36da7SMatthew G. Knepley totDim += spDim*numComp[f]; 291a18a7fb9SMatthew G. Knepley } 2927d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 2937d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 294d374af2bSToby Isaac if (!totDim) continue; 295a18a7fb9SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 296ad96f515SMatthew G. Knepley ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 297ee2838f6SToby Isaac for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE; 298a18a7fb9SMatthew G. Knepley for (i = 0; i < numIds; ++i) { 299a18a7fb9SMatthew G. Knepley IS pointIS; 300a18a7fb9SMatthew G. Knepley const PetscInt *points; 301a18a7fb9SMatthew G. Knepley PetscInt n, p; 302a18a7fb9SMatthew G. Knepley 303a18a7fb9SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr); 304a18a7fb9SMatthew G. Knepley ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr); 305a18a7fb9SMatthew G. Knepley ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 306a18a7fb9SMatthew G. Knepley for (p = 0; p < n; ++p) { 307a18a7fb9SMatthew G. Knepley const PetscInt point = points[p]; 308e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 309a18a7fb9SMatthew G. Knepley 3107d1dd11eSToby Isaac if ((point < pStart) || (point >= pEnd)) continue; 311e1d0b1adSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 312ee2838f6SToby Isaac geom.dim = dim - h; 3137a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 314a18a7fb9SMatthew G. Knepley for (f = 0, v = 0; f < numFields; ++f) { 315a18a7fb9SMatthew G. Knepley void * const ctx = ctxs ? ctxs[f] : NULL; 316bf3434eeSMatthew G. Knepley 3177d1dd11eSToby Isaac if (!sp[f]) continue; 318a18a7fb9SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 319a18a7fb9SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 320a18a7fb9SMatthew G. Knepley if (funcs[f]) { 3210163fd50SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, time, &geom, numComp[f], funcs[f], ctx, &values[v]);CHKERRQ(ierr); 322a18a7fb9SMatthew G. Knepley } else { 323dda36da7SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 324a18a7fb9SMatthew G. Knepley } 325dda36da7SMatthew G. Knepley v += numComp[f]; 326a18a7fb9SMatthew G. Knepley } 327a18a7fb9SMatthew G. Knepley } 328ad96f515SMatthew G. Knepley ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr); 329a18a7fb9SMatthew G. Knepley } 330a18a7fb9SMatthew G. Knepley ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 331a18a7fb9SMatthew G. Knepley ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 332a18a7fb9SMatthew G. Knepley } 3337d1dd11eSToby Isaac if (h) { 3347d1dd11eSToby Isaac for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 3357d1dd11eSToby Isaac } 3367d1dd11eSToby Isaac } 337a18a7fb9SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 338ad96f515SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr); 3399ac3fadcSMatthew G. Knepley for (f = 0; f < numFields; ++f) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 340dda36da7SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 3417d1dd11eSToby Isaac if (maxHeight > 0) { 3427d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 3437d1dd11eSToby Isaac } 344a18a7fb9SMatthew G. Knepley PetscFunctionReturn(0); 345a18a7fb9SMatthew G. Knepley } 346a18a7fb9SMatthew G. Knepley 347a18a7fb9SMatthew G. Knepley #undef __FUNCT__ 348cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexProjectFunctionLocal" 3490163fd50SMatthew G. Knepley PetscErrorCode DMPlexProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX) 350cb1e1211SMatthew G Knepley { 3517d1dd11eSToby Isaac PetscDualSpace *sp, *cellsp; 35215496722SMatthew G. Knepley PetscInt *numComp; 35372f94c41SMatthew G. Knepley PetscSection section; 35472f94c41SMatthew G. Knepley PetscScalar *values; 355b793a5ffSMatthew G. Knepley PetscInt Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight; 356cb1e1211SMatthew G Knepley PetscErrorCode ierr; 357cb1e1211SMatthew G Knepley 358cb1e1211SMatthew G Knepley PetscFunctionBegin; 3599ac3fadcSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 3609ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 3619ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 3629ac3fadcSMatthew G. Knepley if (cEnd <= cStart) PetscFunctionReturn(0); 363cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 364b793a5ffSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 365b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &numComp);CHKERRQ(ierr); 3667d1dd11eSToby Isaac ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 367ee2838f6SToby Isaac ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr); 3687d1dd11eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 3697d1dd11eSToby Isaac if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);} 3707d1dd11eSToby Isaac if (maxHeight > 0) { 371b793a5ffSMatthew G. Knepley ierr = PetscMalloc1(Nf,&cellsp);CHKERRQ(ierr); 3727d1dd11eSToby Isaac } 373048b7b1eSToby Isaac else { 374048b7b1eSToby Isaac cellsp = sp; 375048b7b1eSToby Isaac } 3767d1dd11eSToby Isaac for (h = 0; h <= maxHeight; h++) { 3777d1dd11eSToby Isaac ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr); 378fa5a0009SMatthew G. Knepley if (!h) {pStart = cStart; pEnd = cEnd;} 379048b7b1eSToby Isaac if (pEnd <= pStart) continue; 3807d1dd11eSToby Isaac totDim = 0; 381b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 38215496722SMatthew G. Knepley PetscObject obj; 38315496722SMatthew G. Knepley PetscClassId id; 3840f2d7e86SMatthew G. Knepley 38515496722SMatthew G. Knepley ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr); 38615496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 38715496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 38815496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 38915496722SMatthew G. Knepley 39015496722SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr); 3917d1dd11eSToby Isaac if (!h) { 3927d1dd11eSToby Isaac ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr); 3937d1dd11eSToby Isaac sp[f] = cellsp[f]; 39415496722SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 3957d1dd11eSToby Isaac } 3967d1dd11eSToby Isaac else { 3977d1dd11eSToby Isaac ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr); 3987d1dd11eSToby Isaac if (!sp[f]) { 3997d1dd11eSToby Isaac continue; 4007d1dd11eSToby Isaac } 4017d1dd11eSToby Isaac } 40215496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 40315496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 40415496722SMatthew G. Knepley 405976fa17dSToby Isaac if (!h) { 406976fa17dSToby Isaac ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr); 407976fa17dSToby Isaac ierr = PetscFVGetDualSpace(fv, &cellsp[f]);CHKERRQ(ierr); 408976fa17dSToby Isaac ierr = PetscObjectReference((PetscObject) cellsp[f]);CHKERRQ(ierr); 409976fa17dSToby Isaac sp[f] = cellsp[f]; 410976fa17dSToby Isaac } 411976fa17dSToby Isaac else { 41277d9c660SToby Isaac sp[f] = NULL; 41377d9c660SToby Isaac continue; /* FV doesn't have fields that can be evaluated at faces, corners, etc. */ 41477d9c660SToby Isaac } 41515496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 41672f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 41715496722SMatthew G. Knepley totDim += spDim*numComp[f]; 418cb1e1211SMatthew G Knepley } 4197d1dd11eSToby Isaac ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr); 4207d1dd11eSToby Isaac if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim); 421d374af2bSToby Isaac if (!totDim) continue; 42272f94c41SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 4237d1dd11eSToby Isaac for (p = pStart; p < pEnd; ++p) { 424e1d0b1adSMatthew G. Knepley PetscFECellGeom geom; 425cb1e1211SMatthew G Knepley 42631383a9bSToby Isaac ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr); 427910c25dcSToby Isaac geom.dim = dim - h; 4287a1a1bd4SToby Isaac geom.dimEmbed = dimEmbed; 429b793a5ffSMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 430c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[f] : NULL; 4310f2d7e86SMatthew G. Knepley 4327d1dd11eSToby Isaac if (!sp[f]) continue; 43372f94c41SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 43472f94c41SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 435120386c5SMatthew G. Knepley if (funcs[f]) { 436*5d960a70SMatthew G. Knepley ierr = PetscDualSpaceApply(sp[f], d, time, &geom, numComp[f], funcs[f], ctx, &values[v]); 437*5d960a70SMatthew G. Knepley if (ierr) { 438*5d960a70SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 439*5d960a70SMatthew G. Knepley CHKERRQ(ierr); 440*5d960a70SMatthew G. Knepley } 441120386c5SMatthew G. Knepley } else { 44215496722SMatthew G. Knepley for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0; 443120386c5SMatthew G. Knepley } 44415496722SMatthew G. Knepley v += numComp[f]; 445cb1e1211SMatthew G Knepley } 446cb1e1211SMatthew G Knepley } 4477d1dd11eSToby Isaac ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr); 448cb1e1211SMatthew G Knepley } 44972f94c41SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 450ee2838f6SToby Isaac if (h || !maxHeight) { 451b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 4527d1dd11eSToby Isaac } 4537d1dd11eSToby Isaac } 45415496722SMatthew G. Knepley ierr = PetscFree2(sp, numComp);CHKERRQ(ierr); 4557d1dd11eSToby Isaac if (maxHeight > 0) { 456b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&cellsp[f]);CHKERRQ(ierr);} 4577d1dd11eSToby Isaac ierr = PetscFree(cellsp);CHKERRQ(ierr); 4587d1dd11eSToby Isaac } 459cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 460cb1e1211SMatthew G Knepley } 461cb1e1211SMatthew G Knepley 462cb1e1211SMatthew G Knepley #undef __FUNCT__ 4638040c1f3SToby Isaac #define __FUNCT__ "DMPlexProjectFunction" 4648040c1f3SToby Isaac /*@C 4658040c1f3SToby Isaac DMPlexProjectFunction - This projects the given function into the function space provided. 4668040c1f3SToby Isaac 4678040c1f3SToby Isaac Input Parameters: 4688040c1f3SToby Isaac + dm - The DM 4698040c1f3SToby Isaac . funcs - The coordinate functions to evaluate, one per field 4708040c1f3SToby Isaac . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 4718040c1f3SToby Isaac - mode - The insertion mode for values 4728040c1f3SToby Isaac 4738040c1f3SToby Isaac Output Parameter: 4748040c1f3SToby Isaac . X - vector 4758040c1f3SToby Isaac 476ad917190SMatthew G. Knepley Calling sequence of func: 477ad917190SMatthew G. Knepley $ func(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx); 478ad917190SMatthew G. Knepley 479ad917190SMatthew G. Knepley + dim - The spatial dimension 480ad917190SMatthew G. Knepley . x - The coordinates 481ad917190SMatthew G. Knepley . Nf - The number of fields 482ad917190SMatthew G. Knepley . u - The output field values 483ad917190SMatthew G. Knepley - ctx - optional user-defined function context 484ad917190SMatthew G. Knepley 4858040c1f3SToby Isaac Level: developer 4868040c1f3SToby Isaac 4878040c1f3SToby Isaac .seealso: DMPlexComputeL2Diff() 4888040c1f3SToby Isaac @*/ 4890163fd50SMatthew G. Knepley PetscErrorCode DMPlexProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X) 4908040c1f3SToby Isaac { 4918040c1f3SToby Isaac Vec localX; 4928040c1f3SToby Isaac PetscErrorCode ierr; 4938040c1f3SToby Isaac 4948040c1f3SToby Isaac PetscFunctionBegin; 4958040c1f3SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4968040c1f3SToby Isaac ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 4970163fd50SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr); 4988040c1f3SToby Isaac ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr); 4998040c1f3SToby Isaac ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr); 5008040c1f3SToby Isaac ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 501cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 502cb1e1211SMatthew G Knepley } 503cb1e1211SMatthew G Knepley 504cb1e1211SMatthew G Knepley #undef __FUNCT__ 5050f2d7e86SMatthew G. Knepley #define __FUNCT__ "DMPlexProjectFieldLocal" 5063bc3b0a0SMatthew 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) 5070f2d7e86SMatthew G. Knepley { 5080f2d7e86SMatthew G. Knepley DM dmAux; 509d5396abcSMatthew G. Knepley PetscDS prob, probAux = NULL; 5100f2d7e86SMatthew G. Knepley Vec A; 511d5396abcSMatthew G. Knepley PetscSection section, sectionAux = NULL; 512b793a5ffSMatthew G. Knepley PetscDualSpace *sp; 513b793a5ffSMatthew G. Knepley PetscInt *Ncf; 514326413afSMatthew G. Knepley PetscScalar *values, *u, *u_x, *a, *a_x; 515d5396abcSMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 5169ac3fadcSMatthew G. Knepley PetscInt Nf, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight; 5170f2d7e86SMatthew G. Knepley PetscErrorCode ierr; 5180f2d7e86SMatthew G. Knepley 5190f2d7e86SMatthew G. Knepley PetscFunctionBegin; 520048b7b1eSToby Isaac ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr); 521048b7b1eSToby Isaac if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");} 5222764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 523c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 5240f2d7e86SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 5250f2d7e86SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 526b793a5ffSMatthew G. Knepley ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr); 5270f2d7e86SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 5282764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 5292764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr); 5302764a2aaSMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 5310f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 5320f2d7e86SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 5330f2d7e86SMatthew G. Knepley if (dmAux) { 5342764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 535326413afSMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 5362764a2aaSMatthew G. Knepley ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr); 5370f2d7e86SMatthew G. Knepley } 538d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 5390f2d7e86SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr); 5400f2d7e86SMatthew 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); 5410f2d7e86SMatthew G. Knepley ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 5420f2d7e86SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 5439ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 5449ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 5450f2d7e86SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 546326413afSMatthew G. Knepley PetscScalar *coefficients = NULL, *coefficientsAux = NULL; 547326413afSMatthew G. Knepley 5488e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 549326413afSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 550326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5510f2d7e86SMatthew G. Knepley for (f = 0, v = 0; f < Nf; ++f) { 552d5396abcSMatthew G. Knepley PetscObject obj; 553d5396abcSMatthew G. Knepley PetscClassId id; 5543113607cSMatthew G. Knepley 555d5396abcSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 556d5396abcSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 557d5396abcSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 558d5396abcSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 559d5396abcSMatthew G. Knepley 560b793a5ffSMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr); 561b793a5ffSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 562b793a5ffSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr); 563d5396abcSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 564d5396abcSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 565d5396abcSMatthew G. Knepley 566b793a5ffSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr); 567302440fdSBarry Smith ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr); 568e3e48f69SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) sp[f]);CHKERRQ(ierr); 569d5396abcSMatthew G. Knepley } 570b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr); 5710f2d7e86SMatthew G. Knepley for (d = 0; d < spDim; ++d) { 5720f2d7e86SMatthew G. Knepley PetscQuadrature quad; 5730f2d7e86SMatthew G. Knepley const PetscReal *points, *weights; 5740f2d7e86SMatthew G. Knepley PetscInt numPoints, q; 5750f2d7e86SMatthew G. Knepley 5760f2d7e86SMatthew G. Knepley if (funcs[f]) { 577b793a5ffSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr); 5780f2d7e86SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr); 5790f2d7e86SMatthew G. Knepley for (q = 0; q < numPoints; ++q) { 5800f2d7e86SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x); 581326413afSMatthew G. Knepley ierr = EvaluateFieldJets(prob, PETSC_FALSE, q, invJ, coefficients, NULL, u, u_x, NULL);CHKERRQ(ierr); 582326413afSMatthew G. Knepley ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr); 5833bc3b0a0SMatthew G. Knepley (*funcs[f])(u, NULL, u_x, a, NULL, a_x, x, &values[v]); 5840f2d7e86SMatthew G. Knepley } 5850f2d7e86SMatthew G. Knepley } else { 586b793a5ffSMatthew G. Knepley for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0; 5870f2d7e86SMatthew G. Knepley } 588b793a5ffSMatthew G. Knepley v += Ncf[f]; 5890f2d7e86SMatthew G. Knepley } 5900f2d7e86SMatthew G. Knepley } 591326413afSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr); 592326413afSMatthew G. Knepley if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);} 5930f2d7e86SMatthew G. Knepley ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr); 5940f2d7e86SMatthew G. Knepley } 5950f2d7e86SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr); 5960f2d7e86SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 597b793a5ffSMatthew G. Knepley for (f = 0; f < Nf; f++) {ierr = PetscDualSpaceDestroy(&sp[f]);CHKERRQ(ierr);} 598b793a5ffSMatthew G. Knepley ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr); 5990f2d7e86SMatthew G. Knepley PetscFunctionReturn(0); 6000f2d7e86SMatthew G. Knepley } 6010f2d7e86SMatthew G. Knepley 6020f2d7e86SMatthew G. Knepley #undef __FUNCT__ 603d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FEM_Internal" 6040163fd50SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FEM_Internal(DM dm, PetscReal time, PetscInt field, DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX) 60555f2e967SMatthew G. Knepley { 6060163fd50SMatthew G. Knepley PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 60755f2e967SMatthew G. Knepley void **ctxs; 608d7ddef95SMatthew G. Knepley PetscInt numFields; 609d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 610d7ddef95SMatthew G. Knepley 611d7ddef95SMatthew G. Knepley PetscFunctionBegin; 612d7ddef95SMatthew G. Knepley ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr); 613d7ddef95SMatthew G. Knepley ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr); 614d7ddef95SMatthew G. Knepley funcs[field] = func; 615d7ddef95SMatthew G. Knepley ctxs[field] = ctx; 6160163fd50SMatthew G. Knepley ierr = DMPlexProjectFunctionLabelLocal(dm, time, label, numids, ids, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr); 617d7ddef95SMatthew G. Knepley ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr); 618d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 619d7ddef95SMatthew G. Knepley } 620d7ddef95SMatthew G. Knepley 621d7ddef95SMatthew G. Knepley #undef __FUNCT__ 622d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues_FVM_Internal" 6230e0f25f2SMatthew G. Knepley /* This ignores numcomps/comps */ 624d7ddef95SMatthew G. Knepley static PetscErrorCode DMPlexInsertBoundaryValues_FVM_Internal(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, 625d7ddef95SMatthew 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) 626d7ddef95SMatthew G. Knepley { 62761f58d28SMatthew G. Knepley PetscDS prob; 628da97024aSMatthew G. Knepley PetscSF sf; 629d7ddef95SMatthew G. Knepley DM dmFace, dmCell, dmGrad; 630d7ddef95SMatthew G. Knepley const PetscScalar *facegeom, *cellgeom, *grad; 631da97024aSMatthew G. Knepley const PetscInt *leaves; 632d7ddef95SMatthew G. Knepley PetscScalar *x, *fx; 633520b3818SMatthew G. Knepley PetscInt dim, nleaves, loc, fStart, fEnd, pdim, i; 634d7ddef95SMatthew G. Knepley PetscErrorCode ierr; 635d7ddef95SMatthew G. Knepley 636d7ddef95SMatthew G. Knepley PetscFunctionBegin; 637da97024aSMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 638da97024aSMatthew G. Knepley ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr); 639da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 640d7ddef95SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 641d7ddef95SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 64261f58d28SMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 643d7ddef95SMatthew G. Knepley ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr); 644d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 645d7ddef95SMatthew G. Knepley ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr); 646d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 647d7ddef95SMatthew G. Knepley if (Grad) { 648c0a6632aSMatthew G. Knepley PetscFV fv; 649c0a6632aSMatthew G. Knepley 650c0a6632aSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr); 651d7ddef95SMatthew G. Knepley ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr); 652d7ddef95SMatthew G. Knepley ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr); 653d7ddef95SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr); 654d7ddef95SMatthew G. Knepley ierr = DMGetWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 655d7ddef95SMatthew G. Knepley } 656d7ddef95SMatthew G. Knepley ierr = VecGetArray(locX, &x);CHKERRQ(ierr); 657d7ddef95SMatthew G. Knepley for (i = 0; i < numids; ++i) { 658d7ddef95SMatthew G. Knepley IS faceIS; 659d7ddef95SMatthew G. Knepley const PetscInt *faces; 660d7ddef95SMatthew G. Knepley PetscInt numFaces, f; 661d7ddef95SMatthew G. Knepley 662d7ddef95SMatthew G. Knepley ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr); 663d7ddef95SMatthew G. Knepley if (!faceIS) continue; /* No points with that id on this process */ 664d7ddef95SMatthew G. Knepley ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 665d7ddef95SMatthew G. Knepley ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 666d7ddef95SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 667d7ddef95SMatthew G. Knepley const PetscInt face = faces[f], *cells; 668d7ddef95SMatthew G. Knepley const PetscFVFaceGeom *fg; 669d7ddef95SMatthew G. Knepley 670d7ddef95SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */ 671da97024aSMatthew G. Knepley ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr); 672da97024aSMatthew G. Knepley if (loc >= 0) continue; 673d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr); 674d7ddef95SMatthew G. Knepley ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr); 675d7ddef95SMatthew G. Knepley if (Grad) { 676d7ddef95SMatthew G. Knepley const PetscFVCellGeom *cg; 677d7ddef95SMatthew G. Knepley const PetscScalar *cx, *cgrad; 678d7ddef95SMatthew G. Knepley PetscScalar *xG; 679d7ddef95SMatthew G. Knepley PetscReal dx[3]; 680d7ddef95SMatthew G. Knepley PetscInt d; 681d7ddef95SMatthew G. Knepley 682d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr); 683d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr); 684d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr); 685520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 686d7ddef95SMatthew G. Knepley DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx); 687d7ddef95SMatthew G. Knepley for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx); 688520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr); 689d7ddef95SMatthew G. Knepley } else { 690d7ddef95SMatthew G. Knepley const PetscScalar *xI; 691d7ddef95SMatthew G. Knepley PetscScalar *xG; 692d7ddef95SMatthew G. Knepley 693d7ddef95SMatthew G. Knepley ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr); 694520b3818SMatthew G. Knepley ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr); 695520b3818SMatthew G. Knepley ierr = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);CHKERRQ(ierr); 696d7ddef95SMatthew G. Knepley } 697d7ddef95SMatthew G. Knepley } 698d7ddef95SMatthew G. Knepley ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 699d7ddef95SMatthew G. Knepley ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 700d7ddef95SMatthew G. Knepley } 701d7ddef95SMatthew G. Knepley ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr); 702d7ddef95SMatthew G. Knepley if (Grad) { 703d7ddef95SMatthew G. Knepley ierr = DMRestoreWorkArray(dm, pdim, PETSC_SCALAR, &fx);CHKERRQ(ierr); 704d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr); 705d7ddef95SMatthew G. Knepley } 706d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr); 707d7ddef95SMatthew G. Knepley ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr); 708d7ddef95SMatthew G. Knepley PetscFunctionReturn(0); 709d7ddef95SMatthew G. Knepley } 710d7ddef95SMatthew G. Knepley 711d7ddef95SMatthew G. Knepley #undef __FUNCT__ 712d7ddef95SMatthew G. Knepley #define __FUNCT__ "DMPlexInsertBoundaryValues" 713d7ddef95SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM) 714d7ddef95SMatthew G. Knepley { 715d7ddef95SMatthew G. Knepley PetscInt numBd, b; 71655f2e967SMatthew G. Knepley PetscErrorCode ierr; 71755f2e967SMatthew G. Knepley 71855f2e967SMatthew G. Knepley PetscFunctionBegin; 71955f2e967SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 720d7ddef95SMatthew G. Knepley PetscValidHeaderSpecific(locX, VEC_CLASSID, 2); 721d7ddef95SMatthew G. Knepley if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);} 722d7ddef95SMatthew G. Knepley if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);} 723d7ddef95SMatthew G. Knepley if (gradFVM) {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);} 72455f2e967SMatthew G. Knepley ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr); 72555f2e967SMatthew G. Knepley for (b = 0; b < numBd; ++b) { 72655f2e967SMatthew G. Knepley PetscBool isEssential; 727d7ddef95SMatthew G. Knepley const char *labelname; 728d7ddef95SMatthew G. Knepley DMLabel label; 729d7ddef95SMatthew G. Knepley PetscInt field; 730d7ddef95SMatthew G. Knepley PetscObject obj; 731d7ddef95SMatthew G. Knepley PetscClassId id; 73255f2e967SMatthew G. Knepley void (*func)(); 733d7ddef95SMatthew G. Knepley PetscInt numids; 734d7ddef95SMatthew G. Knepley const PetscInt *ids; 73555f2e967SMatthew G. Knepley void *ctx; 73655f2e967SMatthew G. Knepley 7370e0f25f2SMatthew G. Knepley ierr = DMPlexGetBoundary(dm, b, &isEssential, NULL, &labelname, &field, NULL, NULL, &func, &numids, &ids, &ctx);CHKERRQ(ierr); 738d7ddef95SMatthew G. Knepley if (!isEssential) continue; 739c58f1c22SToby Isaac ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr); 740d7ddef95SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 741d7ddef95SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 742d7ddef95SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 7430163fd50SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FEM_Internal(dm, time, field, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr); 744d7ddef95SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 74543ea7facSMatthew G. Knepley if (!faceGeomFVM) continue; 746d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues_FVM_Internal(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, 747d7ddef95SMatthew G. Knepley field, label, numids, ids, (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr); 748d7ddef95SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 74955f2e967SMatthew G. Knepley } 75055f2e967SMatthew G. Knepley PetscFunctionReturn(0); 75155f2e967SMatthew G. Knepley } 75255f2e967SMatthew G. Knepley 753cb1e1211SMatthew G Knepley #undef __FUNCT__ 754cb1e1211SMatthew G Knepley #define __FUNCT__ "DMPlexComputeL2Diff" 755cb1e1211SMatthew G Knepley /*@C 756cb1e1211SMatthew G Knepley DMPlexComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 757cb1e1211SMatthew G Knepley 758cb1e1211SMatthew G Knepley Input Parameters: 759cb1e1211SMatthew G Knepley + dm - The DM 7600163fd50SMatthew G. Knepley . time - The time 761cb1e1211SMatthew G Knepley . funcs - The functions to evaluate for each field component 76251259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 763cb1e1211SMatthew G Knepley - X - The coefficient vector u_h 764cb1e1211SMatthew G Knepley 765cb1e1211SMatthew G Knepley Output Parameter: 766cb1e1211SMatthew G Knepley . diff - The diff ||u - u_h||_2 767cb1e1211SMatthew G Knepley 768cb1e1211SMatthew G Knepley Level: developer 769cb1e1211SMatthew G Knepley 77015496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 771878cb397SSatish Balay @*/ 7720163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 773cb1e1211SMatthew G Knepley { 774cb1e1211SMatthew G Knepley const PetscInt debug = 0; 775cb1e1211SMatthew G Knepley PetscSection section; 776c5bbbd5bSMatthew G. Knepley PetscQuadrature quad; 777cb1e1211SMatthew G Knepley Vec localX; 77815496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 779cb1e1211SMatthew G Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 780cb1e1211SMatthew G Knepley PetscReal localDiff = 0.0; 78115496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 7829ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 783cb1e1211SMatthew G Knepley PetscErrorCode ierr; 784cb1e1211SMatthew G Knepley 785cb1e1211SMatthew G Knepley PetscFunctionBegin; 786c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 787cb1e1211SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 788cb1e1211SMatthew G Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 789cb1e1211SMatthew G Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 790cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 791cb1e1211SMatthew G Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 792cb1e1211SMatthew G Knepley for (field = 0; field < numFields; ++field) { 79315496722SMatthew G. Knepley PetscObject obj; 79415496722SMatthew G. Knepley PetscClassId id; 795c5bbbd5bSMatthew G. Knepley PetscInt Nc; 796c5bbbd5bSMatthew G. Knepley 79715496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 79815496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 79915496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 80015496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 80115496722SMatthew G. Knepley 8020f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 8030f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 80415496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 80515496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 80615496722SMatthew G. Knepley 80715496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 80815496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 80915496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 810c5bbbd5bSMatthew G. Knepley numComponents += Nc; 811cb1e1211SMatthew G Knepley } 81215496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 8130163fd50SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, 0.0, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 81415496722SMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 815cb1e1211SMatthew G Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 8169ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 8179ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 818cb1e1211SMatthew G Knepley for (c = cStart; c < cEnd; ++c) { 819a1e44745SMatthew G. Knepley PetscScalar *x = NULL; 820cb1e1211SMatthew G Knepley PetscReal elemDiff = 0.0; 821cb1e1211SMatthew G Knepley 8228e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 823cb1e1211SMatthew G Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 824cb1e1211SMatthew G Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 825cb1e1211SMatthew G Knepley 82615496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 82715496722SMatthew G. Knepley PetscObject obj; 82815496722SMatthew G. Knepley PetscClassId id; 829c110b1eeSGeoffrey Irving void * const ctx = ctxs ? ctxs[field] : NULL; 83015496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 831cb1e1211SMatthew G Knepley 83215496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 83315496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 83415496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 83515496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 83615496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 837cb1e1211SMatthew G Knepley if (debug) { 838cb1e1211SMatthew G Knepley char title[1024]; 839cb1e1211SMatthew G Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 84015496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 841cb1e1211SMatthew G Knepley } 842cb1e1211SMatthew G Knepley for (q = 0; q < numQuadPoints; ++q) { 84315496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 8440163fd50SMatthew G. Knepley ierr = (*funcs[field])(dim, time, coords, Nc, funcVal, ctx);CHKERRQ(ierr); 84515496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 84615496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 84715496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 84815496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 84915496722SMatthew 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);} 85015496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 851cb1e1211SMatthew G Knepley } 852cb1e1211SMatthew G Knepley } 85315496722SMatthew G. Knepley fieldOffset += Nb*Nc; 854cb1e1211SMatthew G Knepley } 855cb1e1211SMatthew G Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 856cb1e1211SMatthew G Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 857cb1e1211SMatthew G Knepley localDiff += elemDiff; 858cb1e1211SMatthew G Knepley } 85915496722SMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 860cb1e1211SMatthew G Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 861b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 862cb1e1211SMatthew G Knepley *diff = PetscSqrtReal(*diff); 863cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 864cb1e1211SMatthew G Knepley } 865cb1e1211SMatthew G Knepley 866cb1e1211SMatthew G Knepley #undef __FUNCT__ 86740e14135SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2GradientDiff" 86840e14135SMatthew G. Knepley /*@C 86940e14135SMatthew 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. 87040e14135SMatthew G. Knepley 87140e14135SMatthew G. Knepley Input Parameters: 87240e14135SMatthew G. Knepley + dm - The DM 8730163fd50SMatthew G. Knepley , time - The time 87440e14135SMatthew G. Knepley . funcs - The gradient functions to evaluate for each field component 87551259fa3SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 87640e14135SMatthew G. Knepley . X - The coefficient vector u_h 87740e14135SMatthew G. Knepley - n - The vector to project along 87840e14135SMatthew G. Knepley 87940e14135SMatthew G. Knepley Output Parameter: 88040e14135SMatthew G. Knepley . diff - The diff ||(grad u - grad u_h) . n||_2 88140e14135SMatthew G. Knepley 88240e14135SMatthew G. Knepley Level: developer 88340e14135SMatthew G. Knepley 88440e14135SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff() 88540e14135SMatthew G. Knepley @*/ 8860163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 887cb1e1211SMatthew G Knepley { 88840e14135SMatthew G. Knepley const PetscInt debug = 0; 889cb1e1211SMatthew G Knepley PetscSection section; 89040e14135SMatthew G. Knepley PetscQuadrature quad; 89140e14135SMatthew G. Knepley Vec localX; 89240e14135SMatthew G. Knepley PetscScalar *funcVal, *interpolantVec; 89340e14135SMatthew G. Knepley PetscReal *coords, *realSpaceDer, *v0, *J, *invJ, detJ; 89440e14135SMatthew G. Knepley PetscReal localDiff = 0.0; 8959ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset, comp; 896cb1e1211SMatthew G Knepley PetscErrorCode ierr; 897cb1e1211SMatthew G Knepley 898cb1e1211SMatthew G Knepley PetscFunctionBegin; 899c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 90040e14135SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 90140e14135SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 90240e14135SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 90340e14135SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 90440e14135SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 905652b88e8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 9060f2d7e86SMatthew G. Knepley PetscFE fe; 90740e14135SMatthew G. Knepley PetscInt Nc; 908652b88e8SMatthew G. Knepley 9090f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 9100f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 9110f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 91240e14135SMatthew G. Knepley numComponents += Nc; 913652b88e8SMatthew G. Knepley } 91440e14135SMatthew G. Knepley /* ierr = DMPlexProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */ 91540e14135SMatthew G. Knepley ierr = PetscMalloc7(numComponents,&funcVal,dim,&coords,dim,&realSpaceDer,dim,&v0,dim*dim,&J,dim*dim,&invJ,dim,&interpolantVec);CHKERRQ(ierr); 91640e14135SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 9179ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 9189ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 91940e14135SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 92040e14135SMatthew G. Knepley PetscScalar *x = NULL; 92140e14135SMatthew G. Knepley PetscReal elemDiff = 0.0; 922652b88e8SMatthew G. Knepley 9238e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 92440e14135SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 92540e14135SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 92640e14135SMatthew G. Knepley 92740e14135SMatthew G. Knepley for (field = 0, comp = 0, fieldOffset = 0; field < numFields; ++field) { 9280f2d7e86SMatthew G. Knepley PetscFE fe; 92951259fa3SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 93021454ff5SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 93140e14135SMatthew G. Knepley PetscReal *basisDer; 93221454ff5SMatthew G. Knepley PetscInt numQuadPoints, Nb, Ncomp, q, d, e, fc, f, g; 93340e14135SMatthew G. Knepley 9340f2d7e86SMatthew G. Knepley ierr = DMGetField(dm, field, (PetscObject *) &fe);CHKERRQ(ierr); 93521454ff5SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 9360f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 9370f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Ncomp);CHKERRQ(ierr); 9380f2d7e86SMatthew G. Knepley ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr); 93940e14135SMatthew G. Knepley if (debug) { 94040e14135SMatthew G. Knepley char title[1024]; 94140e14135SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 94240e14135SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Ncomp, &x[fieldOffset]);CHKERRQ(ierr); 943652b88e8SMatthew G. Knepley } 94440e14135SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 94540e14135SMatthew G. Knepley for (d = 0; d < dim; d++) { 94640e14135SMatthew G. Knepley coords[d] = v0[d]; 94740e14135SMatthew G. Knepley for (e = 0; e < dim; e++) { 94840e14135SMatthew G. Knepley coords[d] += J[d*dim+e]*(quadPoints[q*dim+e] + 1.0); 949652b88e8SMatthew G. Knepley } 95040e14135SMatthew G. Knepley } 9510163fd50SMatthew G. Knepley ierr = (*funcs[field])(dim, time, coords, n, numFields, funcVal, ctx);CHKERRQ(ierr); 95240e14135SMatthew G. Knepley for (fc = 0; fc < Ncomp; ++fc) { 95340e14135SMatthew G. Knepley PetscScalar interpolant = 0.0; 95440e14135SMatthew G. Knepley 95540e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolantVec[d] = 0.0; 95640e14135SMatthew G. Knepley for (f = 0; f < Nb; ++f) { 95740e14135SMatthew G. Knepley const PetscInt fidx = f*Ncomp+fc; 95840e14135SMatthew G. Knepley 95940e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) { 96040e14135SMatthew G. Knepley realSpaceDer[d] = 0.0; 96140e14135SMatthew G. Knepley for (g = 0; g < dim; ++g) { 96240e14135SMatthew G. Knepley realSpaceDer[d] += invJ[g*dim+d]*basisDer[(q*Nb*Ncomp+fidx)*dim+g]; 96340e14135SMatthew G. Knepley } 96440e14135SMatthew G. Knepley interpolantVec[d] += x[fieldOffset+fidx]*realSpaceDer[d]; 96540e14135SMatthew G. Knepley } 96640e14135SMatthew G. Knepley } 96740e14135SMatthew G. Knepley for (d = 0; d < dim; ++d) interpolant += interpolantVec[d]*n[d]; 96840e14135SMatthew 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);} 96940e14135SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant - funcVal[fc]))*quadWeights[q]*detJ; 97040e14135SMatthew G. Knepley } 97140e14135SMatthew G. Knepley } 97240e14135SMatthew G. Knepley comp += Ncomp; 97340e14135SMatthew G. Knepley fieldOffset += Nb*Ncomp; 97440e14135SMatthew G. Knepley } 97540e14135SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 97640e14135SMatthew G. Knepley if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, " elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);} 97740e14135SMatthew G. Knepley localDiff += elemDiff; 97840e14135SMatthew G. Knepley } 97940e14135SMatthew G. Knepley ierr = PetscFree7(funcVal,coords,realSpaceDer,v0,J,invJ,interpolantVec);CHKERRQ(ierr); 98040e14135SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 981b2566f29SBarry Smith ierr = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 98240e14135SMatthew G. Knepley *diff = PetscSqrtReal(*diff); 983cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 984cb1e1211SMatthew G Knepley } 985cb1e1211SMatthew G Knepley 986a0845e3aSMatthew G. Knepley #undef __FUNCT__ 98773d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2FieldDiff" 98815496722SMatthew G. Knepley /*@C 98915496722SMatthew 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. 99015496722SMatthew G. Knepley 99115496722SMatthew G. Knepley Input Parameters: 99215496722SMatthew G. Knepley + dm - The DM 9930163fd50SMatthew G. Knepley . time - The time 99415496722SMatthew G. Knepley . funcs - The functions to evaluate for each field component 99515496722SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 99615496722SMatthew G. Knepley - X - The coefficient vector u_h 99715496722SMatthew G. Knepley 99815496722SMatthew G. Knepley Output Parameter: 99915496722SMatthew G. Knepley . diff - The array of differences, ||u^f - u^f_h||_2 100015496722SMatthew G. Knepley 100115496722SMatthew G. Knepley Level: developer 100215496722SMatthew G. Knepley 100315496722SMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2GradientDiff() 100415496722SMatthew G. Knepley @*/ 10050163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 100673d901b8SMatthew G. Knepley { 100773d901b8SMatthew G. Knepley const PetscInt debug = 0; 100873d901b8SMatthew G. Knepley PetscSection section; 100973d901b8SMatthew G. Knepley PetscQuadrature quad; 101073d901b8SMatthew G. Knepley Vec localX; 101115496722SMatthew G. Knepley PetscScalar *funcVal, *interpolant; 101273d901b8SMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 101373d901b8SMatthew G. Knepley PetscReal *localDiff; 101415496722SMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 10159ac3fadcSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 101673d901b8SMatthew G. Knepley PetscErrorCode ierr; 101773d901b8SMatthew G. Knepley 101873d901b8SMatthew G. Knepley PetscFunctionBegin; 1019c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 102073d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 102173d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 102273d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 102373d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 102473d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 102573d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) { 102615496722SMatthew G. Knepley PetscObject obj; 102715496722SMatthew G. Knepley PetscClassId id; 102873d901b8SMatthew G. Knepley PetscInt Nc; 102973d901b8SMatthew G. Knepley 103015496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 103115496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 103215496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 103315496722SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 103415496722SMatthew G. Knepley 10350f2d7e86SMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 10360f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 103715496722SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 103815496722SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 103915496722SMatthew G. Knepley 104015496722SMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 104115496722SMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 104215496722SMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 104373d901b8SMatthew G. Knepley numComponents += Nc; 104473d901b8SMatthew G. Knepley } 104515496722SMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 10460163fd50SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 104715496722SMatthew G. Knepley ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 104873d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 10499ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 10509ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 105173d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 105273d901b8SMatthew G. Knepley PetscScalar *x = NULL; 105373d901b8SMatthew G. Knepley 10548e0841e0SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 105573d901b8SMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 105673d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 105773d901b8SMatthew G. Knepley 105815496722SMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 105915496722SMatthew G. Knepley PetscObject obj; 106015496722SMatthew G. Knepley PetscClassId id; 106173d901b8SMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 106215496722SMatthew G. Knepley PetscInt Nb, Nc, q, fc; 106373d901b8SMatthew G. Knepley 106415496722SMatthew G. Knepley PetscReal elemDiff = 0.0; 106515496722SMatthew G. Knepley 106615496722SMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 106715496722SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 106815496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 106915496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 107015496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 107173d901b8SMatthew G. Knepley if (debug) { 107273d901b8SMatthew G. Knepley char title[1024]; 107373d901b8SMatthew G. Knepley ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr); 107415496722SMatthew G. Knepley ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr); 107573d901b8SMatthew G. Knepley } 107673d901b8SMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 107715496722SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 10780163fd50SMatthew G. Knepley ierr = (*funcs[field])(dim, time, coords, numFields, funcVal, ctx);CHKERRQ(ierr); 107915496722SMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 108015496722SMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 108115496722SMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 108215496722SMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 108315496722SMatthew 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);} 108415496722SMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 108573d901b8SMatthew G. Knepley } 108673d901b8SMatthew G. Knepley } 108715496722SMatthew G. Knepley fieldOffset += Nb*Nc; 108873d901b8SMatthew G. Knepley localDiff[field] += elemDiff; 108973d901b8SMatthew G. Knepley } 109073d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 109173d901b8SMatthew G. Knepley } 109273d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1093b2566f29SBarry Smith ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr); 109473d901b8SMatthew G. Knepley for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]); 109515496722SMatthew G. Knepley ierr = PetscFree7(localDiff,funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 109673d901b8SMatthew G. Knepley PetscFunctionReturn(0); 109773d901b8SMatthew G. Knepley } 109873d901b8SMatthew G. Knepley 109973d901b8SMatthew G. Knepley #undef __FUNCT__ 1100e729f68cSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeL2DiffVec" 1101e729f68cSMatthew G. Knepley /*@C 1102e729f68cSMatthew G. Knepley DMPlexComputeL2DiffVec - This function computes the cellwise L_2 difference between a function u and an FEM interpolant solution u_h, and stores it in a Vec. 1103e729f68cSMatthew G. Knepley 1104e729f68cSMatthew G. Knepley Input Parameters: 1105e729f68cSMatthew G. Knepley + dm - The DM 11060163fd50SMatthew G. Knepley . time - The time 1107e729f68cSMatthew G. Knepley . funcs - The functions to evaluate for each field component 1108e729f68cSMatthew G. Knepley . ctxs - Optional array of contexts to pass to each function, or NULL. 1109e729f68cSMatthew G. Knepley - X - The coefficient vector u_h 1110e729f68cSMatthew G. Knepley 1111e729f68cSMatthew G. Knepley Output Parameter: 1112e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell 1113e729f68cSMatthew G. Knepley 1114e729f68cSMatthew G. Knepley Level: developer 1115e729f68cSMatthew G. Knepley 1116e729f68cSMatthew G. Knepley .seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMPlexComputeL2GradientDiff() 1117e729f68cSMatthew G. Knepley @*/ 11180163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D) 1119e729f68cSMatthew G. Knepley { 1120e729f68cSMatthew G. Knepley PetscSection section; 1121e729f68cSMatthew G. Knepley PetscQuadrature quad; 1122e729f68cSMatthew G. Knepley Vec localX; 1123e729f68cSMatthew G. Knepley PetscScalar *funcVal, *interpolant; 1124e729f68cSMatthew G. Knepley PetscReal *coords, *v0, *J, *invJ, detJ; 1125e729f68cSMatthew G. Knepley const PetscReal *quadPoints, *quadWeights; 1126e729f68cSMatthew G. Knepley PetscInt dim, numFields, numComponents = 0, numQuadPoints, cStart, cEnd, cEndInterior, c, field, fieldOffset; 1127e729f68cSMatthew G. Knepley PetscErrorCode ierr; 1128e729f68cSMatthew G. Knepley 1129e729f68cSMatthew G. Knepley PetscFunctionBegin; 1130e729f68cSMatthew G. Knepley ierr = VecSet(D, 0.0);CHKERRQ(ierr); 1131e729f68cSMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 1132e729f68cSMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1133e729f68cSMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr); 1134e729f68cSMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 1135e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1136e729f68cSMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1137e729f68cSMatthew G. Knepley for (field = 0; field < numFields; ++field) { 1138e729f68cSMatthew G. Knepley PetscObject obj; 1139e729f68cSMatthew G. Knepley PetscClassId id; 1140e729f68cSMatthew G. Knepley PetscInt Nc; 1141e729f68cSMatthew G. Knepley 1142e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1143e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1144e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1145e729f68cSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1146e729f68cSMatthew G. Knepley 1147e729f68cSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr); 1148e729f68cSMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 1149e729f68cSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1150e729f68cSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 1151e729f68cSMatthew G. Knepley 1152e729f68cSMatthew G. Knepley ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr); 1153e729f68cSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 1154e729f68cSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1155e729f68cSMatthew G. Knepley numComponents += Nc; 1156e729f68cSMatthew G. Knepley } 1157e729f68cSMatthew G. Knepley ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, &quadPoints, &quadWeights);CHKERRQ(ierr); 11580163fd50SMatthew G. Knepley ierr = DMPlexProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); 1159e729f68cSMatthew G. Knepley ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,dim,&coords,dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 1160e729f68cSMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1161e729f68cSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 1162e729f68cSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1163e729f68cSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1164e729f68cSMatthew G. Knepley PetscScalar *x = NULL; 11656f288a59SMatthew G. Knepley PetscScalar elemDiff = 0.0; 1166e729f68cSMatthew G. Knepley 1167e729f68cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 1168e729f68cSMatthew G. Knepley if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c); 1169e729f68cSMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1170e729f68cSMatthew G. Knepley 1171e729f68cSMatthew G. Knepley for (field = 0, fieldOffset = 0; field < numFields; ++field) { 1172e729f68cSMatthew G. Knepley PetscObject obj; 1173e729f68cSMatthew G. Knepley PetscClassId id; 1174e729f68cSMatthew G. Knepley void * const ctx = ctxs ? ctxs[field] : NULL; 1175e729f68cSMatthew G. Knepley PetscInt Nb, Nc, q, fc; 1176e729f68cSMatthew G. Knepley 1177e729f68cSMatthew G. Knepley ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr); 1178e729f68cSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1179e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);} 1180e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;} 1181e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1182e729f68cSMatthew G. Knepley for (q = 0; q < numQuadPoints; ++q) { 1183e729f68cSMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &quadPoints[q*dim], coords); 11840163fd50SMatthew G. Knepley ierr = (*funcs[field])(dim, time, coords, Nc, funcVal, ctx);CHKERRQ(ierr); 1185e729f68cSMatthew G. Knepley if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1186e729f68cSMatthew G. Knepley else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);} 1187e729f68cSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field); 1188e729f68cSMatthew G. Knepley for (fc = 0; fc < Nc; ++fc) { 1189e729f68cSMatthew G. Knepley elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*quadWeights[q]*detJ; 1190e729f68cSMatthew G. Knepley } 1191e729f68cSMatthew G. Knepley } 1192e729f68cSMatthew G. Knepley fieldOffset += Nb*Nc; 1193e729f68cSMatthew G. Knepley } 1194e729f68cSMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr); 1195e729f68cSMatthew G. Knepley ierr = VecSetValuesSection(D, section, c, &elemDiff, ADD_VALUES);CHKERRQ(ierr); 1196e729f68cSMatthew G. Knepley } 1197e729f68cSMatthew G. Knepley ierr = PetscFree6(funcVal,interpolant,coords,v0,J,invJ);CHKERRQ(ierr); 1198e729f68cSMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1199e729f68cSMatthew G. Knepley ierr = VecSqrtAbs(D);CHKERRQ(ierr); 1200e729f68cSMatthew G. Knepley PetscFunctionReturn(0); 1201e729f68cSMatthew G. Knepley } 1202e729f68cSMatthew G. Knepley 1203e729f68cSMatthew G. Knepley #undef __FUNCT__ 120473d901b8SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeIntegralFEM" 120573d901b8SMatthew G. Knepley /*@ 120673d901b8SMatthew G. Knepley DMPlexComputeIntegralFEM - Form the local integral F from the local input X using pointwise functions specified by the user 120773d901b8SMatthew G. Knepley 120873d901b8SMatthew G. Knepley Input Parameters: 120973d901b8SMatthew G. Knepley + dm - The mesh 121073d901b8SMatthew G. Knepley . X - Local input vector 121173d901b8SMatthew G. Knepley - user - The user context 121273d901b8SMatthew G. Knepley 121373d901b8SMatthew G. Knepley Output Parameter: 121473d901b8SMatthew G. Knepley . integral - Local integral for each field 121573d901b8SMatthew G. Knepley 121673d901b8SMatthew G. Knepley Level: developer 121773d901b8SMatthew G. Knepley 121873d901b8SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM() 121973d901b8SMatthew G. Knepley @*/ 12200f2d7e86SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscReal *integral, void *user) 122173d901b8SMatthew G. Knepley { 122273d901b8SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dm->data; 122373d901b8SMatthew G. Knepley DM dmAux; 122473d901b8SMatthew G. Knepley Vec localX, A; 12252764a2aaSMatthew G. Knepley PetscDS prob, probAux = NULL; 122673d901b8SMatthew G. Knepley PetscSection section, sectionAux; 1227bbce034cSMatthew G. Knepley PetscFECellGeom *cgeom; 122873d901b8SMatthew G. Knepley PetscScalar *u, *a = NULL; 1229c1f031eeSMatthew G. Knepley PetscReal *lintegral, *vol; 12309ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, numCells, cStart, cEnd, cEndInterior, c; 12310f2d7e86SMatthew G. Knepley PetscInt totDim, totDimAux; 123273d901b8SMatthew G. Knepley PetscErrorCode ierr; 123373d901b8SMatthew G. Knepley 123473d901b8SMatthew G. Knepley PetscFunctionBegin; 1235c1f031eeSMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 123673d901b8SMatthew G. Knepley ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr); 123773d901b8SMatthew G. Knepley ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 123873d901b8SMatthew G. Knepley ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr); 1239c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 124073d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 12412764a2aaSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 12422764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 124373d901b8SMatthew G. Knepley ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr); 124473d901b8SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 12459ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 12469ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 124773d901b8SMatthew G. Knepley numCells = cEnd - cStart; 124873d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr); 124973d901b8SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr); 125073d901b8SMatthew G. Knepley if (dmAux) { 125173d901b8SMatthew G. Knepley ierr = DMGetDefaultSection(dmAux, §ionAux);CHKERRQ(ierr); 12522764a2aaSMatthew G. Knepley ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr); 12532764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr); 125473d901b8SMatthew G. Knepley } 1255d7ddef95SMatthew G. Knepley ierr = DMPlexInsertBoundaryValues(dm, localX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr); 1256c1f031eeSMatthew G. Knepley ierr = PetscMalloc4(Nf,&lintegral,numCells*totDim,&u,numCells,&cgeom,numCells,&vol);CHKERRQ(ierr); 12570f2d7e86SMatthew G. Knepley if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);} 1258c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {lintegral[f] = 0.0;} 125973d901b8SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 126073d901b8SMatthew G. Knepley PetscScalar *x = NULL; 126173d901b8SMatthew G. Knepley PetscInt i; 126273d901b8SMatthew G. Knepley 1263bbce034cSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, cgeom[c].v0, cgeom[c].J, cgeom[c].invJ, &cgeom[c].detJ);CHKERRQ(ierr); 1264c1f031eeSMatthew G. Knepley ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol[c], NULL, NULL);CHKERRQ(ierr); 1265bbce034cSMatthew 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); 126673d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 12670f2d7e86SMatthew G. Knepley for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i]; 126873d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dm, section, localX, c, NULL, &x);CHKERRQ(ierr); 126973d901b8SMatthew G. Knepley if (dmAux) { 127073d901b8SMatthew G. Knepley ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 12710f2d7e86SMatthew G. Knepley for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i]; 127273d901b8SMatthew G. Knepley ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr); 127373d901b8SMatthew G. Knepley } 127473d901b8SMatthew G. Knepley } 127573d901b8SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 1276c1f031eeSMatthew G. Knepley PetscObject obj; 1277c1f031eeSMatthew G. Knepley PetscClassId id; 1278c1f031eeSMatthew G. Knepley PetscInt numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset; 127973d901b8SMatthew G. Knepley 1280c1f031eeSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 1281c1f031eeSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 1282c1f031eeSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 1283c1f031eeSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1284c1f031eeSMatthew G. Knepley PetscQuadrature q; 1285c1f031eeSMatthew G. Knepley PetscInt Nq, Nb; 1286c1f031eeSMatthew G. Knepley 12870f2d7e86SMatthew G. Knepley ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr); 1288c1f031eeSMatthew G. Knepley ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr); 1289c1f031eeSMatthew G. Knepley ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr); 1290c1f031eeSMatthew G. Knepley ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr); 1291c1f031eeSMatthew G. Knepley blockSize = Nb*Nq; 129273d901b8SMatthew G. Knepley batchSize = numBlocks * blockSize; 12930f2d7e86SMatthew G. Knepley ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr); 129473d901b8SMatthew G. Knepley numChunks = numCells / (numBatches*batchSize); 129573d901b8SMatthew G. Knepley Ne = numChunks*numBatches*batchSize; 129673d901b8SMatthew G. Knepley Nr = numCells % (numBatches*batchSize); 129773d901b8SMatthew G. Knepley offset = numCells - Nr; 1298c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Ne, cgeom, u, probAux, a, lintegral);CHKERRQ(ierr); 1299c1f031eeSMatthew G. Knepley ierr = PetscFEIntegrate(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], probAux, &a[offset*totDimAux], lintegral);CHKERRQ(ierr); 1300c1f031eeSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 1301b69edc29SMatthew G. Knepley /* PetscFV fv = (PetscFV) obj; */ 1302c1f031eeSMatthew G. Knepley PetscInt foff; 1303420e96edSMatthew G. Knepley PetscPointFunc obj_func; 1304b69edc29SMatthew G. Knepley PetscScalar lint; 1305c1f031eeSMatthew G. Knepley 1306c1f031eeSMatthew G. Knepley ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr); 1307c1f031eeSMatthew G. Knepley ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr); 1308c1f031eeSMatthew G. Knepley if (obj_func) { 1309c1f031eeSMatthew G. Knepley for (c = 0; c < numCells; ++c) { 1310c1f031eeSMatthew G. Knepley /* TODO: Need full pointwise interpolation and get centroid for x argument */ 131130b9ff8bSMatthew G. Knepley obj_func(dim, Nf, 0, NULL, NULL, &u[totDim*c+foff], NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0.0, NULL, &lint); 1312b69edc29SMatthew G. Knepley lintegral[f] = PetscRealPart(lint)*vol[c]; 131373d901b8SMatthew G. Knepley } 1314c1f031eeSMatthew G. Knepley } 1315c1f031eeSMatthew G. Knepley } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f); 1316c1f031eeSMatthew G. Knepley } 131773d901b8SMatthew G. Knepley if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);} 131873d901b8SMatthew G. Knepley if (mesh->printFEM) { 131973d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Local integral:");CHKERRQ(ierr); 1320c1f031eeSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", lintegral[f]);CHKERRQ(ierr);} 132173d901b8SMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr); 132273d901b8SMatthew G. Knepley } 132373d901b8SMatthew G. Knepley ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr); 1324b2566f29SBarry Smith ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr); 1325c1f031eeSMatthew G. Knepley ierr = PetscFree4(lintegral,u,cgeom,vol);CHKERRQ(ierr); 1326c1f031eeSMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr); 132773d901b8SMatthew G. Knepley PetscFunctionReturn(0); 132873d901b8SMatthew G. Knepley } 132973d901b8SMatthew G. Knepley 133073d901b8SMatthew G. Knepley #undef __FUNCT__ 133168132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorNested" 1332d69c5d34SMatthew G. Knepley /*@ 133368132eb9SMatthew G. Knepley DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM. 1334d69c5d34SMatthew G. Knepley 1335d69c5d34SMatthew G. Knepley Input Parameters: 1336d69c5d34SMatthew G. Knepley + dmf - The fine mesh 1337d69c5d34SMatthew G. Knepley . dmc - The coarse mesh 1338d69c5d34SMatthew G. Knepley - user - The user context 1339d69c5d34SMatthew G. Knepley 1340d69c5d34SMatthew G. Knepley Output Parameter: 1341934789fcSMatthew G. Knepley . In - The interpolation matrix 1342d69c5d34SMatthew G. Knepley 1343d69c5d34SMatthew G. Knepley Level: developer 1344d69c5d34SMatthew G. Knepley 134568132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM() 1346d69c5d34SMatthew G. Knepley @*/ 134768132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user) 1348d69c5d34SMatthew G. Knepley { 1349d69c5d34SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmc->data; 1350d69c5d34SMatthew G. Knepley const char *name = "Interpolator"; 13512764a2aaSMatthew G. Knepley PetscDS prob; 1352d69c5d34SMatthew G. Knepley PetscFE *feRef; 135397c42addSMatthew G. Knepley PetscFV *fvRef; 1354d69c5d34SMatthew G. Knepley PetscSection fsection, fglobalSection; 1355d69c5d34SMatthew G. Knepley PetscSection csection, cglobalSection; 1356d69c5d34SMatthew G. Knepley PetscScalar *elemMat; 13579ac3fadcSMatthew G. Knepley PetscInt dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c; 13580f2d7e86SMatthew G. Knepley PetscInt cTotDim, rTotDim = 0; 1359d69c5d34SMatthew G. Knepley PetscErrorCode ierr; 1360d69c5d34SMatthew G. Knepley 1361d69c5d34SMatthew G. Knepley PetscFunctionBegin; 1362d69c5d34SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1363c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 1364d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 1365d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 1366d69c5d34SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 1367d69c5d34SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 1368d69c5d34SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 1369d69c5d34SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 13709ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 13719ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 13722764a2aaSMatthew G. Knepley ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr); 137397c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 1374d69c5d34SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 137597c42addSMatthew G. Knepley PetscObject obj; 137697c42addSMatthew G. Knepley PetscClassId id; 1377aa7890ccSMatthew G. Knepley PetscInt rNb = 0, Nc = 0; 1378d69c5d34SMatthew G. Knepley 137997c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 138097c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 138197c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 138297c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 138397c42addSMatthew G. Knepley 13840f2d7e86SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 1385d69c5d34SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr); 13860f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 138797c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 138897c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 138997c42addSMatthew G. Knepley PetscDualSpace Q; 139097c42addSMatthew G. Knepley 139197c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 139297c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 139397c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr); 139497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 139597c42addSMatthew G. Knepley } 13960f2d7e86SMatthew G. Knepley rTotDim += rNb*Nc; 1397d69c5d34SMatthew G. Knepley } 13982764a2aaSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 13990f2d7e86SMatthew G. Knepley ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr); 14000f2d7e86SMatthew G. Knepley ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr); 1401d69c5d34SMatthew G. Knepley for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) { 1402d69c5d34SMatthew G. Knepley PetscDualSpace Qref; 1403d69c5d34SMatthew G. Knepley PetscQuadrature f; 1404d69c5d34SMatthew G. Knepley const PetscReal *qpoints, *qweights; 1405d69c5d34SMatthew G. Knepley PetscReal *points; 1406d69c5d34SMatthew G. Knepley PetscInt npoints = 0, Nc, Np, fpdim, i, k, p, d; 1407d69c5d34SMatthew G. Knepley 1408d69c5d34SMatthew G. Knepley /* Compose points from all dual basis functionals */ 140997c42addSMatthew G. Knepley if (feRef[fieldI]) { 1410d69c5d34SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr); 14110f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr); 141297c42addSMatthew G. Knepley } else { 141397c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr); 141497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr); 141597c42addSMatthew G. Knepley } 1416d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr); 1417d69c5d34SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 1418d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1419d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, NULL);CHKERRQ(ierr); 1420d69c5d34SMatthew G. Knepley npoints += Np; 1421d69c5d34SMatthew G. Knepley } 1422d69c5d34SMatthew G. Knepley ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr); 1423d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1424d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1425d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 1426d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d]; 1427d69c5d34SMatthew G. Knepley } 1428d69c5d34SMatthew G. Knepley 1429d69c5d34SMatthew G. Knepley for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) { 143097c42addSMatthew G. Knepley PetscObject obj; 143197c42addSMatthew G. Knepley PetscClassId id; 1432d69c5d34SMatthew G. Knepley PetscReal *B; 1433aa7890ccSMatthew G. Knepley PetscInt NcJ = 0, cpdim = 0, j; 1434d69c5d34SMatthew G. Knepley 143597c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr); 143697c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 143797c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 143897c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 1439d69c5d34SMatthew G. Knepley 1440d69c5d34SMatthew G. Knepley /* Evaluate basis at points */ 14410f2d7e86SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr); 14420f2d7e86SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 1443ffe73a53SMatthew G. Knepley /* For now, fields only interpolate themselves */ 1444ffe73a53SMatthew G. Knepley if (fieldI == fieldJ) { 14457c927364SMatthew 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); 14460f2d7e86SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr); 1447d69c5d34SMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 1448d69c5d34SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 1449d69c5d34SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 1450d69c5d34SMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 145136a6d9c0SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 14520f2d7e86SMatthew 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]; 145336a6d9c0SMatthew G. Knepley } 1454d69c5d34SMatthew G. Knepley } 1455d69c5d34SMatthew G. Knepley } 14560f2d7e86SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 1457ffe73a53SMatthew G. Knepley } 145897c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 145997c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 146097c42addSMatthew G. Knepley 146197c42addSMatthew G. Knepley /* Evaluate constant function at points */ 146297c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr); 146397c42addSMatthew G. Knepley cpdim = 1; 146497c42addSMatthew G. Knepley /* For now, fields only interpolate themselves */ 146597c42addSMatthew G. Knepley if (fieldI == fieldJ) { 146697c42addSMatthew 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); 146797c42addSMatthew G. Knepley for (i = 0, k = 0; i < fpdim; ++i) { 146897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr); 146997c42addSMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, NULL, &qweights);CHKERRQ(ierr); 147097c42addSMatthew G. Knepley for (p = 0; p < Np; ++p, ++k) { 147197c42addSMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 147297c42addSMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(offsetI + i*Nc + c)*cTotDim + offsetJ + j*NcJ + c] += 1.0*qweights[p]; 147397c42addSMatthew G. Knepley } 147497c42addSMatthew G. Knepley } 147597c42addSMatthew G. Knepley } 147697c42addSMatthew G. Knepley } 147797c42addSMatthew G. Knepley } 147836a6d9c0SMatthew G. Knepley offsetJ += cpdim*NcJ; 1479d69c5d34SMatthew G. Knepley } 1480d69c5d34SMatthew G. Knepley offsetI += fpdim*Nc; 1481549a8adaSMatthew G. Knepley ierr = PetscFree(points);CHKERRQ(ierr); 1482d69c5d34SMatthew G. Knepley } 14830f2d7e86SMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);} 14847f5b169aSMatthew G. Knepley /* Preallocate matrix */ 14857f5b169aSMatthew G. Knepley { 1486c094ef40SMatthew G. Knepley Mat preallocator; 1487c094ef40SMatthew G. Knepley PetscScalar *vals; 1488c094ef40SMatthew G. Knepley PetscInt *cellCIndices, *cellFIndices; 1489c094ef40SMatthew G. Knepley PetscInt locRows, locCols, cell; 14907f5b169aSMatthew G. Knepley 1491c094ef40SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr); 1492c094ef40SMatthew G. Knepley ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr); 1493c094ef40SMatthew G. Knepley ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr); 1494c094ef40SMatthew G. Knepley ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr); 1495c094ef40SMatthew G. Knepley ierr = MatSetUp(preallocator);CHKERRQ(ierr); 1496c094ef40SMatthew G. Knepley ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr); 14977f5b169aSMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 14987f5b169aSMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr); 1499c094ef40SMatthew G. Knepley ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr); 15007f5b169aSMatthew G. Knepley } 1501c094ef40SMatthew G. Knepley ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr); 1502c094ef40SMatthew G. Knepley ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1503c094ef40SMatthew G. Knepley ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1504c094ef40SMatthew G. Knepley ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr); 1505c094ef40SMatthew G. Knepley ierr = MatDestroy(&preallocator);CHKERRQ(ierr); 15067f5b169aSMatthew G. Knepley } 15077f5b169aSMatthew G. Knepley /* Fill matrix */ 15087f5b169aSMatthew G. Knepley ierr = MatZeroEntries(In);CHKERRQ(ierr); 1509d69c5d34SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 1510934789fcSMatthew G. Knepley ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr); 1511d69c5d34SMatthew G. Knepley } 1512549a8adaSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);} 151397c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 1514549a8adaSMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 1515934789fcSMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1516934789fcSMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1517d69c5d34SMatthew G. Knepley if (mesh->printFEM) { 1518d69c5d34SMatthew G. Knepley ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr); 1519934789fcSMatthew G. Knepley ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr); 1520934789fcSMatthew G. Knepley ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 1521d69c5d34SMatthew G. Knepley } 1522d69c5d34SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1523d69c5d34SMatthew G. Knepley PetscFunctionReturn(0); 1524d69c5d34SMatthew G. Knepley } 15256c73c22cSMatthew G. Knepley 15266c73c22cSMatthew G. Knepley #undef __FUNCT__ 152768132eb9SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInterpolatorGeneral" 152868132eb9SMatthew G. Knepley /*@ 152968132eb9SMatthew G. Knepley DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM. 153068132eb9SMatthew G. Knepley 153168132eb9SMatthew G. Knepley Input Parameters: 153268132eb9SMatthew G. Knepley + dmf - The fine mesh 153368132eb9SMatthew G. Knepley . dmc - The coarse mesh 153468132eb9SMatthew G. Knepley - user - The user context 153568132eb9SMatthew G. Knepley 153668132eb9SMatthew G. Knepley Output Parameter: 153768132eb9SMatthew G. Knepley . In - The interpolation matrix 153868132eb9SMatthew G. Knepley 153968132eb9SMatthew G. Knepley Level: developer 154068132eb9SMatthew G. Knepley 154168132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM() 154268132eb9SMatthew G. Knepley @*/ 154368132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user) 15444ef9d792SMatthew G. Knepley { 154564e98e1dSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *) dmf->data; 154664e98e1dSMatthew G. Knepley const char *name = "Interpolator"; 15474ef9d792SMatthew G. Knepley PetscDS prob; 15484ef9d792SMatthew G. Knepley PetscSection fsection, csection, globalFSection, globalCSection; 15494ef9d792SMatthew G. Knepley PetscHashJK ht; 15504ef9d792SMatthew G. Knepley PetscLayout rLayout; 15514ef9d792SMatthew G. Knepley PetscInt *dnz, *onz; 15524ef9d792SMatthew G. Knepley PetscInt locRows, rStart, rEnd; 15534ef9d792SMatthew G. Knepley PetscReal *x, *v0, *J, *invJ, detJ; 15544ef9d792SMatthew G. Knepley PetscReal *v0c, *Jc, *invJc, detJc; 15554ef9d792SMatthew G. Knepley PetscScalar *elemMat; 15564ef9d792SMatthew G. Knepley PetscInt dim, Nf, field, totDim, cStart, cEnd, cell, ccell; 15574ef9d792SMatthew G. Knepley PetscErrorCode ierr; 15584ef9d792SMatthew G. Knepley 15594ef9d792SMatthew G. Knepley PetscFunctionBegin; 15604ef9d792SMatthew G. Knepley ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr); 15614ef9d792SMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 15624ef9d792SMatthew G. Knepley ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr); 15634ef9d792SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 15644ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr); 15654ef9d792SMatthew G. Knepley ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr); 15664ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 15674ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr); 15684ef9d792SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 15694ef9d792SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr); 15704ef9d792SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr); 15714ef9d792SMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 15724ef9d792SMatthew G. Knepley ierr = PetscMalloc1(totDim*totDim, &elemMat);CHKERRQ(ierr); 15734ef9d792SMatthew G. Knepley 15744ef9d792SMatthew G. Knepley ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr); 15754ef9d792SMatthew G. Knepley ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr); 15764ef9d792SMatthew G. Knepley ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr); 15774ef9d792SMatthew G. Knepley ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr); 15784ef9d792SMatthew G. Knepley ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr); 15794ef9d792SMatthew G. Knepley ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr); 15804ef9d792SMatthew G. Knepley ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr); 15814ef9d792SMatthew G. Knepley ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr); 15824ef9d792SMatthew G. Knepley ierr = PetscHashJKCreate(&ht);CHKERRQ(ierr); 15834ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 15844ef9d792SMatthew G. Knepley PetscObject obj; 15854ef9d792SMatthew G. Knepley PetscClassId id; 1586c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 15874ef9d792SMatthew G. Knepley PetscQuadrature f; 158817f047d8SMatthew G. Knepley const PetscReal *qpoints; 158917f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 15904ef9d792SMatthew G. Knepley 15914ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 15924ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 15934ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 15944ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 15954ef9d792SMatthew G. Knepley 15964ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 15974ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 15984ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 15994ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 16004ef9d792SMatthew G. Knepley 16014ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 16024ef9d792SMatthew G. Knepley Nc = 1; 16034ef9d792SMatthew G. Knepley } 16044ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 16054ef9d792SMatthew G. Knepley /* For each fine grid cell */ 16064ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 16074ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 16084ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 16094ef9d792SMatthew G. Knepley 16104ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 16114ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 16124ef9d792SMatthew G. Knepley if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc); 16134ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 16144ef9d792SMatthew G. Knepley Vec pointVec; 16154ef9d792SMatthew G. Knepley PetscScalar *pV; 16164ef9d792SMatthew G. Knepley IS coarseCellIS; 16174ef9d792SMatthew G. Knepley const PetscInt *coarseCells; 16184ef9d792SMatthew G. Knepley PetscInt numCoarseCells, q, r, c; 16194ef9d792SMatthew G. Knepley 16204ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 16214ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 16224ef9d792SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr); 16234ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 16244ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 16254ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 16264ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 16274ef9d792SMatthew G. Knepley /* Transform point to real space */ 16284ef9d792SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x); 16294ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 16304ef9d792SMatthew G. Knepley } 16314ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 16324ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 16334ef9d792SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr); 16344ef9d792SMatthew G. Knepley ierr = ISViewFromOptions(coarseCellIS, NULL, "-interp_is_view");CHKERRQ(ierr); 16354ef9d792SMatthew G. Knepley /* Update preallocation info */ 16364ef9d792SMatthew G. Knepley ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr); 16374ef9d792SMatthew G. Knepley ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 16384ef9d792SMatthew G. Knepley for (r = 0; r < Nc; ++r) { 16394ef9d792SMatthew G. Knepley PetscHashJKKey key; 16404ef9d792SMatthew G. Knepley PetscHashJKIter missing, iter; 16414ef9d792SMatthew G. Knepley 16424ef9d792SMatthew G. Knepley key.j = findices[i*Nc+r]; 16434ef9d792SMatthew G. Knepley if (key.j < 0) continue; 16444ef9d792SMatthew G. Knepley /* Get indices for coarse elements */ 16454ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 16464ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 16474ef9d792SMatthew G. Knepley for (c = 0; c < numCIndices; ++c) { 16484ef9d792SMatthew G. Knepley key.k = cindices[c]; 16494ef9d792SMatthew G. Knepley if (key.k < 0) continue; 16504ef9d792SMatthew G. Knepley ierr = PetscHashJKPut(ht, key, &missing, &iter);CHKERRQ(ierr); 16514ef9d792SMatthew G. Knepley if (missing) { 16524ef9d792SMatthew G. Knepley ierr = PetscHashJKSet(ht, iter, 1);CHKERRQ(ierr); 16534ef9d792SMatthew G. Knepley if ((key.k >= rStart) && (key.k < rEnd)) ++dnz[key.j-rStart]; 16544ef9d792SMatthew G. Knepley else ++onz[key.j-rStart]; 16554ef9d792SMatthew G. Knepley } 16564ef9d792SMatthew G. Knepley } 16574ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 16584ef9d792SMatthew G. Knepley } 16594ef9d792SMatthew G. Knepley } 16604ef9d792SMatthew G. Knepley ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 16614ef9d792SMatthew G. Knepley ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr); 16624ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 16634ef9d792SMatthew G. Knepley } 16644ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 16654ef9d792SMatthew G. Knepley } 16664ef9d792SMatthew G. Knepley } 16674ef9d792SMatthew G. Knepley ierr = PetscHashJKDestroy(&ht);CHKERRQ(ierr); 16684ef9d792SMatthew G. Knepley ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr); 16694ef9d792SMatthew G. Knepley ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 16704ef9d792SMatthew G. Knepley ierr = PetscFree2(dnz,onz);CHKERRQ(ierr); 16714ef9d792SMatthew G. Knepley for (field = 0; field < Nf; ++field) { 16724ef9d792SMatthew G. Knepley PetscObject obj; 16734ef9d792SMatthew G. Knepley PetscClassId id; 1674c0d7054bSMatthew G. Knepley PetscDualSpace Q = NULL; 16754ef9d792SMatthew G. Knepley PetscQuadrature f; 16764ef9d792SMatthew G. Knepley const PetscReal *qpoints, *qweights; 167717f047d8SMatthew G. Knepley PetscInt Nc, Np, fpdim, i, d; 16784ef9d792SMatthew G. Knepley 16794ef9d792SMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr); 16804ef9d792SMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 16814ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 16824ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 16834ef9d792SMatthew G. Knepley 16844ef9d792SMatthew G. Knepley ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr); 16854ef9d792SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 16864ef9d792SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 16874ef9d792SMatthew G. Knepley PetscFV fv = (PetscFV) obj; 16884ef9d792SMatthew G. Knepley 16894ef9d792SMatthew G. Knepley ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr); 16904ef9d792SMatthew G. Knepley Nc = 1; 16914ef9d792SMatthew G. Knepley } 16924ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr); 16934ef9d792SMatthew G. Knepley /* For each fine grid cell */ 16944ef9d792SMatthew G. Knepley for (cell = cStart; cell < cEnd; ++cell) { 16954ef9d792SMatthew G. Knepley PetscInt *findices, *cindices; 16964ef9d792SMatthew G. Knepley PetscInt numFIndices, numCIndices; 16974ef9d792SMatthew G. Knepley 16984ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 16994ef9d792SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr); 17004ef9d792SMatthew G. Knepley if (numFIndices != fpdim*Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim*Nc); 17014ef9d792SMatthew G. Knepley for (i = 0; i < fpdim; ++i) { 17024ef9d792SMatthew G. Knepley Vec pointVec; 17034ef9d792SMatthew G. Knepley PetscScalar *pV; 17044ef9d792SMatthew G. Knepley IS coarseCellIS; 17054ef9d792SMatthew G. Knepley const PetscInt *coarseCells; 170617f047d8SMatthew G. Knepley PetscInt numCoarseCells, cpdim, q, c, j; 17074ef9d792SMatthew G. Knepley 17084ef9d792SMatthew G. Knepley /* Get points from the dual basis functional quadrature */ 17094ef9d792SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr); 17104ef9d792SMatthew G. Knepley ierr = PetscQuadratureGetData(f, NULL, &Np, &qpoints, &qweights);CHKERRQ(ierr); 17114ef9d792SMatthew G. Knepley ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr); 17124ef9d792SMatthew G. Knepley ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr); 17134ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 17144ef9d792SMatthew G. Knepley for (q = 0; q < Np; ++q) { 17154ef9d792SMatthew G. Knepley /* Transform point to real space */ 17164ef9d792SMatthew G. Knepley CoordinatesRefToReal(dim, dim, v0, J, &qpoints[q*dim], x); 17174ef9d792SMatthew G. Knepley for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d]; 17184ef9d792SMatthew G. Knepley } 17194ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 17204ef9d792SMatthew G. Knepley /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */ 17214ef9d792SMatthew G. Knepley ierr = DMLocatePoints(dmc, pointVec, &coarseCellIS);CHKERRQ(ierr); 17224ef9d792SMatthew G. Knepley /* Update preallocation info */ 17234ef9d792SMatthew G. Knepley ierr = ISGetLocalSize(coarseCellIS, &numCoarseCells);CHKERRQ(ierr); 17244ef9d792SMatthew G. Knepley ierr = ISGetIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 17254ef9d792SMatthew G. Knepley ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr); 17264ef9d792SMatthew G. Knepley for (ccell = 0; ccell < numCoarseCells; ++ccell) { 1727826eb36dSMatthew G. Knepley PetscReal pVReal[3]; 1728826eb36dSMatthew G. Knepley 17294ef9d792SMatthew G. Knepley ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 17304ef9d792SMatthew G. Knepley /* Transform points from real space to coarse reference space */ 1731f1ac73f2SMatthew G. Knepley ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell], NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr); 1732e2d86523SMatthew G. Knepley for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]); 1733826eb36dSMatthew G. Knepley CoordinatesRealToRef(dim, dim, v0c, invJc, pVReal, x); 17344ef9d792SMatthew G. Knepley 17354ef9d792SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 17364ef9d792SMatthew G. Knepley PetscFE fe = (PetscFE) obj; 17374ef9d792SMatthew G. Knepley PetscReal *B; 17384ef9d792SMatthew G. Knepley 17394ef9d792SMatthew G. Knepley /* Evaluate coarse basis on contained point */ 17404ef9d792SMatthew G. Knepley ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr); 17414ef9d792SMatthew G. Knepley ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr); 17424ef9d792SMatthew G. Knepley /* Get elemMat entries by multiplying by weight */ 17434ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 17444ef9d792SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = B[j*Nc + c]*qweights[ccell]; 17454ef9d792SMatthew G. Knepley } 17464ef9d792SMatthew G. Knepley ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr); 17474ef9d792SMatthew G. Knepley } else { 17484ef9d792SMatthew G. Knepley cpdim = 1; 17494ef9d792SMatthew G. Knepley for (j = 0; j < cpdim; ++j) { 17504ef9d792SMatthew G. Knepley for (c = 0; c < Nc; ++c) elemMat[(c*cpdim + j)*Nc + c] = 1.0*qweights[ccell]; 17514ef9d792SMatthew G. Knepley } 17524ef9d792SMatthew G. Knepley } 17534ef9d792SMatthew G. Knepley /* Update interpolator */ 175464e98e1dSMatthew G. Knepley if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, Nc, numCIndices, elemMat);CHKERRQ(ierr);} 17554ef9d792SMatthew G. Knepley ierr = MatSetValues(In, Nc, &findices[i*Nc], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr); 17564ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell], &numCIndices, &cindices);CHKERRQ(ierr);CHKERRQ(ierr); 17574ef9d792SMatthew G. Knepley } 17584ef9d792SMatthew G. Knepley ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr); 17594ef9d792SMatthew G. Knepley ierr = ISRestoreIndices(coarseCellIS, &coarseCells);CHKERRQ(ierr); 17604ef9d792SMatthew G. Knepley ierr = ISDestroy(&coarseCellIS);CHKERRQ(ierr); 17614ef9d792SMatthew G. Knepley ierr = VecDestroy(&pointVec);CHKERRQ(ierr); 17624ef9d792SMatthew G. Knepley } 17634ef9d792SMatthew G. Knepley ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices);CHKERRQ(ierr);CHKERRQ(ierr); 17644ef9d792SMatthew G. Knepley } 17654ef9d792SMatthew G. Knepley } 17664ef9d792SMatthew G. Knepley ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr); 17674ef9d792SMatthew G. Knepley ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr); 17684ef9d792SMatthew G. Knepley ierr = PetscFree(elemMat);CHKERRQ(ierr); 17694ef9d792SMatthew G. Knepley ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17704ef9d792SMatthew G. Knepley ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17714ef9d792SMatthew G. Knepley PetscFunctionReturn(0); 17724ef9d792SMatthew G. Knepley } 17734ef9d792SMatthew G. Knepley 17744ef9d792SMatthew G. Knepley #undef __FUNCT__ 17757c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeInjectorFEM" 17767c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user) 17777c927364SMatthew G. Knepley { 1778e9d4ef1bSMatthew G. Knepley PetscDS prob; 17797c927364SMatthew G. Knepley PetscFE *feRef; 178097c42addSMatthew G. Knepley PetscFV *fvRef; 17817c927364SMatthew G. Knepley Vec fv, cv; 17827c927364SMatthew G. Knepley IS fis, cis; 17837c927364SMatthew G. Knepley PetscSection fsection, fglobalSection, csection, cglobalSection; 17847c927364SMatthew G. Knepley PetscInt *cmap, *cellCIndices, *cellFIndices, *cindices, *findices; 17850bd915a7SMatthew G. Knepley PetscInt cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m; 17867c927364SMatthew G. Knepley PetscErrorCode ierr; 17877c927364SMatthew G. Knepley 17887c927364SMatthew G. Knepley PetscFunctionBegin; 178975a69067SMatthew G. Knepley ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1790c73cfb54SMatthew G. Knepley ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr); 17917c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr); 17927c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr); 17937c927364SMatthew G. Knepley ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr); 17947c927364SMatthew G. Knepley ierr = DMGetDefaultGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr); 17957c927364SMatthew G. Knepley ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr); 17967c927364SMatthew G. Knepley ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr); 17979ac3fadcSMatthew G. Knepley ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr); 17989ac3fadcSMatthew G. Knepley cEnd = cEndInterior < 0 ? cEnd : cEndInterior; 1799e9d4ef1bSMatthew G. Knepley ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr); 180097c42addSMatthew G. Knepley ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr); 18017c927364SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 180297c42addSMatthew G. Knepley PetscObject obj; 180397c42addSMatthew G. Knepley PetscClassId id; 1804aa7890ccSMatthew G. Knepley PetscInt fNb = 0, Nc = 0; 18057c927364SMatthew G. Knepley 180697c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr); 180797c42addSMatthew G. Knepley ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr); 180897c42addSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 180997c42addSMatthew G. Knepley PetscFE fe = (PetscFE) obj; 181097c42addSMatthew G. Knepley 18117c927364SMatthew G. Knepley ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr); 18127c927364SMatthew G. Knepley ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr); 18137c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr); 181497c42addSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 181597c42addSMatthew G. Knepley PetscFV fv = (PetscFV) obj; 181697c42addSMatthew G. Knepley PetscDualSpace Q; 181797c42addSMatthew G. Knepley 181897c42addSMatthew G. Knepley ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr); 181997c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr); 182097c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr); 182197c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr); 182297c42addSMatthew G. Knepley } 18237c927364SMatthew G. Knepley fTotDim += fNb*Nc; 18247c927364SMatthew G. Knepley } 1825e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr); 18267c927364SMatthew G. Knepley ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr); 18277c927364SMatthew G. Knepley for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) { 18287c927364SMatthew G. Knepley PetscFE feC; 182997c42addSMatthew G. Knepley PetscFV fvC; 18307c927364SMatthew G. Knepley PetscDualSpace QF, QC; 18317c927364SMatthew G. Knepley PetscInt NcF, NcC, fpdim, cpdim; 18327c927364SMatthew G. Knepley 183397c42addSMatthew G. Knepley if (feRef[field]) { 1834e9d4ef1bSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr); 18357c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr); 18367c927364SMatthew G. Knepley ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr); 18377c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr); 18387c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 18397c927364SMatthew G. Knepley ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr); 18407c927364SMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 184197c42addSMatthew G. Knepley } else { 184297c42addSMatthew G. Knepley ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr); 184397c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr); 184497c42addSMatthew G. Knepley ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr); 184597c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr); 184697c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr); 184797c42addSMatthew G. Knepley ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr); 184897c42addSMatthew G. Knepley ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr); 184997c42addSMatthew G. Knepley } 185097c42addSMatthew 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); 18517c927364SMatthew G. Knepley for (c = 0; c < cpdim; ++c) { 18527c927364SMatthew G. Knepley PetscQuadrature cfunc; 18537c927364SMatthew G. Knepley const PetscReal *cqpoints; 18547c927364SMatthew G. Knepley PetscInt NpC; 185597c42addSMatthew G. Knepley PetscBool found = PETSC_FALSE; 18567c927364SMatthew G. Knepley 18577c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr); 18587c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(cfunc, NULL, &NpC, &cqpoints, NULL);CHKERRQ(ierr); 185997c42addSMatthew G. Knepley if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments"); 18607c927364SMatthew G. Knepley for (f = 0; f < fpdim; ++f) { 18617c927364SMatthew G. Knepley PetscQuadrature ffunc; 18627c927364SMatthew G. Knepley const PetscReal *fqpoints; 18637c927364SMatthew G. Knepley PetscReal sum = 0.0; 18647c927364SMatthew G. Knepley PetscInt NpF, comp; 18657c927364SMatthew G. Knepley 18667c927364SMatthew G. Knepley ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr); 18677c927364SMatthew G. Knepley ierr = PetscQuadratureGetData(ffunc, NULL, &NpF, &fqpoints, NULL);CHKERRQ(ierr); 18687c927364SMatthew G. Knepley if (NpC != NpF) continue; 18697c927364SMatthew G. Knepley for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]); 18707c927364SMatthew G. Knepley if (sum > 1.0e-9) continue; 18717c927364SMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 18727c927364SMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+f)*NcF+comp; 18737c927364SMatthew G. Knepley } 187497c42addSMatthew G. Knepley found = PETSC_TRUE; 18757c927364SMatthew G. Knepley break; 18767c927364SMatthew G. Knepley } 187797c42addSMatthew G. Knepley if (!found) { 187897c42addSMatthew G. Knepley /* TODO We really want the average here, but some asshole put VecScatter in the interface */ 187997c42addSMatthew G. Knepley if (fvRef[field]) { 188097c42addSMatthew G. Knepley PetscInt comp; 188197c42addSMatthew G. Knepley for (comp = 0; comp < NcC; ++comp) { 188297c42addSMatthew G. Knepley cmap[(offsetC+c)*NcC+comp] = (offsetF+0)*NcF+comp; 188397c42addSMatthew G. Knepley } 188497c42addSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection"); 188597c42addSMatthew G. Knepley } 18867c927364SMatthew G. Knepley } 18877c927364SMatthew G. Knepley offsetC += cpdim*NcC; 18887c927364SMatthew G. Knepley offsetF += fpdim*NcF; 18897c927364SMatthew G. Knepley } 189097c42addSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);} 189197c42addSMatthew G. Knepley ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr); 18927c927364SMatthew G. Knepley 18937c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr); 18947c927364SMatthew G. Knepley ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr); 18950bd915a7SMatthew G. Knepley ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr); 18967c927364SMatthew G. Knepley ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr); 18977c927364SMatthew G. Knepley ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr); 1898aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr); 1899aa7da3c4SMatthew G. Knepley ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr); 19007c927364SMatthew G. Knepley for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1; 19017c927364SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 19027c927364SMatthew G. Knepley ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr); 19037c927364SMatthew G. Knepley for (d = 0; d < cTotDim; ++d) { 19040bd915a7SMatthew G. Knepley if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue; 19057c927364SMatthew 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]]); 19067c927364SMatthew G. Knepley cindices[cellCIndices[d]-startC] = cellCIndices[d]; 19077c927364SMatthew G. Knepley findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]]; 19087c927364SMatthew G. Knepley } 19097c927364SMatthew G. Knepley } 19107c927364SMatthew G. Knepley ierr = PetscFree(cmap);CHKERRQ(ierr); 19117c927364SMatthew G. Knepley ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr); 19127c927364SMatthew G. Knepley 19137c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr); 19147c927364SMatthew G. Knepley ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr); 19157c927364SMatthew G. Knepley ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr); 19167c927364SMatthew G. Knepley ierr = ISDestroy(&cis);CHKERRQ(ierr); 19177c927364SMatthew G. Knepley ierr = ISDestroy(&fis);CHKERRQ(ierr); 19187c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr); 19197c927364SMatthew G. Knepley ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr); 192075a69067SMatthew G. Knepley ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr); 1921cb1e1211SMatthew G Knepley PetscFunctionReturn(0); 1922cb1e1211SMatthew G Knepley } 1923