1d0295fc0SJunchao Zhang #include <petscvec.h> 2af0996ceSBarry Smith #include <petsc/private/dmimpl.h> /*I "petscdm.h" I*/ 3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I "petscdmlabel.h" I*/ 4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h> /*I "petscds.h" I*/ 53e922f36SToby Isaac #include <petscdmplex.h> 6d2b2dc1eSMatthew G. Knepley #include <petscdmceed.h> 7f19dbd58SToby Isaac #include <petscdmfield.h> 80c312b8eSJed Brown #include <petscsf.h> 92764a2aaSMatthew G. Knepley #include <petscds.h> 1047c6ae99SBarry Smith 11f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED 12f918ec44SMatthew G. Knepley #include <petscfeceed.h> 13f918ec44SMatthew G. Knepley #endif 14f918ec44SMatthew G. Knepley 15cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS) 16cea3dcb8SSatish Balay #include <petsc/private/valgrind/memcheck.h> 1700d952a4SJed Brown #endif 1800d952a4SJed Brown 19732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID; 20d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID; 21708be2fdSJed Brown PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_CreateMassMatrix, DM_Load, DM_AdaptInterpolator, DM_ProjectFunction; 2267a56275SMatthew G Knepley 23ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL}; 24d1b3049bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID", "ESSENTIAL", "NATURAL", "INVALID", "INVALID", "ESSENTIAL_FIELD", "NATURAL_FIELD", "INVALID", "INVALID", "ESSENTIAL_BD_FIELD", "NATURAL_RIEMANN", "DMBoundaryConditionType", "DM_BC_", NULL}; 250e762ea3SJed Brown const char *const DMBlockingTypes[] = {"TOPOLOGICAL_POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL}; 269371c9d4SSatish Balay const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", 279371c9d4SSatish Balay "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL}; 282cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL}; 2960c22052SBarry Smith 30a4121054SBarry Smith /*@ 31bb7acecfSBarry Smith DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the 32bb7acecfSBarry Smith algebraic solvers, time integrators, and optimization algorithms. 33a4121054SBarry Smith 34d083f849SBarry Smith Collective 35a4121054SBarry Smith 36a4121054SBarry Smith Input Parameter: 37bb7acecfSBarry Smith . comm - The communicator for the `DM` object 38a4121054SBarry Smith 39a4121054SBarry Smith Output Parameter: 40bb7acecfSBarry Smith . dm - The `DM` object 41a4121054SBarry Smith 42a4121054SBarry Smith Level: beginner 43a4121054SBarry Smith 44bb7acecfSBarry Smith Notes: 45bb7acecfSBarry Smith See `DMType` for a brief summary of available `DM`. 46bb7acecfSBarry Smith 47bb7acecfSBarry Smith The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an 48bb7acecfSBarry Smith error when you try to use the dm. 49bb7acecfSBarry Smith 501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK` 51a4121054SBarry Smith @*/ 52d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm) 53d71ae5a4SJacob Faibussowitsch { 54a4121054SBarry Smith DM v; 55e5e52638SMatthew G. Knepley PetscDS ds; 56a4121054SBarry Smith 57a4121054SBarry Smith PetscFunctionBegin; 584f572ea9SToby Isaac PetscAssertPointer(dm, 2); 590298fd71SBarry Smith *dm = NULL; 609566063dSJacob Faibussowitsch PetscCall(DMInitializePackage()); 61a4121054SBarry Smith 629566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView)); 63e7c4fc90SDmitry Karpeev 6462e5d2d2SJDBetteridge ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences; 6562e5d2d2SJDBetteridge 6649be4549SMatthew G. Knepley v->setupcalled = PETSC_FALSE; 6749be4549SMatthew G. Knepley v->setfromoptionscalled = PETSC_FALSE; 680298fd71SBarry Smith v->ltogmap = NULL; 69a4ea9b21SRichard Tran Mills v->bind_below = 0; 701411c6eeSJed Brown v->bs = 1; 71171400e9SBarry Smith v->coloringtype = IS_COLORING_GLOBAL; 729566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, &v->sf)); 739566063dSJacob Faibussowitsch PetscCall(PetscSFCreate(comm, &v->sectionSF)); 74c58f1c22SToby Isaac v->labels = NULL; 7534aa8a36SMatthew G. Knepley v->adjacency[0] = PETSC_FALSE; 7634aa8a36SMatthew G. Knepley v->adjacency[1] = PETSC_TRUE; 77c58f1c22SToby Isaac v->depthLabel = NULL; 78ba2698f1SMatthew G. Knepley v->celltypeLabel = NULL; 791bb6d2a8SBarry Smith v->localSection = NULL; 801bb6d2a8SBarry Smith v->globalSection = NULL; 813b8ba7d1SJed Brown v->defaultConstraint.section = NULL; 823b8ba7d1SJed Brown v->defaultConstraint.mat = NULL; 8379769bd5SJed Brown v->defaultConstraint.bias = NULL; 846858538eSMatthew G. Knepley v->coordinates[0].dim = PETSC_DEFAULT; 856858538eSMatthew G. Knepley v->coordinates[1].dim = PETSC_DEFAULT; 866858538eSMatthew G. Knepley v->sparseLocalize = PETSC_TRUE; 8796173672SStefano Zampini v->dim = PETSC_DETERMINE; 88435a35e8SMatthew G Knepley { 89435a35e8SMatthew G Knepley PetscInt i; 90435a35e8SMatthew G Knepley for (i = 0; i < 10; ++i) { 910298fd71SBarry Smith v->nullspaceConstructors[i] = NULL; 92f9d4088aSMatthew G. Knepley v->nearnullspaceConstructors[i] = NULL; 93435a35e8SMatthew G Knepley } 94435a35e8SMatthew G Knepley } 959566063dSJacob Faibussowitsch PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds)); 9607218a29SMatthew G. Knepley PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL)); 979566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&ds)); 989566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxCreate(&v->auxData)); 9914f150ffSMatthew G. Knepley v->dmBC = NULL; 100a8fb8f29SToby Isaac v->coarseMesh = NULL; 101f4d763aaSMatthew G. Knepley v->outputSequenceNum = -1; 102cdb7a50dSMatthew G. Knepley v->outputSequenceVal = 0.0; 1039566063dSJacob Faibussowitsch PetscCall(DMSetVecType(v, VECSTANDARD)); 1049566063dSJacob Faibussowitsch PetscCall(DMSetMatType(v, MATAIJ)); 1054a7a4c06SLawrence Mitchell 1061411c6eeSJed Brown *dm = v; 1073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 108a4121054SBarry Smith } 109a4121054SBarry Smith 11038221697SMatthew G. Knepley /*@ 111bb7acecfSBarry Smith DMClone - Creates a `DM` object with the same topology as the original. 11238221697SMatthew G. Knepley 113d083f849SBarry Smith Collective 11438221697SMatthew G. Knepley 11538221697SMatthew G. Knepley Input Parameter: 116bb7acecfSBarry Smith . dm - The original `DM` object 11738221697SMatthew G. Knepley 11838221697SMatthew G. Knepley Output Parameter: 119bb7acecfSBarry Smith . newdm - The new `DM` object 12038221697SMatthew G. Knepley 12138221697SMatthew G. Knepley Level: beginner 12238221697SMatthew G. Knepley 1231cb8cacdSPatrick Sanan Notes: 124bb7acecfSBarry Smith For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example, 125bb7acecfSBarry Smith `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not 126bb7acecfSBarry Smith share the `PetscSection` of the original `DM`. 1271bb6d2a8SBarry Smith 128bb7acecfSBarry Smith The clone is considered set up if the original has been set up. 12989706ed2SPatrick Sanan 130bb7acecfSBarry Smith Use `DMConvert()` for a general way to create new `DM` from a given `DM` 131bb7acecfSBarry Smith 13260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()` 13338221697SMatthew G. Knepley @*/ 134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm) 135d71ae5a4SJacob Faibussowitsch { 13638221697SMatthew G. Knepley PetscSF sf; 13738221697SMatthew G. Knepley Vec coords; 13838221697SMatthew G. Knepley void *ctx; 1396858538eSMatthew G. Knepley PetscInt dim, cdim, i; 14038221697SMatthew G. Knepley 14138221697SMatthew G. Knepley PetscFunctionBegin; 14238221697SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1434f572ea9SToby Isaac PetscAssertPointer(newdm, 2); 1449566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm)); 1459566063dSJacob Faibussowitsch PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL)); 146ddf8437dSMatthew G. Knepley (*newdm)->leveldown = dm->leveldown; 147ddf8437dSMatthew G. Knepley (*newdm)->levelup = dm->levelup; 148c8a6034eSMark (*newdm)->prealloc_only = dm->prealloc_only; 149fc214432SJed Brown (*newdm)->prealloc_skip = dm->prealloc_skip; 1509566063dSJacob Faibussowitsch PetscCall(PetscFree((*newdm)->vectype)); 1519566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype)); 1529566063dSJacob Faibussowitsch PetscCall(PetscFree((*newdm)->mattype)); 1539566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype)); 1549566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 1559566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*newdm, dim)); 156dbbe0bcdSBarry Smith PetscTryTypeMethod(dm, clone, newdm); 1573f22bcbcSToby Isaac (*newdm)->setupcalled = dm->setupcalled; 1589566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 1599566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(*newdm, sf)); 1609566063dSJacob Faibussowitsch PetscCall(DMGetApplicationContext(dm, &ctx)); 1619566063dSJacob Faibussowitsch PetscCall(DMSetApplicationContext(*newdm, ctx)); 1626858538eSMatthew G. Knepley for (i = 0; i < 2; ++i) { 1636858538eSMatthew G. Knepley if (dm->coordinates[i].dm) { 164be4c1c3eSMatthew G. Knepley DM ncdm; 165be4c1c3eSMatthew G. Knepley PetscSection cs; 1665a0206caSToby Isaac PetscInt pEnd = -1, pEndMax = -1; 167be4c1c3eSMatthew G. Knepley 1686858538eSMatthew G. Knepley PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs)); 1699566063dSJacob Faibussowitsch if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd)); 170712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm))); 1715a0206caSToby Isaac if (pEndMax >= 0) { 1726858538eSMatthew G. Knepley PetscCall(DMClone(dm->coordinates[i].dm, &ncdm)); 1736858538eSMatthew G. Knepley PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm)); 1749566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(ncdm, cs)); 1756858538eSMatthew G. Knepley if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm)); 1766858538eSMatthew G. Knepley else PetscCall(DMSetCoordinateDM(*newdm, ncdm)); 1779566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ncdm)); 178be4c1c3eSMatthew G. Knepley } 179be4c1c3eSMatthew G. Knepley } 1806858538eSMatthew G. Knepley } 1819566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 1829566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(*newdm, cdim)); 1839566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coords)); 18438221697SMatthew G. Knepley if (coords) { 1859566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(*newdm, coords)); 18638221697SMatthew G. Knepley } else { 1879566063dSJacob Faibussowitsch PetscCall(DMGetCoordinates(dm, &coords)); 1889566063dSJacob Faibussowitsch if (coords) PetscCall(DMSetCoordinates(*newdm, coords)); 18938221697SMatthew G. Knepley } 1906858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dm, &coords)); 1916858538eSMatthew G. Knepley if (coords) { 1926858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(*newdm, coords)); 1936858538eSMatthew G. Knepley } else { 1946858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinates(dm, &coords)); 1956858538eSMatthew G. Knepley if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords)); 1966858538eSMatthew G. Knepley } 19790b157c4SStefano Zampini { 1984fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 1996858538eSMatthew G. Knepley 2004fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L)); 2014fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L)); 202c6b900c6SMatthew G. Knepley } 20334aa8a36SMatthew G. Knepley { 20434aa8a36SMatthew G. Knepley PetscBool useCone, useClosure; 20534aa8a36SMatthew G. Knepley 2069566063dSJacob Faibussowitsch PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure)); 2079566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure)); 20834aa8a36SMatthew G. Knepley } 2093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21038221697SMatthew G. Knepley } 21138221697SMatthew G. Knepley 2129a42bb27SBarry Smith /*@C 213bb7acecfSBarry Smith DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()` 2149a42bb27SBarry Smith 21520f4b53cSBarry Smith Logically Collective 2169a42bb27SBarry Smith 217147403d9SBarry Smith Input Parameters: 2189a42bb27SBarry Smith + da - initial distributed array 219bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL` 2209a42bb27SBarry Smith 22120f4b53cSBarry Smith Options Database Key: 222147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create 2239a42bb27SBarry Smith 2249a42bb27SBarry Smith Level: intermediate 2259a42bb27SBarry Smith 22660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`, 227bb7acecfSBarry Smith `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()` 2289a42bb27SBarry Smith @*/ 229d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype) 230d71ae5a4SJacob Faibussowitsch { 2319a42bb27SBarry Smith PetscFunctionBegin; 2329a42bb27SBarry Smith PetscValidHeaderSpecific(da, DM_CLASSID, 1); 2339566063dSJacob Faibussowitsch PetscCall(PetscFree(da->vectype)); 2349566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype)); 2353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2369a42bb27SBarry Smith } 2379a42bb27SBarry Smith 238c0dedaeaSBarry Smith /*@C 239bb7acecfSBarry Smith DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()` 240c0dedaeaSBarry Smith 24120f4b53cSBarry Smith Logically Collective 242c0dedaeaSBarry Smith 243c0dedaeaSBarry Smith Input Parameter: 244c0dedaeaSBarry Smith . da - initial distributed array 245c0dedaeaSBarry Smith 246c0dedaeaSBarry Smith Output Parameter: 247c0dedaeaSBarry Smith . ctype - the vector type 248c0dedaeaSBarry Smith 249c0dedaeaSBarry Smith Level: intermediate 250c0dedaeaSBarry Smith 25160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()` 252c0dedaeaSBarry Smith @*/ 253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype) 254d71ae5a4SJacob Faibussowitsch { 255c0dedaeaSBarry Smith PetscFunctionBegin; 256c0dedaeaSBarry Smith PetscValidHeaderSpecific(da, DM_CLASSID, 1); 257c0dedaeaSBarry Smith *ctype = da->vectype; 2583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 259c0dedaeaSBarry Smith } 260c0dedaeaSBarry Smith 2615f1ad066SMatthew G Knepley /*@ 262bb7acecfSBarry Smith VecGetDM - Gets the `DM` defining the data layout of the vector 2635f1ad066SMatthew G Knepley 26420f4b53cSBarry Smith Not Collective 2655f1ad066SMatthew G Knepley 2665f1ad066SMatthew G Knepley Input Parameter: 267bb7acecfSBarry Smith . v - The `Vec` 2685f1ad066SMatthew G Knepley 2695f1ad066SMatthew G Knepley Output Parameter: 270bb7acecfSBarry Smith . dm - The `DM` 2715f1ad066SMatthew G Knepley 2725f1ad066SMatthew G Knepley Level: intermediate 2735f1ad066SMatthew G Knepley 274bb7acecfSBarry Smith Note: 275bb7acecfSBarry Smith A `Vec` may not have a `DM` associated with it. 276bb7acecfSBarry Smith 2771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()` 2785f1ad066SMatthew G Knepley @*/ 279d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm) 280d71ae5a4SJacob Faibussowitsch { 2815f1ad066SMatthew G Knepley PetscFunctionBegin; 2825f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v, VEC_CLASSID, 1); 2834f572ea9SToby Isaac PetscAssertPointer(dm, 2); 2849566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm)); 2853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2865f1ad066SMatthew G Knepley } 2875f1ad066SMatthew G Knepley 2885f1ad066SMatthew G Knepley /*@ 289bb7acecfSBarry Smith VecSetDM - Sets the `DM` defining the data layout of the vector. 2905f1ad066SMatthew G Knepley 29120f4b53cSBarry Smith Not Collective 2925f1ad066SMatthew G Knepley 2935f1ad066SMatthew G Knepley Input Parameters: 294bb7acecfSBarry Smith + v - The `Vec` 295bb7acecfSBarry Smith - dm - The `DM` 2965f1ad066SMatthew G Knepley 29720f4b53cSBarry Smith Level: developer 29820f4b53cSBarry Smith 299bb7acecfSBarry Smith Note: 300bb7acecfSBarry Smith This is rarely used, generally one uses `DMGetLocalVector()` or `DMGetGlobalVector()` to create a vector associated with a given `DM` 301d9805387SMatthew G. Knepley 302bb7acecfSBarry Smith This is NOT the same as `DMCreateGlobalVector()` since it does not change the view methods or perform other customization, but merely sets the `DM` member. 303bb7acecfSBarry Smith 3041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()` 3055f1ad066SMatthew G Knepley @*/ 306d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm) 307d71ae5a4SJacob Faibussowitsch { 3085f1ad066SMatthew G Knepley PetscFunctionBegin; 3095f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v, VEC_CLASSID, 1); 310d7f50e27SLisandro Dalcin if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 3119566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm)); 3123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3135f1ad066SMatthew G Knepley } 3145f1ad066SMatthew G Knepley 315521d9a4cSLisandro Dalcin /*@C 316bb7acecfSBarry Smith DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM` 3178f1509bcSBarry Smith 31820f4b53cSBarry Smith Logically Collective 3198f1509bcSBarry Smith 3208f1509bcSBarry Smith Input Parameters: 321bb7acecfSBarry Smith + dm - the `DM` context 3228f1509bcSBarry Smith - ctype - the matrix type 3238f1509bcSBarry Smith 32420f4b53cSBarry Smith Options Database Key: 3258f1509bcSBarry Smith . -dm_is_coloring_type - global or local 3268f1509bcSBarry Smith 3278f1509bcSBarry Smith Level: intermediate 3288f1509bcSBarry Smith 3291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, 330bb7acecfSBarry Smith `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL` 3318f1509bcSBarry Smith @*/ 332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype) 333d71ae5a4SJacob Faibussowitsch { 3348f1509bcSBarry Smith PetscFunctionBegin; 3358f1509bcSBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3368f1509bcSBarry Smith dm->coloringtype = ctype; 3373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3388f1509bcSBarry Smith } 3398f1509bcSBarry Smith 3408f1509bcSBarry Smith /*@C 341bb7acecfSBarry Smith DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM` 342521d9a4cSLisandro Dalcin 34320f4b53cSBarry Smith Logically Collective 344521d9a4cSLisandro Dalcin 345521d9a4cSLisandro Dalcin Input Parameter: 346bb7acecfSBarry Smith . dm - the `DM` context 3478f1509bcSBarry Smith 3488f1509bcSBarry Smith Output Parameter: 3498f1509bcSBarry Smith . ctype - the matrix type 3508f1509bcSBarry Smith 35120f4b53cSBarry Smith Options Database Key: 3528f1509bcSBarry Smith . -dm_is_coloring_type - global or local 3538f1509bcSBarry Smith 3548f1509bcSBarry Smith Level: intermediate 3558f1509bcSBarry Smith 3561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, 35742747ad1SJacob Faibussowitsch `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL` 3588f1509bcSBarry Smith @*/ 359d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype) 360d71ae5a4SJacob Faibussowitsch { 3618f1509bcSBarry Smith PetscFunctionBegin; 3628f1509bcSBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3638f1509bcSBarry Smith *ctype = dm->coloringtype; 3643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3658f1509bcSBarry Smith } 3668f1509bcSBarry Smith 3678f1509bcSBarry Smith /*@C 368bb7acecfSBarry Smith DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()` 3698f1509bcSBarry Smith 37020f4b53cSBarry Smith Logically Collective 3718f1509bcSBarry Smith 3728f1509bcSBarry Smith Input Parameters: 373bb7acecfSBarry Smith + dm - the `DM` context 374bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ` 375521d9a4cSLisandro Dalcin 37620f4b53cSBarry Smith Options Database Key: 377bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij 378521d9a4cSLisandro Dalcin 379521d9a4cSLisandro Dalcin Level: intermediate 380521d9a4cSLisandro Dalcin 38142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()` 382521d9a4cSLisandro Dalcin @*/ 383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype) 384d71ae5a4SJacob Faibussowitsch { 385521d9a4cSLisandro Dalcin PetscFunctionBegin; 386521d9a4cSLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3879566063dSJacob Faibussowitsch PetscCall(PetscFree(dm->mattype)); 3889566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype)); 3893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 390521d9a4cSLisandro Dalcin } 391521d9a4cSLisandro Dalcin 392c0dedaeaSBarry Smith /*@C 39320f4b53cSBarry Smith DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()` 394c0dedaeaSBarry Smith 39520f4b53cSBarry Smith Logically Collective 396c0dedaeaSBarry Smith 397c0dedaeaSBarry Smith Input Parameter: 398bb7acecfSBarry Smith . dm - the `DM` context 399c0dedaeaSBarry Smith 400c0dedaeaSBarry Smith Output Parameter: 401c0dedaeaSBarry Smith . ctype - the matrix type 402c0dedaeaSBarry Smith 403c0dedaeaSBarry Smith Level: intermediate 404c0dedaeaSBarry Smith 40542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()` 406c0dedaeaSBarry Smith @*/ 407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype) 408d71ae5a4SJacob Faibussowitsch { 409c0dedaeaSBarry Smith PetscFunctionBegin; 410c0dedaeaSBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 411c0dedaeaSBarry Smith *ctype = dm->mattype; 4123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 413c0dedaeaSBarry Smith } 414c0dedaeaSBarry Smith 415c688c046SMatthew G Knepley /*@ 416bb7acecfSBarry Smith MatGetDM - Gets the `DM` defining the data layout of the matrix 417c688c046SMatthew G Knepley 41820f4b53cSBarry Smith Not Collective 419c688c046SMatthew G Knepley 420c688c046SMatthew G Knepley Input Parameter: 421bb7acecfSBarry Smith . A - The `Mat` 422c688c046SMatthew G Knepley 423c688c046SMatthew G Knepley Output Parameter: 424bb7acecfSBarry Smith . dm - The `DM` 425c688c046SMatthew G Knepley 426c688c046SMatthew G Knepley Level: intermediate 427c688c046SMatthew G Knepley 428bb7acecfSBarry Smith Note: 429bb7acecfSBarry Smith A matrix may not have a `DM` associated with it 430bb7acecfSBarry Smith 43160225df5SJacob Faibussowitsch Developer Notes: 432bb7acecfSBarry Smith Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation 4338f1509bcSBarry Smith 4341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()` 435c688c046SMatthew G Knepley @*/ 436d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm) 437d71ae5a4SJacob Faibussowitsch { 438c688c046SMatthew G Knepley PetscFunctionBegin; 439c688c046SMatthew G Knepley PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 4404f572ea9SToby Isaac PetscAssertPointer(dm, 2); 4419566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm)); 4423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 443c688c046SMatthew G Knepley } 444c688c046SMatthew G Knepley 445c688c046SMatthew G Knepley /*@ 446bb7acecfSBarry Smith MatSetDM - Sets the `DM` defining the data layout of the matrix 447c688c046SMatthew G Knepley 44820f4b53cSBarry Smith Not Collective 449c688c046SMatthew G Knepley 450c688c046SMatthew G Knepley Input Parameters: 45120f4b53cSBarry Smith + A - The `Mat` 45220f4b53cSBarry Smith - dm - The `DM` 453c688c046SMatthew G Knepley 454bb7acecfSBarry Smith Level: developer 455c688c046SMatthew G Knepley 456bb7acecfSBarry Smith Note: 457bb7acecfSBarry Smith This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM` 458bb7acecfSBarry Smith 45960225df5SJacob Faibussowitsch Developer Notes: 460bb7acecfSBarry Smith Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with 461bb7acecfSBarry Smith the `Mat` through a `PetscObjectCompose()` operation 4628f1509bcSBarry Smith 4631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()` 464c688c046SMatthew G Knepley @*/ 465d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm) 466d71ae5a4SJacob Faibussowitsch { 467c688c046SMatthew G Knepley PetscFunctionBegin; 468c688c046SMatthew G Knepley PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 4698865f1eaSKarl Rupp if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 4709566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm)); 4713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 472c688c046SMatthew G Knepley } 473c688c046SMatthew G Knepley 4749a42bb27SBarry Smith /*@C 475bb7acecfSBarry Smith DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database 4769a42bb27SBarry Smith 47720f4b53cSBarry Smith Logically Collective 4789a42bb27SBarry Smith 479d8d19677SJose E. Roman Input Parameters: 48060225df5SJacob Faibussowitsch + dm - the `DM` context 481bb7acecfSBarry Smith - prefix - the prefix to prepend 4829a42bb27SBarry Smith 48320f4b53cSBarry Smith Level: advanced 48420f4b53cSBarry Smith 48520f4b53cSBarry Smith Note: 4869a42bb27SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4879a42bb27SBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 4889a42bb27SBarry Smith 4891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()` 4909a42bb27SBarry Smith @*/ 491d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[]) 492d71ae5a4SJacob Faibussowitsch { 4939a42bb27SBarry Smith PetscFunctionBegin; 4949a42bb27SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4959566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix)); 4961baa6e33SBarry Smith if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix)); 4971baa6e33SBarry Smith if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix)); 4983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4999a42bb27SBarry Smith } 5009a42bb27SBarry Smith 50131697293SDave May /*@C 502da81f932SPierre Jolivet DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for 503bb7acecfSBarry Smith `DM` options in the options database. 50431697293SDave May 50520f4b53cSBarry Smith Logically Collective 50631697293SDave May 50731697293SDave May Input Parameters: 508bb7acecfSBarry Smith + dm - the `DM` context 509bb7acecfSBarry Smith - prefix - the string to append to the current prefix 51031697293SDave May 51120f4b53cSBarry Smith Level: advanced 51220f4b53cSBarry Smith 51320f4b53cSBarry Smith Note: 514bb7acecfSBarry Smith If the `DM` does not currently have an options prefix then this value is used alone as the prefix as if `DMSetOptionsPrefix()` had been called. 51531697293SDave May A hyphen (-) must NOT be given at the beginning of the prefix name. 51631697293SDave May The first character of all runtime options is AUTOMATICALLY the hyphen. 51731697293SDave May 5181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()` 51931697293SDave May @*/ 520d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[]) 521d71ae5a4SJacob Faibussowitsch { 52231697293SDave May PetscFunctionBegin; 52331697293SDave May PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5249566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix)); 5253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52631697293SDave May } 52731697293SDave May 52831697293SDave May /*@C 52931697293SDave May DMGetOptionsPrefix - Gets the prefix used for searching for all 530bb7acecfSBarry Smith DM options in the options database. 53131697293SDave May 53231697293SDave May Not Collective 53331697293SDave May 5342fe279fdSBarry Smith Input Parameter: 535bb7acecfSBarry Smith . dm - the `DM` context 53631697293SDave May 5372fe279fdSBarry Smith Output Parameter: 53831697293SDave May . prefix - pointer to the prefix string used is returned 53931697293SDave May 54031697293SDave May Level: advanced 54131697293SDave May 54260225df5SJacob Faibussowitsch Fortran Notes: 54320f4b53cSBarry Smith Pass in a string 'prefix' of 54420f4b53cSBarry Smith sufficient length to hold the prefix. 54520f4b53cSBarry Smith 5461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()` 54731697293SDave May @*/ 548d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[]) 549d71ae5a4SJacob Faibussowitsch { 55031697293SDave May PetscFunctionBegin; 55131697293SDave May PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix)); 5533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55431697293SDave May } 55531697293SDave May 55662e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct) 557d71ae5a4SJacob Faibussowitsch { 5586eb26441SStefano Zampini PetscInt refct = ((PetscObject)dm)->refct; 55988bdff64SToby Isaac 56088bdff64SToby Isaac PetscFunctionBegin; 561aab5bcd8SJed Brown *ncrefct = 0; 56288bdff64SToby Isaac if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) { 56388bdff64SToby Isaac refct--; 56488bdff64SToby Isaac if (recurseCoarse) { 56588bdff64SToby Isaac PetscInt coarseCount; 56688bdff64SToby Isaac 56762e5d2d2SJDBetteridge PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount)); 56888bdff64SToby Isaac refct += coarseCount; 56988bdff64SToby Isaac } 57088bdff64SToby Isaac } 57188bdff64SToby Isaac if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) { 57288bdff64SToby Isaac refct--; 57388bdff64SToby Isaac if (recurseFine) { 57488bdff64SToby Isaac PetscInt fineCount; 57588bdff64SToby Isaac 57662e5d2d2SJDBetteridge PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount)); 57788bdff64SToby Isaac refct += fineCount; 57888bdff64SToby Isaac } 57988bdff64SToby Isaac } 58088bdff64SToby Isaac *ncrefct = refct; 5813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58288bdff64SToby Isaac } 58388bdff64SToby Isaac 58462e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */ 58562e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct) 58662e5d2d2SJDBetteridge { 58762e5d2d2SJDBetteridge PetscFunctionBegin; 58862e5d2d2SJDBetteridge PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct)); 5893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 59062e5d2d2SJDBetteridge } 59162e5d2d2SJDBetteridge 592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm) 593d71ae5a4SJacob Faibussowitsch { 5945d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 595354557abSToby Isaac 596354557abSToby Isaac PetscFunctionBegin; 597354557abSToby Isaac /* destroy the labels */ 598354557abSToby Isaac while (next) { 599354557abSToby Isaac DMLabelLink tmp = next->next; 600354557abSToby Isaac 6015d80c0bfSVaclav Hapla if (next->label == dm->depthLabel) dm->depthLabel = NULL; 602ba2698f1SMatthew G. Knepley if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL; 6039566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&next->label)); 6049566063dSJacob Faibussowitsch PetscCall(PetscFree(next)); 605354557abSToby Isaac next = tmp; 606354557abSToby Isaac } 6075d80c0bfSVaclav Hapla dm->labels = NULL; 6083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 609354557abSToby Isaac } 610354557abSToby Isaac 61166976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c) 612d71ae5a4SJacob Faibussowitsch { 6136858538eSMatthew G. Knepley PetscFunctionBegin; 6146858538eSMatthew G. Knepley c->dim = PETSC_DEFAULT; 6156858538eSMatthew G. Knepley PetscCall(DMDestroy(&c->dm)); 6166858538eSMatthew G. Knepley PetscCall(VecDestroy(&c->x)); 6176858538eSMatthew G. Knepley PetscCall(VecDestroy(&c->xl)); 6186858538eSMatthew G. Knepley PetscCall(DMFieldDestroy(&c->field)); 6193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6206858538eSMatthew G. Knepley } 6216858538eSMatthew G. Knepley 6221fb7b255SJunchao Zhang /*@C 623bb7acecfSBarry Smith DMDestroy - Destroys a `DM`. 62447c6ae99SBarry Smith 62520f4b53cSBarry Smith Collective 62647c6ae99SBarry Smith 62747c6ae99SBarry Smith Input Parameter: 628bb7acecfSBarry Smith . dm - the `DM` object to destroy 62947c6ae99SBarry Smith 63047c6ae99SBarry Smith Level: developer 63147c6ae99SBarry Smith 6321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()` 63347c6ae99SBarry Smith @*/ 634d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm) 635d71ae5a4SJacob Faibussowitsch { 6366eb26441SStefano Zampini PetscInt cnt; 63747c6ae99SBarry Smith 63847c6ae99SBarry Smith PetscFunctionBegin; 6393ba16761SJacob Faibussowitsch if (!*dm) PetscFunctionReturn(PETSC_SUCCESS); 6406bf464f9SBarry Smith PetscValidHeaderSpecific((*dm), DM_CLASSID, 1); 64187e657c6SBarry Smith 64288bdff64SToby Isaac /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */ 64362e5d2d2SJDBetteridge PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt)); 64488bdff64SToby Isaac --((PetscObject)(*dm))->refct; 6459371c9d4SSatish Balay if (--cnt > 0) { 6469371c9d4SSatish Balay *dm = NULL; 6473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6489371c9d4SSatish Balay } 6493ba16761SJacob Faibussowitsch if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS); 6506bf464f9SBarry Smith ((PetscObject)(*dm))->refct = 0; 6516eb26441SStefano Zampini 6529566063dSJacob Faibussowitsch PetscCall(DMClearGlobalVectors(*dm)); 6539566063dSJacob Faibussowitsch PetscCall(DMClearLocalVectors(*dm)); 654974ca4ecSStefano Zampini PetscCall(DMClearNamedGlobalVectors(*dm)); 655974ca4ecSStefano Zampini PetscCall(DMClearNamedLocalVectors(*dm)); 6562348bcf4SPeter Brune 657b17ce1afSJed Brown /* Destroy the list of hooks */ 658c833c3b5SJed Brown { 659c833c3b5SJed Brown DMCoarsenHookLink link, next; 660b17ce1afSJed Brown for (link = (*dm)->coarsenhook; link; link = next) { 661b17ce1afSJed Brown next = link->next; 6629566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 663b17ce1afSJed Brown } 6640298fd71SBarry Smith (*dm)->coarsenhook = NULL; 665c833c3b5SJed Brown } 666c833c3b5SJed Brown { 667c833c3b5SJed Brown DMRefineHookLink link, next; 668c833c3b5SJed Brown for (link = (*dm)->refinehook; link; link = next) { 669c833c3b5SJed Brown next = link->next; 6709566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 671c833c3b5SJed Brown } 6720298fd71SBarry Smith (*dm)->refinehook = NULL; 673c833c3b5SJed Brown } 674be081cd6SPeter Brune { 675be081cd6SPeter Brune DMSubDomainHookLink link, next; 676be081cd6SPeter Brune for (link = (*dm)->subdomainhook; link; link = next) { 677be081cd6SPeter Brune next = link->next; 6789566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 679be081cd6SPeter Brune } 6800298fd71SBarry Smith (*dm)->subdomainhook = NULL; 681be081cd6SPeter Brune } 682baf369e7SPeter Brune { 683baf369e7SPeter Brune DMGlobalToLocalHookLink link, next; 684baf369e7SPeter Brune for (link = (*dm)->gtolhook; link; link = next) { 685baf369e7SPeter Brune next = link->next; 6869566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 687baf369e7SPeter Brune } 6880298fd71SBarry Smith (*dm)->gtolhook = NULL; 689baf369e7SPeter Brune } 690d4d07f1eSToby Isaac { 691d4d07f1eSToby Isaac DMLocalToGlobalHookLink link, next; 692d4d07f1eSToby Isaac for (link = (*dm)->ltoghook; link; link = next) { 693d4d07f1eSToby Isaac next = link->next; 6949566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 695d4d07f1eSToby Isaac } 696d4d07f1eSToby Isaac (*dm)->ltoghook = NULL; 697d4d07f1eSToby Isaac } 698aa1993deSMatthew G Knepley /* Destroy the work arrays */ 699aa1993deSMatthew G Knepley { 700aa1993deSMatthew G Knepley DMWorkLink link, next; 701936381afSPierre Jolivet PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out %p %p", (void *)(*dm)->workout, (void *)(*dm)->workout->mem); 702aa1993deSMatthew G Knepley for (link = (*dm)->workin; link; link = next) { 703aa1993deSMatthew G Knepley next = link->next; 7049566063dSJacob Faibussowitsch PetscCall(PetscFree(link->mem)); 7059566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 706aa1993deSMatthew G Knepley } 7070298fd71SBarry Smith (*dm)->workin = NULL; 708aa1993deSMatthew G Knepley } 709c58f1c22SToby Isaac /* destroy the labels */ 7109566063dSJacob Faibussowitsch PetscCall(DMDestroyLabelLinkList_Internal(*dm)); 711f4cdcedcSVaclav Hapla /* destroy the fields */ 7129566063dSJacob Faibussowitsch PetscCall(DMClearFields(*dm)); 713f4cdcedcSVaclav Hapla /* destroy the boundaries */ 714e6f8dbb6SToby Isaac { 715e6f8dbb6SToby Isaac DMBoundary next = (*dm)->boundary; 716e6f8dbb6SToby Isaac while (next) { 717e6f8dbb6SToby Isaac DMBoundary b = next; 718e6f8dbb6SToby Isaac 719e6f8dbb6SToby Isaac next = b->next; 7209566063dSJacob Faibussowitsch PetscCall(PetscFree(b)); 721e6f8dbb6SToby Isaac } 722e6f8dbb6SToby Isaac } 723b17ce1afSJed Brown 7249566063dSJacob Faibussowitsch PetscCall(PetscObjectDestroy(&(*dm)->dmksp)); 7259566063dSJacob Faibussowitsch PetscCall(PetscObjectDestroy(&(*dm)->dmsnes)); 7269566063dSJacob Faibussowitsch PetscCall(PetscObjectDestroy(&(*dm)->dmts)); 72752536dc3SBarry Smith 72848a46eb9SPierre Jolivet if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx)); 7299566063dSJacob Faibussowitsch PetscCall(MatFDColoringDestroy(&(*dm)->fd)); 7309566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap)); 7319566063dSJacob Faibussowitsch PetscCall(PetscFree((*dm)->vectype)); 7329566063dSJacob Faibussowitsch PetscCall(PetscFree((*dm)->mattype)); 73388ed4aceSMatthew G Knepley 7349566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*dm)->localSection)); 7359566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*dm)->globalSection)); 7369566063dSJacob Faibussowitsch PetscCall(PetscLayoutDestroy(&(*dm)->map)); 7379566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section)); 7389566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat)); 7399566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&(*dm)->sf)); 7409566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&(*dm)->sectionSF)); 741736995cdSBlaise Bourdin if ((*dm)->useNatural) { 74248a46eb9SPierre Jolivet if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural)); 7439566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration)); 744736995cdSBlaise Bourdin } 7459a2a23afSMatthew G. Knepley { 7469a2a23afSMatthew G. Knepley Vec *auxData; 7479a2a23afSMatthew G. Knepley PetscInt n, i, off = 0; 7489a2a23afSMatthew G. Knepley 7499566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n)); 7509566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &auxData)); 7519566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData)); 7529566063dSJacob Faibussowitsch for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i])); 7539566063dSJacob Faibussowitsch PetscCall(PetscFree(auxData)); 7549566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData)); 7559a2a23afSMatthew G. Knepley } 75648a46eb9SPierre Jolivet if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL)); 7576eb26441SStefano Zampini 7589566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*dm)->coarseMesh)); 75948a46eb9SPierre Jolivet if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL)); 7609566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*dm)->fineMesh)); 7614fb89dddSMatthew G. Knepley PetscCall(PetscFree((*dm)->Lstart)); 7629566063dSJacob Faibussowitsch PetscCall(PetscFree((*dm)->L)); 7639566063dSJacob Faibussowitsch PetscCall(PetscFree((*dm)->maxCell)); 7646858538eSMatthew G. Knepley PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0])); 7656858538eSMatthew G. Knepley PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1])); 7669566063dSJacob Faibussowitsch if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx)); 7679566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*dm)->transformDM)); 7689566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*dm)->transform)); 7696725e60dSJed Brown PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local)); 7706725e60dSJed Brown PetscCall(VecDestroy(&(*dm)->periodic.affine)); 7716636e97aSMatthew G Knepley 7729566063dSJacob Faibussowitsch PetscCall(DMClearDS(*dm)); 7739566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*dm)->dmBC)); 774e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 7759566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm)); 776732e2eb9SMatthew G Knepley 77748a46eb9SPierre Jolivet if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm)); 7789566063dSJacob Faibussowitsch PetscCall(DMMonitorCancel(*dm)); 779d2b2dc1eSMatthew G. Knepley PetscCall(DMCeedDestroy(&(*dm)->dmceed)); 780f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED 7819566063dSJacob Faibussowitsch PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict)); 7829566063dSJacob Faibussowitsch PetscCallCEED(CeedDestroy(&(*dm)->ceed)); 783f918ec44SMatthew G. Knepley #endif 784435a35e8SMatthew G Knepley /* We do not destroy (*dm)->data here so that we can reference count backend objects */ 7859566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(dm)); 7863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 78747c6ae99SBarry Smith } 78847c6ae99SBarry Smith 789d7bf68aeSBarry Smith /*@ 790bb7acecfSBarry Smith DMSetUp - sets up the data structures inside a `DM` object 791d7bf68aeSBarry Smith 79220f4b53cSBarry Smith Collective 793d7bf68aeSBarry Smith 794d7bf68aeSBarry Smith Input Parameter: 795bb7acecfSBarry Smith . dm - the `DM` object to setup 796d7bf68aeSBarry Smith 797bb7acecfSBarry Smith Level: intermediate 798d7bf68aeSBarry Smith 799bb7acecfSBarry Smith Note: 800bb7acecfSBarry Smith This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM` 801bb7acecfSBarry Smith 8021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()` 803d7bf68aeSBarry Smith @*/ 804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm) 805d71ae5a4SJacob Faibussowitsch { 806d7bf68aeSBarry Smith PetscFunctionBegin; 807171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8083ba16761SJacob Faibussowitsch if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 809dbbe0bcdSBarry Smith PetscTryTypeMethod(dm, setup); 8108387afaaSJed Brown dm->setupcalled = PETSC_TRUE; 8113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 812d7bf68aeSBarry Smith } 813d7bf68aeSBarry Smith 814d7bf68aeSBarry Smith /*@ 815bb7acecfSBarry Smith DMSetFromOptions - sets parameters in a `DM` from the options database 816d7bf68aeSBarry Smith 81720f4b53cSBarry Smith Collective 818d7bf68aeSBarry Smith 819d7bf68aeSBarry Smith Input Parameter: 820bb7acecfSBarry Smith . dm - the `DM` object to set options for 821d7bf68aeSBarry Smith 82220f4b53cSBarry Smith Options Database Keys: 823bb7acecfSBarry Smith + -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros 824bb7acecfSBarry Smith . -dm_vec_type <type> - type of vector to create inside `DM` 825bb7acecfSBarry Smith . -dm_mat_type <type> - type of matrix to create inside `DM` 826a4ea9b21SRichard Tran Mills . -dm_is_coloring_type - <global or local> 82720f4b53cSBarry Smith . -dm_bind_below <n> - bind (force execution on CPU) for `Vec` and `Mat` objects with local size (number of vector entries or matrix rows) below n; currently only supported for `DMDA` 82820f4b53cSBarry Smith . -dm_plex_filename <str> - File containing a mesh 8299318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str> - File containing a mesh boundary 830cd7e8a5eSksagiyam . -dm_plex_name <str> - Name of the mesh in the file 8315dca41c3SJed Brown . -dm_plex_shape <shape> - The domain shape, such as `BOX`, `SPHERE`, etc. 8329318fe57SMatthew G. Knepley . -dm_plex_cell <ct> - Cell shape 8339318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain 8349318fe57SMatthew G. Knepley . -dm_plex_dim <dim> - Set the topological dimension 835bb7acecfSBarry Smith . -dm_plex_simplex <bool> - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements 836bb7acecfSBarry Smith . -dm_plex_interpolate <bool> - `PETSC_TRUE` turns on topological interpolation (creating edges and faces) 8379318fe57SMatthew G. Knepley . -dm_plex_scale <sc> - Scale factor for mesh coordinates 8389318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p> - Number of faces along each dimension 8399318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z> - Specify lower-left-bottom coordinates for the box 8409318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z> - Specify upper-right-top coordinates for the box 841bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz> - Specify the `DMBoundaryType` for each direction 8429318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r> - The sphere radius 8439318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r> - Radius of the ball 8449318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz> - Boundary type in the z direction 8459318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n> - Number of wedges around the cylinder 846bdf63967SMatthew G. Knepley . -dm_plex_reorder <order> - Reorder the mesh using the specified algorithm 8479318fe57SMatthew G. Knepley . -dm_refine_pre <n> - The number of refinements before distribution 8489318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool> - Flag for uniform refinement before distribution 8499318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v> - The maximum cell volume after refinement before distribution 8509318fe57SMatthew G. Knepley . -dm_refine <n> - The number of refinements after distribution 851bdf63967SMatthew G. Knepley . -dm_extrude <l> - Activate extrusion and specify the number of layers to extrude 852d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t> - The total thickness of extruded layers 853d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool> - Use tensor cells when extruding 854d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool> - Extrude layers symmetrically about the surface 855d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd> - Specify the extrusion direction 856d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer 857909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells - Flag to create finite volume ghost cells on the boundary 858909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary 8599318fe57SMatthew G. Knepley . -dm_distribute <bool> - Flag to redistribute a mesh among processes 8609318fe57SMatthew G. Knepley . -dm_distribute_overlap <n> - The size of the overlap halo 8619318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool> - Set adjacency direction 86220f4b53cSBarry Smith . -dm_plex_adj_closure <bool> - Set adjacency size 863d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool> - Use LibCEED as the FEM backend 86420f4b53cSBarry Smith . -dm_plex_check_symmetry - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()` 865bb7acecfSBarry Smith . -dm_plex_check_skeleton - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()` 866bb7acecfSBarry Smith . -dm_plex_check_faces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - `DMPlexCheckFaces()` 867bb7acecfSBarry Smith . -dm_plex_check_geometry - Check that cells have positive volume - `DMPlexCheckGeometry()` 868bb7acecfSBarry Smith . -dm_plex_check_pointsf - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()` 869bb7acecfSBarry Smith . -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()` 870384a6580SVaclav Hapla - -dm_plex_check_all - Perform all the checks above 871d7bf68aeSBarry Smith 87295eb5ee5SVaclav Hapla Level: intermediate 87395eb5ee5SVaclav Hapla 8741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, 875bb7acecfSBarry Smith `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`, 87660225df5SJacob Faibussowitsch `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA` 877d7bf68aeSBarry Smith @*/ 878d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm) 879d71ae5a4SJacob Faibussowitsch { 8807781c08eSBarry Smith char typeName[256]; 881ca266f36SBarry Smith PetscBool flg; 882d7bf68aeSBarry Smith 883d7bf68aeSBarry Smith PetscFunctionBegin; 884171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 88549be4549SMatthew G. Knepley dm->setfromoptionscalled = PETSC_TRUE; 8869566063dSJacob Faibussowitsch if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf)); 8879566063dSJacob Faibussowitsch if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF)); 888dd4c3f67SMatthew G. Knepley if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm)); 889d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)dm); 8909566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL)); 8919566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg)); 8921baa6e33SBarry Smith if (flg) PetscCall(DMSetVecType(dm, typeName)); 8939566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg)); 8941baa6e33SBarry Smith if (flg) PetscCall(DMSetMatType(dm, typeName)); 895863027abSJed Brown PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL)); 8969566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL)); 8979566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg)); 898dbbe0bcdSBarry Smith PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject); 899f9ba7244SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 900dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject)); 901d0609cedSBarry Smith PetscOptionsEnd(); 9023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 903d7bf68aeSBarry Smith } 904d7bf68aeSBarry Smith 905fc9bc008SSatish Balay /*@C 906bb7acecfSBarry Smith DMViewFromOptions - View a `DM` in a particular way based on a request in the options database 907fe2efc57SMark 90820f4b53cSBarry Smith Collective 909fe2efc57SMark 910fe2efc57SMark Input Parameters: 911bb7acecfSBarry Smith + dm - the `DM` object 91220f4b53cSBarry Smith . obj - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used) 91360225df5SJacob Faibussowitsch - name - option string that is used to activate viewing 914fe2efc57SMark 915fe2efc57SMark Level: intermediate 916bb7acecfSBarry Smith 917bb7acecfSBarry Smith Note: 918bb7acecfSBarry Smith See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed 919bb7acecfSBarry Smith 92060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()` 921fe2efc57SMark @*/ 922d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[]) 923d71ae5a4SJacob Faibussowitsch { 924fe2efc57SMark PetscFunctionBegin; 925fe2efc57SMark PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 9269566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name)); 9273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 928fe2efc57SMark } 929fe2efc57SMark 930fe2efc57SMark /*@C 931bb7acecfSBarry Smith DMView - Views a `DM`. Depending on the `PetscViewer` and its `PetscViewerFormat` it may print some ASCII information about the `DM` to the screen or a file or 932bb7acecfSBarry Smith save the `DM` in a binary file to be loaded later or create a visualization of the `DM` 93347c6ae99SBarry Smith 93420f4b53cSBarry Smith Collective 93547c6ae99SBarry Smith 936d8d19677SJose E. Roman Input Parameters: 937bb7acecfSBarry Smith + dm - the `DM` object to view 93847c6ae99SBarry Smith - v - the viewer 93947c6ae99SBarry Smith 94020f4b53cSBarry Smith Level: beginner 94120f4b53cSBarry Smith 942cd7e8a5eSksagiyam Notes: 943bb7acecfSBarry Smith Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX` 944bb7acecfSBarry Smith meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()` 945bb7acecfSBarry Smith before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object. 946cd7e8a5eSksagiyam 9471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()` 94847c6ae99SBarry Smith @*/ 949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v) 950d71ae5a4SJacob Faibussowitsch { 95132c0f0efSBarry Smith PetscBool isbinary; 95276a8abe0SBarry Smith PetscMPIInt size; 95376a8abe0SBarry Smith PetscViewerFormat format; 95447c6ae99SBarry Smith 95547c6ae99SBarry Smith PetscFunctionBegin; 956171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 95748a46eb9SPierre Jolivet if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v)); 958b1b135c8SBarry Smith PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2); 95974903a4fSStefano Zampini /* Ideally, we would like to have this test on. 96074903a4fSStefano Zampini However, it currently breaks socket viz via GLVis. 96174903a4fSStefano Zampini During DMView(parallel_mesh,glvis_viewer), each 96274903a4fSStefano Zampini process opens a sequential ASCII socket to visualize 96374903a4fSStefano Zampini the local mesh, and PetscObjectView(dm,local_socket) 96474903a4fSStefano Zampini is internally called inside VecView_GLVis, incurring 96574903a4fSStefano Zampini in an error here */ 96674903a4fSStefano Zampini /* PetscCheckSameComm(dm,1,v,2); */ 9679566063dSJacob Faibussowitsch PetscCall(PetscViewerCheckWritable(v)); 968b1b135c8SBarry Smith 9699566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(v, &format)); 9709566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size)); 9713ba16761SJacob Faibussowitsch if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS); 9729566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v)); 9739566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary)); 97432c0f0efSBarry Smith if (isbinary) { 97555849f57SBarry Smith PetscInt classid = DM_FILE_CLASSID; 97632c0f0efSBarry Smith char type[256]; 97732c0f0efSBarry Smith 9789566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT)); 979c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type))); 9809566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR)); 98132c0f0efSBarry Smith } 982dbbe0bcdSBarry Smith PetscTryTypeMethod(dm, view, v); 9833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 98447c6ae99SBarry Smith } 98547c6ae99SBarry Smith 98647c6ae99SBarry Smith /*@ 987bb7acecfSBarry Smith DMCreateGlobalVector - Creates a global vector from a `DM` object. A global vector is a parallel vector that has no duplicate values shared between MPI ranks, 988bb7acecfSBarry Smith that is it has no ghost locations. 98947c6ae99SBarry Smith 99020f4b53cSBarry Smith Collective 99147c6ae99SBarry Smith 99247c6ae99SBarry Smith Input Parameter: 993bb7acecfSBarry Smith . dm - the `DM` object 99447c6ae99SBarry Smith 99547c6ae99SBarry Smith Output Parameter: 99647c6ae99SBarry Smith . vec - the global vector 99747c6ae99SBarry Smith 998073dac72SJed Brown Level: beginner 99947c6ae99SBarry Smith 10001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, 1001bb7acecfSBarry Smith `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()` 100247c6ae99SBarry Smith @*/ 1003d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec) 1004d71ae5a4SJacob Faibussowitsch { 100547c6ae99SBarry Smith PetscFunctionBegin; 1006171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10074f572ea9SToby Isaac PetscAssertPointer(vec, 2); 1008dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createglobalvector, vec); 100976bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 1010c6b011d8SStefano Zampini DM vdm; 1011c6b011d8SStefano Zampini 10129566063dSJacob Faibussowitsch PetscCall(VecGetDM(*vec, &vdm)); 10137a8be351SBarry Smith PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name); 1014c6b011d8SStefano Zampini } 10153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 101647c6ae99SBarry Smith } 101747c6ae99SBarry Smith 101847c6ae99SBarry Smith /*@ 1019bb7acecfSBarry Smith DMCreateLocalVector - Creates a local vector from a `DM` object. 102047c6ae99SBarry Smith 102147c6ae99SBarry Smith Not Collective 102247c6ae99SBarry Smith 102347c6ae99SBarry Smith Input Parameter: 1024bb7acecfSBarry Smith . dm - the `DM` object 102547c6ae99SBarry Smith 102647c6ae99SBarry Smith Output Parameter: 102747c6ae99SBarry Smith . vec - the local vector 102847c6ae99SBarry Smith 1029073dac72SJed Brown Level: beginner 103047c6ae99SBarry Smith 103120f4b53cSBarry Smith Note: 1032bb7acecfSBarry Smith A local vector usually has ghost locations that contain values that are owned by different MPI ranks. A global vector has no ghost locations. 1033bb7acecfSBarry Smith 10341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()` 1035bb7acecfSBarry Smith `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()` 103647c6ae99SBarry Smith @*/ 1037d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec) 1038d71ae5a4SJacob Faibussowitsch { 103947c6ae99SBarry Smith PetscFunctionBegin; 1040171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10414f572ea9SToby Isaac PetscAssertPointer(vec, 2); 1042dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createlocalvector, vec); 104376bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 1044c6b011d8SStefano Zampini DM vdm; 1045c6b011d8SStefano Zampini 10469566063dSJacob Faibussowitsch PetscCall(VecGetDM(*vec, &vdm)); 10477a8be351SBarry Smith PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name); 1048c6b011d8SStefano Zampini } 10493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 105047c6ae99SBarry Smith } 105147c6ae99SBarry Smith 10521411c6eeSJed Brown /*@ 1053bb7acecfSBarry Smith DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`. 10541411c6eeSJed Brown 105520f4b53cSBarry Smith Collective 10561411c6eeSJed Brown 10571411c6eeSJed Brown Input Parameter: 1058bb7acecfSBarry Smith . dm - the `DM` that provides the mapping 10591411c6eeSJed Brown 10601411c6eeSJed Brown Output Parameter: 10611411c6eeSJed Brown . ltog - the mapping 10621411c6eeSJed Brown 1063bb7acecfSBarry Smith Level: advanced 10641411c6eeSJed Brown 10651411c6eeSJed Brown Notes: 1066bb7acecfSBarry Smith The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()` 10671411c6eeSJed Brown 1068bb7acecfSBarry Smith Vectors obtained with `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do 1069bb7acecfSBarry Smith need to use this function with those objects. 1070bb7acecfSBarry Smith 1071bb7acecfSBarry Smith This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`. 1072bb7acecfSBarry Smith 107360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`, 1074bb7acecfSBarry Smith `DMCreateMatrix()` 10751411c6eeSJed Brown @*/ 1076d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog) 1077d71ae5a4SJacob Faibussowitsch { 10780be3e97aSMatthew G. Knepley PetscInt bs = -1, bsLocal[2], bsMinMax[2]; 10791411c6eeSJed Brown 10801411c6eeSJed Brown PetscFunctionBegin; 10811411c6eeSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10824f572ea9SToby Isaac PetscAssertPointer(ltog, 2); 10831411c6eeSJed Brown if (!dm->ltogmap) { 108437d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 108537d0c07bSMatthew G Knepley 10869566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 108737d0c07bSMatthew G Knepley if (section) { 1088a974ec88SMatthew G. Knepley const PetscInt *cdofs; 108937d0c07bSMatthew G Knepley PetscInt *ltog; 1090ccf3bd66SMatthew G. Knepley PetscInt pStart, pEnd, n, p, k, l; 109137d0c07bSMatthew G Knepley 10929566063dSJacob Faibussowitsch PetscCall(DMGetGlobalSection(dm, §ionGlobal)); 10939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(section, &pStart, &pEnd)); 10949566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(section, &n)); 10959566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, <og)); /* We want the local+overlap size */ 109637d0c07bSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 1097e6befd46SJed Brown PetscInt bdof, cdof, dof, off, c, cind; 109837d0c07bSMatthew G Knepley 109937d0c07bSMatthew G Knepley /* Should probably use constrained dofs */ 11009566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(section, p, &dof)); 11019566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(section, p, &cdof)); 11029566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs)); 11039566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off)); 11041a7dc684SMatthew G. Knepley /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */ 11051a7dc684SMatthew G. Knepley bdof = cdof && (dof - cdof) ? 1 : dof; 1106ad540459SPierre Jolivet if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof); 11075227eafbSStefano Zampini 1108e6befd46SJed Brown for (c = 0, cind = 0; c < dof; ++c, ++l) { 11095227eafbSStefano Zampini if (cind < cdof && c == cdofs[cind]) { 1110e6befd46SJed Brown ltog[l] = off < 0 ? off - c : -(off + c + 1); 1111e6befd46SJed Brown cind++; 1112e6befd46SJed Brown } else { 11135227eafbSStefano Zampini ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind; 1114e6befd46SJed Brown } 111537d0c07bSMatthew G Knepley } 111637d0c07bSMatthew G Knepley } 1117bff27382SMatthew G. Knepley /* Must have same blocksize on all procs (some might have no points) */ 11189371c9d4SSatish Balay bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; 11199371c9d4SSatish Balay bsLocal[1] = bs; 11209566063dSJacob Faibussowitsch PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax)); 11219371c9d4SSatish Balay if (bsMinMax[0] != bsMinMax[1]) { 11229371c9d4SSatish Balay bs = 1; 11239371c9d4SSatish Balay } else { 11249371c9d4SSatish Balay bs = bsMinMax[0]; 11259371c9d4SSatish Balay } 11267591dbb2SMatthew G. Knepley bs = bs < 0 ? 1 : bs; 11277591dbb2SMatthew G. Knepley /* Must reduce indices by blocksize */ 1128ccf3bd66SMatthew G. Knepley if (bs > 1) { 1129ca469d19SJed Brown for (l = 0, k = 0; l < n; l += bs, ++k) { 1130ca469d19SJed Brown // Integer division of negative values truncates toward zero(!), not toward negative infinity 1131ca469d19SJed Brown ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1); 1132ca469d19SJed Brown } 1133ccf3bd66SMatthew G. Knepley n /= bs; 1134ccf3bd66SMatthew G. Knepley } 11359566063dSJacob Faibussowitsch PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap)); 1136dbbe0bcdSBarry Smith } else PetscUseTypeMethod(dm, getlocaltoglobalmapping); 113737d0c07bSMatthew G Knepley } 11381411c6eeSJed Brown *ltog = dm->ltogmap; 11393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11401411c6eeSJed Brown } 11411411c6eeSJed Brown 11421411c6eeSJed Brown /*@ 1143bb7acecfSBarry Smith DMGetBlockSize - Gets the inherent block size associated with a `DM` 11441411c6eeSJed Brown 11451411c6eeSJed Brown Not Collective 11461411c6eeSJed Brown 11471411c6eeSJed Brown Input Parameter: 1148bb7acecfSBarry Smith . dm - the `DM` with block structure 11491411c6eeSJed Brown 11501411c6eeSJed Brown Output Parameter: 11511411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure 11521411c6eeSJed Brown 11531411c6eeSJed Brown Level: intermediate 11541411c6eeSJed Brown 1155bb7acecfSBarry Smith Note: 1156bb7acecfSBarry Smith This might be the number of degrees of freedom at each grid point for a structured grid. 1157bb7acecfSBarry Smith 1158bb7acecfSBarry Smith Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but 1159bb7acecfSBarry Smith rather different locations in the vectors may have a different block size. 1160bb7acecfSBarry Smith 11611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()` 11621411c6eeSJed Brown @*/ 1163d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs) 1164d71ae5a4SJacob Faibussowitsch { 11651411c6eeSJed Brown PetscFunctionBegin; 11661411c6eeSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 11674f572ea9SToby Isaac PetscAssertPointer(bs, 2); 11687a8be351SBarry Smith PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet"); 11691411c6eeSJed Brown *bs = dm->bs; 11703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11711411c6eeSJed Brown } 11721411c6eeSJed Brown 117348eeb7c8SBarry Smith /*@C 1174bb7acecfSBarry Smith DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by 1175bb7acecfSBarry Smith `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`. 117647c6ae99SBarry Smith 117720f4b53cSBarry Smith Collective 117847c6ae99SBarry Smith 1179d8d19677SJose E. Roman Input Parameters: 1180bb7acecfSBarry Smith + dmc - the `DM` object 1181bb7acecfSBarry Smith - dmf - the second, finer `DM` object 118247c6ae99SBarry Smith 1183d8d19677SJose E. Roman Output Parameters: 118447c6ae99SBarry Smith + mat - the interpolation 1185bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()` 118647c6ae99SBarry Smith 118747c6ae99SBarry Smith Level: developer 118847c6ae99SBarry Smith 118995452b02SPatrick Sanan Notes: 1190bb7acecfSBarry Smith For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by 1191bb7acecfSBarry Smith DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation. 1192d52bd9f3SBarry Smith 1193bb7acecfSBarry Smith For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate 1194bb7acecfSBarry Smith vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic. 119585afcc9aSBarry Smith 11961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()` 119747c6ae99SBarry Smith @*/ 1198d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec) 1199d71ae5a4SJacob Faibussowitsch { 120047c6ae99SBarry Smith PetscFunctionBegin; 1201a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dmc, DM_CLASSID, 1); 1202a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dmf, DM_CLASSID, 2); 12034f572ea9SToby Isaac PetscAssertPointer(mat, 3); 12049566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0)); 1205dbbe0bcdSBarry Smith PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec); 12069566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0)); 12073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 120847c6ae99SBarry Smith } 120947c6ae99SBarry Smith 12103ad4599aSBarry Smith /*@ 1211a4e35b19SJacob Faibussowitsch DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is 1212a4e35b19SJacob Faibussowitsch the transpose of the interpolation between the `DM`. 12132ed6491fSPatrick Sanan 12142ed6491fSPatrick Sanan Input Parameters: 1215bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh 1216bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh 12172ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse 12182ed6491fSPatrick Sanan 12192ed6491fSPatrick Sanan Output Parameter: 12202ed6491fSPatrick Sanan . scale - the scaled vector 12212ed6491fSPatrick Sanan 1222bb7acecfSBarry Smith Level: advanced 12232ed6491fSPatrick Sanan 1224a4e35b19SJacob Faibussowitsch Notes: 1225a4e35b19SJacob Faibussowitsch xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) 1226a4e35b19SJacob Faibussowitsch restriction. In other words xcoarse is the coarse representation of xfine. 1227a4e35b19SJacob Faibussowitsch 122860225df5SJacob Faibussowitsch Developer Notes: 1229bb7acecfSBarry Smith If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()` 1230e9c74fd6SRichard Tran Mills on the restriction/interpolation operator to set the bindingpropagates flag to true. 1231e9c74fd6SRichard Tran Mills 123260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()` 12332ed6491fSPatrick Sanan @*/ 1234d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale) 1235d71ae5a4SJacob Faibussowitsch { 12362ed6491fSPatrick Sanan Vec fine; 12372ed6491fSPatrick Sanan PetscScalar one = 1.0; 12389704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA) 1239e9c74fd6SRichard Tran Mills PetscBool bindingpropagates, isbound; 12409704db99SRichard Tran Mills #endif 12412ed6491fSPatrick Sanan 12422ed6491fSPatrick Sanan PetscFunctionBegin; 12439566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(daf, &fine)); 12449566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(dac, scale)); 12459566063dSJacob Faibussowitsch PetscCall(VecSet(fine, one)); 12469704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA) 12479704db99SRichard Tran Mills /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well. 12489704db99SRichard Tran Mills * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL, 12499704db99SRichard Tran Mills * we'll need to do it for that case, too.*/ 12509566063dSJacob Faibussowitsch PetscCall(VecGetBindingPropagates(fine, &bindingpropagates)); 1251e9c74fd6SRichard Tran Mills if (bindingpropagates) { 12529566063dSJacob Faibussowitsch PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE)); 12539566063dSJacob Faibussowitsch PetscCall(VecBoundToCPU(fine, &isbound)); 12549566063dSJacob Faibussowitsch PetscCall(MatBindToCPU(mat, isbound)); 125583aa49f4SRichard Tran Mills } 12569704db99SRichard Tran Mills #endif 12579566063dSJacob Faibussowitsch PetscCall(MatRestrict(mat, fine, *scale)); 12589566063dSJacob Faibussowitsch PetscCall(VecDestroy(&fine)); 12599566063dSJacob Faibussowitsch PetscCall(VecReciprocal(*scale)); 12603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12612ed6491fSPatrick Sanan } 12622ed6491fSPatrick Sanan 12632ed6491fSPatrick Sanan /*@ 1264bb7acecfSBarry Smith DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by 1265bb7acecfSBarry Smith `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`. 12663ad4599aSBarry Smith 126720f4b53cSBarry Smith Collective 12683ad4599aSBarry Smith 1269d8d19677SJose E. Roman Input Parameters: 1270bb7acecfSBarry Smith + dmc - the `DM` object 1271bb7acecfSBarry Smith - dmf - the second, finer `DM` object 12723ad4599aSBarry Smith 12733ad4599aSBarry Smith Output Parameter: 12743ad4599aSBarry Smith . mat - the restriction 12753ad4599aSBarry Smith 12763ad4599aSBarry Smith Level: developer 12773ad4599aSBarry Smith 1278bb7acecfSBarry Smith Note: 1279bb7acecfSBarry Smith This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that 1280bb7acecfSBarry Smith matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object. 12813ad4599aSBarry Smith 12821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()` 12833ad4599aSBarry Smith @*/ 1284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat) 1285d71ae5a4SJacob Faibussowitsch { 12863ad4599aSBarry Smith PetscFunctionBegin; 1287a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dmc, DM_CLASSID, 1); 1288a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dmf, DM_CLASSID, 2); 12894f572ea9SToby Isaac PetscAssertPointer(mat, 3); 12909566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0)); 1291dbbe0bcdSBarry Smith PetscUseTypeMethod(dmc, createrestriction, dmf, mat); 12929566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0)); 12933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12943ad4599aSBarry Smith } 12953ad4599aSBarry Smith 129647c6ae99SBarry Smith /*@ 1297a4e35b19SJacob Faibussowitsch DMCreateInjection - Gets injection matrix between two `DM` objects. 129847c6ae99SBarry Smith 129920f4b53cSBarry Smith Collective 130047c6ae99SBarry Smith 1301d8d19677SJose E. Roman Input Parameters: 1302bb7acecfSBarry Smith + dac - the `DM` object 1303bb7acecfSBarry Smith - daf - the second, finer `DM` object 130447c6ae99SBarry Smith 130547c6ae99SBarry Smith Output Parameter: 13066dbf9973SLawrence Mitchell . mat - the injection 130747c6ae99SBarry Smith 130847c6ae99SBarry Smith Level: developer 130947c6ae99SBarry Smith 1310a4e35b19SJacob Faibussowitsch Notes: 1311a4e35b19SJacob Faibussowitsch This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the 1312a4e35b19SJacob Faibussowitsch fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting 1313a4e35b19SJacob Faibussowitsch the values on the coarse grid points. This compares to the operator obtained by 1314a4e35b19SJacob Faibussowitsch `DMCreateRestriction()` or the transpose of the operator obtained by 1315a4e35b19SJacob Faibussowitsch `DMCreateInterpolation()` that uses a "local weighted average" of the values around the 1316a4e35b19SJacob Faibussowitsch coarse grid point as the coarse grid value. 1317a4e35b19SJacob Faibussowitsch 1318bb7acecfSBarry Smith For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by 1319bb7acecfSBarry Smith `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection. 132085afcc9aSBarry Smith 13211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`, 1322bb7acecfSBarry Smith `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()` 132347c6ae99SBarry Smith @*/ 1324d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat) 1325d71ae5a4SJacob Faibussowitsch { 132647c6ae99SBarry Smith PetscFunctionBegin; 1327a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dac, DM_CLASSID, 1); 1328a5bc1bf3SBarry Smith PetscValidHeaderSpecific(daf, DM_CLASSID, 2); 13294f572ea9SToby Isaac PetscAssertPointer(mat, 3); 13309566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0)); 1331dbbe0bcdSBarry Smith PetscUseTypeMethod(dac, createinjection, daf, mat); 13329566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0)); 13333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 133447c6ae99SBarry Smith } 133547c6ae99SBarry Smith 1336b412c318SBarry Smith /*@ 1337bb7acecfSBarry Smith DMCreateMassMatrix - Gets the mass matrix between two `DM` objects, M_ij = \int \phi_i \psi_j where the \phi are Galerkin basis functions for a 1338bb7acecfSBarry Smith a Galerkin finite element model on the `DM` 1339bd041c0cSMatthew G. Knepley 134020f4b53cSBarry Smith Collective 1341bd041c0cSMatthew G. Knepley 1342d8d19677SJose E. Roman Input Parameters: 1343bb7acecfSBarry Smith + dmc - the target `DM` object 1344bb7acecfSBarry Smith - dmf - the source `DM` object 1345bd041c0cSMatthew G. Knepley 1346bd041c0cSMatthew G. Knepley Output Parameter: 1347b4937a87SMatthew G. Knepley . mat - the mass matrix 1348bd041c0cSMatthew G. Knepley 1349bd041c0cSMatthew G. Knepley Level: developer 1350bd041c0cSMatthew G. Knepley 1351bb7acecfSBarry Smith Notes: 1352bb7acecfSBarry Smith For `DMPLEX` the finite element model for the `DM` must have been already provided. 1353bb7acecfSBarry Smith 135420f4b53cSBarry Smith if `dmc` is `dmf` then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()` 1355bb7acecfSBarry Smith 135642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()` 1357bd041c0cSMatthew G. Knepley @*/ 1358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat) 1359d71ae5a4SJacob Faibussowitsch { 1360bd041c0cSMatthew G. Knepley PetscFunctionBegin; 1361b4937a87SMatthew G. Knepley PetscValidHeaderSpecific(dmc, DM_CLASSID, 1); 1362b4937a87SMatthew G. Knepley PetscValidHeaderSpecific(dmf, DM_CLASSID, 2); 13634f572ea9SToby Isaac PetscAssertPointer(mat, 3); 13645b8ffe73SMark Adams PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0)); 1365dbbe0bcdSBarry Smith PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat); 13665b8ffe73SMark Adams PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0)); 13673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1368b4937a87SMatthew G. Knepley } 1369b4937a87SMatthew G. Knepley 1370b4937a87SMatthew G. Knepley /*@ 1371bb7acecfSBarry Smith DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM` 1372b4937a87SMatthew G. Knepley 137320f4b53cSBarry Smith Collective 1374b4937a87SMatthew G. Knepley 1375b4937a87SMatthew G. Knepley Input Parameter: 1376bb7acecfSBarry Smith . dm - the `DM` object 1377b4937a87SMatthew G. Knepley 1378b4937a87SMatthew G. Knepley Output Parameter: 1379bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector 1380b4937a87SMatthew G. Knepley 1381b4937a87SMatthew G. Knepley Level: developer 1382b4937a87SMatthew G. Knepley 1383bb7acecfSBarry Smith Note: 1384bb7acecfSBarry Smith See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix. 1385bb7acecfSBarry Smith 138660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()` 1387b4937a87SMatthew G. Knepley @*/ 1388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm) 1389d71ae5a4SJacob Faibussowitsch { 1390b4937a87SMatthew G. Knepley PetscFunctionBegin; 1391b4937a87SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 13924f572ea9SToby Isaac PetscAssertPointer(lm, 2); 1393dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createmassmatrixlumped, lm); 13943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1395bd041c0cSMatthew G. Knepley } 1396bd041c0cSMatthew G. Knepley 1397bd041c0cSMatthew G. Knepley /*@ 1398bb7acecfSBarry Smith DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization 1399bb7acecfSBarry Smith of a PDE on the `DM`. 140047c6ae99SBarry Smith 140120f4b53cSBarry Smith Collective 140247c6ae99SBarry Smith 1403d8d19677SJose E. Roman Input Parameters: 1404bb7acecfSBarry Smith + dm - the `DM` object 1405bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL` 140647c6ae99SBarry Smith 140747c6ae99SBarry Smith Output Parameter: 140847c6ae99SBarry Smith . coloring - the coloring 140947c6ae99SBarry Smith 14101bf8429eSBarry Smith Level: developer 14111bf8429eSBarry Smith 1412ec5066bdSBarry Smith Notes: 1413bb7acecfSBarry Smith Coloring of matrices can also be computed directly from the sparse matrix nonzero structure via the `MatColoring` object or from the mesh from which the 1414bb7acecfSBarry Smith matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors). 1415ec5066bdSBarry Smith 1416bb7acecfSBarry Smith This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()` 14171bf8429eSBarry Smith For `DMDA` in three dimensions with periodic boundary conditions the number of grid points in each dimension must be divisible by 2*stencil_width + 1, 14181bf8429eSBarry Smith otherwise an error will be generated. 1419ec5066bdSBarry Smith 14201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()` 1421aab9d709SJed Brown @*/ 1422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring) 1423d71ae5a4SJacob Faibussowitsch { 142447c6ae99SBarry Smith PetscFunctionBegin; 1425171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14264f572ea9SToby Isaac PetscAssertPointer(coloring, 3); 1427dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, getcoloring, ctype, coloring); 14283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 142947c6ae99SBarry Smith } 143047c6ae99SBarry Smith 1431b412c318SBarry Smith /*@ 1432bb7acecfSBarry Smith DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator. 143347c6ae99SBarry Smith 143420f4b53cSBarry Smith Collective 143547c6ae99SBarry Smith 143647c6ae99SBarry Smith Input Parameter: 1437bb7acecfSBarry Smith . dm - the `DM` object 143847c6ae99SBarry Smith 143947c6ae99SBarry Smith Output Parameter: 144047c6ae99SBarry Smith . mat - the empty Jacobian 144147c6ae99SBarry Smith 144220f4b53cSBarry Smith Options Database Key: 1443bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros 1444f27dd7c6SMatthew G. Knepley 144520f4b53cSBarry Smith Level: beginner 144620f4b53cSBarry Smith 144795452b02SPatrick Sanan Notes: 144895452b02SPatrick Sanan This properly preallocates the number of nonzeros in the sparse matrix so you 144994013140SBarry Smith do not need to do it yourself. 145094013140SBarry Smith 145194013140SBarry Smith By default it also sets the nonzero structure and puts in the zero entries. To prevent setting 1452bb7acecfSBarry Smith the nonzero pattern call `DMSetMatrixPreallocateOnly()` 145394013140SBarry Smith 1454bb7acecfSBarry Smith For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used 145594013140SBarry Smith internally by PETSc. 145694013140SBarry Smith 1457bb7acecfSBarry Smith For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because 1458bb7acecfSBarry Smith `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute 145994013140SBarry Smith 14601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()` 1461aab9d709SJed Brown @*/ 1462d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat) 1463d71ae5a4SJacob Faibussowitsch { 146447c6ae99SBarry Smith PetscFunctionBegin; 1465171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 14664f572ea9SToby Isaac PetscAssertPointer(mat, 2); 14679566063dSJacob Faibussowitsch PetscCall(MatInitializePackage()); 14689566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0)); 1469dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, creatematrix, mat); 147076bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 1471c6b011d8SStefano Zampini DM mdm; 1472c6b011d8SStefano Zampini 14739566063dSJacob Faibussowitsch PetscCall(MatGetDM(*mat, &mdm)); 14747a8be351SBarry Smith PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name); 1475c6b011d8SStefano Zampini } 1476e571a35bSMatthew G. Knepley /* Handle nullspace and near nullspace */ 1477e5e52638SMatthew G. Knepley if (dm->Nf) { 1478e571a35bSMatthew G. Knepley MatNullSpace nullSpace; 1479649ef022SMatthew Knepley PetscInt Nf, f; 1480e571a35bSMatthew G. Knepley 14819566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 1482649ef022SMatthew Knepley for (f = 0; f < Nf; ++f) { 1483649ef022SMatthew Knepley if (dm->nullspaceConstructors[f]) { 14849566063dSJacob Faibussowitsch PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace)); 14859566063dSJacob Faibussowitsch PetscCall(MatSetNullSpace(*mat, nullSpace)); 14869566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nullSpace)); 1487649ef022SMatthew Knepley break; 1488e571a35bSMatthew G. Knepley } 1489649ef022SMatthew Knepley } 1490649ef022SMatthew Knepley for (f = 0; f < Nf; ++f) { 1491649ef022SMatthew Knepley if (dm->nearnullspaceConstructors[f]) { 14929566063dSJacob Faibussowitsch PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace)); 14939566063dSJacob Faibussowitsch PetscCall(MatSetNearNullSpace(*mat, nullSpace)); 14949566063dSJacob Faibussowitsch PetscCall(MatNullSpaceDestroy(&nullSpace)); 1495e571a35bSMatthew G. Knepley } 1496e571a35bSMatthew G. Knepley } 1497e571a35bSMatthew G. Knepley } 14989566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0)); 14993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 150047c6ae99SBarry Smith } 150147c6ae99SBarry Smith 1502732e2eb9SMatthew G Knepley /*@ 1503a4e35b19SJacob Faibussowitsch DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and 1504a4e35b19SJacob Faibussowitsch `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the 1505a4e35b19SJacob Faibussowitsch matrices will not be preallocated. 1506aa0f6e3cSJed Brown 150720f4b53cSBarry Smith Logically Collective 1508aa0f6e3cSJed Brown 1509aa0f6e3cSJed Brown Input Parameters: 1510bb7acecfSBarry Smith + dm - the `DM` 1511bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation 1512aa0f6e3cSJed Brown 1513aa0f6e3cSJed Brown Level: developer 1514aa0f6e3cSJed Brown 1515a4e35b19SJacob Faibussowitsch Notes: 1516a4e35b19SJacob Faibussowitsch This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and 1517a4e35b19SJacob Faibussowitsch `MatSetValuesCOO()` will be used. 1518a4e35b19SJacob Faibussowitsch 15191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()` 1520aa0f6e3cSJed Brown @*/ 1521d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip) 1522d71ae5a4SJacob Faibussowitsch { 1523aa0f6e3cSJed Brown PetscFunctionBegin; 1524aa0f6e3cSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1525aa0f6e3cSJed Brown dm->prealloc_skip = skip; 15263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1527aa0f6e3cSJed Brown } 1528aa0f6e3cSJed Brown 1529aa0f6e3cSJed Brown /*@ 1530bb7acecfSBarry Smith DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly 1531732e2eb9SMatthew G Knepley preallocated but the nonzero structure and zero values will not be set. 1532732e2eb9SMatthew G Knepley 153320f4b53cSBarry Smith Logically Collective 1534732e2eb9SMatthew G Knepley 1535d8d19677SJose E. Roman Input Parameters: 1536bb7acecfSBarry Smith + dm - the `DM` 1537bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation 1538732e2eb9SMatthew G Knepley 153920f4b53cSBarry Smith Options Database Key: 1540bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros 1541f27dd7c6SMatthew G. Knepley 154220f4b53cSBarry Smith Level: developer 154320f4b53cSBarry Smith 15441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()` 1545732e2eb9SMatthew G Knepley @*/ 1546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) 1547d71ae5a4SJacob Faibussowitsch { 1548732e2eb9SMatthew G Knepley PetscFunctionBegin; 1549732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1550732e2eb9SMatthew G Knepley dm->prealloc_only = only; 15513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1552732e2eb9SMatthew G Knepley } 1553732e2eb9SMatthew G Knepley 1554b06ff27eSHong Zhang /*@ 1555bb7acecfSBarry Smith DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created 1556bb7acecfSBarry Smith but the array for numerical values will not be allocated. 1557b06ff27eSHong Zhang 155820f4b53cSBarry Smith Logically Collective 1559b06ff27eSHong Zhang 1560d8d19677SJose E. Roman Input Parameters: 1561bb7acecfSBarry Smith + dm - the `DM` 1562da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure 1563b06ff27eSHong Zhang 1564b06ff27eSHong Zhang Level: developer 1565bb7acecfSBarry Smith 15661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()` 1567b06ff27eSHong Zhang @*/ 1568d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only) 1569d71ae5a4SJacob Faibussowitsch { 1570b06ff27eSHong Zhang PetscFunctionBegin; 1571b06ff27eSHong Zhang PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1572b06ff27eSHong Zhang dm->structure_only = only; 15733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1574b06ff27eSHong Zhang } 1575b06ff27eSHong Zhang 1576863027abSJed Brown /*@ 1577863027abSJed Brown DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called 1578863027abSJed Brown 157920f4b53cSBarry Smith Logically Collective 1580863027abSJed Brown 1581863027abSJed Brown Input Parameters: 1582863027abSJed Brown + dm - the `DM` 1583863027abSJed Brown - btype - block by topological point or field node 1584863027abSJed Brown 158520f4b53cSBarry Smith Options Database Key: 15860e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking 1587863027abSJed Brown 158820f4b53cSBarry Smith Level: advanced 158920f4b53cSBarry Smith 15901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()` 1591863027abSJed Brown @*/ 1592863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype) 1593863027abSJed Brown { 1594863027abSJed Brown PetscFunctionBegin; 1595863027abSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1596863027abSJed Brown dm->blocking_type = btype; 15973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1598863027abSJed Brown } 1599863027abSJed Brown 1600863027abSJed Brown /*@ 1601863027abSJed Brown DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called 1602863027abSJed Brown 1603863027abSJed Brown Not Collective 1604863027abSJed Brown 16052fe279fdSBarry Smith Input Parameter: 1606863027abSJed Brown . dm - the `DM` 1607863027abSJed Brown 16082fe279fdSBarry Smith Output Parameter: 1609863027abSJed Brown . btype - block by topological point or field node 1610863027abSJed Brown 1611863027abSJed Brown Level: advanced 1612863027abSJed Brown 16131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()` 1614863027abSJed Brown @*/ 1615863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype) 1616863027abSJed Brown { 1617863027abSJed Brown PetscFunctionBegin; 1618863027abSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 16194f572ea9SToby Isaac PetscAssertPointer(btype, 2); 1620863027abSJed Brown *btype = dm->blocking_type; 16213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1622863027abSJed Brown } 1623863027abSJed Brown 1624a89ea682SMatthew G Knepley /*@C 1625bb7acecfSBarry Smith DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()` 1626a89ea682SMatthew G Knepley 1627a89ea682SMatthew G Knepley Not Collective 1628a89ea682SMatthew G Knepley 1629a89ea682SMatthew G Knepley Input Parameters: 1630bb7acecfSBarry Smith + dm - the `DM` object 1631a5b23f4aSJose E. Roman . count - The minimum size 163220f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`) 1633a89ea682SMatthew G Knepley 1634a89ea682SMatthew G Knepley Output Parameter: 163560225df5SJacob Faibussowitsch . mem - the work array 1636a89ea682SMatthew G Knepley 1637a89ea682SMatthew G Knepley Level: developer 1638a89ea682SMatthew G Knepley 1639bb7acecfSBarry Smith Note: 1640da81f932SPierre Jolivet A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()` 1641bb7acecfSBarry Smith 1642bb7acecfSBarry Smith The array may contain nonzero values 1643bb7acecfSBarry Smith 16441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()` 1645a89ea682SMatthew G Knepley @*/ 1646d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) 1647d71ae5a4SJacob Faibussowitsch { 1648aa1993deSMatthew G Knepley DMWorkLink link; 164969291d52SBarry Smith PetscMPIInt dsize; 1650a89ea682SMatthew G Knepley 1651a89ea682SMatthew G Knepley PetscFunctionBegin; 1652a89ea682SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 16534f572ea9SToby Isaac PetscAssertPointer(mem, 4); 1654442f3b32SStefano Zampini if (!count) { 1655442f3b32SStefano Zampini *(void **)mem = NULL; 1656442f3b32SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 1657442f3b32SStefano Zampini } 1658aa1993deSMatthew G Knepley if (dm->workin) { 1659aa1993deSMatthew G Knepley link = dm->workin; 1660aa1993deSMatthew G Knepley dm->workin = dm->workin->next; 1661aa1993deSMatthew G Knepley } else { 16624dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&link)); 1663a89ea682SMatthew G Knepley } 1664b77fa653SStefano Zampini /* Avoid MPI_Type_size for most used datatypes 1665b77fa653SStefano Zampini Get size directly */ 1666b77fa653SStefano Zampini if (dtype == MPIU_INT) dsize = sizeof(PetscInt); 1667b77fa653SStefano Zampini else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal); 1668b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES) 1669b77fa653SStefano Zampini else if (dtype == MPI_INT) dsize = sizeof(int); 1670b77fa653SStefano Zampini #endif 1671b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX) 1672b77fa653SStefano Zampini else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar); 1673b77fa653SStefano Zampini #endif 1674b77fa653SStefano Zampini else PetscCallMPI(MPI_Type_size(dtype, &dsize)); 1675b77fa653SStefano Zampini 16765056fcd2SBarry Smith if (((size_t)dsize * count) > link->bytes) { 16779566063dSJacob Faibussowitsch PetscCall(PetscFree(link->mem)); 16789566063dSJacob Faibussowitsch PetscCall(PetscMalloc(dsize * count, &link->mem)); 1679854ce69bSBarry Smith link->bytes = dsize * count; 1680aa1993deSMatthew G Knepley } 1681aa1993deSMatthew G Knepley link->next = dm->workout; 1682aa1993deSMatthew G Knepley dm->workout = link; 1683cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin)) 168400d952a4SJed Brown VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count); 168500d952a4SJed Brown VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count); 168600d952a4SJed Brown #endif 1687aa1993deSMatthew G Knepley *(void **)mem = link->mem; 16883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1689a89ea682SMatthew G Knepley } 1690a89ea682SMatthew G Knepley 1691aa1993deSMatthew G Knepley /*@C 1692bb7acecfSBarry Smith DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()` 1693aa1993deSMatthew G Knepley 1694aa1993deSMatthew G Knepley Not Collective 1695aa1993deSMatthew G Knepley 1696aa1993deSMatthew G Knepley Input Parameters: 1697bb7acecfSBarry Smith + dm - the `DM` object 1698a5b23f4aSJose E. Roman . count - The minimum size 169920f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT` 1700aa1993deSMatthew G Knepley 1701aa1993deSMatthew G Knepley Output Parameter: 170260225df5SJacob Faibussowitsch . mem - the work array 1703aa1993deSMatthew G Knepley 1704aa1993deSMatthew G Knepley Level: developer 1705aa1993deSMatthew G Knepley 170660225df5SJacob Faibussowitsch Developer Notes: 1707bb7acecfSBarry Smith count and dtype are ignored, they are only needed for `DMGetWorkArray()` 1708147403d9SBarry Smith 17091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()` 1710aa1993deSMatthew G Knepley @*/ 1711d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem) 1712d71ae5a4SJacob Faibussowitsch { 1713aa1993deSMatthew G Knepley DMWorkLink *p, link; 1714aa1993deSMatthew G Knepley 1715aa1993deSMatthew G Knepley PetscFunctionBegin; 1716aa1993deSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17174f572ea9SToby Isaac PetscAssertPointer(mem, 4); 1718a4e35b19SJacob Faibussowitsch (void)count; 1719a4e35b19SJacob Faibussowitsch (void)dtype; 1720442f3b32SStefano Zampini if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS); 1721aa1993deSMatthew G Knepley for (p = &dm->workout; (link = *p); p = &link->next) { 1722aa1993deSMatthew G Knepley if (link->mem == *(void **)mem) { 1723aa1993deSMatthew G Knepley *p = link->next; 1724aa1993deSMatthew G Knepley link->next = dm->workin; 1725aa1993deSMatthew G Knepley dm->workin = link; 17260298fd71SBarry Smith *(void **)mem = NULL; 17273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1728aa1993deSMatthew G Knepley } 1729aa1993deSMatthew G Knepley } 1730aa1993deSMatthew G Knepley SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out"); 1731aa1993deSMatthew G Knepley } 1732e7c4fc90SDmitry Karpeev 17338cda7954SMatthew G. Knepley /*@C 1734bb7acecfSBarry Smith DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces 1735bb7acecfSBarry Smith are joined or split, such as in `DMCreateSubDM()` 17368cda7954SMatthew G. Knepley 173720f4b53cSBarry Smith Logically Collective; No Fortran Support 17388cda7954SMatthew G. Knepley 17398cda7954SMatthew G. Knepley Input Parameters: 1740bb7acecfSBarry Smith + dm - The `DM` 17418cda7954SMatthew G. Knepley . field - The field number for the nullspace 17428cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace 17438cda7954SMatthew G. Knepley 174420f4b53cSBarry Smith Calling sequence of `nullsp`: 1745bb7acecfSBarry Smith + dm - The present `DM` 1746bb7acecfSBarry Smith . origField - The field number given above, in the original `DM` 1747147403d9SBarry Smith . field - The field number in dm 1748147403d9SBarry Smith - nullSpace - The nullspace for the given field 17498cda7954SMatthew G. Knepley 175049762cbcSSatish Balay Level: intermediate 175149762cbcSSatish Balay 17521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()` 1753147403d9SBarry Smith @*/ 1754a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)) 1755d71ae5a4SJacob Faibussowitsch { 1756435a35e8SMatthew G Knepley PetscFunctionBegin; 1757435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17587a8be351SBarry Smith PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field); 1759435a35e8SMatthew G Knepley dm->nullspaceConstructors[field] = nullsp; 17603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1761435a35e8SMatthew G Knepley } 1762435a35e8SMatthew G Knepley 17638cda7954SMatthew G. Knepley /*@C 1764bb7acecfSBarry Smith DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()` 17658cda7954SMatthew G. Knepley 176620f4b53cSBarry Smith Not Collective; No Fortran Support 17678cda7954SMatthew G. Knepley 17688cda7954SMatthew G. Knepley Input Parameters: 1769bb7acecfSBarry Smith + dm - The `DM` 17708cda7954SMatthew G. Knepley - field - The field number for the nullspace 17718cda7954SMatthew G. Knepley 17728cda7954SMatthew G. Knepley Output Parameter: 17738cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace 17748cda7954SMatthew G. Knepley 177520f4b53cSBarry Smith Calling sequence of `nullsp`: 1776147403d9SBarry Smith + dm - The present DM 1777147403d9SBarry Smith . origField - The field number given above, in the original DM 1778147403d9SBarry Smith . field - The field number in dm 1779147403d9SBarry Smith - nullSpace - The nullspace for the given field 17808cda7954SMatthew G. Knepley 178149762cbcSSatish Balay Level: intermediate 178249762cbcSSatish Balay 17831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()` 1784147403d9SBarry Smith @*/ 1785a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)) 1786d71ae5a4SJacob Faibussowitsch { 17870a50eb56SMatthew G. Knepley PetscFunctionBegin; 17880a50eb56SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17894f572ea9SToby Isaac PetscAssertPointer(nullsp, 3); 17907a8be351SBarry Smith PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field); 17910a50eb56SMatthew G. Knepley *nullsp = dm->nullspaceConstructors[field]; 17923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 17930a50eb56SMatthew G. Knepley } 17940a50eb56SMatthew G. Knepley 17958cda7954SMatthew G. Knepley /*@C 1796bb7acecfSBarry Smith DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()` 17978cda7954SMatthew G. Knepley 179820f4b53cSBarry Smith Logically Collective; No Fortran Support 17998cda7954SMatthew G. Knepley 18008cda7954SMatthew G. Knepley Input Parameters: 1801bb7acecfSBarry Smith + dm - The `DM` 18028cda7954SMatthew G. Knepley . field - The field number for the nullspace 18038cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace 18048cda7954SMatthew G. Knepley 180520f4b53cSBarry Smith Calling sequence of `nullsp`: 1806bb7acecfSBarry Smith + dm - The present `DM` 1807bb7acecfSBarry Smith . origField - The field number given above, in the original `DM` 1808147403d9SBarry Smith . field - The field number in dm 1809147403d9SBarry Smith - nullSpace - The nullspace for the given field 18108cda7954SMatthew G. Knepley 181149762cbcSSatish Balay Level: intermediate 181249762cbcSSatish Balay 18131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`, 1814bb7acecfSBarry Smith `MatNullSpace` 1815147403d9SBarry Smith @*/ 1816a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)) 1817d71ae5a4SJacob Faibussowitsch { 1818f9d4088aSMatthew G. Knepley PetscFunctionBegin; 1819f9d4088aSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 18207a8be351SBarry Smith PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field); 1821f9d4088aSMatthew G. Knepley dm->nearnullspaceConstructors[field] = nullsp; 18223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1823f9d4088aSMatthew G. Knepley } 1824f9d4088aSMatthew G. Knepley 18258cda7954SMatthew G. Knepley /*@C 1826bb7acecfSBarry Smith DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()` 18278cda7954SMatthew G. Knepley 182820f4b53cSBarry Smith Not Collective; No Fortran Support 18298cda7954SMatthew G. Knepley 18308cda7954SMatthew G. Knepley Input Parameters: 1831bb7acecfSBarry Smith + dm - The `DM` 18328cda7954SMatthew G. Knepley - field - The field number for the nullspace 18338cda7954SMatthew G. Knepley 18348cda7954SMatthew G. Knepley Output Parameter: 18358cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace 18368cda7954SMatthew G. Knepley 183720f4b53cSBarry Smith Calling sequence of `nullsp`: 1838bb7acecfSBarry Smith + dm - The present `DM` 1839bb7acecfSBarry Smith . origField - The field number given above, in the original `DM` 1840147403d9SBarry Smith . field - The field number in dm 1841147403d9SBarry Smith - nullSpace - The nullspace for the given field 18428cda7954SMatthew G. Knepley 184349762cbcSSatish Balay Level: intermediate 184449762cbcSSatish Balay 18451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, 1846bb7acecfSBarry Smith `MatNullSpace`, `DMCreateSuperDM()` 1847147403d9SBarry Smith @*/ 1848a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)) 1849d71ae5a4SJacob Faibussowitsch { 1850f9d4088aSMatthew G. Knepley PetscFunctionBegin; 1851f9d4088aSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 18524f572ea9SToby Isaac PetscAssertPointer(nullsp, 3); 18537a8be351SBarry Smith PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field); 1854f9d4088aSMatthew G. Knepley *nullsp = dm->nearnullspaceConstructors[field]; 18553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1856f9d4088aSMatthew G. Knepley } 1857f9d4088aSMatthew G. Knepley 18584f3b5142SJed Brown /*@C 1859bb7acecfSBarry Smith DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()` 18604d343eeaSMatthew G Knepley 186120f4b53cSBarry Smith Not Collective; No Fortran Support 18624d343eeaSMatthew G Knepley 18634d343eeaSMatthew G Knepley Input Parameter: 1864bb7acecfSBarry Smith . dm - the `DM` object 18654d343eeaSMatthew G Knepley 18664d343eeaSMatthew G Knepley Output Parameters: 186720f4b53cSBarry Smith + numFields - The number of fields (or `NULL` if not requested) 186820f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested) 186920f4b53cSBarry Smith - fields - The global indices for each field (or `NULL` if not requested) 18704d343eeaSMatthew G Knepley 18714d343eeaSMatthew G Knepley Level: intermediate 18724d343eeaSMatthew G Knepley 1873bb7acecfSBarry Smith Note: 187421c9b008SJed Brown The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 1875bb7acecfSBarry Smith `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with 1876bb7acecfSBarry Smith `PetscFree()`. 187721c9b008SJed Brown 187860225df5SJacob Faibussowitsch Developer Notes: 1879bb7acecfSBarry Smith It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should 1880bb7acecfSBarry Smith likely be removed. 1881bb7acecfSBarry Smith 18821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, 1883bb7acecfSBarry Smith `DMCreateFieldDecomposition()` 18844d343eeaSMatthew G Knepley @*/ 1885d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) 1886d71ae5a4SJacob Faibussowitsch { 188737d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 18884d343eeaSMatthew G Knepley 18894d343eeaSMatthew G Knepley PetscFunctionBegin; 18904d343eeaSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 189169ca1f37SDmitry Karpeev if (numFields) { 18924f572ea9SToby Isaac PetscAssertPointer(numFields, 2); 189369ca1f37SDmitry Karpeev *numFields = 0; 189469ca1f37SDmitry Karpeev } 189537d0c07bSMatthew G Knepley if (fieldNames) { 18964f572ea9SToby Isaac PetscAssertPointer(fieldNames, 3); 18970298fd71SBarry Smith *fieldNames = NULL; 189869ca1f37SDmitry Karpeev } 189969ca1f37SDmitry Karpeev if (fields) { 19004f572ea9SToby Isaac PetscAssertPointer(fields, 4); 19010298fd71SBarry Smith *fields = NULL; 190269ca1f37SDmitry Karpeev } 19039566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 190437d0c07bSMatthew G Knepley if (section) { 19053a544194SStefano Zampini PetscInt *fieldSizes, *fieldNc, **fieldIndices; 190637d0c07bSMatthew G Knepley PetscInt nF, f, pStart, pEnd, p; 190737d0c07bSMatthew G Knepley 19089566063dSJacob Faibussowitsch PetscCall(DMGetGlobalSection(dm, §ionGlobal)); 19099566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(section, &nF)); 19109566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices)); 19119566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd)); 191237d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 191337d0c07bSMatthew G Knepley fieldSizes[f] = 0; 19149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f])); 191537d0c07bSMatthew G Knepley } 191637d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 191737d0c07bSMatthew G Knepley PetscInt gdof; 191837d0c07bSMatthew G Knepley 19199566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof)); 192037d0c07bSMatthew G Knepley if (gdof > 0) { 192137d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 19223a544194SStefano Zampini PetscInt fdof, fcdof, fpdof; 192337d0c07bSMatthew G Knepley 19249566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof)); 19259566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof)); 19263a544194SStefano Zampini fpdof = fdof - fcdof; 19273a544194SStefano Zampini if (fpdof && fpdof != fieldNc[f]) { 19283a544194SStefano Zampini /* Layout does not admit a pointwise block size */ 19293a544194SStefano Zampini fieldNc[f] = 1; 19303a544194SStefano Zampini } 19313a544194SStefano Zampini fieldSizes[f] += fpdof; 193237d0c07bSMatthew G Knepley } 193337d0c07bSMatthew G Knepley } 193437d0c07bSMatthew G Knepley } 193537d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 19369566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f])); 193737d0c07bSMatthew G Knepley fieldSizes[f] = 0; 193837d0c07bSMatthew G Knepley } 193937d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 194037d0c07bSMatthew G Knepley PetscInt gdof, goff; 194137d0c07bSMatthew G Knepley 19429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof)); 194337d0c07bSMatthew G Knepley if (gdof > 0) { 19449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff)); 194537d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 194637d0c07bSMatthew G Knepley PetscInt fdof, fcdof, fc; 194737d0c07bSMatthew G Knepley 19489566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof)); 19499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof)); 1950ad540459SPierre Jolivet for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++; 195137d0c07bSMatthew G Knepley } 195237d0c07bSMatthew G Knepley } 195337d0c07bSMatthew G Knepley } 19548865f1eaSKarl Rupp if (numFields) *numFields = nF; 195537d0c07bSMatthew G Knepley if (fieldNames) { 19569566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nF, fieldNames)); 195737d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 195837d0c07bSMatthew G Knepley const char *fieldName; 195937d0c07bSMatthew G Knepley 19609566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldName(section, f, &fieldName)); 19619566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f])); 196237d0c07bSMatthew G Knepley } 196337d0c07bSMatthew G Knepley } 196437d0c07bSMatthew G Knepley if (fields) { 19659566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nF, fields)); 196637d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 19673a544194SStefano Zampini PetscInt bs, in[2], out[2]; 19683a544194SStefano Zampini 19699566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f])); 19703a544194SStefano Zampini in[0] = -fieldNc[f]; 19713a544194SStefano Zampini in[1] = fieldNc[f]; 19721c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm))); 19733a544194SStefano Zampini bs = (-out[0] == out[1]) ? out[1] : 1; 19749566063dSJacob Faibussowitsch PetscCall(ISSetBlockSize((*fields)[f], bs)); 197537d0c07bSMatthew G Knepley } 197637d0c07bSMatthew G Knepley } 19779566063dSJacob Faibussowitsch PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices)); 1978dbbe0bcdSBarry Smith } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields); 19793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 19804d343eeaSMatthew G Knepley } 19814d343eeaSMatthew G Knepley 198216621825SDmitry Karpeev /*@C 1983bb7acecfSBarry Smith DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems 1984a4e35b19SJacob Faibussowitsch corresponding to different fields. 1985e7c4fc90SDmitry Karpeev 198620f4b53cSBarry Smith Not Collective; No Fortran Support 1987e7c4fc90SDmitry Karpeev 1988e7c4fc90SDmitry Karpeev Input Parameter: 1989bb7acecfSBarry Smith . dm - the `DM` object 1990e7c4fc90SDmitry Karpeev 1991e7c4fc90SDmitry Karpeev Output Parameters: 199220f4b53cSBarry Smith + len - The number of fields (or `NULL` if not requested) 199320f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested) 199420f4b53cSBarry Smith . islist - The global indices for each field (or `NULL` if not requested) 199520f4b53cSBarry Smith - dmlist - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined) 1996e7c4fc90SDmitry Karpeev 1997e7c4fc90SDmitry Karpeev Level: intermediate 1998e7c4fc90SDmitry Karpeev 1999a4e35b19SJacob Faibussowitsch Notes: 2000a4e35b19SJacob Faibussowitsch Each `IS` contains the global indices of the dofs of the corresponding field, defined by 2001a4e35b19SJacob Faibussowitsch `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem. 2002a4e35b19SJacob Faibussowitsch 2003a4e35b19SJacob Faibussowitsch The same as `DMCreateFieldIS()` but also returns a `DM` for each field. 2004a4e35b19SJacob Faibussowitsch 2005e7c4fc90SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 2006bb7acecfSBarry Smith `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`, 2007bb7acecfSBarry Smith and all of the arrays should be freed with `PetscFree()`. 2008e7c4fc90SDmitry Karpeev 200960225df5SJacob Faibussowitsch Developer Notes: 2010bb7acecfSBarry Smith It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing. 2011bb7acecfSBarry Smith 201260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()` 2013e7c4fc90SDmitry Karpeev @*/ 2014d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) 2015d71ae5a4SJacob Faibussowitsch { 2016e7c4fc90SDmitry Karpeev PetscFunctionBegin; 2017e7c4fc90SDmitry Karpeev PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 20188865f1eaSKarl Rupp if (len) { 20194f572ea9SToby Isaac PetscAssertPointer(len, 2); 20208865f1eaSKarl Rupp *len = 0; 20218865f1eaSKarl Rupp } 20228865f1eaSKarl Rupp if (namelist) { 20234f572ea9SToby Isaac PetscAssertPointer(namelist, 3); 2024ea78f98cSLisandro Dalcin *namelist = NULL; 20258865f1eaSKarl Rupp } 20268865f1eaSKarl Rupp if (islist) { 20274f572ea9SToby Isaac PetscAssertPointer(islist, 4); 2028ea78f98cSLisandro Dalcin *islist = NULL; 20298865f1eaSKarl Rupp } 20308865f1eaSKarl Rupp if (dmlist) { 20314f572ea9SToby Isaac PetscAssertPointer(dmlist, 5); 2032ea78f98cSLisandro Dalcin *dmlist = NULL; 20338865f1eaSKarl Rupp } 2034f3f0edfdSDmitry Karpeev /* 2035f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 2036f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 2037f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 2038f3f0edfdSDmitry Karpeev */ 20397a8be351SBarry Smith PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 204016621825SDmitry Karpeev if (!dm->ops->createfielddecomposition) { 2041435a35e8SMatthew G Knepley PetscSection section; 2042435a35e8SMatthew G Knepley PetscInt numFields, f; 2043435a35e8SMatthew G Knepley 20449566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 20459566063dSJacob Faibussowitsch if (section) PetscCall(PetscSectionGetNumFields(section, &numFields)); 2046435a35e8SMatthew G Knepley if (section && numFields && dm->ops->createsubdm) { 2047f25d98f1SMatthew G. Knepley if (len) *len = numFields; 20489566063dSJacob Faibussowitsch if (namelist) PetscCall(PetscMalloc1(numFields, namelist)); 20499566063dSJacob Faibussowitsch if (islist) PetscCall(PetscMalloc1(numFields, islist)); 20509566063dSJacob Faibussowitsch if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist)); 2051435a35e8SMatthew G Knepley for (f = 0; f < numFields; ++f) { 2052435a35e8SMatthew G Knepley const char *fieldName; 2053435a35e8SMatthew G Knepley 20549566063dSJacob Faibussowitsch PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL)); 205503dc3394SMatthew G. Knepley if (namelist) { 20569566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldName(section, f, &fieldName)); 20579566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f])); 2058435a35e8SMatthew G Knepley } 205903dc3394SMatthew G. Knepley } 2060435a35e8SMatthew G Knepley } else { 20619566063dSJacob Faibussowitsch PetscCall(DMCreateFieldIS(dm, len, namelist, islist)); 2062e7c4fc90SDmitry Karpeev /* By default there are no DMs associated with subproblems. */ 20630298fd71SBarry Smith if (dmlist) *dmlist = NULL; 2064e7c4fc90SDmitry Karpeev } 2065dbbe0bcdSBarry Smith } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist); 20663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 206716621825SDmitry Karpeev } 206816621825SDmitry Karpeev 2069412a4547SAlexis Marboeuf /*@C 207020f4b53cSBarry Smith DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in. 207120f4b53cSBarry Smith The fields are defined by `DMCreateFieldIS()`. 2072435a35e8SMatthew G Knepley 2073435a35e8SMatthew G Knepley Not collective 2074435a35e8SMatthew G Knepley 2075435a35e8SMatthew G Knepley Input Parameters: 2076bb7acecfSBarry Smith + dm - The `DM` object 2077bb7acecfSBarry Smith . numFields - The number of fields to select 20782adcc780SMatthew G. Knepley - fields - The field numbers of the selected fields 2079435a35e8SMatthew G Knepley 2080435a35e8SMatthew G Knepley Output Parameters: 2081bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM` 2082bb7acecfSBarry Smith - subdm - The `DM` for the subproblem 2083435a35e8SMatthew G Knepley 208420f4b53cSBarry Smith Level: intermediate 208520f4b53cSBarry Smith 2086bb7acecfSBarry Smith Note: 2087bb7acecfSBarry Smith You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed 20885d3b26e6SMatthew G. Knepley 208960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()` 2090435a35e8SMatthew G Knepley @*/ 2091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm) 2092d71ae5a4SJacob Faibussowitsch { 2093435a35e8SMatthew G Knepley PetscFunctionBegin; 2094435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 20954f572ea9SToby Isaac PetscAssertPointer(fields, 3); 20964f572ea9SToby Isaac if (is) PetscAssertPointer(is, 4); 20974f572ea9SToby Isaac if (subdm) PetscAssertPointer(subdm, 5); 2098dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm); 20993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2100435a35e8SMatthew G Knepley } 2101435a35e8SMatthew G Knepley 21022adcc780SMatthew G. Knepley /*@C 2103bb7acecfSBarry Smith DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in. 21042adcc780SMatthew G. Knepley 21052adcc780SMatthew G. Knepley Not collective 21062adcc780SMatthew G. Knepley 2107d8d19677SJose E. Roman Input Parameters: 2108bb7acecfSBarry Smith + dms - The `DM` objects 2109bb7acecfSBarry Smith - n - The number of `DM`s 21102adcc780SMatthew G. Knepley 21112adcc780SMatthew G. Knepley Output Parameters: 2112bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL 2113bb7acecfSBarry Smith - superdm - The `DM` for the superproblem 21142adcc780SMatthew G. Knepley 211520f4b53cSBarry Smith Level: intermediate 211620f4b53cSBarry Smith 2117bb7acecfSBarry Smith Note: 2118bb7acecfSBarry Smith You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed 21195d3b26e6SMatthew G. Knepley 21201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()` 21212adcc780SMatthew G. Knepley @*/ 2122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm) 2123d71ae5a4SJacob Faibussowitsch { 21242adcc780SMatthew G. Knepley PetscInt i; 21252adcc780SMatthew G. Knepley 21262adcc780SMatthew G. Knepley PetscFunctionBegin; 21274f572ea9SToby Isaac PetscAssertPointer(dms, 1); 2128ad540459SPierre Jolivet for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1); 21294f572ea9SToby Isaac if (is) PetscAssertPointer(is, 3); 21304f572ea9SToby Isaac PetscAssertPointer(superdm, 4); 2131bb7acecfSBarry Smith PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n); 2132bb7acecfSBarry Smith if (n) { 2133b9d85ea2SLisandro Dalcin DM dm = dms[0]; 2134dbbe0bcdSBarry Smith PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm)); 21352adcc780SMatthew G. Knepley } 21363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21372adcc780SMatthew G. Knepley } 21382adcc780SMatthew G. Knepley 213916621825SDmitry Karpeev /*@C 2140a4e35b19SJacob Faibussowitsch DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a 2141a4e35b19SJacob Faibussowitsch problem into subproblems corresponding to restrictions to pairs of nested subdomains. 214216621825SDmitry Karpeev 214320f4b53cSBarry Smith Not Collective 214416621825SDmitry Karpeev 214516621825SDmitry Karpeev Input Parameter: 2146bb7acecfSBarry Smith . dm - the `DM` object 214716621825SDmitry Karpeev 214816621825SDmitry Karpeev Output Parameters: 214920f4b53cSBarry Smith + n - The number of subproblems in the domain decomposition (or `NULL` if not requested) 215020f4b53cSBarry Smith . namelist - The name for each subdomain (or `NULL` if not requested) 21510298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested) 21520298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested) 215320f4b53cSBarry Smith - dmlist - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined) 215416621825SDmitry Karpeev 215516621825SDmitry Karpeev Level: intermediate 215616621825SDmitry Karpeev 2157bb7acecfSBarry Smith Note: 2158a4e35b19SJacob Faibussowitsch Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the 2159a4e35b19SJacob Faibussowitsch dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping 2160a4e35b19SJacob Faibussowitsch covering, while outer subdomains can overlap. 2161a4e35b19SJacob Faibussowitsch 2162a4e35b19SJacob Faibussowitsch The optional list of `DM`s define a `DM` for each subproblem. 2163a4e35b19SJacob Faibussowitsch 216416621825SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 2165bb7acecfSBarry Smith `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`, 2166bb7acecfSBarry Smith and all of the arrays should be freed with `PetscFree()`. 216716621825SDmitry Karpeev 2168a4e35b19SJacob Faibussowitsch Developer Notes: 216920f4b53cSBarry Smith The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains? 2170bb7acecfSBarry Smith 217160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()` 217216621825SDmitry Karpeev @*/ 2173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) 2174d71ae5a4SJacob Faibussowitsch { 2175be081cd6SPeter Brune DMSubDomainHookLink link; 2176be081cd6SPeter Brune PetscInt i, l; 217716621825SDmitry Karpeev 217816621825SDmitry Karpeev PetscFunctionBegin; 217916621825SDmitry Karpeev PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 21809371c9d4SSatish Balay if (n) { 21814f572ea9SToby Isaac PetscAssertPointer(n, 2); 21829371c9d4SSatish Balay *n = 0; 21839371c9d4SSatish Balay } 21849371c9d4SSatish Balay if (namelist) { 21854f572ea9SToby Isaac PetscAssertPointer(namelist, 3); 21869371c9d4SSatish Balay *namelist = NULL; 21879371c9d4SSatish Balay } 21889371c9d4SSatish Balay if (innerislist) { 21894f572ea9SToby Isaac PetscAssertPointer(innerislist, 4); 21909371c9d4SSatish Balay *innerislist = NULL; 21919371c9d4SSatish Balay } 21929371c9d4SSatish Balay if (outerislist) { 21934f572ea9SToby Isaac PetscAssertPointer(outerislist, 5); 21949371c9d4SSatish Balay *outerislist = NULL; 21959371c9d4SSatish Balay } 21969371c9d4SSatish Balay if (dmlist) { 21974f572ea9SToby Isaac PetscAssertPointer(dmlist, 6); 21989371c9d4SSatish Balay *dmlist = NULL; 21999371c9d4SSatish Balay } 2200f3f0edfdSDmitry Karpeev /* 2201f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 2202f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 2203f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 2204f3f0edfdSDmitry Karpeev */ 22057a8be351SBarry Smith PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 220616621825SDmitry Karpeev if (dm->ops->createdomaindecomposition) { 2207dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist); 220814a18fd3SPeter Brune /* copy subdomain hooks and context over to the subdomain DMs */ 2209f891f5b9SPatrick Sanan if (dmlist && *dmlist) { 2210be081cd6SPeter Brune for (i = 0; i < l; i++) { 2211be081cd6SPeter Brune for (link = dm->subdomainhook; link; link = link->next) { 22129566063dSJacob Faibussowitsch if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx)); 2213be081cd6SPeter Brune } 2214648262bbSPatrick Sanan if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx; 2215e7c4fc90SDmitry Karpeev } 221614a18fd3SPeter Brune } 2217bb7acecfSBarry Smith if (n) *n = l; 221814a18fd3SPeter Brune } 22193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2220e30e807fSPeter Brune } 2221e30e807fSPeter Brune 2222e30e807fSPeter Brune /*@C 2223e30e807fSPeter Brune DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector 2224e30e807fSPeter Brune 222520f4b53cSBarry Smith Not Collective 2226e30e807fSPeter Brune 2227e30e807fSPeter Brune Input Parameters: 2228bb7acecfSBarry Smith + dm - the `DM` object 2229e30e807fSPeter Brune . n - the number of subdomain scatters 2230e30e807fSPeter Brune - subdms - the local subdomains 2231e30e807fSPeter Brune 2232e30e807fSPeter Brune Output Parameters: 22336b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain 2234e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain 2235e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts) 2236e30e807fSPeter Brune 223720f4b53cSBarry Smith Level: developer 223820f4b53cSBarry Smith 2239bb7acecfSBarry Smith Note: 2240bb7acecfSBarry Smith This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution 2241e30e807fSPeter Brune of general nonlinear problems with overlapping subdomain methods. While merely having index sets that enable subsets 2242e30e807fSPeter Brune of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of 2243e30e807fSPeter Brune solution and residual data. 2244e30e807fSPeter Brune 2245a4e35b19SJacob Faibussowitsch Developer Notes: 2246a4e35b19SJacob Faibussowitsch Can the subdms input be anything or are they exactly the `DM` obtained from 2247a4e35b19SJacob Faibussowitsch `DMCreateDomainDecomposition()`? 2248bb7acecfSBarry Smith 22491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()` 2250e30e807fSPeter Brune @*/ 2251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat) 2252d71ae5a4SJacob Faibussowitsch { 2253e30e807fSPeter Brune PetscFunctionBegin; 2254e30e807fSPeter Brune PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 22554f572ea9SToby Isaac PetscAssertPointer(subdms, 3); 2256dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat); 22573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2258e7c4fc90SDmitry Karpeev } 2259e7c4fc90SDmitry Karpeev 226047c6ae99SBarry Smith /*@ 2261bb7acecfSBarry Smith DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh 226247c6ae99SBarry Smith 226320f4b53cSBarry Smith Collective 226447c6ae99SBarry Smith 2265d8d19677SJose E. Roman Input Parameters: 2266bb7acecfSBarry Smith + dm - the `DM` object 2267bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`) 226847c6ae99SBarry Smith 226947c6ae99SBarry Smith Output Parameter: 227020f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL` 2271ae0a1c52SMatthew G Knepley 227220f4b53cSBarry Smith Options Database Key: 2273412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex 2274412e9a14SMatthew G. Knepley 227547c6ae99SBarry Smith Level: developer 227647c6ae99SBarry Smith 227720f4b53cSBarry Smith Note: 227820f4b53cSBarry Smith If no refinement was done, the return value is `NULL` 227920f4b53cSBarry Smith 22801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 228147c6ae99SBarry Smith @*/ 2282d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf) 2283d71ae5a4SJacob Faibussowitsch { 2284c833c3b5SJed Brown DMRefineHookLink link; 228547c6ae99SBarry Smith 228647c6ae99SBarry Smith PetscFunctionBegin; 2287732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 22889566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0)); 2289dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, refine, comm, dmf); 22904057135bSMatthew G Knepley if (*dmf) { 229143842a1eSJed Brown (*dmf)->ops->creatematrix = dm->ops->creatematrix; 22928865f1eaSKarl Rupp 22939566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf)); 22948865f1eaSKarl Rupp 2295644e2e5bSBarry Smith (*dmf)->ctx = dm->ctx; 22960598a293SJed Brown (*dmf)->leveldown = dm->leveldown; 2297656b349aSBarry Smith (*dmf)->levelup = dm->levelup + 1; 22988865f1eaSKarl Rupp 22999566063dSJacob Faibussowitsch PetscCall(DMSetMatType(*dmf, dm->mattype)); 2300c833c3b5SJed Brown for (link = dm->refinehook; link; link = link->next) { 23011baa6e33SBarry Smith if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx)); 2302c833c3b5SJed Brown } 2303c833c3b5SJed Brown } 23049566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0)); 23053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2306c833c3b5SJed Brown } 2307c833c3b5SJed Brown 2308bb9467b5SJed Brown /*@C 2309c833c3b5SJed Brown DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid 2310c833c3b5SJed Brown 231120f4b53cSBarry Smith Logically Collective; No Fortran Support 2312c833c3b5SJed Brown 23134165533cSJose E. Roman Input Parameters: 2314bb7acecfSBarry Smith + coarse - `DM` on which to run a hook when interpolating to a finer level 2315bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level 2316f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`) 231720f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 2318c833c3b5SJed Brown 231920f4b53cSBarry Smith Calling sequence of `refinehook`: 2320bb7acecfSBarry Smith + coarse - coarse level `DM` 2321bb7acecfSBarry Smith . fine - fine level `DM` to interpolate problem to 2322c833c3b5SJed Brown - ctx - optional user-defined function context 2323c833c3b5SJed Brown 232420f4b53cSBarry Smith Calling sequence of `interphook`: 2325bb7acecfSBarry Smith + coarse - coarse level `DM` 2326c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid 2327bb7acecfSBarry Smith . fine - fine level `DM` to update 2328c833c3b5SJed Brown - ctx - optional user-defined function context 2329c833c3b5SJed Brown 2330c833c3b5SJed Brown Level: advanced 2331c833c3b5SJed Brown 2332c833c3b5SJed Brown Notes: 2333bb7acecfSBarry Smith This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be 2334bb7acecfSBarry Smith passed to fine grids while grid sequencing. 2335bb7acecfSBarry Smith 2336bb7acecfSBarry Smith The actual interpolation is done when `DMInterpolate()` is called. 2337c833c3b5SJed Brown 2338c833c3b5SJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 2339c833c3b5SJed Brown 23401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 2341c833c3b5SJed Brown @*/ 2342a4e35b19SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM coarse, DM fine, void *ctx), PetscErrorCode (*interphook)(DM coarse, Mat interp, DM fine, void *ctx), void *ctx) 2343d71ae5a4SJacob Faibussowitsch { 2344c833c3b5SJed Brown DMRefineHookLink link, *p; 2345c833c3b5SJed Brown 2346c833c3b5SJed Brown PetscFunctionBegin; 2347c833c3b5SJed Brown PetscValidHeaderSpecific(coarse, DM_CLASSID, 1); 23483d8e3701SJed Brown for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */ 23493ba16761SJacob Faibussowitsch if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS); 23503d8e3701SJed Brown } 23519566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 2352c833c3b5SJed Brown link->refinehook = refinehook; 2353c833c3b5SJed Brown link->interphook = interphook; 2354c833c3b5SJed Brown link->ctx = ctx; 23550298fd71SBarry Smith link->next = NULL; 2356c833c3b5SJed Brown *p = link; 23573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2358c833c3b5SJed Brown } 2359c833c3b5SJed Brown 23603d8e3701SJed Brown /*@C 2361bb7acecfSBarry Smith DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating 2362bb7acecfSBarry Smith a nonlinear problem to a finer grid 23633d8e3701SJed Brown 236420f4b53cSBarry Smith Logically Collective; No Fortran Support 23653d8e3701SJed Brown 23664165533cSJose E. Roman Input Parameters: 2367bb7acecfSBarry Smith + coarse - the `DM` on which to run a hook when restricting to a coarser level 2368bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level 2369bb7acecfSBarry Smith . interphook - function to run to update data on finer levels 237020f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 23713d8e3701SJed Brown 23723d8e3701SJed Brown Level: advanced 23733d8e3701SJed Brown 2374bb7acecfSBarry Smith Note: 23753d8e3701SJed Brown This function does nothing if the hook is not in the list. 23763d8e3701SJed Brown 23771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 23783d8e3701SJed Brown @*/ 2379d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx) 2380d71ae5a4SJacob Faibussowitsch { 23813d8e3701SJed Brown DMRefineHookLink link, *p; 23823d8e3701SJed Brown 23833d8e3701SJed Brown PetscFunctionBegin; 23843d8e3701SJed Brown PetscValidHeaderSpecific(coarse, DM_CLASSID, 1); 23853d8e3701SJed Brown for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */ 23863d8e3701SJed Brown if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) { 23873d8e3701SJed Brown link = *p; 23883d8e3701SJed Brown *p = link->next; 23899566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 23903d8e3701SJed Brown break; 23913d8e3701SJed Brown } 23923d8e3701SJed Brown } 23933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23943d8e3701SJed Brown } 23953d8e3701SJed Brown 2396c833c3b5SJed Brown /*@ 2397bb7acecfSBarry Smith DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()` 2398c833c3b5SJed Brown 2399c833c3b5SJed Brown Collective if any hooks are 2400c833c3b5SJed Brown 24014165533cSJose E. Roman Input Parameters: 2402bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base 2403bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()` 2404bb7acecfSBarry Smith - fine - finer `DM` to update 2405c833c3b5SJed Brown 2406c833c3b5SJed Brown Level: developer 2407c833c3b5SJed Brown 240860225df5SJacob Faibussowitsch Developer Notes: 2409bb7acecfSBarry Smith This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an 2410bb7acecfSBarry Smith an API with consistent terminology. 2411bb7acecfSBarry Smith 24121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()` 2413c833c3b5SJed Brown @*/ 2414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine) 2415d71ae5a4SJacob Faibussowitsch { 2416c833c3b5SJed Brown DMRefineHookLink link; 2417c833c3b5SJed Brown 2418c833c3b5SJed Brown PetscFunctionBegin; 2419c833c3b5SJed Brown for (link = fine->refinehook; link; link = link->next) { 24201baa6e33SBarry Smith if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx)); 24214057135bSMatthew G Knepley } 24223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 242347c6ae99SBarry Smith } 242447c6ae99SBarry Smith 2425eb3f98d2SBarry Smith /*@ 24261f3379b2SToby Isaac DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh. 24271f3379b2SToby Isaac 242820f4b53cSBarry Smith Collective 24291f3379b2SToby Isaac 24304165533cSJose E. Roman Input Parameters: 2431bb7acecfSBarry Smith + coarse - coarse `DM` 2432bb7acecfSBarry Smith . fine - fine `DM` 2433bb7acecfSBarry Smith . interp - (optional) the matrix computed by `DMCreateInterpolation()`. Implementations may not need this, but if it 2434bb7acecfSBarry Smith is available it can avoid some recomputation. If it is provided, `MatInterpolate()` will be used if 2435bb7acecfSBarry Smith the coarse `DM` does not have a specialized implementation. 24361f3379b2SToby Isaac - coarseSol - solution on the coarse mesh 24371f3379b2SToby Isaac 24384165533cSJose E. Roman Output Parameter: 24391f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh 24401f3379b2SToby Isaac 24411f3379b2SToby Isaac Level: developer 24421f3379b2SToby Isaac 2443bb7acecfSBarry Smith Note: 2444bb7acecfSBarry Smith This function exists because the interpolation of a solution vector between meshes is not always a linear 24451f3379b2SToby Isaac map. For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed 24461f3379b2SToby Isaac out of the solution vector. Or if interpolation is inherently a nonlinear operation, such as a method using 24471f3379b2SToby Isaac slope-limiting reconstruction. 24481f3379b2SToby Isaac 244960225df5SJacob Faibussowitsch Developer Notes: 2450bb7acecfSBarry Smith This doesn't just interpolate "solutions" so its API name is questionable. 2451bb7acecfSBarry Smith 24521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()` 24531f3379b2SToby Isaac @*/ 2454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol) 2455d71ae5a4SJacob Faibussowitsch { 24561f3379b2SToby Isaac PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL; 24571f3379b2SToby Isaac 24581f3379b2SToby Isaac PetscFunctionBegin; 24591f3379b2SToby Isaac PetscValidHeaderSpecific(coarse, DM_CLASSID, 1); 24601f3379b2SToby Isaac if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3); 24611f3379b2SToby Isaac PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4); 24621f3379b2SToby Isaac PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5); 24631f3379b2SToby Isaac 24649566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol)); 24651f3379b2SToby Isaac if (interpsol) { 24669566063dSJacob Faibussowitsch PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol)); 24671f3379b2SToby Isaac } else if (interp) { 24689566063dSJacob Faibussowitsch PetscCall(MatInterpolate(interp, coarseSol, fineSol)); 246998921bdaSJacob Faibussowitsch } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name); 24703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24711f3379b2SToby Isaac } 24721f3379b2SToby Isaac 24731f3379b2SToby Isaac /*@ 2474bb7acecfSBarry Smith DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`. 2475eb3f98d2SBarry Smith 2476eb3f98d2SBarry Smith Not Collective 2477eb3f98d2SBarry Smith 2478eb3f98d2SBarry Smith Input Parameter: 2479bb7acecfSBarry Smith . dm - the `DM` object 2480eb3f98d2SBarry Smith 2481eb3f98d2SBarry Smith Output Parameter: 2482eb3f98d2SBarry Smith . level - number of refinements 2483eb3f98d2SBarry Smith 2484eb3f98d2SBarry Smith Level: developer 2485eb3f98d2SBarry Smith 2486bb7acecfSBarry Smith Note: 2487bb7acecfSBarry Smith This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver. 2488bb7acecfSBarry Smith 24891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 2490eb3f98d2SBarry Smith @*/ 2491d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level) 2492d71ae5a4SJacob Faibussowitsch { 2493eb3f98d2SBarry Smith PetscFunctionBegin; 2494eb3f98d2SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2495eb3f98d2SBarry Smith *level = dm->levelup; 24963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2497eb3f98d2SBarry Smith } 2498eb3f98d2SBarry Smith 2499fef3a512SBarry Smith /*@ 2500bb7acecfSBarry Smith DMSetRefineLevel - Sets the number of refinements that have generated this `DM`. 2501fef3a512SBarry Smith 2502fef3a512SBarry Smith Not Collective 2503fef3a512SBarry Smith 2504d8d19677SJose E. Roman Input Parameters: 2505bb7acecfSBarry Smith + dm - the `DM` object 2506fef3a512SBarry Smith - level - number of refinements 2507fef3a512SBarry Smith 2508fef3a512SBarry Smith Level: advanced 2509fef3a512SBarry Smith 251095452b02SPatrick Sanan Notes: 2511bb7acecfSBarry Smith This value is used by `PCMG` to determine how many multigrid levels to use 2512fef3a512SBarry Smith 2513bb7acecfSBarry Smith The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine. 2514bb7acecfSBarry Smith 25151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 2516fef3a512SBarry Smith @*/ 2517d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level) 2518d71ae5a4SJacob Faibussowitsch { 2519fef3a512SBarry Smith PetscFunctionBegin; 2520fef3a512SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2521fef3a512SBarry Smith dm->levelup = level; 25223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2523fef3a512SBarry Smith } 2524fef3a512SBarry Smith 2525d410b0cfSMatthew G. Knepley /*@ 2526bb7acecfSBarry Smith DMExtrude - Extrude a `DM` object from a surface 2527d410b0cfSMatthew G. Knepley 252820f4b53cSBarry Smith Collective 2529d410b0cfSMatthew G. Knepley 2530f1a722f8SMatthew G. Knepley Input Parameters: 2531bb7acecfSBarry Smith + dm - the `DM` object 2532d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers 2533d410b0cfSMatthew G. Knepley 2534d410b0cfSMatthew G. Knepley Output Parameter: 253520f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL` 2536d410b0cfSMatthew G. Knepley 2537d410b0cfSMatthew G. Knepley Level: developer 2538d410b0cfSMatthew G. Knepley 253920f4b53cSBarry Smith Note: 254020f4b53cSBarry Smith If no extrusion was done, the return value is `NULL` 254120f4b53cSBarry Smith 25421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()` 2543d410b0cfSMatthew G. Knepley @*/ 2544d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme) 2545d71ae5a4SJacob Faibussowitsch { 2546d410b0cfSMatthew G. Knepley PetscFunctionBegin; 2547d410b0cfSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2548dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, extrude, layers, dme); 2549d410b0cfSMatthew G. Knepley if (*dme) { 2550d410b0cfSMatthew G. Knepley (*dme)->ops->creatematrix = dm->ops->creatematrix; 25519566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme)); 2552d410b0cfSMatthew G. Knepley (*dme)->ctx = dm->ctx; 25539566063dSJacob Faibussowitsch PetscCall(DMSetMatType(*dme, dm->mattype)); 2554d410b0cfSMatthew G. Knepley } 25553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2556d410b0cfSMatthew G. Knepley } 2557d410b0cfSMatthew G. Knepley 2558d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm) 2559d71ae5a4SJacob Faibussowitsch { 2560ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 2561ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 25624f572ea9SToby Isaac PetscAssertPointer(tdm, 2); 2563ca3d3a14SMatthew G. Knepley *tdm = dm->transformDM; 25643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2565ca3d3a14SMatthew G. Knepley } 2566ca3d3a14SMatthew G. Knepley 2567d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv) 2568d71ae5a4SJacob Faibussowitsch { 2569ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 2570ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 25714f572ea9SToby Isaac PetscAssertPointer(tv, 2); 2572ca3d3a14SMatthew G. Knepley *tv = dm->transform; 25733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2574ca3d3a14SMatthew G. Knepley } 2575ca3d3a14SMatthew G. Knepley 2576ca3d3a14SMatthew G. Knepley /*@ 2577bb7acecfSBarry Smith DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors 2578ca3d3a14SMatthew G. Knepley 2579ca3d3a14SMatthew G. Knepley Input Parameter: 258020f4b53cSBarry Smith . dm - The `DM` 2581ca3d3a14SMatthew G. Knepley 2582ca3d3a14SMatthew G. Knepley Output Parameter: 258320f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done 2584ca3d3a14SMatthew G. Knepley 2585ca3d3a14SMatthew G. Knepley Level: developer 2586ca3d3a14SMatthew G. Knepley 25871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()` 2588ca3d3a14SMatthew G. Knepley @*/ 2589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg) 2590d71ae5a4SJacob Faibussowitsch { 2591ca3d3a14SMatthew G. Knepley Vec tv; 2592ca3d3a14SMatthew G. Knepley 2593ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 2594ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 25954f572ea9SToby Isaac PetscAssertPointer(flg, 2); 25969566063dSJacob Faibussowitsch PetscCall(DMGetBasisTransformVec_Internal(dm, &tv)); 2597ca3d3a14SMatthew G. Knepley *flg = tv ? PETSC_TRUE : PETSC_FALSE; 25983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2599ca3d3a14SMatthew G. Knepley } 2600ca3d3a14SMatthew G. Knepley 2601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm) 2602d71ae5a4SJacob Faibussowitsch { 2603ca3d3a14SMatthew G. Knepley PetscSection s, ts; 2604ca3d3a14SMatthew G. Knepley PetscScalar *ta; 2605ca3d3a14SMatthew G. Knepley PetscInt cdim, pStart, pEnd, p, Nf, f, Nc, dof; 2606ca3d3a14SMatthew G. Knepley 2607ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 26089566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 26099566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 26109566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 26119566063dSJacob Faibussowitsch PetscCall(PetscSectionGetNumFields(s, &Nf)); 26129566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dm->transformDM)); 26139566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm->transformDM, &ts)); 26149566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(ts, Nf)); 26159566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(ts, pStart, pEnd)); 2616ca3d3a14SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 26179566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldComponents(s, f, &Nc)); 2618ca3d3a14SMatthew G. Knepley /* We could start to label fields by their transformation properties */ 2619ca3d3a14SMatthew G. Knepley if (Nc != cdim) continue; 2620ca3d3a14SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 26219566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(s, p, f, &dof)); 2622ca3d3a14SMatthew G. Knepley if (!dof) continue; 26239566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim))); 26249566063dSJacob Faibussowitsch PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim))); 2625ca3d3a14SMatthew G. Knepley } 2626ca3d3a14SMatthew G. Knepley } 26279566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(ts)); 26289566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform)); 26299566063dSJacob Faibussowitsch PetscCall(VecGetArray(dm->transform, &ta)); 2630ca3d3a14SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 2631ca3d3a14SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 26329566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof)); 2633ca3d3a14SMatthew G. Knepley if (dof) { 2634ca3d3a14SMatthew G. Knepley PetscReal x[3] = {0.0, 0.0, 0.0}; 2635ca3d3a14SMatthew G. Knepley PetscScalar *tva; 2636ca3d3a14SMatthew G. Knepley const PetscScalar *A; 2637ca3d3a14SMatthew G. Knepley 2638ca3d3a14SMatthew G. Knepley /* TODO Get quadrature point for this dual basis vector for coordinate */ 26399566063dSJacob Faibussowitsch PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx)); 26409566063dSJacob Faibussowitsch PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva)); 26419566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim))); 2642ca3d3a14SMatthew G. Knepley } 2643ca3d3a14SMatthew G. Knepley } 2644ca3d3a14SMatthew G. Knepley } 26459566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(dm->transform, &ta)); 26463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2647ca3d3a14SMatthew G. Knepley } 2648ca3d3a14SMatthew G. Knepley 2649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm) 2650d71ae5a4SJacob Faibussowitsch { 2651ca3d3a14SMatthew G. Knepley PetscFunctionBegin; 2652ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2653ca3d3a14SMatthew G. Knepley PetscValidHeaderSpecific(newdm, DM_CLASSID, 2); 2654ca3d3a14SMatthew G. Knepley newdm->transformCtx = dm->transformCtx; 2655ca3d3a14SMatthew G. Knepley newdm->transformSetUp = dm->transformSetUp; 2656ca3d3a14SMatthew G. Knepley newdm->transformDestroy = NULL; 2657ca3d3a14SMatthew G. Knepley newdm->transformGetMatrix = dm->transformGetMatrix; 26589566063dSJacob Faibussowitsch if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm)); 26593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2660ca3d3a14SMatthew G. Knepley } 2661ca3d3a14SMatthew G. Knepley 2662bb9467b5SJed Brown /*@C 2663bb7acecfSBarry Smith DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called 2664baf369e7SPeter Brune 266520f4b53cSBarry Smith Logically Collective 2666baf369e7SPeter Brune 26674165533cSJose E. Roman Input Parameters: 2668bb7acecfSBarry Smith + dm - the `DM` 2669bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()` 2670bb7acecfSBarry Smith . endhook - function to run after `DMGlobalToLocalEnd()` has completed 267120f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 2672baf369e7SPeter Brune 267320f4b53cSBarry Smith Calling sequence of `beginhook`: 2674a4e35b19SJacob Faibussowitsch + dm - global `DM` 2675baf369e7SPeter Brune . g - global vector 2676baf369e7SPeter Brune . mode - mode 2677baf369e7SPeter Brune . l - local vector 2678baf369e7SPeter Brune - ctx - optional user-defined function context 2679baf369e7SPeter Brune 268020f4b53cSBarry Smith Calling sequence of `endhook`: 2681a4e35b19SJacob Faibussowitsch + dm - global `DM` 2682a4e35b19SJacob Faibussowitsch . g - global vector 2683a4e35b19SJacob Faibussowitsch . mode - mode 2684a4e35b19SJacob Faibussowitsch . l - local vector 2685baf369e7SPeter Brune - ctx - optional user-defined function context 2686baf369e7SPeter Brune 2687baf369e7SPeter Brune Level: advanced 2688baf369e7SPeter Brune 2689bb7acecfSBarry Smith Note: 2690bb7acecfSBarry Smith The hook may be used to provide, for example, values that represent boundary conditions in the local vectors that do not exist on the global vector. 2691bb7acecfSBarry Smith 26921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 2693baf369e7SPeter Brune @*/ 2694a4e35b19SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), PetscErrorCode (*endhook)(DM dm, Vec g, InsertMode mode, Vec l, void *ctx), void *ctx) 2695d71ae5a4SJacob Faibussowitsch { 2696baf369e7SPeter Brune DMGlobalToLocalHookLink link, *p; 2697baf369e7SPeter Brune 2698baf369e7SPeter Brune PetscFunctionBegin; 2699baf369e7SPeter Brune PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2700baf369e7SPeter Brune for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */ 27019566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 2702baf369e7SPeter Brune link->beginhook = beginhook; 2703baf369e7SPeter Brune link->endhook = endhook; 2704baf369e7SPeter Brune link->ctx = ctx; 27050298fd71SBarry Smith link->next = NULL; 2706baf369e7SPeter Brune *p = link; 27073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2708baf369e7SPeter Brune } 2709baf369e7SPeter Brune 2710d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx) 2711d71ae5a4SJacob Faibussowitsch { 27124c274da1SToby Isaac Mat cMat; 271379769bd5SJed Brown Vec cVec, cBias; 27144c274da1SToby Isaac PetscSection section, cSec; 27154c274da1SToby Isaac PetscInt pStart, pEnd, p, dof; 27164c274da1SToby Isaac 27174c274da1SToby Isaac PetscFunctionBegin; 2718a4e35b19SJacob Faibussowitsch (void)g; 2719a4e35b19SJacob Faibussowitsch (void)ctx; 27204c274da1SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 27219566063dSJacob Faibussowitsch PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias)); 27224c274da1SToby Isaac if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) { 27235db9a05bSToby Isaac PetscInt nRows; 27245db9a05bSToby Isaac 27259566063dSJacob Faibussowitsch PetscCall(MatGetSize(cMat, &nRows, NULL)); 27263ba16761SJacob Faibussowitsch if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS); 27279566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 27289566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(cMat, NULL, &cVec)); 27299566063dSJacob Faibussowitsch PetscCall(MatMult(cMat, l, cVec)); 27309566063dSJacob Faibussowitsch if (cBias) PetscCall(VecAXPY(cVec, 1., cBias)); 27319566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd)); 27324c274da1SToby Isaac for (p = pStart; p < pEnd; p++) { 27339566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cSec, p, &dof)); 27344c274da1SToby Isaac if (dof) { 27354c274da1SToby Isaac PetscScalar *vals; 27369566063dSJacob Faibussowitsch PetscCall(VecGetValuesSection(cVec, cSec, p, &vals)); 27379566063dSJacob Faibussowitsch PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES)); 27384c274da1SToby Isaac } 27394c274da1SToby Isaac } 27409566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cVec)); 27414c274da1SToby Isaac } 27423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 27434c274da1SToby Isaac } 27444c274da1SToby Isaac 274547c6ae99SBarry Smith /*@ 274601729b5cSPatrick Sanan DMGlobalToLocal - update local vectors from global vector 274701729b5cSPatrick Sanan 274820f4b53cSBarry Smith Neighbor-wise Collective 274901729b5cSPatrick Sanan 275001729b5cSPatrick Sanan Input Parameters: 2751bb7acecfSBarry Smith + dm - the `DM` object 275201729b5cSPatrick Sanan . g - the global vector 2753bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES` 275401729b5cSPatrick Sanan - l - the local vector 275501729b5cSPatrick Sanan 275620f4b53cSBarry Smith Level: beginner 275720f4b53cSBarry Smith 275801729b5cSPatrick Sanan Notes: 2759bb7acecfSBarry Smith The communication involved in this update can be overlapped with computation by instead using 2760bb7acecfSBarry Smith `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`. 2761bb7acecfSBarry Smith 2762bb7acecfSBarry Smith `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process. 276301729b5cSPatrick Sanan 27641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, 276560225df5SJacob Faibussowitsch `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, 2766bb7acecfSBarry Smith `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()` 276701729b5cSPatrick Sanan @*/ 2768d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l) 2769d71ae5a4SJacob Faibussowitsch { 277001729b5cSPatrick Sanan PetscFunctionBegin; 27719566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalBegin(dm, g, mode, l)); 27729566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalEnd(dm, g, mode, l)); 27733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 277401729b5cSPatrick Sanan } 277501729b5cSPatrick Sanan 277601729b5cSPatrick Sanan /*@ 277747c6ae99SBarry Smith DMGlobalToLocalBegin - Begins updating local vectors from global vector 277847c6ae99SBarry Smith 277920f4b53cSBarry Smith Neighbor-wise Collective 278047c6ae99SBarry Smith 278147c6ae99SBarry Smith Input Parameters: 2782bb7acecfSBarry Smith + dm - the `DM` object 278347c6ae99SBarry Smith . g - the global vector 2784bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES` 278547c6ae99SBarry Smith - l - the local vector 278647c6ae99SBarry Smith 278701729b5cSPatrick Sanan Level: intermediate 278847c6ae99SBarry Smith 2789bb7acecfSBarry Smith Notes: 2790bb7acecfSBarry Smith The operation is completed with `DMGlobalToLocalEnd()` 2791bb7acecfSBarry Smith 2792bb7acecfSBarry Smith One can perform local computations between the `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()` to overlap communication and computation 2793bb7acecfSBarry Smith 2794bb7acecfSBarry Smith `DMGlobalToLocal()` is a short form of `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()` 2795bb7acecfSBarry Smith 2796bb7acecfSBarry Smith `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process. 2797bb7acecfSBarry Smith 279860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()` 279947c6ae99SBarry Smith @*/ 2800d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) 2801d71ae5a4SJacob Faibussowitsch { 28027128ae9fSMatthew G Knepley PetscSF sf; 2803baf369e7SPeter Brune DMGlobalToLocalHookLink link; 280447c6ae99SBarry Smith 280547c6ae99SBarry Smith PetscFunctionBegin; 2806171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2807baf369e7SPeter Brune for (link = dm->gtolhook; link; link = link->next) { 28081baa6e33SBarry Smith if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx)); 2809baf369e7SPeter Brune } 28109566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &sf)); 28117128ae9fSMatthew G Knepley if (sf) { 2812ae5cfb4aSMatthew G. Knepley const PetscScalar *gArray; 2813ae5cfb4aSMatthew G. Knepley PetscScalar *lArray; 2814d0295fc0SJunchao Zhang PetscMemType lmtype, gmtype; 28157128ae9fSMatthew G Knepley 28167a8be351SBarry Smith PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode); 28179566063dSJacob Faibussowitsch PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype)); 28189566063dSJacob Faibussowitsch PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype)); 28199566063dSJacob Faibussowitsch PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE)); 28209566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayAndMemType(l, &lArray)); 28219566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayReadAndMemType(g, &gArray)); 28227128ae9fSMatthew G Knepley } else { 28239566063dSJacob Faibussowitsch PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l)); 28247128ae9fSMatthew G Knepley } 28253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 282647c6ae99SBarry Smith } 282747c6ae99SBarry Smith 282847c6ae99SBarry Smith /*@ 282947c6ae99SBarry Smith DMGlobalToLocalEnd - Ends updating local vectors from global vector 283047c6ae99SBarry Smith 283120f4b53cSBarry Smith Neighbor-wise Collective 283247c6ae99SBarry Smith 283347c6ae99SBarry Smith Input Parameters: 2834bb7acecfSBarry Smith + dm - the `DM` object 283547c6ae99SBarry Smith . g - the global vector 2836bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES` 283747c6ae99SBarry Smith - l - the local vector 283847c6ae99SBarry Smith 283901729b5cSPatrick Sanan Level: intermediate 284047c6ae99SBarry Smith 2841bb7acecfSBarry Smith Note: 2842bb7acecfSBarry Smith See `DMGlobalToLocalBegin()` for details. 2843bb7acecfSBarry Smith 284460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()` 284547c6ae99SBarry Smith @*/ 2846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) 2847d71ae5a4SJacob Faibussowitsch { 28487128ae9fSMatthew G Knepley PetscSF sf; 2849ae5cfb4aSMatthew G. Knepley const PetscScalar *gArray; 2850ae5cfb4aSMatthew G. Knepley PetscScalar *lArray; 2851ca3d3a14SMatthew G. Knepley PetscBool transform; 2852baf369e7SPeter Brune DMGlobalToLocalHookLink link; 2853d0295fc0SJunchao Zhang PetscMemType lmtype, gmtype; 285447c6ae99SBarry Smith 285547c6ae99SBarry Smith PetscFunctionBegin; 2856171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 28579566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &sf)); 28589566063dSJacob Faibussowitsch PetscCall(DMHasBasisTransform(dm, &transform)); 28597128ae9fSMatthew G Knepley if (sf) { 28607a8be351SBarry Smith PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode); 28617128ae9fSMatthew G Knepley 28629566063dSJacob Faibussowitsch PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype)); 28639566063dSJacob Faibussowitsch PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype)); 28649566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE)); 28659566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayAndMemType(l, &lArray)); 28669566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayReadAndMemType(g, &gArray)); 28679566063dSJacob Faibussowitsch if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l)); 28687128ae9fSMatthew G Knepley } else { 28699566063dSJacob Faibussowitsch PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l)); 28707128ae9fSMatthew G Knepley } 28719566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL)); 2872baf369e7SPeter Brune for (link = dm->gtolhook; link; link = link->next) { 28739566063dSJacob Faibussowitsch if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx)); 2874baf369e7SPeter Brune } 28753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 287647c6ae99SBarry Smith } 287747c6ae99SBarry Smith 2878d4d07f1eSToby Isaac /*@C 2879d4d07f1eSToby Isaac DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called 2880d4d07f1eSToby Isaac 288120f4b53cSBarry Smith Logically Collective 2882d4d07f1eSToby Isaac 28834165533cSJose E. Roman Input Parameters: 2884bb7acecfSBarry Smith + dm - the `DM` 2885bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()` 2886bb7acecfSBarry Smith . endhook - function to run after `DMLocalToGlobalEnd()` has completed 288720f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 2888d4d07f1eSToby Isaac 288920f4b53cSBarry Smith Calling sequence of `beginhook`: 2890a4e35b19SJacob Faibussowitsch + global - global `DM` 2891d4d07f1eSToby Isaac . l - local vector 2892d4d07f1eSToby Isaac . mode - mode 2893d4d07f1eSToby Isaac . g - global vector 2894d4d07f1eSToby Isaac - ctx - optional user-defined function context 2895d4d07f1eSToby Isaac 289620f4b53cSBarry Smith Calling sequence of `endhook`: 2897bb7acecfSBarry Smith + global - global `DM` 2898d4d07f1eSToby Isaac . l - local vector 2899d4d07f1eSToby Isaac . mode - mode 2900d4d07f1eSToby Isaac . g - global vector 2901d4d07f1eSToby Isaac - ctx - optional user-defined function context 2902d4d07f1eSToby Isaac 2903d4d07f1eSToby Isaac Level: advanced 2904d4d07f1eSToby Isaac 29051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 2906d4d07f1eSToby Isaac @*/ 2907a4e35b19SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), PetscErrorCode (*endhook)(DM global, Vec l, InsertMode mode, Vec g, void *ctx), void *ctx) 2908d71ae5a4SJacob Faibussowitsch { 2909d4d07f1eSToby Isaac DMLocalToGlobalHookLink link, *p; 2910d4d07f1eSToby Isaac 2911d4d07f1eSToby Isaac PetscFunctionBegin; 2912d4d07f1eSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2913d4d07f1eSToby Isaac for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */ 29149566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 2915d4d07f1eSToby Isaac link->beginhook = beginhook; 2916d4d07f1eSToby Isaac link->endhook = endhook; 2917d4d07f1eSToby Isaac link->ctx = ctx; 2918d4d07f1eSToby Isaac link->next = NULL; 2919d4d07f1eSToby Isaac *p = link; 29203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2921d4d07f1eSToby Isaac } 2922d4d07f1eSToby Isaac 2923d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx) 2924d71ae5a4SJacob Faibussowitsch { 29254c274da1SToby Isaac Mat cMat; 29264c274da1SToby Isaac Vec cVec; 29274c274da1SToby Isaac PetscSection section, cSec; 29284c274da1SToby Isaac PetscInt pStart, pEnd, p, dof; 29294c274da1SToby Isaac 29304c274da1SToby Isaac PetscFunctionBegin; 2931a4e35b19SJacob Faibussowitsch (void)g; 2932a4e35b19SJacob Faibussowitsch (void)ctx; 29334c274da1SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 29349566063dSJacob Faibussowitsch PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL)); 29354c274da1SToby Isaac if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) { 29365db9a05bSToby Isaac PetscInt nRows; 29375db9a05bSToby Isaac 29389566063dSJacob Faibussowitsch PetscCall(MatGetSize(cMat, &nRows, NULL)); 29393ba16761SJacob Faibussowitsch if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS); 29409566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 29419566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(cMat, NULL, &cVec)); 29429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd)); 29434c274da1SToby Isaac for (p = pStart; p < pEnd; p++) { 29449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(cSec, p, &dof)); 29454c274da1SToby Isaac if (dof) { 29464c274da1SToby Isaac PetscInt d; 29474c274da1SToby Isaac PetscScalar *vals; 29489566063dSJacob Faibussowitsch PetscCall(VecGetValuesSection(l, section, p, &vals)); 29499566063dSJacob Faibussowitsch PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode)); 29504c274da1SToby Isaac /* for this to be the true transpose, we have to zero the values that 29514c274da1SToby Isaac * we just extracted */ 2952ad540459SPierre Jolivet for (d = 0; d < dof; d++) vals[d] = 0.; 29534c274da1SToby Isaac } 29544c274da1SToby Isaac } 29559566063dSJacob Faibussowitsch PetscCall(MatMultTransposeAdd(cMat, cVec, l, l)); 29569566063dSJacob Faibussowitsch PetscCall(VecDestroy(&cVec)); 29574c274da1SToby Isaac } 29583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29594c274da1SToby Isaac } 296001729b5cSPatrick Sanan /*@ 296101729b5cSPatrick Sanan DMLocalToGlobal - updates global vectors from local vectors 296201729b5cSPatrick Sanan 296320f4b53cSBarry Smith Neighbor-wise Collective 296401729b5cSPatrick Sanan 296501729b5cSPatrick Sanan Input Parameters: 2966bb7acecfSBarry Smith + dm - the `DM` object 296701729b5cSPatrick Sanan . l - the local vector 2968bb7acecfSBarry Smith . mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point. 296901729b5cSPatrick Sanan - g - the global vector 297001729b5cSPatrick Sanan 297120f4b53cSBarry Smith Level: beginner 297220f4b53cSBarry Smith 297301729b5cSPatrick Sanan Notes: 297401729b5cSPatrick Sanan The communication involved in this update can be overlapped with computation by using 2975bb7acecfSBarry Smith `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`. 297601729b5cSPatrick Sanan 2977bb7acecfSBarry Smith In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation. 2978bb7acecfSBarry Smith 2979bb7acecfSBarry Smith `INSERT_VALUES` is not supported for `DMDA`; in that case simply compute the values directly into a global vector instead of a local one. 2980bb7acecfSBarry Smith 2981bb7acecfSBarry Smith Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process 298201729b5cSPatrick Sanan 29831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()` 298401729b5cSPatrick Sanan @*/ 2985d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g) 2986d71ae5a4SJacob Faibussowitsch { 298701729b5cSPatrick Sanan PetscFunctionBegin; 29889566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalBegin(dm, l, mode, g)); 29899566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalEnd(dm, l, mode, g)); 29903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 299101729b5cSPatrick Sanan } 29924c274da1SToby Isaac 299347c6ae99SBarry Smith /*@ 299401729b5cSPatrick Sanan DMLocalToGlobalBegin - begins updating global vectors from local vectors 29959a42bb27SBarry Smith 299620f4b53cSBarry Smith Neighbor-wise Collective 29979a42bb27SBarry Smith 29989a42bb27SBarry Smith Input Parameters: 2999bb7acecfSBarry Smith + dm - the `DM` object 3000f6813fd5SJed Brown . l - the local vector 3001aa624791SPierre Jolivet . mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point. 30021eb28f2eSBarry Smith - g - the global vector 30039a42bb27SBarry Smith 300420f4b53cSBarry Smith Level: intermediate 300520f4b53cSBarry Smith 300695452b02SPatrick Sanan Notes: 3007bb7acecfSBarry Smith In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation. 3008bb7acecfSBarry Smith 3009bb7acecfSBarry Smith `INSERT_VALUES is` not supported for `DMDA`, in that case simply compute the values directly into a global vector instead of a local one. 3010bb7acecfSBarry Smith 3011bb7acecfSBarry Smith Use `DMLocalToGlobalEnd()` to complete the communication process. 3012bb7acecfSBarry Smith 3013bb7acecfSBarry Smith `DMLocalToGlobal()` is a short form of `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()` 3014bb7acecfSBarry Smith 3015bb7acecfSBarry Smith `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process. 30169a42bb27SBarry Smith 30171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()` 30189a42bb27SBarry Smith @*/ 3019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g) 3020d71ae5a4SJacob Faibussowitsch { 30217128ae9fSMatthew G Knepley PetscSF sf; 302284330215SMatthew G. Knepley PetscSection s, gs; 3023d4d07f1eSToby Isaac DMLocalToGlobalHookLink link; 3024ca3d3a14SMatthew G. Knepley Vec tmpl; 3025ae5cfb4aSMatthew G. Knepley const PetscScalar *lArray; 3026ae5cfb4aSMatthew G. Knepley PetscScalar *gArray; 3027fa88e482SJed Brown PetscBool isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE; 3028d0295fc0SJunchao Zhang PetscMemType lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST; 30299a42bb27SBarry Smith 30309a42bb27SBarry Smith PetscFunctionBegin; 3031171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3032d4d07f1eSToby Isaac for (link = dm->ltoghook; link; link = link->next) { 30331baa6e33SBarry Smith if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx)); 3034d4d07f1eSToby Isaac } 30359566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL)); 30369566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &sf)); 30379566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 30387128ae9fSMatthew G Knepley switch (mode) { 30397128ae9fSMatthew G Knepley case INSERT_VALUES: 30407128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 3041d71ae5a4SJacob Faibussowitsch case INSERT_BC_VALUES: 3042d71ae5a4SJacob Faibussowitsch isInsert = PETSC_TRUE; 3043d71ae5a4SJacob Faibussowitsch break; 30447128ae9fSMatthew G Knepley case ADD_VALUES: 30457128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 3046d71ae5a4SJacob Faibussowitsch case ADD_BC_VALUES: 3047d71ae5a4SJacob Faibussowitsch isInsert = PETSC_FALSE; 3048d71ae5a4SJacob Faibussowitsch break; 3049d71ae5a4SJacob Faibussowitsch default: 3050d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode); 30517128ae9fSMatthew G Knepley } 3052ca3d3a14SMatthew G. Knepley if ((sf && !isInsert) || (s && isInsert)) { 30539566063dSJacob Faibussowitsch PetscCall(DMHasBasisTransform(dm, &transform)); 3054ca3d3a14SMatthew G. Knepley if (transform) { 30559566063dSJacob Faibussowitsch PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl)); 30569566063dSJacob Faibussowitsch PetscCall(VecCopy(l, tmpl)); 30579566063dSJacob Faibussowitsch PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl)); 30589566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(tmpl, &lArray)); 3059fa88e482SJed Brown } else if (isInsert) { 30609566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(l, &lArray)); 3061fa88e482SJed Brown } else { 30629566063dSJacob Faibussowitsch PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype)); 3063fa88e482SJed Brown l_inplace = PETSC_TRUE; 3064ca3d3a14SMatthew G. Knepley } 3065fa88e482SJed Brown if (s && isInsert) { 30669566063dSJacob Faibussowitsch PetscCall(VecGetArray(g, &gArray)); 3067fa88e482SJed Brown } else { 30689566063dSJacob Faibussowitsch PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype)); 3069fa88e482SJed Brown g_inplace = PETSC_TRUE; 3070fa88e482SJed Brown } 3071ca3d3a14SMatthew G. Knepley if (sf && !isInsert) { 30729566063dSJacob Faibussowitsch PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM)); 307384330215SMatthew G. Knepley } else if (s && isInsert) { 307484330215SMatthew G. Knepley PetscInt gStart, pStart, pEnd, p; 307584330215SMatthew G. Knepley 30769566063dSJacob Faibussowitsch PetscCall(DMGetGlobalSection(dm, &gs)); 30779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(s, &pStart, &pEnd)); 30789566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(g, &gStart, NULL)); 307984330215SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 3080b3b16f48SMatthew G. Knepley PetscInt dof, gdof, cdof, gcdof, off, goff, d, e; 308184330215SMatthew G. Knepley 30829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(s, p, &dof)); 30839566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(gs, p, &gdof)); 30849566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(s, p, &cdof)); 30859566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof)); 30869566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(s, p, &off)); 30879566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(gs, p, &goff)); 3088b3b16f48SMatthew G. Knepley /* Ignore off-process data and points with no global data */ 308903442857SMatthew G. Knepley if (!gdof || goff < 0) continue; 30907a8be351SBarry Smith PetscCheck(dof == gdof, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %" PetscInt_FMT " dof: %" PetscInt_FMT " gdof: %" PetscInt_FMT " cdof: %" PetscInt_FMT " gcdof: %" PetscInt_FMT, p, dof, gdof, cdof, gcdof); 3091b3b16f48SMatthew G. Knepley /* If no constraints are enforced in the global vector */ 3092b3b16f48SMatthew G. Knepley if (!gcdof) { 309384330215SMatthew G. Knepley for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d]; 3094b3b16f48SMatthew G. Knepley /* If constraints are enforced in the global vector */ 3095b3b16f48SMatthew G. Knepley } else if (cdof == gcdof) { 309684330215SMatthew G. Knepley const PetscInt *cdofs; 309784330215SMatthew G. Knepley PetscInt cind = 0; 309884330215SMatthew G. Knepley 30999566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs)); 3100b3b16f48SMatthew G. Knepley for (d = 0, e = 0; d < dof; ++d) { 31019371c9d4SSatish Balay if ((cind < cdof) && (d == cdofs[cind])) { 31029371c9d4SSatish Balay ++cind; 31039371c9d4SSatish Balay continue; 31049371c9d4SSatish Balay } 3105b3b16f48SMatthew G. Knepley gArray[goff - gStart + e++] = lArray[off + d]; 310684330215SMatthew G. Knepley } 31077a8be351SBarry Smith } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %" PetscInt_FMT " dof: %" PetscInt_FMT " gdof: %" PetscInt_FMT " cdof: %" PetscInt_FMT " gcdof: %" PetscInt_FMT, p, dof, gdof, cdof, gcdof); 310884330215SMatthew G. Knepley } 3109ca3d3a14SMatthew G. Knepley } 3110fa88e482SJed Brown if (g_inplace) { 31119566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayAndMemType(g, &gArray)); 3112fa88e482SJed Brown } else { 31139566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(g, &gArray)); 3114fa88e482SJed Brown } 3115ca3d3a14SMatthew G. Knepley if (transform) { 31169566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(tmpl, &lArray)); 31179566063dSJacob Faibussowitsch PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl)); 3118fa88e482SJed Brown } else if (l_inplace) { 31199566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayReadAndMemType(l, &lArray)); 3120ca3d3a14SMatthew G. Knepley } else { 31219566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(l, &lArray)); 3122ca3d3a14SMatthew G. Knepley } 31237128ae9fSMatthew G Knepley } else { 31249566063dSJacob Faibussowitsch PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g)); 31257128ae9fSMatthew G Knepley } 31263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31279a42bb27SBarry Smith } 31289a42bb27SBarry Smith 31299a42bb27SBarry Smith /*@ 31309a42bb27SBarry Smith DMLocalToGlobalEnd - updates global vectors from local vectors 313147c6ae99SBarry Smith 313220f4b53cSBarry Smith Neighbor-wise Collective 313347c6ae99SBarry Smith 313447c6ae99SBarry Smith Input Parameters: 3135bb7acecfSBarry Smith + dm - the `DM` object 3136f6813fd5SJed Brown . l - the local vector 3137bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES` 3138f6813fd5SJed Brown - g - the global vector 313947c6ae99SBarry Smith 314001729b5cSPatrick Sanan Level: intermediate 314147c6ae99SBarry Smith 3142bb7acecfSBarry Smith Note: 3143bb7acecfSBarry Smith See `DMLocalToGlobalBegin()` for full details 3144bb7acecfSBarry Smith 314560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()` 314647c6ae99SBarry Smith @*/ 3147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g) 3148d71ae5a4SJacob Faibussowitsch { 31497128ae9fSMatthew G Knepley PetscSF sf; 315084330215SMatthew G. Knepley PetscSection s; 3151d4d07f1eSToby Isaac DMLocalToGlobalHookLink link; 3152ca3d3a14SMatthew G. Knepley PetscBool isInsert, transform; 315347c6ae99SBarry Smith 315447c6ae99SBarry Smith PetscFunctionBegin; 3155171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 31569566063dSJacob Faibussowitsch PetscCall(DMGetSectionSF(dm, &sf)); 31579566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 31587128ae9fSMatthew G Knepley switch (mode) { 31597128ae9fSMatthew G Knepley case INSERT_VALUES: 3160d71ae5a4SJacob Faibussowitsch case INSERT_ALL_VALUES: 3161d71ae5a4SJacob Faibussowitsch isInsert = PETSC_TRUE; 3162d71ae5a4SJacob Faibussowitsch break; 31637128ae9fSMatthew G Knepley case ADD_VALUES: 3164d71ae5a4SJacob Faibussowitsch case ADD_ALL_VALUES: 3165d71ae5a4SJacob Faibussowitsch isInsert = PETSC_FALSE; 3166d71ae5a4SJacob Faibussowitsch break; 3167d71ae5a4SJacob Faibussowitsch default: 3168d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode); 31697128ae9fSMatthew G Knepley } 317084330215SMatthew G. Knepley if (sf && !isInsert) { 3171ae5cfb4aSMatthew G. Knepley const PetscScalar *lArray; 3172ae5cfb4aSMatthew G. Knepley PetscScalar *gArray; 3173ca3d3a14SMatthew G. Knepley Vec tmpl; 317484330215SMatthew G. Knepley 31759566063dSJacob Faibussowitsch PetscCall(DMHasBasisTransform(dm, &transform)); 3176ca3d3a14SMatthew G. Knepley if (transform) { 31779566063dSJacob Faibussowitsch PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl)); 31789566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(tmpl, &lArray)); 3179ca3d3a14SMatthew G. Knepley } else { 31809566063dSJacob Faibussowitsch PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL)); 3181ca3d3a14SMatthew G. Knepley } 31829566063dSJacob Faibussowitsch PetscCall(VecGetArrayAndMemType(g, &gArray, NULL)); 31839566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM)); 3184ca3d3a14SMatthew G. Knepley if (transform) { 31859566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(tmpl, &lArray)); 31869566063dSJacob Faibussowitsch PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl)); 3187ca3d3a14SMatthew G. Knepley } else { 31889566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayReadAndMemType(l, &lArray)); 3189ca3d3a14SMatthew G. Knepley } 31909566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayAndMemType(g, &gArray)); 319184330215SMatthew G. Knepley } else if (s && isInsert) { 31927128ae9fSMatthew G Knepley } else { 31939566063dSJacob Faibussowitsch PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g)); 31947128ae9fSMatthew G Knepley } 3195d4d07f1eSToby Isaac for (link = dm->ltoghook; link; link = link->next) { 31969566063dSJacob Faibussowitsch if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx)); 3197d4d07f1eSToby Isaac } 31983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 319947c6ae99SBarry Smith } 320047c6ae99SBarry Smith 3201f089877aSRichard Tran Mills /*@ 3202a4e35b19SJacob Faibussowitsch DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include 3203a4e35b19SJacob Faibussowitsch ghost points that contain irrelevant values) to another local vector where the ghost points 3204a4e35b19SJacob Faibussowitsch in the second are set correctly from values on other MPI ranks. 3205f089877aSRichard Tran Mills 320620f4b53cSBarry Smith Neighbor-wise Collective 3207f089877aSRichard Tran Mills 3208f089877aSRichard Tran Mills Input Parameters: 3209bb7acecfSBarry Smith + dm - the `DM` object 3210bc0a1609SRichard Tran Mills . g - the original local vector 3211bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES` 3212f089877aSRichard Tran Mills 3213bc0a1609SRichard Tran Mills Output Parameter: 3214bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values 3215f089877aSRichard Tran Mills 3216f089877aSRichard Tran Mills Level: intermediate 3217f089877aSRichard Tran Mills 3218a4e35b19SJacob Faibussowitsch Notes: 3219a4e35b19SJacob Faibussowitsch Must be followed by `DMLocalToLocalEnd()`. 3220a4e35b19SJacob Faibussowitsch 322160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()` 3222f089877aSRichard Tran Mills @*/ 3223d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l) 3224d71ae5a4SJacob Faibussowitsch { 3225f089877aSRichard Tran Mills PetscFunctionBegin; 3226f089877aSRichard Tran Mills PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 32279f4ada15SMatthew G. Knepley PetscValidHeaderSpecific(g, VEC_CLASSID, 2); 32289f4ada15SMatthew G. Knepley PetscValidHeaderSpecific(l, VEC_CLASSID, 4); 32299f4ada15SMatthew G. Knepley PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l); 32303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3231f089877aSRichard Tran Mills } 3232f089877aSRichard Tran Mills 3233f089877aSRichard Tran Mills /*@ 3234bb7acecfSBarry Smith DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost 3235bb7acecfSBarry Smith points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`. 3236f089877aSRichard Tran Mills 323720f4b53cSBarry Smith Neighbor-wise Collective 3238f089877aSRichard Tran Mills 3239f089877aSRichard Tran Mills Input Parameters: 324060225df5SJacob Faibussowitsch + dm - the `DM` object 3241bc0a1609SRichard Tran Mills . g - the original local vector 3242bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES` 3243f089877aSRichard Tran Mills 3244bc0a1609SRichard Tran Mills Output Parameter: 3245bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values 3246f089877aSRichard Tran Mills 3247f089877aSRichard Tran Mills Level: intermediate 3248f089877aSRichard Tran Mills 324960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()` 3250f089877aSRichard Tran Mills @*/ 3251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l) 3252d71ae5a4SJacob Faibussowitsch { 3253f089877aSRichard Tran Mills PetscFunctionBegin; 3254f089877aSRichard Tran Mills PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 32559f4ada15SMatthew G. Knepley PetscValidHeaderSpecific(g, VEC_CLASSID, 2); 32569f4ada15SMatthew G. Knepley PetscValidHeaderSpecific(l, VEC_CLASSID, 4); 32579f4ada15SMatthew G. Knepley PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l); 32583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3259f089877aSRichard Tran Mills } 3260f089877aSRichard Tran Mills 326147c6ae99SBarry Smith /*@ 3262bb7acecfSBarry Smith DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh 326347c6ae99SBarry Smith 326420f4b53cSBarry Smith Collective 326547c6ae99SBarry Smith 3266d8d19677SJose E. Roman Input Parameters: 3267bb7acecfSBarry Smith + dm - the `DM` object 326820f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`) 326947c6ae99SBarry Smith 327047c6ae99SBarry Smith Output Parameter: 3271bb7acecfSBarry Smith . dmc - the coarsened `DM` 327247c6ae99SBarry Smith 327347c6ae99SBarry Smith Level: developer 327447c6ae99SBarry Smith 32751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 327647c6ae99SBarry Smith @*/ 3277d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) 3278d71ae5a4SJacob Faibussowitsch { 3279b17ce1afSJed Brown DMCoarsenHookLink link; 328047c6ae99SBarry Smith 328147c6ae99SBarry Smith PetscFunctionBegin; 3282171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 32839566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0)); 3284dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, coarsen, comm, dmc); 3285b9d85ea2SLisandro Dalcin if (*dmc) { 3286a3574896SRichard Tran Mills (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */ 32879566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dm, *dmc)); 328843842a1eSJed Brown (*dmc)->ops->creatematrix = dm->ops->creatematrix; 32899566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc)); 3290644e2e5bSBarry Smith (*dmc)->ctx = dm->ctx; 32910598a293SJed Brown (*dmc)->levelup = dm->levelup; 3292656b349aSBarry Smith (*dmc)->leveldown = dm->leveldown + 1; 32939566063dSJacob Faibussowitsch PetscCall(DMSetMatType(*dmc, dm->mattype)); 3294b17ce1afSJed Brown for (link = dm->coarsenhook; link; link = link->next) { 32959566063dSJacob Faibussowitsch if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx)); 3296b17ce1afSJed Brown } 3297b9d85ea2SLisandro Dalcin } 32989566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0)); 32997a8be351SBarry Smith PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced"); 33003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3301b17ce1afSJed Brown } 3302b17ce1afSJed Brown 3303bb9467b5SJed Brown /*@C 3304b17ce1afSJed Brown DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid 3305b17ce1afSJed Brown 330620f4b53cSBarry Smith Logically Collective; No Fortran Support 3307b17ce1afSJed Brown 33084165533cSJose E. Roman Input Parameters: 3309bb7acecfSBarry Smith + fine - `DM` on which to run a hook when restricting to a coarser level 3310b17ce1afSJed Brown . coarsenhook - function to run when setting up a coarser level 3311bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`) 331220f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 3313b17ce1afSJed Brown 331420f4b53cSBarry Smith Calling sequence of `coarsenhook`: 3315bb7acecfSBarry Smith + fine - fine level `DM` 3316bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to 3317b17ce1afSJed Brown - ctx - optional user-defined function context 3318b17ce1afSJed Brown 331920f4b53cSBarry Smith Calling sequence of `restricthook`: 3320bb7acecfSBarry Smith + fine - fine level `DM` 3321bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation 3322c833c3b5SJed Brown . rscale - scaling vector for restriction 3323c833c3b5SJed Brown . inject - matrix restricting by injection 3324b17ce1afSJed Brown . coarse - coarse level DM to update 3325b17ce1afSJed Brown - ctx - optional user-defined function context 3326b17ce1afSJed Brown 3327b17ce1afSJed Brown Level: advanced 3328b17ce1afSJed Brown 3329b17ce1afSJed Brown Notes: 3330bb7acecfSBarry Smith This function is only needed if auxiliary data, attached to the `DM` with `PetscObjectCompose()`, needs to be set up or passed from the fine `DM` to the coarse `DM`. 3331b17ce1afSJed Brown 3332b17ce1afSJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 3333b17ce1afSJed Brown 3334b17ce1afSJed Brown In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 3335bb7acecfSBarry Smith extract the finest level information from its context (instead of from the `SNES`). 3336b17ce1afSJed Brown 3337bb7acecfSBarry Smith The hooks are automatically called by `DMRestrict()` 3338bb7acecfSBarry Smith 33391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 3340b17ce1afSJed Brown @*/ 3341a4e35b19SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM fine, DM coarse, void *ctx), PetscErrorCode (*restricthook)(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx), void *ctx) 3342d71ae5a4SJacob Faibussowitsch { 3343b17ce1afSJed Brown DMCoarsenHookLink link, *p; 3344b17ce1afSJed Brown 3345b17ce1afSJed Brown PetscFunctionBegin; 3346b17ce1afSJed Brown PetscValidHeaderSpecific(fine, DM_CLASSID, 1); 33471e3d8eccSJed Brown for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */ 33483ba16761SJacob Faibussowitsch if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS); 33491e3d8eccSJed Brown } 33509566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 3351b17ce1afSJed Brown link->coarsenhook = coarsenhook; 3352b17ce1afSJed Brown link->restricthook = restricthook; 3353b17ce1afSJed Brown link->ctx = ctx; 33540298fd71SBarry Smith link->next = NULL; 3355b17ce1afSJed Brown *p = link; 33563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3357b17ce1afSJed Brown } 3358b17ce1afSJed Brown 3359dc822a44SJed Brown /*@C 3360bb7acecfSBarry Smith DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()` 3361dc822a44SJed Brown 336220f4b53cSBarry Smith Logically Collective; No Fortran Support 3363dc822a44SJed Brown 33644165533cSJose E. Roman Input Parameters: 3365bb7acecfSBarry Smith + fine - `DM` on which to run a hook when restricting to a coarser level 3366dc822a44SJed Brown . coarsenhook - function to run when setting up a coarser level 3367bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels 336820f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 3369dc822a44SJed Brown 3370dc822a44SJed Brown Level: advanced 3371dc822a44SJed Brown 3372bb7acecfSBarry Smith Note: 3373dc822a44SJed Brown This function does nothing if the hook is not in the list. 3374dc822a44SJed Brown 33751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 3376dc822a44SJed Brown @*/ 3377d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx) 3378d71ae5a4SJacob Faibussowitsch { 3379dc822a44SJed Brown DMCoarsenHookLink link, *p; 3380dc822a44SJed Brown 3381dc822a44SJed Brown PetscFunctionBegin; 3382dc822a44SJed Brown PetscValidHeaderSpecific(fine, DM_CLASSID, 1); 3383dc822a44SJed Brown for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */ 3384dc822a44SJed Brown if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) { 3385dc822a44SJed Brown link = *p; 3386dc822a44SJed Brown *p = link->next; 33879566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 3388dc822a44SJed Brown break; 3389dc822a44SJed Brown } 3390dc822a44SJed Brown } 33913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3392dc822a44SJed Brown } 3393dc822a44SJed Brown 3394b17ce1afSJed Brown /*@ 3395bb7acecfSBarry Smith DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()` 3396b17ce1afSJed Brown 3397b17ce1afSJed Brown Collective if any hooks are 3398b17ce1afSJed Brown 33994165533cSJose E. Roman Input Parameters: 3400bb7acecfSBarry Smith + fine - finer `DM` from which the data is obtained 3401bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation 3402e91eccc2SStefano Zampini . rscale - scaling vector for restriction 3403bb7acecfSBarry Smith . inject - injection matrix, also use `MatRestrict()` 340420f4b53cSBarry Smith - coarse - coarser `DM` to update 3405b17ce1afSJed Brown 3406b17ce1afSJed Brown Level: developer 3407b17ce1afSJed Brown 340860225df5SJacob Faibussowitsch Developer Notes: 3409bb7acecfSBarry Smith Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better 3410bb7acecfSBarry Smith 34111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()` 3412b17ce1afSJed Brown @*/ 3413d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse) 3414d71ae5a4SJacob Faibussowitsch { 3415b17ce1afSJed Brown DMCoarsenHookLink link; 3416b17ce1afSJed Brown 3417b17ce1afSJed Brown PetscFunctionBegin; 3418b17ce1afSJed Brown for (link = fine->coarsenhook; link; link = link->next) { 34191baa6e33SBarry Smith if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx)); 3420b17ce1afSJed Brown } 34213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 342247c6ae99SBarry Smith } 342347c6ae99SBarry Smith 3424bb9467b5SJed Brown /*@C 3425be081cd6SPeter Brune DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid 34265dbd56e3SPeter Brune 342720f4b53cSBarry Smith Logically Collective; No Fortran Support 34285dbd56e3SPeter Brune 34294165533cSJose E. Roman Input Parameters: 3430bb7acecfSBarry Smith + global - global `DM` 3431bb7acecfSBarry Smith . ddhook - function to run to pass data to the decomposition `DM` upon its creation 34325dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve) 343320f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 34345dbd56e3SPeter Brune 343520f4b53cSBarry Smith Calling sequence of `ddhook`: 3436bb7acecfSBarry Smith + global - global `DM` 3437bb7acecfSBarry Smith . block - block `DM` 3438ec4806b8SPeter Brune - ctx - optional user-defined function context 3439ec4806b8SPeter Brune 344020f4b53cSBarry Smith Calling sequence of `restricthook`: 3441bb7acecfSBarry Smith + global - global `DM` 34425dbd56e3SPeter Brune . out - scatter to the outer (with ghost and overlap points) block vector 34435dbd56e3SPeter Brune . in - scatter to block vector values only owned locally 3444bb7acecfSBarry Smith . block - block `DM` 34455dbd56e3SPeter Brune - ctx - optional user-defined function context 34465dbd56e3SPeter Brune 34475dbd56e3SPeter Brune Level: advanced 34485dbd56e3SPeter Brune 34495dbd56e3SPeter Brune Notes: 3450bb7acecfSBarry Smith This function is only needed if auxiliary data needs to be set up on subdomain `DM`s. 34515dbd56e3SPeter Brune 34525dbd56e3SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 34535dbd56e3SPeter Brune 34545dbd56e3SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 3455bb7acecfSBarry Smith extract the global information from its context (instead of from the `SNES`). 34565dbd56e3SPeter Brune 34571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 34585dbd56e3SPeter Brune @*/ 3459a4e35b19SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM global, DM block, void *ctx), PetscErrorCode (*restricthook)(DM global, VecScatter out, VecScatter in, DM block, void *ctx), void *ctx) 3460d71ae5a4SJacob Faibussowitsch { 3461be081cd6SPeter Brune DMSubDomainHookLink link, *p; 34625dbd56e3SPeter Brune 34635dbd56e3SPeter Brune PetscFunctionBegin; 34645dbd56e3SPeter Brune PetscValidHeaderSpecific(global, DM_CLASSID, 1); 3465b3a6b972SJed Brown for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */ 34663ba16761SJacob Faibussowitsch if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS); 3467b3a6b972SJed Brown } 34689566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 34695dbd56e3SPeter Brune link->restricthook = restricthook; 3470be081cd6SPeter Brune link->ddhook = ddhook; 34715dbd56e3SPeter Brune link->ctx = ctx; 34720298fd71SBarry Smith link->next = NULL; 34735dbd56e3SPeter Brune *p = link; 34743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34755dbd56e3SPeter Brune } 34765dbd56e3SPeter Brune 3477b3a6b972SJed Brown /*@C 3478b3a6b972SJed Brown DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid 3479b3a6b972SJed Brown 348020f4b53cSBarry Smith Logically Collective; No Fortran Support 3481b3a6b972SJed Brown 34824165533cSJose E. Roman Input Parameters: 3483bb7acecfSBarry Smith + global - global `DM` 3484bb7acecfSBarry Smith . ddhook - function to run to pass data to the decomposition `DM` upon its creation 3485b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve) 348620f4b53cSBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`) 3487b3a6b972SJed Brown 3488b3a6b972SJed Brown Level: advanced 3489b3a6b972SJed Brown 34901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()` 3491b3a6b972SJed Brown @*/ 3492d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx) 3493d71ae5a4SJacob Faibussowitsch { 3494b3a6b972SJed Brown DMSubDomainHookLink link, *p; 3495b3a6b972SJed Brown 3496b3a6b972SJed Brown PetscFunctionBegin; 3497b3a6b972SJed Brown PetscValidHeaderSpecific(global, DM_CLASSID, 1); 3498b3a6b972SJed Brown for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */ 3499b3a6b972SJed Brown if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) { 3500b3a6b972SJed Brown link = *p; 3501b3a6b972SJed Brown *p = link->next; 35029566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 3503b3a6b972SJed Brown break; 3504b3a6b972SJed Brown } 3505b3a6b972SJed Brown } 35063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3507b3a6b972SJed Brown } 3508b3a6b972SJed Brown 35095dbd56e3SPeter Brune /*@ 3510bb7acecfSBarry Smith DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()` 35115dbd56e3SPeter Brune 35125dbd56e3SPeter Brune Collective if any hooks are 35135dbd56e3SPeter Brune 35144165533cSJose E. Roman Input Parameters: 3515a4e35b19SJacob Faibussowitsch + global - The global `DM` to use as a base 3516a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap 3517a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts 3518a4e35b19SJacob Faibussowitsch - subdm - The subdomain `DM` to update 35195dbd56e3SPeter Brune 35205dbd56e3SPeter Brune Level: developer 35215dbd56e3SPeter Brune 35221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()` 35235dbd56e3SPeter Brune @*/ 3524d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm) 3525d71ae5a4SJacob Faibussowitsch { 3526be081cd6SPeter Brune DMSubDomainHookLink link; 35275dbd56e3SPeter Brune 35285dbd56e3SPeter Brune PetscFunctionBegin; 3529be081cd6SPeter Brune for (link = global->subdomainhook; link; link = link->next) { 35301baa6e33SBarry Smith if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx)); 35315dbd56e3SPeter Brune } 35323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35335dbd56e3SPeter Brune } 35345dbd56e3SPeter Brune 35355fe1f584SPeter Brune /*@ 3536bb7acecfSBarry Smith DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`. 35375fe1f584SPeter Brune 35385fe1f584SPeter Brune Not Collective 35395fe1f584SPeter Brune 35405fe1f584SPeter Brune Input Parameter: 3541bb7acecfSBarry Smith . dm - the `DM` object 35425fe1f584SPeter Brune 35435fe1f584SPeter Brune Output Parameter: 35446a7d9d85SPeter Brune . level - number of coarsenings 35455fe1f584SPeter Brune 35465fe1f584SPeter Brune Level: developer 35475fe1f584SPeter Brune 35481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 35495fe1f584SPeter Brune @*/ 3550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level) 3551d71ae5a4SJacob Faibussowitsch { 35525fe1f584SPeter Brune PetscFunctionBegin; 35535fe1f584SPeter Brune PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 35544f572ea9SToby Isaac PetscAssertPointer(level, 2); 35555fe1f584SPeter Brune *level = dm->leveldown; 35563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35575fe1f584SPeter Brune } 35585fe1f584SPeter Brune 35599a64c4a8SMatthew G. Knepley /*@ 3560bb7acecfSBarry Smith DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`. 35619a64c4a8SMatthew G. Knepley 356220f4b53cSBarry Smith Collective 35639a64c4a8SMatthew G. Knepley 35649a64c4a8SMatthew G. Knepley Input Parameters: 3565bb7acecfSBarry Smith + dm - the `DM` object 35669a64c4a8SMatthew G. Knepley - level - number of coarsenings 35679a64c4a8SMatthew G. Knepley 35689a64c4a8SMatthew G. Knepley Level: developer 35699a64c4a8SMatthew G. Knepley 3570bb7acecfSBarry Smith Note: 3571bb7acecfSBarry Smith This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()` 3572bb7acecfSBarry Smith 357342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 35749a64c4a8SMatthew G. Knepley @*/ 3575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level) 3576d71ae5a4SJacob Faibussowitsch { 35779a64c4a8SMatthew G. Knepley PetscFunctionBegin; 35789a64c4a8SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 35799a64c4a8SMatthew G. Knepley dm->leveldown = level; 35803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35819a64c4a8SMatthew G. Knepley } 35829a64c4a8SMatthew G. Knepley 358347c6ae99SBarry Smith /*@C 3584bb7acecfSBarry Smith DMRefineHierarchy - Refines a `DM` object, all levels at once 358547c6ae99SBarry Smith 358620f4b53cSBarry Smith Collective 358747c6ae99SBarry Smith 3588d8d19677SJose E. Roman Input Parameters: 3589bb7acecfSBarry Smith + dm - the `DM` object 359047c6ae99SBarry Smith - nlevels - the number of levels of refinement 359147c6ae99SBarry Smith 359247c6ae99SBarry Smith Output Parameter: 3593bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy 359447c6ae99SBarry Smith 359547c6ae99SBarry Smith Level: developer 359647c6ae99SBarry Smith 35971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 359847c6ae99SBarry Smith @*/ 3599d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[]) 3600d71ae5a4SJacob Faibussowitsch { 360147c6ae99SBarry Smith PetscFunctionBegin; 3602171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36037a8be351SBarry Smith PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative"); 36043ba16761SJacob Faibussowitsch if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS); 36054f572ea9SToby Isaac PetscAssertPointer(dmf, 3); 360647c6ae99SBarry Smith if (dm->ops->refinehierarchy) { 3607dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf); 360847c6ae99SBarry Smith } else if (dm->ops->refine) { 360947c6ae99SBarry Smith PetscInt i; 361047c6ae99SBarry Smith 36119566063dSJacob Faibussowitsch PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0])); 361248a46eb9SPierre Jolivet for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i])); 3613ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet"); 36143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 361547c6ae99SBarry Smith } 361647c6ae99SBarry Smith 361747c6ae99SBarry Smith /*@C 3618bb7acecfSBarry Smith DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once 361947c6ae99SBarry Smith 362020f4b53cSBarry Smith Collective 362147c6ae99SBarry Smith 3622d8d19677SJose E. Roman Input Parameters: 3623bb7acecfSBarry Smith + dm - the `DM` object 362447c6ae99SBarry Smith - nlevels - the number of levels of coarsening 362547c6ae99SBarry Smith 362647c6ae99SBarry Smith Output Parameter: 3627bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy 362847c6ae99SBarry Smith 362947c6ae99SBarry Smith Level: developer 363047c6ae99SBarry Smith 36311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()` 363247c6ae99SBarry Smith @*/ 3633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) 3634d71ae5a4SJacob Faibussowitsch { 363547c6ae99SBarry Smith PetscFunctionBegin; 3636171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36377a8be351SBarry Smith PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative"); 36383ba16761SJacob Faibussowitsch if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS); 36394f572ea9SToby Isaac PetscAssertPointer(dmc, 3); 364047c6ae99SBarry Smith if (dm->ops->coarsenhierarchy) { 3641dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc); 364247c6ae99SBarry Smith } else if (dm->ops->coarsen) { 364347c6ae99SBarry Smith PetscInt i; 364447c6ae99SBarry Smith 36459566063dSJacob Faibussowitsch PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0])); 364648a46eb9SPierre Jolivet for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i])); 3647ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet"); 36483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 364947c6ae99SBarry Smith } 365047c6ae99SBarry Smith 36511a266240SBarry Smith /*@C 3652bb7acecfSBarry Smith DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed 36531a266240SBarry Smith 3654bb7acecfSBarry Smith Logically Collective if the function is collective 36551a266240SBarry Smith 36561a266240SBarry Smith Input Parameters: 3657bb7acecfSBarry Smith + dm - the `DM` object 36581a266240SBarry Smith - destroy - the destroy function 36591a266240SBarry Smith 36601a266240SBarry Smith Level: intermediate 36611a266240SBarry Smith 36621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()` 3663f07f9ceaSJed Brown @*/ 3664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **)) 3665d71ae5a4SJacob Faibussowitsch { 36661a266240SBarry Smith PetscFunctionBegin; 3667171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 36681a266240SBarry Smith dm->ctxdestroy = destroy; 36693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36701a266240SBarry Smith } 36711a266240SBarry Smith 3672b07ff414SBarry Smith /*@ 3673bb7acecfSBarry Smith DMSetApplicationContext - Set a user context into a `DM` object 367447c6ae99SBarry Smith 367547c6ae99SBarry Smith Not Collective 367647c6ae99SBarry Smith 367747c6ae99SBarry Smith Input Parameters: 3678bb7acecfSBarry Smith + dm - the `DM` object 367947c6ae99SBarry Smith - ctx - the user context 368047c6ae99SBarry Smith 368147c6ae99SBarry Smith Level: intermediate 368247c6ae99SBarry Smith 3683bb7acecfSBarry Smith Note: 368435cb6cd3SPierre Jolivet A user context is a way to pass problem specific information that is accessible whenever the `DM` is available 3685bb7acecfSBarry Smith 368660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()` 368747c6ae99SBarry Smith @*/ 3688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx) 3689d71ae5a4SJacob Faibussowitsch { 369047c6ae99SBarry Smith PetscFunctionBegin; 3691171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 369247c6ae99SBarry Smith dm->ctx = ctx; 36933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 369447c6ae99SBarry Smith } 369547c6ae99SBarry Smith 369647c6ae99SBarry Smith /*@ 3697bb7acecfSBarry Smith DMGetApplicationContext - Gets a user context from a `DM` object 369847c6ae99SBarry Smith 369947c6ae99SBarry Smith Not Collective 370047c6ae99SBarry Smith 370147c6ae99SBarry Smith Input Parameter: 3702bb7acecfSBarry Smith . dm - the `DM` object 370347c6ae99SBarry Smith 370447c6ae99SBarry Smith Output Parameter: 370547c6ae99SBarry Smith . ctx - the user context 370647c6ae99SBarry Smith 370747c6ae99SBarry Smith Level: intermediate 370847c6ae99SBarry Smith 3709bb7acecfSBarry Smith Note: 371035cb6cd3SPierre Jolivet A user context is a way to pass problem specific information that is accessible whenever the `DM` is available 3711bb7acecfSBarry Smith 371242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()` 371347c6ae99SBarry Smith @*/ 3714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx) 3715d71ae5a4SJacob Faibussowitsch { 371647c6ae99SBarry Smith PetscFunctionBegin; 3717171400e9SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 37181b2093e4SBarry Smith *(void **)ctx = dm->ctx; 37193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 372047c6ae99SBarry Smith } 372147c6ae99SBarry Smith 372208da532bSDmitry Karpeev /*@C 3723bb7acecfSBarry Smith DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`. 372408da532bSDmitry Karpeev 372520f4b53cSBarry Smith Logically Collective 372608da532bSDmitry Karpeev 3727d8d19677SJose E. Roman Input Parameters: 372808da532bSDmitry Karpeev + dm - the DM object 372920f4b53cSBarry Smith - f - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set) 373008da532bSDmitry Karpeev 373108da532bSDmitry Karpeev Level: intermediate 373208da532bSDmitry Karpeev 37331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`, 3734db781477SPatrick Sanan `DMSetJacobian()` 373508da532bSDmitry Karpeev @*/ 3736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec)) 3737d71ae5a4SJacob Faibussowitsch { 373808da532bSDmitry Karpeev PetscFunctionBegin; 37395a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 374008da532bSDmitry Karpeev dm->ops->computevariablebounds = f; 37413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 374208da532bSDmitry Karpeev } 374308da532bSDmitry Karpeev 374408da532bSDmitry Karpeev /*@ 3745bb7acecfSBarry Smith DMHasVariableBounds - does the `DM` object have a variable bounds function? 374608da532bSDmitry Karpeev 374708da532bSDmitry Karpeev Not Collective 374808da532bSDmitry Karpeev 374908da532bSDmitry Karpeev Input Parameter: 3750bb7acecfSBarry Smith . dm - the `DM` object to destroy 375108da532bSDmitry Karpeev 375208da532bSDmitry Karpeev Output Parameter: 3753bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists 375408da532bSDmitry Karpeev 375508da532bSDmitry Karpeev Level: developer 375608da532bSDmitry Karpeev 37571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()` 375808da532bSDmitry Karpeev @*/ 3759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg) 3760d71ae5a4SJacob Faibussowitsch { 376108da532bSDmitry Karpeev PetscFunctionBegin; 37625a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 37634f572ea9SToby Isaac PetscAssertPointer(flg, 2); 376408da532bSDmitry Karpeev *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE; 37653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 376608da532bSDmitry Karpeev } 376708da532bSDmitry Karpeev 376808da532bSDmitry Karpeev /*@C 3769bb7acecfSBarry Smith DMComputeVariableBounds - compute variable bounds used by `SNESVI`. 377008da532bSDmitry Karpeev 377120f4b53cSBarry Smith Logically Collective 377208da532bSDmitry Karpeev 3773f899ff85SJose E. Roman Input Parameter: 3774bb7acecfSBarry Smith . dm - the `DM` object 377508da532bSDmitry Karpeev 377660225df5SJacob Faibussowitsch Output Parameters: 377708da532bSDmitry Karpeev + xl - lower bound 377808da532bSDmitry Karpeev - xu - upper bound 377908da532bSDmitry Karpeev 3780907376e6SBarry Smith Level: advanced 3781907376e6SBarry Smith 378220f4b53cSBarry Smith Note: 378395452b02SPatrick Sanan This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds() 378408da532bSDmitry Karpeev 37851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()` 378608da532bSDmitry Karpeev @*/ 3787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) 3788d71ae5a4SJacob Faibussowitsch { 378908da532bSDmitry Karpeev PetscFunctionBegin; 37905a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 379108da532bSDmitry Karpeev PetscValidHeaderSpecific(xl, VEC_CLASSID, 2); 37925a84ad33SLisandro Dalcin PetscValidHeaderSpecific(xu, VEC_CLASSID, 3); 3793dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, computevariablebounds, xl, xu); 37943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 379508da532bSDmitry Karpeev } 379608da532bSDmitry Karpeev 3797b0ae01b7SPeter Brune /*@ 3798bb7acecfSBarry Smith DMHasColoring - does the `DM` object have a method of providing a coloring? 3799b0ae01b7SPeter Brune 3800b0ae01b7SPeter Brune Not Collective 3801b0ae01b7SPeter Brune 3802b0ae01b7SPeter Brune Input Parameter: 3803b0ae01b7SPeter Brune . dm - the DM object 3804b0ae01b7SPeter Brune 3805b0ae01b7SPeter Brune Output Parameter: 3806bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`. 3807b0ae01b7SPeter Brune 3808b0ae01b7SPeter Brune Level: developer 3809b0ae01b7SPeter Brune 38101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()` 3811b0ae01b7SPeter Brune @*/ 3812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg) 3813d71ae5a4SJacob Faibussowitsch { 3814b0ae01b7SPeter Brune PetscFunctionBegin; 38155a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 38164f572ea9SToby Isaac PetscAssertPointer(flg, 2); 3817b0ae01b7SPeter Brune *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE; 38183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3819b0ae01b7SPeter Brune } 3820b0ae01b7SPeter Brune 38213ad4599aSBarry Smith /*@ 3822bb7acecfSBarry Smith DMHasCreateRestriction - does the `DM` object have a method of providing a restriction? 38233ad4599aSBarry Smith 38243ad4599aSBarry Smith Not Collective 38253ad4599aSBarry Smith 38263ad4599aSBarry Smith Input Parameter: 3827bb7acecfSBarry Smith . dm - the `DM` object 38283ad4599aSBarry Smith 38293ad4599aSBarry Smith Output Parameter: 3830bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`. 38313ad4599aSBarry Smith 38323ad4599aSBarry Smith Level: developer 38333ad4599aSBarry Smith 38341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()` 38353ad4599aSBarry Smith @*/ 3836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg) 3837d71ae5a4SJacob Faibussowitsch { 38383ad4599aSBarry Smith PetscFunctionBegin; 38395a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 38404f572ea9SToby Isaac PetscAssertPointer(flg, 2); 38413ad4599aSBarry Smith *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE; 38423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38433ad4599aSBarry Smith } 38443ad4599aSBarry Smith 3845a7058e45SLawrence Mitchell /*@ 3846bb7acecfSBarry Smith DMHasCreateInjection - does the `DM` object have a method of providing an injection? 3847a7058e45SLawrence Mitchell 3848a7058e45SLawrence Mitchell Not Collective 3849a7058e45SLawrence Mitchell 3850a7058e45SLawrence Mitchell Input Parameter: 3851bb7acecfSBarry Smith . dm - the `DM` object 3852a7058e45SLawrence Mitchell 3853a7058e45SLawrence Mitchell Output Parameter: 3854bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`. 3855a7058e45SLawrence Mitchell 3856a7058e45SLawrence Mitchell Level: developer 3857a7058e45SLawrence Mitchell 38581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()` 3859a7058e45SLawrence Mitchell @*/ 3860d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg) 3861d71ae5a4SJacob Faibussowitsch { 3862a7058e45SLawrence Mitchell PetscFunctionBegin; 38635a84ad33SLisandro Dalcin PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 38644f572ea9SToby Isaac PetscAssertPointer(flg, 2); 3865dbbe0bcdSBarry Smith if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg); 3866ad540459SPierre Jolivet else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE; 38673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3868a7058e45SLawrence Mitchell } 3869a7058e45SLawrence Mitchell 38700298fd71SBarry Smith PetscFunctionList DMList = NULL; 3871264ace61SBarry Smith PetscBool DMRegisterAllCalled = PETSC_FALSE; 3872264ace61SBarry Smith 3873264ace61SBarry Smith /*@C 3874bb7acecfSBarry Smith DMSetType - Builds a `DM`, for a particular `DM` implementation. 3875264ace61SBarry Smith 387620f4b53cSBarry Smith Collective 3877264ace61SBarry Smith 3878264ace61SBarry Smith Input Parameters: 3879bb7acecfSBarry Smith + dm - The `DM` object 3880bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX` 3881264ace61SBarry Smith 3882264ace61SBarry Smith Options Database Key: 3883bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types 3884264ace61SBarry Smith 3885264ace61SBarry Smith Level: intermediate 3886264ace61SBarry Smith 3887bb7acecfSBarry Smith Note: 38880462cc06SPierre Jolivet Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()` 3889bb7acecfSBarry Smith 38901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()` 3891264ace61SBarry Smith @*/ 3892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method) 3893d71ae5a4SJacob Faibussowitsch { 3894264ace61SBarry Smith PetscErrorCode (*r)(DM); 3895264ace61SBarry Smith PetscBool match; 3896264ace61SBarry Smith 3897264ace61SBarry Smith PetscFunctionBegin; 3898264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 38999566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match)); 39003ba16761SJacob Faibussowitsch if (match) PetscFunctionReturn(PETSC_SUCCESS); 3901264ace61SBarry Smith 39029566063dSJacob Faibussowitsch PetscCall(DMRegisterAll()); 39039566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(DMList, method, &r)); 39047a8be351SBarry Smith PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method); 3905264ace61SBarry Smith 3906dbbe0bcdSBarry Smith PetscTryTypeMethod(dm, destroy); 39079566063dSJacob Faibussowitsch PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops))); 39089566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method)); 39099566063dSJacob Faibussowitsch PetscCall((*r)(dm)); 39103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3911264ace61SBarry Smith } 3912264ace61SBarry Smith 3913264ace61SBarry Smith /*@C 3914bb7acecfSBarry Smith DMGetType - Gets the `DM` type name (as a string) from the `DM`. 3915264ace61SBarry Smith 3916264ace61SBarry Smith Not Collective 3917264ace61SBarry Smith 3918264ace61SBarry Smith Input Parameter: 3919bb7acecfSBarry Smith . dm - The `DM` 3920264ace61SBarry Smith 3921264ace61SBarry Smith Output Parameter: 3922bb7acecfSBarry Smith . type - The `DMType` name 3923264ace61SBarry Smith 3924264ace61SBarry Smith Level: intermediate 3925264ace61SBarry Smith 39261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()` 3927264ace61SBarry Smith @*/ 3928d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type) 3929d71ae5a4SJacob Faibussowitsch { 3930264ace61SBarry Smith PetscFunctionBegin; 3931264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 39324f572ea9SToby Isaac PetscAssertPointer(type, 2); 39339566063dSJacob Faibussowitsch PetscCall(DMRegisterAll()); 3934264ace61SBarry Smith *type = ((PetscObject)dm)->type_name; 39353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3936264ace61SBarry Smith } 3937264ace61SBarry Smith 393867a56275SMatthew G Knepley /*@C 3939bb7acecfSBarry Smith DMConvert - Converts a `DM` to another `DM`, either of the same or different type. 394067a56275SMatthew G Knepley 394120f4b53cSBarry Smith Collective 394267a56275SMatthew G Knepley 394367a56275SMatthew G Knepley Input Parameters: 3944bb7acecfSBarry Smith + dm - the `DM` 3945bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type) 394667a56275SMatthew G Knepley 394767a56275SMatthew G Knepley Output Parameter: 3948bb7acecfSBarry Smith . M - pointer to new `DM` 394967a56275SMatthew G Knepley 395020f4b53cSBarry Smith Level: intermediate 395120f4b53cSBarry Smith 395267a56275SMatthew G Knepley Notes: 3953bb7acecfSBarry Smith Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential, 3954bb7acecfSBarry Smith the MPI communicator of the generated `DM` is always the same as the communicator 3955bb7acecfSBarry Smith of the input `DM`. 395667a56275SMatthew G Knepley 39571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()` 395867a56275SMatthew G Knepley @*/ 3959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) 3960d71ae5a4SJacob Faibussowitsch { 396167a56275SMatthew G Knepley DM B; 396267a56275SMatthew G Knepley char convname[256]; 3963c067b6caSMatthew G. Knepley PetscBool sametype /*, issame */; 396467a56275SMatthew G Knepley 396567a56275SMatthew G Knepley PetscFunctionBegin; 396667a56275SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 396767a56275SMatthew G Knepley PetscValidType(dm, 1); 39684f572ea9SToby Isaac PetscAssertPointer(M, 3); 39699566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype)); 39709566063dSJacob Faibussowitsch /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */ 3971c067b6caSMatthew G. Knepley if (sametype) { 3972c067b6caSMatthew G. Knepley *M = dm; 39739566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 39743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3975c067b6caSMatthew G. Knepley } else { 39760298fd71SBarry Smith PetscErrorCode (*conv)(DM, DMType, DM *) = NULL; 397767a56275SMatthew G Knepley 397867a56275SMatthew G Knepley /* 397967a56275SMatthew G Knepley Order of precedence: 398067a56275SMatthew G Knepley 1) See if a specialized converter is known to the current DM. 398167a56275SMatthew G Knepley 2) See if a specialized converter is known to the desired DM class. 398267a56275SMatthew G Knepley 3) See if a good general converter is registered for the desired class 398367a56275SMatthew G Knepley 4) See if a good general converter is known for the current matrix. 398467a56275SMatthew G Knepley 5) Use a really basic converter. 398567a56275SMatthew G Knepley */ 398667a56275SMatthew G Knepley 398767a56275SMatthew G Knepley /* 1) See if a specialized converter is known to the current DM and the desired class */ 39889566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname))); 39899566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname))); 39909566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, "_", sizeof(convname))); 39919566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, newtype, sizeof(convname))); 39929566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, "_C", sizeof(convname))); 39939566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv)); 399467a56275SMatthew G Knepley if (conv) goto foundconv; 399567a56275SMatthew G Knepley 399667a56275SMatthew G Knepley /* 2) See if a specialized converter is known to the desired DM class. */ 39979566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B)); 39989566063dSJacob Faibussowitsch PetscCall(DMSetType(B, newtype)); 39999566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname))); 40009566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname))); 40019566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, "_", sizeof(convname))); 40029566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, newtype, sizeof(convname))); 40039566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(convname, "_C", sizeof(convname))); 40049566063dSJacob Faibussowitsch PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv)); 400567a56275SMatthew G Knepley if (conv) { 40069566063dSJacob Faibussowitsch PetscCall(DMDestroy(&B)); 400767a56275SMatthew G Knepley goto foundconv; 400867a56275SMatthew G Knepley } 400967a56275SMatthew G Knepley 401067a56275SMatthew G Knepley #if 0 401167a56275SMatthew G Knepley /* 3) See if a good general converter is registered for the desired class */ 401267a56275SMatthew G Knepley conv = B->ops->convertfrom; 40139566063dSJacob Faibussowitsch PetscCall(DMDestroy(&B)); 401467a56275SMatthew G Knepley if (conv) goto foundconv; 401567a56275SMatthew G Knepley 401667a56275SMatthew G Knepley /* 4) See if a good general converter is known for the current matrix */ 401767a56275SMatthew G Knepley if (dm->ops->convert) { 401867a56275SMatthew G Knepley conv = dm->ops->convert; 401967a56275SMatthew G Knepley } 402067a56275SMatthew G Knepley if (conv) goto foundconv; 402167a56275SMatthew G Knepley #endif 402267a56275SMatthew G Knepley 402367a56275SMatthew G Knepley /* 5) Use a really basic converter. */ 402498921bdaSJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype); 402567a56275SMatthew G Knepley 402667a56275SMatthew G Knepley foundconv: 40279566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0)); 40289566063dSJacob Faibussowitsch PetscCall((*conv)(dm, newtype, M)); 402912fa691eSMatthew G. Knepley /* Things that are independent of DM type: We should consult DMClone() here */ 403090b157c4SStefano Zampini { 40314fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 40326858538eSMatthew G. Knepley 40334fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L)); 40344fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L)); 4035c8a6034eSMark (*M)->prealloc_only = dm->prealloc_only; 40369566063dSJacob Faibussowitsch PetscCall(PetscFree((*M)->vectype)); 40379566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype)); 40389566063dSJacob Faibussowitsch PetscCall(PetscFree((*M)->mattype)); 40399566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype)); 404012fa691eSMatthew G. Knepley } 40419566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0)); 404267a56275SMatthew G Knepley } 40439566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)*M)); 40443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 404567a56275SMatthew G Knepley } 4046264ace61SBarry Smith 4047264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 4048264ace61SBarry Smith 4049264ace61SBarry Smith /*@C 4050bb7acecfSBarry Smith DMRegister - Adds a new `DM` type implementation 40511c84c290SBarry Smith 40521c84c290SBarry Smith Not Collective 40531c84c290SBarry Smith 40541c84c290SBarry Smith Input Parameters: 405520f4b53cSBarry Smith + sname - The name of a new user-defined creation routine 405620f4b53cSBarry Smith - function - The creation routine itself 405720f4b53cSBarry Smith 405820f4b53cSBarry Smith Level: advanced 40591c84c290SBarry Smith 40601c84c290SBarry Smith Notes: 406120f4b53cSBarry Smith `DMRegister()` may be called multiple times to add several user-defined `DM`s 40621c84c290SBarry Smith 406360225df5SJacob Faibussowitsch Example Usage: 40641c84c290SBarry Smith .vb 4065bdf89e91SBarry Smith DMRegister("my_da", MyDMCreate); 40661c84c290SBarry Smith .ve 40671c84c290SBarry Smith 406820f4b53cSBarry Smith Then, your `DM` type can be chosen with the procedural interface via 40691c84c290SBarry Smith .vb 40701c84c290SBarry Smith DMCreate(MPI_Comm, DM *); 40711c84c290SBarry Smith DMSetType(DM,"my_da"); 40721c84c290SBarry Smith .ve 40731c84c290SBarry Smith or at runtime via the option 40741c84c290SBarry Smith .vb 40751c84c290SBarry Smith -da_type my_da 40761c84c290SBarry Smith .ve 4077264ace61SBarry Smith 40781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()` 4079264ace61SBarry Smith @*/ 4080d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM)) 4081d71ae5a4SJacob Faibussowitsch { 4082264ace61SBarry Smith PetscFunctionBegin; 40839566063dSJacob Faibussowitsch PetscCall(DMInitializePackage()); 40849566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&DMList, sname, function)); 40853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4086264ace61SBarry Smith } 4087264ace61SBarry Smith 4088b859378eSBarry Smith /*@C 4089bb7acecfSBarry Smith DMLoad - Loads a DM that has been stored in binary with `DMView()`. 4090b859378eSBarry Smith 409120f4b53cSBarry Smith Collective 4092b859378eSBarry Smith 4093b859378eSBarry Smith Input Parameters: 4094bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or 4095bb7acecfSBarry Smith some related function before a call to `DMLoad()`. 4096bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or 4097bb7acecfSBarry Smith `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()` 4098b859378eSBarry Smith 4099b859378eSBarry Smith Level: intermediate 4100b859378eSBarry Smith 4101b859378eSBarry Smith Notes: 410255849f57SBarry Smith The type is determined by the data in the file, any type set into the DM before this call is ignored. 4103b859378eSBarry Smith 4104bb7acecfSBarry Smith Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX` 4105bb7acecfSBarry Smith meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()` 4106bb7acecfSBarry Smith before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object. 4107cd7e8a5eSksagiyam 41081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()` 4109b859378eSBarry Smith @*/ 4110d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) 4111d71ae5a4SJacob Faibussowitsch { 41129331c7a4SMatthew G. Knepley PetscBool isbinary, ishdf5; 4113b859378eSBarry Smith 4114b859378eSBarry Smith PetscFunctionBegin; 4115b859378eSBarry Smith PetscValidHeaderSpecific(newdm, DM_CLASSID, 1); 4116b859378eSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 41179566063dSJacob Faibussowitsch PetscCall(PetscViewerCheckReadable(viewer)); 41189566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 41199566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5)); 41209566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0)); 41219331c7a4SMatthew G. Knepley if (isbinary) { 41229331c7a4SMatthew G. Knepley PetscInt classid; 41239331c7a4SMatthew G. Knepley char type[256]; 4124b859378eSBarry Smith 41259566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 41267a8be351SBarry Smith PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid); 41279566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 41289566063dSJacob Faibussowitsch PetscCall(DMSetType(newdm, type)); 4129dbbe0bcdSBarry Smith PetscTryTypeMethod(newdm, load, viewer); 41309331c7a4SMatthew G. Knepley } else if (ishdf5) { 4131dbbe0bcdSBarry Smith PetscTryTypeMethod(newdm, load, viewer); 41329331c7a4SMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()"); 41339566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0)); 41343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4135b859378eSBarry Smith } 4136b859378eSBarry Smith 41377da65231SMatthew G Knepley /******************************** FEM Support **********************************/ 41387da65231SMatthew G Knepley 41392ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[]) 41402ecf6ec3SMatthew G. Knepley { 41412ecf6ec3SMatthew G. Knepley PetscInt f; 41422ecf6ec3SMatthew G. Knepley 41432ecf6ec3SMatthew G. Knepley PetscFunctionBegin; 41442ecf6ec3SMatthew G. Knepley PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name)); 41452ecf6ec3SMatthew G. Knepley for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, " | %" PetscInt_FMT " |\n", x[f])); 41462ecf6ec3SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 41472ecf6ec3SMatthew G. Knepley } 41482ecf6ec3SMatthew G. Knepley 4149d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) 4150d71ae5a4SJacob Faibussowitsch { 41511d47ebbbSSatish Balay PetscInt f; 41521b30c384SMatthew G Knepley 41537da65231SMatthew G Knepley PetscFunctionBegin; 415463a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name)); 415548a46eb9SPierre Jolivet for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, " | %g |\n", (double)PetscRealPart(x[f]))); 41563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41577da65231SMatthew G Knepley } 41587da65231SMatthew G Knepley 4159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) 4160d71ae5a4SJacob Faibussowitsch { 41611b30c384SMatthew G Knepley PetscInt f, g; 41627da65231SMatthew G Knepley 41637da65231SMatthew G Knepley PetscFunctionBegin; 416463a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name)); 41651d47ebbbSSatish Balay for (f = 0; f < rows; ++f) { 41669566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, " |")); 416748a46eb9SPierre Jolivet for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g]))); 41689566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n")); 41697da65231SMatthew G Knepley } 41703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41717da65231SMatthew G Knepley } 4172e7c4fc90SDmitry Karpeev 4173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X) 4174d71ae5a4SJacob Faibussowitsch { 41750c5b8624SToby Isaac PetscInt localSize, bs; 41760c5b8624SToby Isaac PetscMPIInt size; 41770c5b8624SToby Isaac Vec x, xglob; 41780c5b8624SToby Isaac const PetscScalar *xarray; 4179e759306cSMatthew G. Knepley 4180e759306cSMatthew G. Knepley PetscFunctionBegin; 41819566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size)); 41829566063dSJacob Faibussowitsch PetscCall(VecDuplicate(X, &x)); 41839566063dSJacob Faibussowitsch PetscCall(VecCopy(X, x)); 418493ec0da9SPierre Jolivet PetscCall(VecFilter(x, tol)); 41859566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name)); 41860c5b8624SToby Isaac if (size > 1) { 41879566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(x, &localSize)); 41889566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(x, &xarray)); 41899566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(x, &bs)); 41909566063dSJacob Faibussowitsch PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob)); 41910c5b8624SToby Isaac } else { 41920c5b8624SToby Isaac xglob = x; 41930c5b8624SToby Isaac } 41949566063dSJacob Faibussowitsch PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm)))); 41950c5b8624SToby Isaac if (size > 1) { 41969566063dSJacob Faibussowitsch PetscCall(VecDestroy(&xglob)); 41979566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(x, &xarray)); 41980c5b8624SToby Isaac } 41999566063dSJacob Faibussowitsch PetscCall(VecDestroy(&x)); 42003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4201e759306cSMatthew G. Knepley } 4202e759306cSMatthew G. Knepley 420388ed4aceSMatthew G Knepley /*@ 4204bb7acecfSBarry Smith DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`. This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12 4205061576a5SJed Brown 4206061576a5SJed Brown Input Parameter: 4207bb7acecfSBarry Smith . dm - The `DM` 4208061576a5SJed Brown 4209061576a5SJed Brown Output Parameter: 4210bb7acecfSBarry Smith . section - The `PetscSection` 4211061576a5SJed Brown 421220f4b53cSBarry Smith Options Database Key: 4213bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM` 4214061576a5SJed Brown 4215061576a5SJed Brown Level: advanced 4216061576a5SJed Brown 4217061576a5SJed Brown Notes: 4218bb7acecfSBarry Smith Use `DMGetLocalSection()` in new code. 4219061576a5SJed Brown 4220bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSection`. 4221061576a5SJed Brown 42221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()` 4223061576a5SJed Brown @*/ 4224d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section) 4225d71ae5a4SJacob Faibussowitsch { 4226061576a5SJed Brown PetscFunctionBegin; 42279566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, section)); 42283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4229061576a5SJed Brown } 4230061576a5SJed Brown 4231061576a5SJed Brown /*@ 4232bb7acecfSBarry Smith DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`. 423388ed4aceSMatthew G Knepley 423488ed4aceSMatthew G Knepley Input Parameter: 4235bb7acecfSBarry Smith . dm - The `DM` 423688ed4aceSMatthew G Knepley 423788ed4aceSMatthew G Knepley Output Parameter: 4238bb7acecfSBarry Smith . section - The `PetscSection` 423988ed4aceSMatthew G Knepley 424020f4b53cSBarry Smith Options Database Key: 4241bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM` 4242e5893cccSMatthew G. Knepley 424388ed4aceSMatthew G Knepley Level: intermediate 424488ed4aceSMatthew G Knepley 4245bb7acecfSBarry Smith Note: 4246bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSection`. 424788ed4aceSMatthew G Knepley 42481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()` 424988ed4aceSMatthew G Knepley @*/ 4250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section) 4251d71ae5a4SJacob Faibussowitsch { 425288ed4aceSMatthew G Knepley PetscFunctionBegin; 425388ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 42544f572ea9SToby Isaac PetscAssertPointer(section, 2); 42551bb6d2a8SBarry Smith if (!dm->localSection && dm->ops->createlocalsection) { 4256e5e52638SMatthew G. Knepley PetscInt d; 4257e5e52638SMatthew G. Knepley 425845480ffeSMatthew G. Knepley if (dm->setfromoptionscalled) { 425945480ffeSMatthew G. Knepley PetscObject obj = (PetscObject)dm; 426045480ffeSMatthew G. Knepley PetscViewer viewer; 426145480ffeSMatthew G. Knepley PetscViewerFormat format; 426245480ffeSMatthew G. Knepley PetscBool flg; 426345480ffeSMatthew G. Knepley 42649566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg)); 42659566063dSJacob Faibussowitsch if (flg) PetscCall(PetscViewerPushFormat(viewer, format)); 426645480ffeSMatthew G. Knepley for (d = 0; d < dm->Nds; ++d) { 42679566063dSJacob Faibussowitsch PetscCall(PetscDSSetFromOptions(dm->probs[d].ds)); 42689566063dSJacob Faibussowitsch if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer)); 426945480ffeSMatthew G. Knepley } 427045480ffeSMatthew G. Knepley if (flg) { 42719566063dSJacob Faibussowitsch PetscCall(PetscViewerFlush(viewer)); 42729566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 42739566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 427445480ffeSMatthew G. Knepley } 427545480ffeSMatthew G. Knepley } 4276dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, createlocalsection); 42779566063dSJacob Faibussowitsch if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view")); 42782f0f8703SMatthew G. Knepley } 42791bb6d2a8SBarry Smith *section = dm->localSection; 42803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 428188ed4aceSMatthew G Knepley } 428288ed4aceSMatthew G Knepley 428388ed4aceSMatthew G Knepley /*@ 4284bb7acecfSBarry Smith DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`. This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12 4285061576a5SJed Brown 4286061576a5SJed Brown Input Parameters: 4287bb7acecfSBarry Smith + dm - The `DM` 4288bb7acecfSBarry Smith - section - The `PetscSection` 4289061576a5SJed Brown 4290061576a5SJed Brown Level: advanced 4291061576a5SJed Brown 4292061576a5SJed Brown Notes: 4293bb7acecfSBarry Smith Use `DMSetLocalSection()` in new code. 4294061576a5SJed Brown 4295bb7acecfSBarry Smith Any existing `PetscSection` will be destroyed 4296061576a5SJed Brown 42971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()` 4298061576a5SJed Brown @*/ 4299d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section) 4300d71ae5a4SJacob Faibussowitsch { 4301061576a5SJed Brown PetscFunctionBegin; 43029566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dm, section)); 43033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4304061576a5SJed Brown } 4305061576a5SJed Brown 4306061576a5SJed Brown /*@ 4307bb7acecfSBarry Smith DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`. 430888ed4aceSMatthew G Knepley 430988ed4aceSMatthew G Knepley Input Parameters: 4310bb7acecfSBarry Smith + dm - The `DM` 4311bb7acecfSBarry Smith - section - The `PetscSection` 431288ed4aceSMatthew G Knepley 431388ed4aceSMatthew G Knepley Level: intermediate 431488ed4aceSMatthew G Knepley 4315bb7acecfSBarry Smith Note: 4316bb7acecfSBarry Smith Any existing Section will be destroyed 431788ed4aceSMatthew G Knepley 43181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()` 431988ed4aceSMatthew G Knepley @*/ 4320d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section) 4321d71ae5a4SJacob Faibussowitsch { 4322c473ab19SMatthew G. Knepley PetscInt numFields = 0; 4323af122d2aSMatthew G Knepley PetscInt f; 432488ed4aceSMatthew G Knepley 432588ed4aceSMatthew G Knepley PetscFunctionBegin; 432688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4327b9d85ea2SLisandro Dalcin if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2); 43289566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)section)); 43299566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&dm->localSection)); 43301bb6d2a8SBarry Smith dm->localSection = section; 43319566063dSJacob Faibussowitsch if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields)); 4332af122d2aSMatthew G Knepley if (numFields) { 43339566063dSJacob Faibussowitsch PetscCall(DMSetNumFields(dm, numFields)); 4334af122d2aSMatthew G Knepley for (f = 0; f < numFields; ++f) { 43350f21e855SMatthew G. Knepley PetscObject disc; 4336af122d2aSMatthew G Knepley const char *name; 4337af122d2aSMatthew G Knepley 43389566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name)); 43399566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, NULL, &disc)); 43409566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName(disc, name)); 4341af122d2aSMatthew G Knepley } 4342af122d2aSMatthew G Knepley } 4343e87a4003SBarry Smith /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */ 43449566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&dm->globalSection)); 43453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 434688ed4aceSMatthew G Knepley } 434788ed4aceSMatthew G Knepley 43489435951eSToby Isaac /*@ 4349bb7acecfSBarry Smith DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation. 43509435951eSToby Isaac 435120f4b53cSBarry Smith not Collective 4352e228b242SToby Isaac 43539435951eSToby Isaac Input Parameter: 4354bb7acecfSBarry Smith . dm - The `DM` 43559435951eSToby Isaac 4356d8d19677SJose E. Roman Output Parameters: 435720f4b53cSBarry Smith + section - The `PetscSection` describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section. Returns `NULL` if there are no local constraints. 435820f4b53cSBarry Smith . mat - The `Mat` that interpolates local constraints: its width should be the layout size of the default section. Returns `NULL` if there are no local constraints. 435979769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs 43609435951eSToby Isaac 43619435951eSToby Isaac Level: advanced 43629435951eSToby Isaac 4363bb7acecfSBarry Smith Note: 4364bb7acecfSBarry Smith This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`. 43659435951eSToby Isaac 43661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()` 43679435951eSToby Isaac @*/ 4368d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias) 4369d71ae5a4SJacob Faibussowitsch { 43709435951eSToby Isaac PetscFunctionBegin; 43719435951eSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4372dbbe0bcdSBarry Smith if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints); 43733b8ba7d1SJed Brown if (section) *section = dm->defaultConstraint.section; 43743b8ba7d1SJed Brown if (mat) *mat = dm->defaultConstraint.mat; 437579769bd5SJed Brown if (bias) *bias = dm->defaultConstraint.bias; 43763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43779435951eSToby Isaac } 43789435951eSToby Isaac 43799435951eSToby Isaac /*@ 4380bb7acecfSBarry Smith DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation. 43819435951eSToby Isaac 438220f4b53cSBarry Smith Collective 4383e228b242SToby Isaac 43849435951eSToby Isaac Input Parameters: 4385bb7acecfSBarry Smith + dm - The `DM` 4386bb7acecfSBarry Smith . section - The `PetscSection` describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section. Must have a local communicator (`PETSC_COMM_SELF` or derivative). 438720f4b53cSBarry Smith . mat - The `Mat` that interpolates local constraints: its width should be the layout size of the default section: `NULL` indicates no constraints. Must have a local communicator (`PETSC_COMM_SELF` or derivative). 438820f4b53cSBarry Smith - bias - A bias vector to be added to constrained values in the local vector. `NULL` indicates no bias. Must have a local communicator (`PETSC_COMM_SELF` or derivative). 43899435951eSToby Isaac 43909435951eSToby Isaac Level: advanced 43919435951eSToby Isaac 439220f4b53cSBarry Smith Notes: 439320f4b53cSBarry Smith If a constraint matrix is specified, then it is applied during `DMGlobalToLocalEnd()` when mode is `INSERT_VALUES`, `INSERT_BC_VALUES`, or `INSERT_ALL_VALUES`. Without a constraint matrix, the local vector l returned by `DMGlobalToLocalEnd()` contains values that have been scattered from a global vector without modification; with a constraint matrix A, l is modified by computing c = A * l + bias, l[s[i]] = c[i], where the scatter s is defined by the `PetscSection` returned by `DMGetDefaultConstraints()`. 439420f4b53cSBarry Smith 439520f4b53cSBarry Smith If a constraint matrix is specified, then its adjoint is applied during `DMLocalToGlobalBegin()` when mode is `ADD_VALUES`, `ADD_BC_VALUES`, or `ADD_ALL_VALUES`. Without a constraint matrix, the local vector l is accumulated into a global vector without modification; with a constraint matrix A, l is first modified by computing c[i] = l[s[i]], l[s[i]] = 0, l = l + A'*c, which is the adjoint of the operation described above. Any bias, if specified, is ignored when accumulating. 439620f4b53cSBarry Smith 4397bb7acecfSBarry Smith This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them. 43989435951eSToby Isaac 43991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()` 44009435951eSToby Isaac @*/ 4401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias) 4402d71ae5a4SJacob Faibussowitsch { 4403e228b242SToby Isaac PetscMPIInt result; 44049435951eSToby Isaac 44059435951eSToby Isaac PetscFunctionBegin; 44069435951eSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4407e228b242SToby Isaac if (section) { 4408e228b242SToby Isaac PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2); 44099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result)); 44107a8be351SBarry Smith PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator"); 4411e228b242SToby Isaac } 4412e228b242SToby Isaac if (mat) { 4413e228b242SToby Isaac PetscValidHeaderSpecific(mat, MAT_CLASSID, 3); 44149566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result)); 44157a8be351SBarry Smith PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator"); 4416e228b242SToby Isaac } 441779769bd5SJed Brown if (bias) { 441879769bd5SJed Brown PetscValidHeaderSpecific(bias, VEC_CLASSID, 4); 44199566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result)); 442079769bd5SJed Brown PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator"); 442179769bd5SJed Brown } 44229566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)section)); 44239566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section)); 44243b8ba7d1SJed Brown dm->defaultConstraint.section = section; 44259566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mat)); 44269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&dm->defaultConstraint.mat)); 44273b8ba7d1SJed Brown dm->defaultConstraint.mat = mat; 44289566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)bias)); 44299566063dSJacob Faibussowitsch PetscCall(VecDestroy(&dm->defaultConstraint.bias)); 443079769bd5SJed Brown dm->defaultConstraint.bias = bias; 44313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44329435951eSToby Isaac } 44339435951eSToby Isaac 4434497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG) 4435507e4973SMatthew G. Knepley /* 4436bb7acecfSBarry Smith DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent. 4437507e4973SMatthew G. Knepley 4438507e4973SMatthew G. Knepley Input Parameters: 4439bb7acecfSBarry Smith + dm - The `DM` 4440bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout 4441bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout 4442507e4973SMatthew G. Knepley 4443507e4973SMatthew G. Knepley Level: intermediate 4444507e4973SMatthew G. Knepley 44451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()` 4446507e4973SMatthew G. Knepley */ 4447d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection) 4448d71ae5a4SJacob Faibussowitsch { 4449507e4973SMatthew G. Knepley MPI_Comm comm; 4450507e4973SMatthew G. Knepley PetscLayout layout; 4451507e4973SMatthew G. Knepley const PetscInt *ranges; 4452507e4973SMatthew G. Knepley PetscInt pStart, pEnd, p, nroots; 4453507e4973SMatthew G. Knepley PetscMPIInt size, rank; 4454507e4973SMatthew G. Knepley PetscBool valid = PETSC_TRUE, gvalid; 4455507e4973SMatthew G. Knepley 4456507e4973SMatthew G. Knepley PetscFunctionBegin; 44579566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 4458507e4973SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 44599566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 44609566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 44619566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd)); 44629566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots)); 44639566063dSJacob Faibussowitsch PetscCall(PetscLayoutCreate(comm, &layout)); 44649566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetBlockSize(layout, 1)); 44659566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetLocalSize(layout, nroots)); 44669566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetUp(layout)); 44679566063dSJacob Faibussowitsch PetscCall(PetscLayoutGetRanges(layout, &ranges)); 4468507e4973SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 4469f741bcd2SMatthew G. Knepley PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d; 4470507e4973SMatthew G. Knepley 44719566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(localSection, p, &dof)); 44729566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(localSection, p, &off)); 44739566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof)); 44749566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(globalSection, p, &gdof)); 44759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof)); 44769566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(globalSection, p, &goff)); 4477507e4973SMatthew G. Knepley if (!gdof) continue; /* Censored point */ 44789371c9d4SSatish Balay if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) { 44799371c9d4SSatish Balay PetscCall(PetscSynchronizedPrintf(comm, "[%d]Global dof %" PetscInt_FMT " for point %" PetscInt_FMT " not equal to local dof %" PetscInt_FMT "\n", rank, gdof, p, dof)); 44809371c9d4SSatish Balay valid = PETSC_FALSE; 44819371c9d4SSatish Balay } 44829371c9d4SSatish Balay if (gcdof && (gcdof != cdof)) { 44839371c9d4SSatish Balay PetscCall(PetscSynchronizedPrintf(comm, "[%d]Global constraints %" PetscInt_FMT " for point %" PetscInt_FMT " not equal to local constraints %" PetscInt_FMT "\n", rank, gcdof, p, cdof)); 44849371c9d4SSatish Balay valid = PETSC_FALSE; 44859371c9d4SSatish Balay } 4486507e4973SMatthew G. Knepley if (gdof < 0) { 4487507e4973SMatthew G. Knepley gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof; 4488507e4973SMatthew G. Knepley for (d = 0; d < gsize; ++d) { 4489507e4973SMatthew G. Knepley PetscInt offset = -(goff + 1) + d, r; 4490507e4973SMatthew G. Knepley 44919566063dSJacob Faibussowitsch PetscCall(PetscFindInt(offset, size + 1, ranges, &r)); 4492507e4973SMatthew G. Knepley if (r < 0) r = -(r + 2); 44939371c9d4SSatish Balay if ((r < 0) || (r >= size)) { 44949371c9d4SSatish Balay PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff)); 44959371c9d4SSatish Balay valid = PETSC_FALSE; 44969371c9d4SSatish Balay break; 44979371c9d4SSatish Balay } 4498507e4973SMatthew G. Knepley } 4499507e4973SMatthew G. Knepley } 4500507e4973SMatthew G. Knepley } 45019566063dSJacob Faibussowitsch PetscCall(PetscLayoutDestroy(&layout)); 45029566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedFlush(comm, NULL)); 45031c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm)); 4504507e4973SMatthew G. Knepley if (!gvalid) { 45059566063dSJacob Faibussowitsch PetscCall(DMView(dm, NULL)); 4506507e4973SMatthew G. Knepley SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections"); 4507507e4973SMatthew G. Knepley } 45083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4509507e4973SMatthew G. Knepley } 4510f741bcd2SMatthew G. Knepley #endif 4511507e4973SMatthew G. Knepley 45125f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf) 45136725e60dSJed Brown { 45146725e60dSJed Brown PetscErrorCode (*f)(DM, PetscSF *); 45156725e60dSJed Brown PetscFunctionBegin; 45166725e60dSJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 45174f572ea9SToby Isaac PetscAssertPointer(sf, 2); 45185f06a3ddSJed Brown PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f)); 45196725e60dSJed Brown if (f) PetscCall(f(dm, sf)); 45206725e60dSJed Brown else *sf = dm->sf; 45213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45226725e60dSJed Brown } 45236725e60dSJed Brown 452488ed4aceSMatthew G Knepley /*@ 4525bb7acecfSBarry Smith DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`. 452688ed4aceSMatthew G Knepley 452720f4b53cSBarry Smith Collective 45288b1ab98fSJed Brown 452988ed4aceSMatthew G Knepley Input Parameter: 4530bb7acecfSBarry Smith . dm - The `DM` 453188ed4aceSMatthew G Knepley 453288ed4aceSMatthew G Knepley Output Parameter: 4533bb7acecfSBarry Smith . section - The `PetscSection` 453488ed4aceSMatthew G Knepley 453588ed4aceSMatthew G Knepley Level: intermediate 453688ed4aceSMatthew G Knepley 4537bb7acecfSBarry Smith Note: 4538bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSection`. 453988ed4aceSMatthew G Knepley 45401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()` 454188ed4aceSMatthew G Knepley @*/ 4542d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section) 4543d71ae5a4SJacob Faibussowitsch { 454488ed4aceSMatthew G Knepley PetscFunctionBegin; 454588ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 45464f572ea9SToby Isaac PetscAssertPointer(section, 2); 45471bb6d2a8SBarry Smith if (!dm->globalSection) { 4548fd59a867SMatthew G. Knepley PetscSection s; 45496725e60dSJed Brown PetscSF sf; 4550fd59a867SMatthew G. Knepley 45519566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, &s)); 45527a8be351SBarry Smith PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection"); 45537a8be351SBarry Smith PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection"); 45545f06a3ddSJed Brown PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf)); 45556725e60dSJed Brown PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection)); 45569566063dSJacob Faibussowitsch PetscCall(PetscLayoutDestroy(&dm->map)); 45579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map)); 45589566063dSJacob Faibussowitsch PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view")); 455988ed4aceSMatthew G Knepley } 45601bb6d2a8SBarry Smith *section = dm->globalSection; 45613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 456288ed4aceSMatthew G Knepley } 456388ed4aceSMatthew G Knepley 4564b21d0597SMatthew G Knepley /*@ 4565bb7acecfSBarry Smith DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`. 4566b21d0597SMatthew G Knepley 4567b21d0597SMatthew G Knepley Input Parameters: 4568bb7acecfSBarry Smith + dm - The `DM` 45692fe279fdSBarry Smith - section - The PetscSection, or `NULL` 4570b21d0597SMatthew G Knepley 4571b21d0597SMatthew G Knepley Level: intermediate 4572b21d0597SMatthew G Knepley 4573bb7acecfSBarry Smith Note: 4574bb7acecfSBarry Smith Any existing `PetscSection` will be destroyed 4575b21d0597SMatthew G Knepley 45761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()` 4577b21d0597SMatthew G Knepley @*/ 4578d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section) 4579d71ae5a4SJacob Faibussowitsch { 4580b21d0597SMatthew G Knepley PetscFunctionBegin; 4581b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 45825080bbdbSMatthew G Knepley if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2); 45839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)section)); 45849566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&dm->globalSection)); 45851bb6d2a8SBarry Smith dm->globalSection = section; 4586497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG) 45879566063dSJacob Faibussowitsch if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section)); 4588507e4973SMatthew G. Knepley #endif 45893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4590b21d0597SMatthew G Knepley } 4591b21d0597SMatthew G Knepley 459288ed4aceSMatthew G Knepley /*@ 4593bb7acecfSBarry Smith DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set, 4594bb7acecfSBarry Smith it is created from the default `PetscSection` layouts in the `DM`. 459588ed4aceSMatthew G Knepley 459688ed4aceSMatthew G Knepley Input Parameter: 4597bb7acecfSBarry Smith . dm - The `DM` 459888ed4aceSMatthew G Knepley 459988ed4aceSMatthew G Knepley Output Parameter: 4600bb7acecfSBarry Smith . sf - The `PetscSF` 460188ed4aceSMatthew G Knepley 460288ed4aceSMatthew G Knepley Level: intermediate 460388ed4aceSMatthew G Knepley 4604bb7acecfSBarry Smith Note: 4605bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSF`. 460688ed4aceSMatthew G Knepley 46071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()` 460888ed4aceSMatthew G Knepley @*/ 4609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf) 4610d71ae5a4SJacob Faibussowitsch { 461188ed4aceSMatthew G Knepley PetscInt nroots; 461288ed4aceSMatthew G Knepley 461388ed4aceSMatthew G Knepley PetscFunctionBegin; 461488ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 46154f572ea9SToby Isaac PetscAssertPointer(sf, 2); 461648a46eb9SPierre Jolivet if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF)); 46179566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL)); 461888ed4aceSMatthew G Knepley if (nroots < 0) { 461988ed4aceSMatthew G Knepley PetscSection section, gSection; 462088ed4aceSMatthew G Knepley 46219566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 462231ea6d37SMatthew G Knepley if (section) { 46239566063dSJacob Faibussowitsch PetscCall(DMGetGlobalSection(dm, &gSection)); 46249566063dSJacob Faibussowitsch PetscCall(DMCreateSectionSF(dm, section, gSection)); 462531ea6d37SMatthew G Knepley } else { 46260298fd71SBarry Smith *sf = NULL; 46273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 462831ea6d37SMatthew G Knepley } 462988ed4aceSMatthew G Knepley } 46301bb6d2a8SBarry Smith *sf = dm->sectionSF; 46313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 463288ed4aceSMatthew G Knepley } 463388ed4aceSMatthew G Knepley 463488ed4aceSMatthew G Knepley /*@ 4635bb7acecfSBarry Smith DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM` 463688ed4aceSMatthew G Knepley 463788ed4aceSMatthew G Knepley Input Parameters: 4638bb7acecfSBarry Smith + dm - The `DM` 4639bb7acecfSBarry Smith - sf - The `PetscSF` 464088ed4aceSMatthew G Knepley 464188ed4aceSMatthew G Knepley Level: intermediate 464288ed4aceSMatthew G Knepley 4643bb7acecfSBarry Smith Note: 4644bb7acecfSBarry Smith Any previous `PetscSF` is destroyed 464588ed4aceSMatthew G Knepley 46461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()` 464788ed4aceSMatthew G Knepley @*/ 4648d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf) 4649d71ae5a4SJacob Faibussowitsch { 465088ed4aceSMatthew G Knepley PetscFunctionBegin; 465188ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4652b9d85ea2SLisandro Dalcin if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 46539566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)sf)); 46549566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&dm->sectionSF)); 46551bb6d2a8SBarry Smith dm->sectionSF = sf; 46563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 465788ed4aceSMatthew G Knepley } 465888ed4aceSMatthew G Knepley 465988ed4aceSMatthew G Knepley /*@C 4660bb7acecfSBarry Smith DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s 466188ed4aceSMatthew G Knepley describing the data layout. 466288ed4aceSMatthew G Knepley 466388ed4aceSMatthew G Knepley Input Parameters: 4664bb7acecfSBarry Smith + dm - The `DM` 4665bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout 4666bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout 466788ed4aceSMatthew G Knepley 46681bb6d2a8SBarry Smith Level: developer 46691bb6d2a8SBarry Smith 4670bb7acecfSBarry Smith Note: 4671bb7acecfSBarry Smith One usually uses `DMGetSectionSF()` to obtain the `PetscSF` 4672bb7acecfSBarry Smith 467360225df5SJacob Faibussowitsch Developer Notes: 4674bb7acecfSBarry Smith Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF` 4675bb7acecfSBarry Smith directly into the `DM`, perhaps this function should not take the local and global sections as 4676bb7acecfSBarry Smith input and should just obtain them from the `DM`? 46771bb6d2a8SBarry Smith 46781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()` 467988ed4aceSMatthew G Knepley @*/ 4680d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection) 4681d71ae5a4SJacob Faibussowitsch { 468288ed4aceSMatthew G Knepley PetscFunctionBegin; 468388ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 46849566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection)); 46853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 468688ed4aceSMatthew G Knepley } 4687af122d2aSMatthew G Knepley 4688b21d0597SMatthew G Knepley /*@ 4689bb7acecfSBarry Smith DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`. 4690bb7acecfSBarry Smith 4691bb7acecfSBarry Smith Not collective but the resulting `PetscSF` is collective 4692b21d0597SMatthew G Knepley 4693b21d0597SMatthew G Knepley Input Parameter: 4694bb7acecfSBarry Smith . dm - The `DM` 4695b21d0597SMatthew G Knepley 4696b21d0597SMatthew G Knepley Output Parameter: 4697bb7acecfSBarry Smith . sf - The `PetscSF` 4698b21d0597SMatthew G Knepley 4699b21d0597SMatthew G Knepley Level: intermediate 4700b21d0597SMatthew G Knepley 4701bb7acecfSBarry Smith Note: 4702bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSF`. 4703b21d0597SMatthew G Knepley 47041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()` 4705b21d0597SMatthew G Knepley @*/ 4706d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) 4707d71ae5a4SJacob Faibussowitsch { 4708b21d0597SMatthew G Knepley PetscFunctionBegin; 4709b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 47104f572ea9SToby Isaac PetscAssertPointer(sf, 2); 4711b21d0597SMatthew G Knepley *sf = dm->sf; 47123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4713b21d0597SMatthew G Knepley } 4714b21d0597SMatthew G Knepley 4715057b4bcdSMatthew G Knepley /*@ 4716bb7acecfSBarry Smith DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`. 4717bb7acecfSBarry Smith 471820f4b53cSBarry Smith Collective 4719057b4bcdSMatthew G Knepley 4720057b4bcdSMatthew G Knepley Input Parameters: 4721bb7acecfSBarry Smith + dm - The `DM` 4722bb7acecfSBarry Smith - sf - The `PetscSF` 4723057b4bcdSMatthew G Knepley 4724057b4bcdSMatthew G Knepley Level: intermediate 4725057b4bcdSMatthew G Knepley 47261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()` 4727057b4bcdSMatthew G Knepley @*/ 4728d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) 4729d71ae5a4SJacob Faibussowitsch { 4730057b4bcdSMatthew G Knepley PetscFunctionBegin; 4731057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4732b9d85ea2SLisandro Dalcin if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 47339566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)sf)); 47349566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&dm->sf)); 4735057b4bcdSMatthew G Knepley dm->sf = sf; 47363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4737057b4bcdSMatthew G Knepley } 4738057b4bcdSMatthew G Knepley 47394f37162bSMatthew G. Knepley /*@ 4740bb7acecfSBarry Smith DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering 47414f37162bSMatthew G. Knepley 47424f37162bSMatthew G. Knepley Input Parameter: 4743bb7acecfSBarry Smith . dm - The `DM` 47444f37162bSMatthew G. Knepley 47454f37162bSMatthew G. Knepley Output Parameter: 4746bb7acecfSBarry Smith . sf - The `PetscSF` 47474f37162bSMatthew G. Knepley 47484f37162bSMatthew G. Knepley Level: intermediate 47494f37162bSMatthew G. Knepley 4750bb7acecfSBarry Smith Note: 4751bb7acecfSBarry Smith This gets a borrowed reference, so the user should not destroy this `PetscSF`. 47524f37162bSMatthew G. Knepley 47531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()` 47544f37162bSMatthew G. Knepley @*/ 4755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf) 4756d71ae5a4SJacob Faibussowitsch { 47574f37162bSMatthew G. Knepley PetscFunctionBegin; 47584f37162bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 47594f572ea9SToby Isaac PetscAssertPointer(sf, 2); 47604f37162bSMatthew G. Knepley *sf = dm->sfNatural; 47613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47624f37162bSMatthew G. Knepley } 47634f37162bSMatthew G. Knepley 47644f37162bSMatthew G. Knepley /*@ 47654f37162bSMatthew G. Knepley DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering 47664f37162bSMatthew G. Knepley 47674f37162bSMatthew G. Knepley Input Parameters: 47684f37162bSMatthew G. Knepley + dm - The DM 47694f37162bSMatthew G. Knepley - sf - The PetscSF 47704f37162bSMatthew G. Knepley 47714f37162bSMatthew G. Knepley Level: intermediate 47724f37162bSMatthew G. Knepley 47731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()` 47744f37162bSMatthew G. Knepley @*/ 4775d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf) 4776d71ae5a4SJacob Faibussowitsch { 47774f37162bSMatthew G. Knepley PetscFunctionBegin; 47784f37162bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 47794f37162bSMatthew G. Knepley if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 47809566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)sf)); 47819566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&dm->sfNatural)); 47824f37162bSMatthew G. Knepley dm->sfNatural = sf; 47833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47844f37162bSMatthew G. Knepley } 47854f37162bSMatthew G. Knepley 4786d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc) 4787d71ae5a4SJacob Faibussowitsch { 478834aa8a36SMatthew G. Knepley PetscClassId id; 478934aa8a36SMatthew G. Knepley 479034aa8a36SMatthew G. Knepley PetscFunctionBegin; 47919566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 479234aa8a36SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 47939566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE)); 479434aa8a36SMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 47959566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE)); 479617c1d62eSMatthew G. Knepley } else { 47979566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE)); 479834aa8a36SMatthew G. Knepley } 47993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 480034aa8a36SMatthew G. Knepley } 480134aa8a36SMatthew G. Knepley 4802d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew) 4803d71ae5a4SJacob Faibussowitsch { 480444a7f3ddSMatthew G. Knepley RegionField *tmpr; 480544a7f3ddSMatthew G. Knepley PetscInt Nf = dm->Nf, f; 480644a7f3ddSMatthew G. Knepley 480744a7f3ddSMatthew G. Knepley PetscFunctionBegin; 48083ba16761SJacob Faibussowitsch if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS); 48099566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(NfNew, &tmpr)); 481044a7f3ddSMatthew G. Knepley for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f]; 48119371c9d4SSatish Balay for (f = Nf; f < NfNew; ++f) { 48129371c9d4SSatish Balay tmpr[f].disc = NULL; 48139371c9d4SSatish Balay tmpr[f].label = NULL; 48149371c9d4SSatish Balay tmpr[f].avoidTensor = PETSC_FALSE; 48159371c9d4SSatish Balay } 48169566063dSJacob Faibussowitsch PetscCall(PetscFree(dm->fields)); 481744a7f3ddSMatthew G. Knepley dm->Nf = NfNew; 481844a7f3ddSMatthew G. Knepley dm->fields = tmpr; 48193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 482044a7f3ddSMatthew G. Knepley } 482144a7f3ddSMatthew G. Knepley 482244a7f3ddSMatthew G. Knepley /*@ 482320f4b53cSBarry Smith DMClearFields - Remove all fields from the `DM` 482444a7f3ddSMatthew G. Knepley 482520f4b53cSBarry Smith Logically Collective 482644a7f3ddSMatthew G. Knepley 482744a7f3ddSMatthew G. Knepley Input Parameter: 482820f4b53cSBarry Smith . dm - The `DM` 482944a7f3ddSMatthew G. Knepley 483044a7f3ddSMatthew G. Knepley Level: intermediate 483144a7f3ddSMatthew G. Knepley 48321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()` 483344a7f3ddSMatthew G. Knepley @*/ 4834d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm) 4835d71ae5a4SJacob Faibussowitsch { 483644a7f3ddSMatthew G. Knepley PetscInt f; 483744a7f3ddSMatthew G. Knepley 483844a7f3ddSMatthew G. Knepley PetscFunctionBegin; 483944a7f3ddSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 484044a7f3ddSMatthew G. Knepley for (f = 0; f < dm->Nf; ++f) { 48419566063dSJacob Faibussowitsch PetscCall(PetscObjectDestroy(&dm->fields[f].disc)); 48429566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&dm->fields[f].label)); 484344a7f3ddSMatthew G. Knepley } 48449566063dSJacob Faibussowitsch PetscCall(PetscFree(dm->fields)); 484544a7f3ddSMatthew G. Knepley dm->fields = NULL; 484644a7f3ddSMatthew G. Knepley dm->Nf = 0; 48473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 484844a7f3ddSMatthew G. Knepley } 484944a7f3ddSMatthew G. Knepley 4850689b5837SMatthew G. Knepley /*@ 485120f4b53cSBarry Smith DMGetNumFields - Get the number of fields in the `DM` 4852689b5837SMatthew G. Knepley 485320f4b53cSBarry Smith Not Collective 4854689b5837SMatthew G. Knepley 4855689b5837SMatthew G. Knepley Input Parameter: 485620f4b53cSBarry Smith . dm - The `DM` 4857689b5837SMatthew G. Knepley 4858689b5837SMatthew G. Knepley Output Parameter: 485960225df5SJacob Faibussowitsch . numFields - The number of fields 4860689b5837SMatthew G. Knepley 4861689b5837SMatthew G. Knepley Level: intermediate 4862689b5837SMatthew G. Knepley 48631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()` 4864689b5837SMatthew G. Knepley @*/ 4865d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) 4866d71ae5a4SJacob Faibussowitsch { 48670f21e855SMatthew G. Knepley PetscFunctionBegin; 48680f21e855SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 48694f572ea9SToby Isaac PetscAssertPointer(numFields, 2); 487044a7f3ddSMatthew G. Knepley *numFields = dm->Nf; 48713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4872af122d2aSMatthew G Knepley } 4873af122d2aSMatthew G Knepley 4874689b5837SMatthew G. Knepley /*@ 487520f4b53cSBarry Smith DMSetNumFields - Set the number of fields in the `DM` 4876689b5837SMatthew G. Knepley 487720f4b53cSBarry Smith Logically Collective 4878689b5837SMatthew G. Knepley 4879689b5837SMatthew G. Knepley Input Parameters: 488020f4b53cSBarry Smith + dm - The `DM` 488160225df5SJacob Faibussowitsch - numFields - The number of fields 4882689b5837SMatthew G. Knepley 4883689b5837SMatthew G. Knepley Level: intermediate 4884689b5837SMatthew G. Knepley 48851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()` 4886689b5837SMatthew G. Knepley @*/ 4887d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) 4888d71ae5a4SJacob Faibussowitsch { 48890f21e855SMatthew G. Knepley PetscInt Nf, f; 4890af122d2aSMatthew G Knepley 4891af122d2aSMatthew G Knepley PetscFunctionBegin; 4892af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 48939566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 48940f21e855SMatthew G. Knepley for (f = Nf; f < numFields; ++f) { 48950f21e855SMatthew G. Knepley PetscContainer obj; 48960f21e855SMatthew G. Knepley 48979566063dSJacob Faibussowitsch PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj)); 48989566063dSJacob Faibussowitsch PetscCall(DMAddField(dm, NULL, (PetscObject)obj)); 48999566063dSJacob Faibussowitsch PetscCall(PetscContainerDestroy(&obj)); 4900af122d2aSMatthew G Knepley } 49013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4902af122d2aSMatthew G Knepley } 4903af122d2aSMatthew G Knepley 4904c1929be8SMatthew G. Knepley /*@ 4905bb7acecfSBarry Smith DMGetField - Return the `DMLabel` and discretization object for a given `DM` field 4906c1929be8SMatthew G. Knepley 490720f4b53cSBarry Smith Not Collective 4908c1929be8SMatthew G. Knepley 4909c1929be8SMatthew G. Knepley Input Parameters: 4910bb7acecfSBarry Smith + dm - The `DM` 4911c1929be8SMatthew G. Knepley - f - The field number 4912c1929be8SMatthew G. Knepley 491344a7f3ddSMatthew G. Knepley Output Parameters: 491420f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed) 491520f4b53cSBarry Smith - disc - The discretization object (pass in `NULL` if not needed) 4916c1929be8SMatthew G. Knepley 491744a7f3ddSMatthew G. Knepley Level: intermediate 4918c1929be8SMatthew G. Knepley 49191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()` 4920c1929be8SMatthew G. Knepley @*/ 4921d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc) 4922d71ae5a4SJacob Faibussowitsch { 4923af122d2aSMatthew G Knepley PetscFunctionBegin; 4924af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 49254f572ea9SToby Isaac PetscAssertPointer(disc, 4); 49267a8be351SBarry Smith PetscCheck((f >= 0) && (f < dm->Nf), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, dm->Nf); 492744a7f3ddSMatthew G. Knepley if (label) *label = dm->fields[f].label; 4928bb7acecfSBarry Smith if (disc) *disc = dm->fields[f].disc; 49293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4930decb47aaSMatthew G. Knepley } 4931decb47aaSMatthew G. Knepley 4932083401c6SMatthew G. Knepley /* Does not clear the DS */ 4933d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc) 4934d71ae5a4SJacob Faibussowitsch { 4935083401c6SMatthew G. Knepley PetscFunctionBegin; 49369566063dSJacob Faibussowitsch PetscCall(DMFieldEnlarge_Static(dm, f + 1)); 49379566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&dm->fields[f].label)); 49389566063dSJacob Faibussowitsch PetscCall(PetscObjectDestroy(&dm->fields[f].disc)); 4939083401c6SMatthew G. Knepley dm->fields[f].label = label; 4940bb7acecfSBarry Smith dm->fields[f].disc = disc; 49419566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 4942bb7acecfSBarry Smith PetscCall(PetscObjectReference((PetscObject)disc)); 49433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4944083401c6SMatthew G. Knepley } 4945083401c6SMatthew G. Knepley 494646560f82SMatthew G. Knepley /*@C 4947bb7acecfSBarry Smith DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles 4948bb7acecfSBarry Smith the field numbering. 4949c1929be8SMatthew G. Knepley 495020f4b53cSBarry Smith Logically Collective 4951c1929be8SMatthew G. Knepley 4952c1929be8SMatthew G. Knepley Input Parameters: 4953bb7acecfSBarry Smith + dm - The `DM` 4954c1929be8SMatthew G. Knepley . f - The field number 495520f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh 4956bb7acecfSBarry Smith - disc - The discretization object 4957c1929be8SMatthew G. Knepley 495844a7f3ddSMatthew G. Knepley Level: intermediate 4959c1929be8SMatthew G. Knepley 49601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()` 4961c1929be8SMatthew G. Knepley @*/ 4962d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc) 4963d71ae5a4SJacob Faibussowitsch { 4964decb47aaSMatthew G. Knepley PetscFunctionBegin; 4965decb47aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4966e5e52638SMatthew G. Knepley if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3); 4967bb7acecfSBarry Smith PetscValidHeader(disc, 4); 49687a8be351SBarry Smith PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f); 4969bb7acecfSBarry Smith PetscCall(DMSetField_Internal(dm, f, label, disc)); 4970bb7acecfSBarry Smith PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc)); 49719566063dSJacob Faibussowitsch PetscCall(DMClearDS(dm)); 49723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 497344a7f3ddSMatthew G. Knepley } 497444a7f3ddSMatthew G. Knepley 497546560f82SMatthew G. Knepley /*@C 4976bb7acecfSBarry Smith DMAddField - Add a field to a `DM` object. A field is a function space defined by of a set of discretization points (geometric entities) 4977bb7acecfSBarry Smith and a discretization object that defines the function space associated with those points. 497844a7f3ddSMatthew G. Knepley 497920f4b53cSBarry Smith Logically Collective 498044a7f3ddSMatthew G. Knepley 498144a7f3ddSMatthew G. Knepley Input Parameters: 4982bb7acecfSBarry Smith + dm - The `DM` 498320f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh 4984bb7acecfSBarry Smith - disc - The discretization object 498544a7f3ddSMatthew G. Knepley 498644a7f3ddSMatthew G. Knepley Level: intermediate 498744a7f3ddSMatthew G. Knepley 4988bb7acecfSBarry Smith Notes: 4989bb7acecfSBarry Smith The label already exists or will be added to the `DM` with `DMSetLabel()`. 4990bb7acecfSBarry Smith 4991da81f932SPierre Jolivet For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions 4992bb7acecfSBarry Smith within each cell. Thus a specific function in the space is defined by the combination of a `Vec` containing the coefficients, a `DM` defining the 4993bb7acecfSBarry Smith geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`. 4994bb7acecfSBarry Smith 49951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE` 499644a7f3ddSMatthew G. Knepley @*/ 4997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc) 4998d71ae5a4SJacob Faibussowitsch { 499944a7f3ddSMatthew G. Knepley PetscInt Nf = dm->Nf; 500044a7f3ddSMatthew G. Knepley 500144a7f3ddSMatthew G. Knepley PetscFunctionBegin; 500244a7f3ddSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5003064a246eSJacob Faibussowitsch if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 5004bb7acecfSBarry Smith PetscValidHeader(disc, 3); 50059566063dSJacob Faibussowitsch PetscCall(DMFieldEnlarge_Static(dm, Nf + 1)); 500644a7f3ddSMatthew G. Knepley dm->fields[Nf].label = label; 5007bb7acecfSBarry Smith dm->fields[Nf].disc = disc; 50089566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 5009bb7acecfSBarry Smith PetscCall(PetscObjectReference((PetscObject)disc)); 5010bb7acecfSBarry Smith PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc)); 50119566063dSJacob Faibussowitsch PetscCall(DMClearDS(dm)); 50123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5013af122d2aSMatthew G Knepley } 50146636e97aSMatthew G Knepley 5015e5e52638SMatthew G. Knepley /*@ 5016e0b68406SMatthew Knepley DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells 5017e0b68406SMatthew Knepley 501820f4b53cSBarry Smith Logically Collective 5019e0b68406SMatthew Knepley 5020e0b68406SMatthew Knepley Input Parameters: 5021bb7acecfSBarry Smith + dm - The `DM` 5022e0b68406SMatthew Knepley . f - The field index 5023bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells 5024e0b68406SMatthew Knepley 5025e0b68406SMatthew Knepley Level: intermediate 5026e0b68406SMatthew Knepley 50271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()` 5028e0b68406SMatthew Knepley @*/ 5029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor) 5030d71ae5a4SJacob Faibussowitsch { 5031e0b68406SMatthew Knepley PetscFunctionBegin; 503263a3b9bcSJacob Faibussowitsch PetscCheck((f >= 0) && (f < dm->Nf), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " is not in [0, %" PetscInt_FMT ")", f, dm->Nf); 5033e0b68406SMatthew Knepley dm->fields[f].avoidTensor = avoidTensor; 50343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5035e0b68406SMatthew Knepley } 5036e0b68406SMatthew Knepley 5037e0b68406SMatthew Knepley /*@ 5038e0b68406SMatthew Knepley DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells 5039e0b68406SMatthew Knepley 504020f4b53cSBarry Smith Not Collective 5041e0b68406SMatthew Knepley 5042e0b68406SMatthew Knepley Input Parameters: 5043bb7acecfSBarry Smith + dm - The `DM` 5044e0b68406SMatthew Knepley - f - The field index 5045e0b68406SMatthew Knepley 5046e0b68406SMatthew Knepley Output Parameter: 5047e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells 5048e0b68406SMatthew Knepley 5049e0b68406SMatthew Knepley Level: intermediate 5050e0b68406SMatthew Knepley 505160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()` 5052e0b68406SMatthew Knepley @*/ 5053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor) 5054d71ae5a4SJacob Faibussowitsch { 5055e0b68406SMatthew Knepley PetscFunctionBegin; 505663a3b9bcSJacob Faibussowitsch PetscCheck((f >= 0) && (f < dm->Nf), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " is not in [0, %" PetscInt_FMT ")", f, dm->Nf); 5057e0b68406SMatthew Knepley *avoidTensor = dm->fields[f].avoidTensor; 50583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5059e0b68406SMatthew Knepley } 5060e0b68406SMatthew Knepley 5061e0b68406SMatthew Knepley /*@ 5062bb7acecfSBarry Smith DMCopyFields - Copy the discretizations for the `DM` into another `DM` 5063e5e52638SMatthew G. Knepley 506420f4b53cSBarry Smith Collective 5065e5e52638SMatthew G. Knepley 5066e5e52638SMatthew G. Knepley Input Parameter: 5067bb7acecfSBarry Smith . dm - The `DM` 5068e5e52638SMatthew G. Knepley 5069e5e52638SMatthew G. Knepley Output Parameter: 5070bb7acecfSBarry Smith . newdm - The `DM` 5071e5e52638SMatthew G. Knepley 5072e5e52638SMatthew G. Knepley Level: advanced 5073e5e52638SMatthew G. Knepley 50741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()` 5075e5e52638SMatthew G. Knepley @*/ 5076d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm) 5077d71ae5a4SJacob Faibussowitsch { 5078e5e52638SMatthew G. Knepley PetscInt Nf, f; 5079e5e52638SMatthew G. Knepley 5080e5e52638SMatthew G. Knepley PetscFunctionBegin; 50813ba16761SJacob Faibussowitsch if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS); 50829566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 50839566063dSJacob Faibussowitsch PetscCall(DMClearFields(newdm)); 5084e5e52638SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 5085e5e52638SMatthew G. Knepley DMLabel label; 5086e5e52638SMatthew G. Knepley PetscObject field; 508734aa8a36SMatthew G. Knepley PetscBool useCone, useClosure; 5088e5e52638SMatthew G. Knepley 50899566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, &label, &field)); 50909566063dSJacob Faibussowitsch PetscCall(DMSetField(newdm, f, label, field)); 50919566063dSJacob Faibussowitsch PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure)); 50929566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure)); 509334aa8a36SMatthew G. Knepley } 50943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 509534aa8a36SMatthew G. Knepley } 509634aa8a36SMatthew G. Knepley 509734aa8a36SMatthew G. Knepley /*@ 509834aa8a36SMatthew G. Knepley DMGetAdjacency - Returns the flags for determining variable influence 509934aa8a36SMatthew G. Knepley 510020f4b53cSBarry Smith Not Collective 510134aa8a36SMatthew G. Knepley 510234aa8a36SMatthew G. Knepley Input Parameters: 510320f4b53cSBarry Smith + dm - The `DM` object 510420f4b53cSBarry Smith - f - The field number, or `PETSC_DEFAULT` for the default adjacency 510534aa8a36SMatthew G. Knepley 5106d8d19677SJose E. Roman Output Parameters: 510734aa8a36SMatthew G. Knepley + useCone - Flag for variable influence starting with the cone operation 510834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure 510934aa8a36SMatthew G. Knepley 511034aa8a36SMatthew G. Knepley Level: developer 511134aa8a36SMatthew G. Knepley 511220f4b53cSBarry Smith Notes: 511320f4b53cSBarry Smith .vb 511420f4b53cSBarry Smith FEM: Two points p and q are adjacent if q \in closure(star(p)), useCone = PETSC_FALSE, useClosure = PETSC_TRUE 511520f4b53cSBarry Smith FVM: Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE, useClosure = PETSC_FALSE 511620f4b53cSBarry Smith FVM++: Two points p and q are adjacent if q \in star(closure(p)), useCone = PETSC_TRUE, useClosure = PETSC_TRUE 511720f4b53cSBarry Smith .ve 511820f4b53cSBarry Smith Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another. 511920f4b53cSBarry Smith 51201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()` 512134aa8a36SMatthew G. Knepley @*/ 5122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure) 5123d71ae5a4SJacob Faibussowitsch { 512434aa8a36SMatthew G. Knepley PetscFunctionBegin; 512534aa8a36SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 51264f572ea9SToby Isaac if (useCone) PetscAssertPointer(useCone, 3); 51274f572ea9SToby Isaac if (useClosure) PetscAssertPointer(useClosure, 4); 512834aa8a36SMatthew G. Knepley if (f < 0) { 512934aa8a36SMatthew G. Knepley if (useCone) *useCone = dm->adjacency[0]; 513034aa8a36SMatthew G. Knepley if (useClosure) *useClosure = dm->adjacency[1]; 513134aa8a36SMatthew G. Knepley } else { 513234aa8a36SMatthew G. Knepley PetscInt Nf; 513334aa8a36SMatthew G. Knepley 51349566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 51357a8be351SBarry Smith PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf); 513634aa8a36SMatthew G. Knepley if (useCone) *useCone = dm->fields[f].adjacency[0]; 513734aa8a36SMatthew G. Knepley if (useClosure) *useClosure = dm->fields[f].adjacency[1]; 513834aa8a36SMatthew G. Knepley } 51393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 514034aa8a36SMatthew G. Knepley } 514134aa8a36SMatthew G. Knepley 514234aa8a36SMatthew G. Knepley /*@ 514334aa8a36SMatthew G. Knepley DMSetAdjacency - Set the flags for determining variable influence 514434aa8a36SMatthew G. Knepley 514520f4b53cSBarry Smith Not Collective 514634aa8a36SMatthew G. Knepley 514734aa8a36SMatthew G. Knepley Input Parameters: 514820f4b53cSBarry Smith + dm - The `DM` object 514934aa8a36SMatthew G. Knepley . f - The field number 515034aa8a36SMatthew G. Knepley . useCone - Flag for variable influence starting with the cone operation 515134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure 515234aa8a36SMatthew G. Knepley 515334aa8a36SMatthew G. Knepley Level: developer 515434aa8a36SMatthew G. Knepley 515520f4b53cSBarry Smith Notes: 515620f4b53cSBarry Smith .vb 515720f4b53cSBarry Smith FEM: Two points p and q are adjacent if q \in closure(star(p)), useCone = PETSC_FALSE, useClosure = PETSC_TRUE 515820f4b53cSBarry Smith FVM: Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE, useClosure = PETSC_FALSE 515920f4b53cSBarry Smith FVM++: Two points p and q are adjacent if q \in star(closure(p)), useCone = PETSC_TRUE, useClosure = PETSC_TRUE 516020f4b53cSBarry Smith .ve 516120f4b53cSBarry Smith Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another. 516220f4b53cSBarry Smith 51631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()` 516434aa8a36SMatthew G. Knepley @*/ 5165d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure) 5166d71ae5a4SJacob Faibussowitsch { 516734aa8a36SMatthew G. Knepley PetscFunctionBegin; 516834aa8a36SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 516934aa8a36SMatthew G. Knepley if (f < 0) { 517034aa8a36SMatthew G. Knepley dm->adjacency[0] = useCone; 517134aa8a36SMatthew G. Knepley dm->adjacency[1] = useClosure; 517234aa8a36SMatthew G. Knepley } else { 517334aa8a36SMatthew G. Knepley PetscInt Nf; 517434aa8a36SMatthew G. Knepley 51759566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 51767a8be351SBarry Smith PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf); 517734aa8a36SMatthew G. Knepley dm->fields[f].adjacency[0] = useCone; 517834aa8a36SMatthew G. Knepley dm->fields[f].adjacency[1] = useClosure; 5179e5e52638SMatthew G. Knepley } 51803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5181e5e52638SMatthew G. Knepley } 5182e5e52638SMatthew G. Knepley 5183b0441da4SMatthew G. Knepley /*@ 5184b0441da4SMatthew G. Knepley DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined 5185b0441da4SMatthew G. Knepley 5186b0441da4SMatthew G. Knepley Not collective 5187b0441da4SMatthew G. Knepley 5188f899ff85SJose E. Roman Input Parameter: 518920f4b53cSBarry Smith . dm - The `DM` object 5190b0441da4SMatthew G. Knepley 5191d8d19677SJose E. Roman Output Parameters: 5192b0441da4SMatthew G. Knepley + useCone - Flag for variable influence starting with the cone operation 5193b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure 5194b0441da4SMatthew G. Knepley 5195b0441da4SMatthew G. Knepley Level: developer 5196b0441da4SMatthew G. Knepley 519720f4b53cSBarry Smith Notes: 519820f4b53cSBarry Smith .vb 519920f4b53cSBarry Smith FEM: Two points p and q are adjacent if q \in closure(star(p)), useCone = PETSC_FALSE, useClosure = PETSC_TRUE 520020f4b53cSBarry Smith FVM: Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE, useClosure = PETSC_FALSE 520120f4b53cSBarry Smith FVM++: Two points p and q are adjacent if q \in star(closure(p)), useCone = PETSC_TRUE, useClosure = PETSC_TRUE 520220f4b53cSBarry Smith .ve 520320f4b53cSBarry Smith 52041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()` 5205b0441da4SMatthew G. Knepley @*/ 5206d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure) 5207d71ae5a4SJacob Faibussowitsch { 5208b0441da4SMatthew G. Knepley PetscInt Nf; 5209b0441da4SMatthew G. Knepley 5210b0441da4SMatthew G. Knepley PetscFunctionBegin; 5211b0441da4SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 52124f572ea9SToby Isaac if (useCone) PetscAssertPointer(useCone, 2); 52134f572ea9SToby Isaac if (useClosure) PetscAssertPointer(useClosure, 3); 52149566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 5215b0441da4SMatthew G. Knepley if (!Nf) { 52169566063dSJacob Faibussowitsch PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure)); 5217b0441da4SMatthew G. Knepley } else { 52189566063dSJacob Faibussowitsch PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure)); 5219b0441da4SMatthew G. Knepley } 52203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5221b0441da4SMatthew G. Knepley } 5222b0441da4SMatthew G. Knepley 5223b0441da4SMatthew G. Knepley /*@ 5224b0441da4SMatthew G. Knepley DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined 5225b0441da4SMatthew G. Knepley 522620f4b53cSBarry Smith Not Collective 5227b0441da4SMatthew G. Knepley 5228b0441da4SMatthew G. Knepley Input Parameters: 522920f4b53cSBarry Smith + dm - The `DM` object 5230b0441da4SMatthew G. Knepley . useCone - Flag for variable influence starting with the cone operation 5231b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure 5232b0441da4SMatthew G. Knepley 5233b0441da4SMatthew G. Knepley Level: developer 5234b0441da4SMatthew G. Knepley 523520f4b53cSBarry Smith Notes: 523620f4b53cSBarry Smith .vb 523720f4b53cSBarry Smith FEM: Two points p and q are adjacent if q \in closure(star(p)), useCone = PETSC_FALSE, useClosure = PETSC_TRUE 523820f4b53cSBarry Smith FVM: Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE, useClosure = PETSC_FALSE 523920f4b53cSBarry Smith FVM++: Two points p and q are adjacent if q \in star(closure(p)), useCone = PETSC_TRUE, useClosure = PETSC_TRUE 524020f4b53cSBarry Smith .ve 524120f4b53cSBarry Smith 52421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()` 5243b0441da4SMatthew G. Knepley @*/ 5244d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure) 5245d71ae5a4SJacob Faibussowitsch { 5246b0441da4SMatthew G. Knepley PetscInt Nf; 5247b0441da4SMatthew G. Knepley 5248b0441da4SMatthew G. Knepley PetscFunctionBegin; 5249b0441da4SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 52509566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 5251b0441da4SMatthew G. Knepley if (!Nf) { 52529566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure)); 5253b0441da4SMatthew G. Knepley } else { 52549566063dSJacob Faibussowitsch PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure)); 5255e5e52638SMatthew G. Knepley } 52563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5257e5e52638SMatthew G. Knepley } 5258e5e52638SMatthew G. Knepley 5259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm) 5260d71ae5a4SJacob Faibussowitsch { 5261799db056SMatthew G. Knepley DM plex; 5262799db056SMatthew G. Knepley DMLabel *labels, *glabels; 5263799db056SMatthew G. Knepley const char **names; 5264799db056SMatthew G. Knepley char *sendNames, *recvNames; 5265799db056SMatthew G. Knepley PetscInt Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m; 5266799db056SMatthew G. Knepley size_t len; 5267799db056SMatthew G. Knepley MPI_Comm comm; 5268799db056SMatthew G. Knepley PetscMPIInt rank, size, p, *counts, *displs; 5269783e2ec8SMatthew G. Knepley 5270783e2ec8SMatthew G. Knepley PetscFunctionBegin; 5271799db056SMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 5272799db056SMatthew G. Knepley PetscCallMPI(MPI_Comm_size(comm, &size)); 5273799db056SMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(comm, &rank)); 5274799db056SMatthew G. Knepley PetscCall(DMGetNumDS(dm, &Nds)); 5275799db056SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 5276799db056SMatthew G. Knepley PetscDS dsBC; 5277799db056SMatthew G. Knepley PetscInt numBd; 5278799db056SMatthew G. Knepley 527907218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL)); 5280799db056SMatthew G. Knepley PetscCall(PetscDSGetNumBoundary(dsBC, &numBd)); 5281799db056SMatthew G. Knepley maxLabels += numBd; 5282799db056SMatthew G. Knepley } 5283799db056SMatthew G. Knepley PetscCall(PetscCalloc1(maxLabels, &labels)); 5284799db056SMatthew G. Knepley /* Get list of labels to be completed */ 5285799db056SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 5286799db056SMatthew G. Knepley PetscDS dsBC; 5287799db056SMatthew G. Knepley PetscInt numBd, bd; 5288799db056SMatthew G. Knepley 528907218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL)); 5290799db056SMatthew G. Knepley PetscCall(PetscDSGetNumBoundary(dsBC, &numBd)); 5291799db056SMatthew G. Knepley for (bd = 0; bd < numBd; ++bd) { 5292799db056SMatthew G. Knepley DMLabel label; 5293799db056SMatthew G. Knepley PetscInt field; 5294799db056SMatthew G. Knepley PetscObject obj; 5295799db056SMatthew G. Knepley PetscClassId id; 5296799db056SMatthew G. Knepley 5297799db056SMatthew G. Knepley PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL)); 52989566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, field, NULL, &obj)); 52999566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 5300799db056SMatthew G. Knepley if (!(id == PETSCFE_CLASSID) || !label) continue; 53019371c9d4SSatish Balay for (l = 0; l < Nl; ++l) 53029371c9d4SSatish Balay if (labels[l] == label) break; 5303799db056SMatthew G. Knepley if (l == Nl) labels[Nl++] = label; 5304783e2ec8SMatthew G. Knepley } 5305799db056SMatthew G. Knepley } 5306799db056SMatthew G. Knepley /* Get label names */ 5307799db056SMatthew G. Knepley PetscCall(PetscMalloc1(Nl, &names)); 5308799db056SMatthew G. Knepley for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l])); 53099371c9d4SSatish Balay for (l = 0; l < Nl; ++l) { 53109371c9d4SSatish Balay PetscCall(PetscStrlen(names[l], &len)); 53119371c9d4SSatish Balay maxLen = PetscMax(maxLen, (PetscInt)len + 2); 53129371c9d4SSatish Balay } 5313799db056SMatthew G. Knepley PetscCall(PetscFree(labels)); 5314712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm)); 5315799db056SMatthew G. Knepley PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames)); 5316c6a7a370SJeremy L Thompson for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen)); 5317799db056SMatthew G. Knepley PetscCall(PetscFree(names)); 5318799db056SMatthew G. Knepley /* Put all names on all processes */ 5319799db056SMatthew G. Knepley PetscCall(PetscCalloc2(size, &counts, size + 1, &displs)); 5320799db056SMatthew G. Knepley PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm)); 5321799db056SMatthew G. Knepley for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p]; 5322799db056SMatthew G. Knepley gNl = displs[size]; 53239371c9d4SSatish Balay for (p = 0; p < size; ++p) { 53249371c9d4SSatish Balay counts[p] *= gmaxLen; 53259371c9d4SSatish Balay displs[p] *= gmaxLen; 53269371c9d4SSatish Balay } 5327799db056SMatthew G. Knepley PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels)); 5328799db056SMatthew G. Knepley PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm)); 5329799db056SMatthew G. Knepley PetscCall(PetscFree2(counts, displs)); 5330799db056SMatthew G. Knepley PetscCall(PetscFree(sendNames)); 5331799db056SMatthew G. Knepley for (l = 0, gl = 0; l < gNl; ++l) { 5332799db056SMatthew G. Knepley PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl])); 5333799db056SMatthew G. Knepley PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank); 53349371c9d4SSatish Balay for (m = 0; m < gl; ++m) 53359371c9d4SSatish Balay if (glabels[m] == glabels[gl]) continue; 53369566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 5337799db056SMatthew G. Knepley PetscCall(DMPlexLabelComplete(plex, glabels[gl])); 53389566063dSJacob Faibussowitsch PetscCall(DMDestroy(&plex)); 5339799db056SMatthew G. Knepley ++gl; 5340783e2ec8SMatthew G. Knepley } 5341799db056SMatthew G. Knepley PetscCall(PetscFree2(recvNames, glabels)); 53423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5343783e2ec8SMatthew G. Knepley } 5344783e2ec8SMatthew G. Knepley 5345d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew) 5346d71ae5a4SJacob Faibussowitsch { 5347e5e52638SMatthew G. Knepley DMSpace *tmpd; 5348e5e52638SMatthew G. Knepley PetscInt Nds = dm->Nds, s; 5349e5e52638SMatthew G. Knepley 5350e5e52638SMatthew G. Knepley PetscFunctionBegin; 53513ba16761SJacob Faibussowitsch if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS); 53529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(NdsNew, &tmpd)); 5353e5e52638SMatthew G. Knepley for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s]; 53549371c9d4SSatish Balay for (s = Nds; s < NdsNew; ++s) { 53559371c9d4SSatish Balay tmpd[s].ds = NULL; 53569371c9d4SSatish Balay tmpd[s].label = NULL; 53579371c9d4SSatish Balay tmpd[s].fields = NULL; 53589371c9d4SSatish Balay } 53599566063dSJacob Faibussowitsch PetscCall(PetscFree(dm->probs)); 5360e5e52638SMatthew G. Knepley dm->Nds = NdsNew; 5361e5e52638SMatthew G. Knepley dm->probs = tmpd; 53623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5363e5e52638SMatthew G. Knepley } 5364e5e52638SMatthew G. Knepley 5365e5e52638SMatthew G. Knepley /*@ 536620f4b53cSBarry Smith DMGetNumDS - Get the number of discrete systems in the `DM` 5367e5e52638SMatthew G. Knepley 536820f4b53cSBarry Smith Not Collective 5369e5e52638SMatthew G. Knepley 5370e5e52638SMatthew G. Knepley Input Parameter: 537120f4b53cSBarry Smith . dm - The `DM` 5372e5e52638SMatthew G. Knepley 5373e5e52638SMatthew G. Knepley Output Parameter: 537420f4b53cSBarry Smith . Nds - The number of `PetscDS` objects 5375e5e52638SMatthew G. Knepley 5376e5e52638SMatthew G. Knepley Level: intermediate 5377e5e52638SMatthew G. Knepley 53781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()` 5379e5e52638SMatthew G. Knepley @*/ 5380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds) 5381d71ae5a4SJacob Faibussowitsch { 5382e5e52638SMatthew G. Knepley PetscFunctionBegin; 5383e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 53844f572ea9SToby Isaac PetscAssertPointer(Nds, 2); 5385e5e52638SMatthew G. Knepley *Nds = dm->Nds; 53863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5387e5e52638SMatthew G. Knepley } 5388e5e52638SMatthew G. Knepley 5389e5e52638SMatthew G. Knepley /*@ 539020f4b53cSBarry Smith DMClearDS - Remove all discrete systems from the `DM` 5391e5e52638SMatthew G. Knepley 539220f4b53cSBarry Smith Logically Collective 5393e5e52638SMatthew G. Knepley 5394e5e52638SMatthew G. Knepley Input Parameter: 539520f4b53cSBarry Smith . dm - The `DM` 5396e5e52638SMatthew G. Knepley 5397e5e52638SMatthew G. Knepley Level: intermediate 5398e5e52638SMatthew G. Knepley 53991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()` 5400e5e52638SMatthew G. Knepley @*/ 5401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm) 5402d71ae5a4SJacob Faibussowitsch { 5403e5e52638SMatthew G. Knepley PetscInt s; 5404e5e52638SMatthew G. Knepley 5405e5e52638SMatthew G. Knepley PetscFunctionBegin; 5406e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5407e5e52638SMatthew G. Knepley for (s = 0; s < dm->Nds; ++s) { 54089566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dm->probs[s].ds)); 540907218a29SMatthew G. Knepley PetscCall(PetscDSDestroy(&dm->probs[s].dsIn)); 54109566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&dm->probs[s].label)); 54119566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dm->probs[s].fields)); 5412e5e52638SMatthew G. Knepley } 54139566063dSJacob Faibussowitsch PetscCall(PetscFree(dm->probs)); 5414e5e52638SMatthew G. Knepley dm->probs = NULL; 5415e5e52638SMatthew G. Knepley dm->Nds = 0; 54163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5417e5e52638SMatthew G. Knepley } 5418e5e52638SMatthew G. Knepley 5419e5e52638SMatthew G. Knepley /*@ 542020f4b53cSBarry Smith DMGetDS - Get the default `PetscDS` 5421e5e52638SMatthew G. Knepley 542220f4b53cSBarry Smith Not Collective 5423e5e52638SMatthew G. Knepley 5424e5e52638SMatthew G. Knepley Input Parameter: 542520f4b53cSBarry Smith . dm - The `DM` 5426e5e52638SMatthew G. Knepley 5427e5e52638SMatthew G. Knepley Output Parameter: 542807218a29SMatthew G. Knepley . ds - The default `PetscDS` 5429e5e52638SMatthew G. Knepley 5430e5e52638SMatthew G. Knepley Level: intermediate 5431e5e52638SMatthew G. Knepley 54321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()` 5433e5e52638SMatthew G. Knepley @*/ 543407218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds) 5435d71ae5a4SJacob Faibussowitsch { 5436e5e52638SMatthew G. Knepley PetscFunctionBeginHot; 5437e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 54384f572ea9SToby Isaac PetscAssertPointer(ds, 2); 543907218a29SMatthew G. Knepley PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()"); 544007218a29SMatthew G. Knepley *ds = dm->probs[0].ds; 54413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5442e5e52638SMatthew G. Knepley } 5443e5e52638SMatthew G. Knepley 5444e5e52638SMatthew G. Knepley /*@ 544520f4b53cSBarry Smith DMGetCellDS - Get the `PetscDS` defined on a given cell 5446e5e52638SMatthew G. Knepley 544720f4b53cSBarry Smith Not Collective 5448e5e52638SMatthew G. Knepley 5449e5e52638SMatthew G. Knepley Input Parameters: 545020f4b53cSBarry Smith + dm - The `DM` 545120f4b53cSBarry Smith - point - Cell for the `PetscDS` 5452e5e52638SMatthew G. Knepley 545307218a29SMatthew G. Knepley Output Parameters: 545407218a29SMatthew G. Knepley + ds - The `PetscDS` defined on the given cell 545507218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds 5456e5e52638SMatthew G. Knepley 5457e5e52638SMatthew G. Knepley Level: developer 5458e5e52638SMatthew G. Knepley 54591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()` 5460e5e52638SMatthew G. Knepley @*/ 546107218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn) 5462d71ae5a4SJacob Faibussowitsch { 546307218a29SMatthew G. Knepley PetscDS dsDef = NULL; 5464e5e52638SMatthew G. Knepley PetscInt s; 5465e5e52638SMatthew G. Knepley 5466e5e52638SMatthew G. Knepley PetscFunctionBeginHot; 5467e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 54684f572ea9SToby Isaac if (ds) PetscAssertPointer(ds, 3); 54694f572ea9SToby Isaac if (dsIn) PetscAssertPointer(dsIn, 4); 547063a3b9bcSJacob Faibussowitsch PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point); 547107218a29SMatthew G. Knepley if (ds) *ds = NULL; 547207218a29SMatthew G. Knepley if (dsIn) *dsIn = NULL; 5473e5e52638SMatthew G. Knepley for (s = 0; s < dm->Nds; ++s) { 5474e5e52638SMatthew G. Knepley PetscInt val; 5475e5e52638SMatthew G. Knepley 54769371c9d4SSatish Balay if (!dm->probs[s].label) { 547707218a29SMatthew G. Knepley dsDef = dm->probs[s].ds; 54789371c9d4SSatish Balay } else { 54799566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val)); 54809371c9d4SSatish Balay if (val >= 0) { 548107218a29SMatthew G. Knepley if (ds) *ds = dm->probs[s].ds; 548207218a29SMatthew G. Knepley if (dsIn) *dsIn = dm->probs[s].dsIn; 54839371c9d4SSatish Balay break; 54849371c9d4SSatish Balay } 5485e5e52638SMatthew G. Knepley } 5486e5e52638SMatthew G. Knepley } 548707218a29SMatthew G. Knepley if (ds && !*ds) *ds = dsDef; 54883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5489e5e52638SMatthew G. Knepley } 5490e5e52638SMatthew G. Knepley 5491e5e52638SMatthew G. Knepley /*@ 549220f4b53cSBarry Smith DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel` 5493e5e52638SMatthew G. Knepley 549420f4b53cSBarry Smith Not Collective 5495e5e52638SMatthew G. Knepley 5496e5e52638SMatthew G. Knepley Input Parameters: 549720f4b53cSBarry Smith + dm - The `DM` 549820f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh 5499e5e52638SMatthew G. Knepley 5500b3cf3223SMatthew G. Knepley Output Parameters: 550120f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` 550207218a29SMatthew G. Knepley . ds - The `PetscDS` defined on the given region, or `NULL` 550307218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input in the given region, or `NULL` 5504e5e52638SMatthew G. Knepley 5505e5e52638SMatthew G. Knepley Level: advanced 5506e5e52638SMatthew G. Knepley 550720f4b53cSBarry Smith Note: 550820f4b53cSBarry Smith If a non-`NULL` label is given, but there is no `PetscDS` on that specific label, 550920f4b53cSBarry Smith the `PetscDS` for the full domain (if present) is returned. Returns with 551007218a29SMatthew G. Knepley fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain. 551120f4b53cSBarry Smith 55121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()` 5513e5e52638SMatthew G. Knepley @*/ 551407218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn) 5515d71ae5a4SJacob Faibussowitsch { 5516e5e52638SMatthew G. Knepley PetscInt Nds = dm->Nds, s; 5517e5e52638SMatthew G. Knepley 5518e5e52638SMatthew G. Knepley PetscFunctionBegin; 5519e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5520e5e52638SMatthew G. Knepley if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 55219371c9d4SSatish Balay if (fields) { 55224f572ea9SToby Isaac PetscAssertPointer(fields, 3); 55239371c9d4SSatish Balay *fields = NULL; 55249371c9d4SSatish Balay } 55259371c9d4SSatish Balay if (ds) { 55264f572ea9SToby Isaac PetscAssertPointer(ds, 4); 55279371c9d4SSatish Balay *ds = NULL; 55289371c9d4SSatish Balay } 552907218a29SMatthew G. Knepley if (dsIn) { 55304f572ea9SToby Isaac PetscAssertPointer(dsIn, 5); 553107218a29SMatthew G. Knepley *dsIn = NULL; 553207218a29SMatthew G. Knepley } 5533e5e52638SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 5534154ca461SJed Brown if (dm->probs[s].label == label || !dm->probs[s].label) { 5535b3cf3223SMatthew G. Knepley if (fields) *fields = dm->probs[s].fields; 5536b3cf3223SMatthew G. Knepley if (ds) *ds = dm->probs[s].ds; 553707218a29SMatthew G. Knepley if (dsIn) *dsIn = dm->probs[s].dsIn; 55383ba16761SJacob Faibussowitsch if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS); 5539b3cf3223SMatthew G. Knepley } 5540e5e52638SMatthew G. Knepley } 55413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5542e5e52638SMatthew G. Knepley } 5543e5e52638SMatthew G. Knepley 5544e5e52638SMatthew G. Knepley /*@ 5545bb7acecfSBarry Smith DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel` 5546083401c6SMatthew G. Knepley 554720f4b53cSBarry Smith Collective 5548083401c6SMatthew G. Knepley 5549083401c6SMatthew G. Knepley Input Parameters: 5550bb7acecfSBarry Smith + dm - The `DM` 555120f4b53cSBarry Smith . label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh 555207218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields 555307218a29SMatthew G. Knepley . ds - The `PetscDS` defined on the given region 555407218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS` 5555083401c6SMatthew G. Knepley 555620f4b53cSBarry Smith Level: advanced 555720f4b53cSBarry Smith 5558bb7acecfSBarry Smith Note: 5559bb7acecfSBarry Smith If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`. If the `PetscDS` is replaced, 5560083401c6SMatthew G. Knepley the fields argument is ignored. 5561083401c6SMatthew G. Knepley 55621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()` 5563083401c6SMatthew G. Knepley @*/ 556407218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn) 5565d71ae5a4SJacob Faibussowitsch { 5566083401c6SMatthew G. Knepley PetscInt Nds = dm->Nds, s; 5567083401c6SMatthew G. Knepley 5568083401c6SMatthew G. Knepley PetscFunctionBegin; 5569083401c6SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5570083401c6SMatthew G. Knepley if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 557107218a29SMatthew G. Knepley if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3); 5572064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4); 557307218a29SMatthew G. Knepley if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5); 5574083401c6SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 5575083401c6SMatthew G. Knepley if (dm->probs[s].label == label) { 55769566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dm->probs[s].ds)); 557707218a29SMatthew G. Knepley PetscCall(PetscDSDestroy(&dm->probs[s].dsIn)); 5578083401c6SMatthew G. Knepley dm->probs[s].ds = ds; 557907218a29SMatthew G. Knepley dm->probs[s].dsIn = dsIn; 55803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5581083401c6SMatthew G. Knepley } 5582083401c6SMatthew G. Knepley } 55839566063dSJacob Faibussowitsch PetscCall(DMDSEnlarge_Static(dm, Nds + 1)); 55849566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 55859566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)fields)); 55869566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ds)); 558707218a29SMatthew G. Knepley PetscCall(PetscObjectReference((PetscObject)dsIn)); 5588083401c6SMatthew G. Knepley if (!label) { 5589083401c6SMatthew G. Knepley /* Put the NULL label at the front, so it is returned as the default */ 5590083401c6SMatthew G. Knepley for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s]; 5591083401c6SMatthew G. Knepley Nds = 0; 5592083401c6SMatthew G. Knepley } 5593083401c6SMatthew G. Knepley dm->probs[Nds].label = label; 5594083401c6SMatthew G. Knepley dm->probs[Nds].fields = fields; 5595083401c6SMatthew G. Knepley dm->probs[Nds].ds = ds; 559607218a29SMatthew G. Knepley dm->probs[Nds].dsIn = dsIn; 55973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5598083401c6SMatthew G. Knepley } 5599083401c6SMatthew G. Knepley 5600083401c6SMatthew G. Knepley /*@ 560120f4b53cSBarry Smith DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number 5602e5e52638SMatthew G. Knepley 560320f4b53cSBarry Smith Not Collective 5604e5e52638SMatthew G. Knepley 5605e5e52638SMatthew G. Knepley Input Parameters: 560620f4b53cSBarry Smith + dm - The `DM` 5607e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds) 5608e5e52638SMatthew G. Knepley 5609e5e52638SMatthew G. Knepley Output Parameters: 561020f4b53cSBarry Smith + label - The region label, or `NULL` 561120f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` 561207218a29SMatthew G. Knepley . ds - The `PetscDS` defined on the given region, or `NULL` 561307218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input in the given region, or `NULL` 5614e5e52638SMatthew G. Knepley 5615e5e52638SMatthew G. Knepley Level: advanced 5616e5e52638SMatthew G. Knepley 56171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()` 5618e5e52638SMatthew G. Knepley @*/ 561907218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn) 5620d71ae5a4SJacob Faibussowitsch { 5621e5e52638SMatthew G. Knepley PetscInt Nds; 5622e5e52638SMatthew G. Knepley 5623e5e52638SMatthew G. Knepley PetscFunctionBegin; 5624e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 56259566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 562663a3b9bcSJacob Faibussowitsch PetscCheck((num >= 0) && (num < Nds), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %" PetscInt_FMT " is not in [0, %" PetscInt_FMT ")", num, Nds); 5627e5e52638SMatthew G. Knepley if (label) { 56284f572ea9SToby Isaac PetscAssertPointer(label, 3); 5629e5e52638SMatthew G. Knepley *label = dm->probs[num].label; 5630e5e52638SMatthew G. Knepley } 5631b3cf3223SMatthew G. Knepley if (fields) { 56324f572ea9SToby Isaac PetscAssertPointer(fields, 4); 5633b3cf3223SMatthew G. Knepley *fields = dm->probs[num].fields; 5634b3cf3223SMatthew G. Knepley } 5635e5e52638SMatthew G. Knepley if (ds) { 56364f572ea9SToby Isaac PetscAssertPointer(ds, 5); 5637e5e52638SMatthew G. Knepley *ds = dm->probs[num].ds; 5638e5e52638SMatthew G. Knepley } 563907218a29SMatthew G. Knepley if (dsIn) { 56404f572ea9SToby Isaac PetscAssertPointer(dsIn, 6); 564107218a29SMatthew G. Knepley *dsIn = dm->probs[num].dsIn; 564207218a29SMatthew G. Knepley } 56433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5644e5e52638SMatthew G. Knepley } 5645e5e52638SMatthew G. Knepley 5646e5e52638SMatthew G. Knepley /*@ 564720f4b53cSBarry Smith DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number 5648e5e52638SMatthew G. Knepley 564920f4b53cSBarry Smith Not Collective 5650e5e52638SMatthew G. Knepley 5651e5e52638SMatthew G. Knepley Input Parameters: 565220f4b53cSBarry Smith + dm - The `DM` 5653083401c6SMatthew G. Knepley . num - The region number, in [0, Nds) 565420f4b53cSBarry Smith . label - The region label, or `NULL` 565507218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting 565607218a29SMatthew G. Knepley . ds - The `PetscDS` defined on the given region, or `NULL` to prevent setting 565707218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS` 5658e5e52638SMatthew G. Knepley 5659e5e52638SMatthew G. Knepley Level: advanced 5660e5e52638SMatthew G. Knepley 56611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()` 5662e5e52638SMatthew G. Knepley @*/ 566307218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn) 5664d71ae5a4SJacob Faibussowitsch { 5665083401c6SMatthew G. Knepley PetscInt Nds; 5666e5e52638SMatthew G. Knepley 5667e5e52638SMatthew G. Knepley PetscFunctionBegin; 5668e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 5669ad540459SPierre Jolivet if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3); 56709566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 567163a3b9bcSJacob Faibussowitsch PetscCheck((num >= 0) && (num < Nds), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %" PetscInt_FMT " is not in [0, %" PetscInt_FMT ")", num, Nds); 56729566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 56739566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&dm->probs[num].label)); 5674083401c6SMatthew G. Knepley dm->probs[num].label = label; 5675083401c6SMatthew G. Knepley if (fields) { 5676083401c6SMatthew G. Knepley PetscValidHeaderSpecific(fields, IS_CLASSID, 4); 56779566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)fields)); 56789566063dSJacob Faibussowitsch PetscCall(ISDestroy(&dm->probs[num].fields)); 5679083401c6SMatthew G. Knepley dm->probs[num].fields = fields; 5680e5e52638SMatthew G. Knepley } 5681083401c6SMatthew G. Knepley if (ds) { 5682083401c6SMatthew G. Knepley PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5); 56839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ds)); 56849566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dm->probs[num].ds)); 5685083401c6SMatthew G. Knepley dm->probs[num].ds = ds; 5686083401c6SMatthew G. Knepley } 568707218a29SMatthew G. Knepley if (dsIn) { 568807218a29SMatthew G. Knepley PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6); 568907218a29SMatthew G. Knepley PetscCall(PetscObjectReference((PetscObject)dsIn)); 569007218a29SMatthew G. Knepley PetscCall(PetscDSDestroy(&dm->probs[num].dsIn)); 569107218a29SMatthew G. Knepley dm->probs[num].dsIn = dsIn; 569207218a29SMatthew G. Knepley } 56933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5694e5e52638SMatthew G. Knepley } 5695e5e52638SMatthew G. Knepley 5696e5e52638SMatthew G. Knepley /*@ 569720f4b53cSBarry Smith DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found. 56981d3af9e0SMatthew G. Knepley 569920f4b53cSBarry Smith Not Collective 57001d3af9e0SMatthew G. Knepley 57011d3af9e0SMatthew G. Knepley Input Parameters: 570220f4b53cSBarry Smith + dm - The `DM` 570320f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region 57041d3af9e0SMatthew G. Knepley 57051d3af9e0SMatthew G. Knepley Output Parameter: 57061d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found 57071d3af9e0SMatthew G. Knepley 57081d3af9e0SMatthew G. Knepley Level: advanced 57091d3af9e0SMatthew G. Knepley 57101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()` 57111d3af9e0SMatthew G. Knepley @*/ 5712d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num) 5713d71ae5a4SJacob Faibussowitsch { 57141d3af9e0SMatthew G. Knepley PetscInt Nds, n; 57151d3af9e0SMatthew G. Knepley 57161d3af9e0SMatthew G. Knepley PetscFunctionBegin; 57171d3af9e0SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 57181d3af9e0SMatthew G. Knepley PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2); 57194f572ea9SToby Isaac PetscAssertPointer(num, 3); 57209566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 57219371c9d4SSatish Balay for (n = 0; n < Nds; ++n) 57229371c9d4SSatish Balay if (ds == dm->probs[n].ds) break; 57231d3af9e0SMatthew G. Knepley if (n >= Nds) *num = -1; 57241d3af9e0SMatthew G. Knepley else *num = n; 57253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57261d3af9e0SMatthew G. Knepley } 57271d3af9e0SMatthew G. Knepley 57282df84da0SMatthew G. Knepley /*@C 5729bb7acecfSBarry Smith DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh 57302df84da0SMatthew G. Knepley 573120f4b53cSBarry Smith Not Collective 57322df84da0SMatthew G. Knepley 5733f1a722f8SMatthew G. Knepley Input Parameters: 5734bb7acecfSBarry Smith + dm - The `DM` 57352df84da0SMatthew G. Knepley . Nc - The number of components for the field 573620f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL` 5737bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree 57382df84da0SMatthew G. Knepley 57392df84da0SMatthew G. Knepley Output Parameter: 5740bb7acecfSBarry Smith . fem - The `PetscFE` 57412df84da0SMatthew G. Knepley 574220f4b53cSBarry Smith Level: intermediate 574320f4b53cSBarry Smith 5744bb7acecfSBarry Smith Note: 5745bb7acecfSBarry Smith This is a convenience method that just calls `PetscFECreateByCell()` underneath. 57462df84da0SMatthew G. Knepley 57471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()` 57482df84da0SMatthew G. Knepley @*/ 5749d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem) 5750d71ae5a4SJacob Faibussowitsch { 57512df84da0SMatthew G. Knepley DMPolytopeType ct; 57522df84da0SMatthew G. Knepley PetscInt dim, cStart; 57532df84da0SMatthew G. Knepley 57542df84da0SMatthew G. Knepley PetscFunctionBegin; 57552df84da0SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 57562df84da0SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, Nc, 2); 57574f572ea9SToby Isaac if (prefix) PetscAssertPointer(prefix, 3); 57582df84da0SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, qorder, 4); 57594f572ea9SToby Isaac PetscAssertPointer(fem, 5); 57609566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 57619566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL)); 57629566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 57639566063dSJacob Faibussowitsch PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem)); 57643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57652df84da0SMatthew G. Knepley } 57662df84da0SMatthew G. Knepley 57671d3af9e0SMatthew G. Knepley /*@ 5768bb7acecfSBarry Smith DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM` 5769e5e52638SMatthew G. Knepley 577020f4b53cSBarry Smith Collective 5771e5e52638SMatthew G. Knepley 5772e5e52638SMatthew G. Knepley Input Parameter: 5773bb7acecfSBarry Smith . dm - The `DM` 5774e5e52638SMatthew G. Knepley 577520f4b53cSBarry Smith Options Database Key: 5776bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM` 577745480ffeSMatthew G. Knepley 577820f4b53cSBarry Smith Level: intermediate 577920f4b53cSBarry Smith 57801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()` 5781e5e52638SMatthew G. Knepley @*/ 5782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm) 5783d71ae5a4SJacob Faibussowitsch { 5784e5e52638SMatthew G. Knepley MPI_Comm comm; 5785083401c6SMatthew G. Knepley PetscDS dsDef; 5786083401c6SMatthew G. Knepley DMLabel *labelSet; 5787f9244615SMatthew G. Knepley PetscInt dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k; 5788f9244615SMatthew G. Knepley PetscBool doSetup = PETSC_TRUE, flg; 5789e5e52638SMatthew G. Knepley 5790e5e52638SMatthew G. Knepley PetscFunctionBegin; 5791e5e52638SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 57923ba16761SJacob Faibussowitsch if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS); 57939566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 57949566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 5795083401c6SMatthew G. Knepley /* Determine how many regions we have */ 57969566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Nf, &labelSet)); 5797083401c6SMatthew G. Knepley Nl = 0; 5798083401c6SMatthew G. Knepley Ndef = 0; 5799083401c6SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 5800083401c6SMatthew G. Knepley DMLabel label = dm->fields[f].label; 5801083401c6SMatthew G. Knepley PetscInt l; 5802083401c6SMatthew G. Knepley 5803f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED 5804f918ec44SMatthew G. Knepley /* Move CEED context to discretizations */ 5805f918ec44SMatthew G. Knepley { 5806f918ec44SMatthew G. Knepley PetscClassId id; 5807f918ec44SMatthew G. Knepley 58089566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id)); 5809f918ec44SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 5810f918ec44SMatthew G. Knepley Ceed ceed; 5811f918ec44SMatthew G. Knepley 58129566063dSJacob Faibussowitsch PetscCall(DMGetCeed(dm, &ceed)); 58139566063dSJacob Faibussowitsch PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed)); 5814f918ec44SMatthew G. Knepley } 5815f918ec44SMatthew G. Knepley } 5816f918ec44SMatthew G. Knepley #endif 58179371c9d4SSatish Balay if (!label) { 58189371c9d4SSatish Balay ++Ndef; 58199371c9d4SSatish Balay continue; 58209371c9d4SSatish Balay } 58219371c9d4SSatish Balay for (l = 0; l < Nl; ++l) 58229371c9d4SSatish Balay if (label == labelSet[l]) break; 5823083401c6SMatthew G. Knepley if (l < Nl) continue; 5824083401c6SMatthew G. Knepley labelSet[Nl++] = label; 5825083401c6SMatthew G. Knepley } 5826083401c6SMatthew G. Knepley /* Create default DS if there are no labels to intersect with */ 582707218a29SMatthew G. Knepley PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL)); 5828083401c6SMatthew G. Knepley if (!dsDef && Ndef && !Nl) { 5829b3cf3223SMatthew G. Knepley IS fields; 5830b3cf3223SMatthew G. Knepley PetscInt *fld, nf; 5831b3cf3223SMatthew G. Knepley 58329371c9d4SSatish Balay for (f = 0, nf = 0; f < Nf; ++f) 58339371c9d4SSatish Balay if (!dm->fields[f].label) ++nf; 58347a8be351SBarry Smith PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS"); 58359566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nf, &fld)); 58369371c9d4SSatish Balay for (f = 0, nf = 0; f < Nf; ++f) 58379371c9d4SSatish Balay if (!dm->fields[f].label) fld[nf++] = f; 58389566063dSJacob Faibussowitsch PetscCall(ISCreate(PETSC_COMM_SELF, &fields)); 58399566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_")); 58409566063dSJacob Faibussowitsch PetscCall(ISSetType(fields, ISGENERAL)); 58419566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER)); 584288f0c812SMatthew G. Knepley 58439566063dSJacob Faibussowitsch PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef)); 584407218a29SMatthew G. Knepley PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL)); 58459566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dsDef)); 58469566063dSJacob Faibussowitsch PetscCall(ISDestroy(&fields)); 58472df9ee95SMatthew G. Knepley } 584807218a29SMatthew G. Knepley PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL)); 58499566063dSJacob Faibussowitsch if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE)); 5850083401c6SMatthew G. Knepley /* Intersect labels with default fields */ 5851083401c6SMatthew G. Knepley if (Ndef && Nl) { 58520122748bSMatthew G. Knepley DM plex; 5853083401c6SMatthew G. Knepley DMLabel cellLabel; 5854083401c6SMatthew G. Knepley IS fieldIS, allcellIS, defcellIS = NULL; 5855083401c6SMatthew G. Knepley PetscInt *fields; 5856083401c6SMatthew G. Knepley const PetscInt *cells; 5857083401c6SMatthew G. Knepley PetscInt depth, nf = 0, n, c; 58580122748bSMatthew G. Knepley 58599566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 58609566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(plex, &depth)); 58619566063dSJacob Faibussowitsch PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS)); 58629566063dSJacob Faibussowitsch if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS)); 58635fedec97SMatthew G. Knepley /* TODO This looks like it only works for one label */ 5864083401c6SMatthew G. Knepley for (l = 0; l < Nl; ++l) { 5865083401c6SMatthew G. Knepley DMLabel label = labelSet[l]; 5866083401c6SMatthew G. Knepley IS pointIS; 5867083401c6SMatthew G. Knepley 58689566063dSJacob Faibussowitsch PetscCall(ISDestroy(&defcellIS)); 58699566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, 1, &pointIS)); 58709566063dSJacob Faibussowitsch PetscCall(ISDifference(allcellIS, pointIS, &defcellIS)); 58719566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 5872083401c6SMatthew G. Knepley } 58739566063dSJacob Faibussowitsch PetscCall(ISDestroy(&allcellIS)); 5874083401c6SMatthew G. Knepley 58759566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel)); 58769566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(defcellIS, &n)); 58779566063dSJacob Faibussowitsch PetscCall(ISGetIndices(defcellIS, &cells)); 58789566063dSJacob Faibussowitsch for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1)); 58799566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(defcellIS, &cells)); 58809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&defcellIS)); 58819566063dSJacob Faibussowitsch PetscCall(DMPlexLabelComplete(plex, cellLabel)); 5882083401c6SMatthew G. Knepley 58839566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ndef, &fields)); 58849371c9d4SSatish Balay for (f = 0; f < Nf; ++f) 58859371c9d4SSatish Balay if (!dm->fields[f].label) fields[nf++] = f; 58869566063dSJacob Faibussowitsch PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS)); 58879566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_")); 58889566063dSJacob Faibussowitsch PetscCall(ISSetType(fieldIS, ISGENERAL)); 58899566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER)); 5890083401c6SMatthew G. Knepley 58919566063dSJacob Faibussowitsch PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef)); 589207218a29SMatthew G. Knepley PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL)); 58939566063dSJacob Faibussowitsch PetscCall(PetscDSSetCoordinateDimension(dsDef, dE)); 58949566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&cellLabel)); 58959566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dsDef)); 58969566063dSJacob Faibussowitsch PetscCall(ISDestroy(&fieldIS)); 58979566063dSJacob Faibussowitsch PetscCall(DMDestroy(&plex)); 5898083401c6SMatthew G. Knepley } 5899083401c6SMatthew G. Knepley /* Create label DSes 5900083401c6SMatthew G. Knepley - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS 5901083401c6SMatthew G. Knepley */ 5902083401c6SMatthew G. Knepley /* TODO Should check that labels are disjoint */ 5903083401c6SMatthew G. Knepley for (l = 0; l < Nl; ++l) { 5904083401c6SMatthew G. Knepley DMLabel label = labelSet[l]; 590507218a29SMatthew G. Knepley PetscDS ds, dsIn = NULL; 5906083401c6SMatthew G. Knepley IS fields; 5907083401c6SMatthew G. Knepley PetscInt *fld, nf; 5908083401c6SMatthew G. Knepley 59099566063dSJacob Faibussowitsch PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds)); 59109371c9d4SSatish Balay for (f = 0, nf = 0; f < Nf; ++f) 59119371c9d4SSatish Balay if (label == dm->fields[f].label || !dm->fields[f].label) ++nf; 59129566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(nf, &fld)); 59139371c9d4SSatish Balay for (f = 0, nf = 0; f < Nf; ++f) 59149371c9d4SSatish Balay if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f; 59159566063dSJacob Faibussowitsch PetscCall(ISCreate(PETSC_COMM_SELF, &fields)); 59169566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_")); 59179566063dSJacob Faibussowitsch PetscCall(ISSetType(fields, ISGENERAL)); 59189566063dSJacob Faibussowitsch PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER)); 59199566063dSJacob Faibussowitsch PetscCall(PetscDSSetCoordinateDimension(ds, dE)); 5920083401c6SMatthew G. Knepley { 5921083401c6SMatthew G. Knepley DMPolytopeType ct; 5922083401c6SMatthew G. Knepley PetscInt lStart, lEnd; 59235fedec97SMatthew G. Knepley PetscBool isCohesiveLocal = PETSC_FALSE, isCohesive; 59240122748bSMatthew G. Knepley 59259566063dSJacob Faibussowitsch PetscCall(DMLabelGetBounds(label, &lStart, &lEnd)); 5926665f567fSMatthew G. Knepley if (lStart >= 0) { 59279566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, lStart, &ct)); 5928412e9a14SMatthew G. Knepley switch (ct) { 5929412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 5930412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 5931412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 5932d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUAD_PRISM_TENSOR: 5933d71ae5a4SJacob Faibussowitsch isCohesiveLocal = PETSC_TRUE; 5934d71ae5a4SJacob Faibussowitsch break; 5935d71ae5a4SJacob Faibussowitsch default: 5936d71ae5a4SJacob Faibussowitsch break; 5937412e9a14SMatthew G. Knepley } 5938665f567fSMatthew G. Knepley } 5939712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm)); 594007218a29SMatthew G. Knepley if (isCohesive) { 594107218a29SMatthew G. Knepley PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn)); 594207218a29SMatthew G. Knepley PetscCall(PetscDSSetCoordinateDimension(dsIn, dE)); 594307218a29SMatthew G. Knepley } 59445fedec97SMatthew G. Knepley for (f = 0, nf = 0; f < Nf; ++f) { 59455fedec97SMatthew G. Knepley if (label == dm->fields[f].label || !dm->fields[f].label) { 59465fedec97SMatthew G. Knepley if (label == dm->fields[f].label) { 59479566063dSJacob Faibussowitsch PetscCall(PetscDSSetDiscretization(ds, nf, NULL)); 59489566063dSJacob Faibussowitsch PetscCall(PetscDSSetCohesive(ds, nf, isCohesive)); 594907218a29SMatthew G. Knepley if (dsIn) { 595007218a29SMatthew G. Knepley PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL)); 595107218a29SMatthew G. Knepley PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive)); 595207218a29SMatthew G. Knepley } 59535fedec97SMatthew G. Knepley } 59545fedec97SMatthew G. Knepley ++nf; 59555fedec97SMatthew G. Knepley } 59565fedec97SMatthew G. Knepley } 5957e5e52638SMatthew G. Knepley } 595807218a29SMatthew G. Knepley PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn)); 595907218a29SMatthew G. Knepley PetscCall(ISDestroy(&fields)); 59609566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&ds)); 596107218a29SMatthew G. Knepley PetscCall(PetscDSDestroy(&dsIn)); 5962e5e52638SMatthew G. Knepley } 59639566063dSJacob Faibussowitsch PetscCall(PetscFree(labelSet)); 5964e5e52638SMatthew G. Knepley /* Set fields in DSes */ 5965083401c6SMatthew G. Knepley for (s = 0; s < dm->Nds; ++s) { 5966083401c6SMatthew G. Knepley PetscDS ds = dm->probs[s].ds; 596707218a29SMatthew G. Knepley PetscDS dsIn = dm->probs[s].dsIn; 5968083401c6SMatthew G. Knepley IS fields = dm->probs[s].fields; 5969083401c6SMatthew G. Knepley const PetscInt *fld; 59705fedec97SMatthew G. Knepley PetscInt nf, dsnf; 59715fedec97SMatthew G. Knepley PetscBool isCohesive; 5972e5e52638SMatthew G. Knepley 59739566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(ds, &dsnf)); 59749566063dSJacob Faibussowitsch PetscCall(PetscDSIsCohesive(ds, &isCohesive)); 59759566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(fields, &nf)); 59769566063dSJacob Faibussowitsch PetscCall(ISGetIndices(fields, &fld)); 5977083401c6SMatthew G. Knepley for (f = 0; f < nf; ++f) { 5978083401c6SMatthew G. Knepley PetscObject disc = dm->fields[fld[f]].disc; 59795fedec97SMatthew G. Knepley PetscBool isCohesiveField; 5980e5e52638SMatthew G. Knepley PetscClassId id; 5981e5e52638SMatthew G. Knepley 59825fedec97SMatthew G. Knepley /* Handle DS with no fields */ 59839566063dSJacob Faibussowitsch if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField)); 59845fedec97SMatthew G. Knepley /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */ 598507218a29SMatthew G. Knepley if (isCohesive) { 598607218a29SMatthew G. Knepley if (!isCohesiveField) { 598707218a29SMatthew G. Knepley PetscObject bdDisc; 598807218a29SMatthew G. Knepley 598907218a29SMatthew G. Knepley PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc)); 599007218a29SMatthew G. Knepley PetscCall(PetscDSSetDiscretization(ds, f, bdDisc)); 599107218a29SMatthew G. Knepley PetscCall(PetscDSSetDiscretization(dsIn, f, disc)); 599207218a29SMatthew G. Knepley } else { 59939566063dSJacob Faibussowitsch PetscCall(PetscDSSetDiscretization(ds, f, disc)); 599407218a29SMatthew G. Knepley PetscCall(PetscDSSetDiscretization(dsIn, f, disc)); 599507218a29SMatthew G. Knepley } 599607218a29SMatthew G. Knepley } else { 599707218a29SMatthew G. Knepley PetscCall(PetscDSSetDiscretization(ds, f, disc)); 599807218a29SMatthew G. Knepley } 5999083401c6SMatthew G. Knepley /* We allow people to have placeholder fields and construct the Section by hand */ 60009566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(disc, &id)); 6001e5e52638SMatthew G. Knepley if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE; 6002e5e52638SMatthew G. Knepley } 60039566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(fields, &fld)); 6004e5e52638SMatthew G. Knepley } 6005f9244615SMatthew G. Knepley /* Allow k-jet tabulation */ 60069566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg)); 6007f9244615SMatthew G. Knepley if (flg) { 60083b4aee56SMatthew G. Knepley for (s = 0; s < dm->Nds; ++s) { 60093b4aee56SMatthew G. Knepley PetscDS ds = dm->probs[s].ds; 601007218a29SMatthew G. Knepley PetscDS dsIn = dm->probs[s].dsIn; 60113b4aee56SMatthew G. Knepley PetscInt Nf, f; 60123b4aee56SMatthew G. Knepley 60139566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(ds, &Nf)); 601407218a29SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 601507218a29SMatthew G. Knepley PetscCall(PetscDSSetJetDegree(ds, f, k)); 601607218a29SMatthew G. Knepley if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k)); 601707218a29SMatthew G. Knepley } 60183b4aee56SMatthew G. Knepley } 6019f9244615SMatthew G. Knepley } 6020e5e52638SMatthew G. Knepley /* Setup DSes */ 6021e5e52638SMatthew G. Knepley if (doSetup) { 602207218a29SMatthew G. Knepley for (s = 0; s < dm->Nds; ++s) { 60233969c466SMatthew G. Knepley if (dm->setfromoptionscalled) { 60243969c466SMatthew G. Knepley PetscCall(PetscDSSetFromOptions(dm->probs[s].ds)); 60253969c466SMatthew G. Knepley if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn)); 60263969c466SMatthew G. Knepley } 602707218a29SMatthew G. Knepley PetscCall(PetscDSSetUp(dm->probs[s].ds)); 602807218a29SMatthew G. Knepley if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn)); 602907218a29SMatthew G. Knepley } 6030e5e52638SMatthew G. Knepley } 60313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6032e5e52638SMatthew G. Knepley } 6033e5e52638SMatthew G. Knepley 6034e5e52638SMatthew G. Knepley /*@ 6035d2b2dc1eSMatthew G. Knepley DMUseTensorOrder - Use a tensor product closure ordering for the default section 6036d2b2dc1eSMatthew G. Knepley 6037d2b2dc1eSMatthew G. Knepley Input Parameters: 6038d2b2dc1eSMatthew G. Knepley + dm - The DM 6039d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order 6040d2b2dc1eSMatthew G. Knepley 6041d2b2dc1eSMatthew G. Knepley Level: developer 6042d2b2dc1eSMatthew G. Knepley 6043d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()` 6044d2b2dc1eSMatthew G. Knepley @*/ 6045d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor) 6046d2b2dc1eSMatthew G. Knepley { 6047d2b2dc1eSMatthew G. Knepley PetscInt Nf; 6048d2b2dc1eSMatthew G. Knepley PetscBool reorder = PETSC_TRUE, isPlex; 6049d2b2dc1eSMatthew G. Knepley 6050d2b2dc1eSMatthew G. Knepley PetscFunctionBegin; 6051d2b2dc1eSMatthew G. Knepley PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex)); 6052d2b2dc1eSMatthew G. Knepley PetscCall(DMGetNumFields(dm, &Nf)); 6053d2b2dc1eSMatthew G. Knepley for (PetscInt f = 0; f < Nf; ++f) { 6054d2b2dc1eSMatthew G. Knepley PetscObject obj; 6055d2b2dc1eSMatthew G. Knepley PetscClassId id; 6056d2b2dc1eSMatthew G. Knepley 6057d2b2dc1eSMatthew G. Knepley PetscCall(DMGetField(dm, f, NULL, &obj)); 6058d2b2dc1eSMatthew G. Knepley PetscCall(PetscObjectGetClassId(obj, &id)); 6059d2b2dc1eSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 6060d2b2dc1eSMatthew G. Knepley PetscSpace sp; 6061d2b2dc1eSMatthew G. Knepley PetscBool tensor; 6062d2b2dc1eSMatthew G. Knepley 6063d2b2dc1eSMatthew G. Knepley PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp)); 6064d2b2dc1eSMatthew G. Knepley PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor)); 6065d2b2dc1eSMatthew G. Knepley reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE; 6066d2b2dc1eSMatthew G. Knepley } else reorder = PETSC_FALSE; 6067d2b2dc1eSMatthew G. Knepley } 6068d2b2dc1eSMatthew G. Knepley if (tensor) { 6069d2b2dc1eSMatthew G. Knepley if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL)); 6070d2b2dc1eSMatthew G. Knepley } else { 6071d2b2dc1eSMatthew G. Knepley PetscSection s; 6072d2b2dc1eSMatthew G. Knepley 6073d2b2dc1eSMatthew G. Knepley PetscCall(DMGetLocalSection(dm, &s)); 6074d2b2dc1eSMatthew G. Knepley if (s) PetscCall(PetscSectionResetClosurePermutation(s)); 6075d2b2dc1eSMatthew G. Knepley } 6076d2b2dc1eSMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 6077d2b2dc1eSMatthew G. Knepley } 6078d2b2dc1eSMatthew G. Knepley 6079d2b2dc1eSMatthew G. Knepley /*@ 6080bb7acecfSBarry Smith DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information. 60817f96f943SMatthew G. Knepley 608220f4b53cSBarry Smith Collective 6083f2cacb80SMatthew G. Knepley 60847f96f943SMatthew G. Knepley Input Parameters: 6085bb7acecfSBarry Smith + dm - The `DM` 60867f96f943SMatthew G. Knepley - time - The time 60877f96f943SMatthew G. Knepley 60887f96f943SMatthew G. Knepley Output Parameters: 608920f4b53cSBarry Smith + u - The vector will be filled with exact solution values, or `NULL` 609020f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL` 609120f4b53cSBarry Smith 609220f4b53cSBarry Smith Level: developer 60937f96f943SMatthew G. Knepley 6094bb7acecfSBarry Smith Note: 6095bb7acecfSBarry Smith The user must call `PetscDSSetExactSolution()` before using this routine 60967f96f943SMatthew G. Knepley 60971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()` 60987f96f943SMatthew G. Knepley @*/ 6099d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t) 6100d71ae5a4SJacob Faibussowitsch { 61017f96f943SMatthew G. Knepley PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx); 61027f96f943SMatthew G. Knepley void **ectxs; 6103f60fa741SMatthew G. Knepley Vec locu, locu_t; 61047f96f943SMatthew G. Knepley PetscInt Nf, Nds, s; 61057f96f943SMatthew G. Knepley 61067f96f943SMatthew G. Knepley PetscFunctionBegin; 6107f2cacb80SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 6108f60fa741SMatthew G. Knepley if (u) { 6109f60fa741SMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 3); 6110f60fa741SMatthew G. Knepley PetscCall(DMGetLocalVector(dm, &locu)); 6111f60fa741SMatthew G. Knepley PetscCall(VecSet(locu, 0.)); 6112f60fa741SMatthew G. Knepley } 6113f60fa741SMatthew G. Knepley if (u_t) { 6114f60fa741SMatthew G. Knepley PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4); 6115f60fa741SMatthew G. Knepley PetscCall(DMGetLocalVector(dm, &locu_t)); 6116f60fa741SMatthew G. Knepley PetscCall(VecSet(locu_t, 0.)); 6117f60fa741SMatthew G. Knepley } 61189566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 61199566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs)); 61209566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 61217f96f943SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 61227f96f943SMatthew G. Knepley PetscDS ds; 61237f96f943SMatthew G. Knepley DMLabel label; 61247f96f943SMatthew G. Knepley IS fieldIS; 61257f96f943SMatthew G. Knepley const PetscInt *fields, id = 1; 61267f96f943SMatthew G. Knepley PetscInt dsNf, f; 61277f96f943SMatthew G. Knepley 612807218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL)); 61299566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(ds, &dsNf)); 61309566063dSJacob Faibussowitsch PetscCall(ISGetIndices(fieldIS, &fields)); 61319566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(exacts, Nf)); 61329566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(ectxs, Nf)); 6133f2cacb80SMatthew G. Knepley if (u) { 6134f60fa741SMatthew G. Knepley for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]])); 6135f60fa741SMatthew G. Knepley if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu)); 6136f60fa741SMatthew G. Knepley else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu)); 61377f96f943SMatthew G. Knepley } 6138f2cacb80SMatthew G. Knepley if (u_t) { 61399566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(exacts, Nf)); 61409566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(ectxs, Nf)); 6141f60fa741SMatthew G. Knepley for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]])); 6142f60fa741SMatthew G. Knepley if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t)); 6143f60fa741SMatthew G. Knepley else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t)); 6144f2cacb80SMatthew G. Knepley } 61459566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(fieldIS, &fields)); 6146f2cacb80SMatthew G. Knepley } 6147f2cacb80SMatthew G. Knepley if (u) { 61489566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution")); 61499566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_")); 6150f2cacb80SMatthew G. Knepley } 6151f2cacb80SMatthew G. Knepley if (u_t) { 61529566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative")); 61539566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_")); 6154f2cacb80SMatthew G. Knepley } 61559566063dSJacob Faibussowitsch PetscCall(PetscFree2(exacts, ectxs)); 6156f60fa741SMatthew G. Knepley if (u) { 6157f60fa741SMatthew G. Knepley PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u)); 6158f60fa741SMatthew G. Knepley PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u)); 6159f60fa741SMatthew G. Knepley PetscCall(DMRestoreLocalVector(dm, &locu)); 6160f60fa741SMatthew G. Knepley } 6161f60fa741SMatthew G. Knepley if (u_t) { 6162f60fa741SMatthew G. Knepley PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t)); 6163f60fa741SMatthew G. Knepley PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t)); 6164f60fa741SMatthew G. Knepley PetscCall(DMRestoreLocalVector(dm, &locu_t)); 6165f60fa741SMatthew G. Knepley } 61663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 61677f96f943SMatthew G. Knepley } 61687f96f943SMatthew G. Knepley 616907218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn) 6170d71ae5a4SJacob Faibussowitsch { 617107218a29SMatthew G. Knepley PetscDS dsNew, dsInNew = NULL; 617245480ffeSMatthew G. Knepley 617345480ffeSMatthew G. Knepley PetscFunctionBegin; 61749566063dSJacob Faibussowitsch PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew)); 617507218a29SMatthew G. Knepley PetscCall(PetscDSCopy(ds, dm, dsNew)); 617607218a29SMatthew G. Knepley if (dsIn) { 617707218a29SMatthew G. Knepley PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew)); 617807218a29SMatthew G. Knepley PetscCall(PetscDSCopy(dsIn, dm, dsInNew)); 617945480ffeSMatthew G. Knepley } 618007218a29SMatthew G. Knepley PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew)); 61819566063dSJacob Faibussowitsch PetscCall(PetscDSDestroy(&dsNew)); 618207218a29SMatthew G. Knepley PetscCall(PetscDSDestroy(&dsInNew)); 61833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 618445480ffeSMatthew G. Knepley } 618545480ffeSMatthew G. Knepley 61867f96f943SMatthew G. Knepley /*@ 6187bb7acecfSBarry Smith DMCopyDS - Copy the discrete systems for the `DM` into another `DM` 6188e5e52638SMatthew G. Knepley 618920f4b53cSBarry Smith Collective 6190e5e52638SMatthew G. Knepley 6191e5e52638SMatthew G. Knepley Input Parameter: 6192bb7acecfSBarry Smith . dm - The `DM` 6193e5e52638SMatthew G. Knepley 6194e5e52638SMatthew G. Knepley Output Parameter: 6195bb7acecfSBarry Smith . newdm - The `DM` 6196e5e52638SMatthew G. Knepley 6197e5e52638SMatthew G. Knepley Level: advanced 6198e5e52638SMatthew G. Knepley 61991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()` 6200e5e52638SMatthew G. Knepley @*/ 6201d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm) 6202d71ae5a4SJacob Faibussowitsch { 6203e5e52638SMatthew G. Knepley PetscInt Nds, s; 6204e5e52638SMatthew G. Knepley 6205e5e52638SMatthew G. Knepley PetscFunctionBegin; 62063ba16761SJacob Faibussowitsch if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS); 62079566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 62089566063dSJacob Faibussowitsch PetscCall(DMClearDS(newdm)); 6209e5e52638SMatthew G. Knepley for (s = 0; s < Nds; ++s) { 6210e5e52638SMatthew G. Knepley DMLabel label; 6211b3cf3223SMatthew G. Knepley IS fields; 621207218a29SMatthew G. Knepley PetscDS ds, dsIn, newds; 6213783e2ec8SMatthew G. Knepley PetscInt Nbd, bd; 6214e5e52638SMatthew G. Knepley 621507218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn)); 6216b8025e53SMatthew G. Knepley /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */ 621707218a29SMatthew G. Knepley PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn)); 6218d5b43468SJose E. Roman /* Complete new labels in the new DS */ 621907218a29SMatthew G. Knepley PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL)); 62209566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumBoundary(newds, &Nbd)); 6221783e2ec8SMatthew G. Knepley for (bd = 0; bd < Nbd; ++bd) { 6222b8025e53SMatthew G. Knepley PetscWeakForm wf; 622345480ffeSMatthew G. Knepley DMLabel label; 6224783e2ec8SMatthew G. Knepley PetscInt field; 6225783e2ec8SMatthew G. Knepley 62269566063dSJacob Faibussowitsch PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL)); 62279566063dSJacob Faibussowitsch PetscCall(PetscWeakFormReplaceLabel(wf, label)); 6228783e2ec8SMatthew G. Knepley } 6229e5e52638SMatthew G. Knepley } 6230799db056SMatthew G. Knepley PetscCall(DMCompleteBCLabels_Internal(newdm)); 62313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6232e5e52638SMatthew G. Knepley } 6233e5e52638SMatthew G. Knepley 6234e5e52638SMatthew G. Knepley /*@ 6235bb7acecfSBarry Smith DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM` 6236e5e52638SMatthew G. Knepley 623720f4b53cSBarry Smith Collective 6238e5e52638SMatthew G. Knepley 6239e5e52638SMatthew G. Knepley Input Parameter: 6240bb7acecfSBarry Smith . dm - The `DM` 6241e5e52638SMatthew G. Knepley 6242e5e52638SMatthew G. Knepley Output Parameter: 6243bb7acecfSBarry Smith . newdm - The `DM` 6244e5e52638SMatthew G. Knepley 6245e5e52638SMatthew G. Knepley Level: advanced 6246e5e52638SMatthew G. Knepley 624760225df5SJacob Faibussowitsch Developer Notes: 6248bb7acecfSBarry Smith Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation 6249bb7acecfSBarry Smith 62501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()` 6251e5e52638SMatthew G. Knepley @*/ 6252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm) 6253d71ae5a4SJacob Faibussowitsch { 6254e5e52638SMatthew G. Knepley PetscFunctionBegin; 62559566063dSJacob Faibussowitsch PetscCall(DMCopyFields(dm, newdm)); 62569566063dSJacob Faibussowitsch PetscCall(DMCopyDS(dm, newdm)); 62573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6258e5e52638SMatthew G. Knepley } 6259e5e52638SMatthew G. Knepley 6260c73cfb54SMatthew G. Knepley /*@ 6261bb7acecfSBarry Smith DMGetDimension - Return the topological dimension of the `DM` 6262c73cfb54SMatthew G. Knepley 626320f4b53cSBarry Smith Not Collective 6264c73cfb54SMatthew G. Knepley 6265c73cfb54SMatthew G. Knepley Input Parameter: 6266bb7acecfSBarry Smith . dm - The `DM` 6267c73cfb54SMatthew G. Knepley 6268c73cfb54SMatthew G. Knepley Output Parameter: 6269c73cfb54SMatthew G. Knepley . dim - The topological dimension 6270c73cfb54SMatthew G. Knepley 6271c73cfb54SMatthew G. Knepley Level: beginner 6272c73cfb54SMatthew G. Knepley 62731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()` 6274c73cfb54SMatthew G. Knepley @*/ 6275d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim) 6276d71ae5a4SJacob Faibussowitsch { 6277c73cfb54SMatthew G. Knepley PetscFunctionBegin; 6278c73cfb54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 62794f572ea9SToby Isaac PetscAssertPointer(dim, 2); 6280c73cfb54SMatthew G. Knepley *dim = dm->dim; 62813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6282c73cfb54SMatthew G. Knepley } 6283c73cfb54SMatthew G. Knepley 6284c73cfb54SMatthew G. Knepley /*@ 6285bb7acecfSBarry Smith DMSetDimension - Set the topological dimension of the `DM` 6286c73cfb54SMatthew G. Knepley 628720f4b53cSBarry Smith Collective 6288c73cfb54SMatthew G. Knepley 6289c73cfb54SMatthew G. Knepley Input Parameters: 6290bb7acecfSBarry Smith + dm - The `DM` 6291c73cfb54SMatthew G. Knepley - dim - The topological dimension 6292c73cfb54SMatthew G. Knepley 6293c73cfb54SMatthew G. Knepley Level: beginner 6294c73cfb54SMatthew G. Knepley 62951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()` 6296c73cfb54SMatthew G. Knepley @*/ 6297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim) 6298d71ae5a4SJacob Faibussowitsch { 6299e5e52638SMatthew G. Knepley PetscDS ds; 630045480ffeSMatthew G. Knepley PetscInt Nds, n; 6301f17e8794SMatthew G. Knepley 6302c73cfb54SMatthew G. Knepley PetscFunctionBegin; 6303c73cfb54SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 6304c73cfb54SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, dim, 2); 6305c73cfb54SMatthew G. Knepley dm->dim = dim; 6306d17bd122SMatthew G. Knepley if (dm->dim >= 0) { 63079566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 630845480ffeSMatthew G. Knepley for (n = 0; n < Nds; ++n) { 630907218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL)); 63109566063dSJacob Faibussowitsch if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim)); 631145480ffeSMatthew G. Knepley } 6312d17bd122SMatthew G. Knepley } 63133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6314c73cfb54SMatthew G. Knepley } 6315c73cfb54SMatthew G. Knepley 6316793f3fe5SMatthew G. Knepley /*@ 6317793f3fe5SMatthew G. Knepley DMGetDimPoints - Get the half-open interval for all points of a given dimension 6318793f3fe5SMatthew G. Knepley 631920f4b53cSBarry Smith Collective 6320793f3fe5SMatthew G. Knepley 6321793f3fe5SMatthew G. Knepley Input Parameters: 6322bb7acecfSBarry Smith + dm - the `DM` 6323793f3fe5SMatthew G. Knepley - dim - the dimension 6324793f3fe5SMatthew G. Knepley 6325793f3fe5SMatthew G. Knepley Output Parameters: 6326793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension 6327aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension 6328793f3fe5SMatthew G. Knepley 632920f4b53cSBarry Smith Level: intermediate 633020f4b53cSBarry Smith 6331793f3fe5SMatthew G. Knepley Note: 6332793f3fe5SMatthew G. Knepley The points are vertices in the Hasse diagram encoding the topology. This is explained in 6333a8d69d7bSBarry Smith https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme, 6334793f3fe5SMatthew G. Knepley then the interval is empty. 6335793f3fe5SMatthew G. Knepley 63361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()` 6337793f3fe5SMatthew G. Knepley @*/ 6338d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd) 6339d71ae5a4SJacob Faibussowitsch { 6340793f3fe5SMatthew G. Knepley PetscInt d; 6341793f3fe5SMatthew G. Knepley 6342793f3fe5SMatthew G. Knepley PetscFunctionBegin; 6343793f3fe5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 63449566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &d)); 63457a8be351SBarry Smith PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim); 6346dbbe0bcdSBarry Smith PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd); 63473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6348793f3fe5SMatthew G. Knepley } 6349793f3fe5SMatthew G. Knepley 63506636e97aSMatthew G Knepley /*@ 6351bb7acecfSBarry Smith DMGetOutputDM - Retrieve the `DM` associated with the layout for output 6352f4d763aaSMatthew G. Knepley 635320f4b53cSBarry Smith Collective 63548f700142SStefano Zampini 6355f4d763aaSMatthew G. Knepley Input Parameter: 6356bb7acecfSBarry Smith . dm - The original `DM` 6357f4d763aaSMatthew G. Knepley 6358f4d763aaSMatthew G. Knepley Output Parameter: 6359bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output 6360f4d763aaSMatthew G. Knepley 6361f4d763aaSMatthew G. Knepley Level: intermediate 6362f4d763aaSMatthew G. Knepley 6363bb7acecfSBarry Smith Note: 6364bb7acecfSBarry Smith In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary 6365bb7acecfSBarry Smith conditions since the algebraic solver does not solve for those variables. The output `DM` includes these excluded points and its global vector contains the 6366bb7acecfSBarry Smith locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof. 6367bb7acecfSBarry Smith 63681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()` 6369f4d763aaSMatthew G. Knepley @*/ 6370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm) 6371d71ae5a4SJacob Faibussowitsch { 6372c26acbdeSMatthew G. Knepley PetscSection section; 63732d4e4a49SMatthew G. Knepley PetscBool hasConstraints, ghasConstraints; 637414f150ffSMatthew G. Knepley 637514f150ffSMatthew G. Knepley PetscFunctionBegin; 637614f150ffSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 63774f572ea9SToby Isaac PetscAssertPointer(odm, 2); 63789566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(dm, §ion)); 63799566063dSJacob Faibussowitsch PetscCall(PetscSectionHasConstraints(section, &hasConstraints)); 6380712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm))); 63812d4e4a49SMatthew G. Knepley if (!ghasConstraints) { 6382c26acbdeSMatthew G. Knepley *odm = dm; 63833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6384c26acbdeSMatthew G. Knepley } 638514f150ffSMatthew G. Knepley if (!dm->dmBC) { 6386c26acbdeSMatthew G. Knepley PetscSection newSection, gsection; 638714f150ffSMatthew G. Knepley PetscSF sf; 638814f150ffSMatthew G. Knepley 63899566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &dm->dmBC)); 63909566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, dm->dmBC)); 63919566063dSJacob Faibussowitsch PetscCall(PetscSectionClone(section, &newSection)); 63929566063dSJacob Faibussowitsch PetscCall(DMSetLocalSection(dm->dmBC, newSection)); 63939566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&newSection)); 63949566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm->dmBC, &sf)); 63959566063dSJacob Faibussowitsch PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection)); 63969566063dSJacob Faibussowitsch PetscCall(DMSetGlobalSection(dm->dmBC, gsection)); 63979566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&gsection)); 639814f150ffSMatthew G. Knepley } 639914f150ffSMatthew G. Knepley *odm = dm->dmBC; 64003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 640114f150ffSMatthew G. Knepley } 6402f4d763aaSMatthew G. Knepley 6403f4d763aaSMatthew G. Knepley /*@ 6404cdb7a50dSMatthew G. Knepley DMGetOutputSequenceNumber - Retrieve the sequence number/value for output 6405f4d763aaSMatthew G. Knepley 6406f4d763aaSMatthew G. Knepley Input Parameter: 6407bb7acecfSBarry Smith . dm - The original `DM` 6408f4d763aaSMatthew G. Knepley 6409cdb7a50dSMatthew G. Knepley Output Parameters: 6410cdb7a50dSMatthew G. Knepley + num - The output sequence number 6411cdb7a50dSMatthew G. Knepley - val - The output sequence value 6412f4d763aaSMatthew G. Knepley 6413f4d763aaSMatthew G. Knepley Level: intermediate 6414f4d763aaSMatthew G. Knepley 6415bb7acecfSBarry Smith Note: 6416bb7acecfSBarry Smith This is intended for output that should appear in sequence, for instance 6417bb7acecfSBarry Smith a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system. 6418bb7acecfSBarry Smith 641960225df5SJacob Faibussowitsch Developer Notes: 6420bb7acecfSBarry Smith The `DM` serves as a convenient place to store the current iteration value. The iteration is not 6421bb7acecfSBarry Smith not directly related to the `DM`. 6422f4d763aaSMatthew G. Knepley 64231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()` 6424f4d763aaSMatthew G. Knepley @*/ 6425d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val) 6426d71ae5a4SJacob Faibussowitsch { 6427f4d763aaSMatthew G. Knepley PetscFunctionBegin; 6428f4d763aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 64299371c9d4SSatish Balay if (num) { 64304f572ea9SToby Isaac PetscAssertPointer(num, 2); 64319371c9d4SSatish Balay *num = dm->outputSequenceNum; 64329371c9d4SSatish Balay } 64339371c9d4SSatish Balay if (val) { 64344f572ea9SToby Isaac PetscAssertPointer(val, 3); 64359371c9d4SSatish Balay *val = dm->outputSequenceVal; 64369371c9d4SSatish Balay } 64373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6438f4d763aaSMatthew G. Knepley } 6439f4d763aaSMatthew G. Knepley 6440f4d763aaSMatthew G. Knepley /*@ 6441cdb7a50dSMatthew G. Knepley DMSetOutputSequenceNumber - Set the sequence number/value for output 6442f4d763aaSMatthew G. Knepley 6443f4d763aaSMatthew G. Knepley Input Parameters: 6444bb7acecfSBarry Smith + dm - The original `DM` 6445cdb7a50dSMatthew G. Knepley . num - The output sequence number 6446cdb7a50dSMatthew G. Knepley - val - The output sequence value 6447f4d763aaSMatthew G. Knepley 6448f4d763aaSMatthew G. Knepley Level: intermediate 6449f4d763aaSMatthew G. Knepley 6450bb7acecfSBarry Smith Note: 6451bb7acecfSBarry Smith This is intended for output that should appear in sequence, for instance 6452bb7acecfSBarry Smith a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system. 6453f4d763aaSMatthew G. Knepley 64541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()` 6455f4d763aaSMatthew G. Knepley @*/ 6456d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val) 6457d71ae5a4SJacob Faibussowitsch { 6458f4d763aaSMatthew G. Knepley PetscFunctionBegin; 6459f4d763aaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 6460f4d763aaSMatthew G. Knepley dm->outputSequenceNum = num; 6461cdb7a50dSMatthew G. Knepley dm->outputSequenceVal = val; 64623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6463cdb7a50dSMatthew G. Knepley } 6464cdb7a50dSMatthew G. Knepley 6465cdb7a50dSMatthew G. Knepley /*@C 6466bb7acecfSBarry Smith DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer` 6467cdb7a50dSMatthew G. Knepley 6468cdb7a50dSMatthew G. Knepley Input Parameters: 6469bb7acecfSBarry Smith + dm - The original `DM` 6470a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from 6471cdb7a50dSMatthew G. Knepley . name - The sequence name 6472cdb7a50dSMatthew G. Knepley - num - The output sequence number 6473cdb7a50dSMatthew G. Knepley 6474cdb7a50dSMatthew G. Knepley Output Parameter: 6475cdb7a50dSMatthew G. Knepley . val - The output sequence value 6476cdb7a50dSMatthew G. Knepley 6477cdb7a50dSMatthew G. Knepley Level: intermediate 6478cdb7a50dSMatthew G. Knepley 6479bb7acecfSBarry Smith Note: 6480bb7acecfSBarry Smith This is intended for output that should appear in sequence, for instance 6481bb7acecfSBarry Smith a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system. 6482bb7acecfSBarry Smith 648360225df5SJacob Faibussowitsch Developer Notes: 6484bb7acecfSBarry Smith It is unclear at the user API level why a `DM` is needed as input 6485cdb7a50dSMatthew G. Knepley 64861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()` 6487cdb7a50dSMatthew G. Knepley @*/ 6488d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val) 6489d71ae5a4SJacob Faibussowitsch { 6490cdb7a50dSMatthew G. Knepley PetscBool ishdf5; 6491cdb7a50dSMatthew G. Knepley 6492cdb7a50dSMatthew G. Knepley PetscFunctionBegin; 6493cdb7a50dSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 6494cdb7a50dSMatthew G. Knepley PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 64954f572ea9SToby Isaac PetscAssertPointer(val, 5); 64969566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5)); 6497cdb7a50dSMatthew G. Knepley if (ishdf5) { 6498cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5) 6499cdb7a50dSMatthew G. Knepley PetscScalar value; 6500cdb7a50dSMatthew G. Knepley 65019566063dSJacob Faibussowitsch PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer)); 65024aeb217fSMatthew G. Knepley *val = PetscRealPart(value); 6503cdb7a50dSMatthew G. Knepley #endif 6504cdb7a50dSMatthew G. Knepley } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()"); 65053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6506f4d763aaSMatthew G. Knepley } 65078e4ac7eaSMatthew G. Knepley 65088e4ac7eaSMatthew G. Knepley /*@ 6509bb7acecfSBarry Smith DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel 65108e4ac7eaSMatthew G. Knepley 651120f4b53cSBarry Smith Not Collective 65128e4ac7eaSMatthew G. Knepley 65138e4ac7eaSMatthew G. Knepley Input Parameter: 6514bb7acecfSBarry Smith . dm - The `DM` 65158e4ac7eaSMatthew G. Knepley 65168e4ac7eaSMatthew G. Knepley Output Parameter: 6517bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution 65188e4ac7eaSMatthew G. Knepley 65198e4ac7eaSMatthew G. Knepley Level: beginner 65208e4ac7eaSMatthew G. Knepley 65211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()` 65228e4ac7eaSMatthew G. Knepley @*/ 6523d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural) 6524d71ae5a4SJacob Faibussowitsch { 65258e4ac7eaSMatthew G. Knepley PetscFunctionBegin; 65268e4ac7eaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 65274f572ea9SToby Isaac PetscAssertPointer(useNatural, 2); 65288e4ac7eaSMatthew G. Knepley *useNatural = dm->useNatural; 65293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 65308e4ac7eaSMatthew G. Knepley } 65318e4ac7eaSMatthew G. Knepley 65328e4ac7eaSMatthew G. Knepley /*@ 6533bb7acecfSBarry Smith DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel 65348e4ac7eaSMatthew G. Knepley 653520f4b53cSBarry Smith Collective 65368e4ac7eaSMatthew G. Knepley 65378e4ac7eaSMatthew G. Knepley Input Parameters: 6538bb7acecfSBarry Smith + dm - The `DM` 6539bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution 65408e4ac7eaSMatthew G. Knepley 6541bb7acecfSBarry Smith Note: 6542bb7acecfSBarry Smith This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()` 65435d3b26e6SMatthew G. Knepley 65448e4ac7eaSMatthew G. Knepley Level: beginner 65458e4ac7eaSMatthew G. Knepley 65461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()` 65478e4ac7eaSMatthew G. Knepley @*/ 6548d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural) 6549d71ae5a4SJacob Faibussowitsch { 65508e4ac7eaSMatthew G. Knepley PetscFunctionBegin; 65518e4ac7eaSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 65528833efb5SBlaise Bourdin PetscValidLogicalCollectiveBool(dm, useNatural, 2); 65538e4ac7eaSMatthew G. Knepley dm->useNatural = useNatural; 65543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 65558e4ac7eaSMatthew G. Knepley } 6556c58f1c22SToby Isaac 6557c58f1c22SToby Isaac /*@C 6558bb7acecfSBarry Smith DMCreateLabel - Create a label of the given name if it does not already exist in the `DM` 6559c58f1c22SToby Isaac 6560c58f1c22SToby Isaac Not Collective 6561c58f1c22SToby Isaac 6562c58f1c22SToby Isaac Input Parameters: 6563bb7acecfSBarry Smith + dm - The `DM` object 6564c58f1c22SToby Isaac - name - The label name 6565c58f1c22SToby Isaac 6566c58f1c22SToby Isaac Level: intermediate 6567c58f1c22SToby Isaac 65681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 6569c58f1c22SToby Isaac @*/ 6570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[]) 6571d71ae5a4SJacob Faibussowitsch { 65725d80c0bfSVaclav Hapla PetscBool flg; 65735d80c0bfSVaclav Hapla DMLabel label; 6574c58f1c22SToby Isaac 6575c58f1c22SToby Isaac PetscFunctionBegin; 6576c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 65774f572ea9SToby Isaac PetscAssertPointer(name, 2); 65789566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, name, &flg)); 6579c58f1c22SToby Isaac if (!flg) { 65809566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label)); 65819566063dSJacob Faibussowitsch PetscCall(DMAddLabel(dm, label)); 65829566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&label)); 6583c58f1c22SToby Isaac } 65843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6585c58f1c22SToby Isaac } 6586c58f1c22SToby Isaac 6587c58f1c22SToby Isaac /*@C 6588bb7acecfSBarry Smith DMCreateLabelAtIndex - Create a label of the given name at the given index. If it already exists in the `DM`, move it to this index. 65890fdc7489SMatthew Knepley 65900fdc7489SMatthew Knepley Not Collective 65910fdc7489SMatthew Knepley 65920fdc7489SMatthew Knepley Input Parameters: 6593bb7acecfSBarry Smith + dm - The `DM` object 65940fdc7489SMatthew Knepley . l - The index for the label 65950fdc7489SMatthew Knepley - name - The label name 65960fdc7489SMatthew Knepley 65970fdc7489SMatthew Knepley Level: intermediate 65980fdc7489SMatthew Knepley 65991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 66000fdc7489SMatthew Knepley @*/ 6601d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[]) 6602d71ae5a4SJacob Faibussowitsch { 66030fdc7489SMatthew Knepley DMLabelLink orig, prev = NULL; 66040fdc7489SMatthew Knepley DMLabel label; 66050fdc7489SMatthew Knepley PetscInt Nl, m; 66060fdc7489SMatthew Knepley PetscBool flg, match; 66070fdc7489SMatthew Knepley const char *lname; 66080fdc7489SMatthew Knepley 66090fdc7489SMatthew Knepley PetscFunctionBegin; 66100fdc7489SMatthew Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 66114f572ea9SToby Isaac PetscAssertPointer(name, 3); 66129566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, name, &flg)); 66130fdc7489SMatthew Knepley if (!flg) { 66149566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label)); 66159566063dSJacob Faibussowitsch PetscCall(DMAddLabel(dm, label)); 66169566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&label)); 66170fdc7489SMatthew Knepley } 66189566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &Nl)); 661963a3b9bcSJacob Faibussowitsch PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl); 66200fdc7489SMatthew Knepley for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) { 66219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname)); 66229566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &match)); 66230fdc7489SMatthew Knepley if (match) break; 66240fdc7489SMatthew Knepley } 66253ba16761SJacob Faibussowitsch if (m == l) PetscFunctionReturn(PETSC_SUCCESS); 66260fdc7489SMatthew Knepley if (!m) dm->labels = orig->next; 66270fdc7489SMatthew Knepley else prev->next = orig->next; 66280fdc7489SMatthew Knepley if (!l) { 66290fdc7489SMatthew Knepley orig->next = dm->labels; 66300fdc7489SMatthew Knepley dm->labels = orig; 66310fdc7489SMatthew Knepley } else { 66329371c9d4SSatish Balay for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next) 66339371c9d4SSatish Balay ; 66340fdc7489SMatthew Knepley orig->next = prev->next; 66350fdc7489SMatthew Knepley prev->next = orig; 66360fdc7489SMatthew Knepley } 66373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 66380fdc7489SMatthew Knepley } 66390fdc7489SMatthew Knepley 66400fdc7489SMatthew Knepley /*@C 6641bb7acecfSBarry Smith DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default 6642c58f1c22SToby Isaac 6643c58f1c22SToby Isaac Not Collective 6644c58f1c22SToby Isaac 6645c58f1c22SToby Isaac Input Parameters: 6646bb7acecfSBarry Smith + dm - The `DM` object 6647c58f1c22SToby Isaac . name - The label name 6648c58f1c22SToby Isaac - point - The mesh point 6649c58f1c22SToby Isaac 6650c58f1c22SToby Isaac Output Parameter: 6651c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label 6652c58f1c22SToby Isaac 6653c58f1c22SToby Isaac Level: beginner 6654c58f1c22SToby Isaac 66551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 6656c58f1c22SToby Isaac @*/ 6657d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value) 6658d71ae5a4SJacob Faibussowitsch { 6659c58f1c22SToby Isaac DMLabel label; 6660c58f1c22SToby Isaac 6661c58f1c22SToby Isaac PetscFunctionBegin; 6662c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 66634f572ea9SToby Isaac PetscAssertPointer(name, 2); 66649566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 66657a8be351SBarry Smith PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name); 66669566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, point, value)); 66673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6668c58f1c22SToby Isaac } 6669c58f1c22SToby Isaac 6670c58f1c22SToby Isaac /*@C 6671bb7acecfSBarry Smith DMSetLabelValue - Add a point to a `DMLabel` with given value 6672c58f1c22SToby Isaac 6673c58f1c22SToby Isaac Not Collective 6674c58f1c22SToby Isaac 6675c58f1c22SToby Isaac Input Parameters: 6676bb7acecfSBarry Smith + dm - The `DM` object 6677c58f1c22SToby Isaac . name - The label name 6678c58f1c22SToby Isaac . point - The mesh point 6679c58f1c22SToby Isaac - value - The label value for this point 6680c58f1c22SToby Isaac 6681c58f1c22SToby Isaac Output Parameter: 6682c58f1c22SToby Isaac 6683c58f1c22SToby Isaac Level: beginner 6684c58f1c22SToby Isaac 66851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()` 6686c58f1c22SToby Isaac @*/ 6687d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) 6688d71ae5a4SJacob Faibussowitsch { 6689c58f1c22SToby Isaac DMLabel label; 6690c58f1c22SToby Isaac 6691c58f1c22SToby Isaac PetscFunctionBegin; 6692c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 66934f572ea9SToby Isaac PetscAssertPointer(name, 2); 66949566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6695c58f1c22SToby Isaac if (!label) { 66969566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, name)); 66979566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6698c58f1c22SToby Isaac } 66999566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, point, value)); 67003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6701c58f1c22SToby Isaac } 6702c58f1c22SToby Isaac 6703c58f1c22SToby Isaac /*@C 6704bb7acecfSBarry Smith DMClearLabelValue - Remove a point from a `DMLabel` with given value 6705c58f1c22SToby Isaac 6706c58f1c22SToby Isaac Not Collective 6707c58f1c22SToby Isaac 6708c58f1c22SToby Isaac Input Parameters: 6709bb7acecfSBarry Smith + dm - The `DM` object 6710c58f1c22SToby Isaac . name - The label name 6711c58f1c22SToby Isaac . point - The mesh point 6712c58f1c22SToby Isaac - value - The label value for this point 6713c58f1c22SToby Isaac 6714c58f1c22SToby Isaac Level: beginner 6715c58f1c22SToby Isaac 67161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 6717c58f1c22SToby Isaac @*/ 6718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value) 6719d71ae5a4SJacob Faibussowitsch { 6720c58f1c22SToby Isaac DMLabel label; 6721c58f1c22SToby Isaac 6722c58f1c22SToby Isaac PetscFunctionBegin; 6723c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 67244f572ea9SToby Isaac PetscAssertPointer(name, 2); 67259566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 67263ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 67279566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(label, point, value)); 67283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6729c58f1c22SToby Isaac } 6730c58f1c22SToby Isaac 6731c58f1c22SToby Isaac /*@C 6732bb7acecfSBarry Smith DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM` 6733c58f1c22SToby Isaac 6734c58f1c22SToby Isaac Not Collective 6735c58f1c22SToby Isaac 6736c58f1c22SToby Isaac Input Parameters: 6737bb7acecfSBarry Smith + dm - The `DM` object 6738c58f1c22SToby Isaac - name - The label name 6739c58f1c22SToby Isaac 6740c58f1c22SToby Isaac Output Parameter: 6741c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist 6742c58f1c22SToby Isaac 6743c58f1c22SToby Isaac Level: beginner 6744c58f1c22SToby Isaac 674560225df5SJacob Faibussowitsch Developer Notes: 6746bb7acecfSBarry Smith This should be renamed to something like `DMGetLabelNumValues()` or removed. 6747bb7acecfSBarry Smith 67481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()` 6749c58f1c22SToby Isaac @*/ 6750d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size) 6751d71ae5a4SJacob Faibussowitsch { 6752c58f1c22SToby Isaac DMLabel label; 6753c58f1c22SToby Isaac 6754c58f1c22SToby Isaac PetscFunctionBegin; 6755c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 67564f572ea9SToby Isaac PetscAssertPointer(name, 2); 67574f572ea9SToby Isaac PetscAssertPointer(size, 3); 67589566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6759c58f1c22SToby Isaac *size = 0; 67603ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 67619566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, size)); 67623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6763c58f1c22SToby Isaac } 6764c58f1c22SToby Isaac 6765c58f1c22SToby Isaac /*@C 6766bb7acecfSBarry Smith DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM` 6767c58f1c22SToby Isaac 6768c58f1c22SToby Isaac Not Collective 6769c58f1c22SToby Isaac 6770c58f1c22SToby Isaac Input Parameters: 677160225df5SJacob Faibussowitsch + dm - The `DM` object 6772c58f1c22SToby Isaac - name - The label name 6773c58f1c22SToby Isaac 6774c58f1c22SToby Isaac Output Parameter: 677520f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist 6776c58f1c22SToby Isaac 6777c58f1c22SToby Isaac Level: beginner 6778c58f1c22SToby Isaac 67791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()` 6780c58f1c22SToby Isaac @*/ 6781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids) 6782d71ae5a4SJacob Faibussowitsch { 6783c58f1c22SToby Isaac DMLabel label; 6784c58f1c22SToby Isaac 6785c58f1c22SToby Isaac PetscFunctionBegin; 6786c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 67874f572ea9SToby Isaac PetscAssertPointer(name, 2); 67884f572ea9SToby Isaac PetscAssertPointer(ids, 3); 67899566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6790c58f1c22SToby Isaac *ids = NULL; 6791dab2e251SBlaise Bourdin if (label) { 67929566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, ids)); 6793dab2e251SBlaise Bourdin } else { 6794dab2e251SBlaise Bourdin /* returning an empty IS */ 67959566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids)); 6796dab2e251SBlaise Bourdin } 67973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6798c58f1c22SToby Isaac } 6799c58f1c22SToby Isaac 6800c58f1c22SToby Isaac /*@C 6801c58f1c22SToby Isaac DMGetStratumSize - Get the number of points in a label stratum 6802c58f1c22SToby Isaac 6803c58f1c22SToby Isaac Not Collective 6804c58f1c22SToby Isaac 6805c58f1c22SToby Isaac Input Parameters: 6806bb7acecfSBarry Smith + dm - The `DM` object 6807c58f1c22SToby Isaac . name - The label name 6808c58f1c22SToby Isaac - value - The stratum value 6809c58f1c22SToby Isaac 6810c58f1c22SToby Isaac Output Parameter: 6811bb7acecfSBarry Smith . size - The number of points, also called the stratum size 6812c58f1c22SToby Isaac 6813c58f1c22SToby Isaac Level: beginner 6814c58f1c22SToby Isaac 68151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()` 6816c58f1c22SToby Isaac @*/ 6817d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size) 6818d71ae5a4SJacob Faibussowitsch { 6819c58f1c22SToby Isaac DMLabel label; 6820c58f1c22SToby Isaac 6821c58f1c22SToby Isaac PetscFunctionBegin; 6822c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 68234f572ea9SToby Isaac PetscAssertPointer(name, 2); 68244f572ea9SToby Isaac PetscAssertPointer(size, 4); 68259566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6826c58f1c22SToby Isaac *size = 0; 68273ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 68289566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(label, value, size)); 68293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6830c58f1c22SToby Isaac } 6831c58f1c22SToby Isaac 6832c58f1c22SToby Isaac /*@C 6833c58f1c22SToby Isaac DMGetStratumIS - Get the points in a label stratum 6834c58f1c22SToby Isaac 6835c58f1c22SToby Isaac Not Collective 6836c58f1c22SToby Isaac 6837c58f1c22SToby Isaac Input Parameters: 6838bb7acecfSBarry Smith + dm - The `DM` object 6839c58f1c22SToby Isaac . name - The label name 6840c58f1c22SToby Isaac - value - The stratum value 6841c58f1c22SToby Isaac 6842c58f1c22SToby Isaac Output Parameter: 684320f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value 6844c58f1c22SToby Isaac 6845c58f1c22SToby Isaac Level: beginner 6846c58f1c22SToby Isaac 68471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()` 6848c58f1c22SToby Isaac @*/ 6849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points) 6850d71ae5a4SJacob Faibussowitsch { 6851c58f1c22SToby Isaac DMLabel label; 6852c58f1c22SToby Isaac 6853c58f1c22SToby Isaac PetscFunctionBegin; 6854c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 68554f572ea9SToby Isaac PetscAssertPointer(name, 2); 68564f572ea9SToby Isaac PetscAssertPointer(points, 4); 68579566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 6858c58f1c22SToby Isaac *points = NULL; 68593ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 68609566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, value, points)); 68613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6862c58f1c22SToby Isaac } 6863c58f1c22SToby Isaac 68644de306b1SToby Isaac /*@C 68659044fa66SMatthew G. Knepley DMSetStratumIS - Set the points in a label stratum 68664de306b1SToby Isaac 68674de306b1SToby Isaac Not Collective 68684de306b1SToby Isaac 68694de306b1SToby Isaac Input Parameters: 6870bb7acecfSBarry Smith + dm - The `DM` object 68714de306b1SToby Isaac . name - The label name 68724de306b1SToby Isaac . value - The stratum value 68734de306b1SToby Isaac - points - The stratum points 68744de306b1SToby Isaac 68754de306b1SToby Isaac Level: beginner 68764de306b1SToby Isaac 68771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()` 68784de306b1SToby Isaac @*/ 6879d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points) 6880d71ae5a4SJacob Faibussowitsch { 68814de306b1SToby Isaac DMLabel label; 68824de306b1SToby Isaac 68834de306b1SToby Isaac PetscFunctionBegin; 68844de306b1SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 68854f572ea9SToby Isaac PetscAssertPointer(name, 2); 6886292bffcbSToby Isaac PetscValidHeaderSpecific(points, IS_CLASSID, 4); 68879566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 68883ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 68899566063dSJacob Faibussowitsch PetscCall(DMLabelSetStratumIS(label, value, points)); 68903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 68914de306b1SToby Isaac } 68924de306b1SToby Isaac 6893c58f1c22SToby Isaac /*@C 6894bb7acecfSBarry Smith DMClearLabelStratum - Remove all points from a stratum from a `DMLabel` 6895c58f1c22SToby Isaac 6896c58f1c22SToby Isaac Not Collective 6897c58f1c22SToby Isaac 6898c58f1c22SToby Isaac Input Parameters: 6899bb7acecfSBarry Smith + dm - The `DM` object 6900c58f1c22SToby Isaac . name - The label name 6901c58f1c22SToby Isaac - value - The label value for this point 6902c58f1c22SToby Isaac 6903c58f1c22SToby Isaac Output Parameter: 6904c58f1c22SToby Isaac 6905c58f1c22SToby Isaac Level: beginner 6906c58f1c22SToby Isaac 69071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()` 6908c58f1c22SToby Isaac @*/ 6909d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value) 6910d71ae5a4SJacob Faibussowitsch { 6911c58f1c22SToby Isaac DMLabel label; 6912c58f1c22SToby Isaac 6913c58f1c22SToby Isaac PetscFunctionBegin; 6914c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 69154f572ea9SToby Isaac PetscAssertPointer(name, 2); 69169566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 69173ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 69189566063dSJacob Faibussowitsch PetscCall(DMLabelClearStratum(label, value)); 69193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6920c58f1c22SToby Isaac } 6921c58f1c22SToby Isaac 6922c58f1c22SToby Isaac /*@ 6923bb7acecfSBarry Smith DMGetNumLabels - Return the number of labels defined by on the `DM` 6924c58f1c22SToby Isaac 6925c58f1c22SToby Isaac Not Collective 6926c58f1c22SToby Isaac 6927c58f1c22SToby Isaac Input Parameter: 6928bb7acecfSBarry Smith . dm - The `DM` object 6929c58f1c22SToby Isaac 6930c58f1c22SToby Isaac Output Parameter: 6931c58f1c22SToby Isaac . numLabels - the number of Labels 6932c58f1c22SToby Isaac 6933c58f1c22SToby Isaac Level: intermediate 6934c58f1c22SToby Isaac 69351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 6936c58f1c22SToby Isaac @*/ 6937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels) 6938d71ae5a4SJacob Faibussowitsch { 69395d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 6940c58f1c22SToby Isaac PetscInt n = 0; 6941c58f1c22SToby Isaac 6942c58f1c22SToby Isaac PetscFunctionBegin; 6943c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 69444f572ea9SToby Isaac PetscAssertPointer(numLabels, 2); 69459371c9d4SSatish Balay while (next) { 69469371c9d4SSatish Balay ++n; 69479371c9d4SSatish Balay next = next->next; 69489371c9d4SSatish Balay } 6949c58f1c22SToby Isaac *numLabels = n; 69503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6951c58f1c22SToby Isaac } 6952c58f1c22SToby Isaac 6953c58f1c22SToby Isaac /*@C 6954c58f1c22SToby Isaac DMGetLabelName - Return the name of nth label 6955c58f1c22SToby Isaac 6956c58f1c22SToby Isaac Not Collective 6957c58f1c22SToby Isaac 6958c58f1c22SToby Isaac Input Parameters: 6959bb7acecfSBarry Smith + dm - The `DM` object 6960c58f1c22SToby Isaac - n - the label number 6961c58f1c22SToby Isaac 6962c58f1c22SToby Isaac Output Parameter: 6963c58f1c22SToby Isaac . name - the label name 6964c58f1c22SToby Isaac 6965c58f1c22SToby Isaac Level: intermediate 6966c58f1c22SToby Isaac 696760225df5SJacob Faibussowitsch Developer Notes: 6968bb7acecfSBarry Smith Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not. 6969bb7acecfSBarry Smith 69701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 6971c58f1c22SToby Isaac @*/ 6972d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name) 6973d71ae5a4SJacob Faibussowitsch { 69745d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 6975c58f1c22SToby Isaac PetscInt l = 0; 6976c58f1c22SToby Isaac 6977c58f1c22SToby Isaac PetscFunctionBegin; 6978c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 69794f572ea9SToby Isaac PetscAssertPointer(name, 3); 6980c58f1c22SToby Isaac while (next) { 6981c58f1c22SToby Isaac if (l == n) { 69829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, name)); 69833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6984c58f1c22SToby Isaac } 6985c58f1c22SToby Isaac ++l; 6986c58f1c22SToby Isaac next = next->next; 6987c58f1c22SToby Isaac } 698863a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n); 6989c58f1c22SToby Isaac } 6990c58f1c22SToby Isaac 6991c58f1c22SToby Isaac /*@C 6992bb7acecfSBarry Smith DMHasLabel - Determine whether the `DM` has a label of a given name 6993c58f1c22SToby Isaac 6994c58f1c22SToby Isaac Not Collective 6995c58f1c22SToby Isaac 6996c58f1c22SToby Isaac Input Parameters: 6997bb7acecfSBarry Smith + dm - The `DM` object 6998c58f1c22SToby Isaac - name - The label name 6999c58f1c22SToby Isaac 7000c58f1c22SToby Isaac Output Parameter: 7001bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present 7002c58f1c22SToby Isaac 7003c58f1c22SToby Isaac Level: intermediate 7004c58f1c22SToby Isaac 70051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 7006c58f1c22SToby Isaac @*/ 7007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel) 7008d71ae5a4SJacob Faibussowitsch { 70095d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 7010d67d17b1SMatthew G. Knepley const char *lname; 7011c58f1c22SToby Isaac 7012c58f1c22SToby Isaac PetscFunctionBegin; 7013c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 70144f572ea9SToby Isaac PetscAssertPointer(name, 2); 70154f572ea9SToby Isaac PetscAssertPointer(hasLabel, 3); 7016c58f1c22SToby Isaac *hasLabel = PETSC_FALSE; 7017c58f1c22SToby Isaac while (next) { 70189566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, &lname)); 70199566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, hasLabel)); 7020c58f1c22SToby Isaac if (*hasLabel) break; 7021c58f1c22SToby Isaac next = next->next; 7022c58f1c22SToby Isaac } 70233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7024c58f1c22SToby Isaac } 7025c58f1c22SToby Isaac 7026a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown 7027c58f1c22SToby Isaac /*@C 702820f4b53cSBarry Smith DMGetLabel - Return the label of a given name, or `NULL`, from a `DM` 7029c58f1c22SToby Isaac 7030c58f1c22SToby Isaac Not Collective 7031c58f1c22SToby Isaac 7032c58f1c22SToby Isaac Input Parameters: 7033bb7acecfSBarry Smith + dm - The `DM` object 7034c58f1c22SToby Isaac - name - The label name 7035c58f1c22SToby Isaac 7036c58f1c22SToby Isaac Output Parameter: 703720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent 7038c58f1c22SToby Isaac 7039bb7acecfSBarry Smith Default labels in a `DMPLEX`: 7040bb7acecfSBarry Smith + "depth" - Holds the depth (co-dimension) of each mesh point 7041bb7acecfSBarry Smith . "celltype" - Holds the topological type of each cell 7042bb7acecfSBarry Smith . "ghost" - If the DM is distributed with overlap, this marks the cells and faces in the overlap 7043bb7acecfSBarry Smith . "Cell Sets" - Mirrors the cell sets defined by GMsh and ExodusII 7044bb7acecfSBarry Smith . "Face Sets" - Mirrors the face sets defined by GMsh and ExodusII 7045bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh 70466d7c9049SMatthew G. Knepley 7047c58f1c22SToby Isaac Level: intermediate 7048c58f1c22SToby Isaac 704960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()` 7050c58f1c22SToby Isaac @*/ 7051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label) 7052d71ae5a4SJacob Faibussowitsch { 70535d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 7054c58f1c22SToby Isaac PetscBool hasLabel; 7055d67d17b1SMatthew G. Knepley const char *lname; 7056c58f1c22SToby Isaac 7057c58f1c22SToby Isaac PetscFunctionBegin; 7058c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 70594f572ea9SToby Isaac PetscAssertPointer(name, 2); 70604f572ea9SToby Isaac PetscAssertPointer(label, 3); 7061c58f1c22SToby Isaac *label = NULL; 7062c58f1c22SToby Isaac while (next) { 70639566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, &lname)); 70649566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &hasLabel)); 7065c58f1c22SToby Isaac if (hasLabel) { 7066c58f1c22SToby Isaac *label = next->label; 7067c58f1c22SToby Isaac break; 7068c58f1c22SToby Isaac } 7069c58f1c22SToby Isaac next = next->next; 7070c58f1c22SToby Isaac } 70713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7072c58f1c22SToby Isaac } 7073c58f1c22SToby Isaac 7074c58f1c22SToby Isaac /*@C 7075bb7acecfSBarry Smith DMGetLabelByNum - Return the nth label on a `DM` 7076c58f1c22SToby Isaac 7077c58f1c22SToby Isaac Not Collective 7078c58f1c22SToby Isaac 7079c58f1c22SToby Isaac Input Parameters: 7080bb7acecfSBarry Smith + dm - The `DM` object 7081c58f1c22SToby Isaac - n - the label number 7082c58f1c22SToby Isaac 7083c58f1c22SToby Isaac Output Parameter: 7084c58f1c22SToby Isaac . label - the label 7085c58f1c22SToby Isaac 7086c58f1c22SToby Isaac Level: intermediate 7087c58f1c22SToby Isaac 70881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 7089c58f1c22SToby Isaac @*/ 7090d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label) 7091d71ae5a4SJacob Faibussowitsch { 70925d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 7093c58f1c22SToby Isaac PetscInt l = 0; 7094c58f1c22SToby Isaac 7095c58f1c22SToby Isaac PetscFunctionBegin; 7096c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 70974f572ea9SToby Isaac PetscAssertPointer(label, 3); 7098c58f1c22SToby Isaac while (next) { 7099c58f1c22SToby Isaac if (l == n) { 7100c58f1c22SToby Isaac *label = next->label; 71013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7102c58f1c22SToby Isaac } 7103c58f1c22SToby Isaac ++l; 7104c58f1c22SToby Isaac next = next->next; 7105c58f1c22SToby Isaac } 710663a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n); 7107c58f1c22SToby Isaac } 7108c58f1c22SToby Isaac 7109c58f1c22SToby Isaac /*@C 7110bb7acecfSBarry Smith DMAddLabel - Add the label to this `DM` 7111c58f1c22SToby Isaac 7112c58f1c22SToby Isaac Not Collective 7113c58f1c22SToby Isaac 7114c58f1c22SToby Isaac Input Parameters: 7115bb7acecfSBarry Smith + dm - The `DM` object 7116bb7acecfSBarry Smith - label - The `DMLabel` 7117c58f1c22SToby Isaac 7118c58f1c22SToby Isaac Level: developer 7119c58f1c22SToby Isaac 712060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 7121c58f1c22SToby Isaac @*/ 7122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label) 7123d71ae5a4SJacob Faibussowitsch { 71245d80c0bfSVaclav Hapla DMLabelLink l, *p, tmpLabel; 7125c58f1c22SToby Isaac PetscBool hasLabel; 7126d67d17b1SMatthew G. Knepley const char *lname; 71275d80c0bfSVaclav Hapla PetscBool flg; 7128c58f1c22SToby Isaac 7129c58f1c22SToby Isaac PetscFunctionBegin; 7130c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 71319566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)label, &lname)); 71329566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, lname, &hasLabel)); 71337a8be351SBarry Smith PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname); 71349566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(1, &tmpLabel)); 7135c58f1c22SToby Isaac tmpLabel->label = label; 7136c58f1c22SToby Isaac tmpLabel->output = PETSC_TRUE; 71375d80c0bfSVaclav Hapla for (p = &dm->labels; (l = *p); p = &l->next) { } 71385d80c0bfSVaclav Hapla *p = tmpLabel; 71399566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 71409566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "depth", &flg)); 71415d80c0bfSVaclav Hapla if (flg) dm->depthLabel = label; 71429566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "celltype", &flg)); 7143ba2698f1SMatthew G. Knepley if (flg) dm->celltypeLabel = label; 71443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7145c58f1c22SToby Isaac } 7146c58f1c22SToby Isaac 7147a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown 7148c58f1c22SToby Isaac /*@C 71494a7ee7d0SMatthew G. Knepley DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present 71504a7ee7d0SMatthew G. Knepley 71514a7ee7d0SMatthew G. Knepley Not Collective 71524a7ee7d0SMatthew G. Knepley 71534a7ee7d0SMatthew G. Knepley Input Parameters: 7154bb7acecfSBarry Smith + dm - The `DM` object 7155bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute 71564a7ee7d0SMatthew G. Knepley 7157bb7acecfSBarry Smith Default labels in a `DMPLEX`: 7158bb7acecfSBarry Smith + "depth" - Holds the depth (co-dimension) of each mesh point 7159bb7acecfSBarry Smith . "celltype" - Holds the topological type of each cell 7160bb7acecfSBarry Smith . "ghost" - If the DM is distributed with overlap, this marks the cells and faces in the overlap 7161bb7acecfSBarry Smith . "Cell Sets" - Mirrors the cell sets defined by GMsh and ExodusII 7162bb7acecfSBarry Smith . "Face Sets" - Mirrors the face sets defined by GMsh and ExodusII 7163bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh 71644a7ee7d0SMatthew G. Knepley 71654a7ee7d0SMatthew G. Knepley Level: intermediate 71664a7ee7d0SMatthew G. Knepley 71671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()` 71684a7ee7d0SMatthew G. Knepley @*/ 7169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label) 7170d71ae5a4SJacob Faibussowitsch { 71714a7ee7d0SMatthew G. Knepley DMLabelLink next = dm->labels; 71724a7ee7d0SMatthew G. Knepley PetscBool hasLabel, flg; 71734a7ee7d0SMatthew G. Knepley const char *name, *lname; 71744a7ee7d0SMatthew G. Knepley 71754a7ee7d0SMatthew G. Knepley PetscFunctionBegin; 71764a7ee7d0SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 71774a7ee7d0SMatthew G. Knepley PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 71789566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)label, &name)); 71794a7ee7d0SMatthew G. Knepley while (next) { 71809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, &lname)); 71819566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &hasLabel)); 71824a7ee7d0SMatthew G. Knepley if (hasLabel) { 71839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)label)); 71849566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "depth", &flg)); 71854a7ee7d0SMatthew G. Knepley if (flg) dm->depthLabel = label; 71869566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "celltype", &flg)); 71874a7ee7d0SMatthew G. Knepley if (flg) dm->celltypeLabel = label; 71889566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&next->label)); 71894a7ee7d0SMatthew G. Knepley next->label = label; 71904a7ee7d0SMatthew G. Knepley break; 71914a7ee7d0SMatthew G. Knepley } 71924a7ee7d0SMatthew G. Knepley next = next->next; 71934a7ee7d0SMatthew G. Knepley } 71943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 71954a7ee7d0SMatthew G. Knepley } 71964a7ee7d0SMatthew G. Knepley 71974a7ee7d0SMatthew G. Knepley /*@C 7198bb7acecfSBarry Smith DMRemoveLabel - Remove the label given by name from this `DM` 7199c58f1c22SToby Isaac 7200c58f1c22SToby Isaac Not Collective 7201c58f1c22SToby Isaac 7202c58f1c22SToby Isaac Input Parameters: 7203bb7acecfSBarry Smith + dm - The `DM` object 7204c58f1c22SToby Isaac - name - The label name 7205c58f1c22SToby Isaac 7206c58f1c22SToby Isaac Output Parameter: 720720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the 7208bb7acecfSBarry Smith caller is responsible for calling `DMLabelDestroy()`. 7209c58f1c22SToby Isaac 7210c58f1c22SToby Isaac Level: developer 7211c58f1c22SToby Isaac 72121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()` 7213c58f1c22SToby Isaac @*/ 7214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label) 7215d71ae5a4SJacob Faibussowitsch { 721695d578d6SVaclav Hapla DMLabelLink link, *pnext; 7217c58f1c22SToby Isaac PetscBool hasLabel; 7218d67d17b1SMatthew G. Knepley const char *lname; 7219c58f1c22SToby Isaac 7220c58f1c22SToby Isaac PetscFunctionBegin; 7221c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 72224f572ea9SToby Isaac PetscAssertPointer(name, 2); 7223e5472504SVaclav Hapla if (label) { 72244f572ea9SToby Isaac PetscAssertPointer(label, 3); 7225c58f1c22SToby Isaac *label = NULL; 7226e5472504SVaclav Hapla } 72275d80c0bfSVaclav Hapla for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) { 72289566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)link->label, &lname)); 72299566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &hasLabel)); 7230c58f1c22SToby Isaac if (hasLabel) { 723195d578d6SVaclav Hapla *pnext = link->next; /* Remove from list */ 72329566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "depth", &hasLabel)); 723395d578d6SVaclav Hapla if (hasLabel) dm->depthLabel = NULL; 72349566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "celltype", &hasLabel)); 7235ba2698f1SMatthew G. Knepley if (hasLabel) dm->celltypeLabel = NULL; 723695d578d6SVaclav Hapla if (label) *label = link->label; 72379566063dSJacob Faibussowitsch else PetscCall(DMLabelDestroy(&link->label)); 72389566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 7239c58f1c22SToby Isaac break; 7240c58f1c22SToby Isaac } 7241c58f1c22SToby Isaac } 72423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7243c58f1c22SToby Isaac } 7244c58f1c22SToby Isaac 7245306894acSVaclav Hapla /*@ 7246bb7acecfSBarry Smith DMRemoveLabelBySelf - Remove the label from this `DM` 7247306894acSVaclav Hapla 7248306894acSVaclav Hapla Not Collective 7249306894acSVaclav Hapla 7250306894acSVaclav Hapla Input Parameters: 7251bb7acecfSBarry Smith + dm - The `DM` object 7252bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM` 725320f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`? 7254306894acSVaclav Hapla 7255306894acSVaclav Hapla Level: developer 7256306894acSVaclav Hapla 7257bb7acecfSBarry Smith Note: 7258306894acSVaclav Hapla Only exactly the same instance is removed if found, name match is ignored. 7259bb7acecfSBarry Smith If the `DM` has an exclusive reference to the label, the label gets destroyed and 7260306894acSVaclav Hapla *label nullified. 7261306894acSVaclav Hapla 72621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()` 7263306894acSVaclav Hapla @*/ 7264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound) 7265d71ae5a4SJacob Faibussowitsch { 726643e45a93SVaclav Hapla DMLabelLink link, *pnext; 7267306894acSVaclav Hapla PetscBool hasLabel = PETSC_FALSE; 7268306894acSVaclav Hapla 7269306894acSVaclav Hapla PetscFunctionBegin; 7270306894acSVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 72714f572ea9SToby Isaac PetscAssertPointer(label, 2); 72723ba16761SJacob Faibussowitsch if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS); 7273306894acSVaclav Hapla PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2); 7274306894acSVaclav Hapla PetscValidLogicalCollectiveBool(dm, failNotFound, 3); 72755d80c0bfSVaclav Hapla for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) { 727643e45a93SVaclav Hapla if (*label == link->label) { 7277306894acSVaclav Hapla hasLabel = PETSC_TRUE; 727843e45a93SVaclav Hapla *pnext = link->next; /* Remove from list */ 7279306894acSVaclav Hapla if (*label == dm->depthLabel) dm->depthLabel = NULL; 7280ba2698f1SMatthew G. Knepley if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL; 728143e45a93SVaclav Hapla if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */ 72829566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&link->label)); 72839566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 7284306894acSVaclav Hapla break; 7285306894acSVaclav Hapla } 7286306894acSVaclav Hapla } 72877a8be351SBarry Smith PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM"); 72883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7289306894acSVaclav Hapla } 7290306894acSVaclav Hapla 7291c58f1c22SToby Isaac /*@C 7292c58f1c22SToby Isaac DMGetLabelOutput - Get the output flag for a given label 7293c58f1c22SToby Isaac 7294c58f1c22SToby Isaac Not Collective 7295c58f1c22SToby Isaac 7296c58f1c22SToby Isaac Input Parameters: 7297bb7acecfSBarry Smith + dm - The `DM` object 7298c58f1c22SToby Isaac - name - The label name 7299c58f1c22SToby Isaac 7300c58f1c22SToby Isaac Output Parameter: 7301c58f1c22SToby Isaac . output - The flag for output 7302c58f1c22SToby Isaac 7303c58f1c22SToby Isaac Level: developer 7304c58f1c22SToby Isaac 73051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 7306c58f1c22SToby Isaac @*/ 7307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output) 7308d71ae5a4SJacob Faibussowitsch { 73095d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 7310d67d17b1SMatthew G. Knepley const char *lname; 7311c58f1c22SToby Isaac 7312c58f1c22SToby Isaac PetscFunctionBegin; 7313c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 73144f572ea9SToby Isaac PetscAssertPointer(name, 2); 73154f572ea9SToby Isaac PetscAssertPointer(output, 3); 7316c58f1c22SToby Isaac while (next) { 7317c58f1c22SToby Isaac PetscBool flg; 7318c58f1c22SToby Isaac 73199566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, &lname)); 73209566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &flg)); 73219371c9d4SSatish Balay if (flg) { 73229371c9d4SSatish Balay *output = next->output; 73233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 73249371c9d4SSatish Balay } 7325c58f1c22SToby Isaac next = next->next; 7326c58f1c22SToby Isaac } 732798921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name); 7328c58f1c22SToby Isaac } 7329c58f1c22SToby Isaac 7330c58f1c22SToby Isaac /*@C 7331bb7acecfSBarry Smith DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()` 7332c58f1c22SToby Isaac 7333c58f1c22SToby Isaac Not Collective 7334c58f1c22SToby Isaac 7335c58f1c22SToby Isaac Input Parameters: 7336bb7acecfSBarry Smith + dm - The `DM` object 7337c58f1c22SToby Isaac . name - The label name 7338bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer 7339c58f1c22SToby Isaac 7340c58f1c22SToby Isaac Level: developer 7341c58f1c22SToby Isaac 73421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()` 7343c58f1c22SToby Isaac @*/ 7344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output) 7345d71ae5a4SJacob Faibussowitsch { 73465d80c0bfSVaclav Hapla DMLabelLink next = dm->labels; 7347d67d17b1SMatthew G. Knepley const char *lname; 7348c58f1c22SToby Isaac 7349c58f1c22SToby Isaac PetscFunctionBegin; 7350c58f1c22SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 73514f572ea9SToby Isaac PetscAssertPointer(name, 2); 7352c58f1c22SToby Isaac while (next) { 7353c58f1c22SToby Isaac PetscBool flg; 7354c58f1c22SToby Isaac 73559566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)next->label, &lname)); 73569566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, lname, &flg)); 73579371c9d4SSatish Balay if (flg) { 73589371c9d4SSatish Balay next->output = output; 73593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 73609371c9d4SSatish Balay } 7361c58f1c22SToby Isaac next = next->next; 7362c58f1c22SToby Isaac } 736398921bdaSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name); 7364c58f1c22SToby Isaac } 7365c58f1c22SToby Isaac 7366c58f1c22SToby Isaac /*@ 7367bb7acecfSBarry Smith DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points 7368c58f1c22SToby Isaac 736920f4b53cSBarry Smith Collective 7370c58f1c22SToby Isaac 7371d8d19677SJose E. Roman Input Parameters: 7372bb7acecfSBarry Smith + dmA - The `DM` object with initial labels 7373bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied 7374bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`) 7375bb7acecfSBarry Smith . all - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`) 7376bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`) 7377c58f1c22SToby Isaac 7378c58f1c22SToby Isaac Level: intermediate 7379c58f1c22SToby Isaac 7380bb7acecfSBarry Smith Note: 73812cbb9b06SVaclav Hapla This is typically used when interpolating or otherwise adding to a mesh, or testing. 7382c58f1c22SToby Isaac 73831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode` 7384c58f1c22SToby Isaac @*/ 7385d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode) 7386d71ae5a4SJacob Faibussowitsch { 73872cbb9b06SVaclav Hapla DMLabel label, labelNew, labelOld; 7388c58f1c22SToby Isaac const char *name; 7389c58f1c22SToby Isaac PetscBool flg; 73905d80c0bfSVaclav Hapla DMLabelLink link; 7391c58f1c22SToby Isaac 73925d80c0bfSVaclav Hapla PetscFunctionBegin; 73935d80c0bfSVaclav Hapla PetscValidHeaderSpecific(dmA, DM_CLASSID, 1); 73945d80c0bfSVaclav Hapla PetscValidHeaderSpecific(dmB, DM_CLASSID, 2); 73955d80c0bfSVaclav Hapla PetscValidLogicalCollectiveEnum(dmA, mode, 3); 73965d80c0bfSVaclav Hapla PetscValidLogicalCollectiveBool(dmA, all, 4); 73977a8be351SBarry Smith PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects"); 73983ba16761SJacob Faibussowitsch if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS); 73995d80c0bfSVaclav Hapla for (link = dmA->labels; link; link = link->next) { 74005d80c0bfSVaclav Hapla label = link->label; 74019566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)label, &name)); 74025d80c0bfSVaclav Hapla if (!all) { 74039566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "depth", &flg)); 7404c58f1c22SToby Isaac if (flg) continue; 74059566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "dim", &flg)); 74067d5acc75SStefano Zampini if (flg) continue; 74079566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "celltype", &flg)); 7408ba2698f1SMatthew G. Knepley if (flg) continue; 74095d80c0bfSVaclav Hapla } 74109566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dmB, name, &labelOld)); 74112cbb9b06SVaclav Hapla if (labelOld) { 74122cbb9b06SVaclav Hapla switch (emode) { 7413d71ae5a4SJacob Faibussowitsch case DM_COPY_LABELS_KEEP: 7414d71ae5a4SJacob Faibussowitsch continue; 7415d71ae5a4SJacob Faibussowitsch case DM_COPY_LABELS_REPLACE: 7416d71ae5a4SJacob Faibussowitsch PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE)); 7417d71ae5a4SJacob Faibussowitsch break; 7418d71ae5a4SJacob Faibussowitsch case DM_COPY_LABELS_FAIL: 7419d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name); 7420d71ae5a4SJacob Faibussowitsch default: 7421d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode); 74222cbb9b06SVaclav Hapla } 74232cbb9b06SVaclav Hapla } 74245d80c0bfSVaclav Hapla if (mode == PETSC_COPY_VALUES) { 74259566063dSJacob Faibussowitsch PetscCall(DMLabelDuplicate(label, &labelNew)); 74265d80c0bfSVaclav Hapla } else { 74275d80c0bfSVaclav Hapla labelNew = label; 74285d80c0bfSVaclav Hapla } 74299566063dSJacob Faibussowitsch PetscCall(DMAddLabel(dmB, labelNew)); 74309566063dSJacob Faibussowitsch if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew)); 7431c58f1c22SToby Isaac } 74323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7433c58f1c22SToby Isaac } 7434461a15a0SLisandro Dalcin 7435609dae6eSVaclav Hapla /*@C 7436b4028c23SStefano Zampini DMCompareLabels - Compare labels between two `DM` objects 7437609dae6eSVaclav Hapla 743820f4b53cSBarry Smith Collective; No Fortran Support 7439609dae6eSVaclav Hapla 7440609dae6eSVaclav Hapla Input Parameters: 7441bb7acecfSBarry Smith + dm0 - First `DM` object 7442bb7acecfSBarry Smith - dm1 - Second `DM` object 7443609dae6eSVaclav Hapla 7444a4e35b19SJacob Faibussowitsch Output Parameters: 74455efe38ccSVaclav Hapla + equal - (Optional) Flag whether labels of dm0 and dm1 are the same 744620f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference 7447609dae6eSVaclav Hapla 7448609dae6eSVaclav Hapla Level: intermediate 7449609dae6eSVaclav Hapla 7450609dae6eSVaclav Hapla Notes: 7451bb7acecfSBarry Smith The output flag equal will be the same on all processes. 7452bb7acecfSBarry Smith 745320f4b53cSBarry Smith If equal is passed as `NULL` and difference is found, an error is thrown on all processes. 7454bb7acecfSBarry Smith 745520f4b53cSBarry Smith Make sure to pass equal is `NULL` on all processes or none of them. 7456609dae6eSVaclav Hapla 74575efe38ccSVaclav Hapla The output message is set independently on each rank. 7458bb7acecfSBarry Smith 7459bb7acecfSBarry Smith message must be freed with `PetscFree()` 7460bb7acecfSBarry Smith 746120f4b53cSBarry Smith If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner. 7462bb7acecfSBarry Smith 746320f4b53cSBarry Smith Make sure to pass message as `NULL` on all processes or no processes. 7464609dae6eSVaclav Hapla 7465609dae6eSVaclav Hapla Labels are matched by name. If the number of labels and their names are equal, 7466bb7acecfSBarry Smith `DMLabelCompare()` is used to compare each pair of labels with the same name. 7467609dae6eSVaclav Hapla 74681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()` 7469609dae6eSVaclav Hapla @*/ 7470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message) 7471d71ae5a4SJacob Faibussowitsch { 74725efe38ccSVaclav Hapla PetscInt n, i; 7473609dae6eSVaclav Hapla char msg[PETSC_MAX_PATH_LEN] = ""; 74745efe38ccSVaclav Hapla PetscBool eq; 7475609dae6eSVaclav Hapla MPI_Comm comm; 74765efe38ccSVaclav Hapla PetscMPIInt rank; 7477609dae6eSVaclav Hapla 7478609dae6eSVaclav Hapla PetscFunctionBegin; 7479609dae6eSVaclav Hapla PetscValidHeaderSpecific(dm0, DM_CLASSID, 1); 7480609dae6eSVaclav Hapla PetscValidHeaderSpecific(dm1, DM_CLASSID, 2); 7481609dae6eSVaclav Hapla PetscCheckSameComm(dm0, 1, dm1, 2); 74824f572ea9SToby Isaac if (equal) PetscAssertPointer(equal, 3); 74834f572ea9SToby Isaac if (message) PetscAssertPointer(message, 4); 74849566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm)); 74859566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 74865efe38ccSVaclav Hapla { 74875efe38ccSVaclav Hapla PetscInt n1; 74885efe38ccSVaclav Hapla 74899566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm0, &n)); 74909566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm1, &n1)); 74915efe38ccSVaclav Hapla eq = (PetscBool)(n == n1); 749248a46eb9SPierre Jolivet if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1)); 7493712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm)); 74945efe38ccSVaclav Hapla if (!eq) goto finish; 74955efe38ccSVaclav Hapla } 74965efe38ccSVaclav Hapla for (i = 0; i < n; i++) { 7497609dae6eSVaclav Hapla DMLabel l0, l1; 7498609dae6eSVaclav Hapla const char *name; 7499609dae6eSVaclav Hapla char *msgInner; 7500609dae6eSVaclav Hapla 7501609dae6eSVaclav Hapla /* Ignore label order */ 75029566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm0, i, &l0)); 75039566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)l0, &name)); 75049566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm1, name, &l1)); 7505609dae6eSVaclav Hapla if (!l1) { 750663a3b9bcSJacob Faibussowitsch PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i)); 75075efe38ccSVaclav Hapla eq = PETSC_FALSE; 75085efe38ccSVaclav Hapla break; 7509609dae6eSVaclav Hapla } 75109566063dSJacob Faibussowitsch PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner)); 75119566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg))); 75129566063dSJacob Faibussowitsch PetscCall(PetscFree(msgInner)); 75135efe38ccSVaclav Hapla if (!eq) break; 7514609dae6eSVaclav Hapla } 7515712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm)); 7516609dae6eSVaclav Hapla finish: 75175efe38ccSVaclav Hapla /* If message output arg not set, print to stderr */ 7518609dae6eSVaclav Hapla if (message) { 7519609dae6eSVaclav Hapla *message = NULL; 752048a46eb9SPierre Jolivet if (msg[0]) PetscCall(PetscStrallocpy(msg, message)); 75215efe38ccSVaclav Hapla } else { 752248a46eb9SPierre Jolivet if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg)); 75239566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR)); 75245efe38ccSVaclav Hapla } 75255efe38ccSVaclav Hapla /* If same output arg not ser and labels are not equal, throw error */ 75265efe38ccSVaclav Hapla if (equal) *equal = eq; 75277a8be351SBarry Smith else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1"); 75283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7529609dae6eSVaclav Hapla } 7530609dae6eSVaclav Hapla 7531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value) 7532d71ae5a4SJacob Faibussowitsch { 7533461a15a0SLisandro Dalcin PetscFunctionBegin; 75344f572ea9SToby Isaac PetscAssertPointer(label, 2); 7535461a15a0SLisandro Dalcin if (!*label) { 75369566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, name)); 75379566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, label)); 7538461a15a0SLisandro Dalcin } 75399566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(*label, point, value)); 75403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7541461a15a0SLisandro Dalcin } 7542461a15a0SLisandro Dalcin 75430fdc7489SMatthew Knepley /* 75440fdc7489SMatthew Knepley Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would 75450fdc7489SMatthew Knepley like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every 75460fdc7489SMatthew Knepley (label, id) pair in the DM. 75470fdc7489SMatthew Knepley 75480fdc7489SMatthew Knepley However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to 75490fdc7489SMatthew Knepley each label. 75500fdc7489SMatthew Knepley */ 7551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal) 7552d71ae5a4SJacob Faibussowitsch { 75530fdc7489SMatthew Knepley DMUniversalLabel ul; 75540fdc7489SMatthew Knepley PetscBool *active; 75550fdc7489SMatthew Knepley PetscInt pStart, pEnd, p, Nl, l, m; 75560fdc7489SMatthew Knepley 75570fdc7489SMatthew Knepley PetscFunctionBegin; 75589566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(1, &ul)); 75599566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label)); 75609566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &Nl)); 75619566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(Nl, &active)); 75620fdc7489SMatthew Knepley ul->Nl = 0; 75630fdc7489SMatthew Knepley for (l = 0; l < Nl; ++l) { 75640fdc7489SMatthew Knepley PetscBool isdepth, iscelltype; 75650fdc7489SMatthew Knepley const char *name; 75660fdc7489SMatthew Knepley 75679566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(dm, l, &name)); 75689566063dSJacob Faibussowitsch PetscCall(PetscStrncmp(name, "depth", 6, &isdepth)); 75699566063dSJacob Faibussowitsch PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype)); 75700fdc7489SMatthew Knepley active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE; 75710fdc7489SMatthew Knepley if (active[l]) ++ul->Nl; 75720fdc7489SMatthew Knepley } 75739566063dSJacob Faibussowitsch PetscCall(PetscCalloc5(ul->Nl, &ul->names, ul->Nl, &ul->indices, ul->Nl + 1, &ul->offsets, ul->Nl + 1, &ul->bits, ul->Nl, &ul->masks)); 75740fdc7489SMatthew Knepley ul->Nv = 0; 75750fdc7489SMatthew Knepley for (l = 0, m = 0; l < Nl; ++l) { 75760fdc7489SMatthew Knepley DMLabel label; 75770fdc7489SMatthew Knepley PetscInt nv; 75780fdc7489SMatthew Knepley const char *name; 75790fdc7489SMatthew Knepley 75800fdc7489SMatthew Knepley if (!active[l]) continue; 75819566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(dm, l, &name)); 75829566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm, l, &label)); 75839566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &nv)); 75849566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name, &ul->names[m])); 75850fdc7489SMatthew Knepley ul->indices[m] = l; 75860fdc7489SMatthew Knepley ul->Nv += nv; 75870fdc7489SMatthew Knepley ul->offsets[m + 1] = nv; 75880fdc7489SMatthew Knepley ul->bits[m + 1] = PetscCeilReal(PetscLog2Real(nv + 1)); 75890fdc7489SMatthew Knepley ++m; 75900fdc7489SMatthew Knepley } 75910fdc7489SMatthew Knepley for (l = 1; l <= ul->Nl; ++l) { 75920fdc7489SMatthew Knepley ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l]; 75930fdc7489SMatthew Knepley ul->bits[l] = ul->bits[l - 1] + ul->bits[l]; 75940fdc7489SMatthew Knepley } 75950fdc7489SMatthew Knepley for (l = 0; l < ul->Nl; ++l) { 75960fdc7489SMatthew Knepley PetscInt b; 75970fdc7489SMatthew Knepley 75980fdc7489SMatthew Knepley ul->masks[l] = 0; 75990fdc7489SMatthew Knepley for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b; 76000fdc7489SMatthew Knepley } 76019566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(ul->Nv, &ul->values)); 76020fdc7489SMatthew Knepley for (l = 0, m = 0; l < Nl; ++l) { 76030fdc7489SMatthew Knepley DMLabel label; 76040fdc7489SMatthew Knepley IS valueIS; 76050fdc7489SMatthew Knepley const PetscInt *varr; 76060fdc7489SMatthew Knepley PetscInt nv, v; 76070fdc7489SMatthew Knepley 76080fdc7489SMatthew Knepley if (!active[l]) continue; 76099566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm, l, &label)); 76109566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &nv)); 76119566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 76129566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &varr)); 7613ad540459SPierre Jolivet for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v]; 76149566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &varr)); 76159566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 76169566063dSJacob Faibussowitsch PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]])); 76170fdc7489SMatthew Knepley ++m; 76180fdc7489SMatthew Knepley } 76199566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 76200fdc7489SMatthew Knepley for (p = pStart; p < pEnd; ++p) { 76210fdc7489SMatthew Knepley PetscInt uval = 0; 76220fdc7489SMatthew Knepley PetscBool marked = PETSC_FALSE; 76230fdc7489SMatthew Knepley 76240fdc7489SMatthew Knepley for (l = 0, m = 0; l < Nl; ++l) { 76250fdc7489SMatthew Knepley DMLabel label; 76260649b39aSStefano Zampini PetscInt val, defval, loc, nv; 76270fdc7489SMatthew Knepley 76280fdc7489SMatthew Knepley if (!active[l]) continue; 76299566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(dm, l, &label)); 76309566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, p, &val)); 76319566063dSJacob Faibussowitsch PetscCall(DMLabelGetDefaultValue(label, &defval)); 76329371c9d4SSatish Balay if (val == defval) { 76339371c9d4SSatish Balay ++m; 76349371c9d4SSatish Balay continue; 76359371c9d4SSatish Balay } 76360649b39aSStefano Zampini nv = ul->offsets[m + 1] - ul->offsets[m]; 76370fdc7489SMatthew Knepley marked = PETSC_TRUE; 76389566063dSJacob Faibussowitsch PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc)); 763963a3b9bcSJacob Faibussowitsch PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val); 76400fdc7489SMatthew Knepley uval += (loc + 1) << ul->bits[m]; 76410fdc7489SMatthew Knepley ++m; 76420fdc7489SMatthew Knepley } 76439566063dSJacob Faibussowitsch if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval)); 76440fdc7489SMatthew Knepley } 76459566063dSJacob Faibussowitsch PetscCall(PetscFree(active)); 76460fdc7489SMatthew Knepley *universal = ul; 76473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 76480fdc7489SMatthew Knepley } 76490fdc7489SMatthew Knepley 7650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal) 7651d71ae5a4SJacob Faibussowitsch { 76520fdc7489SMatthew Knepley PetscInt l; 76530fdc7489SMatthew Knepley 76540fdc7489SMatthew Knepley PetscFunctionBegin; 76559566063dSJacob Faibussowitsch for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l])); 76569566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&(*universal)->label)); 76579566063dSJacob Faibussowitsch PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks)); 76589566063dSJacob Faibussowitsch PetscCall(PetscFree((*universal)->values)); 76599566063dSJacob Faibussowitsch PetscCall(PetscFree(*universal)); 76600fdc7489SMatthew Knepley *universal = NULL; 76613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 76620fdc7489SMatthew Knepley } 76630fdc7489SMatthew Knepley 7664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel) 7665d71ae5a4SJacob Faibussowitsch { 76660fdc7489SMatthew Knepley PetscFunctionBegin; 76674f572ea9SToby Isaac PetscAssertPointer(ulabel, 2); 76680fdc7489SMatthew Knepley *ulabel = ul->label; 76693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 76700fdc7489SMatthew Knepley } 76710fdc7489SMatthew Knepley 7672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm) 7673d71ae5a4SJacob Faibussowitsch { 76740fdc7489SMatthew Knepley PetscInt Nl = ul->Nl, l; 76750fdc7489SMatthew Knepley 76760fdc7489SMatthew Knepley PetscFunctionBegin; 7677064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(dm, DM_CLASSID, 3); 76780fdc7489SMatthew Knepley for (l = 0; l < Nl; ++l) { 76799566063dSJacob Faibussowitsch if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l])); 76809566063dSJacob Faibussowitsch else PetscCall(DMCreateLabel(dm, ul->names[l])); 76810fdc7489SMatthew Knepley } 76820fdc7489SMatthew Knepley if (preserveOrder) { 76830fdc7489SMatthew Knepley for (l = 0; l < ul->Nl; ++l) { 76840fdc7489SMatthew Knepley const char *name; 76850fdc7489SMatthew Knepley PetscBool match; 76860fdc7489SMatthew Knepley 76879566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(dm, ul->indices[l], &name)); 76889566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, ul->names[l], &match)); 768963a3b9bcSJacob Faibussowitsch PetscCheck(match, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Label %" PetscInt_FMT " name %s does not match new name %s", l, name, ul->names[l]); 76900fdc7489SMatthew Knepley } 76910fdc7489SMatthew Knepley } 76923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 76930fdc7489SMatthew Knepley } 76940fdc7489SMatthew Knepley 7695d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value) 7696d71ae5a4SJacob Faibussowitsch { 76970fdc7489SMatthew Knepley PetscInt l; 76980fdc7489SMatthew Knepley 76990fdc7489SMatthew Knepley PetscFunctionBegin; 77000fdc7489SMatthew Knepley for (l = 0; l < ul->Nl; ++l) { 77010fdc7489SMatthew Knepley DMLabel label; 77020fdc7489SMatthew Knepley PetscInt lval = (value & ul->masks[l]) >> ul->bits[l]; 77030fdc7489SMatthew Knepley 77040fdc7489SMatthew Knepley if (lval) { 77059566063dSJacob Faibussowitsch if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label)); 77069566063dSJacob Faibussowitsch else PetscCall(DMGetLabel(dm, ul->names[l], &label)); 77079566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1])); 77080fdc7489SMatthew Knepley } 77090fdc7489SMatthew Knepley } 77103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 77110fdc7489SMatthew Knepley } 7712a8fb8f29SToby Isaac 7713a8fb8f29SToby Isaac /*@ 7714bb7acecfSBarry Smith DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement 7715bb7acecfSBarry Smith 771620f4b53cSBarry Smith Not Collective 7717a8fb8f29SToby Isaac 7718a8fb8f29SToby Isaac Input Parameter: 7719bb7acecfSBarry Smith . dm - The `DM` object 7720a8fb8f29SToby Isaac 7721a8fb8f29SToby Isaac Output Parameter: 7722bb7acecfSBarry Smith . cdm - The coarse `DM` 7723a8fb8f29SToby Isaac 7724a8fb8f29SToby Isaac Level: intermediate 7725a8fb8f29SToby Isaac 77261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()` 7727a8fb8f29SToby Isaac @*/ 7728d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm) 7729d71ae5a4SJacob Faibussowitsch { 7730a8fb8f29SToby Isaac PetscFunctionBegin; 7731a8fb8f29SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 77324f572ea9SToby Isaac PetscAssertPointer(cdm, 2); 7733a8fb8f29SToby Isaac *cdm = dm->coarseMesh; 77343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7735a8fb8f29SToby Isaac } 7736a8fb8f29SToby Isaac 7737a8fb8f29SToby Isaac /*@ 7738bb7acecfSBarry Smith DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement 7739a8fb8f29SToby Isaac 7740a8fb8f29SToby Isaac Input Parameters: 7741bb7acecfSBarry Smith + dm - The `DM` object 7742bb7acecfSBarry Smith - cdm - The coarse `DM` 7743a8fb8f29SToby Isaac 7744a8fb8f29SToby Isaac Level: intermediate 7745a8fb8f29SToby Isaac 7746bb7acecfSBarry Smith Note: 7747bb7acecfSBarry Smith Normally this is set automatically by `DMRefine()` 7748bb7acecfSBarry Smith 77491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()` 7750a8fb8f29SToby Isaac @*/ 7751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm) 7752d71ae5a4SJacob Faibussowitsch { 7753a8fb8f29SToby Isaac PetscFunctionBegin; 7754a8fb8f29SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 7755a8fb8f29SToby Isaac if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2); 775689d734beSBarry Smith if (dm == cdm) cdm = NULL; 77579566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)cdm)); 77589566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm->coarseMesh)); 7759a8fb8f29SToby Isaac dm->coarseMesh = cdm; 77603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7761a8fb8f29SToby Isaac } 7762a8fb8f29SToby Isaac 776388bdff64SToby Isaac /*@ 7764bb7acecfSBarry Smith DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening 776588bdff64SToby Isaac 776688bdff64SToby Isaac Input Parameter: 7767bb7acecfSBarry Smith . dm - The `DM` object 776888bdff64SToby Isaac 776988bdff64SToby Isaac Output Parameter: 7770bb7acecfSBarry Smith . fdm - The fine `DM` 777188bdff64SToby Isaac 777288bdff64SToby Isaac Level: intermediate 777388bdff64SToby Isaac 77741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()` 777588bdff64SToby Isaac @*/ 7776d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm) 7777d71ae5a4SJacob Faibussowitsch { 777888bdff64SToby Isaac PetscFunctionBegin; 777988bdff64SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 77804f572ea9SToby Isaac PetscAssertPointer(fdm, 2); 778188bdff64SToby Isaac *fdm = dm->fineMesh; 77823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 778388bdff64SToby Isaac } 778488bdff64SToby Isaac 778588bdff64SToby Isaac /*@ 7786bb7acecfSBarry Smith DMSetFineDM - Set the fine mesh from which this was obtained by coarsening 778788bdff64SToby Isaac 778888bdff64SToby Isaac Input Parameters: 7789bb7acecfSBarry Smith + dm - The `DM` object 7790bb7acecfSBarry Smith - fdm - The fine `DM` 779188bdff64SToby Isaac 7792bb7acecfSBarry Smith Level: developer 779388bdff64SToby Isaac 7794bb7acecfSBarry Smith Note: 7795bb7acecfSBarry Smith Normally this is set automatically by `DMCoarsen()` 7796bb7acecfSBarry Smith 77971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()` 779888bdff64SToby Isaac @*/ 7799d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm) 7800d71ae5a4SJacob Faibussowitsch { 780188bdff64SToby Isaac PetscFunctionBegin; 780288bdff64SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 780388bdff64SToby Isaac if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2); 780489d734beSBarry Smith if (dm == fdm) fdm = NULL; 78059566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)fdm)); 78069566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dm->fineMesh)); 780788bdff64SToby Isaac dm->fineMesh = fdm; 78083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 780988bdff64SToby Isaac } 781088bdff64SToby Isaac 7811a6ba4734SToby Isaac /*@C 7812bb7acecfSBarry Smith DMAddBoundary - Add a boundary condition to a model represented by a `DM` 7813a6ba4734SToby Isaac 781420f4b53cSBarry Smith Collective 7815783e2ec8SMatthew G. Knepley 7816a6ba4734SToby Isaac Input Parameters: 7817bb7acecfSBarry Smith + dm - The `DM`, with a `PetscDS` that matches the problem being constrained 7818bb7acecfSBarry Smith . type - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann) 7819a6ba4734SToby Isaac . name - The BC name 782045480ffeSMatthew G. Knepley . label - The label defining constrained points 7821bb7acecfSBarry Smith . Nv - The number of `DMLabel` values for constrained points 782245480ffeSMatthew G. Knepley . values - An array of values for constrained points 7823a6ba4734SToby Isaac . field - The field to constrain 782445480ffeSMatthew G. Knepley . Nc - The number of constrained field components (0 will constrain all fields) 7825a6ba4734SToby Isaac . comps - An array of constrained component numbers 7826a6ba4734SToby Isaac . bcFunc - A pointwise function giving boundary values 782756cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL 7828a6ba4734SToby Isaac - ctx - An optional user context for bcFunc 7829a6ba4734SToby Isaac 783045480ffeSMatthew G. Knepley Output Parameter: 783145480ffeSMatthew G. Knepley . bd - (Optional) Boundary number 783245480ffeSMatthew G. Knepley 7833a6ba4734SToby Isaac Options Database Keys: 7834a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids 7835a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components 7836a6ba4734SToby Isaac 783720f4b53cSBarry Smith Level: intermediate 783820f4b53cSBarry Smith 7839bb7acecfSBarry Smith Notes: 784037fdd005SBarry Smith Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\: 784156cf3b9cSMatthew G. Knepley 784220f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[]) 784356cf3b9cSMatthew G. Knepley 7844a4e35b19SJacob Faibussowitsch If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\: 784556cf3b9cSMatthew G. Knepley 784620f4b53cSBarry Smith .vb 784720f4b53cSBarry Smith void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux, 784820f4b53cSBarry Smith const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 784920f4b53cSBarry Smith const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 785020f4b53cSBarry Smith PetscReal time, const PetscReal x[], PetscScalar bcval[]) 785120f4b53cSBarry Smith .ve 785256cf3b9cSMatthew G. Knepley + dim - the spatial dimension 785356cf3b9cSMatthew G. Knepley . Nf - the number of fields 785456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field 785556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field 785656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point 785756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point 785856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point 785956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field 786056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field 786156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point 786256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point 786356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point 786456cf3b9cSMatthew G. Knepley . t - current time 786556cf3b9cSMatthew G. Knepley . x - coordinates of the current point 786656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters 786756cf3b9cSMatthew G. Knepley . constants - constant parameters 786856cf3b9cSMatthew G. Knepley - bcval - output values at the current point 786956cf3b9cSMatthew G. Knepley 78701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()` 7871a6ba4734SToby Isaac @*/ 7872d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], DMLabel label, PetscInt Nv, const PetscInt values[], PetscInt field, PetscInt Nc, const PetscInt comps[], void (*bcFunc)(void), void (*bcFunc_t)(void), void *ctx, PetscInt *bd) 7873d71ae5a4SJacob Faibussowitsch { 7874e5e52638SMatthew G. Knepley PetscDS ds; 7875a6ba4734SToby Isaac 7876a6ba4734SToby Isaac PetscFunctionBegin; 7877a6ba4734SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 7878783e2ec8SMatthew G. Knepley PetscValidLogicalCollectiveEnum(dm, type, 2); 787945480ffeSMatthew G. Knepley PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4); 788045480ffeSMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, Nv, 5); 788145480ffeSMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, field, 7); 788245480ffeSMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, Nc, 8); 788301a5d20dSJed Brown PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section"); 78849566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &ds)); 7885799db056SMatthew G. Knepley /* Complete label */ 7886799db056SMatthew G. Knepley if (label) { 7887799db056SMatthew G. Knepley PetscObject obj; 7888799db056SMatthew G. Knepley PetscClassId id; 7889799db056SMatthew G. Knepley 7890799db056SMatthew G. Knepley PetscCall(DMGetField(dm, field, NULL, &obj)); 7891799db056SMatthew G. Knepley PetscCall(PetscObjectGetClassId(obj, &id)); 7892799db056SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 7893799db056SMatthew G. Knepley DM plex; 7894799db056SMatthew G. Knepley 7895799db056SMatthew G. Knepley PetscCall(DMConvert(dm, DMPLEX, &plex)); 7896799db056SMatthew G. Knepley if (plex) PetscCall(DMPlexLabelComplete(plex, label)); 7897799db056SMatthew G. Knepley PetscCall(DMDestroy(&plex)); 7898799db056SMatthew G. Knepley } 7899799db056SMatthew G. Knepley } 79009566063dSJacob Faibussowitsch PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd)); 79013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7902a6ba4734SToby Isaac } 7903a6ba4734SToby Isaac 790445480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */ 7905d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm) 7906d71ae5a4SJacob Faibussowitsch { 7907e5e52638SMatthew G. Knepley PetscDS ds; 7908dff059c6SToby Isaac DMBoundary *lastnext; 7909e6f8dbb6SToby Isaac DSBoundary dsbound; 7910e6f8dbb6SToby Isaac 7911e6f8dbb6SToby Isaac PetscFunctionBegin; 79129566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &ds)); 7913e5e52638SMatthew G. Knepley dsbound = ds->boundary; 791447a1f5adSToby Isaac if (dm->boundary) { 791547a1f5adSToby Isaac DMBoundary next = dm->boundary; 791647a1f5adSToby Isaac 791747a1f5adSToby Isaac /* quick check to see if the PetscDS has changed */ 79183ba16761SJacob Faibussowitsch if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS); 791947a1f5adSToby Isaac /* the PetscDS has changed: tear down and rebuild */ 792047a1f5adSToby Isaac while (next) { 792147a1f5adSToby Isaac DMBoundary b = next; 792247a1f5adSToby Isaac 792347a1f5adSToby Isaac next = b->next; 79249566063dSJacob Faibussowitsch PetscCall(PetscFree(b)); 7925a6ba4734SToby Isaac } 792647a1f5adSToby Isaac dm->boundary = NULL; 7927a6ba4734SToby Isaac } 792847a1f5adSToby Isaac 7929dff059c6SToby Isaac lastnext = &(dm->boundary); 7930e6f8dbb6SToby Isaac while (dsbound) { 7931e6f8dbb6SToby Isaac DMBoundary dmbound; 7932e6f8dbb6SToby Isaac 79339566063dSJacob Faibussowitsch PetscCall(PetscNew(&dmbound)); 7934e6f8dbb6SToby Isaac dmbound->dsboundary = dsbound; 793545480ffeSMatthew G. Knepley dmbound->label = dsbound->label; 793647a1f5adSToby Isaac /* push on the back instead of the front so that it is in the same order as in the PetscDS */ 7937dff059c6SToby Isaac *lastnext = dmbound; 7938dff059c6SToby Isaac lastnext = &(dmbound->next); 7939dff059c6SToby Isaac dsbound = dsbound->next; 7940a6ba4734SToby Isaac } 79413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7942a6ba4734SToby Isaac } 7943a6ba4734SToby Isaac 7944bb7acecfSBarry Smith /* TODO: missing manual page */ 7945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd) 7946d71ae5a4SJacob Faibussowitsch { 7947b95f2879SToby Isaac DMBoundary b; 7948a6ba4734SToby Isaac 7949a6ba4734SToby Isaac PetscFunctionBegin; 7950a6ba4734SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 79514f572ea9SToby Isaac PetscAssertPointer(isBd, 3); 7952a6ba4734SToby Isaac *isBd = PETSC_FALSE; 79539566063dSJacob Faibussowitsch PetscCall(DMPopulateBoundary(dm)); 7954b95f2879SToby Isaac b = dm->boundary; 7955a6ba4734SToby Isaac while (b && !(*isBd)) { 7956e6f8dbb6SToby Isaac DMLabel label = b->label; 7957e6f8dbb6SToby Isaac DSBoundary dsb = b->dsboundary; 7958a6ba4734SToby Isaac PetscInt i; 7959a6ba4734SToby Isaac 796045480ffeSMatthew G. Knepley if (label) { 79619566063dSJacob Faibussowitsch for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd)); 7962a6ba4734SToby Isaac } 7963a6ba4734SToby Isaac b = b->next; 7964a6ba4734SToby Isaac } 79653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7966a6ba4734SToby Isaac } 79674d6f44ffSToby Isaac 79684d6f44ffSToby Isaac /*@C 7969bb7acecfSBarry Smith DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector. 7970a6e0b375SMatthew G. Knepley 797120f4b53cSBarry Smith Collective 79724d6f44ffSToby Isaac 79734d6f44ffSToby Isaac Input Parameters: 7974bb7acecfSBarry Smith + dm - The `DM` 79750709b2feSToby Isaac . time - The time 79764d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field 79774d6f44ffSToby Isaac . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 79784d6f44ffSToby Isaac - mode - The insertion mode for values 79794d6f44ffSToby Isaac 79804d6f44ffSToby Isaac Output Parameter: 79814d6f44ffSToby Isaac . X - vector 79824d6f44ffSToby Isaac 798320f4b53cSBarry Smith Calling sequence of `funcs`: 79844d6f44ffSToby Isaac + dim - The spatial dimension 79858ec8862eSJed Brown . time - The time at which to sample 79864d6f44ffSToby Isaac . x - The coordinates 798777b739a6SMatthew Knepley . Nc - The number of components 79884d6f44ffSToby Isaac . u - The output field values 79894d6f44ffSToby Isaac - ctx - optional user-defined function context 79904d6f44ffSToby Isaac 79914d6f44ffSToby Isaac Level: developer 79924d6f44ffSToby Isaac 7993bb7acecfSBarry Smith Developer Notes: 7994bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 7995bb7acecfSBarry Smith 7996bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 7997bb7acecfSBarry Smith 79981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()` 79994d6f44ffSToby Isaac @*/ 8000a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X) 8001d71ae5a4SJacob Faibussowitsch { 80024d6f44ffSToby Isaac Vec localX; 80034d6f44ffSToby Isaac 80044d6f44ffSToby Isaac PetscFunctionBegin; 80054d6f44ffSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8006708be2fdSJed Brown PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0)); 80079566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(dm, &localX)); 8008f60fa741SMatthew G. Knepley PetscCall(VecSet(localX, 0.)); 80099566063dSJacob Faibussowitsch PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX)); 80109566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X)); 80119566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X)); 80129566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(dm, &localX)); 8013708be2fdSJed Brown PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0)); 80143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 80154d6f44ffSToby Isaac } 80164d6f44ffSToby Isaac 8017a6e0b375SMatthew G. Knepley /*@C 8018bb7acecfSBarry Smith DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector. 8019a6e0b375SMatthew G. Knepley 802020f4b53cSBarry Smith Not Collective 8021a6e0b375SMatthew G. Knepley 8022a6e0b375SMatthew G. Knepley Input Parameters: 8023bb7acecfSBarry Smith + dm - The `DM` 8024a6e0b375SMatthew G. Knepley . time - The time 8025a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field 8026a6e0b375SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 8027a6e0b375SMatthew G. Knepley - mode - The insertion mode for values 8028a6e0b375SMatthew G. Knepley 8029a6e0b375SMatthew G. Knepley Output Parameter: 8030a6e0b375SMatthew G. Knepley . localX - vector 8031a6e0b375SMatthew G. Knepley 803220f4b53cSBarry Smith Calling sequence of `funcs`: 8033a6e0b375SMatthew G. Knepley + dim - The spatial dimension 8034a4e35b19SJacob Faibussowitsch . time - The current timestep 8035a6e0b375SMatthew G. Knepley . x - The coordinates 803677b739a6SMatthew Knepley . Nc - The number of components 8037a6e0b375SMatthew G. Knepley . u - The output field values 8038a6e0b375SMatthew G. Knepley - ctx - optional user-defined function context 8039a6e0b375SMatthew G. Knepley 8040a6e0b375SMatthew G. Knepley Level: developer 8041a6e0b375SMatthew G. Knepley 8042bb7acecfSBarry Smith Developer Notes: 8043bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8044bb7acecfSBarry Smith 8045bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8046bb7acecfSBarry Smith 80471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()` 8048a6e0b375SMatthew G. Knepley @*/ 8049a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX) 8050d71ae5a4SJacob Faibussowitsch { 80514d6f44ffSToby Isaac PetscFunctionBegin; 80524d6f44ffSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8053064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localX, VEC_CLASSID, 6); 80549566063dSJacob Faibussowitsch PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX)); 80553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 80564d6f44ffSToby Isaac } 80574d6f44ffSToby Isaac 8058a6e0b375SMatthew G. Knepley /*@C 8059bb7acecfSBarry Smith DMProjectFunctionLabel - This projects the given function into the function space provided by the `DM`, putting the coefficients in a global vector, setting values only for points in the given label. 8060a6e0b375SMatthew G. Knepley 806120f4b53cSBarry Smith Collective 8062a6e0b375SMatthew G. Knepley 8063a6e0b375SMatthew G. Knepley Input Parameters: 8064bb7acecfSBarry Smith + dm - The `DM` 8065a6e0b375SMatthew G. Knepley . time - The time 8066a4e35b19SJacob Faibussowitsch . numIds - The number of ids 8067a4e35b19SJacob Faibussowitsch . ids - The ids 8068a4e35b19SJacob Faibussowitsch . Nc - The number of components 8069a4e35b19SJacob Faibussowitsch . comps - The components 8070bb7acecfSBarry Smith . label - The `DMLabel` selecting the portion of the mesh for projection 8071a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field 8072bb7acecfSBarry Smith . ctxs - Optional array of contexts to pass to each coordinate function. ctxs may be null. 8073a6e0b375SMatthew G. Knepley - mode - The insertion mode for values 8074a6e0b375SMatthew G. Knepley 8075a6e0b375SMatthew G. Knepley Output Parameter: 8076a6e0b375SMatthew G. Knepley . X - vector 8077a6e0b375SMatthew G. Knepley 807820f4b53cSBarry Smith Calling sequence of `funcs`: 8079a6e0b375SMatthew G. Knepley + dim - The spatial dimension 8080a4e35b19SJacob Faibussowitsch . time - The current timestep 8081a6e0b375SMatthew G. Knepley . x - The coordinates 808277b739a6SMatthew Knepley . Nc - The number of components 8083a6e0b375SMatthew G. Knepley . u - The output field values 8084a6e0b375SMatthew G. Knepley - ctx - optional user-defined function context 8085a6e0b375SMatthew G. Knepley 8086a6e0b375SMatthew G. Knepley Level: developer 8087a6e0b375SMatthew G. Knepley 8088bb7acecfSBarry Smith Developer Notes: 8089bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8090bb7acecfSBarry Smith 8091bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8092bb7acecfSBarry Smith 80931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()` 8094a6e0b375SMatthew G. Knepley @*/ 8095a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec X) 8096d71ae5a4SJacob Faibussowitsch { 80972c53366bSMatthew G. Knepley Vec localX; 80982c53366bSMatthew G. Knepley 80992c53366bSMatthew G. Knepley PetscFunctionBegin; 81002c53366bSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 81019566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(dm, &localX)); 8102f60fa741SMatthew G. Knepley PetscCall(VecSet(localX, 0.)); 81039566063dSJacob Faibussowitsch PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX)); 81049566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X)); 81059566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X)); 81069566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(dm, &localX)); 81073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 81082c53366bSMatthew G. Knepley } 81092c53366bSMatthew G. Knepley 8110a6e0b375SMatthew G. Knepley /*@C 8111bb7acecfSBarry Smith DMProjectFunctionLabelLocal - This projects the given function into the function space provided by the `DM`, putting the coefficients in a local vector, setting values only for points in the given label. 8112a6e0b375SMatthew G. Knepley 811320f4b53cSBarry Smith Not Collective 8114a6e0b375SMatthew G. Knepley 8115a6e0b375SMatthew G. Knepley Input Parameters: 8116bb7acecfSBarry Smith + dm - The `DM` 8117a6e0b375SMatthew G. Knepley . time - The time 8118bb7acecfSBarry Smith . label - The `DMLabel` selecting the portion of the mesh for projection 8119a4e35b19SJacob Faibussowitsch . numIds - The number of ids 8120a4e35b19SJacob Faibussowitsch . ids - The ids 8121a4e35b19SJacob Faibussowitsch . Nc - The number of components 8122a4e35b19SJacob Faibussowitsch . comps - The components 8123a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field 8124a6e0b375SMatthew G. Knepley . ctxs - Optional array of contexts to pass to each coordinate function. ctxs itself may be null. 8125a6e0b375SMatthew G. Knepley - mode - The insertion mode for values 8126a6e0b375SMatthew G. Knepley 8127a6e0b375SMatthew G. Knepley Output Parameter: 8128a6e0b375SMatthew G. Knepley . localX - vector 8129a6e0b375SMatthew G. Knepley 813020f4b53cSBarry Smith Calling sequence of `funcs`: 8131a6e0b375SMatthew G. Knepley + dim - The spatial dimension 8132a4e35b19SJacob Faibussowitsch . time - The current time 8133a6e0b375SMatthew G. Knepley . x - The coordinates 813477b739a6SMatthew Knepley . Nc - The number of components 8135a6e0b375SMatthew G. Knepley . u - The output field values 8136a6e0b375SMatthew G. Knepley - ctx - optional user-defined function context 8137a6e0b375SMatthew G. Knepley 8138a6e0b375SMatthew G. Knepley Level: developer 8139a6e0b375SMatthew G. Knepley 8140bb7acecfSBarry Smith Developer Notes: 8141bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8142bb7acecfSBarry Smith 8143bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8144bb7acecfSBarry Smith 81451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()` 8146a6e0b375SMatthew G. Knepley @*/ 8147a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx), void **ctxs, InsertMode mode, Vec localX) 8148d71ae5a4SJacob Faibussowitsch { 81494d6f44ffSToby Isaac PetscFunctionBegin; 81504d6f44ffSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8151064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localX, VEC_CLASSID, 11); 81529566063dSJacob Faibussowitsch PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX)); 81533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 81544d6f44ffSToby Isaac } 81552716604bSToby Isaac 8156a6e0b375SMatthew G. Knepley /*@C 8157bb7acecfSBarry Smith DMProjectFieldLocal - This projects the given function of the input fields into the function space provided by the `DM`, putting the coefficients in a local vector. 8158a6e0b375SMatthew G. Knepley 815920f4b53cSBarry Smith Not Collective 8160a6e0b375SMatthew G. Knepley 8161a6e0b375SMatthew G. Knepley Input Parameters: 8162bb7acecfSBarry Smith + dm - The `DM` 8163a6e0b375SMatthew G. Knepley . time - The time 816420f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates 8165a6e0b375SMatthew G. Knepley . funcs - The functions to evaluate, one per field 8166a6e0b375SMatthew G. Knepley - mode - The insertion mode for values 8167a6e0b375SMatthew G. Knepley 8168a6e0b375SMatthew G. Knepley Output Parameter: 8169a6e0b375SMatthew G. Knepley . localX - The output vector 8170a6e0b375SMatthew G. Knepley 817120f4b53cSBarry Smith Calling sequence of `funcs`: 8172a6e0b375SMatthew G. Knepley + dim - The spatial dimension 8173a6e0b375SMatthew G. Knepley . Nf - The number of input fields 8174a6e0b375SMatthew G. Knepley . NfAux - The number of input auxiliary fields 8175a6e0b375SMatthew G. Knepley . uOff - The offset of each field in u[] 8176a6e0b375SMatthew G. Knepley . uOff_x - The offset of each field in u_x[] 8177a6e0b375SMatthew G. Knepley . u - The field values at this point in space 8178a6e0b375SMatthew G. Knepley . u_t - The field time derivative at this point in space (or NULL) 8179a6e0b375SMatthew G. Knepley . u_x - The field derivatives at this point in space 8180a6e0b375SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 8181a6e0b375SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 8182a6e0b375SMatthew G. Knepley . a - The auxiliary field values at this point in space 8183a6e0b375SMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 8184a6e0b375SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 8185a6e0b375SMatthew G. Knepley . t - The current time 8186a6e0b375SMatthew G. Knepley . x - The coordinates of this point 8187a6e0b375SMatthew G. Knepley . numConstants - The number of constants 8188a6e0b375SMatthew G. Knepley . constants - The value of each constant 8189a6e0b375SMatthew G. Knepley - f - The value of the function at this point in space 8190a6e0b375SMatthew G. Knepley 8191bb7acecfSBarry Smith Note: 8192bb7acecfSBarry Smith There are three different `DM`s that potentially interact in this function. The output `DM`, dm, specifies the layout of the values calculates by funcs. 8193bb7acecfSBarry Smith The input `DM`, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or 8194bb7acecfSBarry Smith a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary `DM`, attached to the 8195a6e0b375SMatthew G. Knepley auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations. 8196a6e0b375SMatthew G. Knepley 8197a6e0b375SMatthew G. Knepley Level: intermediate 8198a6e0b375SMatthew G. Knepley 8199bb7acecfSBarry Smith Developer Notes: 8200bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8201bb7acecfSBarry Smith 8202bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8203bb7acecfSBarry Smith 8204a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, 8205a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()` 8206a6e0b375SMatthew G. Knepley @*/ 8207a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX) 8208d71ae5a4SJacob Faibussowitsch { 82098c6c5593SMatthew G. Knepley PetscFunctionBegin; 82108c6c5593SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8211eb8f539aSJed Brown if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3); 82128c6c5593SMatthew G. Knepley PetscValidHeaderSpecific(localX, VEC_CLASSID, 6); 82139566063dSJacob Faibussowitsch PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX)); 82143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 82158c6c5593SMatthew G. Knepley } 82168c6c5593SMatthew G. Knepley 8217a6e0b375SMatthew G. Knepley /*@C 8218a6e0b375SMatthew G. Knepley DMProjectFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain specified by the label. 8219a6e0b375SMatthew G. Knepley 822020f4b53cSBarry Smith Not Collective 8221a6e0b375SMatthew G. Knepley 8222a6e0b375SMatthew G. Knepley Input Parameters: 8223bb7acecfSBarry Smith + dm - The `DM` 8224a6e0b375SMatthew G. Knepley . time - The time 8225bb7acecfSBarry Smith . label - The `DMLabel` marking the portion of the domain to output 8226a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use 8227a6e0b375SMatthew G. Knepley . ids - The label ids to use for marking 8228bb7acecfSBarry Smith . Nc - The number of components to set in the output, or `PETSC_DETERMINE` for all components 822920f4b53cSBarry Smith . comps - The components to set in the output, or `NULL` for all components 8230a6e0b375SMatthew G. Knepley . localU - The input field vector 8231a6e0b375SMatthew G. Knepley . funcs - The functions to evaluate, one per field 8232a6e0b375SMatthew G. Knepley - mode - The insertion mode for values 8233a6e0b375SMatthew G. Knepley 8234a6e0b375SMatthew G. Knepley Output Parameter: 8235a6e0b375SMatthew G. Knepley . localX - The output vector 8236a6e0b375SMatthew G. Knepley 823720f4b53cSBarry Smith Calling sequence of `funcs`: 8238a6e0b375SMatthew G. Knepley + dim - The spatial dimension 8239a6e0b375SMatthew G. Knepley . Nf - The number of input fields 8240a6e0b375SMatthew G. Knepley . NfAux - The number of input auxiliary fields 8241a6e0b375SMatthew G. Knepley . uOff - The offset of each field in u[] 8242a6e0b375SMatthew G. Knepley . uOff_x - The offset of each field in u_x[] 8243a6e0b375SMatthew G. Knepley . u - The field values at this point in space 8244a6e0b375SMatthew G. Knepley . u_t - The field time derivative at this point in space (or NULL) 8245a6e0b375SMatthew G. Knepley . u_x - The field derivatives at this point in space 8246a6e0b375SMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 8247a6e0b375SMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 8248a6e0b375SMatthew G. Knepley . a - The auxiliary field values at this point in space 8249a6e0b375SMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 8250a6e0b375SMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 8251a6e0b375SMatthew G. Knepley . t - The current time 8252a6e0b375SMatthew G. Knepley . x - The coordinates of this point 8253a6e0b375SMatthew G. Knepley . numConstants - The number of constants 8254a6e0b375SMatthew G. Knepley . constants - The value of each constant 8255a6e0b375SMatthew G. Knepley - f - The value of the function at this point in space 8256a6e0b375SMatthew G. Knepley 8257bb7acecfSBarry Smith Note: 8258bb7acecfSBarry Smith There are three different `DM`s that potentially interact in this function. The output `DM`, dm, specifies the layout of the values calculates by funcs. 8259bb7acecfSBarry Smith The input `DM`, attached to localU, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or 8260bb7acecfSBarry Smith a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary `DM`, attached to the 8261a6e0b375SMatthew G. Knepley auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations. 8262a6e0b375SMatthew G. Knepley 8263a6e0b375SMatthew G. Knepley Level: intermediate 8264a6e0b375SMatthew G. Knepley 8265bb7acecfSBarry Smith Developer Notes: 8266bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8267bb7acecfSBarry Smith 8268bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8269bb7acecfSBarry Smith 82701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()` 8271a6e0b375SMatthew G. Knepley @*/ 8272a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX) 8273d71ae5a4SJacob Faibussowitsch { 82748c6c5593SMatthew G. Knepley PetscFunctionBegin; 82758c6c5593SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8276064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localU, VEC_CLASSID, 8); 8277064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localX, VEC_CLASSID, 11); 82789566063dSJacob Faibussowitsch PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX)); 82793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 82808c6c5593SMatthew G. Knepley } 82818c6c5593SMatthew G. Knepley 82822716604bSToby Isaac /*@C 8283d29d7c6eSMatthew G. Knepley DMProjectFieldLabel - This projects the given function of the input fields into the function space provided, putting the coefficients in a global vector, calculating only over the portion of the domain specified by the label. 8284d29d7c6eSMatthew G. Knepley 828520f4b53cSBarry Smith Not Collective 8286d29d7c6eSMatthew G. Knepley 8287d29d7c6eSMatthew G. Knepley Input Parameters: 8288bb7acecfSBarry Smith + dm - The `DM` 8289d29d7c6eSMatthew G. Knepley . time - The time 8290bb7acecfSBarry Smith . label - The `DMLabel` marking the portion of the domain to output 8291d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use 8292d29d7c6eSMatthew G. Knepley . ids - The label ids to use for marking 8293bb7acecfSBarry Smith . Nc - The number of components to set in the output, or `PETSC_DETERMINE` for all components 829420f4b53cSBarry Smith . comps - The components to set in the output, or `NULL` for all components 8295d29d7c6eSMatthew G. Knepley . U - The input field vector 8296d29d7c6eSMatthew G. Knepley . funcs - The functions to evaluate, one per field 8297d29d7c6eSMatthew G. Knepley - mode - The insertion mode for values 8298d29d7c6eSMatthew G. Knepley 8299d29d7c6eSMatthew G. Knepley Output Parameter: 8300d29d7c6eSMatthew G. Knepley . X - The output vector 8301d29d7c6eSMatthew G. Knepley 830220f4b53cSBarry Smith Calling sequence of `funcs`: 8303d29d7c6eSMatthew G. Knepley + dim - The spatial dimension 8304d29d7c6eSMatthew G. Knepley . Nf - The number of input fields 8305d29d7c6eSMatthew G. Knepley . NfAux - The number of input auxiliary fields 8306d29d7c6eSMatthew G. Knepley . uOff - The offset of each field in u[] 8307d29d7c6eSMatthew G. Knepley . uOff_x - The offset of each field in u_x[] 8308d29d7c6eSMatthew G. Knepley . u - The field values at this point in space 8309d29d7c6eSMatthew G. Knepley . u_t - The field time derivative at this point in space (or NULL) 8310d29d7c6eSMatthew G. Knepley . u_x - The field derivatives at this point in space 8311d29d7c6eSMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 8312d29d7c6eSMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 8313d29d7c6eSMatthew G. Knepley . a - The auxiliary field values at this point in space 8314d29d7c6eSMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 8315d29d7c6eSMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 8316d29d7c6eSMatthew G. Knepley . t - The current time 8317d29d7c6eSMatthew G. Knepley . x - The coordinates of this point 8318d29d7c6eSMatthew G. Knepley . numConstants - The number of constants 8319d29d7c6eSMatthew G. Knepley . constants - The value of each constant 8320d29d7c6eSMatthew G. Knepley - f - The value of the function at this point in space 8321d29d7c6eSMatthew G. Knepley 8322bb7acecfSBarry Smith Note: 8323bb7acecfSBarry Smith There are three different `DM`s that potentially interact in this function. The output `DM`, dm, specifies the layout of the values calculates by funcs. 8324bb7acecfSBarry Smith The input `DM`, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or 8325bb7acecfSBarry Smith a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary `DM`, attached to the 8326d29d7c6eSMatthew G. Knepley auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations. 8327d29d7c6eSMatthew G. Knepley 8328d29d7c6eSMatthew G. Knepley Level: intermediate 8329d29d7c6eSMatthew G. Knepley 8330bb7acecfSBarry Smith Developer Notes: 8331bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8332bb7acecfSBarry Smith 8333bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8334bb7acecfSBarry Smith 83351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()` 8336d29d7c6eSMatthew G. Knepley @*/ 8337a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec X) 8338d71ae5a4SJacob Faibussowitsch { 8339d29d7c6eSMatthew G. Knepley DM dmIn; 8340d29d7c6eSMatthew G. Knepley Vec localU, localX; 8341d29d7c6eSMatthew G. Knepley 8342d29d7c6eSMatthew G. Knepley PetscFunctionBegin; 8343d29d7c6eSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8344d29d7c6eSMatthew G. Knepley PetscCall(VecGetDM(U, &dmIn)); 8345d29d7c6eSMatthew G. Knepley PetscCall(DMGetLocalVector(dmIn, &localU)); 8346d29d7c6eSMatthew G. Knepley PetscCall(DMGetLocalVector(dm, &localX)); 8347f60fa741SMatthew G. Knepley PetscCall(VecSet(localX, 0.)); 834872fc1ce5SMatthew G. Knepley PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU)); 834972fc1ce5SMatthew G. Knepley PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU)); 8350d29d7c6eSMatthew G. Knepley PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX)); 8351d29d7c6eSMatthew G. Knepley PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X)); 8352d29d7c6eSMatthew G. Knepley PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X)); 8353d29d7c6eSMatthew G. Knepley PetscCall(DMRestoreLocalVector(dm, &localX)); 8354d29d7c6eSMatthew G. Knepley PetscCall(DMRestoreLocalVector(dmIn, &localU)); 83553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8356d29d7c6eSMatthew G. Knepley } 8357d29d7c6eSMatthew G. Knepley 8358d29d7c6eSMatthew G. Knepley /*@C 8359ece3a9fcSMatthew G. Knepley DMProjectBdFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain boundary specified by the label. 8360ece3a9fcSMatthew G. Knepley 836120f4b53cSBarry Smith Not Collective 8362ece3a9fcSMatthew G. Knepley 8363ece3a9fcSMatthew G. Knepley Input Parameters: 8364bb7acecfSBarry Smith + dm - The `DM` 8365ece3a9fcSMatthew G. Knepley . time - The time 8366bb7acecfSBarry Smith . label - The `DMLabel` marking the portion of the domain boundary to output 8367ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use 8368ece3a9fcSMatthew G. Knepley . ids - The label ids to use for marking 8369bb7acecfSBarry Smith . Nc - The number of components to set in the output, or `PETSC_DETERMINE` for all components 837020f4b53cSBarry Smith . comps - The components to set in the output, or `NULL` for all components 8371ece3a9fcSMatthew G. Knepley . localU - The input field vector 8372ece3a9fcSMatthew G. Knepley . funcs - The functions to evaluate, one per field 8373ece3a9fcSMatthew G. Knepley - mode - The insertion mode for values 8374ece3a9fcSMatthew G. Knepley 8375ece3a9fcSMatthew G. Knepley Output Parameter: 8376ece3a9fcSMatthew G. Knepley . localX - The output vector 8377ece3a9fcSMatthew G. Knepley 837820f4b53cSBarry Smith Calling sequence of `funcs`: 8379ece3a9fcSMatthew G. Knepley + dim - The spatial dimension 8380ece3a9fcSMatthew G. Knepley . Nf - The number of input fields 8381ece3a9fcSMatthew G. Knepley . NfAux - The number of input auxiliary fields 8382ece3a9fcSMatthew G. Knepley . uOff - The offset of each field in u[] 8383ece3a9fcSMatthew G. Knepley . uOff_x - The offset of each field in u_x[] 8384ece3a9fcSMatthew G. Knepley . u - The field values at this point in space 8385ece3a9fcSMatthew G. Knepley . u_t - The field time derivative at this point in space (or NULL) 8386ece3a9fcSMatthew G. Knepley . u_x - The field derivatives at this point in space 8387ece3a9fcSMatthew G. Knepley . aOff - The offset of each auxiliary field in u[] 8388ece3a9fcSMatthew G. Knepley . aOff_x - The offset of each auxiliary field in u_x[] 8389ece3a9fcSMatthew G. Knepley . a - The auxiliary field values at this point in space 8390ece3a9fcSMatthew G. Knepley . a_t - The auxiliary field time derivative at this point in space (or NULL) 8391ece3a9fcSMatthew G. Knepley . a_x - The auxiliary field derivatives at this point in space 8392ece3a9fcSMatthew G. Knepley . t - The current time 8393ece3a9fcSMatthew G. Knepley . x - The coordinates of this point 8394ece3a9fcSMatthew G. Knepley . n - The face normal 8395ece3a9fcSMatthew G. Knepley . numConstants - The number of constants 8396ece3a9fcSMatthew G. Knepley . constants - The value of each constant 8397ece3a9fcSMatthew G. Knepley - f - The value of the function at this point in space 8398ece3a9fcSMatthew G. Knepley 8399ece3a9fcSMatthew G. Knepley Note: 8400bb7acecfSBarry Smith There are three different `DM`s that potentially interact in this function. The output `DM`, dm, specifies the layout of the values calculates by funcs. 8401bb7acecfSBarry Smith The input `DM`, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or 8402bb7acecfSBarry Smith a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary `DM`, attached to the 8403ece3a9fcSMatthew G. Knepley auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations. 8404ece3a9fcSMatthew G. Knepley 8405ece3a9fcSMatthew G. Knepley Level: intermediate 8406ece3a9fcSMatthew G. Knepley 8407bb7acecfSBarry Smith Developer Notes: 8408bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8409bb7acecfSBarry Smith 8410bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8411bb7acecfSBarry Smith 84121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()` 8413ece3a9fcSMatthew G. Knepley @*/ 8414a4e35b19SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]), InsertMode mode, Vec localX) 8415d71ae5a4SJacob Faibussowitsch { 8416ece3a9fcSMatthew G. Knepley PetscFunctionBegin; 8417ece3a9fcSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8418064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localU, VEC_CLASSID, 8); 8419064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(localX, VEC_CLASSID, 11); 84209566063dSJacob Faibussowitsch PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX)); 84213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8422ece3a9fcSMatthew G. Knepley } 8423ece3a9fcSMatthew G. Knepley 8424ece3a9fcSMatthew G. Knepley /*@C 84252716604bSToby Isaac DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h. 84262716604bSToby Isaac 842720f4b53cSBarry Smith Collective 8428bb7acecfSBarry Smith 84292716604bSToby Isaac Input Parameters: 8430bb7acecfSBarry Smith + dm - The `DM` 84310709b2feSToby Isaac . time - The time 84322716604bSToby Isaac . funcs - The functions to evaluate for each field component 84332716604bSToby Isaac . ctxs - Optional array of contexts to pass to each function, or NULL. 8434574a98acSMatthew G. Knepley - X - The coefficient vector u_h, a global vector 84352716604bSToby Isaac 84362716604bSToby Isaac Output Parameter: 84372716604bSToby Isaac . diff - The diff ||u - u_h||_2 84382716604bSToby Isaac 84392716604bSToby Isaac Level: developer 84402716604bSToby Isaac 8441bb7acecfSBarry Smith Developer Notes: 8442bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8443bb7acecfSBarry Smith 8444bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8445bb7acecfSBarry Smith 84461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()` 84472716604bSToby Isaac @*/ 8448d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff) 8449d71ae5a4SJacob Faibussowitsch { 84502716604bSToby Isaac PetscFunctionBegin; 84512716604bSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8452b698f381SToby Isaac PetscValidHeaderSpecific(X, VEC_CLASSID, 5); 84539566063dSJacob Faibussowitsch PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff)); 84543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 84552716604bSToby Isaac } 8456b698f381SToby Isaac 8457b698f381SToby Isaac /*@C 8458b698f381SToby Isaac DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h. 8459b698f381SToby Isaac 846020f4b53cSBarry Smith Collective 8461d083f849SBarry Smith 8462b698f381SToby Isaac Input Parameters: 8463bb7acecfSBarry Smith + dm - The `DM` 8464a4e35b19SJacob Faibussowitsch . time - The time 8465b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component 8466b698f381SToby Isaac . ctxs - Optional array of contexts to pass to each function, or NULL. 8467574a98acSMatthew G. Knepley . X - The coefficient vector u_h, a global vector 8468b698f381SToby Isaac - n - The vector to project along 8469b698f381SToby Isaac 8470b698f381SToby Isaac Output Parameter: 8471b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2 8472b698f381SToby Isaac 8473b698f381SToby Isaac Level: developer 8474b698f381SToby Isaac 8475bb7acecfSBarry Smith Developer Notes: 8476bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8477bb7acecfSBarry Smith 8478bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8479bb7acecfSBarry Smith 84801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()` 8481b698f381SToby Isaac @*/ 8482d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff) 8483d71ae5a4SJacob Faibussowitsch { 8484b698f381SToby Isaac PetscFunctionBegin; 8485b698f381SToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8486b698f381SToby Isaac PetscValidHeaderSpecific(X, VEC_CLASSID, 5); 84879566063dSJacob Faibussowitsch PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff)); 84883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8489b698f381SToby Isaac } 8490b698f381SToby Isaac 84912a16baeaSToby Isaac /*@C 84922a16baeaSToby Isaac DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components. 84932a16baeaSToby Isaac 849420f4b53cSBarry Smith Collective 8495d083f849SBarry Smith 84962a16baeaSToby Isaac Input Parameters: 8497bb7acecfSBarry Smith + dm - The `DM` 84982a16baeaSToby Isaac . time - The time 84992a16baeaSToby Isaac . funcs - The functions to evaluate for each field component 85002a16baeaSToby Isaac . ctxs - Optional array of contexts to pass to each function, or NULL. 8501574a98acSMatthew G. Knepley - X - The coefficient vector u_h, a global vector 85022a16baeaSToby Isaac 85032a16baeaSToby Isaac Output Parameter: 85042a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2 85052a16baeaSToby Isaac 85062a16baeaSToby Isaac Level: developer 85072a16baeaSToby Isaac 8508bb7acecfSBarry Smith Developer Notes: 8509bb7acecfSBarry Smith This API is specific to only particular usage of `DM` 8510bb7acecfSBarry Smith 8511bb7acecfSBarry Smith The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation. 8512bb7acecfSBarry Smith 851342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()` 85142a16baeaSToby Isaac @*/ 8515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[]) 8516d71ae5a4SJacob Faibussowitsch { 85172a16baeaSToby Isaac PetscFunctionBegin; 85182a16baeaSToby Isaac PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 85192a16baeaSToby Isaac PetscValidHeaderSpecific(X, VEC_CLASSID, 5); 85209566063dSJacob Faibussowitsch PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff)); 85213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 85222a16baeaSToby Isaac } 85232a16baeaSToby Isaac 8524df0b854cSToby Isaac /*@C 8525bb7acecfSBarry Smith DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors 8526502a2867SDave May 8527502a2867SDave May Not Collective 8528502a2867SDave May 8529502a2867SDave May Input Parameter: 8530bb7acecfSBarry Smith . dm - The `DM` 8531502a2867SDave May 85320a19bb7dSprj- Output Parameters: 85330a19bb7dSprj- + nranks - the number of neighbours 85340a19bb7dSprj- - ranks - the neighbors ranks 8535502a2867SDave May 85369bdbcad8SBarry Smith Level: beginner 85379bdbcad8SBarry Smith 8538bb7acecfSBarry Smith Note: 8539bb7acecfSBarry Smith Do not free the array, it is freed when the `DM` is destroyed. 8540502a2867SDave May 85411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()` 8542502a2867SDave May @*/ 8543d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[]) 8544d71ae5a4SJacob Faibussowitsch { 8545502a2867SDave May PetscFunctionBegin; 8546502a2867SDave May PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 85479566063dSJacob Faibussowitsch PetscCall((dm->ops->getneighbors)(dm, nranks, ranks)); 85483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8549502a2867SDave May } 8550502a2867SDave May 8551531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */ 8552531c7667SBarry Smith 8553531c7667SBarry Smith /* 8554531c7667SBarry Smith Converts the input vector to a ghosted vector and then calls the standard coloring code. 85552b6f951bSStefano Zampini This must be a different function because it requires DM which is not defined in the Mat library 8556531c7667SBarry Smith */ 855766976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx) 8558d71ae5a4SJacob Faibussowitsch { 8559531c7667SBarry Smith PetscFunctionBegin; 8560531c7667SBarry Smith if (coloring->ctype == IS_COLORING_LOCAL) { 8561531c7667SBarry Smith Vec x1local; 8562531c7667SBarry Smith DM dm; 85639566063dSJacob Faibussowitsch PetscCall(MatGetDM(J, &dm)); 85647a8be351SBarry Smith PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM"); 85659566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(dm, &x1local)); 85669566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local)); 85679566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local)); 8568531c7667SBarry Smith x1 = x1local; 8569531c7667SBarry Smith } 85709566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx)); 8571531c7667SBarry Smith if (coloring->ctype == IS_COLORING_LOCAL) { 8572531c7667SBarry Smith DM dm; 85739566063dSJacob Faibussowitsch PetscCall(MatGetDM(J, &dm)); 85749566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(dm, &x1)); 8575531c7667SBarry Smith } 85763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8577531c7667SBarry Smith } 8578531c7667SBarry Smith 8579531c7667SBarry Smith /*@ 8580bb7acecfSBarry Smith MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring 8581531c7667SBarry Smith 8582a4e35b19SJacob Faibussowitsch Input Parameters: 8583a4e35b19SJacob Faibussowitsch + coloring - The matrix to get the `DM` from 8584a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object 8585531c7667SBarry Smith 85869bdbcad8SBarry Smith Level: advanced 85879bdbcad8SBarry Smith 858860225df5SJacob Faibussowitsch Developer Notes: 8589bb7acecfSBarry Smith this routine exists because the PETSc `Mat` library does not know about the `DM` objects 8590531c7667SBarry Smith 85911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType` 8592531c7667SBarry Smith @*/ 8593d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring) 8594d71ae5a4SJacob Faibussowitsch { 8595531c7667SBarry Smith PetscFunctionBegin; 8596531c7667SBarry Smith coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM; 85973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8598531c7667SBarry Smith } 85998320bc6fSPatrick Sanan 86008320bc6fSPatrick Sanan /*@ 8601bb7acecfSBarry Smith DMGetCompatibility - determine if two `DM`s are compatible 86028320bc6fSPatrick Sanan 86038320bc6fSPatrick Sanan Collective 86048320bc6fSPatrick Sanan 86058320bc6fSPatrick Sanan Input Parameters: 8606bb7acecfSBarry Smith + dm1 - the first `DM` 8607bb7acecfSBarry Smith - dm2 - the second `DM` 86088320bc6fSPatrick Sanan 86098320bc6fSPatrick Sanan Output Parameters: 8610bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible 8611bb7acecfSBarry Smith - set - whether or not the compatible value was actually determined and set 86128320bc6fSPatrick Sanan 861320f4b53cSBarry Smith Level: advanced 861420f4b53cSBarry Smith 86158320bc6fSPatrick Sanan Notes: 8616bb7acecfSBarry Smith Two `DM`s are deemed compatible if they represent the same parallel decomposition 86173d862458SPatrick Sanan of the same topology. This implies that the section (field data) on one 86188320bc6fSPatrick Sanan "makes sense" with respect to the topology and parallel decomposition of the other. 8619bb7acecfSBarry Smith Loosely speaking, compatible `DM`s represent the same domain and parallel 86203d862458SPatrick Sanan decomposition, but hold different data. 86218320bc6fSPatrick Sanan 86228320bc6fSPatrick Sanan Typically, one would confirm compatibility if intending to simultaneously iterate 8623bb7acecfSBarry Smith over a pair of vectors obtained from different `DM`s. 86248320bc6fSPatrick Sanan 8625bb7acecfSBarry Smith For example, two `DMDA` objects are compatible if they have the same local 86268320bc6fSPatrick Sanan and global sizes and the same stencil width. They can have different numbers 86278320bc6fSPatrick Sanan of degrees of freedom per node. Thus, one could use the node numbering from 8628bb7acecfSBarry Smith either `DM` in bounds for a loop over vectors derived from either `DM`. 86298320bc6fSPatrick Sanan 8630bb7acecfSBarry Smith Consider the operation of summing data living on a 2-dof `DMDA` to data living 8631bb7acecfSBarry Smith on a 1-dof `DMDA`, which should be compatible, as in the following snippet. 86328320bc6fSPatrick Sanan .vb 86338320bc6fSPatrick Sanan ... 86349566063dSJacob Faibussowitsch PetscCall(DMGetCompatibility(da1,da2,&compatible,&set)); 86358320bc6fSPatrick Sanan if (set && compatible) { 86369566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1)); 86379566063dSJacob Faibussowitsch PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2)); 86389566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL)); 86398320bc6fSPatrick Sanan for (j=y; j<y+n; ++j) { 86408320bc6fSPatrick Sanan for (i=x; i<x+m, ++i) { 86418320bc6fSPatrick Sanan arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1]; 86428320bc6fSPatrick Sanan } 86438320bc6fSPatrick Sanan } 86449566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1)); 86459566063dSJacob Faibussowitsch PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2)); 86468320bc6fSPatrick Sanan } else { 86478320bc6fSPatrick Sanan SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible"); 86488320bc6fSPatrick Sanan } 86498320bc6fSPatrick Sanan ... 86508320bc6fSPatrick Sanan .ve 86518320bc6fSPatrick Sanan 8652bb7acecfSBarry Smith Checking compatibility might be expensive for a given implementation of `DM`, 86538320bc6fSPatrick Sanan or might be impossible to unambiguously confirm or deny. For this reason, 86548320bc6fSPatrick Sanan this function may decline to determine compatibility, and hence users should 86558320bc6fSPatrick Sanan always check the "set" output parameter. 86568320bc6fSPatrick Sanan 8657bb7acecfSBarry Smith A `DM` is always compatible with itself. 86588320bc6fSPatrick Sanan 8659bb7acecfSBarry Smith In the current implementation, `DM`s which live on "unequal" communicators 86608320bc6fSPatrick Sanan (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed 86618320bc6fSPatrick Sanan incompatible. 86628320bc6fSPatrick Sanan 86638320bc6fSPatrick Sanan This function is labeled "Collective," as information about all subdomains 8664bb7acecfSBarry Smith is required on each rank. However, in `DM` implementations which store all this 86658320bc6fSPatrick Sanan information locally, this function may be merely "Logically Collective". 86668320bc6fSPatrick Sanan 866760225df5SJacob Faibussowitsch Developer Notes: 8668bb7acecfSBarry Smith Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B 86693d862458SPatrick Sanan iff B is compatible with A. Thus, this function checks the implementations 8670a5bc1bf3SBarry Smith of both dm and dmc (if they are of different types), attempting to determine 8671bb7acecfSBarry Smith compatibility. It is left to `DM` implementers to ensure that symmetry is 86728320bc6fSPatrick Sanan preserved. The simplest way to do this is, when implementing type-specific 86733d862458SPatrick Sanan logic for this function, is to check for existing logic in the implementation 8674bb7acecfSBarry Smith of other `DM` types and let *set = PETSC_FALSE if found. 86758320bc6fSPatrick Sanan 86761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()` 86778320bc6fSPatrick Sanan @*/ 8678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set) 8679d71ae5a4SJacob Faibussowitsch { 86808320bc6fSPatrick Sanan PetscMPIInt compareResult; 86818320bc6fSPatrick Sanan DMType type, type2; 86828320bc6fSPatrick Sanan PetscBool sameType; 86838320bc6fSPatrick Sanan 86848320bc6fSPatrick Sanan PetscFunctionBegin; 8685a5bc1bf3SBarry Smith PetscValidHeaderSpecific(dm1, DM_CLASSID, 1); 86868320bc6fSPatrick Sanan PetscValidHeaderSpecific(dm2, DM_CLASSID, 2); 86878320bc6fSPatrick Sanan 86888320bc6fSPatrick Sanan /* Declare a DM compatible with itself */ 8689a5bc1bf3SBarry Smith if (dm1 == dm2) { 86908320bc6fSPatrick Sanan *set = PETSC_TRUE; 86918320bc6fSPatrick Sanan *compatible = PETSC_TRUE; 86923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 86938320bc6fSPatrick Sanan } 86948320bc6fSPatrick Sanan 86958320bc6fSPatrick Sanan /* Declare a DM incompatible with a DM that lives on an "unequal" 86968320bc6fSPatrick Sanan communicator. Note that this does not preclude compatibility with 86978320bc6fSPatrick Sanan DMs living on "congruent" or "similar" communicators, but this must be 86988320bc6fSPatrick Sanan determined by the implementation-specific logic */ 86999566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult)); 87008320bc6fSPatrick Sanan if (compareResult == MPI_UNEQUAL) { 87018320bc6fSPatrick Sanan *set = PETSC_TRUE; 87028320bc6fSPatrick Sanan *compatible = PETSC_FALSE; 87033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 87048320bc6fSPatrick Sanan } 87058320bc6fSPatrick Sanan 87068320bc6fSPatrick Sanan /* Pass to the implementation-specific routine, if one exists. */ 8707a5bc1bf3SBarry Smith if (dm1->ops->getcompatibility) { 8708dbbe0bcdSBarry Smith PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set); 87093ba16761SJacob Faibussowitsch if (*set) PetscFunctionReturn(PETSC_SUCCESS); 87108320bc6fSPatrick Sanan } 87118320bc6fSPatrick Sanan 8712a5bc1bf3SBarry Smith /* If dm1 and dm2 are of different types, then attempt to check compatibility 87138320bc6fSPatrick Sanan with an implementation of this function from dm2 */ 87149566063dSJacob Faibussowitsch PetscCall(DMGetType(dm1, &type)); 87159566063dSJacob Faibussowitsch PetscCall(DMGetType(dm2, &type2)); 87169566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(type, type2, &sameType)); 87178320bc6fSPatrick Sanan if (!sameType && dm2->ops->getcompatibility) { 8718dbbe0bcdSBarry Smith PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */ 87198320bc6fSPatrick Sanan } else { 87208320bc6fSPatrick Sanan *set = PETSC_FALSE; 87218320bc6fSPatrick Sanan } 87223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 87238320bc6fSPatrick Sanan } 8724c0f0dcc3SMatthew G. Knepley 8725c0f0dcc3SMatthew G. Knepley /*@C 8726bb7acecfSBarry Smith DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance. 8727c0f0dcc3SMatthew G. Knepley 872820f4b53cSBarry Smith Logically Collective 8729c0f0dcc3SMatthew G. Knepley 8730c0f0dcc3SMatthew G. Knepley Input Parameters: 873160225df5SJacob Faibussowitsch + dm - the `DM` 8732c0f0dcc3SMatthew G. Knepley . f - the monitor function 873320f4b53cSBarry Smith . mctx - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired) 873420f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`) 8735c0f0dcc3SMatthew G. Knepley 873620f4b53cSBarry Smith Options Database Key: 873760225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but 8738c0f0dcc3SMatthew G. Knepley does not cancel those set via the options database. 8739c0f0dcc3SMatthew G. Knepley 87409bdbcad8SBarry Smith Level: intermediate 87419bdbcad8SBarry Smith 8742bb7acecfSBarry Smith Note: 8743c0f0dcc3SMatthew G. Knepley Several different monitoring routines may be set by calling 8744bb7acecfSBarry Smith `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the 8745c0f0dcc3SMatthew G. Knepley order in which they were set. 8746c0f0dcc3SMatthew G. Knepley 874760225df5SJacob Faibussowitsch Fortran Notes: 8748bb7acecfSBarry Smith Only a single monitor function can be set for each `DM` object 8749bb7acecfSBarry Smith 875060225df5SJacob Faibussowitsch Developer Notes: 8751bb7acecfSBarry Smith This API has a generic name but seems specific to a very particular aspect of the use of `DM` 8752c0f0dcc3SMatthew G. Knepley 87531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()` 8754c0f0dcc3SMatthew G. Knepley @*/ 8755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **)) 8756d71ae5a4SJacob Faibussowitsch { 8757c0f0dcc3SMatthew G. Knepley PetscInt m; 8758c0f0dcc3SMatthew G. Knepley 8759c0f0dcc3SMatthew G. Knepley PetscFunctionBegin; 8760c0f0dcc3SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8761c0f0dcc3SMatthew G. Knepley for (m = 0; m < dm->numbermonitors; ++m) { 8762c0f0dcc3SMatthew G. Knepley PetscBool identical; 8763c0f0dcc3SMatthew G. Knepley 87649566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical)); 87653ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 8766c0f0dcc3SMatthew G. Knepley } 87677a8be351SBarry Smith PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set"); 8768c0f0dcc3SMatthew G. Knepley dm->monitor[dm->numbermonitors] = f; 8769c0f0dcc3SMatthew G. Knepley dm->monitordestroy[dm->numbermonitors] = monitordestroy; 8770c0f0dcc3SMatthew G. Knepley dm->monitorcontext[dm->numbermonitors++] = (void *)mctx; 87713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8772c0f0dcc3SMatthew G. Knepley } 8773c0f0dcc3SMatthew G. Knepley 8774c0f0dcc3SMatthew G. Knepley /*@ 8775bb7acecfSBarry Smith DMMonitorCancel - Clears all the monitor functions for a `DM` object. 8776c0f0dcc3SMatthew G. Knepley 877720f4b53cSBarry Smith Logically Collective 8778c0f0dcc3SMatthew G. Knepley 8779c0f0dcc3SMatthew G. Knepley Input Parameter: 8780c0f0dcc3SMatthew G. Knepley . dm - the DM 8781c0f0dcc3SMatthew G. Knepley 8782c0f0dcc3SMatthew G. Knepley Options Database Key: 8783c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired 8784bb7acecfSBarry Smith into a code by calls to `DMonitorSet()`, but does not cancel those 8785c0f0dcc3SMatthew G. Knepley set via the options database 8786c0f0dcc3SMatthew G. Knepley 87879bdbcad8SBarry Smith Level: intermediate 87889bdbcad8SBarry Smith 8789bb7acecfSBarry Smith Note: 8790bb7acecfSBarry Smith There is no way to clear one specific monitor from a `DM` object. 8791c0f0dcc3SMatthew G. Knepley 87921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()` 8793c0f0dcc3SMatthew G. Knepley @*/ 8794d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm) 8795d71ae5a4SJacob Faibussowitsch { 8796c0f0dcc3SMatthew G. Knepley PetscInt m; 8797c0f0dcc3SMatthew G. Knepley 8798c0f0dcc3SMatthew G. Knepley PetscFunctionBegin; 8799c0f0dcc3SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 8800c0f0dcc3SMatthew G. Knepley for (m = 0; m < dm->numbermonitors; ++m) { 88019566063dSJacob Faibussowitsch if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m])); 8802c0f0dcc3SMatthew G. Knepley } 8803c0f0dcc3SMatthew G. Knepley dm->numbermonitors = 0; 88043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8805c0f0dcc3SMatthew G. Knepley } 8806c0f0dcc3SMatthew G. Knepley 8807c0f0dcc3SMatthew G. Knepley /*@C 8808c0f0dcc3SMatthew G. Knepley DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 8809c0f0dcc3SMatthew G. Knepley 881020f4b53cSBarry Smith Collective 8811c0f0dcc3SMatthew G. Knepley 8812c0f0dcc3SMatthew G. Knepley Input Parameters: 8813bb7acecfSBarry Smith + dm - `DM` object you wish to monitor 8814c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking 8815c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done 8816c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor 8817c0f0dcc3SMatthew G. Knepley . monitor - the monitor function 8818bb7acecfSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the `DM` or `PetscViewer` objects 8819c0f0dcc3SMatthew G. Knepley 8820c0f0dcc3SMatthew G. Knepley Output Parameter: 8821c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created 8822c0f0dcc3SMatthew G. Knepley 8823c0f0dcc3SMatthew G. Knepley Level: developer 8824c0f0dcc3SMatthew G. Knepley 88251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`, 8826db781477SPatrick Sanan `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()` 882760225df5SJacob Faibussowitsch `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, 8828db781477SPatrick Sanan `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`, 8829c2e3fba1SPatrick Sanan `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`, 8830db781477SPatrick Sanan `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`, 8831bb7acecfSBarry Smith `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()` 8832c0f0dcc3SMatthew G. Knepley @*/ 8833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg) 8834d71ae5a4SJacob Faibussowitsch { 8835c0f0dcc3SMatthew G. Knepley PetscViewer viewer; 8836c0f0dcc3SMatthew G. Knepley PetscViewerFormat format; 8837c0f0dcc3SMatthew G. Knepley 8838c0f0dcc3SMatthew G. Knepley PetscFunctionBegin; 8839c0f0dcc3SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 88409566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg)); 8841c0f0dcc3SMatthew G. Knepley if (*flg) { 8842c0f0dcc3SMatthew G. Knepley PetscViewerAndFormat *vf; 8843c0f0dcc3SMatthew G. Knepley 88449566063dSJacob Faibussowitsch PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf)); 88459566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)viewer)); 88469566063dSJacob Faibussowitsch if (monitorsetup) PetscCall((*monitorsetup)(dm, vf)); 88479566063dSJacob Faibussowitsch PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy)); 8848c0f0dcc3SMatthew G. Knepley } 88493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8850c0f0dcc3SMatthew G. Knepley } 8851c0f0dcc3SMatthew G. Knepley 8852c0f0dcc3SMatthew G. Knepley /*@ 8853c0f0dcc3SMatthew G. Knepley DMMonitor - runs the user provided monitor routines, if they exist 8854c0f0dcc3SMatthew G. Knepley 885520f4b53cSBarry Smith Collective 8856c0f0dcc3SMatthew G. Knepley 88572fe279fdSBarry Smith Input Parameter: 8858bb7acecfSBarry Smith . dm - The `DM` 8859c0f0dcc3SMatthew G. Knepley 8860c0f0dcc3SMatthew G. Knepley Level: developer 8861c0f0dcc3SMatthew G. Knepley 8862a4e35b19SJacob Faibussowitsch Developer Notes: 8863a4e35b19SJacob Faibussowitsch Note should indicate when during the life of the `DM` the monitor is run. It appears to be 8864a4e35b19SJacob Faibussowitsch related to the discretization process seems rather specialized since some `DM` have no 8865a4e35b19SJacob Faibussowitsch concept of discretization. 8866bb7acecfSBarry Smith 88671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()` 8868c0f0dcc3SMatthew G. Knepley @*/ 8869d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm) 8870d71ae5a4SJacob Faibussowitsch { 8871c0f0dcc3SMatthew G. Knepley PetscInt m; 8872c0f0dcc3SMatthew G. Knepley 8873c0f0dcc3SMatthew G. Knepley PetscFunctionBegin; 88743ba16761SJacob Faibussowitsch if (!dm) PetscFunctionReturn(PETSC_SUCCESS); 8875c0f0dcc3SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 887648a46eb9SPierre Jolivet for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m])); 88773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8878c0f0dcc3SMatthew G. Knepley } 88792e4af2aeSMatthew G. Knepley 88802e4af2aeSMatthew G. Knepley /*@ 8881bb7acecfSBarry Smith DMComputeError - Computes the error assuming the user has provided the exact solution functions 88822e4af2aeSMatthew G. Knepley 888320f4b53cSBarry Smith Collective 88842e4af2aeSMatthew G. Knepley 88852e4af2aeSMatthew G. Knepley Input Parameters: 8886bb7acecfSBarry Smith + dm - The `DM` 88876b867d5aSJose E. Roman - sol - The solution vector 88882e4af2aeSMatthew G. Knepley 88896b867d5aSJose E. Roman Input/Output Parameter: 889020f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output 88916b867d5aSJose E. Roman contains the error in each field 88926b867d5aSJose E. Roman 88936b867d5aSJose E. Roman Output Parameter: 889420f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`) 889520f4b53cSBarry Smith 889620f4b53cSBarry Smith Level: developer 88972e4af2aeSMatthew G. Knepley 8898bb7acecfSBarry Smith Note: 8899bb7acecfSBarry Smith The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`. 89002e4af2aeSMatthew G. Knepley 89011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()` 89022e4af2aeSMatthew G. Knepley @*/ 8903d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec) 8904d71ae5a4SJacob Faibussowitsch { 89052e4af2aeSMatthew G. Knepley PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *); 89062e4af2aeSMatthew G. Knepley void **ctxs; 89072e4af2aeSMatthew G. Knepley PetscReal time; 89082e4af2aeSMatthew G. Knepley PetscInt Nf, f, Nds, s; 89092e4af2aeSMatthew G. Knepley 89102e4af2aeSMatthew G. Knepley PetscFunctionBegin; 89119566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 89129566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs)); 89139566063dSJacob Faibussowitsch PetscCall(DMGetNumDS(dm, &Nds)); 89142e4af2aeSMatthew G. Knepley for (s = 0; s < Nds; ++s) { 89152e4af2aeSMatthew G. Knepley PetscDS ds; 89162e4af2aeSMatthew G. Knepley DMLabel label; 89172e4af2aeSMatthew G. Knepley IS fieldIS; 89182e4af2aeSMatthew G. Knepley const PetscInt *fields; 89192e4af2aeSMatthew G. Knepley PetscInt dsNf; 89202e4af2aeSMatthew G. Knepley 892107218a29SMatthew G. Knepley PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL)); 89229566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(ds, &dsNf)); 89239566063dSJacob Faibussowitsch if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields)); 89242e4af2aeSMatthew G. Knepley for (f = 0; f < dsNf; ++f) { 89252e4af2aeSMatthew G. Knepley const PetscInt field = fields[f]; 89269566063dSJacob Faibussowitsch PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field])); 89272e4af2aeSMatthew G. Knepley } 89289566063dSJacob Faibussowitsch if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields)); 89292e4af2aeSMatthew G. Knepley } 8930ad540459SPierre Jolivet for (f = 0; f < Nf; ++f) PetscCheck(exactSol[f], PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "DS must contain exact solution functions in order to calculate error, missing for field %" PetscInt_FMT, f); 89319566063dSJacob Faibussowitsch PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time)); 89329566063dSJacob Faibussowitsch if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors)); 89332e4af2aeSMatthew G. Knepley if (errorVec) { 89342e4af2aeSMatthew G. Knepley DM edm; 89352e4af2aeSMatthew G. Knepley DMPolytopeType ct; 89362e4af2aeSMatthew G. Knepley PetscBool simplex; 89372e4af2aeSMatthew G. Knepley PetscInt dim, cStart, Nf; 89382e4af2aeSMatthew G. Knepley 89399566063dSJacob Faibussowitsch PetscCall(DMClone(dm, &edm)); 89409566063dSJacob Faibussowitsch PetscCall(DMGetDimension(edm, &dim)); 89419566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL)); 89429566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 89432e4af2aeSMatthew G. Knepley simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE; 89449566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 89452e4af2aeSMatthew G. Knepley for (f = 0; f < Nf; ++f) { 89462e4af2aeSMatthew G. Knepley PetscFE fe, efe; 89472e4af2aeSMatthew G. Knepley PetscQuadrature q; 89482e4af2aeSMatthew G. Knepley const char *name; 89492e4af2aeSMatthew G. Knepley 89509566063dSJacob Faibussowitsch PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe)); 89519566063dSJacob Faibussowitsch PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe)); 89529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)fe, &name)); 89539566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)efe, name)); 89549566063dSJacob Faibussowitsch PetscCall(PetscFEGetQuadrature(fe, &q)); 89559566063dSJacob Faibussowitsch PetscCall(PetscFESetQuadrature(efe, q)); 89569566063dSJacob Faibussowitsch PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe)); 89579566063dSJacob Faibussowitsch PetscCall(PetscFEDestroy(&efe)); 89582e4af2aeSMatthew G. Knepley } 89599566063dSJacob Faibussowitsch PetscCall(DMCreateDS(edm)); 89602e4af2aeSMatthew G. Knepley 89619566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(edm, errorVec)); 89629566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error")); 89639566063dSJacob Faibussowitsch PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec)); 89649566063dSJacob Faibussowitsch PetscCall(DMDestroy(&edm)); 89652e4af2aeSMatthew G. Knepley } 89669566063dSJacob Faibussowitsch PetscCall(PetscFree2(exactSol, ctxs)); 89673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 89682e4af2aeSMatthew G. Knepley } 89699a2a23afSMatthew G. Knepley 89709a2a23afSMatthew G. Knepley /*@ 8971bb7acecfSBarry Smith DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM` 89729a2a23afSMatthew G. Knepley 897320f4b53cSBarry Smith Not Collective 89749a2a23afSMatthew G. Knepley 89759a2a23afSMatthew G. Knepley Input Parameter: 8976bb7acecfSBarry Smith . dm - The `DM` 89779a2a23afSMatthew G. Knepley 89789a2a23afSMatthew G. Knepley Output Parameter: 8979a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors 89809a2a23afSMatthew G. Knepley 89819a2a23afSMatthew G. Knepley Level: advanced 89829a2a23afSMatthew G. Knepley 898360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()` 89849a2a23afSMatthew G. Knepley @*/ 8985d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux) 8986d71ae5a4SJacob Faibussowitsch { 89879a2a23afSMatthew G. Knepley PetscFunctionBegin; 89889a2a23afSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 89899566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux)); 89903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 89919a2a23afSMatthew G. Knepley } 89929a2a23afSMatthew G. Knepley 89939a2a23afSMatthew G. Knepley /*@ 8994ac17215fSMatthew G. Knepley DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part 89959a2a23afSMatthew G. Knepley 899620f4b53cSBarry Smith Not Collective 89979a2a23afSMatthew G. Knepley 89989a2a23afSMatthew G. Knepley Input Parameters: 8999bb7acecfSBarry Smith + dm - The `DM` 9000bb7acecfSBarry Smith . label - The `DMLabel` 9001ac17215fSMatthew G. Knepley . value - The label value indicating the region 9002ac17215fSMatthew G. Knepley - part - The equation part, or 0 if unused 90039a2a23afSMatthew G. Knepley 90049a2a23afSMatthew G. Knepley Output Parameter: 9005bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data 90069a2a23afSMatthew G. Knepley 90079bdbcad8SBarry Smith Level: advanced 90089bdbcad8SBarry Smith 9009bb7acecfSBarry Smith Note: 9010bb7acecfSBarry Smith If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well. 901104c51a94SMatthew G. Knepley 90121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()` 90139a2a23afSMatthew G. Knepley @*/ 9014d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux) 9015d71ae5a4SJacob Faibussowitsch { 9016ac17215fSMatthew G. Knepley PetscHashAuxKey key, wild = {NULL, 0, 0}; 901704c51a94SMatthew G. Knepley PetscBool has; 90189a2a23afSMatthew G. Knepley 90199a2a23afSMatthew G. Knepley PetscFunctionBegin; 90209a2a23afSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 90219a2a23afSMatthew G. Knepley if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 90229a2a23afSMatthew G. Knepley key.label = label; 90239a2a23afSMatthew G. Knepley key.value = value; 9024ac17215fSMatthew G. Knepley key.part = part; 90259566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxHas(dm->auxData, key, &has)); 90269566063dSJacob Faibussowitsch if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux)); 90279566063dSJacob Faibussowitsch else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux)); 90283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 90299a2a23afSMatthew G. Knepley } 90309a2a23afSMatthew G. Knepley 90319a2a23afSMatthew G. Knepley /*@ 9032bb7acecfSBarry Smith DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part 90339a2a23afSMatthew G. Knepley 903420f4b53cSBarry Smith Not Collective because auxiliary vectors are not parallel 90359a2a23afSMatthew G. Knepley 90369a2a23afSMatthew G. Knepley Input Parameters: 9037bb7acecfSBarry Smith + dm - The `DM` 9038bb7acecfSBarry Smith . label - The `DMLabel` 90399a2a23afSMatthew G. Knepley . value - The label value indicating the region 9040ac17215fSMatthew G. Knepley . part - The equation part, or 0 if unused 9041bb7acecfSBarry Smith - aux - The `Vec` holding auxiliary field data 90429a2a23afSMatthew G. Knepley 90439a2a23afSMatthew G. Knepley Level: advanced 90449a2a23afSMatthew G. Knepley 90451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()` 90469a2a23afSMatthew G. Knepley @*/ 9047d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux) 9048d71ae5a4SJacob Faibussowitsch { 90499a2a23afSMatthew G. Knepley Vec old; 90509a2a23afSMatthew G. Knepley PetscHashAuxKey key; 90519a2a23afSMatthew G. Knepley 90529a2a23afSMatthew G. Knepley PetscFunctionBegin; 90539a2a23afSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 90549a2a23afSMatthew G. Knepley if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2); 90559a2a23afSMatthew G. Knepley key.label = label; 90569a2a23afSMatthew G. Knepley key.value = value; 9057ac17215fSMatthew G. Knepley key.part = part; 90589566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxGet(dm->auxData, key, &old)); 90599566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)aux)); 90609566063dSJacob Faibussowitsch if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key)); 90619566063dSJacob Faibussowitsch else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux)); 9062*d705d0eaSStefano Zampini PetscCall(VecDestroy(&old)); 90633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 90649a2a23afSMatthew G. Knepley } 90659a2a23afSMatthew G. Knepley 90669a2a23afSMatthew G. Knepley /*@C 9067bb7acecfSBarry Smith DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM` 90689a2a23afSMatthew G. Knepley 906920f4b53cSBarry Smith Not Collective 90709a2a23afSMatthew G. Knepley 90719a2a23afSMatthew G. Knepley Input Parameter: 9072bb7acecfSBarry Smith . dm - The `DM` 90739a2a23afSMatthew G. Knepley 90749a2a23afSMatthew G. Knepley Output Parameters: 9075bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec` 9076bb7acecfSBarry Smith . values - The label values for each `Vec` 9077bb7acecfSBarry Smith - parts - The equation parts for each `Vec` 90789a2a23afSMatthew G. Knepley 90799bdbcad8SBarry Smith Level: advanced 90809bdbcad8SBarry Smith 9081bb7acecfSBarry Smith Note: 9082bb7acecfSBarry Smith The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`. 90839a2a23afSMatthew G. Knepley 908460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()` 90859a2a23afSMatthew G. Knepley @*/ 9086d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[]) 9087d71ae5a4SJacob Faibussowitsch { 90889a2a23afSMatthew G. Knepley PetscHashAuxKey *keys; 90899a2a23afSMatthew G. Knepley PetscInt n, i, off = 0; 90909a2a23afSMatthew G. Knepley 90919a2a23afSMatthew G. Knepley PetscFunctionBegin; 90929a2a23afSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 90934f572ea9SToby Isaac PetscAssertPointer(labels, 2); 90944f572ea9SToby Isaac PetscAssertPointer(values, 3); 90954f572ea9SToby Isaac PetscAssertPointer(parts, 4); 90969566063dSJacob Faibussowitsch PetscCall(DMGetNumAuxiliaryVec(dm, &n)); 90979566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(n, &keys)); 90989566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys)); 90999371c9d4SSatish Balay for (i = 0; i < n; ++i) { 91009371c9d4SSatish Balay labels[i] = keys[i].label; 91019371c9d4SSatish Balay values[i] = keys[i].value; 91029371c9d4SSatish Balay parts[i] = keys[i].part; 91039371c9d4SSatish Balay } 91049566063dSJacob Faibussowitsch PetscCall(PetscFree(keys)); 91053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 91069a2a23afSMatthew G. Knepley } 91079a2a23afSMatthew G. Knepley 91089a2a23afSMatthew G. Knepley /*@ 9109bb7acecfSBarry Smith DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM` 91109a2a23afSMatthew G. Knepley 911120f4b53cSBarry Smith Not Collective 91129a2a23afSMatthew G. Knepley 91139a2a23afSMatthew G. Knepley Input Parameter: 9114bb7acecfSBarry Smith . dm - The `DM` 91159a2a23afSMatthew G. Knepley 91169a2a23afSMatthew G. Knepley Output Parameter: 9117bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data 91189a2a23afSMatthew G. Knepley 91199a2a23afSMatthew G. Knepley Level: advanced 91209a2a23afSMatthew G. Knepley 9121bb7acecfSBarry Smith Note: 9122bb7acecfSBarry Smith This is a shallow copy of the auxiliary vectors 9123bb7acecfSBarry Smith 91241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()` 91259a2a23afSMatthew G. Knepley @*/ 9126d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew) 9127d71ae5a4SJacob Faibussowitsch { 91289a2a23afSMatthew G. Knepley PetscFunctionBegin; 91299a2a23afSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 9130*d705d0eaSStefano Zampini PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2); 9131*d705d0eaSStefano Zampini if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS); 9132*d705d0eaSStefano Zampini PetscHMapAux oldData = dmNew->auxData; 91339566063dSJacob Faibussowitsch PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData)); 9134*d705d0eaSStefano Zampini { 9135*d705d0eaSStefano Zampini Vec *auxData; 9136*d705d0eaSStefano Zampini PetscInt n, i, off = 0; 9137*d705d0eaSStefano Zampini 9138*d705d0eaSStefano Zampini PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n)); 9139*d705d0eaSStefano Zampini PetscCall(PetscMalloc1(n, &auxData)); 9140*d705d0eaSStefano Zampini PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData)); 9141*d705d0eaSStefano Zampini for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i])); 9142*d705d0eaSStefano Zampini PetscCall(PetscFree(auxData)); 9143*d705d0eaSStefano Zampini off = 0; 9144*d705d0eaSStefano Zampini PetscCall(PetscHMapAuxGetSize(oldData, &n)); 9145*d705d0eaSStefano Zampini PetscCall(PetscMalloc1(n, &auxData)); 9146*d705d0eaSStefano Zampini PetscCall(PetscHMapAuxGetVals(oldData, &off, auxData)); 9147*d705d0eaSStefano Zampini for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i])); 9148*d705d0eaSStefano Zampini PetscCall(PetscFree(auxData)); 9149*d705d0eaSStefano Zampini } 9150*d705d0eaSStefano Zampini PetscCall(PetscHMapAuxDestroy(&oldData)); 91513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 91529a2a23afSMatthew G. Knepley } 9153b5a892a1SMatthew G. Knepley 9154b5a892a1SMatthew G. Knepley /*@C 9155bb7acecfSBarry Smith DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement 9156b5a892a1SMatthew G. Knepley 915720f4b53cSBarry Smith Not Collective 9158b5a892a1SMatthew G. Knepley 9159b5a892a1SMatthew G. Knepley Input Parameters: 9160bb7acecfSBarry Smith + ct - The `DMPolytopeType` 9161b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces 9162b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces 9163b5a892a1SMatthew G. Knepley 9164b5a892a1SMatthew G. Knepley Output Parameters: 9165bb7acecfSBarry Smith + ornt - The orientation (transformation) which will take the source arrangement to the target arrangement 9166b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found 9167b5a892a1SMatthew G. Knepley 9168b5a892a1SMatthew G. Knepley Level: advanced 9169b5a892a1SMatthew G. Knepley 9170bb7acecfSBarry Smith Note: 9171bb7acecfSBarry Smith An arrangement is a face order combined with an orientation for each face 9172bb7acecfSBarry Smith 9173bb7acecfSBarry Smith Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2 9174bb7acecfSBarry Smith that labels each arrangement (face ordering plus orientation for each face). 9175bb7acecfSBarry Smith 9176bb7acecfSBarry Smith See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement 9177bb7acecfSBarry Smith 91781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()` 9179b5a892a1SMatthew G. Knepley @*/ 9180d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found) 9181d71ae5a4SJacob Faibussowitsch { 9182b5a892a1SMatthew G. Knepley const PetscInt cS = DMPolytopeTypeGetConeSize(ct); 9183b5a892a1SMatthew G. Knepley const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2; 9184b5a892a1SMatthew G. Knepley PetscInt o, c; 9185b5a892a1SMatthew G. Knepley 9186b5a892a1SMatthew G. Knepley PetscFunctionBegin; 91879371c9d4SSatish Balay if (!nO) { 91889371c9d4SSatish Balay *ornt = 0; 91899371c9d4SSatish Balay *found = PETSC_TRUE; 91903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 91919371c9d4SSatish Balay } 9192b5a892a1SMatthew G. Knepley for (o = -nO; o < nO; ++o) { 9193b5a892a1SMatthew G. Knepley const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o); 9194b5a892a1SMatthew G. Knepley 91959371c9d4SSatish Balay for (c = 0; c < cS; ++c) 91969371c9d4SSatish Balay if (sourceCone[arr[c * 2]] != targetCone[c]) break; 91979371c9d4SSatish Balay if (c == cS) { 91989371c9d4SSatish Balay *ornt = o; 91999371c9d4SSatish Balay break; 92009371c9d4SSatish Balay } 9201b5a892a1SMatthew G. Knepley } 9202b5a892a1SMatthew G. Knepley *found = o == nO ? PETSC_FALSE : PETSC_TRUE; 92033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9204b5a892a1SMatthew G. Knepley } 9205b5a892a1SMatthew G. Knepley 9206b5a892a1SMatthew G. Knepley /*@C 9207bb7acecfSBarry Smith DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement 9208b5a892a1SMatthew G. Knepley 920920f4b53cSBarry Smith Not Collective 9210b5a892a1SMatthew G. Knepley 9211b5a892a1SMatthew G. Knepley Input Parameters: 9212bb7acecfSBarry Smith + ct - The `DMPolytopeType` 9213b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces 9214b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces 9215b5a892a1SMatthew G. Knepley 92162fe279fdSBarry Smith Output Parameter: 9217bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement 9218b5a892a1SMatthew G. Knepley 9219b5a892a1SMatthew G. Knepley Level: advanced 9220b5a892a1SMatthew G. Knepley 9221bb7acecfSBarry Smith Note: 9222bb7acecfSBarry Smith This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found. 9223bb7acecfSBarry Smith 922460225df5SJacob Faibussowitsch Developer Notes: 9225bb7acecfSBarry Smith It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found 9226bb7acecfSBarry Smith 92271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()` 9228b5a892a1SMatthew G. Knepley @*/ 9229d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) 9230d71ae5a4SJacob Faibussowitsch { 9231b5a892a1SMatthew G. Knepley PetscBool found; 9232b5a892a1SMatthew G. Knepley 9233b5a892a1SMatthew G. Knepley PetscFunctionBegin; 92349566063dSJacob Faibussowitsch PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found)); 92357a8be351SBarry Smith PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]); 92363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9237b5a892a1SMatthew G. Knepley } 9238b5a892a1SMatthew G. Knepley 9239b5a892a1SMatthew G. Knepley /*@C 9240bb7acecfSBarry Smith DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement 9241b5a892a1SMatthew G. Knepley 924220f4b53cSBarry Smith Not Collective 9243b5a892a1SMatthew G. Knepley 9244b5a892a1SMatthew G. Knepley Input Parameters: 9245bb7acecfSBarry Smith + ct - The `DMPolytopeType` 9246b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices 9247b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices 9248b5a892a1SMatthew G. Knepley 9249b5a892a1SMatthew G. Knepley Output Parameters: 9250bb7acecfSBarry Smith + ornt - The orientation (transformation) which will take the source arrangement to the target arrangement 9251b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found 9252b5a892a1SMatthew G. Knepley 9253b5a892a1SMatthew G. Knepley Level: advanced 9254b5a892a1SMatthew G. Knepley 9255bb7acecfSBarry Smith Note: 9256bb7acecfSBarry Smith An arrangement is a vertex order 9257bb7acecfSBarry Smith 9258bb7acecfSBarry Smith Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2 9259bb7acecfSBarry Smith that labels each arrangement (vertex ordering). 9260bb7acecfSBarry Smith 9261bb7acecfSBarry Smith See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement 9262bb7acecfSBarry Smith 92631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()` 9264b5a892a1SMatthew G. Knepley @*/ 9265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found) 9266d71ae5a4SJacob Faibussowitsch { 9267b5a892a1SMatthew G. Knepley const PetscInt cS = DMPolytopeTypeGetNumVertices(ct); 9268b5a892a1SMatthew G. Knepley const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2; 9269b5a892a1SMatthew G. Knepley PetscInt o, c; 9270b5a892a1SMatthew G. Knepley 9271b5a892a1SMatthew G. Knepley PetscFunctionBegin; 92729371c9d4SSatish Balay if (!nO) { 92739371c9d4SSatish Balay *ornt = 0; 92749371c9d4SSatish Balay *found = PETSC_TRUE; 92753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 92769371c9d4SSatish Balay } 9277b5a892a1SMatthew G. Knepley for (o = -nO; o < nO; ++o) { 9278b5a892a1SMatthew G. Knepley const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o); 9279b5a892a1SMatthew G. Knepley 92809371c9d4SSatish Balay for (c = 0; c < cS; ++c) 92819371c9d4SSatish Balay if (sourceVert[arr[c]] != targetVert[c]) break; 92829371c9d4SSatish Balay if (c == cS) { 92839371c9d4SSatish Balay *ornt = o; 92849371c9d4SSatish Balay break; 92859371c9d4SSatish Balay } 9286b5a892a1SMatthew G. Knepley } 9287b5a892a1SMatthew G. Knepley *found = o == nO ? PETSC_FALSE : PETSC_TRUE; 92883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9289b5a892a1SMatthew G. Knepley } 9290b5a892a1SMatthew G. Knepley 9291b5a892a1SMatthew G. Knepley /*@C 9292bb7acecfSBarry Smith DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement 9293b5a892a1SMatthew G. Knepley 929420f4b53cSBarry Smith Not Collective 9295b5a892a1SMatthew G. Knepley 9296b5a892a1SMatthew G. Knepley Input Parameters: 9297bb7acecfSBarry Smith + ct - The `DMPolytopeType` 9298b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices 9299b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices 9300b5a892a1SMatthew G. Knepley 93012fe279fdSBarry Smith Output Parameter: 9302bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement 9303b5a892a1SMatthew G. Knepley 9304b5a892a1SMatthew G. Knepley Level: advanced 9305b5a892a1SMatthew G. Knepley 9306bb7acecfSBarry Smith Note: 9307bb7acecfSBarry Smith This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible. 9308bb7acecfSBarry Smith 930960225df5SJacob Faibussowitsch Developer Notes: 9310bb7acecfSBarry Smith It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found 9311bb7acecfSBarry Smith 93121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()` 9313b5a892a1SMatthew G. Knepley @*/ 9314d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt) 9315d71ae5a4SJacob Faibussowitsch { 9316b5a892a1SMatthew G. Knepley PetscBool found; 9317b5a892a1SMatthew G. Knepley 9318b5a892a1SMatthew G. Knepley PetscFunctionBegin; 93199566063dSJacob Faibussowitsch PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found)); 93207a8be351SBarry Smith PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]); 93213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9322b5a892a1SMatthew G. Knepley } 9323012bc364SMatthew G. Knepley 9324012bc364SMatthew G. Knepley /*@C 9325012bc364SMatthew G. Knepley DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type 9326012bc364SMatthew G. Knepley 932720f4b53cSBarry Smith Not Collective 9328012bc364SMatthew G. Knepley 9329012bc364SMatthew G. Knepley Input Parameters: 9330bb7acecfSBarry Smith + ct - The `DMPolytopeType` 9331012bc364SMatthew G. Knepley - point - Coordinates of the point 9332012bc364SMatthew G. Knepley 93332fe279fdSBarry Smith Output Parameter: 9334012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type 9335012bc364SMatthew G. Knepley 9336012bc364SMatthew G. Knepley Level: advanced 9337012bc364SMatthew G. Knepley 93381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()` 9339012bc364SMatthew G. Knepley @*/ 9340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside) 9341d71ae5a4SJacob Faibussowitsch { 9342012bc364SMatthew G. Knepley PetscReal sum = 0.0; 9343012bc364SMatthew G. Knepley PetscInt d; 9344012bc364SMatthew G. Knepley 9345012bc364SMatthew G. Knepley PetscFunctionBegin; 9346012bc364SMatthew G. Knepley *inside = PETSC_TRUE; 9347012bc364SMatthew G. Knepley switch (ct) { 9348012bc364SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 9349012bc364SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 9350012bc364SMatthew G. Knepley for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) { 93519371c9d4SSatish Balay if (point[d] < -1.0) { 93529371c9d4SSatish Balay *inside = PETSC_FALSE; 93539371c9d4SSatish Balay break; 93549371c9d4SSatish Balay } 9355012bc364SMatthew G. Knepley sum += point[d]; 9356012bc364SMatthew G. Knepley } 93579371c9d4SSatish Balay if (sum > PETSC_SMALL) { 93589371c9d4SSatish Balay *inside = PETSC_FALSE; 93599371c9d4SSatish Balay break; 93609371c9d4SSatish Balay } 9361012bc364SMatthew G. Knepley break; 9362012bc364SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 9363012bc364SMatthew G. Knepley case DM_POLYTOPE_HEXAHEDRON: 9364012bc364SMatthew G. Knepley for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) 93659371c9d4SSatish Balay if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) { 93669371c9d4SSatish Balay *inside = PETSC_FALSE; 9367012bc364SMatthew G. Knepley break; 93689371c9d4SSatish Balay } 93699371c9d4SSatish Balay break; 9370d71ae5a4SJacob Faibussowitsch default: 9371d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]); 9372012bc364SMatthew G. Knepley } 93733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9374012bc364SMatthew G. Knepley } 9375