xref: /petsc/src/dm/interface/dm.c (revision 32546409a44f7abd1c5a8a2624e5fc2a924f4760)
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:
218*32546409SMatthew G. Knepley + dm    - 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 @*/
229*32546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
230d71ae5a4SJacob Faibussowitsch {
231*32546409SMatthew G. Knepley   char *tmp;
232*32546409SMatthew G. Knepley 
2339a42bb27SBarry Smith   PetscFunctionBegin;
234*32546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
235*32546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
236*32546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
237*32546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
238*32546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2409a42bb27SBarry Smith }
2419a42bb27SBarry Smith 
242c0dedaeaSBarry Smith /*@C
243bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
244c0dedaeaSBarry Smith 
24520f4b53cSBarry Smith   Logically Collective
246c0dedaeaSBarry Smith 
247c0dedaeaSBarry Smith   Input Parameter:
248c0dedaeaSBarry Smith . da - initial distributed array
249c0dedaeaSBarry Smith 
250c0dedaeaSBarry Smith   Output Parameter:
251c0dedaeaSBarry Smith . ctype - the vector type
252c0dedaeaSBarry Smith 
253c0dedaeaSBarry Smith   Level: intermediate
254c0dedaeaSBarry Smith 
25560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
256c0dedaeaSBarry Smith @*/
257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
258d71ae5a4SJacob Faibussowitsch {
259c0dedaeaSBarry Smith   PetscFunctionBegin;
260c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
261c0dedaeaSBarry Smith   *ctype = da->vectype;
2623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
263c0dedaeaSBarry Smith }
264c0dedaeaSBarry Smith 
2655f1ad066SMatthew G Knepley /*@
266bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2675f1ad066SMatthew G Knepley 
26820f4b53cSBarry Smith   Not Collective
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Input Parameter:
271bb7acecfSBarry Smith . v - The `Vec`
2725f1ad066SMatthew G Knepley 
2735f1ad066SMatthew G Knepley   Output Parameter:
274bb7acecfSBarry Smith . dm - The `DM`
2755f1ad066SMatthew G Knepley 
2765f1ad066SMatthew G Knepley   Level: intermediate
2775f1ad066SMatthew G Knepley 
278bb7acecfSBarry Smith   Note:
279bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
280bb7acecfSBarry Smith 
2811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2825f1ad066SMatthew G Knepley @*/
283d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
284d71ae5a4SJacob Faibussowitsch {
2855f1ad066SMatthew G Knepley   PetscFunctionBegin;
2865f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2874f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2889566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2905f1ad066SMatthew G Knepley }
2915f1ad066SMatthew G Knepley 
2925f1ad066SMatthew G Knepley /*@
293bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2945f1ad066SMatthew G Knepley 
29520f4b53cSBarry Smith   Not Collective
2965f1ad066SMatthew G Knepley 
2975f1ad066SMatthew G Knepley   Input Parameters:
298bb7acecfSBarry Smith + v  - The `Vec`
299bb7acecfSBarry Smith - dm - The `DM`
3005f1ad066SMatthew G Knepley 
30120f4b53cSBarry Smith   Level: developer
30220f4b53cSBarry Smith 
303bb7acecfSBarry Smith   Note:
304bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
305d9805387SMatthew G. Knepley 
306bb7acecfSBarry 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.
307bb7acecfSBarry Smith 
3081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3095f1ad066SMatthew G Knepley @*/
310d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
311d71ae5a4SJacob Faibussowitsch {
3125f1ad066SMatthew G Knepley   PetscFunctionBegin;
3135f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
314d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3159566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3175f1ad066SMatthew G Knepley }
3185f1ad066SMatthew G Knepley 
319521d9a4cSLisandro Dalcin /*@C
320bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3218f1509bcSBarry Smith 
32220f4b53cSBarry Smith   Logically Collective
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith   Input Parameters:
325bb7acecfSBarry Smith + dm    - the `DM` context
3268f1509bcSBarry Smith - ctype - the matrix type
3278f1509bcSBarry Smith 
32820f4b53cSBarry Smith   Options Database Key:
3298f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3308f1509bcSBarry Smith 
3318f1509bcSBarry Smith   Level: intermediate
3328f1509bcSBarry Smith 
3331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
334bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3358f1509bcSBarry Smith @*/
336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
337d71ae5a4SJacob Faibussowitsch {
3388f1509bcSBarry Smith   PetscFunctionBegin;
3398f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3408f1509bcSBarry Smith   dm->coloringtype = ctype;
3413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3428f1509bcSBarry Smith }
3438f1509bcSBarry Smith 
3448f1509bcSBarry Smith /*@C
345bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
346521d9a4cSLisandro Dalcin 
34720f4b53cSBarry Smith   Logically Collective
348521d9a4cSLisandro Dalcin 
349521d9a4cSLisandro Dalcin   Input Parameter:
350bb7acecfSBarry Smith . dm - the `DM` context
3518f1509bcSBarry Smith 
3528f1509bcSBarry Smith   Output Parameter:
3538f1509bcSBarry Smith . ctype - the matrix type
3548f1509bcSBarry Smith 
35520f4b53cSBarry Smith   Options Database Key:
3568f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3578f1509bcSBarry Smith 
3588f1509bcSBarry Smith   Level: intermediate
3598f1509bcSBarry Smith 
3601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36142747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3628f1509bcSBarry Smith @*/
363d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
364d71ae5a4SJacob Faibussowitsch {
3658f1509bcSBarry Smith   PetscFunctionBegin;
3668f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3678f1509bcSBarry Smith   *ctype = dm->coloringtype;
3683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3698f1509bcSBarry Smith }
3708f1509bcSBarry Smith 
3718f1509bcSBarry Smith /*@C
372bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3738f1509bcSBarry Smith 
37420f4b53cSBarry Smith   Logically Collective
3758f1509bcSBarry Smith 
3768f1509bcSBarry Smith   Input Parameters:
377bb7acecfSBarry Smith + dm    - the `DM` context
378bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
379521d9a4cSLisandro Dalcin 
38020f4b53cSBarry Smith   Options Database Key:
381bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
382521d9a4cSLisandro Dalcin 
383521d9a4cSLisandro Dalcin   Level: intermediate
384521d9a4cSLisandro Dalcin 
38542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
386521d9a4cSLisandro Dalcin @*/
387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
388d71ae5a4SJacob Faibussowitsch {
389*32546409SMatthew G. Knepley   char *tmp;
390*32546409SMatthew G. Knepley 
391521d9a4cSLisandro Dalcin   PetscFunctionBegin;
392521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
393*32546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
394*32546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
3959566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
396*32546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
3973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
398521d9a4cSLisandro Dalcin }
399521d9a4cSLisandro Dalcin 
400c0dedaeaSBarry Smith /*@C
40120f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
402c0dedaeaSBarry Smith 
40320f4b53cSBarry Smith   Logically Collective
404c0dedaeaSBarry Smith 
405c0dedaeaSBarry Smith   Input Parameter:
406bb7acecfSBarry Smith . dm - the `DM` context
407c0dedaeaSBarry Smith 
408c0dedaeaSBarry Smith   Output Parameter:
409c0dedaeaSBarry Smith . ctype - the matrix type
410c0dedaeaSBarry Smith 
411c0dedaeaSBarry Smith   Level: intermediate
412c0dedaeaSBarry Smith 
41342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
414c0dedaeaSBarry Smith @*/
415d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
416d71ae5a4SJacob Faibussowitsch {
417c0dedaeaSBarry Smith   PetscFunctionBegin;
418c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419c0dedaeaSBarry Smith   *ctype = dm->mattype;
4203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
421c0dedaeaSBarry Smith }
422c0dedaeaSBarry Smith 
423c688c046SMatthew G Knepley /*@
424bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
425c688c046SMatthew G Knepley 
42620f4b53cSBarry Smith   Not Collective
427c688c046SMatthew G Knepley 
428c688c046SMatthew G Knepley   Input Parameter:
429bb7acecfSBarry Smith . A - The `Mat`
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley   Output Parameter:
432bb7acecfSBarry Smith . dm - The `DM`
433c688c046SMatthew G Knepley 
434c688c046SMatthew G Knepley   Level: intermediate
435c688c046SMatthew G Knepley 
436bb7acecfSBarry Smith   Note:
437bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
438bb7acecfSBarry Smith 
43960225df5SJacob Faibussowitsch   Developer Notes:
440bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4418f1509bcSBarry Smith 
4421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
443c688c046SMatthew G Knepley @*/
444d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
445d71ae5a4SJacob Faibussowitsch {
446c688c046SMatthew G Knepley   PetscFunctionBegin;
447c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4484f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4499566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
451c688c046SMatthew G Knepley }
452c688c046SMatthew G Knepley 
453c688c046SMatthew G Knepley /*@
454bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
455c688c046SMatthew G Knepley 
45620f4b53cSBarry Smith   Not Collective
457c688c046SMatthew G Knepley 
458c688c046SMatthew G Knepley   Input Parameters:
45920f4b53cSBarry Smith + A  - The `Mat`
46020f4b53cSBarry Smith - dm - The `DM`
461c688c046SMatthew G Knepley 
462bb7acecfSBarry Smith   Level: developer
463c688c046SMatthew G Knepley 
464bb7acecfSBarry Smith   Note:
465bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
466bb7acecfSBarry Smith 
46760225df5SJacob Faibussowitsch   Developer Notes:
468bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
469bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4708f1509bcSBarry Smith 
4711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
472c688c046SMatthew G Knepley @*/
473d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
474d71ae5a4SJacob Faibussowitsch {
475c688c046SMatthew G Knepley   PetscFunctionBegin;
476c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4778865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4789566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480c688c046SMatthew G Knepley }
481c688c046SMatthew G Knepley 
4829a42bb27SBarry Smith /*@C
483bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4849a42bb27SBarry Smith 
48520f4b53cSBarry Smith   Logically Collective
4869a42bb27SBarry Smith 
487d8d19677SJose E. Roman   Input Parameters:
48860225df5SJacob Faibussowitsch + dm     - the `DM` context
489bb7acecfSBarry Smith - prefix - the prefix to prepend
4909a42bb27SBarry Smith 
49120f4b53cSBarry Smith   Level: advanced
49220f4b53cSBarry Smith 
49320f4b53cSBarry Smith   Note:
4949a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
4959a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
4969a42bb27SBarry Smith 
4971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4989a42bb27SBarry Smith @*/
499d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
500d71ae5a4SJacob Faibussowitsch {
5019a42bb27SBarry Smith   PetscFunctionBegin;
5029a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5039566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5041baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5051baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5079a42bb27SBarry Smith }
5089a42bb27SBarry Smith 
50931697293SDave May /*@C
510da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
511bb7acecfSBarry Smith   `DM` options in the options database.
51231697293SDave May 
51320f4b53cSBarry Smith   Logically Collective
51431697293SDave May 
51531697293SDave May   Input Parameters:
516bb7acecfSBarry Smith + dm     - the `DM` context
517bb7acecfSBarry Smith - prefix - the string to append to the current prefix
51831697293SDave May 
51920f4b53cSBarry Smith   Level: advanced
52020f4b53cSBarry Smith 
52120f4b53cSBarry Smith   Note:
522bb7acecfSBarry 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.
52331697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
52431697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
52531697293SDave May 
5261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
52731697293SDave May @*/
528d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
529d71ae5a4SJacob Faibussowitsch {
53031697293SDave May   PetscFunctionBegin;
53131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5329566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53431697293SDave May }
53531697293SDave May 
53631697293SDave May /*@C
53731697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
538bb7acecfSBarry Smith   DM options in the options database.
53931697293SDave May 
54031697293SDave May   Not Collective
54131697293SDave May 
5422fe279fdSBarry Smith   Input Parameter:
543bb7acecfSBarry Smith . dm - the `DM` context
54431697293SDave May 
5452fe279fdSBarry Smith   Output Parameter:
54631697293SDave May . prefix - pointer to the prefix string used is returned
54731697293SDave May 
54831697293SDave May   Level: advanced
54931697293SDave May 
55060225df5SJacob Faibussowitsch   Fortran Notes:
55120f4b53cSBarry Smith   Pass in a string 'prefix' of
55220f4b53cSBarry Smith   sufficient length to hold the prefix.
55320f4b53cSBarry Smith 
5541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
55531697293SDave May @*/
556d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
557d71ae5a4SJacob Faibussowitsch {
55831697293SDave May   PetscFunctionBegin;
55931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5609566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56231697293SDave May }
56331697293SDave May 
56462e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
565d71ae5a4SJacob Faibussowitsch {
5666eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
56788bdff64SToby Isaac 
56888bdff64SToby Isaac   PetscFunctionBegin;
569aab5bcd8SJed Brown   *ncrefct = 0;
57088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57188bdff64SToby Isaac     refct--;
57288bdff64SToby Isaac     if (recurseCoarse) {
57388bdff64SToby Isaac       PetscInt coarseCount;
57488bdff64SToby Isaac 
57562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
57688bdff64SToby Isaac       refct += coarseCount;
57788bdff64SToby Isaac     }
57888bdff64SToby Isaac   }
57988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58088bdff64SToby Isaac     refct--;
58188bdff64SToby Isaac     if (recurseFine) {
58288bdff64SToby Isaac       PetscInt fineCount;
58388bdff64SToby Isaac 
58462e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
58588bdff64SToby Isaac       refct += fineCount;
58688bdff64SToby Isaac     }
58788bdff64SToby Isaac   }
58888bdff64SToby Isaac   *ncrefct = refct;
5893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59088bdff64SToby Isaac }
59188bdff64SToby Isaac 
59262e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
59362e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
59462e5d2d2SJDBetteridge {
59562e5d2d2SJDBetteridge   PetscFunctionBegin;
59662e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
5973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59862e5d2d2SJDBetteridge }
59962e5d2d2SJDBetteridge 
600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
601d71ae5a4SJacob Faibussowitsch {
6025d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
603354557abSToby Isaac 
604354557abSToby Isaac   PetscFunctionBegin;
605354557abSToby Isaac   /* destroy the labels */
606354557abSToby Isaac   while (next) {
607354557abSToby Isaac     DMLabelLink tmp = next->next;
608354557abSToby Isaac 
6095d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
610ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6119566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6129566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
613354557abSToby Isaac     next = tmp;
614354557abSToby Isaac   }
6155d80c0bfSVaclav Hapla   dm->labels = NULL;
6163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
617354557abSToby Isaac }
618354557abSToby Isaac 
61966976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
620d71ae5a4SJacob Faibussowitsch {
6216858538eSMatthew G. Knepley   PetscFunctionBegin;
6226858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6236858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6246858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6256858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6266858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6286858538eSMatthew G. Knepley }
6296858538eSMatthew G. Knepley 
6301fb7b255SJunchao Zhang /*@C
631bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
63247c6ae99SBarry Smith 
63320f4b53cSBarry Smith   Collective
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith   Input Parameter:
636bb7acecfSBarry Smith . dm - the `DM` object to destroy
63747c6ae99SBarry Smith 
63847c6ae99SBarry Smith   Level: developer
63947c6ae99SBarry Smith 
6401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64147c6ae99SBarry Smith @*/
642d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
643d71ae5a4SJacob Faibussowitsch {
6446eb26441SStefano Zampini   PetscInt cnt;
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith   PetscFunctionBegin;
6473ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
6486bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
64987e657c6SBarry Smith 
65088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65162e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
65288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6539371c9d4SSatish Balay   if (--cnt > 0) {
6549371c9d4SSatish Balay     *dm = NULL;
6553ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6569371c9d4SSatish Balay   }
6573ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
6586bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6596eb26441SStefano Zampini 
6609566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6619566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
662974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
663974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6642348bcf4SPeter Brune 
665b17ce1afSJed Brown   /* Destroy the list of hooks */
666c833c3b5SJed Brown   {
667c833c3b5SJed Brown     DMCoarsenHookLink link, next;
668b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
669b17ce1afSJed Brown       next = link->next;
6709566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
671b17ce1afSJed Brown     }
6720298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
673c833c3b5SJed Brown   }
674c833c3b5SJed Brown   {
675c833c3b5SJed Brown     DMRefineHookLink link, next;
676c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
677c833c3b5SJed Brown       next = link->next;
6789566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
679c833c3b5SJed Brown     }
6800298fd71SBarry Smith     (*dm)->refinehook = NULL;
681c833c3b5SJed Brown   }
682be081cd6SPeter Brune   {
683be081cd6SPeter Brune     DMSubDomainHookLink link, next;
684be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
685be081cd6SPeter Brune       next = link->next;
6869566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
687be081cd6SPeter Brune     }
6880298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
689be081cd6SPeter Brune   }
690baf369e7SPeter Brune   {
691baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
692baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
693baf369e7SPeter Brune       next = link->next;
6949566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
695baf369e7SPeter Brune     }
6960298fd71SBarry Smith     (*dm)->gtolhook = NULL;
697baf369e7SPeter Brune   }
698d4d07f1eSToby Isaac   {
699d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
700d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
701d4d07f1eSToby Isaac       next = link->next;
7029566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
703d4d07f1eSToby Isaac     }
704d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
705d4d07f1eSToby Isaac   }
706aa1993deSMatthew G Knepley   /* Destroy the work arrays */
707aa1993deSMatthew G Knepley   {
708aa1993deSMatthew G Knepley     DMWorkLink link, next;
709936381afSPierre 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);
710aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
711aa1993deSMatthew G Knepley       next = link->next;
7129566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7139566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
714aa1993deSMatthew G Knepley     }
7150298fd71SBarry Smith     (*dm)->workin = NULL;
716aa1993deSMatthew G Knepley   }
717c58f1c22SToby Isaac   /* destroy the labels */
7189566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
719f4cdcedcSVaclav Hapla   /* destroy the fields */
7209566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
721f4cdcedcSVaclav Hapla   /* destroy the boundaries */
722e6f8dbb6SToby Isaac   {
723e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
724e6f8dbb6SToby Isaac     while (next) {
725e6f8dbb6SToby Isaac       DMBoundary b = next;
726e6f8dbb6SToby Isaac 
727e6f8dbb6SToby Isaac       next = b->next;
7289566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
729e6f8dbb6SToby Isaac     }
730e6f8dbb6SToby Isaac   }
731b17ce1afSJed Brown 
7329566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7339566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7349566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
73552536dc3SBarry Smith 
73648a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7379566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7389566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7399566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7409566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74188ed4aceSMatthew G Knepley 
7429566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7439566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
7449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7459566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7469566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7479566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7489566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
74948a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7509566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
7519a2a23afSMatthew G. Knepley   {
7529a2a23afSMatthew G. Knepley     Vec     *auxData;
7539a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7549a2a23afSMatthew G. Knepley 
7559566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7569566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7579566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7589566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7599566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7609566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7619a2a23afSMatthew G. Knepley   }
76248a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7636eb26441SStefano Zampini 
7649566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76548a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7674fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7689566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7699566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7706858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7716858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7729566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7739566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7756725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7766725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7776636e97aSMatthew G Knepley 
7789566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7799566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
780e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7819566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
782732e2eb9SMatthew G Knepley 
78348a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7849566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
785d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
786f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7879566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7889566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
789f918ec44SMatthew G. Knepley #endif
790435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7919566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79347c6ae99SBarry Smith }
79447c6ae99SBarry Smith 
795d7bf68aeSBarry Smith /*@
796bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
797d7bf68aeSBarry Smith 
79820f4b53cSBarry Smith   Collective
799d7bf68aeSBarry Smith 
800d7bf68aeSBarry Smith   Input Parameter:
801bb7acecfSBarry Smith . dm - the `DM` object to setup
802d7bf68aeSBarry Smith 
803bb7acecfSBarry Smith   Level: intermediate
804d7bf68aeSBarry Smith 
805bb7acecfSBarry Smith   Note:
806bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
807bb7acecfSBarry Smith 
8081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
809d7bf68aeSBarry Smith @*/
810d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
811d71ae5a4SJacob Faibussowitsch {
812d7bf68aeSBarry Smith   PetscFunctionBegin;
813171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8143ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
815dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8168387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
818d7bf68aeSBarry Smith }
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith /*@
821bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
822d7bf68aeSBarry Smith 
82320f4b53cSBarry Smith   Collective
824d7bf68aeSBarry Smith 
825d7bf68aeSBarry Smith   Input Parameter:
826bb7acecfSBarry Smith . dm - the `DM` object to set options for
827d7bf68aeSBarry Smith 
82820f4b53cSBarry Smith   Options Database Keys:
829bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
830bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
831bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
832a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83320f4b53cSBarry 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`
83420f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8359318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
836cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8375dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8389318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8399318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8409318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
841bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
842bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8439318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
8449318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8459318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8469318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
847bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8489318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8499318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8509318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8519318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
852bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8539318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8549318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8559318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8569318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
857bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
858d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
859d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
860d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
861d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
862d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
863909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
864909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8659318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8669318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8679318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
86820f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
869d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87020f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
871bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
872bb7acecfSBarry 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()`
873bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
874bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
875bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
876384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
877d7bf68aeSBarry Smith 
87895eb5ee5SVaclav Hapla   Level: intermediate
87995eb5ee5SVaclav Hapla 
8801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
881bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
88260225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
883d7bf68aeSBarry Smith @*/
884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
885d71ae5a4SJacob Faibussowitsch {
8867781c08eSBarry Smith   char      typeName[256];
887ca266f36SBarry Smith   PetscBool flg;
888d7bf68aeSBarry Smith 
889d7bf68aeSBarry Smith   PetscFunctionBegin;
890171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89149be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8929566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8939566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
894dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
895d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
8969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
8979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
8981baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
8999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9001baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
901863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9039566063dSJacob 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));
904dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
905f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
906dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
907d0609cedSBarry Smith   PetscOptionsEnd();
9083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
909d7bf68aeSBarry Smith }
910d7bf68aeSBarry Smith 
911fc9bc008SSatish Balay /*@C
912bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
913fe2efc57SMark 
91420f4b53cSBarry Smith   Collective
915fe2efc57SMark 
916fe2efc57SMark   Input Parameters:
917bb7acecfSBarry Smith + dm   - the `DM` object
91820f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
91960225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
920fe2efc57SMark 
921fe2efc57SMark   Level: intermediate
922bb7acecfSBarry Smith 
923bb7acecfSBarry Smith   Note:
924bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
925bb7acecfSBarry Smith 
92660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
927fe2efc57SMark @*/
928d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
929d71ae5a4SJacob Faibussowitsch {
930fe2efc57SMark   PetscFunctionBegin;
931fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9329566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
934fe2efc57SMark }
935fe2efc57SMark 
936fe2efc57SMark /*@C
937bb7acecfSBarry 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
938bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
93947c6ae99SBarry Smith 
94020f4b53cSBarry Smith   Collective
94147c6ae99SBarry Smith 
942d8d19677SJose E. Roman   Input Parameters:
943bb7acecfSBarry Smith + dm - the `DM` object to view
94447c6ae99SBarry Smith - v  - the viewer
94547c6ae99SBarry Smith 
94620f4b53cSBarry Smith   Level: beginner
94720f4b53cSBarry Smith 
948cd7e8a5eSksagiyam   Notes:
949bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
950bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
951bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
952cd7e8a5eSksagiyam 
9531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
95447c6ae99SBarry Smith @*/
955d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
956d71ae5a4SJacob Faibussowitsch {
95732c0f0efSBarry Smith   PetscBool         isbinary;
95876a8abe0SBarry Smith   PetscMPIInt       size;
95976a8abe0SBarry Smith   PetscViewerFormat format;
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith   PetscFunctionBegin;
962171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96348a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
964b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
96574903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
96674903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
96774903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
96874903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
96974903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97074903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97174903a4fSStefano Zampini      in an error here */
97274903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9739566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
974b1b135c8SBarry Smith 
9759566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9773ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9789566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9799566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98032c0f0efSBarry Smith   if (isbinary) {
98155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98232c0f0efSBarry Smith     char     type[256];
98332c0f0efSBarry Smith 
9849566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
985c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9869566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
98732c0f0efSBarry Smith   }
988dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99047c6ae99SBarry Smith }
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith /*@
993bb7acecfSBarry 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,
994bb7acecfSBarry Smith   that is it has no ghost locations.
99547c6ae99SBarry Smith 
99620f4b53cSBarry Smith   Collective
99747c6ae99SBarry Smith 
99847c6ae99SBarry Smith   Input Parameter:
999bb7acecfSBarry Smith . dm - the `DM` object
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith   Output Parameter:
100247c6ae99SBarry Smith . vec - the global vector
100347c6ae99SBarry Smith 
1004073dac72SJed Brown   Level: beginner
100547c6ae99SBarry Smith 
10061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1007bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
100847c6ae99SBarry Smith @*/
1009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1010d71ae5a4SJacob Faibussowitsch {
101147c6ae99SBarry Smith   PetscFunctionBegin;
1012171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10134f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1014dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
101576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1016c6b011d8SStefano Zampini     DM vdm;
1017c6b011d8SStefano Zampini 
10189566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10197a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1020c6b011d8SStefano Zampini   }
10213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102247c6ae99SBarry Smith }
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith /*@
1025bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
102647c6ae99SBarry Smith 
102747c6ae99SBarry Smith   Not Collective
102847c6ae99SBarry Smith 
102947c6ae99SBarry Smith   Input Parameter:
1030bb7acecfSBarry Smith . dm - the `DM` object
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith   Output Parameter:
103347c6ae99SBarry Smith . vec - the local vector
103447c6ae99SBarry Smith 
1035073dac72SJed Brown   Level: beginner
103647c6ae99SBarry Smith 
103720f4b53cSBarry Smith   Note:
1038bb7acecfSBarry 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.
1039bb7acecfSBarry Smith 
10401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1041bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
104247c6ae99SBarry Smith @*/
1043d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1044d71ae5a4SJacob Faibussowitsch {
104547c6ae99SBarry Smith   PetscFunctionBegin;
1046171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10474f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1048dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
104976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1050c6b011d8SStefano Zampini     DM vdm;
1051c6b011d8SStefano Zampini 
10529566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10537a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1054c6b011d8SStefano Zampini   }
10553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
105647c6ae99SBarry Smith }
105747c6ae99SBarry Smith 
10581411c6eeSJed Brown /*@
1059bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10601411c6eeSJed Brown 
106120f4b53cSBarry Smith   Collective
10621411c6eeSJed Brown 
10631411c6eeSJed Brown   Input Parameter:
1064bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10651411c6eeSJed Brown 
10661411c6eeSJed Brown   Output Parameter:
10671411c6eeSJed Brown . ltog - the mapping
10681411c6eeSJed Brown 
1069bb7acecfSBarry Smith   Level: advanced
10701411c6eeSJed Brown 
10711411c6eeSJed Brown   Notes:
1072bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10731411c6eeSJed Brown 
1074bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1075bb7acecfSBarry Smith   need to use this function with those objects.
1076bb7acecfSBarry Smith 
1077bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1078bb7acecfSBarry Smith 
107960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1080bb7acecfSBarry Smith           `DMCreateMatrix()`
10811411c6eeSJed Brown @*/
1082d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1083d71ae5a4SJacob Faibussowitsch {
10840be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10851411c6eeSJed Brown 
10861411c6eeSJed Brown   PetscFunctionBegin;
10871411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10884f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10891411c6eeSJed Brown   if (!dm->ltogmap) {
109037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109137d0c07bSMatthew G Knepley 
10929566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
109337d0c07bSMatthew G Knepley     if (section) {
1094a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
109537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1096ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
109737d0c07bSMatthew G Knepley 
10989566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
10999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11009566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11019566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
110237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1103e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
110437d0c07bSMatthew G Knepley 
110537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11101a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11111a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1112ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11135227eafbSStefano Zampini 
1114e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11155227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1116e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1117e6befd46SJed Brown             cind++;
1118e6befd46SJed Brown           } else {
11195227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1120e6befd46SJed Brown           }
112137d0c07bSMatthew G Knepley         }
112237d0c07bSMatthew G Knepley       }
1123bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11249371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11259371c9d4SSatish Balay       bsLocal[1] = bs;
11269566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11279371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11289371c9d4SSatish Balay         bs = 1;
11299371c9d4SSatish Balay       } else {
11309371c9d4SSatish Balay         bs = bsMinMax[0];
11319371c9d4SSatish Balay       }
11327591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11337591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1134ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1135ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1136ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1137ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1138ca469d19SJed Brown         }
1139ccf3bd66SMatthew G. Knepley         n /= bs;
1140ccf3bd66SMatthew G. Knepley       }
11419566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1142dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
114337d0c07bSMatthew G Knepley   }
11441411c6eeSJed Brown   *ltog = dm->ltogmap;
11453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11461411c6eeSJed Brown }
11471411c6eeSJed Brown 
11481411c6eeSJed Brown /*@
1149bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11501411c6eeSJed Brown 
11511411c6eeSJed Brown   Not Collective
11521411c6eeSJed Brown 
11531411c6eeSJed Brown   Input Parameter:
1154bb7acecfSBarry Smith . dm - the `DM` with block structure
11551411c6eeSJed Brown 
11561411c6eeSJed Brown   Output Parameter:
11571411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11581411c6eeSJed Brown 
11591411c6eeSJed Brown   Level: intermediate
11601411c6eeSJed Brown 
1161bb7acecfSBarry Smith   Note:
1162bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1163bb7acecfSBarry Smith 
1164bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1165bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1166bb7acecfSBarry Smith 
11671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11681411c6eeSJed Brown @*/
1169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1170d71ae5a4SJacob Faibussowitsch {
11711411c6eeSJed Brown   PetscFunctionBegin;
11721411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11734f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11747a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11751411c6eeSJed Brown   *bs = dm->bs;
11763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11771411c6eeSJed Brown }
11781411c6eeSJed Brown 
117948eeb7c8SBarry Smith /*@C
1180bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1181bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
118247c6ae99SBarry Smith 
118320f4b53cSBarry Smith   Collective
118447c6ae99SBarry Smith 
1185d8d19677SJose E. Roman   Input Parameters:
1186bb7acecfSBarry Smith + dmc - the `DM` object
1187bb7acecfSBarry Smith - dmf - the second, finer `DM` object
118847c6ae99SBarry Smith 
1189d8d19677SJose E. Roman   Output Parameters:
119047c6ae99SBarry Smith + mat - the interpolation
1191bb7acecfSBarry Smith - vec - the scaling (optional), see `DMCreateInterpolationScale()`
119247c6ae99SBarry Smith 
119347c6ae99SBarry Smith   Level: developer
119447c6ae99SBarry Smith 
119595452b02SPatrick Sanan   Notes:
1196bb7acecfSBarry 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
1197bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1198d52bd9f3SBarry Smith 
1199bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1200bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120185afcc9aSBarry Smith 
12021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
120347c6ae99SBarry Smith @*/
1204d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1205d71ae5a4SJacob Faibussowitsch {
120647c6ae99SBarry Smith   PetscFunctionBegin;
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1208a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12094f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1211dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
121447c6ae99SBarry Smith }
121547c6ae99SBarry Smith 
12163ad4599aSBarry Smith /*@
1217a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1218a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12192ed6491fSPatrick Sanan 
12202ed6491fSPatrick Sanan   Input Parameters:
1221bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1222bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12232ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12242ed6491fSPatrick Sanan 
12252ed6491fSPatrick Sanan   Output Parameter:
12262ed6491fSPatrick Sanan . scale - the scaled vector
12272ed6491fSPatrick Sanan 
1228bb7acecfSBarry Smith   Level: advanced
12292ed6491fSPatrick Sanan 
1230a4e35b19SJacob Faibussowitsch   Notes:
1231a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1232a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1233a4e35b19SJacob Faibussowitsch 
123460225df5SJacob Faibussowitsch   Developer Notes:
1235bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1236e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1237e9c74fd6SRichard Tran Mills 
123860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12392ed6491fSPatrick Sanan @*/
1240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1241d71ae5a4SJacob Faibussowitsch {
12422ed6491fSPatrick Sanan   Vec         fine;
12432ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12449704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1245e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12469704db99SRichard Tran Mills #endif
12472ed6491fSPatrick Sanan 
12482ed6491fSPatrick Sanan   PetscFunctionBegin;
12499566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12509566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12519566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12529704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12539704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12549704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12559704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12569566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1257e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12589566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12599566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12609566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126183aa49f4SRichard Tran Mills   }
12629704db99SRichard Tran Mills #endif
12639566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12659566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12672ed6491fSPatrick Sanan }
12682ed6491fSPatrick Sanan 
12692ed6491fSPatrick Sanan /*@
1270bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1271bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12723ad4599aSBarry Smith 
127320f4b53cSBarry Smith   Collective
12743ad4599aSBarry Smith 
1275d8d19677SJose E. Roman   Input Parameters:
1276bb7acecfSBarry Smith + dmc - the `DM` object
1277bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12783ad4599aSBarry Smith 
12793ad4599aSBarry Smith   Output Parameter:
12803ad4599aSBarry Smith . mat - the restriction
12813ad4599aSBarry Smith 
12823ad4599aSBarry Smith   Level: developer
12833ad4599aSBarry Smith 
1284bb7acecfSBarry Smith   Note:
1285bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1286bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12873ad4599aSBarry Smith 
12881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12893ad4599aSBarry Smith @*/
1290d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1291d71ae5a4SJacob Faibussowitsch {
12923ad4599aSBarry Smith   PetscFunctionBegin;
1293a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1294a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12954f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12969566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1297dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
12989566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
12993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13003ad4599aSBarry Smith }
13013ad4599aSBarry Smith 
130247c6ae99SBarry Smith /*@
1303a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
130447c6ae99SBarry Smith 
130520f4b53cSBarry Smith   Collective
130647c6ae99SBarry Smith 
1307d8d19677SJose E. Roman   Input Parameters:
1308bb7acecfSBarry Smith + dac - the `DM` object
1309bb7acecfSBarry Smith - daf - the second, finer `DM` object
131047c6ae99SBarry Smith 
131147c6ae99SBarry Smith   Output Parameter:
13126dbf9973SLawrence Mitchell . mat - the injection
131347c6ae99SBarry Smith 
131447c6ae99SBarry Smith   Level: developer
131547c6ae99SBarry Smith 
1316a4e35b19SJacob Faibussowitsch   Notes:
1317a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1318a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1319a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1320a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1321a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1322a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1323a4e35b19SJacob Faibussowitsch 
1324bb7acecfSBarry 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
1325bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132685afcc9aSBarry Smith 
13271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1328bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
132947c6ae99SBarry Smith @*/
1330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1331d71ae5a4SJacob Faibussowitsch {
133247c6ae99SBarry Smith   PetscFunctionBegin;
1333a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1334a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13354f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1337dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134047c6ae99SBarry Smith }
134147c6ae99SBarry Smith 
1342b412c318SBarry Smith /*@
1343bb7acecfSBarry 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
1344bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1345bd041c0cSMatthew G. Knepley 
134620f4b53cSBarry Smith   Collective
1347bd041c0cSMatthew G. Knepley 
1348d8d19677SJose E. Roman   Input Parameters:
1349bb7acecfSBarry Smith + dmc - the target `DM` object
1350bb7acecfSBarry Smith - dmf - the source `DM` object
1351bd041c0cSMatthew G. Knepley 
1352bd041c0cSMatthew G. Knepley   Output Parameter:
1353b4937a87SMatthew G. Knepley . mat - the mass matrix
1354bd041c0cSMatthew G. Knepley 
1355bd041c0cSMatthew G. Knepley   Level: developer
1356bd041c0cSMatthew G. Knepley 
1357bb7acecfSBarry Smith   Notes:
1358bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1359bb7acecfSBarry Smith 
136020f4b53cSBarry 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()`
1361bb7acecfSBarry Smith 
136242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1363bd041c0cSMatthew G. Knepley @*/
1364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1365d71ae5a4SJacob Faibussowitsch {
1366bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1367b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1368b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13694f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13705b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1371dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13725b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1374b4937a87SMatthew G. Knepley }
1375b4937a87SMatthew G. Knepley 
1376b4937a87SMatthew G. Knepley /*@
1377bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1378b4937a87SMatthew G. Knepley 
137920f4b53cSBarry Smith   Collective
1380b4937a87SMatthew G. Knepley 
1381b4937a87SMatthew G. Knepley   Input Parameter:
1382bb7acecfSBarry Smith . dm - the `DM` object
1383b4937a87SMatthew G. Knepley 
1384b4937a87SMatthew G. Knepley   Output Parameter:
1385bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Level: developer
1388b4937a87SMatthew G. Knepley 
1389bb7acecfSBarry Smith   Note:
1390bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1391bb7acecfSBarry Smith 
139260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1393b4937a87SMatthew G. Knepley @*/
1394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1395d71ae5a4SJacob Faibussowitsch {
1396b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1397b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13984f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1399dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1401bd041c0cSMatthew G. Knepley }
1402bd041c0cSMatthew G. Knepley 
1403bd041c0cSMatthew G. Knepley /*@
1404bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1405bb7acecfSBarry Smith   of a PDE on the `DM`.
140647c6ae99SBarry Smith 
140720f4b53cSBarry Smith   Collective
140847c6ae99SBarry Smith 
1409d8d19677SJose E. Roman   Input Parameters:
1410bb7acecfSBarry Smith + dm    - the `DM` object
1411bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141247c6ae99SBarry Smith 
141347c6ae99SBarry Smith   Output Parameter:
141447c6ae99SBarry Smith . coloring - the coloring
141547c6ae99SBarry Smith 
14161bf8429eSBarry Smith   Level: developer
14171bf8429eSBarry Smith 
1418ec5066bdSBarry Smith   Notes:
1419bb7acecfSBarry 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
1420bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1421ec5066bdSBarry Smith 
1422bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14231bf8429eSBarry 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,
14241bf8429eSBarry Smith   otherwise an error will be generated.
1425ec5066bdSBarry Smith 
14261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1427aab9d709SJed Brown @*/
1428d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1429d71ae5a4SJacob Faibussowitsch {
143047c6ae99SBarry Smith   PetscFunctionBegin;
1431171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14324f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1433dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143547c6ae99SBarry Smith }
143647c6ae99SBarry Smith 
1437b412c318SBarry Smith /*@
1438bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
143947c6ae99SBarry Smith 
144020f4b53cSBarry Smith   Collective
144147c6ae99SBarry Smith 
144247c6ae99SBarry Smith   Input Parameter:
1443bb7acecfSBarry Smith . dm - the `DM` object
144447c6ae99SBarry Smith 
144547c6ae99SBarry Smith   Output Parameter:
144647c6ae99SBarry Smith . mat - the empty Jacobian
144747c6ae99SBarry Smith 
144820f4b53cSBarry Smith   Options Database Key:
1449bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1450f27dd7c6SMatthew G. Knepley 
145120f4b53cSBarry Smith   Level: beginner
145220f4b53cSBarry Smith 
145395452b02SPatrick Sanan   Notes:
145495452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
145594013140SBarry Smith   do not need to do it yourself.
145694013140SBarry Smith 
145794013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1458bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
145994013140SBarry Smith 
1460bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146194013140SBarry Smith   internally by PETSc.
146294013140SBarry Smith 
1463bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1464bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146594013140SBarry Smith 
14661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1467aab9d709SJed Brown @*/
1468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1469d71ae5a4SJacob Faibussowitsch {
147047c6ae99SBarry Smith   PetscFunctionBegin;
1471171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14724f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14739566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14749566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1475dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1477c6b011d8SStefano Zampini     DM mdm;
1478c6b011d8SStefano Zampini 
14799566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14807a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1481c6b011d8SStefano Zampini   }
1482e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1483e5e52638SMatthew G. Knepley   if (dm->Nf) {
1484e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1485649ef022SMatthew Knepley     PetscInt     Nf, f;
1486e571a35bSMatthew G. Knepley 
14879566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1488649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1489649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14909566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14919566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14929566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1493649ef022SMatthew Knepley         break;
1494e571a35bSMatthew G. Knepley       }
1495649ef022SMatthew Knepley     }
1496649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1497649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
14989566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
14999566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15009566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1501e571a35bSMatthew G. Knepley       }
1502e571a35bSMatthew G. Knepley     }
1503e571a35bSMatthew G. Knepley   }
15049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150647c6ae99SBarry Smith }
150747c6ae99SBarry Smith 
1508732e2eb9SMatthew G Knepley /*@
1509a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1510a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1511a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1512aa0f6e3cSJed Brown 
151320f4b53cSBarry Smith   Logically Collective
1514aa0f6e3cSJed Brown 
1515aa0f6e3cSJed Brown   Input Parameters:
1516bb7acecfSBarry Smith + dm   - the `DM`
1517bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1518aa0f6e3cSJed Brown 
1519aa0f6e3cSJed Brown   Level: developer
1520aa0f6e3cSJed Brown 
1521a4e35b19SJacob Faibussowitsch   Notes:
1522a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1523a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1524a4e35b19SJacob Faibussowitsch 
15251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1526aa0f6e3cSJed Brown @*/
1527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1528d71ae5a4SJacob Faibussowitsch {
1529aa0f6e3cSJed Brown   PetscFunctionBegin;
1530aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1531aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1533aa0f6e3cSJed Brown }
1534aa0f6e3cSJed Brown 
1535aa0f6e3cSJed Brown /*@
1536bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1537732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1538732e2eb9SMatthew G Knepley 
153920f4b53cSBarry Smith   Logically Collective
1540732e2eb9SMatthew G Knepley 
1541d8d19677SJose E. Roman   Input Parameters:
1542bb7acecfSBarry Smith + dm   - the `DM`
1543bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1544732e2eb9SMatthew G Knepley 
154520f4b53cSBarry Smith   Options Database Key:
1546bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1547f27dd7c6SMatthew G. Knepley 
154820f4b53cSBarry Smith   Level: developer
154920f4b53cSBarry Smith 
15501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1551732e2eb9SMatthew G Knepley @*/
1552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1553d71ae5a4SJacob Faibussowitsch {
1554732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1555732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1556732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1558732e2eb9SMatthew G Knepley }
1559732e2eb9SMatthew G Knepley 
1560b06ff27eSHong Zhang /*@
1561bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1562bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1563b06ff27eSHong Zhang 
156420f4b53cSBarry Smith   Logically Collective
1565b06ff27eSHong Zhang 
1566d8d19677SJose E. Roman   Input Parameters:
1567bb7acecfSBarry Smith + dm   - the `DM`
1568da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1569b06ff27eSHong Zhang 
1570b06ff27eSHong Zhang   Level: developer
1571bb7acecfSBarry Smith 
15721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1573b06ff27eSHong Zhang @*/
1574d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1575d71ae5a4SJacob Faibussowitsch {
1576b06ff27eSHong Zhang   PetscFunctionBegin;
1577b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1578b06ff27eSHong Zhang   dm->structure_only = only;
15793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1580b06ff27eSHong Zhang }
1581b06ff27eSHong Zhang 
1582863027abSJed Brown /*@
1583863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1584863027abSJed Brown 
158520f4b53cSBarry Smith   Logically Collective
1586863027abSJed Brown 
1587863027abSJed Brown   Input Parameters:
1588863027abSJed Brown + dm    - the `DM`
1589863027abSJed Brown - btype - block by topological point or field node
1590863027abSJed Brown 
159120f4b53cSBarry Smith   Options Database Key:
15920e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1593863027abSJed Brown 
159420f4b53cSBarry Smith   Level: advanced
159520f4b53cSBarry Smith 
15961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1597863027abSJed Brown @*/
1598863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1599863027abSJed Brown {
1600863027abSJed Brown   PetscFunctionBegin;
1601863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1602863027abSJed Brown   dm->blocking_type = btype;
16033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1604863027abSJed Brown }
1605863027abSJed Brown 
1606863027abSJed Brown /*@
1607863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1608863027abSJed Brown 
1609863027abSJed Brown   Not Collective
1610863027abSJed Brown 
16112fe279fdSBarry Smith   Input Parameter:
1612863027abSJed Brown . dm - the `DM`
1613863027abSJed Brown 
16142fe279fdSBarry Smith   Output Parameter:
1615863027abSJed Brown . btype - block by topological point or field node
1616863027abSJed Brown 
1617863027abSJed Brown   Level: advanced
1618863027abSJed Brown 
16191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1620863027abSJed Brown @*/
1621863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1622863027abSJed Brown {
1623863027abSJed Brown   PetscFunctionBegin;
1624863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16254f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1626863027abSJed Brown   *btype = dm->blocking_type;
16273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1628863027abSJed Brown }
1629863027abSJed Brown 
1630a89ea682SMatthew G Knepley /*@C
1631bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1632a89ea682SMatthew G Knepley 
1633a89ea682SMatthew G Knepley   Not Collective
1634a89ea682SMatthew G Knepley 
1635a89ea682SMatthew G Knepley   Input Parameters:
1636bb7acecfSBarry Smith + dm    - the `DM` object
1637a5b23f4aSJose E. Roman . count - The minimum size
163820f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1639a89ea682SMatthew G Knepley 
1640a89ea682SMatthew G Knepley   Output Parameter:
164160225df5SJacob Faibussowitsch . mem - the work array
1642a89ea682SMatthew G Knepley 
1643a89ea682SMatthew G Knepley   Level: developer
1644a89ea682SMatthew G Knepley 
1645bb7acecfSBarry Smith   Note:
1646da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1647bb7acecfSBarry Smith 
1648bb7acecfSBarry Smith   The array may contain nonzero values
1649bb7acecfSBarry Smith 
16501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1651a89ea682SMatthew G Knepley @*/
1652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1653d71ae5a4SJacob Faibussowitsch {
1654aa1993deSMatthew G Knepley   DMWorkLink  link;
165569291d52SBarry Smith   PetscMPIInt dsize;
1656a89ea682SMatthew G Knepley 
1657a89ea682SMatthew G Knepley   PetscFunctionBegin;
1658a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16594f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1660442f3b32SStefano Zampini   if (!count) {
1661442f3b32SStefano Zampini     *(void **)mem = NULL;
1662442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1663442f3b32SStefano Zampini   }
1664aa1993deSMatthew G Knepley   if (dm->workin) {
1665aa1993deSMatthew G Knepley     link       = dm->workin;
1666aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1667aa1993deSMatthew G Knepley   } else {
16684dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1669a89ea682SMatthew G Knepley   }
1670b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1671b77fa653SStefano Zampini      Get size directly */
1672b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1673b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1674b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1675b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1676b77fa653SStefano Zampini #endif
1677b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1678b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1679b77fa653SStefano Zampini #endif
1680b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1681b77fa653SStefano Zampini 
16825056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16839566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16849566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1685854ce69bSBarry Smith     link->bytes = dsize * count;
1686aa1993deSMatthew G Knepley   }
1687aa1993deSMatthew G Knepley   link->next  = dm->workout;
1688aa1993deSMatthew G Knepley   dm->workout = link;
1689cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
169000d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
169100d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
169200d952a4SJed Brown #endif
1693aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1695a89ea682SMatthew G Knepley }
1696a89ea682SMatthew G Knepley 
1697aa1993deSMatthew G Knepley /*@C
1698bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1699aa1993deSMatthew G Knepley 
1700aa1993deSMatthew G Knepley   Not Collective
1701aa1993deSMatthew G Knepley 
1702aa1993deSMatthew G Knepley   Input Parameters:
1703bb7acecfSBarry Smith + dm    - the `DM` object
1704a5b23f4aSJose E. Roman . count - The minimum size
170520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1706aa1993deSMatthew G Knepley 
1707aa1993deSMatthew G Knepley   Output Parameter:
170860225df5SJacob Faibussowitsch . mem - the work array
1709aa1993deSMatthew G Knepley 
1710aa1993deSMatthew G Knepley   Level: developer
1711aa1993deSMatthew G Knepley 
171260225df5SJacob Faibussowitsch   Developer Notes:
1713bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1714147403d9SBarry Smith 
17151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1716aa1993deSMatthew G Knepley @*/
1717d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1718d71ae5a4SJacob Faibussowitsch {
1719aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1720aa1993deSMatthew G Knepley 
1721aa1993deSMatthew G Knepley   PetscFunctionBegin;
1722aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17234f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1724a4e35b19SJacob Faibussowitsch   (void)count;
1725a4e35b19SJacob Faibussowitsch   (void)dtype;
1726442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1727aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1728aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1729aa1993deSMatthew G Knepley       *p            = link->next;
1730aa1993deSMatthew G Knepley       link->next    = dm->workin;
1731aa1993deSMatthew G Knepley       dm->workin    = link;
17320298fd71SBarry Smith       *(void **)mem = NULL;
17333ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1734aa1993deSMatthew G Knepley     }
1735aa1993deSMatthew G Knepley   }
1736aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1737aa1993deSMatthew G Knepley }
1738e7c4fc90SDmitry Karpeev 
17398cda7954SMatthew G. Knepley /*@C
1740bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1741bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17428cda7954SMatthew G. Knepley 
174320f4b53cSBarry Smith   Logically Collective; No Fortran Support
17448cda7954SMatthew G. Knepley 
17458cda7954SMatthew G. Knepley   Input Parameters:
1746bb7acecfSBarry Smith + dm     - The `DM`
17478cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17488cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17498cda7954SMatthew G. Knepley 
175020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1751bb7acecfSBarry Smith + dm        - The present `DM`
1752bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1753147403d9SBarry Smith . field     - The field number in dm
1754147403d9SBarry Smith - nullSpace - The nullspace for the given field
17558cda7954SMatthew G. Knepley 
175649762cbcSSatish Balay   Level: intermediate
175749762cbcSSatish Balay 
17581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1759147403d9SBarry Smith @*/
1760a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1761d71ae5a4SJacob Faibussowitsch {
1762435a35e8SMatthew G Knepley   PetscFunctionBegin;
1763435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17647a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1765435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1767435a35e8SMatthew G Knepley }
1768435a35e8SMatthew G Knepley 
17698cda7954SMatthew G. Knepley /*@C
1770bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17718cda7954SMatthew G. Knepley 
177220f4b53cSBarry Smith   Not Collective; No Fortran Support
17738cda7954SMatthew G. Knepley 
17748cda7954SMatthew G. Knepley   Input Parameters:
1775bb7acecfSBarry Smith + dm    - The `DM`
17768cda7954SMatthew G. Knepley - field - The field number for the nullspace
17778cda7954SMatthew G. Knepley 
17788cda7954SMatthew G. Knepley   Output Parameter:
17798cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17808cda7954SMatthew G. Knepley 
178120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1782147403d9SBarry Smith + dm        - The present DM
1783147403d9SBarry Smith . origField - The field number given above, in the original DM
1784147403d9SBarry Smith . field     - The field number in dm
1785147403d9SBarry Smith - nullSpace - The nullspace for the given field
17868cda7954SMatthew G. Knepley 
178749762cbcSSatish Balay   Level: intermediate
178849762cbcSSatish Balay 
17891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1790147403d9SBarry Smith @*/
1791a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1792d71ae5a4SJacob Faibussowitsch {
17930a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17940a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17954f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
17967a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17970a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17990a50eb56SMatthew G. Knepley }
18000a50eb56SMatthew G. Knepley 
18018cda7954SMatthew G. Knepley /*@C
1802bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18038cda7954SMatthew G. Knepley 
180420f4b53cSBarry Smith   Logically Collective; No Fortran Support
18058cda7954SMatthew G. Knepley 
18068cda7954SMatthew G. Knepley   Input Parameters:
1807bb7acecfSBarry Smith + dm     - The `DM`
18088cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18098cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18108cda7954SMatthew G. Knepley 
181120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1812bb7acecfSBarry Smith + dm        - The present `DM`
1813bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1814147403d9SBarry Smith . field     - The field number in dm
1815147403d9SBarry Smith - nullSpace - The nullspace for the given field
18168cda7954SMatthew G. Knepley 
181749762cbcSSatish Balay   Level: intermediate
181849762cbcSSatish Balay 
18191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1820bb7acecfSBarry Smith           `MatNullSpace`
1821147403d9SBarry Smith @*/
1822a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1823d71ae5a4SJacob Faibussowitsch {
1824f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1825f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18267a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1827f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1829f9d4088aSMatthew G. Knepley }
1830f9d4088aSMatthew G. Knepley 
18318cda7954SMatthew G. Knepley /*@C
1832bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18338cda7954SMatthew G. Knepley 
183420f4b53cSBarry Smith   Not Collective; No Fortran Support
18358cda7954SMatthew G. Knepley 
18368cda7954SMatthew G. Knepley   Input Parameters:
1837bb7acecfSBarry Smith + dm    - The `DM`
18388cda7954SMatthew G. Knepley - field - The field number for the nullspace
18398cda7954SMatthew G. Knepley 
18408cda7954SMatthew G. Knepley   Output Parameter:
18418cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18428cda7954SMatthew G. Knepley 
184320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1844bb7acecfSBarry Smith + dm        - The present `DM`
1845bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1846147403d9SBarry Smith . field     - The field number in dm
1847147403d9SBarry Smith - nullSpace - The nullspace for the given field
18488cda7954SMatthew G. Knepley 
184949762cbcSSatish Balay   Level: intermediate
185049762cbcSSatish Balay 
18511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1852bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1853147403d9SBarry Smith @*/
1854a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1855d71ae5a4SJacob Faibussowitsch {
1856f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1857f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18584f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18597a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1860f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1862f9d4088aSMatthew G. Knepley }
1863f9d4088aSMatthew G. Knepley 
18644f3b5142SJed Brown /*@C
1865bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18664d343eeaSMatthew G Knepley 
186720f4b53cSBarry Smith   Not Collective; No Fortran Support
18684d343eeaSMatthew G Knepley 
18694d343eeaSMatthew G Knepley   Input Parameter:
1870bb7acecfSBarry Smith . dm - the `DM` object
18714d343eeaSMatthew G Knepley 
18724d343eeaSMatthew G Knepley   Output Parameters:
187320f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
187420f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
187520f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18764d343eeaSMatthew G Knepley 
18774d343eeaSMatthew G Knepley   Level: intermediate
18784d343eeaSMatthew G Knepley 
1879bb7acecfSBarry Smith   Note:
188021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1881bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1882bb7acecfSBarry Smith   `PetscFree()`.
188321c9b008SJed Brown 
188460225df5SJacob Faibussowitsch   Developer Notes:
1885bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1886bb7acecfSBarry Smith   likely be removed.
1887bb7acecfSBarry Smith 
18881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1889bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18904d343eeaSMatthew G Knepley @*/
1891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1892d71ae5a4SJacob Faibussowitsch {
189337d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18944d343eeaSMatthew G Knepley 
18954d343eeaSMatthew G Knepley   PetscFunctionBegin;
18964d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
189769ca1f37SDmitry Karpeev   if (numFields) {
18984f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
189969ca1f37SDmitry Karpeev     *numFields = 0;
190069ca1f37SDmitry Karpeev   }
190137d0c07bSMatthew G Knepley   if (fieldNames) {
19024f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19030298fd71SBarry Smith     *fieldNames = NULL;
190469ca1f37SDmitry Karpeev   }
190569ca1f37SDmitry Karpeev   if (fields) {
19064f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19070298fd71SBarry Smith     *fields = NULL;
190869ca1f37SDmitry Karpeev   }
19099566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191037d0c07bSMatthew G Knepley   if (section) {
19113a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
191237d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
191337d0c07bSMatthew G Knepley 
19149566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19169566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19179566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
191837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
191937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19209566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192137d0c07bSMatthew G Knepley     }
192237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
192337d0c07bSMatthew G Knepley       PetscInt gdof;
192437d0c07bSMatthew G Knepley 
19259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
192637d0c07bSMatthew G Knepley       if (gdof > 0) {
192737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19283a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
192937d0c07bSMatthew G Knepley 
19309566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19319566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19323a544194SStefano Zampini           fpdof = fdof - fcdof;
19333a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19343a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19353a544194SStefano Zampini             fieldNc[f] = 1;
19363a544194SStefano Zampini           }
19373a544194SStefano Zampini           fieldSizes[f] += fpdof;
193837d0c07bSMatthew G Knepley         }
193937d0c07bSMatthew G Knepley       }
194037d0c07bSMatthew G Knepley     }
194137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19429566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
194337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
194437d0c07bSMatthew G Knepley     }
194537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
194737d0c07bSMatthew G Knepley 
19489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194937d0c07bSMatthew G Knepley       if (gdof > 0) {
19509566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
195237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
195337d0c07bSMatthew G Knepley 
19549566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19559566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1956ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
195737d0c07bSMatthew G Knepley         }
195837d0c07bSMatthew G Knepley       }
195937d0c07bSMatthew G Knepley     }
19608865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196137d0c07bSMatthew G Knepley     if (fieldNames) {
19629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
196337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
196437d0c07bSMatthew G Knepley         const char *fieldName;
196537d0c07bSMatthew G Knepley 
19669566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19679566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
196837d0c07bSMatthew G Knepley       }
196937d0c07bSMatthew G Knepley     }
197037d0c07bSMatthew G Knepley     if (fields) {
19719566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
197237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19733a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19743a544194SStefano Zampini 
19759566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19763a544194SStefano Zampini         in[0] = -fieldNc[f];
19773a544194SStefano Zampini         in[1] = fieldNc[f];
19781c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19793a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19809566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198137d0c07bSMatthew G Knepley       }
198237d0c07bSMatthew G Knepley     }
19839566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1984dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19864d343eeaSMatthew G Knepley }
19874d343eeaSMatthew G Knepley 
198816621825SDmitry Karpeev /*@C
1989bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1990a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1991e7c4fc90SDmitry Karpeev 
199220f4b53cSBarry Smith   Not Collective; No Fortran Support
1993e7c4fc90SDmitry Karpeev 
1994e7c4fc90SDmitry Karpeev   Input Parameter:
1995bb7acecfSBarry Smith . dm - the `DM` object
1996e7c4fc90SDmitry Karpeev 
1997e7c4fc90SDmitry Karpeev   Output Parameters:
199820f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
199920f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
200020f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
200120f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2002e7c4fc90SDmitry Karpeev 
2003e7c4fc90SDmitry Karpeev   Level: intermediate
2004e7c4fc90SDmitry Karpeev 
2005a4e35b19SJacob Faibussowitsch   Notes:
2006a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2007a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2008a4e35b19SJacob Faibussowitsch 
2009a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2010a4e35b19SJacob Faibussowitsch 
2011e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2012bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2013bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2014e7c4fc90SDmitry Karpeev 
201560225df5SJacob Faibussowitsch   Developer Notes:
2016bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2017bb7acecfSBarry Smith 
201860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2019e7c4fc90SDmitry Karpeev @*/
2020d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2021d71ae5a4SJacob Faibussowitsch {
2022e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2023e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20248865f1eaSKarl Rupp   if (len) {
20254f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20268865f1eaSKarl Rupp     *len = 0;
20278865f1eaSKarl Rupp   }
20288865f1eaSKarl Rupp   if (namelist) {
20294f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2030ea78f98cSLisandro Dalcin     *namelist = NULL;
20318865f1eaSKarl Rupp   }
20328865f1eaSKarl Rupp   if (islist) {
20334f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2034ea78f98cSLisandro Dalcin     *islist = NULL;
20358865f1eaSKarl Rupp   }
20368865f1eaSKarl Rupp   if (dmlist) {
20374f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2038ea78f98cSLisandro Dalcin     *dmlist = NULL;
20398865f1eaSKarl Rupp   }
2040f3f0edfdSDmitry Karpeev   /*
2041f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2042f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2043f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2044f3f0edfdSDmitry Karpeev    */
20457a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
204616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2047435a35e8SMatthew G Knepley     PetscSection section;
2048435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2049435a35e8SMatthew G Knepley 
20509566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20519566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2052435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2053f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20549566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20559566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20569566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2057435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2058435a35e8SMatthew G Knepley         const char *fieldName;
2059435a35e8SMatthew G Knepley 
20609566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
206103dc3394SMatthew G. Knepley         if (namelist) {
20629566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20639566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2064435a35e8SMatthew G Knepley         }
206503dc3394SMatthew G. Knepley       }
2066435a35e8SMatthew G Knepley     } else {
20679566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2068e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20690298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2070e7c4fc90SDmitry Karpeev     }
2071dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
207316621825SDmitry Karpeev }
207416621825SDmitry Karpeev 
2075412a4547SAlexis Marboeuf /*@C
207620f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
207720f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2078435a35e8SMatthew G Knepley 
2079435a35e8SMatthew G Knepley   Not collective
2080435a35e8SMatthew G Knepley 
2081435a35e8SMatthew G Knepley   Input Parameters:
2082bb7acecfSBarry Smith + dm        - The `DM` object
2083bb7acecfSBarry Smith . numFields - The number of fields to select
20842adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2085435a35e8SMatthew G Knepley 
2086435a35e8SMatthew G Knepley   Output Parameters:
2087bb7acecfSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`
2088bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2089435a35e8SMatthew G Knepley 
209020f4b53cSBarry Smith   Level: intermediate
209120f4b53cSBarry Smith 
2092bb7acecfSBarry Smith   Note:
2093bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20945d3b26e6SMatthew G. Knepley 
209560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2096435a35e8SMatthew G Knepley @*/
2097d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2098d71ae5a4SJacob Faibussowitsch {
2099435a35e8SMatthew G Knepley   PetscFunctionBegin;
2100435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21014f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21024f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21034f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2104dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2106435a35e8SMatthew G Knepley }
2107435a35e8SMatthew G Knepley 
21082adcc780SMatthew G. Knepley /*@C
2109bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21102adcc780SMatthew G. Knepley 
21112adcc780SMatthew G. Knepley   Not collective
21122adcc780SMatthew G. Knepley 
2113d8d19677SJose E. Roman   Input Parameters:
2114bb7acecfSBarry Smith + dms - The `DM` objects
2115bb7acecfSBarry Smith - n   - The number of `DM`s
21162adcc780SMatthew G. Knepley 
21172adcc780SMatthew G. Knepley   Output Parameters:
2118bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2119bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21202adcc780SMatthew G. Knepley 
212120f4b53cSBarry Smith   Level: intermediate
212220f4b53cSBarry Smith 
2123bb7acecfSBarry Smith   Note:
2124bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21255d3b26e6SMatthew G. Knepley 
21261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21272adcc780SMatthew G. Knepley @*/
2128d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2129d71ae5a4SJacob Faibussowitsch {
21302adcc780SMatthew G. Knepley   PetscInt i;
21312adcc780SMatthew G. Knepley 
21322adcc780SMatthew G. Knepley   PetscFunctionBegin;
21334f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2134ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21354f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21364f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2137bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2138bb7acecfSBarry Smith   if (n) {
2139b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2140dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21412adcc780SMatthew G. Knepley   }
21423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21432adcc780SMatthew G. Knepley }
21442adcc780SMatthew G. Knepley 
214516621825SDmitry Karpeev /*@C
2146a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2147a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
214816621825SDmitry Karpeev 
214920f4b53cSBarry Smith   Not Collective
215016621825SDmitry Karpeev 
215116621825SDmitry Karpeev   Input Parameter:
2152bb7acecfSBarry Smith . dm - the `DM` object
215316621825SDmitry Karpeev 
215416621825SDmitry Karpeev   Output Parameters:
215520f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
215620f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21570298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21580298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
215920f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
216016621825SDmitry Karpeev 
216116621825SDmitry Karpeev   Level: intermediate
216216621825SDmitry Karpeev 
2163bb7acecfSBarry Smith   Note:
2164a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2165a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2166a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2167a4e35b19SJacob Faibussowitsch 
2168a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2169a4e35b19SJacob Faibussowitsch 
217016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2171bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2172bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217316621825SDmitry Karpeev 
2174a4e35b19SJacob Faibussowitsch   Developer Notes:
217520f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2176bb7acecfSBarry Smith 
217760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
217816621825SDmitry Karpeev @*/
2179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2180d71ae5a4SJacob Faibussowitsch {
2181be081cd6SPeter Brune   DMSubDomainHookLink link;
2182be081cd6SPeter Brune   PetscInt            i, l;
218316621825SDmitry Karpeev 
218416621825SDmitry Karpeev   PetscFunctionBegin;
218516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21869371c9d4SSatish Balay   if (n) {
21874f572ea9SToby Isaac     PetscAssertPointer(n, 2);
21889371c9d4SSatish Balay     *n = 0;
21899371c9d4SSatish Balay   }
21909371c9d4SSatish Balay   if (namelist) {
21914f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
21929371c9d4SSatish Balay     *namelist = NULL;
21939371c9d4SSatish Balay   }
21949371c9d4SSatish Balay   if (innerislist) {
21954f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
21969371c9d4SSatish Balay     *innerislist = NULL;
21979371c9d4SSatish Balay   }
21989371c9d4SSatish Balay   if (outerislist) {
21994f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22009371c9d4SSatish Balay     *outerislist = NULL;
22019371c9d4SSatish Balay   }
22029371c9d4SSatish Balay   if (dmlist) {
22034f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22049371c9d4SSatish Balay     *dmlist = NULL;
22059371c9d4SSatish Balay   }
2206f3f0edfdSDmitry Karpeev   /*
2207f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2208f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2209f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2210f3f0edfdSDmitry Karpeev    */
22117a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2213dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
221414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2215f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2216be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2217be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22189566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2219be081cd6SPeter Brune         }
2220648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2221e7c4fc90SDmitry Karpeev       }
222214a18fd3SPeter Brune     }
2223bb7acecfSBarry Smith     if (n) *n = l;
222414a18fd3SPeter Brune   }
22253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2226e30e807fSPeter Brune }
2227e30e807fSPeter Brune 
2228e30e807fSPeter Brune /*@C
2229e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2230e30e807fSPeter Brune 
223120f4b53cSBarry Smith   Not Collective
2232e30e807fSPeter Brune 
2233e30e807fSPeter Brune   Input Parameters:
2234bb7acecfSBarry Smith + dm     - the `DM` object
2235e30e807fSPeter Brune . n      - the number of subdomain scatters
2236e30e807fSPeter Brune - subdms - the local subdomains
2237e30e807fSPeter Brune 
2238e30e807fSPeter Brune   Output Parameters:
22396b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2240e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2241e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2242e30e807fSPeter Brune 
224320f4b53cSBarry Smith   Level: developer
224420f4b53cSBarry Smith 
2245bb7acecfSBarry Smith   Note:
2246bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2247e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2248e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2249e30e807fSPeter Brune   solution and residual data.
2250e30e807fSPeter Brune 
2251a4e35b19SJacob Faibussowitsch   Developer Notes:
2252a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2253a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2254bb7acecfSBarry Smith 
22551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2256e30e807fSPeter Brune @*/
2257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2258d71ae5a4SJacob Faibussowitsch {
2259e30e807fSPeter Brune   PetscFunctionBegin;
2260e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22614f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2262dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2264e7c4fc90SDmitry Karpeev }
2265e7c4fc90SDmitry Karpeev 
226647c6ae99SBarry Smith /*@
2267bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
226847c6ae99SBarry Smith 
226920f4b53cSBarry Smith   Collective
227047c6ae99SBarry Smith 
2271d8d19677SJose E. Roman   Input Parameters:
2272bb7acecfSBarry Smith + dm   - the `DM` object
2273bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227447c6ae99SBarry Smith 
227547c6ae99SBarry Smith   Output Parameter:
227620f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2277ae0a1c52SMatthew G Knepley 
227820f4b53cSBarry Smith   Options Database Key:
2279412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2280412e9a14SMatthew G. Knepley 
228147c6ae99SBarry Smith   Level: developer
228247c6ae99SBarry Smith 
228320f4b53cSBarry Smith   Note:
228420f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
228520f4b53cSBarry Smith 
22861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
228747c6ae99SBarry Smith @*/
2288d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2289d71ae5a4SJacob Faibussowitsch {
2290c833c3b5SJed Brown   DMRefineHookLink link;
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith   PetscFunctionBegin;
2293732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2295dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22964057135bSMatthew G Knepley   if (*dmf) {
229743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22988865f1eaSKarl Rupp 
22999566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23008865f1eaSKarl Rupp 
2301644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23020598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2303656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23048865f1eaSKarl Rupp 
23059566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2306c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23071baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2308c833c3b5SJed Brown     }
2309c833c3b5SJed Brown   }
23109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2312c833c3b5SJed Brown }
2313c833c3b5SJed Brown 
2314bb9467b5SJed Brown /*@C
2315c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2316c833c3b5SJed Brown 
231720f4b53cSBarry Smith   Logically Collective; No Fortran Support
2318c833c3b5SJed Brown 
23194165533cSJose E. Roman   Input Parameters:
2320bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2321bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2322f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
232320f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2324c833c3b5SJed Brown 
232520f4b53cSBarry Smith   Calling sequence of `refinehook`:
2326bb7acecfSBarry Smith + coarse - coarse level `DM`
2327bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2328c833c3b5SJed Brown - ctx    - optional user-defined function context
2329c833c3b5SJed Brown 
233020f4b53cSBarry Smith   Calling sequence of `interphook`:
2331bb7acecfSBarry Smith + coarse - coarse level `DM`
2332c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2333bb7acecfSBarry Smith . fine   - fine level `DM` to update
2334c833c3b5SJed Brown - ctx    - optional user-defined function context
2335c833c3b5SJed Brown 
2336c833c3b5SJed Brown   Level: advanced
2337c833c3b5SJed Brown 
2338c833c3b5SJed Brown   Notes:
2339bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2340bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2341bb7acecfSBarry Smith 
2342bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2343c833c3b5SJed Brown 
2344c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2345c833c3b5SJed Brown 
23461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2347c833c3b5SJed Brown @*/
2348a4e35b19SJacob 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)
2349d71ae5a4SJacob Faibussowitsch {
2350c833c3b5SJed Brown   DMRefineHookLink link, *p;
2351c833c3b5SJed Brown 
2352c833c3b5SJed Brown   PetscFunctionBegin;
2353c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23543d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23553ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23563d8e3701SJed Brown   }
23579566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2358c833c3b5SJed Brown   link->refinehook = refinehook;
2359c833c3b5SJed Brown   link->interphook = interphook;
2360c833c3b5SJed Brown   link->ctx        = ctx;
23610298fd71SBarry Smith   link->next       = NULL;
2362c833c3b5SJed Brown   *p               = link;
23633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2364c833c3b5SJed Brown }
2365c833c3b5SJed Brown 
23663d8e3701SJed Brown /*@C
2367bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2368bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23693d8e3701SJed Brown 
237020f4b53cSBarry Smith   Logically Collective; No Fortran Support
23713d8e3701SJed Brown 
23724165533cSJose E. Roman   Input Parameters:
2373bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2374bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2375bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
237620f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23773d8e3701SJed Brown 
23783d8e3701SJed Brown   Level: advanced
23793d8e3701SJed Brown 
2380bb7acecfSBarry Smith   Note:
23813d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23823d8e3701SJed Brown 
23831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23843d8e3701SJed Brown @*/
2385d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2386d71ae5a4SJacob Faibussowitsch {
23873d8e3701SJed Brown   DMRefineHookLink link, *p;
23883d8e3701SJed Brown 
23893d8e3701SJed Brown   PetscFunctionBegin;
23903d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23913d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23923d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23933d8e3701SJed Brown       link = *p;
23943d8e3701SJed Brown       *p   = link->next;
23959566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23963d8e3701SJed Brown       break;
23973d8e3701SJed Brown     }
23983d8e3701SJed Brown   }
23993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24003d8e3701SJed Brown }
24013d8e3701SJed Brown 
2402c833c3b5SJed Brown /*@
2403bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2404c833c3b5SJed Brown 
2405c833c3b5SJed Brown   Collective if any hooks are
2406c833c3b5SJed Brown 
24074165533cSJose E. Roman   Input Parameters:
2408bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2409bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2410bb7acecfSBarry Smith - fine   - finer `DM` to update
2411c833c3b5SJed Brown 
2412c833c3b5SJed Brown   Level: developer
2413c833c3b5SJed Brown 
241460225df5SJacob Faibussowitsch   Developer Notes:
2415bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2416bb7acecfSBarry Smith   an API with consistent terminology.
2417bb7acecfSBarry Smith 
24181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2419c833c3b5SJed Brown @*/
2420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2421d71ae5a4SJacob Faibussowitsch {
2422c833c3b5SJed Brown   DMRefineHookLink link;
2423c833c3b5SJed Brown 
2424c833c3b5SJed Brown   PetscFunctionBegin;
2425c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24261baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24274057135bSMatthew G Knepley   }
24283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
242947c6ae99SBarry Smith }
243047c6ae99SBarry Smith 
2431eb3f98d2SBarry Smith /*@
24321f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24331f3379b2SToby Isaac 
243420f4b53cSBarry Smith   Collective
24351f3379b2SToby Isaac 
24364165533cSJose E. Roman   Input Parameters:
2437bb7acecfSBarry Smith + coarse    - coarse `DM`
2438bb7acecfSBarry Smith . fine      - fine `DM`
2439bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2440bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2441bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24421f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24431f3379b2SToby Isaac 
24444165533cSJose E. Roman   Output Parameter:
24451f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24461f3379b2SToby Isaac 
24471f3379b2SToby Isaac   Level: developer
24481f3379b2SToby Isaac 
2449bb7acecfSBarry Smith   Note:
2450bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24511f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24521f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24531f3379b2SToby Isaac   slope-limiting reconstruction.
24541f3379b2SToby Isaac 
245560225df5SJacob Faibussowitsch   Developer Notes:
2456bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2457bb7acecfSBarry Smith 
24581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24591f3379b2SToby Isaac @*/
2460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2461d71ae5a4SJacob Faibussowitsch {
24621f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24631f3379b2SToby Isaac 
24641f3379b2SToby Isaac   PetscFunctionBegin;
24651f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24661f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24671f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24681f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24691f3379b2SToby Isaac 
24709566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24711f3379b2SToby Isaac   if (interpsol) {
24729566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24731f3379b2SToby Isaac   } else if (interp) {
24749566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
247598921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24771f3379b2SToby Isaac }
24781f3379b2SToby Isaac 
24791f3379b2SToby Isaac /*@
2480bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2481eb3f98d2SBarry Smith 
2482eb3f98d2SBarry Smith   Not Collective
2483eb3f98d2SBarry Smith 
2484eb3f98d2SBarry Smith   Input Parameter:
2485bb7acecfSBarry Smith . dm - the `DM` object
2486eb3f98d2SBarry Smith 
2487eb3f98d2SBarry Smith   Output Parameter:
2488eb3f98d2SBarry Smith . level - number of refinements
2489eb3f98d2SBarry Smith 
2490eb3f98d2SBarry Smith   Level: developer
2491eb3f98d2SBarry Smith 
2492bb7acecfSBarry Smith   Note:
2493bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2494bb7acecfSBarry Smith 
24951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2496eb3f98d2SBarry Smith @*/
2497d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2498d71ae5a4SJacob Faibussowitsch {
2499eb3f98d2SBarry Smith   PetscFunctionBegin;
2500eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2501eb3f98d2SBarry Smith   *level = dm->levelup;
25023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2503eb3f98d2SBarry Smith }
2504eb3f98d2SBarry Smith 
2505fef3a512SBarry Smith /*@
2506bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2507fef3a512SBarry Smith 
2508fef3a512SBarry Smith   Not Collective
2509fef3a512SBarry Smith 
2510d8d19677SJose E. Roman   Input Parameters:
2511bb7acecfSBarry Smith + dm    - the `DM` object
2512fef3a512SBarry Smith - level - number of refinements
2513fef3a512SBarry Smith 
2514fef3a512SBarry Smith   Level: advanced
2515fef3a512SBarry Smith 
251695452b02SPatrick Sanan   Notes:
2517bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2518fef3a512SBarry Smith 
2519bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2520bb7acecfSBarry Smith 
25211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2522fef3a512SBarry Smith @*/
2523d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2524d71ae5a4SJacob Faibussowitsch {
2525fef3a512SBarry Smith   PetscFunctionBegin;
2526fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2527fef3a512SBarry Smith   dm->levelup = level;
25283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2529fef3a512SBarry Smith }
2530fef3a512SBarry Smith 
2531d410b0cfSMatthew G. Knepley /*@
2532bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2533d410b0cfSMatthew G. Knepley 
253420f4b53cSBarry Smith   Collective
2535d410b0cfSMatthew G. Knepley 
2536f1a722f8SMatthew G. Knepley   Input Parameters:
2537bb7acecfSBarry Smith + dm     - the `DM` object
2538d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2539d410b0cfSMatthew G. Knepley 
2540d410b0cfSMatthew G. Knepley   Output Parameter:
254120f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2542d410b0cfSMatthew G. Knepley 
2543d410b0cfSMatthew G. Knepley   Level: developer
2544d410b0cfSMatthew G. Knepley 
254520f4b53cSBarry Smith   Note:
254620f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
254720f4b53cSBarry Smith 
25481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2549d410b0cfSMatthew G. Knepley @*/
2550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2551d71ae5a4SJacob Faibussowitsch {
2552d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2553d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2554dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2555d410b0cfSMatthew G. Knepley   if (*dme) {
2556d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25579566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2558d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25599566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2560d410b0cfSMatthew G. Knepley   }
25613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2562d410b0cfSMatthew G. Knepley }
2563d410b0cfSMatthew G. Knepley 
2564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2565d71ae5a4SJacob Faibussowitsch {
2566ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2567ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25684f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2569ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2571ca3d3a14SMatthew G. Knepley }
2572ca3d3a14SMatthew G. Knepley 
2573d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2574d71ae5a4SJacob Faibussowitsch {
2575ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2576ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25774f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2578ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2580ca3d3a14SMatthew G. Knepley }
2581ca3d3a14SMatthew G. Knepley 
2582ca3d3a14SMatthew G. Knepley /*@
2583bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2584ca3d3a14SMatthew G. Knepley 
2585ca3d3a14SMatthew G. Knepley   Input Parameter:
258620f4b53cSBarry Smith . dm - The `DM`
2587ca3d3a14SMatthew G. Knepley 
2588ca3d3a14SMatthew G. Knepley   Output Parameter:
258920f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2590ca3d3a14SMatthew G. Knepley 
2591ca3d3a14SMatthew G. Knepley   Level: developer
2592ca3d3a14SMatthew G. Knepley 
25931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2594ca3d3a14SMatthew G. Knepley @*/
2595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2596d71ae5a4SJacob Faibussowitsch {
2597ca3d3a14SMatthew G. Knepley   Vec tv;
2598ca3d3a14SMatthew G. Knepley 
2599ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2600ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26014f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26029566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2603ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2605ca3d3a14SMatthew G. Knepley }
2606ca3d3a14SMatthew G. Knepley 
2607d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2608d71ae5a4SJacob Faibussowitsch {
2609ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2610ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2611ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2612ca3d3a14SMatthew G. Knepley 
2613ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26149566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26159566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26179566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26189566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26199566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2622ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2624ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2625ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2626ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2628ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26299566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26309566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2631ca3d3a14SMatthew G. Knepley     }
2632ca3d3a14SMatthew G. Knepley   }
26339566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26349566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26359566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2636ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2637ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2639ca3d3a14SMatthew G. Knepley       if (dof) {
2640ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2641ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2642ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2643ca3d3a14SMatthew G. Knepley 
2644ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26459566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26469566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26479566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2648ca3d3a14SMatthew G. Knepley       }
2649ca3d3a14SMatthew G. Knepley     }
2650ca3d3a14SMatthew G. Knepley   }
26519566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2653ca3d3a14SMatthew G. Knepley }
2654ca3d3a14SMatthew G. Knepley 
2655d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2656d71ae5a4SJacob Faibussowitsch {
2657ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2658ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2659ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2660ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2661ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2662ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2663ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26649566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2666ca3d3a14SMatthew G. Knepley }
2667ca3d3a14SMatthew G. Knepley 
2668bb9467b5SJed Brown /*@C
2669bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2670baf369e7SPeter Brune 
267120f4b53cSBarry Smith   Logically Collective
2672baf369e7SPeter Brune 
26734165533cSJose E. Roman   Input Parameters:
2674bb7acecfSBarry Smith + dm        - the `DM`
2675bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2676bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
267720f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2678baf369e7SPeter Brune 
267920f4b53cSBarry Smith   Calling sequence of `beginhook`:
2680a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2681baf369e7SPeter Brune . g    - global vector
2682baf369e7SPeter Brune . mode - mode
2683baf369e7SPeter Brune . l    - local vector
2684baf369e7SPeter Brune - ctx  - optional user-defined function context
2685baf369e7SPeter Brune 
268620f4b53cSBarry Smith   Calling sequence of `endhook`:
2687a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2688a4e35b19SJacob Faibussowitsch . g    - global vector
2689a4e35b19SJacob Faibussowitsch . mode - mode
2690a4e35b19SJacob Faibussowitsch . l    - local vector
2691baf369e7SPeter Brune - ctx  - optional user-defined function context
2692baf369e7SPeter Brune 
2693baf369e7SPeter Brune   Level: advanced
2694baf369e7SPeter Brune 
2695bb7acecfSBarry Smith   Note:
2696bb7acecfSBarry 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.
2697bb7acecfSBarry Smith 
26981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2699baf369e7SPeter Brune @*/
2700a4e35b19SJacob 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)
2701d71ae5a4SJacob Faibussowitsch {
2702baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2703baf369e7SPeter Brune 
2704baf369e7SPeter Brune   PetscFunctionBegin;
2705baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2706baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27079566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2708baf369e7SPeter Brune   link->beginhook = beginhook;
2709baf369e7SPeter Brune   link->endhook   = endhook;
2710baf369e7SPeter Brune   link->ctx       = ctx;
27110298fd71SBarry Smith   link->next      = NULL;
2712baf369e7SPeter Brune   *p              = link;
27133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2714baf369e7SPeter Brune }
2715baf369e7SPeter Brune 
2716d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2717d71ae5a4SJacob Faibussowitsch {
27184c274da1SToby Isaac   Mat          cMat;
271979769bd5SJed Brown   Vec          cVec, cBias;
27204c274da1SToby Isaac   PetscSection section, cSec;
27214c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27224c274da1SToby Isaac 
27234c274da1SToby Isaac   PetscFunctionBegin;
2724a4e35b19SJacob Faibussowitsch   (void)g;
2725a4e35b19SJacob Faibussowitsch   (void)ctx;
27264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27279566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27284c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27295db9a05bSToby Isaac     PetscInt nRows;
27305db9a05bSToby Isaac 
27319566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27323ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27339566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27349566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27359566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27369566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27384c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27404c274da1SToby Isaac       if (dof) {
27414c274da1SToby Isaac         PetscScalar *vals;
27429566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27439566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27444c274da1SToby Isaac       }
27454c274da1SToby Isaac     }
27469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27474c274da1SToby Isaac   }
27483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27494c274da1SToby Isaac }
27504c274da1SToby Isaac 
275147c6ae99SBarry Smith /*@
275201729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
275301729b5cSPatrick Sanan 
275420f4b53cSBarry Smith   Neighbor-wise Collective
275501729b5cSPatrick Sanan 
275601729b5cSPatrick Sanan   Input Parameters:
2757bb7acecfSBarry Smith + dm   - the `DM` object
275801729b5cSPatrick Sanan . g    - the global vector
2759bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
276001729b5cSPatrick Sanan - l    - the local vector
276101729b5cSPatrick Sanan 
276220f4b53cSBarry Smith   Level: beginner
276320f4b53cSBarry Smith 
276401729b5cSPatrick Sanan   Notes:
2765bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2766bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2767bb7acecfSBarry Smith 
2768bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
276901729b5cSPatrick Sanan 
27701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
277160225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2772bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
277301729b5cSPatrick Sanan @*/
2774d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2775d71ae5a4SJacob Faibussowitsch {
277601729b5cSPatrick Sanan   PetscFunctionBegin;
27779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27789566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
278001729b5cSPatrick Sanan }
278101729b5cSPatrick Sanan 
278201729b5cSPatrick Sanan /*@
278347c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
278447c6ae99SBarry Smith 
278520f4b53cSBarry Smith   Neighbor-wise Collective
278647c6ae99SBarry Smith 
278747c6ae99SBarry Smith   Input Parameters:
2788bb7acecfSBarry Smith + dm   - the `DM` object
278947c6ae99SBarry Smith . g    - the global vector
2790bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279147c6ae99SBarry Smith - l    - the local vector
279247c6ae99SBarry Smith 
279301729b5cSPatrick Sanan   Level: intermediate
279447c6ae99SBarry Smith 
2795bb7acecfSBarry Smith   Notes:
2796bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2797bb7acecfSBarry Smith 
2798bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2799bb7acecfSBarry Smith 
2800bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2801bb7acecfSBarry Smith 
2802bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2803bb7acecfSBarry Smith 
280460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
280547c6ae99SBarry Smith @*/
2806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2807d71ae5a4SJacob Faibussowitsch {
28087128ae9fSMatthew G Knepley   PetscSF                 sf;
2809baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
281047c6ae99SBarry Smith 
281147c6ae99SBarry Smith   PetscFunctionBegin;
2812171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2813baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28141baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2815baf369e7SPeter Brune   }
28169566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28177128ae9fSMatthew G Knepley   if (sf) {
2818ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2819ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2820d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28217128ae9fSMatthew G Knepley 
28227a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28239566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28259566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28279566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28287128ae9fSMatthew G Knepley   } else {
28299566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28307128ae9fSMatthew G Knepley   }
28313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
283247c6ae99SBarry Smith }
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith /*@
283547c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
283647c6ae99SBarry Smith 
283720f4b53cSBarry Smith   Neighbor-wise Collective
283847c6ae99SBarry Smith 
283947c6ae99SBarry Smith   Input Parameters:
2840bb7acecfSBarry Smith + dm   - the `DM` object
284147c6ae99SBarry Smith . g    - the global vector
2842bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
284347c6ae99SBarry Smith - l    - the local vector
284447c6ae99SBarry Smith 
284501729b5cSPatrick Sanan   Level: intermediate
284647c6ae99SBarry Smith 
2847bb7acecfSBarry Smith   Note:
2848bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2849bb7acecfSBarry Smith 
285060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
285147c6ae99SBarry Smith @*/
2852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2853d71ae5a4SJacob Faibussowitsch {
28547128ae9fSMatthew G Knepley   PetscSF                 sf;
2855ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2856ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2857ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2858baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2859d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
286047c6ae99SBarry Smith 
286147c6ae99SBarry Smith   PetscFunctionBegin;
2862171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28639566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28649566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28657128ae9fSMatthew G Knepley   if (sf) {
28667a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28677128ae9fSMatthew G Knepley 
28689566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28709566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28729566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28739566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28747128ae9fSMatthew G Knepley   } else {
28759566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28767128ae9fSMatthew G Knepley   }
28779566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2878baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28799566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2880baf369e7SPeter Brune   }
28813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
288247c6ae99SBarry Smith }
288347c6ae99SBarry Smith 
2884d4d07f1eSToby Isaac /*@C
2885d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2886d4d07f1eSToby Isaac 
288720f4b53cSBarry Smith   Logically Collective
2888d4d07f1eSToby Isaac 
28894165533cSJose E. Roman   Input Parameters:
2890bb7acecfSBarry Smith + dm        - the `DM`
2891bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2892bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
289320f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2894d4d07f1eSToby Isaac 
289520f4b53cSBarry Smith   Calling sequence of `beginhook`:
2896a4e35b19SJacob Faibussowitsch + global - global `DM`
2897d4d07f1eSToby Isaac . l      - local vector
2898d4d07f1eSToby Isaac . mode   - mode
2899d4d07f1eSToby Isaac . g      - global vector
2900d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2901d4d07f1eSToby Isaac 
290220f4b53cSBarry Smith   Calling sequence of `endhook`:
2903bb7acecfSBarry Smith + global - global `DM`
2904d4d07f1eSToby Isaac . l      - local vector
2905d4d07f1eSToby Isaac . mode   - mode
2906d4d07f1eSToby Isaac . g      - global vector
2907d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2908d4d07f1eSToby Isaac 
2909d4d07f1eSToby Isaac   Level: advanced
2910d4d07f1eSToby Isaac 
29111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2912d4d07f1eSToby Isaac @*/
2913a4e35b19SJacob 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)
2914d71ae5a4SJacob Faibussowitsch {
2915d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2916d4d07f1eSToby Isaac 
2917d4d07f1eSToby Isaac   PetscFunctionBegin;
2918d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2919d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29209566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2921d4d07f1eSToby Isaac   link->beginhook = beginhook;
2922d4d07f1eSToby Isaac   link->endhook   = endhook;
2923d4d07f1eSToby Isaac   link->ctx       = ctx;
2924d4d07f1eSToby Isaac   link->next      = NULL;
2925d4d07f1eSToby Isaac   *p              = link;
29263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2927d4d07f1eSToby Isaac }
2928d4d07f1eSToby Isaac 
2929d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2930d71ae5a4SJacob Faibussowitsch {
29314c274da1SToby Isaac   Mat          cMat;
29324c274da1SToby Isaac   Vec          cVec;
29334c274da1SToby Isaac   PetscSection section, cSec;
29344c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29354c274da1SToby Isaac 
29364c274da1SToby Isaac   PetscFunctionBegin;
2937a4e35b19SJacob Faibussowitsch   (void)g;
2938a4e35b19SJacob Faibussowitsch   (void)ctx;
29394c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29409566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29414c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29425db9a05bSToby Isaac     PetscInt nRows;
29435db9a05bSToby Isaac 
29449566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29453ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29469566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29479566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29494c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29509566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29514c274da1SToby Isaac       if (dof) {
29524c274da1SToby Isaac         PetscInt     d;
29534c274da1SToby Isaac         PetscScalar *vals;
29549566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29559566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29564c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29574c274da1SToby Isaac          * we just extracted */
2958ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29594c274da1SToby Isaac       }
29604c274da1SToby Isaac     }
29619566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29634c274da1SToby Isaac   }
29643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29654c274da1SToby Isaac }
296601729b5cSPatrick Sanan /*@
296701729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
296801729b5cSPatrick Sanan 
296920f4b53cSBarry Smith   Neighbor-wise Collective
297001729b5cSPatrick Sanan 
297101729b5cSPatrick Sanan   Input Parameters:
2972bb7acecfSBarry Smith + dm   - the `DM` object
297301729b5cSPatrick Sanan . l    - the local vector
2974bb7acecfSBarry 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.
297501729b5cSPatrick Sanan - g    - the global vector
297601729b5cSPatrick Sanan 
297720f4b53cSBarry Smith   Level: beginner
297820f4b53cSBarry Smith 
297901729b5cSPatrick Sanan   Notes:
298001729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2981bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
298201729b5cSPatrick Sanan 
2983bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2984bb7acecfSBarry Smith 
2985bb7acecfSBarry 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.
2986bb7acecfSBarry Smith 
2987bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
298801729b5cSPatrick Sanan 
29891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
299001729b5cSPatrick Sanan @*/
2991d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2992d71ae5a4SJacob Faibussowitsch {
299301729b5cSPatrick Sanan   PetscFunctionBegin;
29949566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
299701729b5cSPatrick Sanan }
29984c274da1SToby Isaac 
299947c6ae99SBarry Smith /*@
300001729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30019a42bb27SBarry Smith 
300220f4b53cSBarry Smith   Neighbor-wise Collective
30039a42bb27SBarry Smith 
30049a42bb27SBarry Smith   Input Parameters:
3005bb7acecfSBarry Smith + dm   - the `DM` object
3006f6813fd5SJed Brown . l    - the local vector
3007aa624791SPierre 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.
30081eb28f2eSBarry Smith - g    - the global vector
30099a42bb27SBarry Smith 
301020f4b53cSBarry Smith   Level: intermediate
301120f4b53cSBarry Smith 
301295452b02SPatrick Sanan   Notes:
3013bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3014bb7acecfSBarry Smith 
3015bb7acecfSBarry 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.
3016bb7acecfSBarry Smith 
3017bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3018bb7acecfSBarry Smith 
3019bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3020bb7acecfSBarry Smith 
3021bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30229a42bb27SBarry Smith 
30231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30249a42bb27SBarry Smith @*/
3025d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3026d71ae5a4SJacob Faibussowitsch {
30277128ae9fSMatthew G Knepley   PetscSF                 sf;
302884330215SMatthew G. Knepley   PetscSection            s, gs;
3029d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3030ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3031ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3032ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3033fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3034d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30359a42bb27SBarry Smith 
30369a42bb27SBarry Smith   PetscFunctionBegin;
3037171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3038d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30391baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3040d4d07f1eSToby Isaac   }
30419566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30429566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30447128ae9fSMatthew G Knepley   switch (mode) {
30457128ae9fSMatthew G Knepley   case INSERT_VALUES:
30467128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3047d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3048d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3049d71ae5a4SJacob Faibussowitsch     break;
30507128ae9fSMatthew G Knepley   case ADD_VALUES:
30517128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3052d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3053d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3054d71ae5a4SJacob Faibussowitsch     break;
3055d71ae5a4SJacob Faibussowitsch   default:
3056d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30577128ae9fSMatthew G Knepley   }
3058ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30599566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3060ca3d3a14SMatthew G. Knepley     if (transform) {
30619566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30629566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30639566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30649566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3065fa88e482SJed Brown     } else if (isInsert) {
30669566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3067fa88e482SJed Brown     } else {
30689566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3069fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3070ca3d3a14SMatthew G. Knepley     }
3071fa88e482SJed Brown     if (s && isInsert) {
30729566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3073fa88e482SJed Brown     } else {
30749566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3075fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3076fa88e482SJed Brown     }
3077ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30789566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
307984330215SMatthew G. Knepley     } else if (s && isInsert) {
308084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
308184330215SMatthew G. Knepley 
30829566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30839566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30849566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
308584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3086b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
308784330215SMatthew G. Knepley 
30889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30919566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30939566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3094b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
309503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30967a8be351SBarry 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);
3097b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3098b3b16f48SMatthew G. Knepley         if (!gcdof) {
309984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3100b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3101b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
310284330215SMatthew G. Knepley           const PetscInt *cdofs;
310384330215SMatthew G. Knepley           PetscInt        cind = 0;
310484330215SMatthew G. Knepley 
31059566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3106b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31079371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31089371c9d4SSatish Balay               ++cind;
31099371c9d4SSatish Balay               continue;
31109371c9d4SSatish Balay             }
3111b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
311284330215SMatthew G. Knepley           }
31137a8be351SBarry 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);
311484330215SMatthew G. Knepley       }
3115ca3d3a14SMatthew G. Knepley     }
3116fa88e482SJed Brown     if (g_inplace) {
31179566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3118fa88e482SJed Brown     } else {
31199566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3120fa88e482SJed Brown     }
3121ca3d3a14SMatthew G. Knepley     if (transform) {
31229566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31239566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3124fa88e482SJed Brown     } else if (l_inplace) {
31259566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3126ca3d3a14SMatthew G. Knepley     } else {
31279566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3128ca3d3a14SMatthew G. Knepley     }
31297128ae9fSMatthew G Knepley   } else {
31309566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31317128ae9fSMatthew G Knepley   }
31323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31339a42bb27SBarry Smith }
31349a42bb27SBarry Smith 
31359a42bb27SBarry Smith /*@
31369a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
313747c6ae99SBarry Smith 
313820f4b53cSBarry Smith   Neighbor-wise Collective
313947c6ae99SBarry Smith 
314047c6ae99SBarry Smith   Input Parameters:
3141bb7acecfSBarry Smith + dm   - the `DM` object
3142f6813fd5SJed Brown . l    - the local vector
3143bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3144f6813fd5SJed Brown - g    - the global vector
314547c6ae99SBarry Smith 
314601729b5cSPatrick Sanan   Level: intermediate
314747c6ae99SBarry Smith 
3148bb7acecfSBarry Smith   Note:
3149bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3150bb7acecfSBarry Smith 
315160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
315247c6ae99SBarry Smith @*/
3153d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3154d71ae5a4SJacob Faibussowitsch {
31557128ae9fSMatthew G Knepley   PetscSF                 sf;
315684330215SMatthew G. Knepley   PetscSection            s;
3157d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3158ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
315947c6ae99SBarry Smith 
316047c6ae99SBarry Smith   PetscFunctionBegin;
3161171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31629566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31639566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31647128ae9fSMatthew G Knepley   switch (mode) {
31657128ae9fSMatthew G Knepley   case INSERT_VALUES:
3166d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3167d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3168d71ae5a4SJacob Faibussowitsch     break;
31697128ae9fSMatthew G Knepley   case ADD_VALUES:
3170d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3171d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3172d71ae5a4SJacob Faibussowitsch     break;
3173d71ae5a4SJacob Faibussowitsch   default:
3174d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31757128ae9fSMatthew G Knepley   }
317684330215SMatthew G. Knepley   if (sf && !isInsert) {
3177ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3178ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3179ca3d3a14SMatthew G. Knepley     Vec                tmpl;
318084330215SMatthew G. Knepley 
31819566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3182ca3d3a14SMatthew G. Knepley     if (transform) {
31839566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31849566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3185ca3d3a14SMatthew G. Knepley     } else {
31869566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3187ca3d3a14SMatthew G. Knepley     }
31889566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31899566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3190ca3d3a14SMatthew G. Knepley     if (transform) {
31919566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31929566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3193ca3d3a14SMatthew G. Knepley     } else {
31949566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3195ca3d3a14SMatthew G. Knepley     }
31969566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
319784330215SMatthew G. Knepley   } else if (s && isInsert) {
31987128ae9fSMatthew G Knepley   } else {
31999566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
32007128ae9fSMatthew G Knepley   }
3201d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32029566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3203d4d07f1eSToby Isaac   }
32043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
320547c6ae99SBarry Smith }
320647c6ae99SBarry Smith 
3207f089877aSRichard Tran Mills /*@
3208a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3209a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3210a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3211f089877aSRichard Tran Mills 
321220f4b53cSBarry Smith   Neighbor-wise Collective
3213f089877aSRichard Tran Mills 
3214f089877aSRichard Tran Mills   Input Parameters:
3215bb7acecfSBarry Smith + dm   - the `DM` object
3216bc0a1609SRichard Tran Mills . g    - the original local vector
3217bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3218f089877aSRichard Tran Mills 
3219bc0a1609SRichard Tran Mills   Output Parameter:
3220bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3221f089877aSRichard Tran Mills 
3222f089877aSRichard Tran Mills   Level: intermediate
3223f089877aSRichard Tran Mills 
3224a4e35b19SJacob Faibussowitsch   Notes:
3225a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3226a4e35b19SJacob Faibussowitsch 
322760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3228f089877aSRichard Tran Mills @*/
3229d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3230d71ae5a4SJacob Faibussowitsch {
3231f089877aSRichard Tran Mills   PetscFunctionBegin;
3232f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32339f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32349f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32359f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3237f089877aSRichard Tran Mills }
3238f089877aSRichard Tran Mills 
3239f089877aSRichard Tran Mills /*@
3240bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3241bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3242f089877aSRichard Tran Mills 
324320f4b53cSBarry Smith   Neighbor-wise Collective
3244f089877aSRichard Tran Mills 
3245f089877aSRichard Tran Mills   Input Parameters:
324660225df5SJacob Faibussowitsch + dm   - the `DM` object
3247bc0a1609SRichard Tran Mills . g    - the original local vector
3248bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3249f089877aSRichard Tran Mills 
3250bc0a1609SRichard Tran Mills   Output Parameter:
3251bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3252f089877aSRichard Tran Mills 
3253f089877aSRichard Tran Mills   Level: intermediate
3254f089877aSRichard Tran Mills 
325560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3256f089877aSRichard Tran Mills @*/
3257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3258d71ae5a4SJacob Faibussowitsch {
3259f089877aSRichard Tran Mills   PetscFunctionBegin;
3260f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32619f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32629f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32639f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3265f089877aSRichard Tran Mills }
3266f089877aSRichard Tran Mills 
326747c6ae99SBarry Smith /*@
3268bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
326947c6ae99SBarry Smith 
327020f4b53cSBarry Smith   Collective
327147c6ae99SBarry Smith 
3272d8d19677SJose E. Roman   Input Parameters:
3273bb7acecfSBarry Smith + dm   - the `DM` object
327420f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
327547c6ae99SBarry Smith 
327647c6ae99SBarry Smith   Output Parameter:
3277bb7acecfSBarry Smith . dmc - the coarsened `DM`
327847c6ae99SBarry Smith 
327947c6ae99SBarry Smith   Level: developer
328047c6ae99SBarry Smith 
32811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
328247c6ae99SBarry Smith @*/
3283d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3284d71ae5a4SJacob Faibussowitsch {
3285b17ce1afSJed Brown   DMCoarsenHookLink link;
328647c6ae99SBarry Smith 
328747c6ae99SBarry Smith   PetscFunctionBegin;
3288171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32899566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3290dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3291b9d85ea2SLisandro Dalcin   if (*dmc) {
3292a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32939566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
329443842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32959566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3296644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32970598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3298656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32999566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3300b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33019566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3302b17ce1afSJed Brown     }
3303b9d85ea2SLisandro Dalcin   }
33049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33057a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3307b17ce1afSJed Brown }
3308b17ce1afSJed Brown 
3309bb9467b5SJed Brown /*@C
3310b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3311b17ce1afSJed Brown 
331220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3313b17ce1afSJed Brown 
33144165533cSJose E. Roman   Input Parameters:
3315bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3316b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3317bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
331820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3319b17ce1afSJed Brown 
332020f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3321bb7acecfSBarry Smith + fine   - fine level `DM`
3322bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3323b17ce1afSJed Brown - ctx    - optional user-defined function context
3324b17ce1afSJed Brown 
332520f4b53cSBarry Smith   Calling sequence of `restricthook`:
3326bb7acecfSBarry Smith + fine      - fine level `DM`
3327bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3328c833c3b5SJed Brown . rscale    - scaling vector for restriction
3329c833c3b5SJed Brown . inject    - matrix restricting by injection
3330b17ce1afSJed Brown . coarse    - coarse level DM to update
3331b17ce1afSJed Brown - ctx       - optional user-defined function context
3332b17ce1afSJed Brown 
3333b17ce1afSJed Brown   Level: advanced
3334b17ce1afSJed Brown 
3335b17ce1afSJed Brown   Notes:
3336bb7acecfSBarry 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`.
3337b17ce1afSJed Brown 
3338b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3339b17ce1afSJed Brown 
3340b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3341bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3342b17ce1afSJed Brown 
3343bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3344bb7acecfSBarry Smith 
33451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3346b17ce1afSJed Brown @*/
3347a4e35b19SJacob 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)
3348d71ae5a4SJacob Faibussowitsch {
3349b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3350b17ce1afSJed Brown 
3351b17ce1afSJed Brown   PetscFunctionBegin;
3352b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33531e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33543ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33551e3d8eccSJed Brown   }
33569566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3357b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3358b17ce1afSJed Brown   link->restricthook = restricthook;
3359b17ce1afSJed Brown   link->ctx          = ctx;
33600298fd71SBarry Smith   link->next         = NULL;
3361b17ce1afSJed Brown   *p                 = link;
33623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3363b17ce1afSJed Brown }
3364b17ce1afSJed Brown 
3365dc822a44SJed Brown /*@C
3366bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3367dc822a44SJed Brown 
336820f4b53cSBarry Smith   Logically Collective; No Fortran Support
3369dc822a44SJed Brown 
33704165533cSJose E. Roman   Input Parameters:
3371bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3372dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3373bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
337420f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3375dc822a44SJed Brown 
3376dc822a44SJed Brown   Level: advanced
3377dc822a44SJed Brown 
3378bb7acecfSBarry Smith   Note:
3379dc822a44SJed Brown   This function does nothing if the hook is not in the list.
3380dc822a44SJed Brown 
33811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3382dc822a44SJed Brown @*/
3383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3384d71ae5a4SJacob Faibussowitsch {
3385dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3386dc822a44SJed Brown 
3387dc822a44SJed Brown   PetscFunctionBegin;
3388dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3389dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3390dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3391dc822a44SJed Brown       link = *p;
3392dc822a44SJed Brown       *p   = link->next;
33939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3394dc822a44SJed Brown       break;
3395dc822a44SJed Brown     }
3396dc822a44SJed Brown   }
33973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3398dc822a44SJed Brown }
3399dc822a44SJed Brown 
3400b17ce1afSJed Brown /*@
3401bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3402b17ce1afSJed Brown 
3403b17ce1afSJed Brown   Collective if any hooks are
3404b17ce1afSJed Brown 
34054165533cSJose E. Roman   Input Parameters:
3406bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3407bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3408e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3409bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
341020f4b53cSBarry Smith - coarse  - coarser `DM` to update
3411b17ce1afSJed Brown 
3412b17ce1afSJed Brown   Level: developer
3413b17ce1afSJed Brown 
341460225df5SJacob Faibussowitsch   Developer Notes:
3415bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3416bb7acecfSBarry Smith 
34171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3418b17ce1afSJed Brown @*/
3419d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3420d71ae5a4SJacob Faibussowitsch {
3421b17ce1afSJed Brown   DMCoarsenHookLink link;
3422b17ce1afSJed Brown 
3423b17ce1afSJed Brown   PetscFunctionBegin;
3424b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34251baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3426b17ce1afSJed Brown   }
34273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
342847c6ae99SBarry Smith }
342947c6ae99SBarry Smith 
3430bb9467b5SJed Brown /*@C
3431be081cd6SPeter Brune   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34325dbd56e3SPeter Brune 
343320f4b53cSBarry Smith   Logically Collective; No Fortran Support
34345dbd56e3SPeter Brune 
34354165533cSJose E. Roman   Input Parameters:
3436bb7acecfSBarry Smith + global       - global `DM`
3437bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34385dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
343920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34405dbd56e3SPeter Brune 
344120f4b53cSBarry Smith   Calling sequence of `ddhook`:
3442bb7acecfSBarry Smith + global - global `DM`
3443bb7acecfSBarry Smith . block  - block `DM`
3444ec4806b8SPeter Brune - ctx    - optional user-defined function context
3445ec4806b8SPeter Brune 
344620f4b53cSBarry Smith   Calling sequence of `restricthook`:
3447bb7acecfSBarry Smith + global - global `DM`
34485dbd56e3SPeter Brune . out    - scatter to the outer (with ghost and overlap points) block vector
34495dbd56e3SPeter Brune . in     - scatter to block vector values only owned locally
3450bb7acecfSBarry Smith . block  - block `DM`
34515dbd56e3SPeter Brune - ctx    - optional user-defined function context
34525dbd56e3SPeter Brune 
34535dbd56e3SPeter Brune   Level: advanced
34545dbd56e3SPeter Brune 
34555dbd56e3SPeter Brune   Notes:
3456bb7acecfSBarry Smith   This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34575dbd56e3SPeter Brune 
34585dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34595dbd56e3SPeter Brune 
34605dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3461bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34625dbd56e3SPeter Brune 
34631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34645dbd56e3SPeter Brune @*/
3465a4e35b19SJacob 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)
3466d71ae5a4SJacob Faibussowitsch {
3467be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34685dbd56e3SPeter Brune 
34695dbd56e3SPeter Brune   PetscFunctionBegin;
34705dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3471b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34723ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3473b3a6b972SJed Brown   }
34749566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34755dbd56e3SPeter Brune   link->restricthook = restricthook;
3476be081cd6SPeter Brune   link->ddhook       = ddhook;
34775dbd56e3SPeter Brune   link->ctx          = ctx;
34780298fd71SBarry Smith   link->next         = NULL;
34795dbd56e3SPeter Brune   *p                 = link;
34803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34815dbd56e3SPeter Brune }
34825dbd56e3SPeter Brune 
3483b3a6b972SJed Brown /*@C
3484b3a6b972SJed Brown   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3485b3a6b972SJed Brown 
348620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3487b3a6b972SJed Brown 
34884165533cSJose E. Roman   Input Parameters:
3489bb7acecfSBarry Smith + global       - global `DM`
3490bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3491b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
349220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3493b3a6b972SJed Brown 
3494b3a6b972SJed Brown   Level: advanced
3495b3a6b972SJed Brown 
34961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3497b3a6b972SJed Brown @*/
3498d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3499d71ae5a4SJacob Faibussowitsch {
3500b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3501b3a6b972SJed Brown 
3502b3a6b972SJed Brown   PetscFunctionBegin;
3503b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3504b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3505b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3506b3a6b972SJed Brown       link = *p;
3507b3a6b972SJed Brown       *p   = link->next;
35089566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3509b3a6b972SJed Brown       break;
3510b3a6b972SJed Brown     }
3511b3a6b972SJed Brown   }
35123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3513b3a6b972SJed Brown }
3514b3a6b972SJed Brown 
35155dbd56e3SPeter Brune /*@
3516bb7acecfSBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35175dbd56e3SPeter Brune 
35185dbd56e3SPeter Brune   Collective if any hooks are
35195dbd56e3SPeter Brune 
35204165533cSJose E. Roman   Input Parameters:
3521a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3522a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3523a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3524a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35255dbd56e3SPeter Brune 
35265dbd56e3SPeter Brune   Level: developer
35275dbd56e3SPeter Brune 
35281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`
35295dbd56e3SPeter Brune @*/
3530d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3531d71ae5a4SJacob Faibussowitsch {
3532be081cd6SPeter Brune   DMSubDomainHookLink link;
35335dbd56e3SPeter Brune 
35345dbd56e3SPeter Brune   PetscFunctionBegin;
3535be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35361baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35375dbd56e3SPeter Brune   }
35383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35395dbd56e3SPeter Brune }
35405dbd56e3SPeter Brune 
35415fe1f584SPeter Brune /*@
3542bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35435fe1f584SPeter Brune 
35445fe1f584SPeter Brune   Not Collective
35455fe1f584SPeter Brune 
35465fe1f584SPeter Brune   Input Parameter:
3547bb7acecfSBarry Smith . dm - the `DM` object
35485fe1f584SPeter Brune 
35495fe1f584SPeter Brune   Output Parameter:
35506a7d9d85SPeter Brune . level - number of coarsenings
35515fe1f584SPeter Brune 
35525fe1f584SPeter Brune   Level: developer
35535fe1f584SPeter Brune 
35541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35555fe1f584SPeter Brune @*/
3556d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3557d71ae5a4SJacob Faibussowitsch {
35585fe1f584SPeter Brune   PetscFunctionBegin;
35595fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35604f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35615fe1f584SPeter Brune   *level = dm->leveldown;
35623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35635fe1f584SPeter Brune }
35645fe1f584SPeter Brune 
35659a64c4a8SMatthew G. Knepley /*@
3566bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35679a64c4a8SMatthew G. Knepley 
356820f4b53cSBarry Smith   Collective
35699a64c4a8SMatthew G. Knepley 
35709a64c4a8SMatthew G. Knepley   Input Parameters:
3571bb7acecfSBarry Smith + dm    - the `DM` object
35729a64c4a8SMatthew G. Knepley - level - number of coarsenings
35739a64c4a8SMatthew G. Knepley 
35749a64c4a8SMatthew G. Knepley   Level: developer
35759a64c4a8SMatthew G. Knepley 
3576bb7acecfSBarry Smith   Note:
3577bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3578bb7acecfSBarry Smith 
357942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35809a64c4a8SMatthew G. Knepley @*/
3581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3582d71ae5a4SJacob Faibussowitsch {
35839a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35849a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35859a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35879a64c4a8SMatthew G. Knepley }
35889a64c4a8SMatthew G. Knepley 
358947c6ae99SBarry Smith /*@C
3590bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
359147c6ae99SBarry Smith 
359220f4b53cSBarry Smith   Collective
359347c6ae99SBarry Smith 
3594d8d19677SJose E. Roman   Input Parameters:
3595bb7acecfSBarry Smith + dm      - the `DM` object
359647c6ae99SBarry Smith - nlevels - the number of levels of refinement
359747c6ae99SBarry Smith 
359847c6ae99SBarry Smith   Output Parameter:
3599bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
360047c6ae99SBarry Smith 
360147c6ae99SBarry Smith   Level: developer
360247c6ae99SBarry Smith 
36031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
360447c6ae99SBarry Smith @*/
3605d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3606d71ae5a4SJacob Faibussowitsch {
360747c6ae99SBarry Smith   PetscFunctionBegin;
3608171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36097a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36103ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36114f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
361247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3613dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
361447c6ae99SBarry Smith   } else if (dm->ops->refine) {
361547c6ae99SBarry Smith     PetscInt i;
361647c6ae99SBarry Smith 
36179566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
361848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3619ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362147c6ae99SBarry Smith }
362247c6ae99SBarry Smith 
362347c6ae99SBarry Smith /*@C
3624bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
362547c6ae99SBarry Smith 
362620f4b53cSBarry Smith   Collective
362747c6ae99SBarry Smith 
3628d8d19677SJose E. Roman   Input Parameters:
3629bb7acecfSBarry Smith + dm      - the `DM` object
363047c6ae99SBarry Smith - nlevels - the number of levels of coarsening
363147c6ae99SBarry Smith 
363247c6ae99SBarry Smith   Output Parameter:
3633bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
363447c6ae99SBarry Smith 
363547c6ae99SBarry Smith   Level: developer
363647c6ae99SBarry Smith 
36371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363847c6ae99SBarry Smith @*/
3639d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3640d71ae5a4SJacob Faibussowitsch {
364147c6ae99SBarry Smith   PetscFunctionBegin;
3642171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36437a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36443ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36454f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
364647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3647dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
364847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
364947c6ae99SBarry Smith     PetscInt i;
365047c6ae99SBarry Smith 
36519566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
365248a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3653ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365547c6ae99SBarry Smith }
365647c6ae99SBarry Smith 
36571a266240SBarry Smith /*@C
3658bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36591a266240SBarry Smith 
3660bb7acecfSBarry Smith   Logically Collective if the function is collective
36611a266240SBarry Smith 
36621a266240SBarry Smith   Input Parameters:
3663bb7acecfSBarry Smith + dm      - the `DM` object
36641a266240SBarry Smith - destroy - the destroy function
36651a266240SBarry Smith 
36661a266240SBarry Smith   Level: intermediate
36671a266240SBarry Smith 
36681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3669f07f9ceaSJed Brown @*/
3670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3671d71ae5a4SJacob Faibussowitsch {
36721a266240SBarry Smith   PetscFunctionBegin;
3673171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36741a266240SBarry Smith   dm->ctxdestroy = destroy;
36753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36761a266240SBarry Smith }
36771a266240SBarry Smith 
3678b07ff414SBarry Smith /*@
3679bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
368047c6ae99SBarry Smith 
368147c6ae99SBarry Smith   Not Collective
368247c6ae99SBarry Smith 
368347c6ae99SBarry Smith   Input Parameters:
3684bb7acecfSBarry Smith + dm  - the `DM` object
368547c6ae99SBarry Smith - ctx - the user context
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith   Level: intermediate
368847c6ae99SBarry Smith 
3689bb7acecfSBarry Smith   Note:
369035cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3691bb7acecfSBarry Smith 
369260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
369347c6ae99SBarry Smith @*/
3694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3695d71ae5a4SJacob Faibussowitsch {
369647c6ae99SBarry Smith   PetscFunctionBegin;
3697171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369847c6ae99SBarry Smith   dm->ctx = ctx;
36993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
370047c6ae99SBarry Smith }
370147c6ae99SBarry Smith 
370247c6ae99SBarry Smith /*@
3703bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
370447c6ae99SBarry Smith 
370547c6ae99SBarry Smith   Not Collective
370647c6ae99SBarry Smith 
370747c6ae99SBarry Smith   Input Parameter:
3708bb7acecfSBarry Smith . dm - the `DM` object
370947c6ae99SBarry Smith 
371047c6ae99SBarry Smith   Output Parameter:
371147c6ae99SBarry Smith . ctx - the user context
371247c6ae99SBarry Smith 
371347c6ae99SBarry Smith   Level: intermediate
371447c6ae99SBarry Smith 
3715bb7acecfSBarry Smith   Note:
371635cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3717bb7acecfSBarry Smith 
371842747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371947c6ae99SBarry Smith @*/
3720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3721d71ae5a4SJacob Faibussowitsch {
372247c6ae99SBarry Smith   PetscFunctionBegin;
3723171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37241b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372647c6ae99SBarry Smith }
372747c6ae99SBarry Smith 
372808da532bSDmitry Karpeev /*@C
3729bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
373008da532bSDmitry Karpeev 
373120f4b53cSBarry Smith   Logically Collective
373208da532bSDmitry Karpeev 
3733d8d19677SJose E. Roman   Input Parameters:
373408da532bSDmitry Karpeev + dm - the DM object
373520f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
373608da532bSDmitry Karpeev 
373708da532bSDmitry Karpeev   Level: intermediate
373808da532bSDmitry Karpeev 
37391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3740db781477SPatrick Sanan          `DMSetJacobian()`
374108da532bSDmitry Karpeev @*/
3742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3743d71ae5a4SJacob Faibussowitsch {
374408da532bSDmitry Karpeev   PetscFunctionBegin;
37455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374808da532bSDmitry Karpeev }
374908da532bSDmitry Karpeev 
375008da532bSDmitry Karpeev /*@
3751bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
375208da532bSDmitry Karpeev 
375308da532bSDmitry Karpeev   Not Collective
375408da532bSDmitry Karpeev 
375508da532bSDmitry Karpeev   Input Parameter:
3756bb7acecfSBarry Smith . dm - the `DM` object to destroy
375708da532bSDmitry Karpeev 
375808da532bSDmitry Karpeev   Output Parameter:
3759bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
376008da532bSDmitry Karpeev 
376108da532bSDmitry Karpeev   Level: developer
376208da532bSDmitry Karpeev 
37631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
376408da532bSDmitry Karpeev @*/
3765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3766d71ae5a4SJacob Faibussowitsch {
376708da532bSDmitry Karpeev   PetscFunctionBegin;
37685a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37694f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
377008da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377208da532bSDmitry Karpeev }
377308da532bSDmitry Karpeev 
377408da532bSDmitry Karpeev /*@C
3775bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
377608da532bSDmitry Karpeev 
377720f4b53cSBarry Smith   Logically Collective
377808da532bSDmitry Karpeev 
3779f899ff85SJose E. Roman   Input Parameter:
3780bb7acecfSBarry Smith . dm - the `DM` object
378108da532bSDmitry Karpeev 
378260225df5SJacob Faibussowitsch   Output Parameters:
378308da532bSDmitry Karpeev + xl - lower bound
378408da532bSDmitry Karpeev - xu - upper bound
378508da532bSDmitry Karpeev 
3786907376e6SBarry Smith   Level: advanced
3787907376e6SBarry Smith 
378820f4b53cSBarry Smith   Note:
378995452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
379008da532bSDmitry Karpeev 
37911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379208da532bSDmitry Karpeev @*/
3793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3794d71ae5a4SJacob Faibussowitsch {
379508da532bSDmitry Karpeev   PetscFunctionBegin;
37965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37985a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3799dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380108da532bSDmitry Karpeev }
380208da532bSDmitry Karpeev 
3803b0ae01b7SPeter Brune /*@
3804bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3805b0ae01b7SPeter Brune 
3806b0ae01b7SPeter Brune   Not Collective
3807b0ae01b7SPeter Brune 
3808b0ae01b7SPeter Brune   Input Parameter:
3809b0ae01b7SPeter Brune . dm - the DM object
3810b0ae01b7SPeter Brune 
3811b0ae01b7SPeter Brune   Output Parameter:
3812bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3813b0ae01b7SPeter Brune 
3814b0ae01b7SPeter Brune   Level: developer
3815b0ae01b7SPeter Brune 
38161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3817b0ae01b7SPeter Brune @*/
3818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3819d71ae5a4SJacob Faibussowitsch {
3820b0ae01b7SPeter Brune   PetscFunctionBegin;
38215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38224f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3823b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3825b0ae01b7SPeter Brune }
3826b0ae01b7SPeter Brune 
38273ad4599aSBarry Smith /*@
3828bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38293ad4599aSBarry Smith 
38303ad4599aSBarry Smith   Not Collective
38313ad4599aSBarry Smith 
38323ad4599aSBarry Smith   Input Parameter:
3833bb7acecfSBarry Smith . dm - the `DM` object
38343ad4599aSBarry Smith 
38353ad4599aSBarry Smith   Output Parameter:
3836bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38373ad4599aSBarry Smith 
38383ad4599aSBarry Smith   Level: developer
38393ad4599aSBarry Smith 
38401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38413ad4599aSBarry Smith @*/
3842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3843d71ae5a4SJacob Faibussowitsch {
38443ad4599aSBarry Smith   PetscFunctionBegin;
38455a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38464f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38473ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38493ad4599aSBarry Smith }
38503ad4599aSBarry Smith 
3851a7058e45SLawrence Mitchell /*@
3852bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3853a7058e45SLawrence Mitchell 
3854a7058e45SLawrence Mitchell   Not Collective
3855a7058e45SLawrence Mitchell 
3856a7058e45SLawrence Mitchell   Input Parameter:
3857bb7acecfSBarry Smith . dm - the `DM` object
3858a7058e45SLawrence Mitchell 
3859a7058e45SLawrence Mitchell   Output Parameter:
3860bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3861a7058e45SLawrence Mitchell 
3862a7058e45SLawrence Mitchell   Level: developer
3863a7058e45SLawrence Mitchell 
38641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3865a7058e45SLawrence Mitchell @*/
3866d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3867d71ae5a4SJacob Faibussowitsch {
3868a7058e45SLawrence Mitchell   PetscFunctionBegin;
38695a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38704f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3871dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3872ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3874a7058e45SLawrence Mitchell }
3875a7058e45SLawrence Mitchell 
38760298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3877264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3878264ace61SBarry Smith 
3879264ace61SBarry Smith /*@C
3880bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3881264ace61SBarry Smith 
388220f4b53cSBarry Smith   Collective
3883264ace61SBarry Smith 
3884264ace61SBarry Smith   Input Parameters:
3885bb7acecfSBarry Smith + dm     - The `DM` object
3886bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3887264ace61SBarry Smith 
3888264ace61SBarry Smith   Options Database Key:
3889bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3890264ace61SBarry Smith 
3891264ace61SBarry Smith   Level: intermediate
3892264ace61SBarry Smith 
3893bb7acecfSBarry Smith   Note:
38940462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3895bb7acecfSBarry Smith 
38961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3897264ace61SBarry Smith @*/
3898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3899d71ae5a4SJacob Faibussowitsch {
3900264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3901264ace61SBarry Smith   PetscBool match;
3902264ace61SBarry Smith 
3903264ace61SBarry Smith   PetscFunctionBegin;
3904264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39059566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39063ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3907264ace61SBarry Smith 
39089566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39099566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39107a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3911264ace61SBarry Smith 
3912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39139566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39149566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39159566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3917264ace61SBarry Smith }
3918264ace61SBarry Smith 
3919264ace61SBarry Smith /*@C
3920bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3921264ace61SBarry Smith 
3922264ace61SBarry Smith   Not Collective
3923264ace61SBarry Smith 
3924264ace61SBarry Smith   Input Parameter:
3925bb7acecfSBarry Smith . dm - The `DM`
3926264ace61SBarry Smith 
3927264ace61SBarry Smith   Output Parameter:
3928bb7acecfSBarry Smith . type - The `DMType` name
3929264ace61SBarry Smith 
3930264ace61SBarry Smith   Level: intermediate
3931264ace61SBarry Smith 
39321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3933264ace61SBarry Smith @*/
3934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3935d71ae5a4SJacob Faibussowitsch {
3936264ace61SBarry Smith   PetscFunctionBegin;
3937264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39384f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39399566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3940264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3942264ace61SBarry Smith }
3943264ace61SBarry Smith 
394467a56275SMatthew G Knepley /*@C
3945bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
394667a56275SMatthew G Knepley 
394720f4b53cSBarry Smith   Collective
394867a56275SMatthew G Knepley 
394967a56275SMatthew G Knepley   Input Parameters:
3950bb7acecfSBarry Smith + dm      - the `DM`
3951bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
395267a56275SMatthew G Knepley 
395367a56275SMatthew G Knepley   Output Parameter:
3954bb7acecfSBarry Smith . M - pointer to new `DM`
395567a56275SMatthew G Knepley 
395620f4b53cSBarry Smith   Level: intermediate
395720f4b53cSBarry Smith 
395867a56275SMatthew G Knepley   Notes:
3959bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3960bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3961bb7acecfSBarry Smith   of the input `DM`.
396267a56275SMatthew G Knepley 
39631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
396467a56275SMatthew G Knepley @*/
3965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3966d71ae5a4SJacob Faibussowitsch {
396767a56275SMatthew G Knepley   DM        B;
396867a56275SMatthew G Knepley   char      convname[256];
3969c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
397067a56275SMatthew G Knepley 
397167a56275SMatthew G Knepley   PetscFunctionBegin;
397267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
397367a56275SMatthew G Knepley   PetscValidType(dm, 1);
39744f572ea9SToby Isaac   PetscAssertPointer(M, 3);
39759566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39769566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3977c067b6caSMatthew G. Knepley   if (sametype) {
3978c067b6caSMatthew G. Knepley     *M = dm;
39799566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39803ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3981c067b6caSMatthew G. Knepley   } else {
39820298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley     /*
398567a56275SMatthew G Knepley        Order of precedence:
398667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
398767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
398867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
398967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
399067a56275SMatthew G Knepley        5) Use a really basic converter.
399167a56275SMatthew G Knepley     */
399267a56275SMatthew G Knepley 
399367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39949566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39959566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39969566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39979566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39989566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39999566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
400067a56275SMatthew G Knepley     if (conv) goto foundconv;
400167a56275SMatthew G Knepley 
400267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40039566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40049566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40059566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40069566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40079566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40089566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40099566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40109566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
401167a56275SMatthew G Knepley     if (conv) {
40129566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
401367a56275SMatthew G Knepley       goto foundconv;
401467a56275SMatthew G Knepley     }
401567a56275SMatthew G Knepley 
401667a56275SMatthew G Knepley #if 0
401767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
401867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40199566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
402067a56275SMatthew G Knepley     if (conv) goto foundconv;
402167a56275SMatthew G Knepley 
402267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
402367a56275SMatthew G Knepley     if (dm->ops->convert) {
402467a56275SMatthew G Knepley       conv = dm->ops->convert;
402567a56275SMatthew G Knepley     }
402667a56275SMatthew G Knepley     if (conv) goto foundconv;
402767a56275SMatthew G Knepley #endif
402867a56275SMatthew G Knepley 
402967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
403098921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
403167a56275SMatthew G Knepley 
403267a56275SMatthew G Knepley   foundconv:
40339566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40349566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
403512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
403690b157c4SStefano Zampini     {
40374fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40386858538eSMatthew G. Knepley 
40394fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40404fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4041c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40429566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40439566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40449566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40459566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
404612fa691eSMatthew G. Knepley     }
40479566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
404867a56275SMatthew G Knepley   }
40499566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
405167a56275SMatthew G Knepley }
4052264ace61SBarry Smith 
4053264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4054264ace61SBarry Smith 
4055264ace61SBarry Smith /*@C
4056bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40571c84c290SBarry Smith 
40581c84c290SBarry Smith   Not Collective
40591c84c290SBarry Smith 
40601c84c290SBarry Smith   Input Parameters:
406120f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
406220f4b53cSBarry Smith - function - The creation routine itself
406320f4b53cSBarry Smith 
406420f4b53cSBarry Smith   Level: advanced
40651c84c290SBarry Smith 
40661c84c290SBarry Smith   Notes:
406720f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40681c84c290SBarry Smith 
406960225df5SJacob Faibussowitsch   Example Usage:
40701c84c290SBarry Smith .vb
4071bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40721c84c290SBarry Smith .ve
40731c84c290SBarry Smith 
407420f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40751c84c290SBarry Smith .vb
40761c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40771c84c290SBarry Smith     DMSetType(DM,"my_da");
40781c84c290SBarry Smith .ve
40791c84c290SBarry Smith   or at runtime via the option
40801c84c290SBarry Smith .vb
40811c84c290SBarry Smith     -da_type my_da
40821c84c290SBarry Smith .ve
4083264ace61SBarry Smith 
40841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4085264ace61SBarry Smith @*/
4086d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4087d71ae5a4SJacob Faibussowitsch {
4088264ace61SBarry Smith   PetscFunctionBegin;
40899566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40909566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4092264ace61SBarry Smith }
4093264ace61SBarry Smith 
4094b859378eSBarry Smith /*@C
4095bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4096b859378eSBarry Smith 
409720f4b53cSBarry Smith   Collective
4098b859378eSBarry Smith 
4099b859378eSBarry Smith   Input Parameters:
4100bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4101bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4102bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4103bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4104b859378eSBarry Smith 
4105b859378eSBarry Smith   Level: intermediate
4106b859378eSBarry Smith 
4107b859378eSBarry Smith   Notes:
410855849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4109b859378eSBarry Smith 
4110bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4111bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4112bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4113cd7e8a5eSksagiyam 
41141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4115b859378eSBarry Smith @*/
4116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4117d71ae5a4SJacob Faibussowitsch {
41189331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4119b859378eSBarry Smith 
4120b859378eSBarry Smith   PetscFunctionBegin;
4121b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4122b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41239566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41249566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41279331c7a4SMatthew G. Knepley   if (isbinary) {
41289331c7a4SMatthew G. Knepley     PetscInt classid;
41299331c7a4SMatthew G. Knepley     char     type[256];
4130b859378eSBarry Smith 
41319566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41327a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41339566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41349566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4135dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41369331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4137dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41389331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4141b859378eSBarry Smith }
4142b859378eSBarry Smith 
41437da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41447da65231SMatthew G Knepley 
41452ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41462ecf6ec3SMatthew G. Knepley {
41472ecf6ec3SMatthew G. Knepley   PetscInt f;
41482ecf6ec3SMatthew G. Knepley 
41492ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41502ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41512ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41522ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41532ecf6ec3SMatthew G. Knepley }
41542ecf6ec3SMatthew G. Knepley 
4155d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4156d71ae5a4SJacob Faibussowitsch {
41571d47ebbbSSatish Balay   PetscInt f;
41581b30c384SMatthew G Knepley 
41597da65231SMatthew G Knepley   PetscFunctionBegin;
416063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
416148a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41637da65231SMatthew G Knepley }
41647da65231SMatthew G Knepley 
41655962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41665962854dSMatthew G. Knepley {
41675962854dSMatthew G. Knepley   PetscInt f;
41685962854dSMatthew G. Knepley 
41695962854dSMatthew G. Knepley   PetscFunctionBegin;
41705962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41715962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41725962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41735962854dSMatthew G. Knepley }
41745962854dSMatthew G. Knepley 
4175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4176d71ae5a4SJacob Faibussowitsch {
41771b30c384SMatthew G Knepley   PetscInt f, g;
41787da65231SMatthew G Knepley 
41797da65231SMatthew G Knepley   PetscFunctionBegin;
418063a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41811d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41829566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
418348a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41849566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41857da65231SMatthew G Knepley   }
41863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41877da65231SMatthew G Knepley }
4188e7c4fc90SDmitry Karpeev 
4189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4190d71ae5a4SJacob Faibussowitsch {
41910c5b8624SToby Isaac   PetscInt           localSize, bs;
41920c5b8624SToby Isaac   PetscMPIInt        size;
41930c5b8624SToby Isaac   Vec                x, xglob;
41940c5b8624SToby Isaac   const PetscScalar *xarray;
4195e759306cSMatthew G. Knepley 
4196e759306cSMatthew G. Knepley   PetscFunctionBegin;
41979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41989566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41999566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
420093ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42019566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42020c5b8624SToby Isaac   if (size > 1) {
42039566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42049566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42059566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42069566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42070c5b8624SToby Isaac   } else {
42080c5b8624SToby Isaac     xglob = x;
42090c5b8624SToby Isaac   }
42109566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42110c5b8624SToby Isaac   if (size > 1) {
42129566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42139566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42140c5b8624SToby Isaac   }
42159566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4217e759306cSMatthew G. Knepley }
4218e759306cSMatthew G. Knepley 
421988ed4aceSMatthew G Knepley /*@
4220bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4221061576a5SJed Brown 
4222061576a5SJed Brown   Input Parameter:
4223bb7acecfSBarry Smith . dm - The `DM`
4224061576a5SJed Brown 
4225061576a5SJed Brown   Output Parameter:
4226bb7acecfSBarry Smith . section - The `PetscSection`
4227061576a5SJed Brown 
422820f4b53cSBarry Smith   Options Database Key:
4229bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4230061576a5SJed Brown 
4231061576a5SJed Brown   Level: advanced
4232061576a5SJed Brown 
4233061576a5SJed Brown   Notes:
4234bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4235061576a5SJed Brown 
4236bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4237061576a5SJed Brown 
42381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4239061576a5SJed Brown @*/
4240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4241d71ae5a4SJacob Faibussowitsch {
4242061576a5SJed Brown   PetscFunctionBegin;
42439566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4245061576a5SJed Brown }
4246061576a5SJed Brown 
4247061576a5SJed Brown /*@
4248bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
424988ed4aceSMatthew G Knepley 
425088ed4aceSMatthew G Knepley   Input Parameter:
4251bb7acecfSBarry Smith . dm - The `DM`
425288ed4aceSMatthew G Knepley 
425388ed4aceSMatthew G Knepley   Output Parameter:
4254bb7acecfSBarry Smith . section - The `PetscSection`
425588ed4aceSMatthew G Knepley 
425620f4b53cSBarry Smith   Options Database Key:
4257bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4258e5893cccSMatthew G. Knepley 
425988ed4aceSMatthew G Knepley   Level: intermediate
426088ed4aceSMatthew G Knepley 
4261bb7acecfSBarry Smith   Note:
4262bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
426388ed4aceSMatthew G Knepley 
42641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
426588ed4aceSMatthew G Knepley @*/
4266d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4267d71ae5a4SJacob Faibussowitsch {
426888ed4aceSMatthew G Knepley   PetscFunctionBegin;
426988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42704f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42711bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4272e5e52638SMatthew G. Knepley     PetscInt d;
4273e5e52638SMatthew G. Knepley 
427445480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
427545480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
427645480ffeSMatthew G. Knepley       PetscViewer       viewer;
427745480ffeSMatthew G. Knepley       PetscViewerFormat format;
427845480ffeSMatthew G. Knepley       PetscBool         flg;
427945480ffeSMatthew G. Knepley 
42809566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42819566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
428245480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42839566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42849566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
428545480ffeSMatthew G. Knepley       }
428645480ffeSMatthew G. Knepley       if (flg) {
42879566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42889566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42899566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
429045480ffeSMatthew G. Knepley       }
429145480ffeSMatthew G. Knepley     }
4292dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42939566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42942f0f8703SMatthew G. Knepley   }
42951bb6d2a8SBarry Smith   *section = dm->localSection;
42963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
429788ed4aceSMatthew G Knepley }
429888ed4aceSMatthew G Knepley 
429988ed4aceSMatthew G Knepley /*@
4300bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4301061576a5SJed Brown 
4302061576a5SJed Brown   Input Parameters:
4303bb7acecfSBarry Smith + dm      - The `DM`
4304bb7acecfSBarry Smith - section - The `PetscSection`
4305061576a5SJed Brown 
4306061576a5SJed Brown   Level: advanced
4307061576a5SJed Brown 
4308061576a5SJed Brown   Notes:
4309bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4310061576a5SJed Brown 
4311bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4312061576a5SJed Brown 
43131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4314061576a5SJed Brown @*/
4315d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4316d71ae5a4SJacob Faibussowitsch {
4317061576a5SJed Brown   PetscFunctionBegin;
43189566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4320061576a5SJed Brown }
4321061576a5SJed Brown 
4322061576a5SJed Brown /*@
4323bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
432488ed4aceSMatthew G Knepley 
432588ed4aceSMatthew G Knepley   Input Parameters:
4326bb7acecfSBarry Smith + dm      - The `DM`
4327bb7acecfSBarry Smith - section - The `PetscSection`
432888ed4aceSMatthew G Knepley 
432988ed4aceSMatthew G Knepley   Level: intermediate
433088ed4aceSMatthew G Knepley 
4331bb7acecfSBarry Smith   Note:
4332bb7acecfSBarry Smith   Any existing Section will be destroyed
433388ed4aceSMatthew G Knepley 
43341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
433588ed4aceSMatthew G Knepley @*/
4336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4337d71ae5a4SJacob Faibussowitsch {
4338c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4339af122d2aSMatthew G Knepley   PetscInt f;
434088ed4aceSMatthew G Knepley 
434188ed4aceSMatthew G Knepley   PetscFunctionBegin;
434288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4343b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43459566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43461bb6d2a8SBarry Smith   dm->localSection = section;
43479566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4348af122d2aSMatthew G Knepley   if (numFields) {
43499566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4350af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43510f21e855SMatthew G. Knepley       PetscObject disc;
4352af122d2aSMatthew G Knepley       const char *name;
4353af122d2aSMatthew G Knepley 
43549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43559566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43569566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4357af122d2aSMatthew G Knepley     }
4358af122d2aSMatthew G Knepley   }
4359e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43609566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
436288ed4aceSMatthew G Knepley }
436388ed4aceSMatthew G Knepley 
43649435951eSToby Isaac /*@
4365bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43669435951eSToby Isaac 
436720f4b53cSBarry Smith   not Collective
4368e228b242SToby Isaac 
43699435951eSToby Isaac   Input Parameter:
4370bb7acecfSBarry Smith . dm - The `DM`
43719435951eSToby Isaac 
4372d8d19677SJose E. Roman   Output Parameters:
437320f4b53cSBarry 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.
437420f4b53cSBarry 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.
437579769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
43769435951eSToby Isaac 
43779435951eSToby Isaac   Level: advanced
43789435951eSToby Isaac 
4379bb7acecfSBarry Smith   Note:
4380bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43819435951eSToby Isaac 
43821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
43839435951eSToby Isaac @*/
4384d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4385d71ae5a4SJacob Faibussowitsch {
43869435951eSToby Isaac   PetscFunctionBegin;
43879435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4388dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43893b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43903b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
439179769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43939435951eSToby Isaac }
43949435951eSToby Isaac 
43959435951eSToby Isaac /*@
4396bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43979435951eSToby Isaac 
439820f4b53cSBarry Smith   Collective
4399e228b242SToby Isaac 
44009435951eSToby Isaac   Input Parameters:
4401bb7acecfSBarry Smith + dm      - The `DM`
4402bb7acecfSBarry 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).
440320f4b53cSBarry 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).
440420f4b53cSBarry 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).
44059435951eSToby Isaac 
44069435951eSToby Isaac   Level: advanced
44079435951eSToby Isaac 
440820f4b53cSBarry Smith   Notes:
440920f4b53cSBarry 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()`.
441020f4b53cSBarry Smith 
441120f4b53cSBarry 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.
441220f4b53cSBarry Smith 
4413bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44149435951eSToby Isaac 
44151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44169435951eSToby Isaac @*/
4417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4418d71ae5a4SJacob Faibussowitsch {
4419e228b242SToby Isaac   PetscMPIInt result;
44209435951eSToby Isaac 
44219435951eSToby Isaac   PetscFunctionBegin;
44229435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4423e228b242SToby Isaac   if (section) {
4424e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44259566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44267a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4427e228b242SToby Isaac   }
4428e228b242SToby Isaac   if (mat) {
4429e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44309566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44317a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4432e228b242SToby Isaac   }
443379769bd5SJed Brown   if (bias) {
443479769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44359566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
443679769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
443779769bd5SJed Brown   }
44389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44399566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44403b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44429566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44433b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44459566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
444679769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44489435951eSToby Isaac }
44499435951eSToby Isaac 
4450497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4451507e4973SMatthew G. Knepley /*
4452bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4453507e4973SMatthew G. Knepley 
4454507e4973SMatthew G. Knepley   Input Parameters:
4455bb7acecfSBarry Smith + dm - The `DM`
4456bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4457bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4458507e4973SMatthew G. Knepley 
4459507e4973SMatthew G. Knepley   Level: intermediate
4460507e4973SMatthew G. Knepley 
44611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4462507e4973SMatthew G. Knepley */
4463d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4464d71ae5a4SJacob Faibussowitsch {
4465507e4973SMatthew G. Knepley   MPI_Comm        comm;
4466507e4973SMatthew G. Knepley   PetscLayout     layout;
4467507e4973SMatthew G. Knepley   const PetscInt *ranges;
4468507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4469507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4470507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4471507e4973SMatthew G. Knepley 
4472507e4973SMatthew G. Knepley   PetscFunctionBegin;
44739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4474507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44779566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44789566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44799566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44809566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44819566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44829566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44839566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4484507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4485f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4486507e4973SMatthew G. Knepley 
44879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44889566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44899566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44909566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44919566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44929566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4493507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44949371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44959371c9d4SSatish 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));
44969371c9d4SSatish Balay       valid = PETSC_FALSE;
44979371c9d4SSatish Balay     }
44989371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44999371c9d4SSatish 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));
45009371c9d4SSatish Balay       valid = PETSC_FALSE;
45019371c9d4SSatish Balay     }
4502507e4973SMatthew G. Knepley     if (gdof < 0) {
4503507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4504507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4505507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4506507e4973SMatthew G. Knepley 
45079566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4508507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45099371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45109371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45119371c9d4SSatish Balay           valid = PETSC_FALSE;
45129371c9d4SSatish Balay           break;
45139371c9d4SSatish Balay         }
4514507e4973SMatthew G. Knepley       }
4515507e4973SMatthew G. Knepley     }
4516507e4973SMatthew G. Knepley   }
45179566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45189566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45191c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4520507e4973SMatthew G. Knepley   if (!gvalid) {
45219566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4522507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4523507e4973SMatthew G. Knepley   }
45243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4525507e4973SMatthew G. Knepley }
4526f741bcd2SMatthew G. Knepley #endif
4527507e4973SMatthew G. Knepley 
45285f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45296725e60dSJed Brown {
45306725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45316725e60dSJed Brown   PetscFunctionBegin;
45326725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45334f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45345f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45356725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45366725e60dSJed Brown   else *sf = dm->sf;
45373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45386725e60dSJed Brown }
45396725e60dSJed Brown 
454088ed4aceSMatthew G Knepley /*@
4541bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
454288ed4aceSMatthew G Knepley 
454320f4b53cSBarry Smith   Collective
45448b1ab98fSJed Brown 
454588ed4aceSMatthew G Knepley   Input Parameter:
4546bb7acecfSBarry Smith . dm - The `DM`
454788ed4aceSMatthew G Knepley 
454888ed4aceSMatthew G Knepley   Output Parameter:
4549bb7acecfSBarry Smith . section - The `PetscSection`
455088ed4aceSMatthew G Knepley 
455188ed4aceSMatthew G Knepley   Level: intermediate
455288ed4aceSMatthew G Knepley 
4553bb7acecfSBarry Smith   Note:
4554bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
455588ed4aceSMatthew G Knepley 
45561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
455788ed4aceSMatthew G Knepley @*/
4558d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4559d71ae5a4SJacob Faibussowitsch {
456088ed4aceSMatthew G Knepley   PetscFunctionBegin;
456188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45624f572ea9SToby Isaac   PetscAssertPointer(section, 2);
45631bb6d2a8SBarry Smith   if (!dm->globalSection) {
4564fd59a867SMatthew G. Knepley     PetscSection s;
45656725e60dSJed Brown     PetscSF      sf;
4566fd59a867SMatthew G. Knepley 
45679566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45687a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45697a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45705f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45716725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45729566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45749566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
457588ed4aceSMatthew G Knepley   }
45761bb6d2a8SBarry Smith   *section = dm->globalSection;
45773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
457888ed4aceSMatthew G Knepley }
457988ed4aceSMatthew G Knepley 
4580b21d0597SMatthew G Knepley /*@
4581bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4582b21d0597SMatthew G Knepley 
4583b21d0597SMatthew G Knepley   Input Parameters:
4584bb7acecfSBarry Smith + dm      - The `DM`
45852fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4586b21d0597SMatthew G Knepley 
4587b21d0597SMatthew G Knepley   Level: intermediate
4588b21d0597SMatthew G Knepley 
4589bb7acecfSBarry Smith   Note:
4590bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4591b21d0597SMatthew G Knepley 
45921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4593b21d0597SMatthew G Knepley @*/
4594d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4595d71ae5a4SJacob Faibussowitsch {
4596b21d0597SMatthew G Knepley   PetscFunctionBegin;
4597b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45985080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46009566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46011bb6d2a8SBarry Smith   dm->globalSection = section;
4602497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46039566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4604507e4973SMatthew G. Knepley #endif
46053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4606b21d0597SMatthew G Knepley }
4607b21d0597SMatthew G Knepley 
460888ed4aceSMatthew G Knepley /*@
4609bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4610bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
461188ed4aceSMatthew G Knepley 
461288ed4aceSMatthew G Knepley   Input Parameter:
4613bb7acecfSBarry Smith . dm - The `DM`
461488ed4aceSMatthew G Knepley 
461588ed4aceSMatthew G Knepley   Output Parameter:
4616bb7acecfSBarry Smith . sf - The `PetscSF`
461788ed4aceSMatthew G Knepley 
461888ed4aceSMatthew G Knepley   Level: intermediate
461988ed4aceSMatthew G Knepley 
4620bb7acecfSBarry Smith   Note:
4621bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
462288ed4aceSMatthew G Knepley 
46231cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
462488ed4aceSMatthew G Knepley @*/
4625d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4626d71ae5a4SJacob Faibussowitsch {
462788ed4aceSMatthew G Knepley   PetscInt nroots;
462888ed4aceSMatthew G Knepley 
462988ed4aceSMatthew G Knepley   PetscFunctionBegin;
463088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46314f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
463248a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46339566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
463488ed4aceSMatthew G Knepley   if (nroots < 0) {
463588ed4aceSMatthew G Knepley     PetscSection section, gSection;
463688ed4aceSMatthew G Knepley 
46379566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
463831ea6d37SMatthew G Knepley     if (section) {
46399566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46409566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
464131ea6d37SMatthew G Knepley     } else {
46420298fd71SBarry Smith       *sf = NULL;
46433ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
464431ea6d37SMatthew G Knepley     }
464588ed4aceSMatthew G Knepley   }
46461bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
464888ed4aceSMatthew G Knepley }
464988ed4aceSMatthew G Knepley 
465088ed4aceSMatthew G Knepley /*@
4651bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
465288ed4aceSMatthew G Knepley 
465388ed4aceSMatthew G Knepley   Input Parameters:
4654bb7acecfSBarry Smith + dm - The `DM`
4655bb7acecfSBarry Smith - sf - The `PetscSF`
465688ed4aceSMatthew G Knepley 
465788ed4aceSMatthew G Knepley   Level: intermediate
465888ed4aceSMatthew G Knepley 
4659bb7acecfSBarry Smith   Note:
4660bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
466188ed4aceSMatthew G Knepley 
46621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
466388ed4aceSMatthew G Knepley @*/
4664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4665d71ae5a4SJacob Faibussowitsch {
466688ed4aceSMatthew G Knepley   PetscFunctionBegin;
466788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4668b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46709566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46711bb6d2a8SBarry Smith   dm->sectionSF = sf;
46723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467388ed4aceSMatthew G Knepley }
467488ed4aceSMatthew G Knepley 
467588ed4aceSMatthew G Knepley /*@C
4676bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
467788ed4aceSMatthew G Knepley   describing the data layout.
467888ed4aceSMatthew G Knepley 
467988ed4aceSMatthew G Knepley   Input Parameters:
4680bb7acecfSBarry Smith + dm            - The `DM`
4681bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4682bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
468388ed4aceSMatthew G Knepley 
46841bb6d2a8SBarry Smith   Level: developer
46851bb6d2a8SBarry Smith 
4686bb7acecfSBarry Smith   Note:
4687bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4688bb7acecfSBarry Smith 
468960225df5SJacob Faibussowitsch   Developer Notes:
4690bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4691bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4692bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46931bb6d2a8SBarry Smith 
46941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
469588ed4aceSMatthew G Knepley @*/
4696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4697d71ae5a4SJacob Faibussowitsch {
469888ed4aceSMatthew G Knepley   PetscFunctionBegin;
469988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47009566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
470288ed4aceSMatthew G Knepley }
4703af122d2aSMatthew G Knepley 
4704b21d0597SMatthew G Knepley /*@
4705bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4706bb7acecfSBarry Smith 
4707bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4708b21d0597SMatthew G Knepley 
4709b21d0597SMatthew G Knepley   Input Parameter:
4710bb7acecfSBarry Smith . dm - The `DM`
4711b21d0597SMatthew G Knepley 
4712b21d0597SMatthew G Knepley   Output Parameter:
4713bb7acecfSBarry Smith . sf - The `PetscSF`
4714b21d0597SMatthew G Knepley 
4715b21d0597SMatthew G Knepley   Level: intermediate
4716b21d0597SMatthew G Knepley 
4717bb7acecfSBarry Smith   Note:
4718bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4719b21d0597SMatthew G Knepley 
47201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4721b21d0597SMatthew G Knepley @*/
4722d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4723d71ae5a4SJacob Faibussowitsch {
4724b21d0597SMatthew G Knepley   PetscFunctionBegin;
4725b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47264f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4727b21d0597SMatthew G Knepley   *sf = dm->sf;
47283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4729b21d0597SMatthew G Knepley }
4730b21d0597SMatthew G Knepley 
4731057b4bcdSMatthew G Knepley /*@
4732bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4733bb7acecfSBarry Smith 
473420f4b53cSBarry Smith   Collective
4735057b4bcdSMatthew G Knepley 
4736057b4bcdSMatthew G Knepley   Input Parameters:
4737bb7acecfSBarry Smith + dm - The `DM`
4738bb7acecfSBarry Smith - sf - The `PetscSF`
4739057b4bcdSMatthew G Knepley 
4740057b4bcdSMatthew G Knepley   Level: intermediate
4741057b4bcdSMatthew G Knepley 
47421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4743057b4bcdSMatthew G Knepley @*/
4744d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4745d71ae5a4SJacob Faibussowitsch {
4746057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4747057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4748b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47509566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4751057b4bcdSMatthew G Knepley   dm->sf = sf;
47523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4753057b4bcdSMatthew G Knepley }
4754057b4bcdSMatthew G Knepley 
47554f37162bSMatthew G. Knepley /*@
4756bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47574f37162bSMatthew G. Knepley 
47584f37162bSMatthew G. Knepley   Input Parameter:
4759bb7acecfSBarry Smith . dm - The `DM`
47604f37162bSMatthew G. Knepley 
47614f37162bSMatthew G. Knepley   Output Parameter:
4762bb7acecfSBarry Smith . sf - The `PetscSF`
47634f37162bSMatthew G. Knepley 
47644f37162bSMatthew G. Knepley   Level: intermediate
47654f37162bSMatthew G. Knepley 
4766bb7acecfSBarry Smith   Note:
4767bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47684f37162bSMatthew G. Knepley 
47691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47704f37162bSMatthew G. Knepley @*/
4771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4772d71ae5a4SJacob Faibussowitsch {
47734f37162bSMatthew G. Knepley   PetscFunctionBegin;
47744f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47754f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
47764f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47784f37162bSMatthew G. Knepley }
47794f37162bSMatthew G. Knepley 
47804f37162bSMatthew G. Knepley /*@
47814f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47824f37162bSMatthew G. Knepley 
47834f37162bSMatthew G. Knepley   Input Parameters:
47844f37162bSMatthew G. Knepley + dm - The DM
47854f37162bSMatthew G. Knepley - sf - The PetscSF
47864f37162bSMatthew G. Knepley 
47874f37162bSMatthew G. Knepley   Level: intermediate
47884f37162bSMatthew G. Knepley 
47891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47904f37162bSMatthew G. Knepley @*/
4791d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4792d71ae5a4SJacob Faibussowitsch {
47934f37162bSMatthew G. Knepley   PetscFunctionBegin;
47944f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47954f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47969566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47979566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47984f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48004f37162bSMatthew G. Knepley }
48014f37162bSMatthew G. Knepley 
4802d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4803d71ae5a4SJacob Faibussowitsch {
480434aa8a36SMatthew G. Knepley   PetscClassId id;
480534aa8a36SMatthew G. Knepley 
480634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
480834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48099566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
481034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48119566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
481217c1d62eSMatthew G. Knepley   } else {
48139566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
481434aa8a36SMatthew G. Knepley   }
48153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
481634aa8a36SMatthew G. Knepley }
481734aa8a36SMatthew G. Knepley 
4818d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4819d71ae5a4SJacob Faibussowitsch {
482044a7f3ddSMatthew G. Knepley   RegionField *tmpr;
482144a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
482244a7f3ddSMatthew G. Knepley 
482344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48243ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
482644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48279371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48289371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48299371c9d4SSatish Balay     tmpr[f].label       = NULL;
48309371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48319371c9d4SSatish Balay   }
48329566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
483344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
483444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
483644a7f3ddSMatthew G. Knepley }
483744a7f3ddSMatthew G. Knepley 
483844a7f3ddSMatthew G. Knepley /*@
483920f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
484044a7f3ddSMatthew G. Knepley 
484120f4b53cSBarry Smith   Logically Collective
484244a7f3ddSMatthew G. Knepley 
484344a7f3ddSMatthew G. Knepley   Input Parameter:
484420f4b53cSBarry Smith . dm - The `DM`
484544a7f3ddSMatthew G. Knepley 
484644a7f3ddSMatthew G. Knepley   Level: intermediate
484744a7f3ddSMatthew G. Knepley 
48481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
484944a7f3ddSMatthew G. Knepley @*/
4850d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4851d71ae5a4SJacob Faibussowitsch {
485244a7f3ddSMatthew G. Knepley   PetscInt f;
485344a7f3ddSMatthew G. Knepley 
485444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
485544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
485644a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48579566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48589566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
485944a7f3ddSMatthew G. Knepley   }
48609566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
486144a7f3ddSMatthew G. Knepley   dm->fields = NULL;
486244a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
486444a7f3ddSMatthew G. Knepley }
486544a7f3ddSMatthew G. Knepley 
4866689b5837SMatthew G. Knepley /*@
486720f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4868689b5837SMatthew G. Knepley 
486920f4b53cSBarry Smith   Not Collective
4870689b5837SMatthew G. Knepley 
4871689b5837SMatthew G. Knepley   Input Parameter:
487220f4b53cSBarry Smith . dm - The `DM`
4873689b5837SMatthew G. Knepley 
4874689b5837SMatthew G. Knepley   Output Parameter:
487560225df5SJacob Faibussowitsch . numFields - The number of fields
4876689b5837SMatthew G. Knepley 
4877689b5837SMatthew G. Knepley   Level: intermediate
4878689b5837SMatthew G. Knepley 
48791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4880689b5837SMatthew G. Knepley @*/
4881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4882d71ae5a4SJacob Faibussowitsch {
48830f21e855SMatthew G. Knepley   PetscFunctionBegin;
48840f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48854f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
488644a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
48873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4888af122d2aSMatthew G Knepley }
4889af122d2aSMatthew G Knepley 
4890689b5837SMatthew G. Knepley /*@
489120f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4892689b5837SMatthew G. Knepley 
489320f4b53cSBarry Smith   Logically Collective
4894689b5837SMatthew G. Knepley 
4895689b5837SMatthew G. Knepley   Input Parameters:
489620f4b53cSBarry Smith + dm        - The `DM`
489760225df5SJacob Faibussowitsch - numFields - The number of fields
4898689b5837SMatthew G. Knepley 
4899689b5837SMatthew G. Knepley   Level: intermediate
4900689b5837SMatthew G. Knepley 
49011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4902689b5837SMatthew G. Knepley @*/
4903d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4904d71ae5a4SJacob Faibussowitsch {
49050f21e855SMatthew G. Knepley   PetscInt Nf, f;
4906af122d2aSMatthew G Knepley 
4907af122d2aSMatthew G Knepley   PetscFunctionBegin;
4908af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49099566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49100f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49110f21e855SMatthew G. Knepley     PetscContainer obj;
49120f21e855SMatthew G. Knepley 
49139566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49149566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49159566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4916af122d2aSMatthew G Knepley   }
49173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4918af122d2aSMatthew G Knepley }
4919af122d2aSMatthew G Knepley 
4920c1929be8SMatthew G. Knepley /*@
4921bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4922c1929be8SMatthew G. Knepley 
492320f4b53cSBarry Smith   Not Collective
4924c1929be8SMatthew G. Knepley 
4925c1929be8SMatthew G. Knepley   Input Parameters:
4926bb7acecfSBarry Smith + dm - The `DM`
4927c1929be8SMatthew G. Knepley - f  - The field number
4928c1929be8SMatthew G. Knepley 
492944a7f3ddSMatthew G. Knepley   Output Parameters:
493020f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
493120f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4932c1929be8SMatthew G. Knepley 
493344a7f3ddSMatthew G. Knepley   Level: intermediate
4934c1929be8SMatthew G. Knepley 
49351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
4936c1929be8SMatthew G. Knepley @*/
4937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4938d71ae5a4SJacob Faibussowitsch {
4939af122d2aSMatthew G Knepley   PetscFunctionBegin;
4940af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49414f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
49427a8be351SBarry 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);
494344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4944bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4946decb47aaSMatthew G. Knepley }
4947decb47aaSMatthew G. Knepley 
4948083401c6SMatthew G. Knepley /* Does not clear the DS */
4949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4950d71ae5a4SJacob Faibussowitsch {
4951083401c6SMatthew G. Knepley   PetscFunctionBegin;
49529566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49539566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49549566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4955083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4956bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4958bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4960083401c6SMatthew G. Knepley }
4961083401c6SMatthew G. Knepley 
496246560f82SMatthew G. Knepley /*@C
4963bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4964bb7acecfSBarry Smith   the field numbering.
4965c1929be8SMatthew G. Knepley 
496620f4b53cSBarry Smith   Logically Collective
4967c1929be8SMatthew G. Knepley 
4968c1929be8SMatthew G. Knepley   Input Parameters:
4969bb7acecfSBarry Smith + dm    - The `DM`
4970c1929be8SMatthew G. Knepley . f     - The field number
497120f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4972bb7acecfSBarry Smith - disc  - The discretization object
4973c1929be8SMatthew G. Knepley 
497444a7f3ddSMatthew G. Knepley   Level: intermediate
4975c1929be8SMatthew G. Knepley 
49761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
4977c1929be8SMatthew G. Knepley @*/
4978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4979d71ae5a4SJacob Faibussowitsch {
4980decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4981decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4982e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4983bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49847a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4985bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4986bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49879566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
498944a7f3ddSMatthew G. Knepley }
499044a7f3ddSMatthew G. Knepley 
499146560f82SMatthew G. Knepley /*@C
4992bb7acecfSBarry 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)
4993bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
499444a7f3ddSMatthew G. Knepley 
499520f4b53cSBarry Smith   Logically Collective
499644a7f3ddSMatthew G. Knepley 
499744a7f3ddSMatthew G. Knepley   Input Parameters:
4998bb7acecfSBarry Smith + dm    - The `DM`
499920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5000bb7acecfSBarry Smith - disc  - The discretization object
500144a7f3ddSMatthew G. Knepley 
500244a7f3ddSMatthew G. Knepley   Level: intermediate
500344a7f3ddSMatthew G. Knepley 
5004bb7acecfSBarry Smith   Notes:
5005bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5006bb7acecfSBarry Smith 
5007da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5008bb7acecfSBarry 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
5009bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5010bb7acecfSBarry Smith 
50111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
501244a7f3ddSMatthew G. Knepley @*/
5013d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5014d71ae5a4SJacob Faibussowitsch {
501544a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
501644a7f3ddSMatthew G. Knepley 
501744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
501844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5019064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5020bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50219566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
502244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5023bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5025bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5026bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50279566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5029af122d2aSMatthew G Knepley }
50306636e97aSMatthew G Knepley 
5031e5e52638SMatthew G. Knepley /*@
5032e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5033e0b68406SMatthew Knepley 
503420f4b53cSBarry Smith   Logically Collective
5035e0b68406SMatthew Knepley 
5036e0b68406SMatthew Knepley   Input Parameters:
5037bb7acecfSBarry Smith + dm          - The `DM`
5038e0b68406SMatthew Knepley . f           - The field index
5039bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5040e0b68406SMatthew Knepley 
5041e0b68406SMatthew Knepley   Level: intermediate
5042e0b68406SMatthew Knepley 
50431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5044e0b68406SMatthew Knepley @*/
5045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5046d71ae5a4SJacob Faibussowitsch {
5047e0b68406SMatthew Knepley   PetscFunctionBegin;
504863a3b9bcSJacob 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);
5049e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5051e0b68406SMatthew Knepley }
5052e0b68406SMatthew Knepley 
5053e0b68406SMatthew Knepley /*@
5054e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5055e0b68406SMatthew Knepley 
505620f4b53cSBarry Smith   Not Collective
5057e0b68406SMatthew Knepley 
5058e0b68406SMatthew Knepley   Input Parameters:
5059bb7acecfSBarry Smith + dm - The `DM`
5060e0b68406SMatthew Knepley - f  - The field index
5061e0b68406SMatthew Knepley 
5062e0b68406SMatthew Knepley   Output Parameter:
5063e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5064e0b68406SMatthew Knepley 
5065e0b68406SMatthew Knepley   Level: intermediate
5066e0b68406SMatthew Knepley 
506760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5068e0b68406SMatthew Knepley @*/
5069d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5070d71ae5a4SJacob Faibussowitsch {
5071e0b68406SMatthew Knepley   PetscFunctionBegin;
507263a3b9bcSJacob 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);
5073e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5075e0b68406SMatthew Knepley }
5076e0b68406SMatthew Knepley 
5077e0b68406SMatthew Knepley /*@
5078bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5079e5e52638SMatthew G. Knepley 
508020f4b53cSBarry Smith   Collective
5081e5e52638SMatthew G. Knepley 
5082e5e52638SMatthew G. Knepley   Input Parameter:
5083bb7acecfSBarry Smith . dm - The `DM`
5084e5e52638SMatthew G. Knepley 
5085e5e52638SMatthew G. Knepley   Output Parameter:
5086bb7acecfSBarry Smith . newdm - The `DM`
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley   Level: advanced
5089e5e52638SMatthew G. Knepley 
50901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5091e5e52638SMatthew G. Knepley @*/
5092d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5093d71ae5a4SJacob Faibussowitsch {
5094e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley   PetscFunctionBegin;
50973ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
50989566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50999566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5100e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5101e5e52638SMatthew G. Knepley     DMLabel     label;
5102e5e52638SMatthew G. Knepley     PetscObject field;
510334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5104e5e52638SMatthew G. Knepley 
51059566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51069566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51079566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51089566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
510934aa8a36SMatthew G. Knepley   }
51103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
511134aa8a36SMatthew G. Knepley }
511234aa8a36SMatthew G. Knepley 
511334aa8a36SMatthew G. Knepley /*@
511434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
511534aa8a36SMatthew G. Knepley 
511620f4b53cSBarry Smith   Not Collective
511734aa8a36SMatthew G. Knepley 
511834aa8a36SMatthew G. Knepley   Input Parameters:
511920f4b53cSBarry Smith + dm - The `DM` object
512020f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
512134aa8a36SMatthew G. Knepley 
5122d8d19677SJose E. Roman   Output Parameters:
512334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
512434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
512534aa8a36SMatthew G. Knepley 
512634aa8a36SMatthew G. Knepley   Level: developer
512734aa8a36SMatthew G. Knepley 
512820f4b53cSBarry Smith   Notes:
512920f4b53cSBarry Smith .vb
513020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
513120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
513220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
513320f4b53cSBarry Smith .ve
513420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
513520f4b53cSBarry Smith 
51361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
513734aa8a36SMatthew G. Knepley @*/
5138d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5139d71ae5a4SJacob Faibussowitsch {
514034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
514134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51424f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
51434f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
514434aa8a36SMatthew G. Knepley   if (f < 0) {
514534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
514634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
514734aa8a36SMatthew G. Knepley   } else {
514834aa8a36SMatthew G. Knepley     PetscInt Nf;
514934aa8a36SMatthew G. Knepley 
51509566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51517a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
515234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
515334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
515434aa8a36SMatthew G. Knepley   }
51553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
515634aa8a36SMatthew G. Knepley }
515734aa8a36SMatthew G. Knepley 
515834aa8a36SMatthew G. Knepley /*@
515934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
516034aa8a36SMatthew G. Knepley 
516120f4b53cSBarry Smith   Not Collective
516234aa8a36SMatthew G. Knepley 
516334aa8a36SMatthew G. Knepley   Input Parameters:
516420f4b53cSBarry Smith + dm         - The `DM` object
516534aa8a36SMatthew G. Knepley . f          - The field number
516634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
516734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
516834aa8a36SMatthew G. Knepley 
516934aa8a36SMatthew G. Knepley   Level: developer
517034aa8a36SMatthew G. Knepley 
517120f4b53cSBarry Smith   Notes:
517220f4b53cSBarry Smith .vb
517320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
517420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
517520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
517620f4b53cSBarry Smith .ve
517720f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
517820f4b53cSBarry Smith 
51791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
518034aa8a36SMatthew G. Knepley @*/
5181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5182d71ae5a4SJacob Faibussowitsch {
518334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
518434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
518534aa8a36SMatthew G. Knepley   if (f < 0) {
518634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
518734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
518834aa8a36SMatthew G. Knepley   } else {
518934aa8a36SMatthew G. Knepley     PetscInt Nf;
519034aa8a36SMatthew G. Knepley 
51919566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51927a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
519334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
519434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5195e5e52638SMatthew G. Knepley   }
51963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5197e5e52638SMatthew G. Knepley }
5198e5e52638SMatthew G. Knepley 
5199b0441da4SMatthew G. Knepley /*@
5200b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5201b0441da4SMatthew G. Knepley 
5202b0441da4SMatthew G. Knepley   Not collective
5203b0441da4SMatthew G. Knepley 
5204f899ff85SJose E. Roman   Input Parameter:
520520f4b53cSBarry Smith . dm - The `DM` object
5206b0441da4SMatthew G. Knepley 
5207d8d19677SJose E. Roman   Output Parameters:
5208b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5209b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5210b0441da4SMatthew G. Knepley 
5211b0441da4SMatthew G. Knepley   Level: developer
5212b0441da4SMatthew G. Knepley 
521320f4b53cSBarry Smith   Notes:
521420f4b53cSBarry Smith .vb
521520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
521620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
521720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
521820f4b53cSBarry Smith .ve
521920f4b53cSBarry Smith 
52201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5221b0441da4SMatthew G. Knepley @*/
5222d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5223d71ae5a4SJacob Faibussowitsch {
5224b0441da4SMatthew G. Knepley   PetscInt Nf;
5225b0441da4SMatthew G. Knepley 
5226b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5227b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52284f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52294f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52309566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5231b0441da4SMatthew G. Knepley   if (!Nf) {
52329566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5233b0441da4SMatthew G. Knepley   } else {
52349566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5235b0441da4SMatthew G. Knepley   }
52363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5237b0441da4SMatthew G. Knepley }
5238b0441da4SMatthew G. Knepley 
5239b0441da4SMatthew G. Knepley /*@
5240b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5241b0441da4SMatthew G. Knepley 
524220f4b53cSBarry Smith   Not Collective
5243b0441da4SMatthew G. Knepley 
5244b0441da4SMatthew G. Knepley   Input Parameters:
524520f4b53cSBarry Smith + dm         - The `DM` object
5246b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5247b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5248b0441da4SMatthew G. Knepley 
5249b0441da4SMatthew G. Knepley   Level: developer
5250b0441da4SMatthew G. Knepley 
525120f4b53cSBarry Smith   Notes:
525220f4b53cSBarry Smith .vb
525320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
525420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
525520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
525620f4b53cSBarry Smith .ve
525720f4b53cSBarry Smith 
52581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5259b0441da4SMatthew G. Knepley @*/
5260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5261d71ae5a4SJacob Faibussowitsch {
5262b0441da4SMatthew G. Knepley   PetscInt Nf;
5263b0441da4SMatthew G. Knepley 
5264b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5265b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52669566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5267b0441da4SMatthew G. Knepley   if (!Nf) {
52689566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5269b0441da4SMatthew G. Knepley   } else {
52709566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5271e5e52638SMatthew G. Knepley   }
52723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5273e5e52638SMatthew G. Knepley }
5274e5e52638SMatthew G. Knepley 
5275d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5276d71ae5a4SJacob Faibussowitsch {
5277799db056SMatthew G. Knepley   DM           plex;
5278799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5279799db056SMatthew G. Knepley   const char **names;
5280799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5281799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5282799db056SMatthew G. Knepley   size_t       len;
5283799db056SMatthew G. Knepley   MPI_Comm     comm;
5284799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5285783e2ec8SMatthew G. Knepley 
5286783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5287799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5288799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5289799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5290799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5291799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5292799db056SMatthew G. Knepley     PetscDS  dsBC;
5293799db056SMatthew G. Knepley     PetscInt numBd;
5294799db056SMatthew G. Knepley 
529507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5296799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5297799db056SMatthew G. Knepley     maxLabels += numBd;
5298799db056SMatthew G. Knepley   }
5299799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5300799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5301799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5302799db056SMatthew G. Knepley     PetscDS  dsBC;
5303799db056SMatthew G. Knepley     PetscInt numBd, bd;
5304799db056SMatthew G. Knepley 
530507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5306799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5307799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5308799db056SMatthew G. Knepley       DMLabel      label;
5309799db056SMatthew G. Knepley       PetscInt     field;
5310799db056SMatthew G. Knepley       PetscObject  obj;
5311799db056SMatthew G. Knepley       PetscClassId id;
5312799db056SMatthew G. Knepley 
5313799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53149566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53159566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5316799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53179371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53189371c9d4SSatish Balay         if (labels[l] == label) break;
5319799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5320783e2ec8SMatthew G. Knepley     }
5321799db056SMatthew G. Knepley   }
5322799db056SMatthew G. Knepley   /* Get label names */
5323799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5324799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53259371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53269371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53279371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53289371c9d4SSatish Balay   }
5329799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5330712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5331799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5332c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5333799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5334799db056SMatthew G. Knepley   /* Put all names on all processes */
5335799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5336799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5337799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5338799db056SMatthew G. Knepley   gNl = displs[size];
53399371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53409371c9d4SSatish Balay     counts[p] *= gmaxLen;
53419371c9d4SSatish Balay     displs[p] *= gmaxLen;
53429371c9d4SSatish Balay   }
5343799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5344799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5345799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5346799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5347799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5348799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5349799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53509371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53519371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53529566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5353799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53549566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5355799db056SMatthew G. Knepley     ++gl;
5356783e2ec8SMatthew G. Knepley   }
5357799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5359783e2ec8SMatthew G. Knepley }
5360783e2ec8SMatthew G. Knepley 
5361d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5362d71ae5a4SJacob Faibussowitsch {
5363e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5364e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5365e5e52638SMatthew G. Knepley 
5366e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53673ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53689566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5369e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53709371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53719371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53729371c9d4SSatish Balay     tmpd[s].label  = NULL;
53739371c9d4SSatish Balay     tmpd[s].fields = NULL;
53749371c9d4SSatish Balay   }
53759566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5376e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5377e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5379e5e52638SMatthew G. Knepley }
5380e5e52638SMatthew G. Knepley 
5381e5e52638SMatthew G. Knepley /*@
538220f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5383e5e52638SMatthew G. Knepley 
538420f4b53cSBarry Smith   Not Collective
5385e5e52638SMatthew G. Knepley 
5386e5e52638SMatthew G. Knepley   Input Parameter:
538720f4b53cSBarry Smith . dm - The `DM`
5388e5e52638SMatthew G. Knepley 
5389e5e52638SMatthew G. Knepley   Output Parameter:
539020f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5391e5e52638SMatthew G. Knepley 
5392e5e52638SMatthew G. Knepley   Level: intermediate
5393e5e52638SMatthew G. Knepley 
53941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5395e5e52638SMatthew G. Knepley @*/
5396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5397d71ae5a4SJacob Faibussowitsch {
5398e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5399e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54004f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5401e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5403e5e52638SMatthew G. Knepley }
5404e5e52638SMatthew G. Knepley 
5405e5e52638SMatthew G. Knepley /*@
540620f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5407e5e52638SMatthew G. Knepley 
540820f4b53cSBarry Smith   Logically Collective
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   Input Parameter:
541120f4b53cSBarry Smith . dm - The `DM`
5412e5e52638SMatthew G. Knepley 
5413e5e52638SMatthew G. Knepley   Level: intermediate
5414e5e52638SMatthew G. Knepley 
54151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5416e5e52638SMatthew G. Knepley @*/
5417d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5418d71ae5a4SJacob Faibussowitsch {
5419e5e52638SMatthew G. Knepley   PetscInt s;
5420e5e52638SMatthew G. Knepley 
5421e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5422e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5423e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54249566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
542507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54269566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5428e5e52638SMatthew G. Knepley   }
54299566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5430e5e52638SMatthew G. Knepley   dm->probs = NULL;
5431e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5433e5e52638SMatthew G. Knepley }
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley /*@
543620f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5437e5e52638SMatthew G. Knepley 
543820f4b53cSBarry Smith   Not Collective
5439e5e52638SMatthew G. Knepley 
5440e5e52638SMatthew G. Knepley   Input Parameter:
544120f4b53cSBarry Smith . dm - The `DM`
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley   Output Parameter:
544407218a29SMatthew G. Knepley . ds - The default `PetscDS`
5445e5e52638SMatthew G. Knepley 
5446e5e52638SMatthew G. Knepley   Level: intermediate
5447e5e52638SMatthew G. Knepley 
54481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5449e5e52638SMatthew G. Knepley @*/
545007218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5451d71ae5a4SJacob Faibussowitsch {
5452e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5453e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54544f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
545507218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
545607218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
54573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5458e5e52638SMatthew G. Knepley }
5459e5e52638SMatthew G. Knepley 
5460e5e52638SMatthew G. Knepley /*@
546120f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5462e5e52638SMatthew G. Knepley 
546320f4b53cSBarry Smith   Not Collective
5464e5e52638SMatthew G. Knepley 
5465e5e52638SMatthew G. Knepley   Input Parameters:
546620f4b53cSBarry Smith + dm    - The `DM`
546720f4b53cSBarry Smith - point - Cell for the `PetscDS`
5468e5e52638SMatthew G. Knepley 
546907218a29SMatthew G. Knepley   Output Parameters:
547007218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
547107218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5472e5e52638SMatthew G. Knepley 
5473e5e52638SMatthew G. Knepley   Level: developer
5474e5e52638SMatthew G. Knepley 
54751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5476e5e52638SMatthew G. Knepley @*/
547707218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5478d71ae5a4SJacob Faibussowitsch {
547907218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5480e5e52638SMatthew G. Knepley   PetscInt s;
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5483e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54844f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
54854f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
548663a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
548707218a29SMatthew G. Knepley   if (ds) *ds = NULL;
548807218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5489e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5490e5e52638SMatthew G. Knepley     PetscInt val;
5491e5e52638SMatthew G. Knepley 
54929371c9d4SSatish Balay     if (!dm->probs[s].label) {
549307218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
54949371c9d4SSatish Balay     } else {
54959566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54969371c9d4SSatish Balay       if (val >= 0) {
549707218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
549807218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
54999371c9d4SSatish Balay         break;
55009371c9d4SSatish Balay       }
5501e5e52638SMatthew G. Knepley     }
5502e5e52638SMatthew G. Knepley   }
550307218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5505e5e52638SMatthew G. Knepley }
5506e5e52638SMatthew G. Knepley 
5507e5e52638SMatthew G. Knepley /*@
550820f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5509e5e52638SMatthew G. Knepley 
551020f4b53cSBarry Smith   Not Collective
5511e5e52638SMatthew G. Knepley 
5512e5e52638SMatthew G. Knepley   Input Parameters:
551320f4b53cSBarry Smith + dm    - The `DM`
551420f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5515e5e52638SMatthew G. Knepley 
5516b3cf3223SMatthew G. Knepley   Output Parameters:
551720f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
551807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
551907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5520e5e52638SMatthew G. Knepley 
5521e5e52638SMatthew G. Knepley   Level: advanced
5522e5e52638SMatthew G. Knepley 
552320f4b53cSBarry Smith   Note:
552420f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
552520f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
552607218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
552720f4b53cSBarry Smith 
55281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5529e5e52638SMatthew G. Knepley @*/
553007218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5531d71ae5a4SJacob Faibussowitsch {
5532e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5533e5e52638SMatthew G. Knepley 
5534e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5535e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5536e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55379371c9d4SSatish Balay   if (fields) {
55384f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
55399371c9d4SSatish Balay     *fields = NULL;
55409371c9d4SSatish Balay   }
55419371c9d4SSatish Balay   if (ds) {
55424f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
55439371c9d4SSatish Balay     *ds = NULL;
55449371c9d4SSatish Balay   }
554507218a29SMatthew G. Knepley   if (dsIn) {
55464f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
554707218a29SMatthew G. Knepley     *dsIn = NULL;
554807218a29SMatthew G. Knepley   }
5549e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5550154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5551b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5552b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
555307218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
55543ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5555b3cf3223SMatthew G. Knepley     }
5556e5e52638SMatthew G. Knepley   }
55573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5558e5e52638SMatthew G. Knepley }
5559e5e52638SMatthew G. Knepley 
5560e5e52638SMatthew G. Knepley /*@
5561bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5562083401c6SMatthew G. Knepley 
556320f4b53cSBarry Smith   Collective
5564083401c6SMatthew G. Knepley 
5565083401c6SMatthew G. Knepley   Input Parameters:
5566bb7acecfSBarry Smith + dm     - The `DM`
556720f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
556807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
556907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
557007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5571083401c6SMatthew G. Knepley 
557220f4b53cSBarry Smith   Level: advanced
557320f4b53cSBarry Smith 
5574bb7acecfSBarry Smith   Note:
5575bb7acecfSBarry 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,
5576083401c6SMatthew G. Knepley   the fields argument is ignored.
5577083401c6SMatthew G. Knepley 
55781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5579083401c6SMatthew G. Knepley @*/
558007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5581d71ae5a4SJacob Faibussowitsch {
5582083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5583083401c6SMatthew G. Knepley 
5584083401c6SMatthew G. Knepley   PetscFunctionBegin;
5585083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5586083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
558707218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5588064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
558907218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5590083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5591083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55929566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
559307218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5594083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
559507218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
55963ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5597083401c6SMatthew G. Knepley     }
5598083401c6SMatthew G. Knepley   }
55999566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
560307218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5604083401c6SMatthew G. Knepley   if (!label) {
5605083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5606083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5607083401c6SMatthew G. Knepley     Nds = 0;
5608083401c6SMatthew G. Knepley   }
5609083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5610083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5611083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
561207218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5614083401c6SMatthew G. Knepley }
5615083401c6SMatthew G. Knepley 
5616083401c6SMatthew G. Knepley /*@
561720f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5618e5e52638SMatthew G. Knepley 
561920f4b53cSBarry Smith   Not Collective
5620e5e52638SMatthew G. Knepley 
5621e5e52638SMatthew G. Knepley   Input Parameters:
562220f4b53cSBarry Smith + dm  - The `DM`
5623e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5624e5e52638SMatthew G. Knepley 
5625e5e52638SMatthew G. Knepley   Output Parameters:
562620f4b53cSBarry Smith + label  - The region label, or `NULL`
562720f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
562807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
562907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5630e5e52638SMatthew G. Knepley 
5631e5e52638SMatthew G. Knepley   Level: advanced
5632e5e52638SMatthew G. Knepley 
56331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5634e5e52638SMatthew G. Knepley @*/
563507218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5636d71ae5a4SJacob Faibussowitsch {
5637e5e52638SMatthew G. Knepley   PetscInt Nds;
5638e5e52638SMatthew G. Knepley 
5639e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5640e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56419566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
564263a3b9bcSJacob 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);
5643e5e52638SMatthew G. Knepley   if (label) {
56444f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5645e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5646e5e52638SMatthew G. Knepley   }
5647b3cf3223SMatthew G. Knepley   if (fields) {
56484f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5649b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5650b3cf3223SMatthew G. Knepley   }
5651e5e52638SMatthew G. Knepley   if (ds) {
56524f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5653e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5654e5e52638SMatthew G. Knepley   }
565507218a29SMatthew G. Knepley   if (dsIn) {
56564f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
565707218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
565807218a29SMatthew G. Knepley   }
56593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5660e5e52638SMatthew G. Knepley }
5661e5e52638SMatthew G. Knepley 
5662e5e52638SMatthew G. Knepley /*@
566320f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5664e5e52638SMatthew G. Knepley 
566520f4b53cSBarry Smith   Not Collective
5666e5e52638SMatthew G. Knepley 
5667e5e52638SMatthew G. Knepley   Input Parameters:
566820f4b53cSBarry Smith + dm     - The `DM`
5669083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
567020f4b53cSBarry Smith . label  - The region label, or `NULL`
567107218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
567207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
567307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5674e5e52638SMatthew G. Knepley 
5675e5e52638SMatthew G. Knepley   Level: advanced
5676e5e52638SMatthew G. Knepley 
56771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5678e5e52638SMatthew G. Knepley @*/
567907218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5680d71ae5a4SJacob Faibussowitsch {
5681083401c6SMatthew G. Knepley   PetscInt Nds;
5682e5e52638SMatthew G. Knepley 
5683e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5684e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5685ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56869566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568763a3b9bcSJacob 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);
56889566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56899566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5690083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5691083401c6SMatthew G. Knepley   if (fields) {
5692083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56939566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56949566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5695083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5696e5e52638SMatthew G. Knepley   }
5697083401c6SMatthew G. Knepley   if (ds) {
5698083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56999566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57009566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5701083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5702083401c6SMatthew G. Knepley   }
570307218a29SMatthew G. Knepley   if (dsIn) {
570407218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
570507218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
570607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
570707218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
570807218a29SMatthew G. Knepley   }
57093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5710e5e52638SMatthew G. Knepley }
5711e5e52638SMatthew G. Knepley 
5712e5e52638SMatthew G. Knepley /*@
571320f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57141d3af9e0SMatthew G. Knepley 
571520f4b53cSBarry Smith   Not Collective
57161d3af9e0SMatthew G. Knepley 
57171d3af9e0SMatthew G. Knepley   Input Parameters:
571820f4b53cSBarry Smith + dm - The `DM`
571920f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57201d3af9e0SMatthew G. Knepley 
57211d3af9e0SMatthew G. Knepley   Output Parameter:
57221d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57231d3af9e0SMatthew G. Knepley 
57241d3af9e0SMatthew G. Knepley   Level: advanced
57251d3af9e0SMatthew G. Knepley 
57261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57271d3af9e0SMatthew G. Knepley @*/
5728d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5729d71ae5a4SJacob Faibussowitsch {
57301d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57311d3af9e0SMatthew G. Knepley 
57321d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57331d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57341d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57354f572ea9SToby Isaac   PetscAssertPointer(num, 3);
57369566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57379371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57389371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57391d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57401d3af9e0SMatthew G. Knepley   else *num = n;
57413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57421d3af9e0SMatthew G. Knepley }
57431d3af9e0SMatthew G. Knepley 
57442df84da0SMatthew G. Knepley /*@C
5745bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57462df84da0SMatthew G. Knepley 
574720f4b53cSBarry Smith   Not Collective
57482df84da0SMatthew G. Knepley 
5749f1a722f8SMatthew G. Knepley   Input Parameters:
5750bb7acecfSBarry Smith + dm     - The `DM`
57512df84da0SMatthew G. Knepley . Nc     - The number of components for the field
575220f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5753bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57542df84da0SMatthew G. Knepley 
57552df84da0SMatthew G. Knepley   Output Parameter:
5756bb7acecfSBarry Smith . fem - The `PetscFE`
57572df84da0SMatthew G. Knepley 
575820f4b53cSBarry Smith   Level: intermediate
575920f4b53cSBarry Smith 
5760bb7acecfSBarry Smith   Note:
5761bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57622df84da0SMatthew G. Knepley 
57631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57642df84da0SMatthew G. Knepley @*/
5765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5766d71ae5a4SJacob Faibussowitsch {
57672df84da0SMatthew G. Knepley   DMPolytopeType ct;
57682df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57692df84da0SMatthew G. Knepley 
57702df84da0SMatthew G. Knepley   PetscFunctionBegin;
57712df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57722df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57734f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
57742df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57754f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
57769566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57779566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57799566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57812df84da0SMatthew G. Knepley }
57822df84da0SMatthew G. Knepley 
57831d3af9e0SMatthew G. Knepley /*@
5784bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5785e5e52638SMatthew G. Knepley 
578620f4b53cSBarry Smith   Collective
5787e5e52638SMatthew G. Knepley 
5788e5e52638SMatthew G. Knepley   Input Parameter:
5789bb7acecfSBarry Smith . dm - The `DM`
5790e5e52638SMatthew G. Knepley 
579120f4b53cSBarry Smith   Options Database Key:
5792bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
579345480ffeSMatthew G. Knepley 
579420f4b53cSBarry Smith   Level: intermediate
579520f4b53cSBarry Smith 
57961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5797e5e52638SMatthew G. Knepley @*/
5798d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5799d71ae5a4SJacob Faibussowitsch {
5800e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5801083401c6SMatthew G. Knepley   PetscDS   dsDef;
5802083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5803f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5804f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5805e5e52638SMatthew G. Knepley 
5806e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5807e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58083ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5811083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58129566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5813083401c6SMatthew G. Knepley   Nl   = 0;
5814083401c6SMatthew G. Knepley   Ndef = 0;
5815083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5816083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5817083401c6SMatthew G. Knepley     PetscInt l;
5818083401c6SMatthew G. Knepley 
5819f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5820f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5821f918ec44SMatthew G. Knepley     {
5822f918ec44SMatthew G. Knepley       PetscClassId id;
5823f918ec44SMatthew G. Knepley 
58249566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5825f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5826f918ec44SMatthew G. Knepley         Ceed ceed;
5827f918ec44SMatthew G. Knepley 
58289566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58299566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5830f918ec44SMatthew G. Knepley       }
5831f918ec44SMatthew G. Knepley     }
5832f918ec44SMatthew G. Knepley #endif
58339371c9d4SSatish Balay     if (!label) {
58349371c9d4SSatish Balay       ++Ndef;
58359371c9d4SSatish Balay       continue;
58369371c9d4SSatish Balay     }
58379371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58389371c9d4SSatish Balay       if (label == labelSet[l]) break;
5839083401c6SMatthew G. Knepley     if (l < Nl) continue;
5840083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5841083401c6SMatthew G. Knepley   }
5842083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
584307218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5844083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5845b3cf3223SMatthew G. Knepley     IS        fields;
5846b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5847b3cf3223SMatthew G. Knepley 
58489371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58499371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58507a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58519566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58529371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58539371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58549566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58559566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58569566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58579566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
585888f0c812SMatthew G. Knepley 
58599566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
586007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
58619566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58629566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58632df9ee95SMatthew G. Knepley   }
586407218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
58659566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5866083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5867083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58680122748bSMatthew G. Knepley     DM              plex;
5869083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5870083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5871083401c6SMatthew G. Knepley     PetscInt       *fields;
5872083401c6SMatthew G. Knepley     const PetscInt *cells;
5873083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58740122748bSMatthew G. Knepley 
58759566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58779566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58789566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58795fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5880083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5881083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5882083401c6SMatthew G. Knepley       IS      pointIS;
5883083401c6SMatthew G. Knepley 
58849566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58859566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58869566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58879566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5888083401c6SMatthew G. Knepley     }
58899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5890083401c6SMatthew G. Knepley 
58919566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58929566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58939566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58949566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58959566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58969566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58979566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5898083401c6SMatthew G. Knepley 
58999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59009371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59019371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59029566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59049566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59059566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5906083401c6SMatthew G. Knepley 
59079566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
590807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59099566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59109566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59119566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59139566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5914083401c6SMatthew G. Knepley   }
5915083401c6SMatthew G. Knepley   /* Create label DSes
5916083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5917083401c6SMatthew G. Knepley   */
5918083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5919083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5920083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
592107218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5922083401c6SMatthew G. Knepley     IS        fields;
5923083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5924083401c6SMatthew G. Knepley 
59259566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59269371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59279371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59289566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59299371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59309371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59319566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59329566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59339566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59349566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59359566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5936083401c6SMatthew G. Knepley     {
5937083401c6SMatthew G. Knepley       DMPolytopeType ct;
5938083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59395fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59400122748bSMatthew G. Knepley 
59419566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5942665f567fSMatthew G. Knepley       if (lStart >= 0) {
59439566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5944412e9a14SMatthew G. Knepley         switch (ct) {
5945412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5946412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5947412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5948d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5949d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5950d71ae5a4SJacob Faibussowitsch           break;
5951d71ae5a4SJacob Faibussowitsch         default:
5952d71ae5a4SJacob Faibussowitsch           break;
5953412e9a14SMatthew G. Knepley         }
5954665f567fSMatthew G. Knepley       }
5955712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
595607218a29SMatthew G. Knepley       if (isCohesive) {
595707218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
595807218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
595907218a29SMatthew G. Knepley       }
59605fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59615fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59625fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59639566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59649566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
596507218a29SMatthew G. Knepley             if (dsIn) {
596607218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
596707218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
596807218a29SMatthew G. Knepley             }
59695fedec97SMatthew G. Knepley           }
59705fedec97SMatthew G. Knepley           ++nf;
59715fedec97SMatthew G. Knepley         }
59725fedec97SMatthew G. Knepley       }
5973e5e52638SMatthew G. Knepley     }
597407218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
597507218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
59769566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
597707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
5978e5e52638SMatthew G. Knepley   }
59799566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5980e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5981083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5982083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
598307218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
5984083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5985083401c6SMatthew G. Knepley     const PetscInt *fld;
59865fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59875fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5988e5e52638SMatthew G. Knepley 
59899566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59909566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59919566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59929566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5993083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5994083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59955fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5996e5e52638SMatthew G. Knepley       PetscClassId id;
5997e5e52638SMatthew G. Knepley 
59985fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59999566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60005fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
600107218a29SMatthew G. Knepley       if (isCohesive) {
600207218a29SMatthew G. Knepley         if (!isCohesiveField) {
600307218a29SMatthew G. Knepley           PetscObject bdDisc;
600407218a29SMatthew G. Knepley 
600507218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
600607218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
600707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
600807218a29SMatthew G. Knepley         } else {
60099566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
601007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
601107218a29SMatthew G. Knepley         }
601207218a29SMatthew G. Knepley       } else {
601307218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
601407218a29SMatthew G. Knepley       }
6015083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60169566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6017e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6018e5e52638SMatthew G. Knepley     }
60199566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6020e5e52638SMatthew G. Knepley   }
6021f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6023f9244615SMatthew G. Knepley   if (flg) {
60243b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60253b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
602607218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60273b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60283b4aee56SMatthew G. Knepley 
60299566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
603007218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
603107218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
603207218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
603307218a29SMatthew G. Knepley       }
60343b4aee56SMatthew G. Knepley     }
6035f9244615SMatthew G. Knepley   }
6036e5e52638SMatthew G. Knepley   /* Setup DSes */
6037e5e52638SMatthew G. Knepley   if (doSetup) {
603807218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
603907218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
604007218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
604107218a29SMatthew G. Knepley     }
6042e5e52638SMatthew G. Knepley   }
60433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6044e5e52638SMatthew G. Knepley }
6045e5e52638SMatthew G. Knepley 
6046e5e52638SMatthew G. Knepley /*@
6047d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6048d2b2dc1eSMatthew G. Knepley 
6049d2b2dc1eSMatthew G. Knepley   Input Parameters:
6050d2b2dc1eSMatthew G. Knepley + dm     - The DM
6051d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6052d2b2dc1eSMatthew G. Knepley 
6053d2b2dc1eSMatthew G. Knepley   Level: developer
6054d2b2dc1eSMatthew G. Knepley 
6055d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6056d2b2dc1eSMatthew G. Knepley @*/
6057d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6058d2b2dc1eSMatthew G. Knepley {
6059d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6060d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6061d2b2dc1eSMatthew G. Knepley 
6062d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6063d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6064d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6065d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6066d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6067d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6068d2b2dc1eSMatthew G. Knepley 
6069d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6070d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6071d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6072d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6073d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6074d2b2dc1eSMatthew G. Knepley 
6075d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6076d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6077d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6078d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6079d2b2dc1eSMatthew G. Knepley   }
6080d2b2dc1eSMatthew G. Knepley   if (tensor) {
6081d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6082d2b2dc1eSMatthew G. Knepley   } else {
6083d2b2dc1eSMatthew G. Knepley     PetscSection s;
6084d2b2dc1eSMatthew G. Knepley 
6085d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6086d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6087d2b2dc1eSMatthew G. Knepley   }
6088d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6089d2b2dc1eSMatthew G. Knepley }
6090d2b2dc1eSMatthew G. Knepley 
6091d2b2dc1eSMatthew G. Knepley /*@
6092bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60937f96f943SMatthew G. Knepley 
609420f4b53cSBarry Smith   Collective
6095f2cacb80SMatthew G. Knepley 
60967f96f943SMatthew G. Knepley   Input Parameters:
6097bb7acecfSBarry Smith + dm   - The `DM`
60987f96f943SMatthew G. Knepley - time - The time
60997f96f943SMatthew G. Knepley 
61007f96f943SMatthew G. Knepley   Output Parameters:
610120f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
610220f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
610320f4b53cSBarry Smith 
610420f4b53cSBarry Smith   Level: developer
61057f96f943SMatthew G. Knepley 
6106bb7acecfSBarry Smith   Note:
6107bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61087f96f943SMatthew G. Knepley 
61091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61107f96f943SMatthew G. Knepley @*/
6111d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6112d71ae5a4SJacob Faibussowitsch {
61137f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61147f96f943SMatthew G. Knepley   void   **ectxs;
6115f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61167f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61177f96f943SMatthew G. Knepley 
61187f96f943SMatthew G. Knepley   PetscFunctionBegin;
6119f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6120f60fa741SMatthew G. Knepley   if (u) {
6121f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6122f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6123f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6124f60fa741SMatthew G. Knepley   }
6125f60fa741SMatthew G. Knepley   if (u_t) {
6126f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6127f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6128f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6129f60fa741SMatthew G. Knepley   }
61309566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61319566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
61329566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61337f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
61347f96f943SMatthew G. Knepley     PetscDS         ds;
61357f96f943SMatthew G. Knepley     DMLabel         label;
61367f96f943SMatthew G. Knepley     IS              fieldIS;
61377f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
61387f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
61397f96f943SMatthew G. Knepley 
614007218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
61419566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
61429566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
61439566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
61449566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6145f2cacb80SMatthew G. Knepley     if (u) {
6146f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6147f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6148f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
61497f96f943SMatthew G. Knepley     }
6150f2cacb80SMatthew G. Knepley     if (u_t) {
61519566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
61529566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6153f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6154f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6155f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6156f2cacb80SMatthew G. Knepley     }
61579566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6158f2cacb80SMatthew G. Knepley   }
6159f2cacb80SMatthew G. Knepley   if (u) {
61609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
61619566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6162f2cacb80SMatthew G. Knepley   }
6163f2cacb80SMatthew G. Knepley   if (u_t) {
61649566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
61659566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6166f2cacb80SMatthew G. Knepley   }
61679566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6168f60fa741SMatthew G. Knepley   if (u) {
6169f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6170f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6171f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6172f60fa741SMatthew G. Knepley   }
6173f60fa741SMatthew G. Knepley   if (u_t) {
6174f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6175f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6176f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6177f60fa741SMatthew G. Knepley   }
61783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61797f96f943SMatthew G. Knepley }
61807f96f943SMatthew G. Knepley 
618107218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6182d71ae5a4SJacob Faibussowitsch {
618307218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
618445480ffeSMatthew G. Knepley 
618545480ffeSMatthew G. Knepley   PetscFunctionBegin;
61869566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
618707218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
618807218a29SMatthew G. Knepley   if (dsIn) {
618907218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
619007218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
619145480ffeSMatthew G. Knepley   }
619207218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
61939566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
619407218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
61953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
619645480ffeSMatthew G. Knepley }
619745480ffeSMatthew G. Knepley 
61987f96f943SMatthew G. Knepley /*@
6199bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6200e5e52638SMatthew G. Knepley 
620120f4b53cSBarry Smith   Collective
6202e5e52638SMatthew G. Knepley 
6203e5e52638SMatthew G. Knepley   Input Parameter:
6204bb7acecfSBarry Smith . dm - The `DM`
6205e5e52638SMatthew G. Knepley 
6206e5e52638SMatthew G. Knepley   Output Parameter:
6207bb7acecfSBarry Smith . newdm - The `DM`
6208e5e52638SMatthew G. Knepley 
6209e5e52638SMatthew G. Knepley   Level: advanced
6210e5e52638SMatthew G. Knepley 
62111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6212e5e52638SMatthew G. Knepley @*/
6213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6214d71ae5a4SJacob Faibussowitsch {
6215e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6216e5e52638SMatthew G. Knepley 
6217e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62183ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62199566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62209566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6221e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6222e5e52638SMatthew G. Knepley     DMLabel  label;
6223b3cf3223SMatthew G. Knepley     IS       fields;
622407218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6225783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6226e5e52638SMatthew G. Knepley 
622707218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6228b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
622907218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6230d5b43468SJose E. Roman     /* Complete new labels in the new DS */
623107218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
62329566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6233783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6234b8025e53SMatthew G. Knepley       PetscWeakForm wf;
623545480ffeSMatthew G. Knepley       DMLabel       label;
6236783e2ec8SMatthew G. Knepley       PetscInt      field;
6237783e2ec8SMatthew G. Knepley 
62389566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
62399566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6240783e2ec8SMatthew G. Knepley     }
6241e5e52638SMatthew G. Knepley   }
6242799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
62433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6244e5e52638SMatthew G. Knepley }
6245e5e52638SMatthew G. Knepley 
6246e5e52638SMatthew G. Knepley /*@
6247bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6248e5e52638SMatthew G. Knepley 
624920f4b53cSBarry Smith   Collective
6250e5e52638SMatthew G. Knepley 
6251e5e52638SMatthew G. Knepley   Input Parameter:
6252bb7acecfSBarry Smith . dm - The `DM`
6253e5e52638SMatthew G. Knepley 
6254e5e52638SMatthew G. Knepley   Output Parameter:
6255bb7acecfSBarry Smith . newdm - The `DM`
6256e5e52638SMatthew G. Knepley 
6257e5e52638SMatthew G. Knepley   Level: advanced
6258e5e52638SMatthew G. Knepley 
625960225df5SJacob Faibussowitsch   Developer Notes:
6260bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6261bb7acecfSBarry Smith 
62621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6263e5e52638SMatthew G. Knepley @*/
6264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6265d71ae5a4SJacob Faibussowitsch {
6266e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62679566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62689566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
62693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6270e5e52638SMatthew G. Knepley }
6271e5e52638SMatthew G. Knepley 
6272c73cfb54SMatthew G. Knepley /*@
6273bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6274c73cfb54SMatthew G. Knepley 
627520f4b53cSBarry Smith   Not Collective
6276c73cfb54SMatthew G. Knepley 
6277c73cfb54SMatthew G. Knepley   Input Parameter:
6278bb7acecfSBarry Smith . dm - The `DM`
6279c73cfb54SMatthew G. Knepley 
6280c73cfb54SMatthew G. Knepley   Output Parameter:
6281c73cfb54SMatthew G. Knepley . dim - The topological dimension
6282c73cfb54SMatthew G. Knepley 
6283c73cfb54SMatthew G. Knepley   Level: beginner
6284c73cfb54SMatthew G. Knepley 
62851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6286c73cfb54SMatthew G. Knepley @*/
6287d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6288d71ae5a4SJacob Faibussowitsch {
6289c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6290c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62914f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6292c73cfb54SMatthew G. Knepley   *dim = dm->dim;
62933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6294c73cfb54SMatthew G. Knepley }
6295c73cfb54SMatthew G. Knepley 
6296c73cfb54SMatthew G. Knepley /*@
6297bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6298c73cfb54SMatthew G. Knepley 
629920f4b53cSBarry Smith   Collective
6300c73cfb54SMatthew G. Knepley 
6301c73cfb54SMatthew G. Knepley   Input Parameters:
6302bb7acecfSBarry Smith + dm  - The `DM`
6303c73cfb54SMatthew G. Knepley - dim - The topological dimension
6304c73cfb54SMatthew G. Knepley 
6305c73cfb54SMatthew G. Knepley   Level: beginner
6306c73cfb54SMatthew G. Knepley 
63071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6308c73cfb54SMatthew G. Knepley @*/
6309d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6310d71ae5a4SJacob Faibussowitsch {
6311e5e52638SMatthew G. Knepley   PetscDS  ds;
631245480ffeSMatthew G. Knepley   PetscInt Nds, n;
6313f17e8794SMatthew G. Knepley 
6314c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6315c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6316c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6317c73cfb54SMatthew G. Knepley   dm->dim = dim;
6318d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63199566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
632045480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
632107218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63229566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
632345480ffeSMatthew G. Knepley     }
6324d17bd122SMatthew G. Knepley   }
63253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6326c73cfb54SMatthew G. Knepley }
6327c73cfb54SMatthew G. Knepley 
6328793f3fe5SMatthew G. Knepley /*@
6329793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6330793f3fe5SMatthew G. Knepley 
633120f4b53cSBarry Smith   Collective
6332793f3fe5SMatthew G. Knepley 
6333793f3fe5SMatthew G. Knepley   Input Parameters:
6334bb7acecfSBarry Smith + dm  - the `DM`
6335793f3fe5SMatthew G. Knepley - dim - the dimension
6336793f3fe5SMatthew G. Knepley 
6337793f3fe5SMatthew G. Knepley   Output Parameters:
6338793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6339aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6340793f3fe5SMatthew G. Knepley 
634120f4b53cSBarry Smith   Level: intermediate
634220f4b53cSBarry Smith 
6343793f3fe5SMatthew G. Knepley   Note:
6344793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6345a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6346793f3fe5SMatthew G. Knepley   then the interval is empty.
6347793f3fe5SMatthew G. Knepley 
63481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6349793f3fe5SMatthew G. Knepley @*/
6350d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6351d71ae5a4SJacob Faibussowitsch {
6352793f3fe5SMatthew G. Knepley   PetscInt d;
6353793f3fe5SMatthew G. Knepley 
6354793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6355793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63569566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
63577a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6358dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
63593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6360793f3fe5SMatthew G. Knepley }
6361793f3fe5SMatthew G. Knepley 
63626636e97aSMatthew G Knepley /*@
6363bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6364f4d763aaSMatthew G. Knepley 
636520f4b53cSBarry Smith   Collective
63668f700142SStefano Zampini 
6367f4d763aaSMatthew G. Knepley   Input Parameter:
6368bb7acecfSBarry Smith . dm - The original `DM`
6369f4d763aaSMatthew G. Knepley 
6370f4d763aaSMatthew G. Knepley   Output Parameter:
6371bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6372f4d763aaSMatthew G. Knepley 
6373f4d763aaSMatthew G. Knepley   Level: intermediate
6374f4d763aaSMatthew G. Knepley 
6375bb7acecfSBarry Smith   Note:
6376bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6377bb7acecfSBarry 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
6378bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6379bb7acecfSBarry Smith 
63801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6381f4d763aaSMatthew G. Knepley @*/
6382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6383d71ae5a4SJacob Faibussowitsch {
6384c26acbdeSMatthew G. Knepley   PetscSection section;
63852d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
638614f150ffSMatthew G. Knepley 
638714f150ffSMatthew G. Knepley   PetscFunctionBegin;
638814f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63894f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
63909566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63919566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6392712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63932d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6394c26acbdeSMatthew G. Knepley     *odm = dm;
63953ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6396c26acbdeSMatthew G. Knepley   }
639714f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6398c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
639914f150ffSMatthew G. Knepley     PetscSF      sf;
640014f150ffSMatthew G. Knepley 
64019566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64029566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64039566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64049566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64059566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64069566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
64079566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
64089566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64099566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
641014f150ffSMatthew G. Knepley   }
641114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
641314f150ffSMatthew G. Knepley }
6414f4d763aaSMatthew G. Knepley 
6415f4d763aaSMatthew G. Knepley /*@
6416cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6417f4d763aaSMatthew G. Knepley 
6418f4d763aaSMatthew G. Knepley   Input Parameter:
6419bb7acecfSBarry Smith . dm - The original `DM`
6420f4d763aaSMatthew G. Knepley 
6421cdb7a50dSMatthew G. Knepley   Output Parameters:
6422cdb7a50dSMatthew G. Knepley + num - The output sequence number
6423cdb7a50dSMatthew G. Knepley - val - The output sequence value
6424f4d763aaSMatthew G. Knepley 
6425f4d763aaSMatthew G. Knepley   Level: intermediate
6426f4d763aaSMatthew G. Knepley 
6427bb7acecfSBarry Smith   Note:
6428bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6429bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6430bb7acecfSBarry Smith 
643160225df5SJacob Faibussowitsch   Developer Notes:
6432bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6433bb7acecfSBarry Smith   not directly related to the `DM`.
6434f4d763aaSMatthew G. Knepley 
64351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6436f4d763aaSMatthew G. Knepley @*/
6437d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6438d71ae5a4SJacob Faibussowitsch {
6439f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6440f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64419371c9d4SSatish Balay   if (num) {
64424f572ea9SToby Isaac     PetscAssertPointer(num, 2);
64439371c9d4SSatish Balay     *num = dm->outputSequenceNum;
64449371c9d4SSatish Balay   }
64459371c9d4SSatish Balay   if (val) {
64464f572ea9SToby Isaac     PetscAssertPointer(val, 3);
64479371c9d4SSatish Balay     *val = dm->outputSequenceVal;
64489371c9d4SSatish Balay   }
64493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6450f4d763aaSMatthew G. Knepley }
6451f4d763aaSMatthew G. Knepley 
6452f4d763aaSMatthew G. Knepley /*@
6453cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6454f4d763aaSMatthew G. Knepley 
6455f4d763aaSMatthew G. Knepley   Input Parameters:
6456bb7acecfSBarry Smith + dm  - The original `DM`
6457cdb7a50dSMatthew G. Knepley . num - The output sequence number
6458cdb7a50dSMatthew G. Knepley - val - The output sequence value
6459f4d763aaSMatthew G. Knepley 
6460f4d763aaSMatthew G. Knepley   Level: intermediate
6461f4d763aaSMatthew G. Knepley 
6462bb7acecfSBarry Smith   Note:
6463bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6464bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6465f4d763aaSMatthew G. Knepley 
64661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6467f4d763aaSMatthew G. Knepley @*/
6468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6469d71ae5a4SJacob Faibussowitsch {
6470f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6471f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6472f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6473cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
64743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6475cdb7a50dSMatthew G. Knepley }
6476cdb7a50dSMatthew G. Knepley 
6477cdb7a50dSMatthew G. Knepley /*@C
6478bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6479cdb7a50dSMatthew G. Knepley 
6480cdb7a50dSMatthew G. Knepley   Input Parameters:
6481bb7acecfSBarry Smith + dm     - The original `DM`
6482a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6483cdb7a50dSMatthew G. Knepley . name   - The sequence name
6484cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6485cdb7a50dSMatthew G. Knepley 
6486cdb7a50dSMatthew G. Knepley   Output Parameter:
6487cdb7a50dSMatthew G. Knepley . val - The output sequence value
6488cdb7a50dSMatthew G. Knepley 
6489cdb7a50dSMatthew G. Knepley   Level: intermediate
6490cdb7a50dSMatthew G. Knepley 
6491bb7acecfSBarry Smith   Note:
6492bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6493bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6494bb7acecfSBarry Smith 
649560225df5SJacob Faibussowitsch   Developer Notes:
6496bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6497cdb7a50dSMatthew G. Knepley 
64981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6499cdb7a50dSMatthew G. Knepley @*/
6500d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6501d71ae5a4SJacob Faibussowitsch {
6502cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6503cdb7a50dSMatthew G. Knepley 
6504cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6505cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6506cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65074f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65089566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6509cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6510cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6511cdb7a50dSMatthew G. Knepley     PetscScalar value;
6512cdb7a50dSMatthew G. Knepley 
65139566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65144aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6515cdb7a50dSMatthew G. Knepley #endif
6516cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6518f4d763aaSMatthew G. Knepley }
65198e4ac7eaSMatthew G. Knepley 
65208e4ac7eaSMatthew G. Knepley /*@
6521bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65228e4ac7eaSMatthew G. Knepley 
652320f4b53cSBarry Smith   Not Collective
65248e4ac7eaSMatthew G. Knepley 
65258e4ac7eaSMatthew G. Knepley   Input Parameter:
6526bb7acecfSBarry Smith . dm - The `DM`
65278e4ac7eaSMatthew G. Knepley 
65288e4ac7eaSMatthew G. Knepley   Output Parameter:
6529bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65308e4ac7eaSMatthew G. Knepley 
65318e4ac7eaSMatthew G. Knepley   Level: beginner
65328e4ac7eaSMatthew G. Knepley 
65331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
65348e4ac7eaSMatthew G. Knepley @*/
6535d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6536d71ae5a4SJacob Faibussowitsch {
65378e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65388e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65394f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
65408e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65428e4ac7eaSMatthew G. Knepley }
65438e4ac7eaSMatthew G. Knepley 
65448e4ac7eaSMatthew G. Knepley /*@
6545bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65468e4ac7eaSMatthew G. Knepley 
654720f4b53cSBarry Smith   Collective
65488e4ac7eaSMatthew G. Knepley 
65498e4ac7eaSMatthew G. Knepley   Input Parameters:
6550bb7acecfSBarry Smith + dm         - The `DM`
6551bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
65528e4ac7eaSMatthew G. Knepley 
6553bb7acecfSBarry Smith   Note:
6554bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
65555d3b26e6SMatthew G. Knepley 
65568e4ac7eaSMatthew G. Knepley   Level: beginner
65578e4ac7eaSMatthew G. Knepley 
65581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
65598e4ac7eaSMatthew G. Knepley @*/
6560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6561d71ae5a4SJacob Faibussowitsch {
65628e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65638e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65648833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65658e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65678e4ac7eaSMatthew G. Knepley }
6568c58f1c22SToby Isaac 
6569c58f1c22SToby Isaac /*@C
6570bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6571c58f1c22SToby Isaac 
6572c58f1c22SToby Isaac   Not Collective
6573c58f1c22SToby Isaac 
6574c58f1c22SToby Isaac   Input Parameters:
6575bb7acecfSBarry Smith + dm   - The `DM` object
6576c58f1c22SToby Isaac - name - The label name
6577c58f1c22SToby Isaac 
6578c58f1c22SToby Isaac   Level: intermediate
6579c58f1c22SToby Isaac 
65801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6581c58f1c22SToby Isaac @*/
6582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6583d71ae5a4SJacob Faibussowitsch {
65845d80c0bfSVaclav Hapla   PetscBool flg;
65855d80c0bfSVaclav Hapla   DMLabel   label;
6586c58f1c22SToby Isaac 
6587c58f1c22SToby Isaac   PetscFunctionBegin;
6588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65894f572ea9SToby Isaac   PetscAssertPointer(name, 2);
65909566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6591c58f1c22SToby Isaac   if (!flg) {
65929566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65939566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65949566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6595c58f1c22SToby Isaac   }
65963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6597c58f1c22SToby Isaac }
6598c58f1c22SToby Isaac 
6599c58f1c22SToby Isaac /*@C
6600bb7acecfSBarry 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.
66010fdc7489SMatthew Knepley 
66020fdc7489SMatthew Knepley   Not Collective
66030fdc7489SMatthew Knepley 
66040fdc7489SMatthew Knepley   Input Parameters:
6605bb7acecfSBarry Smith + dm   - The `DM` object
66060fdc7489SMatthew Knepley . l    - The index for the label
66070fdc7489SMatthew Knepley - name - The label name
66080fdc7489SMatthew Knepley 
66090fdc7489SMatthew Knepley   Level: intermediate
66100fdc7489SMatthew Knepley 
66111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66120fdc7489SMatthew Knepley @*/
6613d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6614d71ae5a4SJacob Faibussowitsch {
66150fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66160fdc7489SMatthew Knepley   DMLabel     label;
66170fdc7489SMatthew Knepley   PetscInt    Nl, m;
66180fdc7489SMatthew Knepley   PetscBool   flg, match;
66190fdc7489SMatthew Knepley   const char *lname;
66200fdc7489SMatthew Knepley 
66210fdc7489SMatthew Knepley   PetscFunctionBegin;
66220fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66234f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66249566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
66250fdc7489SMatthew Knepley   if (!flg) {
66269566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66279566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66289566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
66290fdc7489SMatthew Knepley   }
66309566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
663163a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
66320fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
66339566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
66349566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
66350fdc7489SMatthew Knepley     if (match) break;
66360fdc7489SMatthew Knepley   }
66373ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
66380fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
66390fdc7489SMatthew Knepley   else prev->next = orig->next;
66400fdc7489SMatthew Knepley   if (!l) {
66410fdc7489SMatthew Knepley     orig->next = dm->labels;
66420fdc7489SMatthew Knepley     dm->labels = orig;
66430fdc7489SMatthew Knepley   } else {
66449371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
66459371c9d4SSatish Balay       ;
66460fdc7489SMatthew Knepley     orig->next = prev->next;
66470fdc7489SMatthew Knepley     prev->next = orig;
66480fdc7489SMatthew Knepley   }
66493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66500fdc7489SMatthew Knepley }
66510fdc7489SMatthew Knepley 
66520fdc7489SMatthew Knepley /*@C
6653bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac   Not Collective
6656c58f1c22SToby Isaac 
6657c58f1c22SToby Isaac   Input Parameters:
6658bb7acecfSBarry Smith + dm    - The `DM` object
6659c58f1c22SToby Isaac . name  - The label name
6660c58f1c22SToby Isaac - point - The mesh point
6661c58f1c22SToby Isaac 
6662c58f1c22SToby Isaac   Output Parameter:
6663c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6664c58f1c22SToby Isaac 
6665c58f1c22SToby Isaac   Level: beginner
6666c58f1c22SToby Isaac 
66671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6668c58f1c22SToby Isaac @*/
6669d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6670d71ae5a4SJacob Faibussowitsch {
6671c58f1c22SToby Isaac   DMLabel label;
6672c58f1c22SToby Isaac 
6673c58f1c22SToby Isaac   PetscFunctionBegin;
6674c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66769566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66777a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66789566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
66793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6680c58f1c22SToby Isaac }
6681c58f1c22SToby Isaac 
6682c58f1c22SToby Isaac /*@C
6683bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Not Collective
6686c58f1c22SToby Isaac 
6687c58f1c22SToby Isaac   Input Parameters:
6688bb7acecfSBarry Smith + dm    - The `DM` object
6689c58f1c22SToby Isaac . name  - The label name
6690c58f1c22SToby Isaac . point - The mesh point
6691c58f1c22SToby Isaac - value - The label value for this point
6692c58f1c22SToby Isaac 
6693c58f1c22SToby Isaac   Output Parameter:
6694c58f1c22SToby Isaac 
6695c58f1c22SToby Isaac   Level: beginner
6696c58f1c22SToby Isaac 
66971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6698c58f1c22SToby Isaac @*/
6699d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6700d71ae5a4SJacob Faibussowitsch {
6701c58f1c22SToby Isaac   DMLabel label;
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac   PetscFunctionBegin;
6704c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67054f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67069566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6707c58f1c22SToby Isaac   if (!label) {
67089566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67099566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6710c58f1c22SToby Isaac   }
67119566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6713c58f1c22SToby Isaac }
6714c58f1c22SToby Isaac 
6715c58f1c22SToby Isaac /*@C
6716bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6717c58f1c22SToby Isaac 
6718c58f1c22SToby Isaac   Not Collective
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac   Input Parameters:
6721bb7acecfSBarry Smith + dm    - The `DM` object
6722c58f1c22SToby Isaac . name  - The label name
6723c58f1c22SToby Isaac . point - The mesh point
6724c58f1c22SToby Isaac - value - The label value for this point
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac   Level: beginner
6727c58f1c22SToby Isaac 
67281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6729c58f1c22SToby Isaac @*/
6730d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6731d71ae5a4SJacob Faibussowitsch {
6732c58f1c22SToby Isaac   DMLabel label;
6733c58f1c22SToby Isaac 
6734c58f1c22SToby Isaac   PetscFunctionBegin;
6735c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67364f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67379566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67383ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67399566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
67403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6741c58f1c22SToby Isaac }
6742c58f1c22SToby Isaac 
6743c58f1c22SToby Isaac /*@C
6744bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac   Not Collective
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Input Parameters:
6749bb7acecfSBarry Smith + dm   - The `DM` object
6750c58f1c22SToby Isaac - name - The label name
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac   Output Parameter:
6753c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6754c58f1c22SToby Isaac 
6755c58f1c22SToby Isaac   Level: beginner
6756c58f1c22SToby Isaac 
675760225df5SJacob Faibussowitsch   Developer Notes:
6758bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6759bb7acecfSBarry Smith 
67601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6761c58f1c22SToby Isaac @*/
6762d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6763d71ae5a4SJacob Faibussowitsch {
6764c58f1c22SToby Isaac   DMLabel label;
6765c58f1c22SToby Isaac 
6766c58f1c22SToby Isaac   PetscFunctionBegin;
6767c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67684f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67694f572ea9SToby Isaac   PetscAssertPointer(size, 3);
67709566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6771c58f1c22SToby Isaac   *size = 0;
67723ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67739566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
67743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6775c58f1c22SToby Isaac }
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac /*@C
6778bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Not Collective
6781c58f1c22SToby Isaac 
6782c58f1c22SToby Isaac   Input Parameters:
678360225df5SJacob Faibussowitsch + dm   - The `DM` object
6784c58f1c22SToby Isaac - name - The label name
6785c58f1c22SToby Isaac 
6786c58f1c22SToby Isaac   Output Parameter:
678720f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Level: beginner
6790c58f1c22SToby Isaac 
67911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6792c58f1c22SToby Isaac @*/
6793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6794d71ae5a4SJacob Faibussowitsch {
6795c58f1c22SToby Isaac   DMLabel label;
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   PetscFunctionBegin;
6798c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67994f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68004f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68019566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6802c58f1c22SToby Isaac   *ids = NULL;
6803dab2e251SBlaise Bourdin   if (label) {
68049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6805dab2e251SBlaise Bourdin   } else {
6806dab2e251SBlaise Bourdin     /* returning an empty IS */
68079566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6808dab2e251SBlaise Bourdin   }
68093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6810c58f1c22SToby Isaac }
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac /*@C
6813c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   Not Collective
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   Input Parameters:
6818bb7acecfSBarry Smith + dm    - The `DM` object
6819c58f1c22SToby Isaac . name  - The label name
6820c58f1c22SToby Isaac - value - The stratum value
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac   Output Parameter:
6823bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac   Level: beginner
6826c58f1c22SToby Isaac 
68271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6828c58f1c22SToby Isaac @*/
6829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6830d71ae5a4SJacob Faibussowitsch {
6831c58f1c22SToby Isaac   DMLabel label;
6832c58f1c22SToby Isaac 
6833c58f1c22SToby Isaac   PetscFunctionBegin;
6834c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68354f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68364f572ea9SToby Isaac   PetscAssertPointer(size, 4);
68379566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6838c58f1c22SToby Isaac   *size = 0;
68393ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68409566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
68413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6842c58f1c22SToby Isaac }
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac /*@C
6845c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6846c58f1c22SToby Isaac 
6847c58f1c22SToby Isaac   Not Collective
6848c58f1c22SToby Isaac 
6849c58f1c22SToby Isaac   Input Parameters:
6850bb7acecfSBarry Smith + dm    - The `DM` object
6851c58f1c22SToby Isaac . name  - The label name
6852c58f1c22SToby Isaac - value - The stratum value
6853c58f1c22SToby Isaac 
6854c58f1c22SToby Isaac   Output Parameter:
685520f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   Level: beginner
6858c58f1c22SToby Isaac 
68591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6860c58f1c22SToby Isaac @*/
6861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6862d71ae5a4SJacob Faibussowitsch {
6863c58f1c22SToby Isaac   DMLabel label;
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   PetscFunctionBegin;
6866c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68674f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68684f572ea9SToby Isaac   PetscAssertPointer(points, 4);
68699566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6870c58f1c22SToby Isaac   *points = NULL;
68713ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68729566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
68733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6874c58f1c22SToby Isaac }
6875c58f1c22SToby Isaac 
68764de306b1SToby Isaac /*@C
68779044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68784de306b1SToby Isaac 
68794de306b1SToby Isaac   Not Collective
68804de306b1SToby Isaac 
68814de306b1SToby Isaac   Input Parameters:
6882bb7acecfSBarry Smith + dm     - The `DM` object
68834de306b1SToby Isaac . name   - The label name
68844de306b1SToby Isaac . value  - The stratum value
68854de306b1SToby Isaac - points - The stratum points
68864de306b1SToby Isaac 
68874de306b1SToby Isaac   Level: beginner
68884de306b1SToby Isaac 
68891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68904de306b1SToby Isaac @*/
6891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6892d71ae5a4SJacob Faibussowitsch {
68934de306b1SToby Isaac   DMLabel label;
68944de306b1SToby Isaac 
68954de306b1SToby Isaac   PetscFunctionBegin;
68964de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68974f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6898292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
68999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69003ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69019566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69034de306b1SToby Isaac }
69044de306b1SToby Isaac 
6905c58f1c22SToby Isaac /*@C
6906bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Not Collective
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Input Parameters:
6911bb7acecfSBarry Smith + dm    - The `DM` object
6912c58f1c22SToby Isaac . name  - The label name
6913c58f1c22SToby Isaac - value - The label value for this point
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   Output Parameter:
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac   Level: beginner
6918c58f1c22SToby Isaac 
69191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6920c58f1c22SToby Isaac @*/
6921d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6922d71ae5a4SJacob Faibussowitsch {
6923c58f1c22SToby Isaac   DMLabel label;
6924c58f1c22SToby Isaac 
6925c58f1c22SToby Isaac   PetscFunctionBegin;
6926c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69274f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69289566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69293ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69309566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
69313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6932c58f1c22SToby Isaac }
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac /*@
6935bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac   Not Collective
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   Input Parameter:
6940bb7acecfSBarry Smith . dm - The `DM` object
6941c58f1c22SToby Isaac 
6942c58f1c22SToby Isaac   Output Parameter:
6943c58f1c22SToby Isaac . numLabels - the number of Labels
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac   Level: intermediate
6946c58f1c22SToby Isaac 
69471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6948c58f1c22SToby Isaac @*/
6949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6950d71ae5a4SJacob Faibussowitsch {
69515d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6952c58f1c22SToby Isaac   PetscInt    n    = 0;
6953c58f1c22SToby Isaac 
6954c58f1c22SToby Isaac   PetscFunctionBegin;
6955c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69564f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
69579371c9d4SSatish Balay   while (next) {
69589371c9d4SSatish Balay     ++n;
69599371c9d4SSatish Balay     next = next->next;
69609371c9d4SSatish Balay   }
6961c58f1c22SToby Isaac   *numLabels = n;
69623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6963c58f1c22SToby Isaac }
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac /*@C
6966c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6967c58f1c22SToby Isaac 
6968c58f1c22SToby Isaac   Not Collective
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Input Parameters:
6971bb7acecfSBarry Smith + dm - The `DM` object
6972c58f1c22SToby Isaac - n  - the label number
6973c58f1c22SToby Isaac 
6974c58f1c22SToby Isaac   Output Parameter:
6975c58f1c22SToby Isaac . name - the label name
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   Level: intermediate
6978c58f1c22SToby Isaac 
697960225df5SJacob Faibussowitsch   Developer Notes:
6980bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6981bb7acecfSBarry Smith 
69821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6983c58f1c22SToby Isaac @*/
6984d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6985d71ae5a4SJacob Faibussowitsch {
69865d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6987c58f1c22SToby Isaac   PetscInt    l    = 0;
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   PetscFunctionBegin;
6990c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69914f572ea9SToby Isaac   PetscAssertPointer(name, 3);
6992c58f1c22SToby Isaac   while (next) {
6993c58f1c22SToby Isaac     if (l == n) {
69949566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
69953ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6996c58f1c22SToby Isaac     }
6997c58f1c22SToby Isaac     ++l;
6998c58f1c22SToby Isaac     next = next->next;
6999c58f1c22SToby Isaac   }
700063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7001c58f1c22SToby Isaac }
7002c58f1c22SToby Isaac 
7003c58f1c22SToby Isaac /*@C
7004bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Not Collective
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   Input Parameters:
7009bb7acecfSBarry Smith + dm   - The `DM` object
7010c58f1c22SToby Isaac - name - The label name
7011c58f1c22SToby Isaac 
7012c58f1c22SToby Isaac   Output Parameter:
7013bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7014c58f1c22SToby Isaac 
7015c58f1c22SToby Isaac   Level: intermediate
7016c58f1c22SToby Isaac 
70171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7018c58f1c22SToby Isaac @*/
7019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7020d71ae5a4SJacob Faibussowitsch {
70215d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7022d67d17b1SMatthew G. Knepley   const char *lname;
7023c58f1c22SToby Isaac 
7024c58f1c22SToby Isaac   PetscFunctionBegin;
7025c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70264f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70274f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7028c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7029c58f1c22SToby Isaac   while (next) {
70309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70319566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7032c58f1c22SToby Isaac     if (*hasLabel) break;
7033c58f1c22SToby Isaac     next = next->next;
7034c58f1c22SToby Isaac   }
70353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7036c58f1c22SToby Isaac }
7037c58f1c22SToby Isaac 
7038a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7039c58f1c22SToby Isaac /*@C
704020f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   Not Collective
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Input Parameters:
7045bb7acecfSBarry Smith + dm   - The `DM` object
7046c58f1c22SToby Isaac - name - The label name
7047c58f1c22SToby Isaac 
7048c58f1c22SToby Isaac   Output Parameter:
704920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7050c58f1c22SToby Isaac 
7051bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7052bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7053bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7054bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7055bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7056bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7057bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70586d7c9049SMatthew G. Knepley 
7059c58f1c22SToby Isaac   Level: intermediate
7060c58f1c22SToby Isaac 
706160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7062c58f1c22SToby Isaac @*/
7063d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7064d71ae5a4SJacob Faibussowitsch {
70655d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7066c58f1c22SToby Isaac   PetscBool   hasLabel;
7067d67d17b1SMatthew G. Knepley   const char *lname;
7068c58f1c22SToby Isaac 
7069c58f1c22SToby Isaac   PetscFunctionBegin;
7070c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70714f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70724f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7073c58f1c22SToby Isaac   *label = NULL;
7074c58f1c22SToby Isaac   while (next) {
70759566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70769566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7077c58f1c22SToby Isaac     if (hasLabel) {
7078c58f1c22SToby Isaac       *label = next->label;
7079c58f1c22SToby Isaac       break;
7080c58f1c22SToby Isaac     }
7081c58f1c22SToby Isaac     next = next->next;
7082c58f1c22SToby Isaac   }
70833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7084c58f1c22SToby Isaac }
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac /*@C
7087bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Not Collective
7090c58f1c22SToby Isaac 
7091c58f1c22SToby Isaac   Input Parameters:
7092bb7acecfSBarry Smith + dm - The `DM` object
7093c58f1c22SToby Isaac - n  - the label number
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac   Output Parameter:
7096c58f1c22SToby Isaac . label - the label
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   Level: intermediate
7099c58f1c22SToby Isaac 
71001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7101c58f1c22SToby Isaac @*/
7102d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7103d71ae5a4SJacob Faibussowitsch {
71045d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7105c58f1c22SToby Isaac   PetscInt    l    = 0;
7106c58f1c22SToby Isaac 
7107c58f1c22SToby Isaac   PetscFunctionBegin;
7108c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71094f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7110c58f1c22SToby Isaac   while (next) {
7111c58f1c22SToby Isaac     if (l == n) {
7112c58f1c22SToby Isaac       *label = next->label;
71133ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7114c58f1c22SToby Isaac     }
7115c58f1c22SToby Isaac     ++l;
7116c58f1c22SToby Isaac     next = next->next;
7117c58f1c22SToby Isaac   }
711863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7119c58f1c22SToby Isaac }
7120c58f1c22SToby Isaac 
7121c58f1c22SToby Isaac /*@C
7122bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac   Not Collective
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac   Input Parameters:
7127bb7acecfSBarry Smith + dm    - The `DM` object
7128bb7acecfSBarry Smith - label - The `DMLabel`
7129c58f1c22SToby Isaac 
7130c58f1c22SToby Isaac   Level: developer
7131c58f1c22SToby Isaac 
713260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7133c58f1c22SToby Isaac @*/
7134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7135d71ae5a4SJacob Faibussowitsch {
71365d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7137c58f1c22SToby Isaac   PetscBool   hasLabel;
7138d67d17b1SMatthew G. Knepley   const char *lname;
71395d80c0bfSVaclav Hapla   PetscBool   flg;
7140c58f1c22SToby Isaac 
7141c58f1c22SToby Isaac   PetscFunctionBegin;
7142c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71439566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
71449566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
71457a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
71469566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7147c58f1c22SToby Isaac   tmpLabel->label  = label;
7148c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71495d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
71505d80c0bfSVaclav Hapla   *p = tmpLabel;
71519566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
71529566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
71535d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
71549566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7155ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
71563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7157c58f1c22SToby Isaac }
7158c58f1c22SToby Isaac 
7159a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7160c58f1c22SToby Isaac /*@C
71614a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
71624a7ee7d0SMatthew G. Knepley 
71634a7ee7d0SMatthew G. Knepley   Not Collective
71644a7ee7d0SMatthew G. Knepley 
71654a7ee7d0SMatthew G. Knepley   Input Parameters:
7166bb7acecfSBarry Smith + dm    - The `DM` object
7167bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71684a7ee7d0SMatthew G. Knepley 
7169bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7170bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7171bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7172bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7173bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7174bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7175bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71764a7ee7d0SMatthew G. Knepley 
71774a7ee7d0SMatthew G. Knepley   Level: intermediate
71784a7ee7d0SMatthew G. Knepley 
71791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71804a7ee7d0SMatthew G. Knepley @*/
7181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7182d71ae5a4SJacob Faibussowitsch {
71834a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71844a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71854a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71864a7ee7d0SMatthew G. Knepley 
71874a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71884a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71894a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71914a7ee7d0SMatthew G. Knepley   while (next) {
71929566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71939566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71944a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71959566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71969566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71974a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71989566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71994a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72009566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72014a7ee7d0SMatthew G. Knepley       next->label = label;
72024a7ee7d0SMatthew G. Knepley       break;
72034a7ee7d0SMatthew G. Knepley     }
72044a7ee7d0SMatthew G. Knepley     next = next->next;
72054a7ee7d0SMatthew G. Knepley   }
72063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72074a7ee7d0SMatthew G. Knepley }
72084a7ee7d0SMatthew G. Knepley 
72094a7ee7d0SMatthew G. Knepley /*@C
7210bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7211c58f1c22SToby Isaac 
7212c58f1c22SToby Isaac   Not Collective
7213c58f1c22SToby Isaac 
7214c58f1c22SToby Isaac   Input Parameters:
7215bb7acecfSBarry Smith + dm   - The `DM` object
7216c58f1c22SToby Isaac - name - The label name
7217c58f1c22SToby Isaac 
7218c58f1c22SToby Isaac   Output Parameter:
721920f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7220bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7221c58f1c22SToby Isaac 
7222c58f1c22SToby Isaac   Level: developer
7223c58f1c22SToby Isaac 
72241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7225c58f1c22SToby Isaac @*/
7226d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7227d71ae5a4SJacob Faibussowitsch {
722895d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7229c58f1c22SToby Isaac   PetscBool   hasLabel;
7230d67d17b1SMatthew G. Knepley   const char *lname;
7231c58f1c22SToby Isaac 
7232c58f1c22SToby Isaac   PetscFunctionBegin;
7233c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72344f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7235e5472504SVaclav Hapla   if (label) {
72364f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7237c58f1c22SToby Isaac     *label = NULL;
7238e5472504SVaclav Hapla   }
72395d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
72409566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
72419566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7242c58f1c22SToby Isaac     if (hasLabel) {
724395d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
72449566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
724595d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
72469566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7247ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
724895d578d6SVaclav Hapla       if (label) *label = link->label;
72499566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
72509566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7251c58f1c22SToby Isaac       break;
7252c58f1c22SToby Isaac     }
7253c58f1c22SToby Isaac   }
72543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7255c58f1c22SToby Isaac }
7256c58f1c22SToby Isaac 
7257306894acSVaclav Hapla /*@
7258bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7259306894acSVaclav Hapla 
7260306894acSVaclav Hapla   Not Collective
7261306894acSVaclav Hapla 
7262306894acSVaclav Hapla   Input Parameters:
7263bb7acecfSBarry Smith + dm           - The `DM` object
7264bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
726520f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7266306894acSVaclav Hapla 
7267306894acSVaclav Hapla   Level: developer
7268306894acSVaclav Hapla 
7269bb7acecfSBarry Smith   Note:
7270306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7271bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7272306894acSVaclav Hapla   *label nullified.
7273306894acSVaclav Hapla 
72741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7275306894acSVaclav Hapla @*/
7276d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7277d71ae5a4SJacob Faibussowitsch {
727843e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7279306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7280306894acSVaclav Hapla 
7281306894acSVaclav Hapla   PetscFunctionBegin;
7282306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72834f572ea9SToby Isaac   PetscAssertPointer(label, 2);
72843ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7285306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7286306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72875d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
728843e45a93SVaclav Hapla     if (*label == link->label) {
7289306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
729043e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7291306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7292ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
729343e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72949566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72959566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7296306894acSVaclav Hapla       break;
7297306894acSVaclav Hapla     }
7298306894acSVaclav Hapla   }
72997a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7301306894acSVaclav Hapla }
7302306894acSVaclav Hapla 
7303c58f1c22SToby Isaac /*@C
7304c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac   Not Collective
7307c58f1c22SToby Isaac 
7308c58f1c22SToby Isaac   Input Parameters:
7309bb7acecfSBarry Smith + dm   - The `DM` object
7310c58f1c22SToby Isaac - name - The label name
7311c58f1c22SToby Isaac 
7312c58f1c22SToby Isaac   Output Parameter:
7313c58f1c22SToby Isaac . output - The flag for output
7314c58f1c22SToby Isaac 
7315c58f1c22SToby Isaac   Level: developer
7316c58f1c22SToby Isaac 
73171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7318c58f1c22SToby Isaac @*/
7319d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7320d71ae5a4SJacob Faibussowitsch {
73215d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7322d67d17b1SMatthew G. Knepley   const char *lname;
7323c58f1c22SToby Isaac 
7324c58f1c22SToby Isaac   PetscFunctionBegin;
7325c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73264f572ea9SToby Isaac   PetscAssertPointer(name, 2);
73274f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7328c58f1c22SToby Isaac   while (next) {
7329c58f1c22SToby Isaac     PetscBool flg;
7330c58f1c22SToby Isaac 
73319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73339371c9d4SSatish Balay     if (flg) {
73349371c9d4SSatish Balay       *output = next->output;
73353ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73369371c9d4SSatish Balay     }
7337c58f1c22SToby Isaac     next = next->next;
7338c58f1c22SToby Isaac   }
733998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7340c58f1c22SToby Isaac }
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac /*@C
7343bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7344c58f1c22SToby Isaac 
7345c58f1c22SToby Isaac   Not Collective
7346c58f1c22SToby Isaac 
7347c58f1c22SToby Isaac   Input Parameters:
7348bb7acecfSBarry Smith + dm     - The `DM` object
7349c58f1c22SToby Isaac . name   - The label name
7350bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7351c58f1c22SToby Isaac 
7352c58f1c22SToby Isaac   Level: developer
7353c58f1c22SToby Isaac 
73541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7355c58f1c22SToby Isaac @*/
7356d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7357d71ae5a4SJacob Faibussowitsch {
73585d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7359d67d17b1SMatthew G. Knepley   const char *lname;
7360c58f1c22SToby Isaac 
7361c58f1c22SToby Isaac   PetscFunctionBegin;
7362c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73634f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7364c58f1c22SToby Isaac   while (next) {
7365c58f1c22SToby Isaac     PetscBool flg;
7366c58f1c22SToby Isaac 
73679566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73689566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73699371c9d4SSatish Balay     if (flg) {
73709371c9d4SSatish Balay       next->output = output;
73713ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
73729371c9d4SSatish Balay     }
7373c58f1c22SToby Isaac     next = next->next;
7374c58f1c22SToby Isaac   }
737598921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7376c58f1c22SToby Isaac }
7377c58f1c22SToby Isaac 
7378c58f1c22SToby Isaac /*@
7379bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7380c58f1c22SToby Isaac 
738120f4b53cSBarry Smith   Collective
7382c58f1c22SToby Isaac 
7383d8d19677SJose E. Roman   Input Parameters:
7384bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7385bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7386bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7387bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7388bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7389c58f1c22SToby Isaac 
7390c58f1c22SToby Isaac   Level: intermediate
7391c58f1c22SToby Isaac 
7392bb7acecfSBarry Smith   Note:
73932cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7394c58f1c22SToby Isaac 
73951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7396c58f1c22SToby Isaac @*/
7397d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7398d71ae5a4SJacob Faibussowitsch {
73992cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7400c58f1c22SToby Isaac   const char *name;
7401c58f1c22SToby Isaac   PetscBool   flg;
74025d80c0bfSVaclav Hapla   DMLabelLink link;
7403c58f1c22SToby Isaac 
74045d80c0bfSVaclav Hapla   PetscFunctionBegin;
74055d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74065d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74075d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74085d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74097a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74103ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74115d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74125d80c0bfSVaclav Hapla     label = link->label;
74139566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74145d80c0bfSVaclav Hapla     if (!all) {
74159566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7416c58f1c22SToby Isaac       if (flg) continue;
74179566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74187d5acc75SStefano Zampini       if (flg) continue;
74199566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7420ba2698f1SMatthew G. Knepley       if (flg) continue;
74215d80c0bfSVaclav Hapla     }
74229566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74232cbb9b06SVaclav Hapla     if (labelOld) {
74242cbb9b06SVaclav Hapla       switch (emode) {
7425d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7426d71ae5a4SJacob Faibussowitsch         continue;
7427d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7428d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7429d71ae5a4SJacob Faibussowitsch         break;
7430d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7431d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7432d71ae5a4SJacob Faibussowitsch       default:
7433d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
74342cbb9b06SVaclav Hapla       }
74352cbb9b06SVaclav Hapla     }
74365d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
74379566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
74385d80c0bfSVaclav Hapla     } else {
74395d80c0bfSVaclav Hapla       labelNew = label;
74405d80c0bfSVaclav Hapla     }
74419566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
74429566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7443c58f1c22SToby Isaac   }
74443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7445c58f1c22SToby Isaac }
7446461a15a0SLisandro Dalcin 
7447609dae6eSVaclav Hapla /*@C
7448b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7449609dae6eSVaclav Hapla 
745020f4b53cSBarry Smith   Collective; No Fortran Support
7451609dae6eSVaclav Hapla 
7452609dae6eSVaclav Hapla   Input Parameters:
7453bb7acecfSBarry Smith + dm0 - First `DM` object
7454bb7acecfSBarry Smith - dm1 - Second `DM` object
7455609dae6eSVaclav Hapla 
7456a4e35b19SJacob Faibussowitsch   Output Parameters:
74575efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
745820f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7459609dae6eSVaclav Hapla 
7460609dae6eSVaclav Hapla   Level: intermediate
7461609dae6eSVaclav Hapla 
7462609dae6eSVaclav Hapla   Notes:
7463bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7464bb7acecfSBarry Smith 
746520f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7466bb7acecfSBarry Smith 
746720f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7468609dae6eSVaclav Hapla 
74695efe38ccSVaclav Hapla   The output message is set independently on each rank.
7470bb7acecfSBarry Smith 
7471bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7472bb7acecfSBarry Smith 
747320f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7474bb7acecfSBarry Smith 
747520f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7476609dae6eSVaclav Hapla 
7477609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7478bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7479609dae6eSVaclav Hapla 
74801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7481609dae6eSVaclav Hapla @*/
7482d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7483d71ae5a4SJacob Faibussowitsch {
74845efe38ccSVaclav Hapla   PetscInt    n, i;
7485609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74865efe38ccSVaclav Hapla   PetscBool   eq;
7487609dae6eSVaclav Hapla   MPI_Comm    comm;
74885efe38ccSVaclav Hapla   PetscMPIInt rank;
7489609dae6eSVaclav Hapla 
7490609dae6eSVaclav Hapla   PetscFunctionBegin;
7491609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7492609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7493609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74944f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
74954f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
74969566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74985efe38ccSVaclav Hapla   {
74995efe38ccSVaclav Hapla     PetscInt n1;
75005efe38ccSVaclav Hapla 
75019566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75029566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75035efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
750448a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7505712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75065efe38ccSVaclav Hapla     if (!eq) goto finish;
75075efe38ccSVaclav Hapla   }
75085efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7509609dae6eSVaclav Hapla     DMLabel     l0, l1;
7510609dae6eSVaclav Hapla     const char *name;
7511609dae6eSVaclav Hapla     char       *msgInner;
7512609dae6eSVaclav Hapla 
7513609dae6eSVaclav Hapla     /* Ignore label order */
75149566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75169566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7517609dae6eSVaclav Hapla     if (!l1) {
751863a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75195efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75205efe38ccSVaclav Hapla       break;
7521609dae6eSVaclav Hapla     }
75229566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75239566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75249566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75255efe38ccSVaclav Hapla     if (!eq) break;
7526609dae6eSVaclav Hapla   }
7527712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7528609dae6eSVaclav Hapla finish:
75295efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7530609dae6eSVaclav Hapla   if (message) {
7531609dae6eSVaclav Hapla     *message = NULL;
753248a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
75335efe38ccSVaclav Hapla   } else {
753448a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
75359566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
75365efe38ccSVaclav Hapla   }
75375efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
75385efe38ccSVaclav Hapla   if (equal) *equal = eq;
75397a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
75403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7541609dae6eSVaclav Hapla }
7542609dae6eSVaclav Hapla 
7543d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7544d71ae5a4SJacob Faibussowitsch {
7545461a15a0SLisandro Dalcin   PetscFunctionBegin;
75464f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7547461a15a0SLisandro Dalcin   if (!*label) {
75489566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
75499566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7550461a15a0SLisandro Dalcin   }
75519566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
75523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7553461a15a0SLisandro Dalcin }
7554461a15a0SLisandro Dalcin 
75550fdc7489SMatthew Knepley /*
75560fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
75570fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
75580fdc7489SMatthew Knepley   (label, id) pair in the DM.
75590fdc7489SMatthew Knepley 
75600fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
75610fdc7489SMatthew Knepley   each label.
75620fdc7489SMatthew Knepley */
7563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7564d71ae5a4SJacob Faibussowitsch {
75650fdc7489SMatthew Knepley   DMUniversalLabel ul;
75660fdc7489SMatthew Knepley   PetscBool       *active;
75670fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75680fdc7489SMatthew Knepley 
75690fdc7489SMatthew Knepley   PetscFunctionBegin;
75709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75719566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75729566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75739566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75740fdc7489SMatthew Knepley   ul->Nl = 0;
75750fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75760fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75770fdc7489SMatthew Knepley     const char *name;
75780fdc7489SMatthew Knepley 
75799566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75809566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75819566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75820fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75830fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75840fdc7489SMatthew Knepley   }
75859566063dSJacob 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));
75860fdc7489SMatthew Knepley   ul->Nv = 0;
75870fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75880fdc7489SMatthew Knepley     DMLabel     label;
75890fdc7489SMatthew Knepley     PetscInt    nv;
75900fdc7489SMatthew Knepley     const char *name;
75910fdc7489SMatthew Knepley 
75920fdc7489SMatthew Knepley     if (!active[l]) continue;
75939566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75949566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75959566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75969566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75970fdc7489SMatthew Knepley     ul->indices[m] = l;
75980fdc7489SMatthew Knepley     ul->Nv += nv;
75990fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76000fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76010fdc7489SMatthew Knepley     ++m;
76020fdc7489SMatthew Knepley   }
76030fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76040fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76050fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76060fdc7489SMatthew Knepley   }
76070fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76080fdc7489SMatthew Knepley     PetscInt b;
76090fdc7489SMatthew Knepley 
76100fdc7489SMatthew Knepley     ul->masks[l] = 0;
76110fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76120fdc7489SMatthew Knepley   }
76139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76140fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76150fdc7489SMatthew Knepley     DMLabel         label;
76160fdc7489SMatthew Knepley     IS              valueIS;
76170fdc7489SMatthew Knepley     const PetscInt *varr;
76180fdc7489SMatthew Knepley     PetscInt        nv, v;
76190fdc7489SMatthew Knepley 
76200fdc7489SMatthew Knepley     if (!active[l]) continue;
76219566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76239566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76249566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7625ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
76269566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
76279566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
76289566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
76290fdc7489SMatthew Knepley     ++m;
76300fdc7489SMatthew Knepley   }
76319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
76320fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
76330fdc7489SMatthew Knepley     PetscInt  uval   = 0;
76340fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
76350fdc7489SMatthew Knepley 
76360fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
76370fdc7489SMatthew Knepley       DMLabel  label;
76380649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
76390fdc7489SMatthew Knepley 
76400fdc7489SMatthew Knepley       if (!active[l]) continue;
76419566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
76429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
76439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
76449371c9d4SSatish Balay       if (val == defval) {
76459371c9d4SSatish Balay         ++m;
76469371c9d4SSatish Balay         continue;
76479371c9d4SSatish Balay       }
76480649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
76490fdc7489SMatthew Knepley       marked = PETSC_TRUE;
76509566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
765163a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
76520fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
76530fdc7489SMatthew Knepley       ++m;
76540fdc7489SMatthew Knepley     }
76559566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
76560fdc7489SMatthew Knepley   }
76579566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
76580fdc7489SMatthew Knepley   *universal = ul;
76593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76600fdc7489SMatthew Knepley }
76610fdc7489SMatthew Knepley 
7662d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7663d71ae5a4SJacob Faibussowitsch {
76640fdc7489SMatthew Knepley   PetscInt l;
76650fdc7489SMatthew Knepley 
76660fdc7489SMatthew Knepley   PetscFunctionBegin;
76679566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76689566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76699566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76709566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76719566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76720fdc7489SMatthew Knepley   *universal = NULL;
76733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76740fdc7489SMatthew Knepley }
76750fdc7489SMatthew Knepley 
7676d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7677d71ae5a4SJacob Faibussowitsch {
76780fdc7489SMatthew Knepley   PetscFunctionBegin;
76794f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
76800fdc7489SMatthew Knepley   *ulabel = ul->label;
76813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76820fdc7489SMatthew Knepley }
76830fdc7489SMatthew Knepley 
7684d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7685d71ae5a4SJacob Faibussowitsch {
76860fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76870fdc7489SMatthew Knepley 
76880fdc7489SMatthew Knepley   PetscFunctionBegin;
7689064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76900fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76919566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76929566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76930fdc7489SMatthew Knepley   }
76940fdc7489SMatthew Knepley   if (preserveOrder) {
76950fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76960fdc7489SMatthew Knepley       const char *name;
76970fdc7489SMatthew Knepley       PetscBool   match;
76980fdc7489SMatthew Knepley 
76999566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77009566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
770163a3b9bcSJacob 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]);
77020fdc7489SMatthew Knepley     }
77030fdc7489SMatthew Knepley   }
77043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77050fdc7489SMatthew Knepley }
77060fdc7489SMatthew Knepley 
7707d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7708d71ae5a4SJacob Faibussowitsch {
77090fdc7489SMatthew Knepley   PetscInt l;
77100fdc7489SMatthew Knepley 
77110fdc7489SMatthew Knepley   PetscFunctionBegin;
77120fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77130fdc7489SMatthew Knepley     DMLabel  label;
77140fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77150fdc7489SMatthew Knepley 
77160fdc7489SMatthew Knepley     if (lval) {
77179566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77189566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77199566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77200fdc7489SMatthew Knepley     }
77210fdc7489SMatthew Knepley   }
77223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77230fdc7489SMatthew Knepley }
7724a8fb8f29SToby Isaac 
7725a8fb8f29SToby Isaac /*@
7726bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7727bb7acecfSBarry Smith 
772820f4b53cSBarry Smith   Not Collective
7729a8fb8f29SToby Isaac 
7730a8fb8f29SToby Isaac   Input Parameter:
7731bb7acecfSBarry Smith . dm - The `DM` object
7732a8fb8f29SToby Isaac 
7733a8fb8f29SToby Isaac   Output Parameter:
7734bb7acecfSBarry Smith . cdm - The coarse `DM`
7735a8fb8f29SToby Isaac 
7736a8fb8f29SToby Isaac   Level: intermediate
7737a8fb8f29SToby Isaac 
77381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7739a8fb8f29SToby Isaac @*/
7740d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7741d71ae5a4SJacob Faibussowitsch {
7742a8fb8f29SToby Isaac   PetscFunctionBegin;
7743a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77444f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7745a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
77463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7747a8fb8f29SToby Isaac }
7748a8fb8f29SToby Isaac 
7749a8fb8f29SToby Isaac /*@
7750bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7751a8fb8f29SToby Isaac 
7752a8fb8f29SToby Isaac   Input Parameters:
7753bb7acecfSBarry Smith + dm  - The `DM` object
7754bb7acecfSBarry Smith - cdm - The coarse `DM`
7755a8fb8f29SToby Isaac 
7756a8fb8f29SToby Isaac   Level: intermediate
7757a8fb8f29SToby Isaac 
7758bb7acecfSBarry Smith   Note:
7759bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7760bb7acecfSBarry Smith 
77611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7762a8fb8f29SToby Isaac @*/
7763d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7764d71ae5a4SJacob Faibussowitsch {
7765a8fb8f29SToby Isaac   PetscFunctionBegin;
7766a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7767a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
776889d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77709566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7771a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
77723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7773a8fb8f29SToby Isaac }
7774a8fb8f29SToby Isaac 
777588bdff64SToby Isaac /*@
7776bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
777788bdff64SToby Isaac 
777888bdff64SToby Isaac   Input Parameter:
7779bb7acecfSBarry Smith . dm - The `DM` object
778088bdff64SToby Isaac 
778188bdff64SToby Isaac   Output Parameter:
7782bb7acecfSBarry Smith . fdm - The fine `DM`
778388bdff64SToby Isaac 
778488bdff64SToby Isaac   Level: intermediate
778588bdff64SToby Isaac 
77861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
778788bdff64SToby Isaac @*/
7788d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7789d71ae5a4SJacob Faibussowitsch {
779088bdff64SToby Isaac   PetscFunctionBegin;
779188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77924f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
779388bdff64SToby Isaac   *fdm = dm->fineMesh;
77943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
779588bdff64SToby Isaac }
779688bdff64SToby Isaac 
779788bdff64SToby Isaac /*@
7798bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
779988bdff64SToby Isaac 
780088bdff64SToby Isaac   Input Parameters:
7801bb7acecfSBarry Smith + dm  - The `DM` object
7802bb7acecfSBarry Smith - fdm - The fine `DM`
780388bdff64SToby Isaac 
7804bb7acecfSBarry Smith   Level: developer
780588bdff64SToby Isaac 
7806bb7acecfSBarry Smith   Note:
7807bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7808bb7acecfSBarry Smith 
78091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
781088bdff64SToby Isaac @*/
7811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7812d71ae5a4SJacob Faibussowitsch {
781388bdff64SToby Isaac   PetscFunctionBegin;
781488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
781588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
781689d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78179566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78189566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
781988bdff64SToby Isaac   dm->fineMesh = fdm;
78203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
782188bdff64SToby Isaac }
782288bdff64SToby Isaac 
7823a6ba4734SToby Isaac /*@C
7824bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7825a6ba4734SToby Isaac 
782620f4b53cSBarry Smith   Collective
7827783e2ec8SMatthew G. Knepley 
7828a6ba4734SToby Isaac   Input Parameters:
7829bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7830bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7831a6ba4734SToby Isaac . name     - The BC name
783245480ffeSMatthew G. Knepley . label    - The label defining constrained points
7833bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
783445480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7835a6ba4734SToby Isaac . field    - The field to constrain
783645480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7837a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7838a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
783956cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7840a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7841a6ba4734SToby Isaac 
784245480ffeSMatthew G. Knepley   Output Parameter:
784345480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
784445480ffeSMatthew G. Knepley 
7845a6ba4734SToby Isaac   Options Database Keys:
7846a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7847a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7848a6ba4734SToby Isaac 
784920f4b53cSBarry Smith   Level: intermediate
785020f4b53cSBarry Smith 
7851bb7acecfSBarry Smith   Notes:
785237fdd005SBarry 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\:
785356cf3b9cSMatthew G. Knepley 
785420f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
785556cf3b9cSMatthew G. Knepley 
7856a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
785756cf3b9cSMatthew G. Knepley 
785820f4b53cSBarry Smith .vb
785920f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
786020f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
786120f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
786220f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
786320f4b53cSBarry Smith .ve
786456cf3b9cSMatthew G. Knepley + dim - the spatial dimension
786556cf3b9cSMatthew G. Knepley . Nf - the number of fields
786656cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
786756cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
786856cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
786956cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
787056cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
787156cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
787256cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
787356cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
787456cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
787556cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
787656cf3b9cSMatthew G. Knepley . t - current time
787756cf3b9cSMatthew G. Knepley . x - coordinates of the current point
787856cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
787956cf3b9cSMatthew G. Knepley . constants - constant parameters
788056cf3b9cSMatthew G. Knepley - bcval - output values at the current point
788156cf3b9cSMatthew G. Knepley 
78821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7883a6ba4734SToby Isaac @*/
7884d71ae5a4SJacob 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)
7885d71ae5a4SJacob Faibussowitsch {
7886e5e52638SMatthew G. Knepley   PetscDS ds;
7887a6ba4734SToby Isaac 
7888a6ba4734SToby Isaac   PetscFunctionBegin;
7889a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7890783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
789145480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
789245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
789345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
789445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
789501a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78969566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7897799db056SMatthew G. Knepley   /* Complete label */
7898799db056SMatthew G. Knepley   if (label) {
7899799db056SMatthew G. Knepley     PetscObject  obj;
7900799db056SMatthew G. Knepley     PetscClassId id;
7901799db056SMatthew G. Knepley 
7902799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7903799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7904799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7905799db056SMatthew G. Knepley       DM plex;
7906799db056SMatthew G. Knepley 
7907799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7908799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7909799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7910799db056SMatthew G. Knepley     }
7911799db056SMatthew G. Knepley   }
79129566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7914a6ba4734SToby Isaac }
7915a6ba4734SToby Isaac 
791645480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7917d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7918d71ae5a4SJacob Faibussowitsch {
7919e5e52638SMatthew G. Knepley   PetscDS     ds;
7920dff059c6SToby Isaac   DMBoundary *lastnext;
7921e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7922e6f8dbb6SToby Isaac 
7923e6f8dbb6SToby Isaac   PetscFunctionBegin;
79249566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7925e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
792647a1f5adSToby Isaac   if (dm->boundary) {
792747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
792847a1f5adSToby Isaac 
792947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
79303ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
793147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
793247a1f5adSToby Isaac     while (next) {
793347a1f5adSToby Isaac       DMBoundary b = next;
793447a1f5adSToby Isaac 
793547a1f5adSToby Isaac       next = b->next;
79369566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7937a6ba4734SToby Isaac     }
793847a1f5adSToby Isaac     dm->boundary = NULL;
7939a6ba4734SToby Isaac   }
794047a1f5adSToby Isaac 
7941dff059c6SToby Isaac   lastnext = &(dm->boundary);
7942e6f8dbb6SToby Isaac   while (dsbound) {
7943e6f8dbb6SToby Isaac     DMBoundary dmbound;
7944e6f8dbb6SToby Isaac 
79459566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7946e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
794745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
794847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7949dff059c6SToby Isaac     *lastnext = dmbound;
7950dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7951dff059c6SToby Isaac     dsbound   = dsbound->next;
7952a6ba4734SToby Isaac   }
79533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7954a6ba4734SToby Isaac }
7955a6ba4734SToby Isaac 
7956bb7acecfSBarry Smith /* TODO: missing manual page */
7957d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7958d71ae5a4SJacob Faibussowitsch {
7959b95f2879SToby Isaac   DMBoundary b;
7960a6ba4734SToby Isaac 
7961a6ba4734SToby Isaac   PetscFunctionBegin;
7962a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79634f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
7964a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
79659566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7966b95f2879SToby Isaac   b = dm->boundary;
7967a6ba4734SToby Isaac   while (b && !(*isBd)) {
7968e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7969e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7970a6ba4734SToby Isaac     PetscInt   i;
7971a6ba4734SToby Isaac 
797245480ffeSMatthew G. Knepley     if (label) {
79739566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7974a6ba4734SToby Isaac     }
7975a6ba4734SToby Isaac     b = b->next;
7976a6ba4734SToby Isaac   }
79773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7978a6ba4734SToby Isaac }
79794d6f44ffSToby Isaac 
79804d6f44ffSToby Isaac /*@C
7981bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7982a6e0b375SMatthew G. Knepley 
798320f4b53cSBarry Smith   Collective
79844d6f44ffSToby Isaac 
79854d6f44ffSToby Isaac   Input Parameters:
7986bb7acecfSBarry Smith + dm    - The `DM`
79870709b2feSToby Isaac . time  - The time
79884d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
79894d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79904d6f44ffSToby Isaac - mode  - The insertion mode for values
79914d6f44ffSToby Isaac 
79924d6f44ffSToby Isaac   Output Parameter:
79934d6f44ffSToby Isaac . X - vector
79944d6f44ffSToby Isaac 
799520f4b53cSBarry Smith   Calling sequence of `funcs`:
79964d6f44ffSToby Isaac + dim  - The spatial dimension
79978ec8862eSJed Brown . time - The time at which to sample
79984d6f44ffSToby Isaac . x    - The coordinates
799977b739a6SMatthew Knepley . Nc   - The number of components
80004d6f44ffSToby Isaac . u    - The output field values
80014d6f44ffSToby Isaac - ctx  - optional user-defined function context
80024d6f44ffSToby Isaac 
80034d6f44ffSToby Isaac   Level: developer
80044d6f44ffSToby Isaac 
8005bb7acecfSBarry Smith   Developer Notes:
8006bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8007bb7acecfSBarry Smith 
8008bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8009bb7acecfSBarry Smith 
80101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80114d6f44ffSToby Isaac @*/
8012a4e35b19SJacob 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)
8013d71ae5a4SJacob Faibussowitsch {
80144d6f44ffSToby Isaac   Vec localX;
80154d6f44ffSToby Isaac 
80164d6f44ffSToby Isaac   PetscFunctionBegin;
80174d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8018708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80199566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8020f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80219566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80229566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80239566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80249566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8025708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
80263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80274d6f44ffSToby Isaac }
80284d6f44ffSToby Isaac 
8029a6e0b375SMatthew G. Knepley /*@C
8030bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8031a6e0b375SMatthew G. Knepley 
803220f4b53cSBarry Smith   Not Collective
8033a6e0b375SMatthew G. Knepley 
8034a6e0b375SMatthew G. Knepley   Input Parameters:
8035bb7acecfSBarry Smith + dm    - The `DM`
8036a6e0b375SMatthew G. Knepley . time  - The time
8037a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8038a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8039a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8040a6e0b375SMatthew G. Knepley 
8041a6e0b375SMatthew G. Knepley   Output Parameter:
8042a6e0b375SMatthew G. Knepley . localX - vector
8043a6e0b375SMatthew G. Knepley 
804420f4b53cSBarry Smith   Calling sequence of `funcs`:
8045a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8046a4e35b19SJacob Faibussowitsch . time - The current timestep
8047a6e0b375SMatthew G. Knepley . x    - The coordinates
804877b739a6SMatthew Knepley . Nc   - The number of components
8049a6e0b375SMatthew G. Knepley . u    - The output field values
8050a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8051a6e0b375SMatthew G. Knepley 
8052a6e0b375SMatthew G. Knepley   Level: developer
8053a6e0b375SMatthew G. Knepley 
8054bb7acecfSBarry Smith   Developer Notes:
8055bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8056bb7acecfSBarry Smith 
8057bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8058bb7acecfSBarry Smith 
80591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8060a6e0b375SMatthew G. Knepley @*/
8061a4e35b19SJacob 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)
8062d71ae5a4SJacob Faibussowitsch {
80634d6f44ffSToby Isaac   PetscFunctionBegin;
80644d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8065064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
80669566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
80673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80684d6f44ffSToby Isaac }
80694d6f44ffSToby Isaac 
8070a6e0b375SMatthew G. Knepley /*@C
8071bb7acecfSBarry 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.
8072a6e0b375SMatthew G. Knepley 
807320f4b53cSBarry Smith   Collective
8074a6e0b375SMatthew G. Knepley 
8075a6e0b375SMatthew G. Knepley   Input Parameters:
8076bb7acecfSBarry Smith + dm     - The `DM`
8077a6e0b375SMatthew G. Knepley . time   - The time
8078a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8079a4e35b19SJacob Faibussowitsch . ids    - The ids
8080a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8081a4e35b19SJacob Faibussowitsch . comps  - The components
8082bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8083a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8084bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8085a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8086a6e0b375SMatthew G. Knepley 
8087a6e0b375SMatthew G. Knepley   Output Parameter:
8088a6e0b375SMatthew G. Knepley . X - vector
8089a6e0b375SMatthew G. Knepley 
809020f4b53cSBarry Smith   Calling sequence of `funcs`:
8091a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8092a4e35b19SJacob Faibussowitsch . time - The current timestep
8093a6e0b375SMatthew G. Knepley . x    - The coordinates
809477b739a6SMatthew Knepley . Nc   - The number of components
8095a6e0b375SMatthew G. Knepley . u    - The output field values
8096a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8097a6e0b375SMatthew G. Knepley 
8098a6e0b375SMatthew G. Knepley   Level: developer
8099a6e0b375SMatthew G. Knepley 
8100bb7acecfSBarry Smith   Developer Notes:
8101bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8102bb7acecfSBarry Smith 
8103bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8104bb7acecfSBarry Smith 
81051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8106a6e0b375SMatthew G. Knepley @*/
8107a4e35b19SJacob 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)
8108d71ae5a4SJacob Faibussowitsch {
81092c53366bSMatthew G. Knepley   Vec localX;
81102c53366bSMatthew G. Knepley 
81112c53366bSMatthew G. Knepley   PetscFunctionBegin;
81122c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81139566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8114f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81159566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81169566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81179566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81189566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81202c53366bSMatthew G. Knepley }
81212c53366bSMatthew G. Knepley 
8122a6e0b375SMatthew G. Knepley /*@C
8123bb7acecfSBarry 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.
8124a6e0b375SMatthew G. Knepley 
812520f4b53cSBarry Smith   Not Collective
8126a6e0b375SMatthew G. Knepley 
8127a6e0b375SMatthew G. Knepley   Input Parameters:
8128bb7acecfSBarry Smith + dm     - The `DM`
8129a6e0b375SMatthew G. Knepley . time   - The time
8130bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8131a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8132a4e35b19SJacob Faibussowitsch . ids    - The ids
8133a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8134a4e35b19SJacob Faibussowitsch . comps  - The components
8135a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8136a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8137a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8138a6e0b375SMatthew G. Knepley 
8139a6e0b375SMatthew G. Knepley   Output Parameter:
8140a6e0b375SMatthew G. Knepley . localX - vector
8141a6e0b375SMatthew G. Knepley 
814220f4b53cSBarry Smith   Calling sequence of `funcs`:
8143a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8144a4e35b19SJacob Faibussowitsch . time - The current time
8145a6e0b375SMatthew G. Knepley . x    - The coordinates
814677b739a6SMatthew Knepley . Nc   - The number of components
8147a6e0b375SMatthew G. Knepley . u    - The output field values
8148a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8149a6e0b375SMatthew G. Knepley 
8150a6e0b375SMatthew G. Knepley   Level: developer
8151a6e0b375SMatthew G. Knepley 
8152bb7acecfSBarry Smith   Developer Notes:
8153bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8154bb7acecfSBarry Smith 
8155bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8156bb7acecfSBarry Smith 
81571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8158a6e0b375SMatthew G. Knepley @*/
8159a4e35b19SJacob 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)
8160d71ae5a4SJacob Faibussowitsch {
81614d6f44ffSToby Isaac   PetscFunctionBegin;
81624d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8163064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81649566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81664d6f44ffSToby Isaac }
81672716604bSToby Isaac 
8168a6e0b375SMatthew G. Knepley /*@C
8169bb7acecfSBarry 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.
8170a6e0b375SMatthew G. Knepley 
817120f4b53cSBarry Smith   Not Collective
8172a6e0b375SMatthew G. Knepley 
8173a6e0b375SMatthew G. Knepley   Input Parameters:
8174bb7acecfSBarry Smith + dm     - The `DM`
8175a6e0b375SMatthew G. Knepley . time   - The time
817620f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8177a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8178a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8179a6e0b375SMatthew G. Knepley 
8180a6e0b375SMatthew G. Knepley   Output Parameter:
8181a6e0b375SMatthew G. Knepley . localX - The output vector
8182a6e0b375SMatthew G. Knepley 
818320f4b53cSBarry Smith   Calling sequence of `funcs`:
8184a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8185a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8186a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8187a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8188a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8189a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8190a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8191a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8192a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8193a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8194a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8195a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8196a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8197a6e0b375SMatthew G. Knepley . t            - The current time
8198a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8199a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8200a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8201a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8202a6e0b375SMatthew G. Knepley 
8203bb7acecfSBarry Smith   Note:
8204bb7acecfSBarry 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.
8205bb7acecfSBarry 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
8206bb7acecfSBarry 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
8207a6e0b375SMatthew 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.
8208a6e0b375SMatthew G. Knepley 
8209a6e0b375SMatthew G. Knepley   Level: intermediate
8210a6e0b375SMatthew G. Knepley 
8211bb7acecfSBarry Smith   Developer Notes:
8212bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8213bb7acecfSBarry Smith 
8214bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8215bb7acecfSBarry Smith 
8216a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8217a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8218a6e0b375SMatthew G. Knepley @*/
8219a4e35b19SJacob 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)
8220d71ae5a4SJacob Faibussowitsch {
82218c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82228c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8223eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82248c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
82259566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
82263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82278c6c5593SMatthew G. Knepley }
82288c6c5593SMatthew G. Knepley 
8229a6e0b375SMatthew G. Knepley /*@C
8230a6e0b375SMatthew 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.
8231a6e0b375SMatthew G. Knepley 
823220f4b53cSBarry Smith   Not Collective
8233a6e0b375SMatthew G. Knepley 
8234a6e0b375SMatthew G. Knepley   Input Parameters:
8235bb7acecfSBarry Smith + dm     - The `DM`
8236a6e0b375SMatthew G. Knepley . time   - The time
8237bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8238a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8239a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8240bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
824120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8242a6e0b375SMatthew G. Knepley . localU - The input field vector
8243a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8244a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8245a6e0b375SMatthew G. Knepley 
8246a6e0b375SMatthew G. Knepley   Output Parameter:
8247a6e0b375SMatthew G. Knepley . localX - The output vector
8248a6e0b375SMatthew G. Knepley 
824920f4b53cSBarry Smith   Calling sequence of `funcs`:
8250a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8251a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8252a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8253a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8254a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8255a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8256a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8257a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8258a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8259a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8260a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8261a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8262a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8263a6e0b375SMatthew G. Knepley . t            - The current time
8264a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8265a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8266a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8267a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8268a6e0b375SMatthew G. Knepley 
8269bb7acecfSBarry Smith   Note:
8270bb7acecfSBarry 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.
8271bb7acecfSBarry 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
8272bb7acecfSBarry 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
8273a6e0b375SMatthew 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.
8274a6e0b375SMatthew G. Knepley 
8275a6e0b375SMatthew G. Knepley   Level: intermediate
8276a6e0b375SMatthew G. Knepley 
8277bb7acecfSBarry Smith   Developer Notes:
8278bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8279bb7acecfSBarry Smith 
8280bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8281bb7acecfSBarry Smith 
82821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8283a6e0b375SMatthew G. Knepley @*/
8284a4e35b19SJacob 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)
8285d71ae5a4SJacob Faibussowitsch {
82868c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82878c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8288064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8289064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82909566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82928c6c5593SMatthew G. Knepley }
82938c6c5593SMatthew G. Knepley 
82942716604bSToby Isaac /*@C
8295d29d7c6eSMatthew 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.
8296d29d7c6eSMatthew G. Knepley 
829720f4b53cSBarry Smith   Not Collective
8298d29d7c6eSMatthew G. Knepley 
8299d29d7c6eSMatthew G. Knepley   Input Parameters:
8300bb7acecfSBarry Smith + dm     - The `DM`
8301d29d7c6eSMatthew G. Knepley . time   - The time
8302bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8303d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8304d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8305bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
830620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8307d29d7c6eSMatthew G. Knepley . U      - The input field vector
8308d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8309d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8310d29d7c6eSMatthew G. Knepley 
8311d29d7c6eSMatthew G. Knepley   Output Parameter:
8312d29d7c6eSMatthew G. Knepley . X - The output vector
8313d29d7c6eSMatthew G. Knepley 
831420f4b53cSBarry Smith   Calling sequence of `funcs`:
8315d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8316d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8317d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8318d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8319d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8320d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8321d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8322d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8323d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8324d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8325d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8326d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8327d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8328d29d7c6eSMatthew G. Knepley . t            - The current time
8329d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8330d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8331d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8332d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8333d29d7c6eSMatthew G. Knepley 
8334bb7acecfSBarry Smith   Note:
8335bb7acecfSBarry 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.
8336bb7acecfSBarry 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
8337bb7acecfSBarry 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
8338d29d7c6eSMatthew 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.
8339d29d7c6eSMatthew G. Knepley 
8340d29d7c6eSMatthew G. Knepley   Level: intermediate
8341d29d7c6eSMatthew G. Knepley 
8342bb7acecfSBarry Smith   Developer Notes:
8343bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8344bb7acecfSBarry Smith 
8345bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8346bb7acecfSBarry Smith 
83471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8348d29d7c6eSMatthew G. Knepley @*/
8349a4e35b19SJacob 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)
8350d71ae5a4SJacob Faibussowitsch {
8351d29d7c6eSMatthew G. Knepley   DM  dmIn;
8352d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8353d29d7c6eSMatthew G. Knepley 
8354d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8355d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8356d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8357d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8358d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8359f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
836072fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
836172fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8362d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8363d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8364d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8365d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8366d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
83673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8368d29d7c6eSMatthew G. Knepley }
8369d29d7c6eSMatthew G. Knepley 
8370d29d7c6eSMatthew G. Knepley /*@C
8371ece3a9fcSMatthew 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.
8372ece3a9fcSMatthew G. Knepley 
837320f4b53cSBarry Smith   Not Collective
8374ece3a9fcSMatthew G. Knepley 
8375ece3a9fcSMatthew G. Knepley   Input Parameters:
8376bb7acecfSBarry Smith + dm     - The `DM`
8377ece3a9fcSMatthew G. Knepley . time   - The time
8378bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8379ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8380ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8381bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
838220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8383ece3a9fcSMatthew G. Knepley . localU - The input field vector
8384ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8385ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8386ece3a9fcSMatthew G. Knepley 
8387ece3a9fcSMatthew G. Knepley   Output Parameter:
8388ece3a9fcSMatthew G. Knepley . localX - The output vector
8389ece3a9fcSMatthew G. Knepley 
839020f4b53cSBarry Smith   Calling sequence of `funcs`:
8391ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8392ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8393ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8394ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8395ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8396ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8397ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8398ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8399ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8400ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8401ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8402ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8403ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8404ece3a9fcSMatthew G. Knepley . t            - The current time
8405ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8406ece3a9fcSMatthew G. Knepley . n            - The face normal
8407ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8408ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8409ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8410ece3a9fcSMatthew G. Knepley 
8411ece3a9fcSMatthew G. Knepley   Note:
8412bb7acecfSBarry 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.
8413bb7acecfSBarry 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
8414bb7acecfSBarry 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
8415ece3a9fcSMatthew 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.
8416ece3a9fcSMatthew G. Knepley 
8417ece3a9fcSMatthew G. Knepley   Level: intermediate
8418ece3a9fcSMatthew G. Knepley 
8419bb7acecfSBarry Smith   Developer Notes:
8420bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8421bb7acecfSBarry Smith 
8422bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8423bb7acecfSBarry Smith 
84241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8425ece3a9fcSMatthew G. Knepley @*/
8426a4e35b19SJacob 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)
8427d71ae5a4SJacob Faibussowitsch {
8428ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8429ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8430064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8431064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
84329566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
84333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8434ece3a9fcSMatthew G. Knepley }
8435ece3a9fcSMatthew G. Knepley 
8436ece3a9fcSMatthew G. Knepley /*@C
84372716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
84382716604bSToby Isaac 
843920f4b53cSBarry Smith   Collective
8440bb7acecfSBarry Smith 
84412716604bSToby Isaac   Input Parameters:
8442bb7acecfSBarry Smith + dm    - The `DM`
84430709b2feSToby Isaac . time  - The time
84442716604bSToby Isaac . funcs - The functions to evaluate for each field component
84452716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8446574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84472716604bSToby Isaac 
84482716604bSToby Isaac   Output Parameter:
84492716604bSToby Isaac . diff - The diff ||u - u_h||_2
84502716604bSToby Isaac 
84512716604bSToby Isaac   Level: developer
84522716604bSToby Isaac 
8453bb7acecfSBarry Smith   Developer Notes:
8454bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8455bb7acecfSBarry Smith 
8456bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8457bb7acecfSBarry Smith 
84581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84592716604bSToby Isaac @*/
8460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8461d71ae5a4SJacob Faibussowitsch {
84622716604bSToby Isaac   PetscFunctionBegin;
84632716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8464b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84659566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
84663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84672716604bSToby Isaac }
8468b698f381SToby Isaac 
8469b698f381SToby Isaac /*@C
8470b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8471b698f381SToby Isaac 
847220f4b53cSBarry Smith   Collective
8473d083f849SBarry Smith 
8474b698f381SToby Isaac   Input Parameters:
8475bb7acecfSBarry Smith + dm    - The `DM`
8476a4e35b19SJacob Faibussowitsch . time  - The time
8477b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8478b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8479574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8480b698f381SToby Isaac - n     - The vector to project along
8481b698f381SToby Isaac 
8482b698f381SToby Isaac   Output Parameter:
8483b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8484b698f381SToby Isaac 
8485b698f381SToby Isaac   Level: developer
8486b698f381SToby Isaac 
8487bb7acecfSBarry Smith   Developer Notes:
8488bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8489bb7acecfSBarry Smith 
8490bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8491bb7acecfSBarry Smith 
84921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8493b698f381SToby Isaac @*/
8494d71ae5a4SJacob 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)
8495d71ae5a4SJacob Faibussowitsch {
8496b698f381SToby Isaac   PetscFunctionBegin;
8497b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8498b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84999566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
85003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8501b698f381SToby Isaac }
8502b698f381SToby Isaac 
85032a16baeaSToby Isaac /*@C
85042a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85052a16baeaSToby Isaac 
850620f4b53cSBarry Smith   Collective
8507d083f849SBarry Smith 
85082a16baeaSToby Isaac   Input Parameters:
8509bb7acecfSBarry Smith + dm    - The `DM`
85102a16baeaSToby Isaac . time  - The time
85112a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85122a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8513574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85142a16baeaSToby Isaac 
85152a16baeaSToby Isaac   Output Parameter:
85162a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85172a16baeaSToby Isaac 
85182a16baeaSToby Isaac   Level: developer
85192a16baeaSToby Isaac 
8520bb7acecfSBarry Smith   Developer Notes:
8521bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8522bb7acecfSBarry Smith 
8523bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8524bb7acecfSBarry Smith 
852542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
85262a16baeaSToby Isaac @*/
8527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8528d71ae5a4SJacob Faibussowitsch {
85292a16baeaSToby Isaac   PetscFunctionBegin;
85302a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85312a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
85329566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
85333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85342a16baeaSToby Isaac }
85352a16baeaSToby Isaac 
8536df0b854cSToby Isaac /*@C
8537bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8538502a2867SDave May 
8539502a2867SDave May   Not Collective
8540502a2867SDave May 
8541502a2867SDave May   Input Parameter:
8542bb7acecfSBarry Smith . dm - The `DM`
8543502a2867SDave May 
85440a19bb7dSprj-   Output Parameters:
85450a19bb7dSprj- + nranks - the number of neighbours
85460a19bb7dSprj- - ranks  - the neighbors ranks
8547502a2867SDave May 
85489bdbcad8SBarry Smith   Level: beginner
85499bdbcad8SBarry Smith 
8550bb7acecfSBarry Smith   Note:
8551bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8552502a2867SDave May 
85531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8554502a2867SDave May @*/
8555d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8556d71ae5a4SJacob Faibussowitsch {
8557502a2867SDave May   PetscFunctionBegin;
8558502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85599566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
85603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8561502a2867SDave May }
8562502a2867SDave May 
8563531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8564531c7667SBarry Smith 
8565531c7667SBarry Smith /*
8566531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
85672b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8568531c7667SBarry Smith */
856966976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8570d71ae5a4SJacob Faibussowitsch {
8571531c7667SBarry Smith   PetscFunctionBegin;
8572531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8573531c7667SBarry Smith     Vec x1local;
8574531c7667SBarry Smith     DM  dm;
85759566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85767a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85779566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85789566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85799566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8580531c7667SBarry Smith     x1 = x1local;
8581531c7667SBarry Smith   }
85829566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8583531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8584531c7667SBarry Smith     DM dm;
85859566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85869566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8587531c7667SBarry Smith   }
85883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8589531c7667SBarry Smith }
8590531c7667SBarry Smith 
8591531c7667SBarry Smith /*@
8592bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8593531c7667SBarry Smith 
8594a4e35b19SJacob Faibussowitsch   Input Parameters:
8595a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8596a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8597531c7667SBarry Smith 
85989bdbcad8SBarry Smith   Level: advanced
85999bdbcad8SBarry Smith 
860060225df5SJacob Faibussowitsch   Developer Notes:
8601bb7acecfSBarry Smith   this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8602531c7667SBarry Smith 
86031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8604531c7667SBarry Smith @*/
8605d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8606d71ae5a4SJacob Faibussowitsch {
8607531c7667SBarry Smith   PetscFunctionBegin;
8608531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8610531c7667SBarry Smith }
86118320bc6fSPatrick Sanan 
86128320bc6fSPatrick Sanan /*@
8613bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86148320bc6fSPatrick Sanan 
86158320bc6fSPatrick Sanan   Collective
86168320bc6fSPatrick Sanan 
86178320bc6fSPatrick Sanan   Input Parameters:
8618bb7acecfSBarry Smith + dm1 - the first `DM`
8619bb7acecfSBarry Smith - dm2 - the second `DM`
86208320bc6fSPatrick Sanan 
86218320bc6fSPatrick Sanan   Output Parameters:
8622bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8623bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86248320bc6fSPatrick Sanan 
862520f4b53cSBarry Smith   Level: advanced
862620f4b53cSBarry Smith 
86278320bc6fSPatrick Sanan   Notes:
8628bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
86293d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
86308320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8631bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
86323d862458SPatrick Sanan   decomposition, but hold different data.
86338320bc6fSPatrick Sanan 
86348320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8635bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
86368320bc6fSPatrick Sanan 
8637bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
86388320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
86398320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8640bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
86418320bc6fSPatrick Sanan 
8642bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8643bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
86448320bc6fSPatrick Sanan .vb
86458320bc6fSPatrick Sanan   ...
86469566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
86478320bc6fSPatrick Sanan   if (set && compatible)  {
86489566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86499566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86509566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
86518320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86528320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86538320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86548320bc6fSPatrick Sanan       }
86558320bc6fSPatrick Sanan     }
86569566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86579566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86588320bc6fSPatrick Sanan   } else {
86598320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86608320bc6fSPatrick Sanan   }
86618320bc6fSPatrick Sanan   ...
86628320bc6fSPatrick Sanan .ve
86638320bc6fSPatrick Sanan 
8664bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
86658320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
86668320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
86678320bc6fSPatrick Sanan   always check the "set" output parameter.
86688320bc6fSPatrick Sanan 
8669bb7acecfSBarry Smith   A `DM` is always compatible with itself.
86708320bc6fSPatrick Sanan 
8671bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
86728320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86738320bc6fSPatrick Sanan   incompatible.
86748320bc6fSPatrick Sanan 
86758320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8676bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
86778320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
86788320bc6fSPatrick Sanan 
867960225df5SJacob Faibussowitsch   Developer Notes:
8680bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86813d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8682a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8683bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
86848320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
86853d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8686bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
86878320bc6fSPatrick Sanan 
86881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86898320bc6fSPatrick Sanan @*/
8690d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8691d71ae5a4SJacob Faibussowitsch {
86928320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86938320bc6fSPatrick Sanan   DMType      type, type2;
86948320bc6fSPatrick Sanan   PetscBool   sameType;
86958320bc6fSPatrick Sanan 
86968320bc6fSPatrick Sanan   PetscFunctionBegin;
8697a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86988320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86998320bc6fSPatrick Sanan 
87008320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8701a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87028320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87038320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87043ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87058320bc6fSPatrick Sanan   }
87068320bc6fSPatrick Sanan 
87078320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87088320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87098320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87108320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87128320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87138320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87148320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87153ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87168320bc6fSPatrick Sanan   }
87178320bc6fSPatrick Sanan 
87188320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8719a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8720dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87213ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87228320bc6fSPatrick Sanan   }
87238320bc6fSPatrick Sanan 
8724a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
87258320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
87269566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
87279566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
87289566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
87298320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8730dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
87318320bc6fSPatrick Sanan   } else {
87328320bc6fSPatrick Sanan     *set = PETSC_FALSE;
87338320bc6fSPatrick Sanan   }
87343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
87358320bc6fSPatrick Sanan }
8736c0f0dcc3SMatthew G. Knepley 
8737c0f0dcc3SMatthew G. Knepley /*@C
8738bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8739c0f0dcc3SMatthew G. Knepley 
874020f4b53cSBarry Smith   Logically Collective
8741c0f0dcc3SMatthew G. Knepley 
8742c0f0dcc3SMatthew G. Knepley   Input Parameters:
874360225df5SJacob Faibussowitsch + dm             - the `DM`
8744c0f0dcc3SMatthew G. Knepley . f              - the monitor function
874520f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
874620f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8747c0f0dcc3SMatthew G. Knepley 
874820f4b53cSBarry Smith   Options Database Key:
874960225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8750c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8751c0f0dcc3SMatthew G. Knepley 
87529bdbcad8SBarry Smith   Level: intermediate
87539bdbcad8SBarry Smith 
8754bb7acecfSBarry Smith   Note:
8755c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8756bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8757c0f0dcc3SMatthew G. Knepley   order in which they were set.
8758c0f0dcc3SMatthew G. Knepley 
875960225df5SJacob Faibussowitsch   Fortran Notes:
8760bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8761bb7acecfSBarry Smith 
876260225df5SJacob Faibussowitsch   Developer Notes:
8763bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8764c0f0dcc3SMatthew G. Knepley 
87651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8766c0f0dcc3SMatthew G. Knepley @*/
8767d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8768d71ae5a4SJacob Faibussowitsch {
8769c0f0dcc3SMatthew G. Knepley   PetscInt m;
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8772c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8773c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8774c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8775c0f0dcc3SMatthew G. Knepley 
87769566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
87773ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8778c0f0dcc3SMatthew G. Knepley   }
87797a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8780c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8781c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8782c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
87833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8784c0f0dcc3SMatthew G. Knepley }
8785c0f0dcc3SMatthew G. Knepley 
8786c0f0dcc3SMatthew G. Knepley /*@
8787bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8788c0f0dcc3SMatthew G. Knepley 
878920f4b53cSBarry Smith   Logically Collective
8790c0f0dcc3SMatthew G. Knepley 
8791c0f0dcc3SMatthew G. Knepley   Input Parameter:
8792c0f0dcc3SMatthew G. Knepley . dm - the DM
8793c0f0dcc3SMatthew G. Knepley 
8794c0f0dcc3SMatthew G. Knepley   Options Database Key:
8795c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8796bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8797c0f0dcc3SMatthew G. Knepley   set via the options database
8798c0f0dcc3SMatthew G. Knepley 
87999bdbcad8SBarry Smith   Level: intermediate
88009bdbcad8SBarry Smith 
8801bb7acecfSBarry Smith   Note:
8802bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8803c0f0dcc3SMatthew G. Knepley 
88041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8805c0f0dcc3SMatthew G. Knepley @*/
8806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8807d71ae5a4SJacob Faibussowitsch {
8808c0f0dcc3SMatthew G. Knepley   PetscInt m;
8809c0f0dcc3SMatthew G. Knepley 
8810c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8811c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8812c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88139566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8814c0f0dcc3SMatthew G. Knepley   }
8815c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8817c0f0dcc3SMatthew G. Knepley }
8818c0f0dcc3SMatthew G. Knepley 
8819c0f0dcc3SMatthew G. Knepley /*@C
8820c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8821c0f0dcc3SMatthew G. Knepley 
882220f4b53cSBarry Smith   Collective
8823c0f0dcc3SMatthew G. Knepley 
8824c0f0dcc3SMatthew G. Knepley   Input Parameters:
8825bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8826c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8827c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8828c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8829c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8830bb7acecfSBarry 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
8831c0f0dcc3SMatthew G. Knepley 
8832c0f0dcc3SMatthew G. Knepley   Output Parameter:
8833c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8834c0f0dcc3SMatthew G. Knepley 
8835c0f0dcc3SMatthew G. Knepley   Level: developer
8836c0f0dcc3SMatthew G. Knepley 
88371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8838db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
883960225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8840db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8841c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8842db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8843bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8844c0f0dcc3SMatthew G. Knepley @*/
8845d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8846d71ae5a4SJacob Faibussowitsch {
8847c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8848c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8849c0f0dcc3SMatthew G. Knepley 
8850c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8851c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8853c0f0dcc3SMatthew G. Knepley   if (*flg) {
8854c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8855c0f0dcc3SMatthew G. Knepley 
88569566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
88579566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
88589566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88599566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8860c0f0dcc3SMatthew G. Knepley   }
88613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8862c0f0dcc3SMatthew G. Knepley }
8863c0f0dcc3SMatthew G. Knepley 
8864c0f0dcc3SMatthew G. Knepley /*@
8865c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8866c0f0dcc3SMatthew G. Knepley 
886720f4b53cSBarry Smith   Collective
8868c0f0dcc3SMatthew G. Knepley 
88692fe279fdSBarry Smith   Input Parameter:
8870bb7acecfSBarry Smith . dm - The `DM`
8871c0f0dcc3SMatthew G. Knepley 
8872c0f0dcc3SMatthew G. Knepley   Level: developer
8873c0f0dcc3SMatthew G. Knepley 
8874a4e35b19SJacob Faibussowitsch   Developer Notes:
8875a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8876a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8877a4e35b19SJacob Faibussowitsch   concept of discretization.
8878bb7acecfSBarry Smith 
88791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8880c0f0dcc3SMatthew G. Knepley @*/
8881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8882d71ae5a4SJacob Faibussowitsch {
8883c0f0dcc3SMatthew G. Knepley   PetscInt m;
8884c0f0dcc3SMatthew G. Knepley 
8885c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
88863ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8887c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
888848a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
88893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8890c0f0dcc3SMatthew G. Knepley }
88912e4af2aeSMatthew G. Knepley 
88922e4af2aeSMatthew G. Knepley /*@
8893bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88942e4af2aeSMatthew G. Knepley 
889520f4b53cSBarry Smith   Collective
88962e4af2aeSMatthew G. Knepley 
88972e4af2aeSMatthew G. Knepley   Input Parameters:
8898bb7acecfSBarry Smith + dm  - The `DM`
88996b867d5aSJose E. Roman - sol - The solution vector
89002e4af2aeSMatthew G. Knepley 
89016b867d5aSJose E. Roman   Input/Output Parameter:
890220f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89036b867d5aSJose E. Roman            contains the error in each field
89046b867d5aSJose E. Roman 
89056b867d5aSJose E. Roman   Output Parameter:
890620f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
890720f4b53cSBarry Smith 
890820f4b53cSBarry Smith   Level: developer
89092e4af2aeSMatthew G. Knepley 
8910bb7acecfSBarry Smith   Note:
8911bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89122e4af2aeSMatthew G. Knepley 
89131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89142e4af2aeSMatthew G. Knepley @*/
8915d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8916d71ae5a4SJacob Faibussowitsch {
89172e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89182e4af2aeSMatthew G. Knepley   void    **ctxs;
89192e4af2aeSMatthew G. Knepley   PetscReal time;
89202e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89212e4af2aeSMatthew G. Knepley 
89222e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89239566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89249566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
89259566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
89262e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
89272e4af2aeSMatthew G. Knepley     PetscDS         ds;
89282e4af2aeSMatthew G. Knepley     DMLabel         label;
89292e4af2aeSMatthew G. Knepley     IS              fieldIS;
89302e4af2aeSMatthew G. Knepley     const PetscInt *fields;
89312e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
89322e4af2aeSMatthew G. Knepley 
893307218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
89349566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
89359566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
89362e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
89372e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
89389566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
89392e4af2aeSMatthew G. Knepley     }
89409566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
89412e4af2aeSMatthew G. Knepley   }
8942ad540459SPierre 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);
89439566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
89449566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
89452e4af2aeSMatthew G. Knepley   if (errorVec) {
89462e4af2aeSMatthew G. Knepley     DM             edm;
89472e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89482e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89492e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89502e4af2aeSMatthew G. Knepley 
89519566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
89529566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89552e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89569566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89572e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89582e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89592e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89602e4af2aeSMatthew G. Knepley       const char     *name;
89612e4af2aeSMatthew G. Knepley 
89629566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89639566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89649566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89659566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89669566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89679566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89689566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89699566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89702e4af2aeSMatthew G. Knepley     }
89719566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89722e4af2aeSMatthew G. Knepley 
89739566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89749566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89759566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89769566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89772e4af2aeSMatthew G. Knepley   }
89789566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89802e4af2aeSMatthew G. Knepley }
89819a2a23afSMatthew G. Knepley 
89829a2a23afSMatthew G. Knepley /*@
8983bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89849a2a23afSMatthew G. Knepley 
898520f4b53cSBarry Smith   Not Collective
89869a2a23afSMatthew G. Knepley 
89879a2a23afSMatthew G. Knepley   Input Parameter:
8988bb7acecfSBarry Smith . dm - The `DM`
89899a2a23afSMatthew G. Knepley 
89909a2a23afSMatthew G. Knepley   Output Parameter:
8991a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89929a2a23afSMatthew G. Knepley 
89939a2a23afSMatthew G. Knepley   Level: advanced
89949a2a23afSMatthew G. Knepley 
899560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
89969a2a23afSMatthew G. Knepley @*/
8997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8998d71ae5a4SJacob Faibussowitsch {
89999a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90009a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90019566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90039a2a23afSMatthew G. Knepley }
90049a2a23afSMatthew G. Knepley 
90059a2a23afSMatthew G. Knepley /*@
9006ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90079a2a23afSMatthew G. Knepley 
900820f4b53cSBarry Smith   Not Collective
90099a2a23afSMatthew G. Knepley 
90109a2a23afSMatthew G. Knepley   Input Parameters:
9011bb7acecfSBarry Smith + dm    - The `DM`
9012bb7acecfSBarry Smith . label - The `DMLabel`
9013ac17215fSMatthew G. Knepley . value - The label value indicating the region
9014ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90159a2a23afSMatthew G. Knepley 
90169a2a23afSMatthew G. Knepley   Output Parameter:
9017bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90189a2a23afSMatthew G. Knepley 
90199bdbcad8SBarry Smith   Level: advanced
90209bdbcad8SBarry Smith 
9021bb7acecfSBarry Smith   Note:
9022bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
902304c51a94SMatthew G. Knepley 
90241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
90259a2a23afSMatthew G. Knepley @*/
9026d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9027d71ae5a4SJacob Faibussowitsch {
9028ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
902904c51a94SMatthew G. Knepley   PetscBool       has;
90309a2a23afSMatthew G. Knepley 
90319a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90329a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90339a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90349a2a23afSMatthew G. Knepley   key.label = label;
90359a2a23afSMatthew G. Knepley   key.value = value;
9036ac17215fSMatthew G. Knepley   key.part  = part;
90379566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
90389566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
90399566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
90403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90419a2a23afSMatthew G. Knepley }
90429a2a23afSMatthew G. Knepley 
90439a2a23afSMatthew G. Knepley /*@
9044bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
90459a2a23afSMatthew G. Knepley 
904620f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
90479a2a23afSMatthew G. Knepley 
90489a2a23afSMatthew G. Knepley   Input Parameters:
9049bb7acecfSBarry Smith + dm    - The `DM`
9050bb7acecfSBarry Smith . label - The `DMLabel`
90519a2a23afSMatthew G. Knepley . value - The label value indicating the region
9052ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9053bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
90549a2a23afSMatthew G. Knepley 
90559a2a23afSMatthew G. Knepley   Level: advanced
90569a2a23afSMatthew G. Knepley 
90571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90589a2a23afSMatthew G. Knepley @*/
9059d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9060d71ae5a4SJacob Faibussowitsch {
90619a2a23afSMatthew G. Knepley   Vec             old;
90629a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90639a2a23afSMatthew G. Knepley 
90649a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90659a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90669a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90679a2a23afSMatthew G. Knepley   key.label = label;
90689a2a23afSMatthew G. Knepley   key.value = value;
9069ac17215fSMatthew G. Knepley   key.part  = part;
90709566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90729566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
90739566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90749566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90769a2a23afSMatthew G. Knepley }
90779a2a23afSMatthew G. Knepley 
90789a2a23afSMatthew G. Knepley /*@C
9079bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90809a2a23afSMatthew G. Knepley 
908120f4b53cSBarry Smith   Not Collective
90829a2a23afSMatthew G. Knepley 
90839a2a23afSMatthew G. Knepley   Input Parameter:
9084bb7acecfSBarry Smith . dm - The `DM`
90859a2a23afSMatthew G. Knepley 
90869a2a23afSMatthew G. Knepley   Output Parameters:
9087bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9088bb7acecfSBarry Smith . values - The label values for each `Vec`
9089bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
90909a2a23afSMatthew G. Knepley 
90919bdbcad8SBarry Smith   Level: advanced
90929bdbcad8SBarry Smith 
9093bb7acecfSBarry Smith   Note:
9094bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90959a2a23afSMatthew G. Knepley 
909660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
90979a2a23afSMatthew G. Knepley @*/
9098d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9099d71ae5a4SJacob Faibussowitsch {
91009a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91019a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91029a2a23afSMatthew G. Knepley 
91039a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91049a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91054f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91064f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91074f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91089566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91109566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91119371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91129371c9d4SSatish Balay     labels[i] = keys[i].label;
91139371c9d4SSatish Balay     values[i] = keys[i].value;
91149371c9d4SSatish Balay     parts[i]  = keys[i].part;
91159371c9d4SSatish Balay   }
91169566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91189a2a23afSMatthew G. Knepley }
91199a2a23afSMatthew G. Knepley 
91209a2a23afSMatthew G. Knepley /*@
9121bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91229a2a23afSMatthew G. Knepley 
912320f4b53cSBarry Smith   Not Collective
91249a2a23afSMatthew G. Knepley 
91259a2a23afSMatthew G. Knepley   Input Parameter:
9126bb7acecfSBarry Smith . dm - The `DM`
91279a2a23afSMatthew G. Knepley 
91289a2a23afSMatthew G. Knepley   Output Parameter:
9129bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
91309a2a23afSMatthew G. Knepley 
91319a2a23afSMatthew G. Knepley   Level: advanced
91329a2a23afSMatthew G. Knepley 
9133bb7acecfSBarry Smith   Note:
9134bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9135bb7acecfSBarry Smith 
91361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
91379a2a23afSMatthew G. Knepley @*/
9138d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9139d71ae5a4SJacob Faibussowitsch {
91409a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91419a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91429566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
91439566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
91443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91459a2a23afSMatthew G. Knepley }
9146b5a892a1SMatthew G. Knepley 
9147b5a892a1SMatthew G. Knepley /*@C
9148bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9149b5a892a1SMatthew G. Knepley 
915020f4b53cSBarry Smith   Not Collective
9151b5a892a1SMatthew G. Knepley 
9152b5a892a1SMatthew G. Knepley   Input Parameters:
9153bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9154b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9155b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9156b5a892a1SMatthew G. Knepley 
9157b5a892a1SMatthew G. Knepley   Output Parameters:
9158bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9159b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9160b5a892a1SMatthew G. Knepley 
9161b5a892a1SMatthew G. Knepley   Level: advanced
9162b5a892a1SMatthew G. Knepley 
9163bb7acecfSBarry Smith   Note:
9164bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9165bb7acecfSBarry Smith 
9166bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9167bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9168bb7acecfSBarry Smith 
9169bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9170bb7acecfSBarry Smith 
91711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9172b5a892a1SMatthew G. Knepley @*/
9173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9174d71ae5a4SJacob Faibussowitsch {
9175b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9176b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9177b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9178b5a892a1SMatthew G. Knepley 
9179b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91809371c9d4SSatish Balay   if (!nO) {
91819371c9d4SSatish Balay     *ornt  = 0;
91829371c9d4SSatish Balay     *found = PETSC_TRUE;
91833ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91849371c9d4SSatish Balay   }
9185b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9186b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9187b5a892a1SMatthew G. Knepley 
91889371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91899371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91909371c9d4SSatish Balay     if (c == cS) {
91919371c9d4SSatish Balay       *ornt = o;
91929371c9d4SSatish Balay       break;
91939371c9d4SSatish Balay     }
9194b5a892a1SMatthew G. Knepley   }
9195b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9197b5a892a1SMatthew G. Knepley }
9198b5a892a1SMatthew G. Knepley 
9199b5a892a1SMatthew G. Knepley /*@C
9200bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9201b5a892a1SMatthew G. Knepley 
920220f4b53cSBarry Smith   Not Collective
9203b5a892a1SMatthew G. Knepley 
9204b5a892a1SMatthew G. Knepley   Input Parameters:
9205bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9206b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9207b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9208b5a892a1SMatthew G. Knepley 
92092fe279fdSBarry Smith   Output Parameter:
9210bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9211b5a892a1SMatthew G. Knepley 
9212b5a892a1SMatthew G. Knepley   Level: advanced
9213b5a892a1SMatthew G. Knepley 
9214bb7acecfSBarry Smith   Note:
9215bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9216bb7acecfSBarry Smith 
921760225df5SJacob Faibussowitsch   Developer Notes:
9218bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9219bb7acecfSBarry Smith 
92201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9221b5a892a1SMatthew G. Knepley @*/
9222d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9223d71ae5a4SJacob Faibussowitsch {
9224b5a892a1SMatthew G. Knepley   PetscBool found;
9225b5a892a1SMatthew G. Knepley 
9226b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92279566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
92287a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9230b5a892a1SMatthew G. Knepley }
9231b5a892a1SMatthew G. Knepley 
9232b5a892a1SMatthew G. Knepley /*@C
9233bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9234b5a892a1SMatthew G. Knepley 
923520f4b53cSBarry Smith   Not Collective
9236b5a892a1SMatthew G. Knepley 
9237b5a892a1SMatthew G. Knepley   Input Parameters:
9238bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9239b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9240b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9241b5a892a1SMatthew G. Knepley 
9242b5a892a1SMatthew G. Knepley   Output Parameters:
9243bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9244b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9245b5a892a1SMatthew G. Knepley 
9246b5a892a1SMatthew G. Knepley   Level: advanced
9247b5a892a1SMatthew G. Knepley 
9248bb7acecfSBarry Smith   Note:
9249bb7acecfSBarry Smith   An arrangement is a vertex order
9250bb7acecfSBarry Smith 
9251bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9252bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9253bb7acecfSBarry Smith 
9254bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9255bb7acecfSBarry Smith 
92561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9257b5a892a1SMatthew G. Knepley @*/
9258d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9259d71ae5a4SJacob Faibussowitsch {
9260b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9261b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9262b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9263b5a892a1SMatthew G. Knepley 
9264b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92659371c9d4SSatish Balay   if (!nO) {
92669371c9d4SSatish Balay     *ornt  = 0;
92679371c9d4SSatish Balay     *found = PETSC_TRUE;
92683ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
92699371c9d4SSatish Balay   }
9270b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9271b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9272b5a892a1SMatthew G. Knepley 
92739371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92749371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92759371c9d4SSatish Balay     if (c == cS) {
92769371c9d4SSatish Balay       *ornt = o;
92779371c9d4SSatish Balay       break;
92789371c9d4SSatish Balay     }
9279b5a892a1SMatthew G. Knepley   }
9280b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
92813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9282b5a892a1SMatthew G. Knepley }
9283b5a892a1SMatthew G. Knepley 
9284b5a892a1SMatthew G. Knepley /*@C
9285bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9286b5a892a1SMatthew G. Knepley 
928720f4b53cSBarry Smith   Not Collective
9288b5a892a1SMatthew G. Knepley 
9289b5a892a1SMatthew G. Knepley   Input Parameters:
9290bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9291b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9292b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9293b5a892a1SMatthew G. Knepley 
92942fe279fdSBarry Smith   Output Parameter:
9295bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9296b5a892a1SMatthew G. Knepley 
9297b5a892a1SMatthew G. Knepley   Level: advanced
9298b5a892a1SMatthew G. Knepley 
9299bb7acecfSBarry Smith   Note:
9300bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9301bb7acecfSBarry Smith 
930260225df5SJacob Faibussowitsch   Developer Notes:
9303bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9304bb7acecfSBarry Smith 
93051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9306b5a892a1SMatthew G. Knepley @*/
9307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9308d71ae5a4SJacob Faibussowitsch {
9309b5a892a1SMatthew G. Knepley   PetscBool found;
9310b5a892a1SMatthew G. Knepley 
9311b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93129566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
93137a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9315b5a892a1SMatthew G. Knepley }
9316012bc364SMatthew G. Knepley 
9317012bc364SMatthew G. Knepley /*@C
9318012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9319012bc364SMatthew G. Knepley 
932020f4b53cSBarry Smith   Not Collective
9321012bc364SMatthew G. Knepley 
9322012bc364SMatthew G. Knepley   Input Parameters:
9323bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9324012bc364SMatthew G. Knepley - point - Coordinates of the point
9325012bc364SMatthew G. Knepley 
93262fe279fdSBarry Smith   Output Parameter:
9327012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9328012bc364SMatthew G. Knepley 
9329012bc364SMatthew G. Knepley   Level: advanced
9330012bc364SMatthew G. Knepley 
93311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9332012bc364SMatthew G. Knepley @*/
9333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9334d71ae5a4SJacob Faibussowitsch {
9335012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9336012bc364SMatthew G. Knepley   PetscInt  d;
9337012bc364SMatthew G. Knepley 
9338012bc364SMatthew G. Knepley   PetscFunctionBegin;
9339012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9340012bc364SMatthew G. Knepley   switch (ct) {
9341012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9342012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9343012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
93449371c9d4SSatish Balay       if (point[d] < -1.0) {
93459371c9d4SSatish Balay         *inside = PETSC_FALSE;
93469371c9d4SSatish Balay         break;
93479371c9d4SSatish Balay       }
9348012bc364SMatthew G. Knepley       sum += point[d];
9349012bc364SMatthew G. Knepley     }
93509371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
93519371c9d4SSatish Balay       *inside = PETSC_FALSE;
93529371c9d4SSatish Balay       break;
93539371c9d4SSatish Balay     }
9354012bc364SMatthew G. Knepley     break;
9355012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9356012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9357012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
93589371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
93599371c9d4SSatish Balay         *inside = PETSC_FALSE;
9360012bc364SMatthew G. Knepley         break;
93619371c9d4SSatish Balay       }
93629371c9d4SSatish Balay     break;
9363d71ae5a4SJacob Faibussowitsch   default:
9364d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9365012bc364SMatthew G. Knepley   }
93663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9367012bc364SMatthew G. Knepley }
9368