xref: /petsc/src/dm/interface/dm.c (revision aeebefc24116144b8d95b79b68f961bd1104fb5c)
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};
26476787b7SMatthew G. Knepley const char *const DMPolytopeTypes[] =
27476787b7SMatthew G. Knepley   {"vertex",  "segment",      "tensor_segment", "triangle", "quadrilateral",  "tensor_quad",  "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell",
28476787b7SMatthew G. Knepley    "unknown", "unknown_cell", "unknown_face",   "invalid",  "DMPolytopeType", "DM_POLYTOPE_", NULL};
292cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
3060c22052SBarry Smith 
31a4121054SBarry Smith /*@
32bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
33bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
34a4121054SBarry Smith 
35d083f849SBarry Smith   Collective
36a4121054SBarry Smith 
37a4121054SBarry Smith   Input Parameter:
38bb7acecfSBarry Smith . comm - The communicator for the `DM` object
39a4121054SBarry Smith 
40a4121054SBarry Smith   Output Parameter:
41bb7acecfSBarry Smith . dm - The `DM` object
42a4121054SBarry Smith 
43a4121054SBarry Smith   Level: beginner
44a4121054SBarry Smith 
45bb7acecfSBarry Smith   Notes:
46bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
47bb7acecfSBarry Smith 
48bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
49bb7acecfSBarry Smith   error when you try to use the dm.
50bb7acecfSBarry Smith 
511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
52a4121054SBarry Smith @*/
53d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
54d71ae5a4SJacob Faibussowitsch {
55a4121054SBarry Smith   DM      v;
56e5e52638SMatthew G. Knepley   PetscDS ds;
57a4121054SBarry Smith 
58a4121054SBarry Smith   PetscFunctionBegin;
594f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
600298fd71SBarry Smith   *dm = NULL;
619566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
62a4121054SBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
64e7c4fc90SDmitry Karpeev 
6562e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6662e5d2d2SJDBetteridge 
6749be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6849be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
690298fd71SBarry Smith   v->ltogmap              = NULL;
70a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
711411c6eeSJed Brown   v->bs                   = 1;
72171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
739566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
749566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
75c58f1c22SToby Isaac   v->labels                    = NULL;
7634aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7734aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
78c58f1c22SToby Isaac   v->depthLabel                = NULL;
79ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
801bb6d2a8SBarry Smith   v->localSection              = NULL;
811bb6d2a8SBarry Smith   v->globalSection             = NULL;
823b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
833b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8479769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
856858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
866858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
876858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8896173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
89435a35e8SMatthew G Knepley   {
90435a35e8SMatthew G Knepley     PetscInt i;
91435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
920298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
93f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
94435a35e8SMatthew G Knepley     }
95435a35e8SMatthew G Knepley   }
969566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9707218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
989566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
10014f150ffSMatthew G. Knepley   v->dmBC              = NULL;
101a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
102f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
103cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1049566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1059566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1064a7a4c06SLawrence Mitchell 
1071411c6eeSJed Brown   *dm = v;
1083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
109a4121054SBarry Smith }
110a4121054SBarry Smith 
11138221697SMatthew G. Knepley /*@
112bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11338221697SMatthew G. Knepley 
114d083f849SBarry Smith   Collective
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Input Parameter:
117bb7acecfSBarry Smith . dm - The original `DM` object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Output Parameter:
120bb7acecfSBarry Smith . newdm - The new `DM` object
12138221697SMatthew G. Knepley 
12238221697SMatthew G. Knepley   Level: beginner
12338221697SMatthew G. Knepley 
1241cb8cacdSPatrick Sanan   Notes:
125bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
126bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
127bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1281bb6d2a8SBarry Smith 
129bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
13089706ed2SPatrick Sanan 
131bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
132bb7acecfSBarry Smith 
13360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13438221697SMatthew G. Knepley @*/
135d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
136d71ae5a4SJacob Faibussowitsch {
13738221697SMatthew G. Knepley   PetscSF              sf;
13838221697SMatthew G. Knepley   Vec                  coords;
13938221697SMatthew G. Knepley   void                *ctx;
140ec196627SMatthew G. Knepley   MatOrderingType      otype;
141ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1426858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
14338221697SMatthew G. Knepley 
14438221697SMatthew G. Knepley   PetscFunctionBegin;
14538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1464f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1479566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1489566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
149ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
150ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
151c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
152fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1539566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1549566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1559566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1569566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
159dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1603f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1619566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1629566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1639566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1649566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
165ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
166ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
167ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
168ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1696858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1706858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
171be4c1c3eSMatthew G. Knepley       DM           ncdm;
172be4c1c3eSMatthew G. Knepley       PetscSection cs;
1735a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
174be4c1c3eSMatthew G. Knepley 
1756858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1769566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
177712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1785a0206caSToby Isaac       if (pEndMax >= 0) {
1796858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1806858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1819566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1825e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1835e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1845e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1855e50ca95SJames Wright         }
1866858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1876858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1889566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
189be4c1c3eSMatthew G. Knepley       }
190be4c1c3eSMatthew G. Knepley     }
1916858538eSMatthew G. Knepley   }
1929566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1939566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
19538221697SMatthew G. Knepley   if (coords) {
1969566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
19738221697SMatthew G. Knepley   } else {
1989566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1999566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
20038221697SMatthew G. Knepley   }
2016858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
2026858538eSMatthew G. Knepley   if (coords) {
2036858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
2046858538eSMatthew G. Knepley   } else {
2056858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
2066858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
2076858538eSMatthew G. Knepley   }
20890b157c4SStefano Zampini   {
2094fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2106858538eSMatthew G. Knepley 
2114fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2124fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
213c6b900c6SMatthew G. Knepley   }
21434aa8a36SMatthew G. Knepley   {
21534aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
21634aa8a36SMatthew G. Knepley 
2179566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2189566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
21934aa8a36SMatthew G. Knepley   }
2203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22138221697SMatthew G. Knepley }
22238221697SMatthew G. Knepley 
2239a42bb27SBarry Smith /*@C
224bb7acecfSBarry Smith   DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2259a42bb27SBarry Smith 
22620f4b53cSBarry Smith   Logically Collective
2279a42bb27SBarry Smith 
228147403d9SBarry Smith   Input Parameters:
22932546409SMatthew G. Knepley + dm    - initial distributed array
230bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2319a42bb27SBarry Smith 
23220f4b53cSBarry Smith   Options Database Key:
233147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2349a42bb27SBarry Smith 
2359a42bb27SBarry Smith   Level: intermediate
2369a42bb27SBarry Smith 
23760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
238bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2399a42bb27SBarry Smith @*/
24032546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
241d71ae5a4SJacob Faibussowitsch {
24232546409SMatthew G. Knepley   char *tmp;
24332546409SMatthew G. Knepley 
2449a42bb27SBarry Smith   PetscFunctionBegin;
24532546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24632546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
24732546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
24832546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
24932546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2519a42bb27SBarry Smith }
2529a42bb27SBarry Smith 
253c0dedaeaSBarry Smith /*@C
254bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
255c0dedaeaSBarry Smith 
25620f4b53cSBarry Smith   Logically Collective
257c0dedaeaSBarry Smith 
258c0dedaeaSBarry Smith   Input Parameter:
259c0dedaeaSBarry Smith . da - initial distributed array
260c0dedaeaSBarry Smith 
261c0dedaeaSBarry Smith   Output Parameter:
262c0dedaeaSBarry Smith . ctype - the vector type
263c0dedaeaSBarry Smith 
264c0dedaeaSBarry Smith   Level: intermediate
265c0dedaeaSBarry Smith 
26660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
267c0dedaeaSBarry Smith @*/
268d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
269d71ae5a4SJacob Faibussowitsch {
270c0dedaeaSBarry Smith   PetscFunctionBegin;
271c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
272c0dedaeaSBarry Smith   *ctype = da->vectype;
2733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
274c0dedaeaSBarry Smith }
275c0dedaeaSBarry Smith 
2765f1ad066SMatthew G Knepley /*@
277bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2785f1ad066SMatthew G Knepley 
27920f4b53cSBarry Smith   Not Collective
2805f1ad066SMatthew G Knepley 
2815f1ad066SMatthew G Knepley   Input Parameter:
282bb7acecfSBarry Smith . v - The `Vec`
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley   Output Parameter:
285bb7acecfSBarry Smith . dm - The `DM`
2865f1ad066SMatthew G Knepley 
2875f1ad066SMatthew G Knepley   Level: intermediate
2885f1ad066SMatthew G Knepley 
289bb7acecfSBarry Smith   Note:
290bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
291bb7acecfSBarry Smith 
2921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2935f1ad066SMatthew G Knepley @*/
294d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
295d71ae5a4SJacob Faibussowitsch {
2965f1ad066SMatthew G Knepley   PetscFunctionBegin;
2975f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2984f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2999566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
3003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3015f1ad066SMatthew G Knepley }
3025f1ad066SMatthew G Knepley 
3035f1ad066SMatthew G Knepley /*@
304bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
3055f1ad066SMatthew G Knepley 
30620f4b53cSBarry Smith   Not Collective
3075f1ad066SMatthew G Knepley 
3085f1ad066SMatthew G Knepley   Input Parameters:
309bb7acecfSBarry Smith + v  - The `Vec`
310bb7acecfSBarry Smith - dm - The `DM`
3115f1ad066SMatthew G Knepley 
31220f4b53cSBarry Smith   Level: developer
31320f4b53cSBarry Smith 
31473ff1848SBarry Smith   Notes:
315bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
316d9805387SMatthew G. Knepley 
317bb7acecfSBarry 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.
318bb7acecfSBarry Smith 
3191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3205f1ad066SMatthew G Knepley @*/
321d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
322d71ae5a4SJacob Faibussowitsch {
3235f1ad066SMatthew G Knepley   PetscFunctionBegin;
3245f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
325d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3269566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3285f1ad066SMatthew G Knepley }
3295f1ad066SMatthew G Knepley 
330521d9a4cSLisandro Dalcin /*@C
331bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3328f1509bcSBarry Smith 
33320f4b53cSBarry Smith   Logically Collective
3348f1509bcSBarry Smith 
3358f1509bcSBarry Smith   Input Parameters:
336bb7acecfSBarry Smith + dm    - the `DM` context
3378f1509bcSBarry Smith - ctype - the matrix type
3388f1509bcSBarry Smith 
33920f4b53cSBarry Smith   Options Database Key:
3408f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3418f1509bcSBarry Smith 
3428f1509bcSBarry Smith   Level: intermediate
3438f1509bcSBarry Smith 
3441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
345bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3468f1509bcSBarry Smith @*/
347d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
348d71ae5a4SJacob Faibussowitsch {
3498f1509bcSBarry Smith   PetscFunctionBegin;
3508f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3518f1509bcSBarry Smith   dm->coloringtype = ctype;
3523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3538f1509bcSBarry Smith }
3548f1509bcSBarry Smith 
3558f1509bcSBarry Smith /*@C
356bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
357521d9a4cSLisandro Dalcin 
35820f4b53cSBarry Smith   Logically Collective
359521d9a4cSLisandro Dalcin 
360521d9a4cSLisandro Dalcin   Input Parameter:
361bb7acecfSBarry Smith . dm - the `DM` context
3628f1509bcSBarry Smith 
3638f1509bcSBarry Smith   Output Parameter:
3648f1509bcSBarry Smith . ctype - the matrix type
3658f1509bcSBarry Smith 
36620f4b53cSBarry Smith   Options Database Key:
3678f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3688f1509bcSBarry Smith 
3698f1509bcSBarry Smith   Level: intermediate
3708f1509bcSBarry Smith 
3711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
37242747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3738f1509bcSBarry Smith @*/
374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
375d71ae5a4SJacob Faibussowitsch {
3768f1509bcSBarry Smith   PetscFunctionBegin;
3778f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3788f1509bcSBarry Smith   *ctype = dm->coloringtype;
3793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3808f1509bcSBarry Smith }
3818f1509bcSBarry Smith 
3828f1509bcSBarry Smith /*@C
383bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3848f1509bcSBarry Smith 
38520f4b53cSBarry Smith   Logically Collective
3868f1509bcSBarry Smith 
3878f1509bcSBarry Smith   Input Parameters:
388bb7acecfSBarry Smith + dm    - the `DM` context
389bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
390521d9a4cSLisandro Dalcin 
39120f4b53cSBarry Smith   Options Database Key:
392bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
393521d9a4cSLisandro Dalcin 
394521d9a4cSLisandro Dalcin   Level: intermediate
395521d9a4cSLisandro Dalcin 
39642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
397521d9a4cSLisandro Dalcin @*/
398d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
399d71ae5a4SJacob Faibussowitsch {
40032546409SMatthew G. Knepley   char *tmp;
40132546409SMatthew G. Knepley 
402521d9a4cSLisandro Dalcin   PetscFunctionBegin;
403521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40432546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
40532546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
4069566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
40732546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
4083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
409521d9a4cSLisandro Dalcin }
410521d9a4cSLisandro Dalcin 
411c0dedaeaSBarry Smith /*@C
41220f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
413c0dedaeaSBarry Smith 
41420f4b53cSBarry Smith   Logically Collective
415c0dedaeaSBarry Smith 
416c0dedaeaSBarry Smith   Input Parameter:
417bb7acecfSBarry Smith . dm - the `DM` context
418c0dedaeaSBarry Smith 
419c0dedaeaSBarry Smith   Output Parameter:
420c0dedaeaSBarry Smith . ctype - the matrix type
421c0dedaeaSBarry Smith 
422c0dedaeaSBarry Smith   Level: intermediate
423c0dedaeaSBarry Smith 
42442747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
425c0dedaeaSBarry Smith @*/
426d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
427d71ae5a4SJacob Faibussowitsch {
428c0dedaeaSBarry Smith   PetscFunctionBegin;
429c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
430c0dedaeaSBarry Smith   *ctype = dm->mattype;
4313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432c0dedaeaSBarry Smith }
433c0dedaeaSBarry Smith 
434c688c046SMatthew G Knepley /*@
435bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
436c688c046SMatthew G Knepley 
43720f4b53cSBarry Smith   Not Collective
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   Input Parameter:
440bb7acecfSBarry Smith . A - The `Mat`
441c688c046SMatthew G Knepley 
442c688c046SMatthew G Knepley   Output Parameter:
443bb7acecfSBarry Smith . dm - The `DM`
444c688c046SMatthew G Knepley 
445c688c046SMatthew G Knepley   Level: intermediate
446c688c046SMatthew G Knepley 
447bb7acecfSBarry Smith   Note:
448bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
449bb7acecfSBarry Smith 
45073ff1848SBarry Smith   Developer Note:
451bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4528f1509bcSBarry Smith 
4531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
454c688c046SMatthew G Knepley @*/
455d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
456d71ae5a4SJacob Faibussowitsch {
457c688c046SMatthew G Knepley   PetscFunctionBegin;
458c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4594f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4609566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
462c688c046SMatthew G Knepley }
463c688c046SMatthew G Knepley 
464c688c046SMatthew G Knepley /*@
465bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
466c688c046SMatthew G Knepley 
46720f4b53cSBarry Smith   Not Collective
468c688c046SMatthew G Knepley 
469c688c046SMatthew G Knepley   Input Parameters:
47020f4b53cSBarry Smith + A  - The `Mat`
47120f4b53cSBarry Smith - dm - The `DM`
472c688c046SMatthew G Knepley 
473bb7acecfSBarry Smith   Level: developer
474c688c046SMatthew G Knepley 
475bb7acecfSBarry Smith   Note:
476bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
477bb7acecfSBarry Smith 
47873ff1848SBarry Smith   Developer Note:
479bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
480bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4818f1509bcSBarry Smith 
4821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
483c688c046SMatthew G Knepley @*/
484d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
485d71ae5a4SJacob Faibussowitsch {
486c688c046SMatthew G Knepley   PetscFunctionBegin;
487c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4888865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4899566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
491c688c046SMatthew G Knepley }
492c688c046SMatthew G Knepley 
4939a42bb27SBarry Smith /*@C
494bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4959a42bb27SBarry Smith 
49620f4b53cSBarry Smith   Logically Collective
4979a42bb27SBarry Smith 
498d8d19677SJose E. Roman   Input Parameters:
49960225df5SJacob Faibussowitsch + dm     - the `DM` context
500bb7acecfSBarry Smith - prefix - the prefix to prepend
5019a42bb27SBarry Smith 
50220f4b53cSBarry Smith   Level: advanced
50320f4b53cSBarry Smith 
50420f4b53cSBarry Smith   Note:
5059a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
5069a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
5079a42bb27SBarry Smith 
5081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
5099a42bb27SBarry Smith @*/
510d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
511d71ae5a4SJacob Faibussowitsch {
5129a42bb27SBarry Smith   PetscFunctionBegin;
5139a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5149566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5151baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5161baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5189a42bb27SBarry Smith }
5199a42bb27SBarry Smith 
52031697293SDave May /*@C
521da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
522bb7acecfSBarry Smith   `DM` options in the options database.
52331697293SDave May 
52420f4b53cSBarry Smith   Logically Collective
52531697293SDave May 
52631697293SDave May   Input Parameters:
527bb7acecfSBarry Smith + dm     - the `DM` context
528bb7acecfSBarry Smith - prefix - the string to append to the current prefix
52931697293SDave May 
53020f4b53cSBarry Smith   Level: advanced
53120f4b53cSBarry Smith 
53220f4b53cSBarry Smith   Note:
533bb7acecfSBarry 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.
53431697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
53531697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
53631697293SDave May 
5371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
53831697293SDave May @*/
539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
540d71ae5a4SJacob Faibussowitsch {
54131697293SDave May   PetscFunctionBegin;
54231697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5439566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54531697293SDave May }
54631697293SDave May 
54731697293SDave May /*@C
54831697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
549bb7acecfSBarry Smith   DM options in the options database.
55031697293SDave May 
55131697293SDave May   Not Collective
55231697293SDave May 
5532fe279fdSBarry Smith   Input Parameter:
554bb7acecfSBarry Smith . dm - the `DM` context
55531697293SDave May 
5562fe279fdSBarry Smith   Output Parameter:
55731697293SDave May . prefix - pointer to the prefix string used is returned
55831697293SDave May 
55931697293SDave May   Level: advanced
56031697293SDave May 
56173ff1848SBarry Smith   Fortran Note:
56220f4b53cSBarry Smith   Pass in a string 'prefix' of
56320f4b53cSBarry Smith   sufficient length to hold the prefix.
56420f4b53cSBarry Smith 
5651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
56631697293SDave May @*/
567d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
568d71ae5a4SJacob Faibussowitsch {
56931697293SDave May   PetscFunctionBegin;
57031697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57331697293SDave May }
57431697293SDave May 
57562e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
576d71ae5a4SJacob Faibussowitsch {
5776eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57888bdff64SToby Isaac 
57988bdff64SToby Isaac   PetscFunctionBegin;
580aab5bcd8SJed Brown   *ncrefct = 0;
58188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
58288bdff64SToby Isaac     refct--;
58388bdff64SToby Isaac     if (recurseCoarse) {
58488bdff64SToby Isaac       PetscInt coarseCount;
58588bdff64SToby Isaac 
58662e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
58788bdff64SToby Isaac       refct += coarseCount;
58888bdff64SToby Isaac     }
58988bdff64SToby Isaac   }
59088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
59188bdff64SToby Isaac     refct--;
59288bdff64SToby Isaac     if (recurseFine) {
59388bdff64SToby Isaac       PetscInt fineCount;
59488bdff64SToby Isaac 
59562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
59688bdff64SToby Isaac       refct += fineCount;
59788bdff64SToby Isaac     }
59888bdff64SToby Isaac   }
59988bdff64SToby Isaac   *ncrefct = refct;
6003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60188bdff64SToby Isaac }
60288bdff64SToby Isaac 
60362e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
60462e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
60562e5d2d2SJDBetteridge {
60662e5d2d2SJDBetteridge   PetscFunctionBegin;
60762e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60962e5d2d2SJDBetteridge }
61062e5d2d2SJDBetteridge 
611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
612d71ae5a4SJacob Faibussowitsch {
6135d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
614354557abSToby Isaac 
615354557abSToby Isaac   PetscFunctionBegin;
616354557abSToby Isaac   /* destroy the labels */
617354557abSToby Isaac   while (next) {
618354557abSToby Isaac     DMLabelLink tmp = next->next;
619354557abSToby Isaac 
6205d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
621ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6229566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6239566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
624354557abSToby Isaac     next = tmp;
625354557abSToby Isaac   }
6265d80c0bfSVaclav Hapla   dm->labels = NULL;
6273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
628354557abSToby Isaac }
629354557abSToby Isaac 
63066976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
631d71ae5a4SJacob Faibussowitsch {
6326858538eSMatthew G. Knepley   PetscFunctionBegin;
6336858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6346858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6356858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6366858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6376858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6396858538eSMatthew G. Knepley }
6406858538eSMatthew G. Knepley 
6411fb7b255SJunchao Zhang /*@C
642bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
64347c6ae99SBarry Smith 
64420f4b53cSBarry Smith   Collective
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith   Input Parameter:
647bb7acecfSBarry Smith . dm - the `DM` object to destroy
64847c6ae99SBarry Smith 
64947c6ae99SBarry Smith   Level: developer
65047c6ae99SBarry Smith 
6511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
65247c6ae99SBarry Smith @*/
653d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
654d71ae5a4SJacob Faibussowitsch {
6556eb26441SStefano Zampini   PetscInt cnt;
65647c6ae99SBarry Smith 
65747c6ae99SBarry Smith   PetscFunctionBegin;
6583ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
659f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
66087e657c6SBarry Smith 
66188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
66262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
663f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6649371c9d4SSatish Balay   if (--cnt > 0) {
6659371c9d4SSatish Balay     *dm = NULL;
6663ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6679371c9d4SSatish Balay   }
668f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
669f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6706eb26441SStefano Zampini 
6719566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6729566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
673974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
674974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6752348bcf4SPeter Brune 
676b17ce1afSJed Brown   /* Destroy the list of hooks */
677c833c3b5SJed Brown   {
678c833c3b5SJed Brown     DMCoarsenHookLink link, next;
679b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
680b17ce1afSJed Brown       next = link->next;
6819566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
682b17ce1afSJed Brown     }
6830298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
684c833c3b5SJed Brown   }
685c833c3b5SJed Brown   {
686c833c3b5SJed Brown     DMRefineHookLink link, next;
687c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
688c833c3b5SJed Brown       next = link->next;
6899566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
690c833c3b5SJed Brown     }
6910298fd71SBarry Smith     (*dm)->refinehook = NULL;
692c833c3b5SJed Brown   }
693be081cd6SPeter Brune   {
694be081cd6SPeter Brune     DMSubDomainHookLink link, next;
695be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
696be081cd6SPeter Brune       next = link->next;
6979566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
698be081cd6SPeter Brune     }
6990298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
700be081cd6SPeter Brune   }
701baf369e7SPeter Brune   {
702baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
703baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
704baf369e7SPeter Brune       next = link->next;
7059566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
706baf369e7SPeter Brune     }
7070298fd71SBarry Smith     (*dm)->gtolhook = NULL;
708baf369e7SPeter Brune   }
709d4d07f1eSToby Isaac   {
710d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
711d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
712d4d07f1eSToby Isaac       next = link->next;
7139566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
714d4d07f1eSToby Isaac     }
715d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
716d4d07f1eSToby Isaac   }
717aa1993deSMatthew G Knepley   /* Destroy the work arrays */
718aa1993deSMatthew G Knepley   {
719aa1993deSMatthew G Knepley     DMWorkLink link, next;
720936381afSPierre 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);
721aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
722aa1993deSMatthew G Knepley       next = link->next;
7239566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7249566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
725aa1993deSMatthew G Knepley     }
7260298fd71SBarry Smith     (*dm)->workin = NULL;
727aa1993deSMatthew G Knepley   }
728c58f1c22SToby Isaac   /* destroy the labels */
7299566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
730f4cdcedcSVaclav Hapla   /* destroy the fields */
7319566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
732f4cdcedcSVaclav Hapla   /* destroy the boundaries */
733e6f8dbb6SToby Isaac   {
734e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
735e6f8dbb6SToby Isaac     while (next) {
736e6f8dbb6SToby Isaac       DMBoundary b = next;
737e6f8dbb6SToby Isaac 
738e6f8dbb6SToby Isaac       next = b->next;
7399566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
740e6f8dbb6SToby Isaac     }
741e6f8dbb6SToby Isaac   }
742b17ce1afSJed Brown 
7439566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7449566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7459566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74652536dc3SBarry Smith 
74748a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7489566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7499566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7519566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
75288ed4aceSMatthew G Knepley 
7539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7549566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
755adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7569566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7579566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7589566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7599566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7609566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
76148a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7629566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
763e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7649566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
76548a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7666eb26441SStefano Zampini 
7679566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76848a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7699566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7704fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7719566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7729566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7736858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7746858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7759566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7779566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
778ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
779ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
780ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
781ddedc8f6SJames Wright   }
782ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7836636e97aSMatthew G Knepley 
7849566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7859566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
786e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7879566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
788732e2eb9SMatthew G Knepley 
789213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7909566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
791d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
792f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7939566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7949566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
795f918ec44SMatthew G. Knepley #endif
796435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7979566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79947c6ae99SBarry Smith }
80047c6ae99SBarry Smith 
801d7bf68aeSBarry Smith /*@
802bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
803d7bf68aeSBarry Smith 
80420f4b53cSBarry Smith   Collective
805d7bf68aeSBarry Smith 
806d7bf68aeSBarry Smith   Input Parameter:
807bb7acecfSBarry Smith . dm - the `DM` object to setup
808d7bf68aeSBarry Smith 
809bb7acecfSBarry Smith   Level: intermediate
810d7bf68aeSBarry Smith 
811bb7acecfSBarry Smith   Note:
812bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
813bb7acecfSBarry Smith 
8141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
815d7bf68aeSBarry Smith @*/
816d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
817d71ae5a4SJacob Faibussowitsch {
818d7bf68aeSBarry Smith   PetscFunctionBegin;
819171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8203ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
821dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8228387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
824d7bf68aeSBarry Smith }
825d7bf68aeSBarry Smith 
826d7bf68aeSBarry Smith /*@
827bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
828d7bf68aeSBarry Smith 
82920f4b53cSBarry Smith   Collective
830d7bf68aeSBarry Smith 
831d7bf68aeSBarry Smith   Input Parameter:
832bb7acecfSBarry Smith . dm - the `DM` object to set options for
833d7bf68aeSBarry Smith 
83420f4b53cSBarry Smith   Options Database Keys:
835bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
836bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
837bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
838a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83920f4b53cSBarry 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`
84020f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8419318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
842cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8435dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8449318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8459318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8469318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
847bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
848bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8499318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
850be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
851be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
852be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8539318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8549318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8559318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
856bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8579318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8589318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8599318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8609318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
861bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8629318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8639318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8649318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8659318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
866bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
872909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
873909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8749318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8759318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8769318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87720f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
878d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87920f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
880bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
881bb7acecfSBarry 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()`
882bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
883bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
884bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
885384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
886d7bf68aeSBarry Smith 
88795eb5ee5SVaclav Hapla   Level: intermediate
88895eb5ee5SVaclav Hapla 
8891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
890bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
89160225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
892d7bf68aeSBarry Smith @*/
893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
894d71ae5a4SJacob Faibussowitsch {
8957781c08eSBarry Smith   char      typeName[256];
896ca266f36SBarry Smith   PetscBool flg;
897d7bf68aeSBarry Smith 
898d7bf68aeSBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9019566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9029566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
903dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
904d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9071baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9091baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
910863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9129566063dSJacob 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));
913eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
914dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
915f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
916dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
917d0609cedSBarry Smith   PetscOptionsEnd();
9183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
919d7bf68aeSBarry Smith }
920d7bf68aeSBarry Smith 
921fc9bc008SSatish Balay /*@C
922bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
923fe2efc57SMark 
92420f4b53cSBarry Smith   Collective
925fe2efc57SMark 
926fe2efc57SMark   Input Parameters:
927bb7acecfSBarry Smith + dm   - the `DM` object
92820f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
92960225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
930fe2efc57SMark 
931fe2efc57SMark   Level: intermediate
932bb7acecfSBarry Smith 
933bb7acecfSBarry Smith   Note:
934bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
935bb7acecfSBarry Smith 
93660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
937fe2efc57SMark @*/
938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
939d71ae5a4SJacob Faibussowitsch {
940fe2efc57SMark   PetscFunctionBegin;
941fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9429566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
944fe2efc57SMark }
945fe2efc57SMark 
946fe2efc57SMark /*@C
947bb7acecfSBarry 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
948bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94947c6ae99SBarry Smith 
95020f4b53cSBarry Smith   Collective
95147c6ae99SBarry Smith 
952d8d19677SJose E. Roman   Input Parameters:
953bb7acecfSBarry Smith + dm - the `DM` object to view
95447c6ae99SBarry Smith - v  - the viewer
95547c6ae99SBarry Smith 
95620f4b53cSBarry Smith   Level: beginner
95720f4b53cSBarry Smith 
95873ff1848SBarry Smith   Note:
959bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
960bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
961bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
962cd7e8a5eSksagiyam 
9631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96447c6ae99SBarry Smith @*/
965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
966d71ae5a4SJacob Faibussowitsch {
96732c0f0efSBarry Smith   PetscBool         isbinary;
96876a8abe0SBarry Smith   PetscMPIInt       size;
96976a8abe0SBarry Smith   PetscViewerFormat format;
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith   PetscFunctionBegin;
972171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97348a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
974b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97574903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97674903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97774903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97874903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97974903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98074903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98174903a4fSStefano Zampini      in an error here */
98274903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9839566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
984b1b135c8SBarry Smith 
9859566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9873ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9899566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
99032c0f0efSBarry Smith   if (isbinary) {
99155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99232c0f0efSBarry Smith     char     type[256];
99332c0f0efSBarry Smith 
9949566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
995c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9969566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99732c0f0efSBarry Smith   }
998dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
100047c6ae99SBarry Smith }
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith /*@
1003bb7acecfSBarry 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,
1004bb7acecfSBarry Smith   that is it has no ghost locations.
100547c6ae99SBarry Smith 
100620f4b53cSBarry Smith   Collective
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith   Input Parameter:
1009bb7acecfSBarry Smith . dm - the `DM` object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith   Output Parameter:
101247c6ae99SBarry Smith . vec - the global vector
101347c6ae99SBarry Smith 
1014073dac72SJed Brown   Level: beginner
101547c6ae99SBarry Smith 
10161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1017bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101847c6ae99SBarry Smith @*/
1019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1020d71ae5a4SJacob Faibussowitsch {
102147c6ae99SBarry Smith   PetscFunctionBegin;
1022171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10234f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1024dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1026c6b011d8SStefano Zampini     DM vdm;
1027c6b011d8SStefano Zampini 
10289566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10297a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1030c6b011d8SStefano Zampini   }
10313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103247c6ae99SBarry Smith }
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith /*@
1035bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith   Not Collective
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith   Input Parameter:
1040bb7acecfSBarry Smith . dm - the `DM` object
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith   Output Parameter:
104347c6ae99SBarry Smith . vec - the local vector
104447c6ae99SBarry Smith 
1045073dac72SJed Brown   Level: beginner
104647c6ae99SBarry Smith 
104720f4b53cSBarry Smith   Note:
1048bb7acecfSBarry 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.
1049bb7acecfSBarry Smith 
10501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1051bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105247c6ae99SBarry Smith @*/
1053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1054d71ae5a4SJacob Faibussowitsch {
105547c6ae99SBarry Smith   PetscFunctionBegin;
1056171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10574f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1058dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1060c6b011d8SStefano Zampini     DM vdm;
1061c6b011d8SStefano Zampini 
10629566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10637a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1064c6b011d8SStefano Zampini   }
10653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106647c6ae99SBarry Smith }
106747c6ae99SBarry Smith 
10681411c6eeSJed Brown /*@
1069bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10701411c6eeSJed Brown 
107120f4b53cSBarry Smith   Collective
10721411c6eeSJed Brown 
10731411c6eeSJed Brown   Input Parameter:
1074bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10751411c6eeSJed Brown 
10761411c6eeSJed Brown   Output Parameter:
10771411c6eeSJed Brown . ltog - the mapping
10781411c6eeSJed Brown 
1079bb7acecfSBarry Smith   Level: advanced
10801411c6eeSJed Brown 
10811411c6eeSJed Brown   Notes:
1082bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10831411c6eeSJed Brown 
1084bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1085bb7acecfSBarry Smith   need to use this function with those objects.
1086bb7acecfSBarry Smith 
1087bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1088bb7acecfSBarry Smith 
108960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1090bb7acecfSBarry Smith           `DMCreateMatrix()`
10911411c6eeSJed Brown @*/
1092d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1093d71ae5a4SJacob Faibussowitsch {
10940be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10951411c6eeSJed Brown 
10961411c6eeSJed Brown   PetscFunctionBegin;
10971411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10984f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10991411c6eeSJed Brown   if (!dm->ltogmap) {
110037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110137d0c07bSMatthew G Knepley 
11029566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110337d0c07bSMatthew G Knepley     if (section) {
1104a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1106ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110737d0c07bSMatthew G Knepley 
11089566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11109566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11119566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1113e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111437d0c07bSMatthew G Knepley 
111537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11201a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11211a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1122ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11235227eafbSStefano Zampini 
1124e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11255227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1126e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1127e6befd46SJed Brown             cind++;
1128e6befd46SJed Brown           } else {
11295227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1130e6befd46SJed Brown           }
113137d0c07bSMatthew G Knepley         }
113237d0c07bSMatthew G Knepley       }
1133bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11349371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11359371c9d4SSatish Balay       bsLocal[1] = bs;
11369566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11379371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11389371c9d4SSatish Balay         bs = 1;
11399371c9d4SSatish Balay       } else {
11409371c9d4SSatish Balay         bs = bsMinMax[0];
11419371c9d4SSatish Balay       }
11427591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11437591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1144ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1145ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1146ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1147ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1148ca469d19SJed Brown         }
1149ccf3bd66SMatthew G. Knepley         n /= bs;
1150ccf3bd66SMatthew G. Knepley       }
11519566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1152dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115337d0c07bSMatthew G Knepley   }
11541411c6eeSJed Brown   *ltog = dm->ltogmap;
11553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11561411c6eeSJed Brown }
11571411c6eeSJed Brown 
11581411c6eeSJed Brown /*@
1159bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11601411c6eeSJed Brown 
11611411c6eeSJed Brown   Not Collective
11621411c6eeSJed Brown 
11631411c6eeSJed Brown   Input Parameter:
1164bb7acecfSBarry Smith . dm - the `DM` with block structure
11651411c6eeSJed Brown 
11661411c6eeSJed Brown   Output Parameter:
11671411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11681411c6eeSJed Brown 
11691411c6eeSJed Brown   Level: intermediate
11701411c6eeSJed Brown 
117173ff1848SBarry Smith   Notes:
1172bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1173bb7acecfSBarry Smith 
1174bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1175bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1176bb7acecfSBarry Smith 
11771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11781411c6eeSJed Brown @*/
1179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1180d71ae5a4SJacob Faibussowitsch {
11811411c6eeSJed Brown   PetscFunctionBegin;
11821411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11834f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11847a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11851411c6eeSJed Brown   *bs = dm->bs;
11863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11871411c6eeSJed Brown }
11881411c6eeSJed Brown 
118948eeb7c8SBarry Smith /*@C
1190bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1191bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119247c6ae99SBarry Smith 
119320f4b53cSBarry Smith   Collective
119447c6ae99SBarry Smith 
1195d8d19677SJose E. Roman   Input Parameters:
1196bb7acecfSBarry Smith + dmc - the `DM` object
1197bb7acecfSBarry Smith - dmf - the second, finer `DM` object
119847c6ae99SBarry Smith 
1199d8d19677SJose E. Roman   Output Parameters:
120047c6ae99SBarry Smith + mat - the interpolation
1201b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith   Level: developer
120447c6ae99SBarry Smith 
120595452b02SPatrick Sanan   Notes:
1206bb7acecfSBarry 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
1207bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1208d52bd9f3SBarry Smith 
1209bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1210bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121185afcc9aSBarry Smith 
12121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121347c6ae99SBarry Smith @*/
1214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1215d71ae5a4SJacob Faibussowitsch {
121647c6ae99SBarry Smith   PetscFunctionBegin;
1217a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1218a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12194f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1221dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122447c6ae99SBarry Smith }
122547c6ae99SBarry Smith 
12263ad4599aSBarry Smith /*@
1227a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1228a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12292ed6491fSPatrick Sanan 
12302ed6491fSPatrick Sanan   Input Parameters:
1231bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1232bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12332ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   Output Parameter:
12362ed6491fSPatrick Sanan . scale - the scaled vector
12372ed6491fSPatrick Sanan 
1238bb7acecfSBarry Smith   Level: advanced
12392ed6491fSPatrick Sanan 
124073ff1848SBarry Smith   Note:
1241a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1242a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1243a4e35b19SJacob Faibussowitsch 
124473ff1848SBarry Smith   Developer Note:
1245bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1246e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1247e9c74fd6SRichard Tran Mills 
124860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12492ed6491fSPatrick Sanan @*/
1250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1251d71ae5a4SJacob Faibussowitsch {
12522ed6491fSPatrick Sanan   Vec         fine;
12532ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12549704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1255e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12569704db99SRichard Tran Mills #endif
12572ed6491fSPatrick Sanan 
12582ed6491fSPatrick Sanan   PetscFunctionBegin;
12599566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12609566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12619566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12629704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12639704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12649704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12659704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12669566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1267e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12689566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12699566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12709566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
127183aa49f4SRichard Tran Mills   }
12729704db99SRichard Tran Mills #endif
12739566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12759566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12772ed6491fSPatrick Sanan }
12782ed6491fSPatrick Sanan 
12792ed6491fSPatrick Sanan /*@
1280bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1281bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12823ad4599aSBarry Smith 
128320f4b53cSBarry Smith   Collective
12843ad4599aSBarry Smith 
1285d8d19677SJose E. Roman   Input Parameters:
1286bb7acecfSBarry Smith + dmc - the `DM` object
1287bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith   Output Parameter:
12903ad4599aSBarry Smith . mat - the restriction
12913ad4599aSBarry Smith 
12923ad4599aSBarry Smith   Level: developer
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith   Note:
1295bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1296bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12973ad4599aSBarry Smith 
12981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12993ad4599aSBarry Smith @*/
1300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1301d71ae5a4SJacob Faibussowitsch {
13023ad4599aSBarry Smith   PetscFunctionBegin;
1303a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13054f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13103ad4599aSBarry Smith }
13113ad4599aSBarry Smith 
131247c6ae99SBarry Smith /*@
1313a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
131447c6ae99SBarry Smith 
131520f4b53cSBarry Smith   Collective
131647c6ae99SBarry Smith 
1317d8d19677SJose E. Roman   Input Parameters:
1318bb7acecfSBarry Smith + dac - the `DM` object
1319bb7acecfSBarry Smith - daf - the second, finer `DM` object
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith   Output Parameter:
13226dbf9973SLawrence Mitchell . mat - the injection
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith   Level: developer
132547c6ae99SBarry Smith 
1326a4e35b19SJacob Faibussowitsch   Notes:
1327a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1328a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1329a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1330a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1331a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1332a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1333a4e35b19SJacob Faibussowitsch 
1334bb7acecfSBarry 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
1335bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133685afcc9aSBarry Smith 
13371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1338bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133947c6ae99SBarry Smith @*/
1340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1341d71ae5a4SJacob Faibussowitsch {
134247c6ae99SBarry Smith   PetscFunctionBegin;
1343a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1344a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13454f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1347dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13489566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
135047c6ae99SBarry Smith }
135147c6ae99SBarry Smith 
1352b412c318SBarry Smith /*@
1353bb7acecfSBarry 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
1354bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1355bd041c0cSMatthew G. Knepley 
135620f4b53cSBarry Smith   Collective
1357bd041c0cSMatthew G. Knepley 
1358d8d19677SJose E. Roman   Input Parameters:
1359bb7acecfSBarry Smith + dmc - the target `DM` object
1360bb7acecfSBarry Smith - dmf - the source `DM` object
1361bd041c0cSMatthew G. Knepley 
1362bd041c0cSMatthew G. Knepley   Output Parameter:
1363b4937a87SMatthew G. Knepley . mat - the mass matrix
1364bd041c0cSMatthew G. Knepley 
1365bd041c0cSMatthew G. Knepley   Level: developer
1366bd041c0cSMatthew G. Knepley 
1367bb7acecfSBarry Smith   Notes:
1368bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1369bb7acecfSBarry Smith 
137020f4b53cSBarry 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()`
1371bb7acecfSBarry Smith 
137242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1373bd041c0cSMatthew G. Knepley @*/
1374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1375d71ae5a4SJacob Faibussowitsch {
1376bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1377b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1378b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13794f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13805b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1381dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13825b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1384b4937a87SMatthew G. Knepley }
1385b4937a87SMatthew G. Knepley 
1386b4937a87SMatthew G. Knepley /*@
1387bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1388b4937a87SMatthew G. Knepley 
138920f4b53cSBarry Smith   Collective
1390b4937a87SMatthew G. Knepley 
1391b4937a87SMatthew G. Knepley   Input Parameter:
1392bb7acecfSBarry Smith . dm - the `DM` object
1393b4937a87SMatthew G. Knepley 
1394b4937a87SMatthew G. Knepley   Output Parameter:
1395bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1396b4937a87SMatthew G. Knepley 
1397b4937a87SMatthew G. Knepley   Level: developer
1398b4937a87SMatthew G. Knepley 
1399bb7acecfSBarry Smith   Note:
1400bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1401bb7acecfSBarry Smith 
140260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1403b4937a87SMatthew G. Knepley @*/
1404d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1405d71ae5a4SJacob Faibussowitsch {
1406b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1407b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14084f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1409dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1411bd041c0cSMatthew G. Knepley }
1412bd041c0cSMatthew G. Knepley 
1413bd041c0cSMatthew G. Knepley /*@
1414bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1415bb7acecfSBarry Smith   of a PDE on the `DM`.
141647c6ae99SBarry Smith 
141720f4b53cSBarry Smith   Collective
141847c6ae99SBarry Smith 
1419d8d19677SJose E. Roman   Input Parameters:
1420bb7acecfSBarry Smith + dm    - the `DM` object
1421bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
142247c6ae99SBarry Smith 
142347c6ae99SBarry Smith   Output Parameter:
142447c6ae99SBarry Smith . coloring - the coloring
142547c6ae99SBarry Smith 
14261bf8429eSBarry Smith   Level: developer
14271bf8429eSBarry Smith 
1428ec5066bdSBarry Smith   Notes:
1429bb7acecfSBarry 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
1430bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1431ec5066bdSBarry Smith 
1432bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14331bf8429eSBarry 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,
14341bf8429eSBarry Smith   otherwise an error will be generated.
1435ec5066bdSBarry Smith 
14361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1437aab9d709SJed Brown @*/
1438d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1439d71ae5a4SJacob Faibussowitsch {
144047c6ae99SBarry Smith   PetscFunctionBegin;
1441171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14424f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1443dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
144547c6ae99SBarry Smith }
144647c6ae99SBarry Smith 
1447b412c318SBarry Smith /*@
1448bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144947c6ae99SBarry Smith 
145020f4b53cSBarry Smith   Collective
145147c6ae99SBarry Smith 
145247c6ae99SBarry Smith   Input Parameter:
1453bb7acecfSBarry Smith . dm - the `DM` object
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith   Output Parameter:
145647c6ae99SBarry Smith . mat - the empty Jacobian
145747c6ae99SBarry Smith 
145820f4b53cSBarry Smith   Options Database Key:
1459bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1460f27dd7c6SMatthew G. Knepley 
146120f4b53cSBarry Smith   Level: beginner
146220f4b53cSBarry Smith 
146395452b02SPatrick Sanan   Notes:
146495452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
146594013140SBarry Smith   do not need to do it yourself.
146694013140SBarry Smith 
146794013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1468bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146994013140SBarry Smith 
1470bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
147194013140SBarry Smith   internally by PETSc.
147294013140SBarry Smith 
1473bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1474bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
147594013140SBarry Smith 
14761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1477aab9d709SJed Brown @*/
1478d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1479d71ae5a4SJacob Faibussowitsch {
148047c6ae99SBarry Smith   PetscFunctionBegin;
1481171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14824f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14839566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1485dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1487c6b011d8SStefano Zampini     DM mdm;
1488c6b011d8SStefano Zampini 
14899566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14907a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1491c6b011d8SStefano Zampini   }
1492e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1493e5e52638SMatthew G. Knepley   if (dm->Nf) {
1494e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1495649ef022SMatthew Knepley     PetscInt     Nf, f;
1496e571a35bSMatthew G. Knepley 
14979566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1498649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1499649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15009566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15019566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15029566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1503649ef022SMatthew Knepley         break;
1504e571a35bSMatthew G. Knepley       }
1505649ef022SMatthew Knepley     }
1506649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1507649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15089566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15099566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15109566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1511e571a35bSMatthew G. Knepley       }
1512e571a35bSMatthew G. Knepley     }
1513e571a35bSMatthew G. Knepley   }
15149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151647c6ae99SBarry Smith }
151747c6ae99SBarry Smith 
1518732e2eb9SMatthew G Knepley /*@
1519a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1520a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1521a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1522aa0f6e3cSJed Brown 
152320f4b53cSBarry Smith   Logically Collective
1524aa0f6e3cSJed Brown 
1525aa0f6e3cSJed Brown   Input Parameters:
1526bb7acecfSBarry Smith + dm   - the `DM`
1527bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1528aa0f6e3cSJed Brown 
1529aa0f6e3cSJed Brown   Level: developer
1530aa0f6e3cSJed Brown 
153173ff1848SBarry Smith   Note:
1532a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1533a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1534a4e35b19SJacob Faibussowitsch 
15351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1536aa0f6e3cSJed Brown @*/
1537d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1538d71ae5a4SJacob Faibussowitsch {
1539aa0f6e3cSJed Brown   PetscFunctionBegin;
1540aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1541aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1543aa0f6e3cSJed Brown }
1544aa0f6e3cSJed Brown 
1545aa0f6e3cSJed Brown /*@
1546bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1547732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1548732e2eb9SMatthew G Knepley 
154920f4b53cSBarry Smith   Logically Collective
1550732e2eb9SMatthew G Knepley 
1551d8d19677SJose E. Roman   Input Parameters:
1552bb7acecfSBarry Smith + dm   - the `DM`
1553bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1554732e2eb9SMatthew G Knepley 
155520f4b53cSBarry Smith   Options Database Key:
1556bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1557f27dd7c6SMatthew G. Knepley 
155820f4b53cSBarry Smith   Level: developer
155920f4b53cSBarry Smith 
15601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1561732e2eb9SMatthew G Knepley @*/
1562d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1563d71ae5a4SJacob Faibussowitsch {
1564732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1565732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1566732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1568732e2eb9SMatthew G Knepley }
1569732e2eb9SMatthew G Knepley 
1570b06ff27eSHong Zhang /*@
1571bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1572bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1573b06ff27eSHong Zhang 
157420f4b53cSBarry Smith   Logically Collective
1575b06ff27eSHong Zhang 
1576d8d19677SJose E. Roman   Input Parameters:
1577bb7acecfSBarry Smith + dm   - the `DM`
1578da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1579b06ff27eSHong Zhang 
1580b06ff27eSHong Zhang   Level: developer
1581bb7acecfSBarry Smith 
15821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1583b06ff27eSHong Zhang @*/
1584d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1585d71ae5a4SJacob Faibussowitsch {
1586b06ff27eSHong Zhang   PetscFunctionBegin;
1587b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1588b06ff27eSHong Zhang   dm->structure_only = only;
15893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1590b06ff27eSHong Zhang }
1591b06ff27eSHong Zhang 
1592863027abSJed Brown /*@
1593863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1594863027abSJed Brown 
159520f4b53cSBarry Smith   Logically Collective
1596863027abSJed Brown 
1597863027abSJed Brown   Input Parameters:
1598863027abSJed Brown + dm    - the `DM`
1599863027abSJed Brown - btype - block by topological point or field node
1600863027abSJed Brown 
160120f4b53cSBarry Smith   Options Database Key:
16020e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1603863027abSJed Brown 
160420f4b53cSBarry Smith   Level: advanced
160520f4b53cSBarry Smith 
16061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1607863027abSJed Brown @*/
1608863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1609863027abSJed Brown {
1610863027abSJed Brown   PetscFunctionBegin;
1611863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1612863027abSJed Brown   dm->blocking_type = btype;
16133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1614863027abSJed Brown }
1615863027abSJed Brown 
1616863027abSJed Brown /*@
1617863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1618863027abSJed Brown 
1619863027abSJed Brown   Not Collective
1620863027abSJed Brown 
16212fe279fdSBarry Smith   Input Parameter:
1622863027abSJed Brown . dm - the `DM`
1623863027abSJed Brown 
16242fe279fdSBarry Smith   Output Parameter:
1625863027abSJed Brown . btype - block by topological point or field node
1626863027abSJed Brown 
1627863027abSJed Brown   Level: advanced
1628863027abSJed Brown 
16291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1630863027abSJed Brown @*/
1631863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1632863027abSJed Brown {
1633863027abSJed Brown   PetscFunctionBegin;
1634863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16354f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1636863027abSJed Brown   *btype = dm->blocking_type;
16373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1638863027abSJed Brown }
1639863027abSJed Brown 
1640a89ea682SMatthew G Knepley /*@C
1641bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1642a89ea682SMatthew G Knepley 
1643a89ea682SMatthew G Knepley   Not Collective
1644a89ea682SMatthew G Knepley 
1645a89ea682SMatthew G Knepley   Input Parameters:
1646bb7acecfSBarry Smith + dm    - the `DM` object
1647a5b23f4aSJose E. Roman . count - The minimum size
164820f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1649a89ea682SMatthew G Knepley 
1650a89ea682SMatthew G Knepley   Output Parameter:
165160225df5SJacob Faibussowitsch . mem - the work array
1652a89ea682SMatthew G Knepley 
1653a89ea682SMatthew G Knepley   Level: developer
1654a89ea682SMatthew G Knepley 
165573ff1848SBarry Smith   Notes:
1656da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1657bb7acecfSBarry Smith 
1658bb7acecfSBarry Smith   The array may contain nonzero values
1659bb7acecfSBarry Smith 
16601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1661a89ea682SMatthew G Knepley @*/
1662d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1663d71ae5a4SJacob Faibussowitsch {
1664aa1993deSMatthew G Knepley   DMWorkLink  link;
166569291d52SBarry Smith   PetscMPIInt dsize;
1666a89ea682SMatthew G Knepley 
1667a89ea682SMatthew G Knepley   PetscFunctionBegin;
1668a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16694f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1670442f3b32SStefano Zampini   if (!count) {
1671442f3b32SStefano Zampini     *(void **)mem = NULL;
1672442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1673442f3b32SStefano Zampini   }
1674aa1993deSMatthew G Knepley   if (dm->workin) {
1675aa1993deSMatthew G Knepley     link       = dm->workin;
1676aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1677aa1993deSMatthew G Knepley   } else {
16784dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1679a89ea682SMatthew G Knepley   }
1680b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1681b77fa653SStefano Zampini      Get size directly */
1682b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1683b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1684b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1685b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1686b77fa653SStefano Zampini #endif
1687b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1688b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1689b77fa653SStefano Zampini #endif
1690b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1691b77fa653SStefano Zampini 
16925056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16939566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1695854ce69bSBarry Smith     link->bytes = dsize * count;
1696aa1993deSMatthew G Knepley   }
1697aa1993deSMatthew G Knepley   link->next  = dm->workout;
1698aa1993deSMatthew G Knepley   dm->workout = link;
1699cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
170000d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
170100d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
170200d952a4SJed Brown #endif
1703aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1705a89ea682SMatthew G Knepley }
1706a89ea682SMatthew G Knepley 
1707aa1993deSMatthew G Knepley /*@C
1708bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1709aa1993deSMatthew G Knepley 
1710aa1993deSMatthew G Knepley   Not Collective
1711aa1993deSMatthew G Knepley 
1712aa1993deSMatthew G Knepley   Input Parameters:
1713bb7acecfSBarry Smith + dm    - the `DM` object
1714a5b23f4aSJose E. Roman . count - The minimum size
171520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1716aa1993deSMatthew G Knepley 
1717aa1993deSMatthew G Knepley   Output Parameter:
171860225df5SJacob Faibussowitsch . mem - the work array
1719aa1993deSMatthew G Knepley 
1720aa1993deSMatthew G Knepley   Level: developer
1721aa1993deSMatthew G Knepley 
172273ff1848SBarry Smith   Developer Note:
1723bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1724147403d9SBarry Smith 
17251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1726aa1993deSMatthew G Knepley @*/
1727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1728d71ae5a4SJacob Faibussowitsch {
1729aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1730aa1993deSMatthew G Knepley 
1731aa1993deSMatthew G Knepley   PetscFunctionBegin;
1732aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17334f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1734a4e35b19SJacob Faibussowitsch   (void)count;
1735a4e35b19SJacob Faibussowitsch   (void)dtype;
1736442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1737aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1738aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1739aa1993deSMatthew G Knepley       *p            = link->next;
1740aa1993deSMatthew G Knepley       link->next    = dm->workin;
1741aa1993deSMatthew G Knepley       dm->workin    = link;
17420298fd71SBarry Smith       *(void **)mem = NULL;
17433ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1744aa1993deSMatthew G Knepley     }
1745aa1993deSMatthew G Knepley   }
1746aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1747aa1993deSMatthew G Knepley }
1748e7c4fc90SDmitry Karpeev 
17498cda7954SMatthew G. Knepley /*@C
1750bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1751bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17528cda7954SMatthew G. Knepley 
175320f4b53cSBarry Smith   Logically Collective; No Fortran Support
17548cda7954SMatthew G. Knepley 
17558cda7954SMatthew G. Knepley   Input Parameters:
1756bb7acecfSBarry Smith + dm     - The `DM`
17578cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17588cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17598cda7954SMatthew G. Knepley 
176020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1761bb7acecfSBarry Smith + dm        - The present `DM`
1762bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1763147403d9SBarry Smith . field     - The field number in dm
1764147403d9SBarry Smith - nullSpace - The nullspace for the given field
17658cda7954SMatthew G. Knepley 
176649762cbcSSatish Balay   Level: intermediate
176749762cbcSSatish Balay 
17681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1769147403d9SBarry Smith @*/
1770a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1771d71ae5a4SJacob Faibussowitsch {
1772435a35e8SMatthew G Knepley   PetscFunctionBegin;
1773435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17747a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1775435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1777435a35e8SMatthew G Knepley }
1778435a35e8SMatthew G Knepley 
17798cda7954SMatthew G. Knepley /*@C
1780bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17818cda7954SMatthew G. Knepley 
178220f4b53cSBarry Smith   Not Collective; No Fortran Support
17838cda7954SMatthew G. Knepley 
17848cda7954SMatthew G. Knepley   Input Parameters:
1785bb7acecfSBarry Smith + dm    - The `DM`
17868cda7954SMatthew G. Knepley - field - The field number for the nullspace
17878cda7954SMatthew G. Knepley 
17888cda7954SMatthew G. Knepley   Output Parameter:
17898cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17908cda7954SMatthew G. Knepley 
179120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1792147403d9SBarry Smith + dm        - The present DM
1793147403d9SBarry Smith . origField - The field number given above, in the original DM
1794147403d9SBarry Smith . field     - The field number in dm
1795147403d9SBarry Smith - nullSpace - The nullspace for the given field
17968cda7954SMatthew G. Knepley 
179749762cbcSSatish Balay   Level: intermediate
179849762cbcSSatish Balay 
17991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1800147403d9SBarry Smith @*/
1801a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1802d71ae5a4SJacob Faibussowitsch {
18030a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18040a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18054f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18067a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18070a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18090a50eb56SMatthew G. Knepley }
18100a50eb56SMatthew G. Knepley 
18118cda7954SMatthew G. Knepley /*@C
1812bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18138cda7954SMatthew G. Knepley 
181420f4b53cSBarry Smith   Logically Collective; No Fortran Support
18158cda7954SMatthew G. Knepley 
18168cda7954SMatthew G. Knepley   Input Parameters:
1817bb7acecfSBarry Smith + dm     - The `DM`
18188cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18198cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18208cda7954SMatthew G. Knepley 
182120f4b53cSBarry Smith   Calling sequence of `nullsp`:
1822bb7acecfSBarry Smith + dm        - The present `DM`
1823bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1824147403d9SBarry Smith . field     - The field number in dm
1825147403d9SBarry Smith - nullSpace - The nullspace for the given field
18268cda7954SMatthew G. Knepley 
182749762cbcSSatish Balay   Level: intermediate
182849762cbcSSatish Balay 
18291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1830bb7acecfSBarry Smith           `MatNullSpace`
1831147403d9SBarry Smith @*/
1832a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1833d71ae5a4SJacob Faibussowitsch {
1834f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1835f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18367a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1837f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1839f9d4088aSMatthew G. Knepley }
1840f9d4088aSMatthew G. Knepley 
18418cda7954SMatthew G. Knepley /*@C
1842bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18438cda7954SMatthew G. Knepley 
184420f4b53cSBarry Smith   Not Collective; No Fortran Support
18458cda7954SMatthew G. Knepley 
18468cda7954SMatthew G. Knepley   Input Parameters:
1847bb7acecfSBarry Smith + dm    - The `DM`
18488cda7954SMatthew G. Knepley - field - The field number for the nullspace
18498cda7954SMatthew G. Knepley 
18508cda7954SMatthew G. Knepley   Output Parameter:
18518cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18528cda7954SMatthew G. Knepley 
185320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1854bb7acecfSBarry Smith + dm        - The present `DM`
1855bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1856147403d9SBarry Smith . field     - The field number in dm
1857147403d9SBarry Smith - nullSpace - The nullspace for the given field
18588cda7954SMatthew G. Knepley 
185949762cbcSSatish Balay   Level: intermediate
186049762cbcSSatish Balay 
18611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1862bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1863147403d9SBarry Smith @*/
1864a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1865d71ae5a4SJacob Faibussowitsch {
1866f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1867f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18684f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18697a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1870f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1872f9d4088aSMatthew G. Knepley }
1873f9d4088aSMatthew G. Knepley 
18744f3b5142SJed Brown /*@C
1875bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18764d343eeaSMatthew G Knepley 
187720f4b53cSBarry Smith   Not Collective; No Fortran Support
18784d343eeaSMatthew G Knepley 
18794d343eeaSMatthew G Knepley   Input Parameter:
1880bb7acecfSBarry Smith . dm - the `DM` object
18814d343eeaSMatthew G Knepley 
18824d343eeaSMatthew G Knepley   Output Parameters:
188320f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188420f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188520f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18864d343eeaSMatthew G Knepley 
18874d343eeaSMatthew G Knepley   Level: intermediate
18884d343eeaSMatthew G Knepley 
1889bb7acecfSBarry Smith   Note:
189073ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
189173ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1892bb7acecfSBarry Smith   `PetscFree()`.
189321c9b008SJed Brown 
189473ff1848SBarry Smith   Developer Note:
1895bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1896bb7acecfSBarry Smith   likely be removed.
1897bb7acecfSBarry Smith 
18981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1899bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19004d343eeaSMatthew G Knepley @*/
1901d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1902d71ae5a4SJacob Faibussowitsch {
190337d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19044d343eeaSMatthew G Knepley 
19054d343eeaSMatthew G Knepley   PetscFunctionBegin;
19064d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190769ca1f37SDmitry Karpeev   if (numFields) {
19084f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
190969ca1f37SDmitry Karpeev     *numFields = 0;
191069ca1f37SDmitry Karpeev   }
191137d0c07bSMatthew G Knepley   if (fieldNames) {
19124f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19130298fd71SBarry Smith     *fieldNames = NULL;
191469ca1f37SDmitry Karpeev   }
191569ca1f37SDmitry Karpeev   if (fields) {
19164f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19170298fd71SBarry Smith     *fields = NULL;
191869ca1f37SDmitry Karpeev   }
19199566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
192037d0c07bSMatthew G Knepley   if (section) {
19213a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
192237d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192337d0c07bSMatthew G Knepley 
19249566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19259566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19279566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
193137d0c07bSMatthew G Knepley     }
193237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193337d0c07bSMatthew G Knepley       PetscInt gdof;
193437d0c07bSMatthew G Knepley 
19359566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193637d0c07bSMatthew G Knepley       if (gdof > 0) {
193737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19383a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193937d0c07bSMatthew G Knepley 
19409566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19419566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19423a544194SStefano Zampini           fpdof = fdof - fcdof;
19433a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19443a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19453a544194SStefano Zampini             fieldNc[f] = 1;
19463a544194SStefano Zampini           }
19473a544194SStefano Zampini           fieldSizes[f] += fpdof;
194837d0c07bSMatthew G Knepley         }
194937d0c07bSMatthew G Knepley       }
195037d0c07bSMatthew G Knepley     }
195137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19529566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195437d0c07bSMatthew G Knepley     }
195537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195737d0c07bSMatthew G Knepley 
19589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195937d0c07bSMatthew G Knepley       if (gdof > 0) {
19609566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
196137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
196237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196337d0c07bSMatthew G Knepley 
19649566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19659566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1966ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196737d0c07bSMatthew G Knepley         }
196837d0c07bSMatthew G Knepley       }
196937d0c07bSMatthew G Knepley     }
19708865f1eaSKarl Rupp     if (numFields) *numFields = nF;
197137d0c07bSMatthew G Knepley     if (fieldNames) {
19729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197437d0c07bSMatthew G Knepley         const char *fieldName;
197537d0c07bSMatthew G Knepley 
19769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19779566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197837d0c07bSMatthew G Knepley       }
197937d0c07bSMatthew G Knepley     }
198037d0c07bSMatthew G Knepley     if (fields) {
19819566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
198237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19833a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19843a544194SStefano Zampini 
19859566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19863a544194SStefano Zampini         in[0] = -fieldNc[f];
19873a544194SStefano Zampini         in[1] = fieldNc[f];
19881c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19893a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19909566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
199137d0c07bSMatthew G Knepley       }
199237d0c07bSMatthew G Knepley     }
19939566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1994dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19964d343eeaSMatthew G Knepley }
19974d343eeaSMatthew G Knepley 
199816621825SDmitry Karpeev /*@C
1999bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2000a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2001e7c4fc90SDmitry Karpeev 
200220f4b53cSBarry Smith   Not Collective; No Fortran Support
2003e7c4fc90SDmitry Karpeev 
2004e7c4fc90SDmitry Karpeev   Input Parameter:
2005bb7acecfSBarry Smith . dm - the `DM` object
2006e7c4fc90SDmitry Karpeev 
2007e7c4fc90SDmitry Karpeev   Output Parameters:
200820f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
200920f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
201020f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
201120f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2012e7c4fc90SDmitry Karpeev 
2013e7c4fc90SDmitry Karpeev   Level: intermediate
2014e7c4fc90SDmitry Karpeev 
2015a4e35b19SJacob Faibussowitsch   Notes:
2016a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2017a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2018a4e35b19SJacob Faibussowitsch 
2019a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2020a4e35b19SJacob Faibussowitsch 
202173ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
202273ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2023bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2024e7c4fc90SDmitry Karpeev 
202560225df5SJacob Faibussowitsch   Developer Notes:
2026bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2027bb7acecfSBarry Smith 
202873ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
202973ff1848SBarry Smith   decomposition is computed.
203073ff1848SBarry Smith 
203173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2032e7c4fc90SDmitry Karpeev @*/
2033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2034d71ae5a4SJacob Faibussowitsch {
2035e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2036e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20378865f1eaSKarl Rupp   if (len) {
20384f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20398865f1eaSKarl Rupp     *len = 0;
20408865f1eaSKarl Rupp   }
20418865f1eaSKarl Rupp   if (namelist) {
20424f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2043ea78f98cSLisandro Dalcin     *namelist = NULL;
20448865f1eaSKarl Rupp   }
20458865f1eaSKarl Rupp   if (islist) {
20464f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2047ea78f98cSLisandro Dalcin     *islist = NULL;
20488865f1eaSKarl Rupp   }
20498865f1eaSKarl Rupp   if (dmlist) {
20504f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2051ea78f98cSLisandro Dalcin     *dmlist = NULL;
20528865f1eaSKarl Rupp   }
2053f3f0edfdSDmitry Karpeev   /*
2054f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2055f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2056f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2057f3f0edfdSDmitry Karpeev    */
20587a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2060435a35e8SMatthew G Knepley     PetscSection section;
2061435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2062435a35e8SMatthew G Knepley 
20639566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20649566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2065435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2066f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20679566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20689566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20699566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2070435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2071435a35e8SMatthew G Knepley         const char *fieldName;
2072435a35e8SMatthew G Knepley 
20739566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
207403dc3394SMatthew G. Knepley         if (namelist) {
20759566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20769566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2077435a35e8SMatthew G Knepley         }
207803dc3394SMatthew G. Knepley       }
2079435a35e8SMatthew G Knepley     } else {
20809566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2081e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20820298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2083e7c4fc90SDmitry Karpeev     }
2084dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208616621825SDmitry Karpeev }
208716621825SDmitry Karpeev 
2088412a4547SAlexis Marboeuf /*@C
208920f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
209020f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2091435a35e8SMatthew G Knepley 
2092435a35e8SMatthew G Knepley   Not collective
2093435a35e8SMatthew G Knepley 
2094435a35e8SMatthew G Knepley   Input Parameters:
2095bb7acecfSBarry Smith + dm        - The `DM` object
2096bb7acecfSBarry Smith . numFields - The number of fields to select
20972adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2098435a35e8SMatthew G Knepley 
2099435a35e8SMatthew G Knepley   Output Parameters:
2100b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2101b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2102435a35e8SMatthew G Knepley 
210320f4b53cSBarry Smith   Level: intermediate
210420f4b53cSBarry Smith 
2105bb7acecfSBarry Smith   Note:
2106bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21075d3b26e6SMatthew G. Knepley 
210860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2109435a35e8SMatthew G Knepley @*/
2110d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2111d71ae5a4SJacob Faibussowitsch {
2112435a35e8SMatthew G Knepley   PetscFunctionBegin;
2113435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21144f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21154f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21164f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2117dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2119435a35e8SMatthew G Knepley }
2120435a35e8SMatthew G Knepley 
21212adcc780SMatthew G. Knepley /*@C
2122bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21232adcc780SMatthew G. Knepley 
21242adcc780SMatthew G. Knepley   Not collective
21252adcc780SMatthew G. Knepley 
2126d8d19677SJose E. Roman   Input Parameters:
2127bb7acecfSBarry Smith + dms - The `DM` objects
2128bb7acecfSBarry Smith - n   - The number of `DM`s
21292adcc780SMatthew G. Knepley 
21302adcc780SMatthew G. Knepley   Output Parameters:
2131bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2132bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21332adcc780SMatthew G. Knepley 
213420f4b53cSBarry Smith   Level: intermediate
213520f4b53cSBarry Smith 
2136bb7acecfSBarry Smith   Note:
2137bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21385d3b26e6SMatthew G. Knepley 
213973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21402adcc780SMatthew G. Knepley @*/
2141d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2142d71ae5a4SJacob Faibussowitsch {
21432adcc780SMatthew G. Knepley   PetscInt i;
21442adcc780SMatthew G. Knepley 
21452adcc780SMatthew G. Knepley   PetscFunctionBegin;
21464f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2147ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21484f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21494f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2150bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2151bb7acecfSBarry Smith   if (n) {
2152b9d85ea2SLisandro Dalcin     DM dm = dms[0];
215300045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2154dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21552adcc780SMatthew G. Knepley   }
21563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21572adcc780SMatthew G. Knepley }
21582adcc780SMatthew G. Knepley 
215916621825SDmitry Karpeev /*@C
2160a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2161a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
216216621825SDmitry Karpeev 
216320f4b53cSBarry Smith   Not Collective
216416621825SDmitry Karpeev 
216516621825SDmitry Karpeev   Input Parameter:
2166bb7acecfSBarry Smith . dm - the `DM` object
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Output Parameters:
216920f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
217020f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
217173ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
217273ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
217373ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
217416621825SDmitry Karpeev 
217516621825SDmitry Karpeev   Level: intermediate
217616621825SDmitry Karpeev 
217773ff1848SBarry Smith   Notes:
2178a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2179a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2180a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2181a4e35b19SJacob Faibussowitsch 
2182a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2183a4e35b19SJacob Faibussowitsch 
218473ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
218573ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2186bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
218716621825SDmitry Karpeev 
2188a4e35b19SJacob Faibussowitsch   Developer Notes:
218920f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2190bb7acecfSBarry Smith 
219173ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
219273ff1848SBarry Smith 
219373ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
219473ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
219516621825SDmitry Karpeev @*/
2196d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2197d71ae5a4SJacob Faibussowitsch {
2198be081cd6SPeter Brune   DMSubDomainHookLink link;
2199be081cd6SPeter Brune   PetscInt            i, l;
220016621825SDmitry Karpeev 
220116621825SDmitry Karpeev   PetscFunctionBegin;
220216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22039371c9d4SSatish Balay   if (n) {
22044f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22059371c9d4SSatish Balay     *n = 0;
22069371c9d4SSatish Balay   }
22079371c9d4SSatish Balay   if (namelist) {
22084f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22099371c9d4SSatish Balay     *namelist = NULL;
22109371c9d4SSatish Balay   }
22119371c9d4SSatish Balay   if (innerislist) {
22124f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22139371c9d4SSatish Balay     *innerislist = NULL;
22149371c9d4SSatish Balay   }
22159371c9d4SSatish Balay   if (outerislist) {
22164f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22179371c9d4SSatish Balay     *outerislist = NULL;
22189371c9d4SSatish Balay   }
22199371c9d4SSatish Balay   if (dmlist) {
22204f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22219371c9d4SSatish Balay     *dmlist = NULL;
22229371c9d4SSatish Balay   }
2223f3f0edfdSDmitry Karpeev   /*
2224f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2225f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2226f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2227f3f0edfdSDmitry Karpeev    */
22287a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
222916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2230dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
223114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2232f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2233be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2234be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22359566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2236be081cd6SPeter Brune         }
2237648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2238e7c4fc90SDmitry Karpeev       }
223914a18fd3SPeter Brune     }
2240bb7acecfSBarry Smith     if (n) *n = l;
224114a18fd3SPeter Brune   }
22423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2243e30e807fSPeter Brune }
2244e30e807fSPeter Brune 
2245e30e807fSPeter Brune /*@C
224673ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
224773ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2248e30e807fSPeter Brune 
224920f4b53cSBarry Smith   Not Collective
2250e30e807fSPeter Brune 
2251e30e807fSPeter Brune   Input Parameters:
2252bb7acecfSBarry Smith + dm     - the `DM` object
225373ff1848SBarry Smith . n      - the number of subdomains
2254e30e807fSPeter Brune - subdms - the local subdomains
2255e30e807fSPeter Brune 
2256e30e807fSPeter Brune   Output Parameters:
22576b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2258e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2259e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2260e30e807fSPeter Brune 
226120f4b53cSBarry Smith   Level: developer
226220f4b53cSBarry Smith 
2263bb7acecfSBarry Smith   Note:
2264bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2265e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2266e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2267e30e807fSPeter Brune   solution and residual data.
2268e30e807fSPeter Brune 
226973ff1848SBarry Smith   Developer Note:
2270a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2271a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2272bb7acecfSBarry Smith 
22731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2274e30e807fSPeter Brune @*/
2275d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2276d71ae5a4SJacob Faibussowitsch {
2277e30e807fSPeter Brune   PetscFunctionBegin;
2278e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22794f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2280dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2282e7c4fc90SDmitry Karpeev }
2283e7c4fc90SDmitry Karpeev 
228447c6ae99SBarry Smith /*@
2285bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
228647c6ae99SBarry Smith 
228720f4b53cSBarry Smith   Collective
228847c6ae99SBarry Smith 
2289d8d19677SJose E. Roman   Input Parameters:
2290bb7acecfSBarry Smith + dm   - the `DM` object
2291bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
229247c6ae99SBarry Smith 
229347c6ae99SBarry Smith   Output Parameter:
229420f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2295ae0a1c52SMatthew G Knepley 
229620f4b53cSBarry Smith   Options Database Key:
2297412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2298412e9a14SMatthew G. Knepley 
229947c6ae99SBarry Smith   Level: developer
230047c6ae99SBarry Smith 
230120f4b53cSBarry Smith   Note:
230220f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
230320f4b53cSBarry Smith 
230473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
230573ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
230647c6ae99SBarry Smith @*/
2307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2308d71ae5a4SJacob Faibussowitsch {
2309c833c3b5SJed Brown   DMRefineHookLink link;
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith   PetscFunctionBegin;
2312732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2314dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23154057135bSMatthew G Knepley   if (*dmf) {
231643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23178865f1eaSKarl Rupp 
23189566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23198865f1eaSKarl Rupp 
2320644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23210598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2322656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23238865f1eaSKarl Rupp 
23249566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2325c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23261baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2327c833c3b5SJed Brown     }
2328c833c3b5SJed Brown   }
23299566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2331c833c3b5SJed Brown }
2332c833c3b5SJed Brown 
2333bb9467b5SJed Brown /*@C
2334c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2335c833c3b5SJed Brown 
233620f4b53cSBarry Smith   Logically Collective; No Fortran Support
2337c833c3b5SJed Brown 
23384165533cSJose E. Roman   Input Parameters:
2339bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2340bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2341f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
234220f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2343c833c3b5SJed Brown 
234420f4b53cSBarry Smith   Calling sequence of `refinehook`:
2345bb7acecfSBarry Smith + coarse - coarse level `DM`
2346bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2347c833c3b5SJed Brown - ctx    - optional user-defined function context
2348c833c3b5SJed Brown 
234920f4b53cSBarry Smith   Calling sequence of `interphook`:
2350bb7acecfSBarry Smith + coarse - coarse level `DM`
2351c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2352bb7acecfSBarry Smith . fine   - fine level `DM` to update
2353c833c3b5SJed Brown - ctx    - optional user-defined function context
2354c833c3b5SJed Brown 
2355c833c3b5SJed Brown   Level: advanced
2356c833c3b5SJed Brown 
2357c833c3b5SJed Brown   Notes:
2358bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2359bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2360bb7acecfSBarry Smith 
2361bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2362c833c3b5SJed Brown 
2363c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2364c833c3b5SJed Brown 
23651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2366c833c3b5SJed Brown @*/
2367a4e35b19SJacob 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)
2368d71ae5a4SJacob Faibussowitsch {
2369c833c3b5SJed Brown   DMRefineHookLink link, *p;
2370c833c3b5SJed Brown 
2371c833c3b5SJed Brown   PetscFunctionBegin;
2372c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23733d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23743ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23753d8e3701SJed Brown   }
23769566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2377c833c3b5SJed Brown   link->refinehook = refinehook;
2378c833c3b5SJed Brown   link->interphook = interphook;
2379c833c3b5SJed Brown   link->ctx        = ctx;
23800298fd71SBarry Smith   link->next       = NULL;
2381c833c3b5SJed Brown   *p               = link;
23823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2383c833c3b5SJed Brown }
2384c833c3b5SJed Brown 
23853d8e3701SJed Brown /*@C
2386bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2387bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23883d8e3701SJed Brown 
238920f4b53cSBarry Smith   Logically Collective; No Fortran Support
23903d8e3701SJed Brown 
23914165533cSJose E. Roman   Input Parameters:
2392bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2393bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2394bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
239520f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23963d8e3701SJed Brown 
23973d8e3701SJed Brown   Level: advanced
23983d8e3701SJed Brown 
2399bb7acecfSBarry Smith   Note:
24003d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24013d8e3701SJed Brown 
24021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24033d8e3701SJed Brown @*/
2404d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2405d71ae5a4SJacob Faibussowitsch {
24063d8e3701SJed Brown   DMRefineHookLink link, *p;
24073d8e3701SJed Brown 
24083d8e3701SJed Brown   PetscFunctionBegin;
24093d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24103d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24113d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24123d8e3701SJed Brown       link = *p;
24133d8e3701SJed Brown       *p   = link->next;
24149566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24153d8e3701SJed Brown       break;
24163d8e3701SJed Brown     }
24173d8e3701SJed Brown   }
24183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24193d8e3701SJed Brown }
24203d8e3701SJed Brown 
2421c833c3b5SJed Brown /*@
2422bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2423c833c3b5SJed Brown 
2424c833c3b5SJed Brown   Collective if any hooks are
2425c833c3b5SJed Brown 
24264165533cSJose E. Roman   Input Parameters:
2427bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2428bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2429bb7acecfSBarry Smith - fine   - finer `DM` to update
2430c833c3b5SJed Brown 
2431c833c3b5SJed Brown   Level: developer
2432c833c3b5SJed Brown 
243373ff1848SBarry Smith   Developer Note:
2434bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2435bb7acecfSBarry Smith   an API with consistent terminology.
2436bb7acecfSBarry Smith 
24371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2438c833c3b5SJed Brown @*/
2439d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2440d71ae5a4SJacob Faibussowitsch {
2441c833c3b5SJed Brown   DMRefineHookLink link;
2442c833c3b5SJed Brown 
2443c833c3b5SJed Brown   PetscFunctionBegin;
2444c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24451baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24464057135bSMatthew G Knepley   }
24473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
244847c6ae99SBarry Smith }
244947c6ae99SBarry Smith 
2450eb3f98d2SBarry Smith /*@
24511f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24521f3379b2SToby Isaac 
245320f4b53cSBarry Smith   Collective
24541f3379b2SToby Isaac 
24554165533cSJose E. Roman   Input Parameters:
2456bb7acecfSBarry Smith + coarse    - coarse `DM`
2457bb7acecfSBarry Smith . fine      - fine `DM`
2458bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2459bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2460bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24611f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24621f3379b2SToby Isaac 
24634165533cSJose E. Roman   Output Parameter:
24641f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24651f3379b2SToby Isaac 
24661f3379b2SToby Isaac   Level: developer
24671f3379b2SToby Isaac 
2468bb7acecfSBarry Smith   Note:
2469bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24701f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24711f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24721f3379b2SToby Isaac   slope-limiting reconstruction.
24731f3379b2SToby Isaac 
247473ff1848SBarry Smith   Developer Note:
2475bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2476bb7acecfSBarry Smith 
24771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24781f3379b2SToby Isaac @*/
2479d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2480d71ae5a4SJacob Faibussowitsch {
24811f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24821f3379b2SToby Isaac 
24831f3379b2SToby Isaac   PetscFunctionBegin;
24841f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24851f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24861f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24871f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24881f3379b2SToby Isaac 
24899566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24901f3379b2SToby Isaac   if (interpsol) {
24919566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24921f3379b2SToby Isaac   } else if (interp) {
24939566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
249498921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24961f3379b2SToby Isaac }
24971f3379b2SToby Isaac 
24981f3379b2SToby Isaac /*@
2499bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2500eb3f98d2SBarry Smith 
2501eb3f98d2SBarry Smith   Not Collective
2502eb3f98d2SBarry Smith 
2503eb3f98d2SBarry Smith   Input Parameter:
2504bb7acecfSBarry Smith . dm - the `DM` object
2505eb3f98d2SBarry Smith 
2506eb3f98d2SBarry Smith   Output Parameter:
2507eb3f98d2SBarry Smith . level - number of refinements
2508eb3f98d2SBarry Smith 
2509eb3f98d2SBarry Smith   Level: developer
2510eb3f98d2SBarry Smith 
2511bb7acecfSBarry Smith   Note:
2512bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2513bb7acecfSBarry Smith 
25141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2515eb3f98d2SBarry Smith @*/
2516d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2517d71ae5a4SJacob Faibussowitsch {
2518eb3f98d2SBarry Smith   PetscFunctionBegin;
2519eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2520eb3f98d2SBarry Smith   *level = dm->levelup;
25213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2522eb3f98d2SBarry Smith }
2523eb3f98d2SBarry Smith 
2524fef3a512SBarry Smith /*@
2525bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2526fef3a512SBarry Smith 
2527fef3a512SBarry Smith   Not Collective
2528fef3a512SBarry Smith 
2529d8d19677SJose E. Roman   Input Parameters:
2530bb7acecfSBarry Smith + dm    - the `DM` object
2531fef3a512SBarry Smith - level - number of refinements
2532fef3a512SBarry Smith 
2533fef3a512SBarry Smith   Level: advanced
2534fef3a512SBarry Smith 
253595452b02SPatrick Sanan   Notes:
2536bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2537fef3a512SBarry Smith 
2538bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2539bb7acecfSBarry Smith 
25401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2541fef3a512SBarry Smith @*/
2542d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2543d71ae5a4SJacob Faibussowitsch {
2544fef3a512SBarry Smith   PetscFunctionBegin;
2545fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2546fef3a512SBarry Smith   dm->levelup = level;
25473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2548fef3a512SBarry Smith }
2549fef3a512SBarry Smith 
2550d410b0cfSMatthew G. Knepley /*@
2551bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2552d410b0cfSMatthew G. Knepley 
255320f4b53cSBarry Smith   Collective
2554d410b0cfSMatthew G. Knepley 
2555f1a722f8SMatthew G. Knepley   Input Parameters:
2556bb7acecfSBarry Smith + dm     - the `DM` object
2557d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2558d410b0cfSMatthew G. Knepley 
2559d410b0cfSMatthew G. Knepley   Output Parameter:
256020f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2561d410b0cfSMatthew G. Knepley 
2562d410b0cfSMatthew G. Knepley   Level: developer
2563d410b0cfSMatthew G. Knepley 
256420f4b53cSBarry Smith   Note:
256520f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
256620f4b53cSBarry Smith 
25671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2568d410b0cfSMatthew G. Knepley @*/
2569d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2570d71ae5a4SJacob Faibussowitsch {
2571d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2572d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2573dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2574d410b0cfSMatthew G. Knepley   if (*dme) {
2575d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25769566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2577d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25789566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2579d410b0cfSMatthew G. Knepley   }
25803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2581d410b0cfSMatthew G. Knepley }
2582d410b0cfSMatthew G. Knepley 
2583d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2584d71ae5a4SJacob Faibussowitsch {
2585ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2586ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25874f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2588ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2590ca3d3a14SMatthew G. Knepley }
2591ca3d3a14SMatthew G. Knepley 
2592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2593d71ae5a4SJacob Faibussowitsch {
2594ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2595ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25964f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2597ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2599ca3d3a14SMatthew G. Knepley }
2600ca3d3a14SMatthew G. Knepley 
2601ca3d3a14SMatthew G. Knepley /*@
2602bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2603ca3d3a14SMatthew G. Knepley 
2604ca3d3a14SMatthew G. Knepley   Input Parameter:
260520f4b53cSBarry Smith . dm - The `DM`
2606ca3d3a14SMatthew G. Knepley 
2607ca3d3a14SMatthew G. Knepley   Output Parameter:
260820f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2609ca3d3a14SMatthew G. Knepley 
2610ca3d3a14SMatthew G. Knepley   Level: developer
2611ca3d3a14SMatthew G. Knepley 
26121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2613ca3d3a14SMatthew G. Knepley @*/
2614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2615d71ae5a4SJacob Faibussowitsch {
2616ca3d3a14SMatthew G. Knepley   Vec tv;
2617ca3d3a14SMatthew G. Knepley 
2618ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2619ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26204f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26219566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2622ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2624ca3d3a14SMatthew G. Knepley }
2625ca3d3a14SMatthew G. Knepley 
2626d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2627d71ae5a4SJacob Faibussowitsch {
2628ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2629ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2630ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2631ca3d3a14SMatthew G. Knepley 
2632ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26339566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26359566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26379566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26389566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2641ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2643ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2644ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2645ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2647ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26489566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26499566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2650ca3d3a14SMatthew G. Knepley     }
2651ca3d3a14SMatthew G. Knepley   }
26529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26539566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26549566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2655ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2656ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2658ca3d3a14SMatthew G. Knepley       if (dof) {
2659ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2660ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2661ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2662ca3d3a14SMatthew G. Knepley 
2663ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26649566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26659566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26669566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2667ca3d3a14SMatthew G. Knepley       }
2668ca3d3a14SMatthew G. Knepley     }
2669ca3d3a14SMatthew G. Knepley   }
26709566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2672ca3d3a14SMatthew G. Knepley }
2673ca3d3a14SMatthew G. Knepley 
2674d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2675d71ae5a4SJacob Faibussowitsch {
2676ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2677ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2678ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2679ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2680ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2681ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2682ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26839566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2685ca3d3a14SMatthew G. Knepley }
2686ca3d3a14SMatthew G. Knepley 
2687bb9467b5SJed Brown /*@C
2688bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2689baf369e7SPeter Brune 
269020f4b53cSBarry Smith   Logically Collective
2691baf369e7SPeter Brune 
26924165533cSJose E. Roman   Input Parameters:
2693bb7acecfSBarry Smith + dm        - the `DM`
2694bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2695bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
269620f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2697baf369e7SPeter Brune 
269820f4b53cSBarry Smith   Calling sequence of `beginhook`:
2699a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2700baf369e7SPeter Brune . g    - global vector
2701baf369e7SPeter Brune . mode - mode
2702baf369e7SPeter Brune . l    - local vector
2703baf369e7SPeter Brune - ctx  - optional user-defined function context
2704baf369e7SPeter Brune 
270520f4b53cSBarry Smith   Calling sequence of `endhook`:
2706a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2707a4e35b19SJacob Faibussowitsch . g    - global vector
2708a4e35b19SJacob Faibussowitsch . mode - mode
2709a4e35b19SJacob Faibussowitsch . l    - local vector
2710baf369e7SPeter Brune - ctx  - optional user-defined function context
2711baf369e7SPeter Brune 
2712baf369e7SPeter Brune   Level: advanced
2713baf369e7SPeter Brune 
2714bb7acecfSBarry Smith   Note:
2715bb7acecfSBarry 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.
2716bb7acecfSBarry Smith 
27171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2718baf369e7SPeter Brune @*/
2719a4e35b19SJacob 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)
2720d71ae5a4SJacob Faibussowitsch {
2721baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2722baf369e7SPeter Brune 
2723baf369e7SPeter Brune   PetscFunctionBegin;
2724baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2725baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27269566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2727baf369e7SPeter Brune   link->beginhook = beginhook;
2728baf369e7SPeter Brune   link->endhook   = endhook;
2729baf369e7SPeter Brune   link->ctx       = ctx;
27300298fd71SBarry Smith   link->next      = NULL;
2731baf369e7SPeter Brune   *p              = link;
27323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2733baf369e7SPeter Brune }
2734baf369e7SPeter Brune 
2735d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2736d71ae5a4SJacob Faibussowitsch {
27374c274da1SToby Isaac   Mat          cMat;
273879769bd5SJed Brown   Vec          cVec, cBias;
27394c274da1SToby Isaac   PetscSection section, cSec;
27404c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27414c274da1SToby Isaac 
27424c274da1SToby Isaac   PetscFunctionBegin;
2743a4e35b19SJacob Faibussowitsch   (void)g;
2744a4e35b19SJacob Faibussowitsch   (void)ctx;
27454c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27469566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27474c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27485db9a05bSToby Isaac     PetscInt nRows;
27495db9a05bSToby Isaac 
27509566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27513ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27529566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27539566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27549566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27559566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27574c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27594c274da1SToby Isaac       if (dof) {
27604c274da1SToby Isaac         PetscScalar *vals;
27619566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27629566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27634c274da1SToby Isaac       }
27644c274da1SToby Isaac     }
27659566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27664c274da1SToby Isaac   }
27673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27684c274da1SToby Isaac }
27694c274da1SToby Isaac 
277047c6ae99SBarry Smith /*@
277101729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
277201729b5cSPatrick Sanan 
277320f4b53cSBarry Smith   Neighbor-wise Collective
277401729b5cSPatrick Sanan 
277501729b5cSPatrick Sanan   Input Parameters:
2776bb7acecfSBarry Smith + dm   - the `DM` object
277701729b5cSPatrick Sanan . g    - the global vector
2778bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
277901729b5cSPatrick Sanan - l    - the local vector
278001729b5cSPatrick Sanan 
278120f4b53cSBarry Smith   Level: beginner
278220f4b53cSBarry Smith 
278301729b5cSPatrick Sanan   Notes:
2784bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2785bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2786bb7acecfSBarry Smith 
2787bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
278801729b5cSPatrick Sanan 
27891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
279060225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2791bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
279201729b5cSPatrick Sanan @*/
2793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2794d71ae5a4SJacob Faibussowitsch {
279501729b5cSPatrick Sanan   PetscFunctionBegin;
27969566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27979566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279901729b5cSPatrick Sanan }
280001729b5cSPatrick Sanan 
280101729b5cSPatrick Sanan /*@
280247c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
280347c6ae99SBarry Smith 
280420f4b53cSBarry Smith   Neighbor-wise Collective
280547c6ae99SBarry Smith 
280647c6ae99SBarry Smith   Input Parameters:
2807bb7acecfSBarry Smith + dm   - the `DM` object
280847c6ae99SBarry Smith . g    - the global vector
2809bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
281047c6ae99SBarry Smith - l    - the local vector
281147c6ae99SBarry Smith 
281201729b5cSPatrick Sanan   Level: intermediate
281347c6ae99SBarry Smith 
2814bb7acecfSBarry Smith   Notes:
2815bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2816bb7acecfSBarry Smith 
2817bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2818bb7acecfSBarry Smith 
2819bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2820bb7acecfSBarry Smith 
2821bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2822bb7acecfSBarry Smith 
282360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
282447c6ae99SBarry Smith @*/
2825d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2826d71ae5a4SJacob Faibussowitsch {
28277128ae9fSMatthew G Knepley   PetscSF                 sf;
2828baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith   PetscFunctionBegin;
2831171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2832baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28331baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2834baf369e7SPeter Brune   }
28359566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28367128ae9fSMatthew G Knepley   if (sf) {
2837ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2838ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2839d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28407128ae9fSMatthew G Knepley 
28417a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28429566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28439566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28449566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28459566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28469566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28477128ae9fSMatthew G Knepley   } else {
2848fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28497128ae9fSMatthew G Knepley   }
28503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
285147c6ae99SBarry Smith }
285247c6ae99SBarry Smith 
285347c6ae99SBarry Smith /*@
285447c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
285547c6ae99SBarry Smith 
285620f4b53cSBarry Smith   Neighbor-wise Collective
285747c6ae99SBarry Smith 
285847c6ae99SBarry Smith   Input Parameters:
2859bb7acecfSBarry Smith + dm   - the `DM` object
286047c6ae99SBarry Smith . g    - the global vector
2861bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
286247c6ae99SBarry Smith - l    - the local vector
286347c6ae99SBarry Smith 
286401729b5cSPatrick Sanan   Level: intermediate
286547c6ae99SBarry Smith 
2866bb7acecfSBarry Smith   Note:
2867bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2868bb7acecfSBarry Smith 
286960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
287047c6ae99SBarry Smith @*/
2871d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2872d71ae5a4SJacob Faibussowitsch {
28737128ae9fSMatthew G Knepley   PetscSF                 sf;
2874ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2875ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2876ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2877baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2878d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
287947c6ae99SBarry Smith 
288047c6ae99SBarry Smith   PetscFunctionBegin;
2881171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28829566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28839566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28847128ae9fSMatthew G Knepley   if (sf) {
28857a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28867128ae9fSMatthew G Knepley 
28879566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28889566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28899566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28909566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28919566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28929566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28937128ae9fSMatthew G Knepley   } else {
2894fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28957128ae9fSMatthew G Knepley   }
28969566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2897baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28989566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2899baf369e7SPeter Brune   }
29003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
290147c6ae99SBarry Smith }
290247c6ae99SBarry Smith 
2903d4d07f1eSToby Isaac /*@C
2904d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2905d4d07f1eSToby Isaac 
290620f4b53cSBarry Smith   Logically Collective
2907d4d07f1eSToby Isaac 
29084165533cSJose E. Roman   Input Parameters:
2909bb7acecfSBarry Smith + dm        - the `DM`
2910bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2911bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
291220f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2913d4d07f1eSToby Isaac 
291420f4b53cSBarry Smith   Calling sequence of `beginhook`:
2915a4e35b19SJacob Faibussowitsch + global - global `DM`
2916d4d07f1eSToby Isaac . l      - local vector
2917d4d07f1eSToby Isaac . mode   - mode
2918d4d07f1eSToby Isaac . g      - global vector
2919d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2920d4d07f1eSToby Isaac 
292120f4b53cSBarry Smith   Calling sequence of `endhook`:
2922bb7acecfSBarry Smith + global - global `DM`
2923d4d07f1eSToby Isaac . l      - local vector
2924d4d07f1eSToby Isaac . mode   - mode
2925d4d07f1eSToby Isaac . g      - global vector
2926d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2927d4d07f1eSToby Isaac 
2928d4d07f1eSToby Isaac   Level: advanced
2929d4d07f1eSToby Isaac 
29301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2931d4d07f1eSToby Isaac @*/
2932a4e35b19SJacob 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)
2933d71ae5a4SJacob Faibussowitsch {
2934d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2935d4d07f1eSToby Isaac 
2936d4d07f1eSToby Isaac   PetscFunctionBegin;
2937d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2938d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29399566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2940d4d07f1eSToby Isaac   link->beginhook = beginhook;
2941d4d07f1eSToby Isaac   link->endhook   = endhook;
2942d4d07f1eSToby Isaac   link->ctx       = ctx;
2943d4d07f1eSToby Isaac   link->next      = NULL;
2944d4d07f1eSToby Isaac   *p              = link;
29453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2946d4d07f1eSToby Isaac }
2947d4d07f1eSToby Isaac 
2948d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2949d71ae5a4SJacob Faibussowitsch {
29504c274da1SToby Isaac   PetscFunctionBegin;
2951a4e35b19SJacob Faibussowitsch   (void)g;
2952a4e35b19SJacob Faibussowitsch   (void)ctx;
29534c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29542b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29552b6b7d09SToby Isaac     Mat          cMat;
29562b6b7d09SToby Isaac     Vec          cVec;
29575db9a05bSToby Isaac     PetscInt     nRows;
29582b6b7d09SToby Isaac     PetscSection section, cSec;
29592b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29602b6b7d09SToby Isaac 
29612b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29622b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29635db9a05bSToby Isaac 
29649566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29653ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29669566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29679566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29694c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29709566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29714c274da1SToby Isaac       if (dof) {
29724c274da1SToby Isaac         PetscInt     d;
29734c274da1SToby Isaac         PetscScalar *vals;
29749566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29759566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29764c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29774c274da1SToby Isaac          * we just extracted */
2978ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29794c274da1SToby Isaac       }
29804c274da1SToby Isaac     }
29819566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29829566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29834c274da1SToby Isaac   }
29843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29854c274da1SToby Isaac }
298601729b5cSPatrick Sanan /*@
298701729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
298801729b5cSPatrick Sanan 
298920f4b53cSBarry Smith   Neighbor-wise Collective
299001729b5cSPatrick Sanan 
299101729b5cSPatrick Sanan   Input Parameters:
2992bb7acecfSBarry Smith + dm   - the `DM` object
299301729b5cSPatrick Sanan . l    - the local vector
2994bb7acecfSBarry 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.
299501729b5cSPatrick Sanan - g    - the global vector
299601729b5cSPatrick Sanan 
299720f4b53cSBarry Smith   Level: beginner
299820f4b53cSBarry Smith 
299901729b5cSPatrick Sanan   Notes:
300001729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3001bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
300201729b5cSPatrick Sanan 
3003bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3004bb7acecfSBarry Smith 
3005bb7acecfSBarry 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.
3006bb7acecfSBarry Smith 
3007bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
300801729b5cSPatrick Sanan 
30091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
301001729b5cSPatrick Sanan @*/
3011d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3012d71ae5a4SJacob Faibussowitsch {
301301729b5cSPatrick Sanan   PetscFunctionBegin;
30149566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30159566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
301701729b5cSPatrick Sanan }
30184c274da1SToby Isaac 
301947c6ae99SBarry Smith /*@
302001729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30219a42bb27SBarry Smith 
302220f4b53cSBarry Smith   Neighbor-wise Collective
30239a42bb27SBarry Smith 
30249a42bb27SBarry Smith   Input Parameters:
3025bb7acecfSBarry Smith + dm   - the `DM` object
3026f6813fd5SJed Brown . l    - the local vector
3027aa624791SPierre 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.
30281eb28f2eSBarry Smith - g    - the global vector
30299a42bb27SBarry Smith 
303020f4b53cSBarry Smith   Level: intermediate
303120f4b53cSBarry Smith 
303295452b02SPatrick Sanan   Notes:
3033bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3034bb7acecfSBarry Smith 
3035bb7acecfSBarry 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.
3036bb7acecfSBarry Smith 
3037bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3038bb7acecfSBarry Smith 
3039bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3040bb7acecfSBarry Smith 
3041bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30429a42bb27SBarry Smith 
30431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30449a42bb27SBarry Smith @*/
3045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3046d71ae5a4SJacob Faibussowitsch {
30477128ae9fSMatthew G Knepley   PetscSF                 sf;
304884330215SMatthew G. Knepley   PetscSection            s, gs;
3049d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3050ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3051ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3052ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3053fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3054d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30559a42bb27SBarry Smith 
30569a42bb27SBarry Smith   PetscFunctionBegin;
3057171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3058d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30591baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3060d4d07f1eSToby Isaac   }
30619566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30629566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30639566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30647128ae9fSMatthew G Knepley   switch (mode) {
30657128ae9fSMatthew G Knepley   case INSERT_VALUES:
30667128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3067d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3068d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3069d71ae5a4SJacob Faibussowitsch     break;
30707128ae9fSMatthew G Knepley   case ADD_VALUES:
30717128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3072d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3073d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3074d71ae5a4SJacob Faibussowitsch     break;
3075d71ae5a4SJacob Faibussowitsch   default:
3076d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30777128ae9fSMatthew G Knepley   }
3078ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30799566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3080ca3d3a14SMatthew G. Knepley     if (transform) {
30819566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30829566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30839566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30849566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3085fa88e482SJed Brown     } else if (isInsert) {
30869566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3087fa88e482SJed Brown     } else {
30889566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3089fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3090ca3d3a14SMatthew G. Knepley     }
3091fa88e482SJed Brown     if (s && isInsert) {
30929566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3093fa88e482SJed Brown     } else {
30949566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3095fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3096fa88e482SJed Brown     }
3097ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30989566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
309984330215SMatthew G. Knepley     } else if (s && isInsert) {
310084330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
310184330215SMatthew G. Knepley 
31029566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31039566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31049566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
310584330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3106b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
310784330215SMatthew G. Knepley 
31089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31109566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3114b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
311503442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31167a8be351SBarry 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);
3117b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3118b3b16f48SMatthew G. Knepley         if (!gcdof) {
311984330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3120b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3121b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
312284330215SMatthew G. Knepley           const PetscInt *cdofs;
312384330215SMatthew G. Knepley           PetscInt        cind = 0;
312484330215SMatthew G. Knepley 
31259566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3126b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31279371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31289371c9d4SSatish Balay               ++cind;
31299371c9d4SSatish Balay               continue;
31309371c9d4SSatish Balay             }
3131b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
313284330215SMatthew G. Knepley           }
31337a8be351SBarry 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);
313484330215SMatthew G. Knepley       }
3135ca3d3a14SMatthew G. Knepley     }
3136fa88e482SJed Brown     if (g_inplace) {
31379566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3138fa88e482SJed Brown     } else {
31399566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3140fa88e482SJed Brown     }
3141ca3d3a14SMatthew G. Knepley     if (transform) {
31429566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31439566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3144fa88e482SJed Brown     } else if (l_inplace) {
31459566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3146ca3d3a14SMatthew G. Knepley     } else {
31479566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3148ca3d3a14SMatthew G. Knepley     }
31497128ae9fSMatthew G Knepley   } else {
3150fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31517128ae9fSMatthew G Knepley   }
31523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31539a42bb27SBarry Smith }
31549a42bb27SBarry Smith 
31559a42bb27SBarry Smith /*@
31569a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
315747c6ae99SBarry Smith 
315820f4b53cSBarry Smith   Neighbor-wise Collective
315947c6ae99SBarry Smith 
316047c6ae99SBarry Smith   Input Parameters:
3161bb7acecfSBarry Smith + dm   - the `DM` object
3162f6813fd5SJed Brown . l    - the local vector
3163bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3164f6813fd5SJed Brown - g    - the global vector
316547c6ae99SBarry Smith 
316601729b5cSPatrick Sanan   Level: intermediate
316747c6ae99SBarry Smith 
3168bb7acecfSBarry Smith   Note:
3169bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3170bb7acecfSBarry Smith 
317160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
317247c6ae99SBarry Smith @*/
3173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3174d71ae5a4SJacob Faibussowitsch {
31757128ae9fSMatthew G Knepley   PetscSF                 sf;
317684330215SMatthew G. Knepley   PetscSection            s;
3177d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3178ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
317947c6ae99SBarry Smith 
318047c6ae99SBarry Smith   PetscFunctionBegin;
3181171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31829566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31839566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31847128ae9fSMatthew G Knepley   switch (mode) {
31857128ae9fSMatthew G Knepley   case INSERT_VALUES:
3186d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3187d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3188d71ae5a4SJacob Faibussowitsch     break;
31897128ae9fSMatthew G Knepley   case ADD_VALUES:
3190d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3191d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3192d71ae5a4SJacob Faibussowitsch     break;
3193d71ae5a4SJacob Faibussowitsch   default:
3194d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31957128ae9fSMatthew G Knepley   }
319684330215SMatthew G. Knepley   if (sf && !isInsert) {
3197ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3198ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3199ca3d3a14SMatthew G. Knepley     Vec                tmpl;
320084330215SMatthew G. Knepley 
32019566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3202ca3d3a14SMatthew G. Knepley     if (transform) {
32039566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3205ca3d3a14SMatthew G. Knepley     } else {
32069566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3207ca3d3a14SMatthew G. Knepley     }
32089566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32099566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3210ca3d3a14SMatthew G. Knepley     if (transform) {
32119566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32129566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3213ca3d3a14SMatthew G. Knepley     } else {
32149566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3215ca3d3a14SMatthew G. Knepley     }
32169566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
321784330215SMatthew G. Knepley   } else if (s && isInsert) {
32187128ae9fSMatthew G Knepley   } else {
3219fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32207128ae9fSMatthew G Knepley   }
3221d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32229566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3223d4d07f1eSToby Isaac   }
32243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
322547c6ae99SBarry Smith }
322647c6ae99SBarry Smith 
3227f089877aSRichard Tran Mills /*@
3228a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3229a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3230a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3231f089877aSRichard Tran Mills 
323220f4b53cSBarry Smith   Neighbor-wise Collective
3233f089877aSRichard Tran Mills 
3234f089877aSRichard Tran Mills   Input Parameters:
3235bb7acecfSBarry Smith + dm   - the `DM` object
3236bc0a1609SRichard Tran Mills . g    - the original local vector
3237bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3238f089877aSRichard Tran Mills 
3239bc0a1609SRichard Tran Mills   Output Parameter:
3240bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3241f089877aSRichard Tran Mills 
3242f089877aSRichard Tran Mills   Level: intermediate
3243f089877aSRichard Tran Mills 
324473ff1848SBarry Smith   Note:
3245a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3246a4e35b19SJacob Faibussowitsch 
324760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3248f089877aSRichard Tran Mills @*/
3249d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3250d71ae5a4SJacob Faibussowitsch {
3251f089877aSRichard Tran Mills   PetscFunctionBegin;
3252f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32539f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32549f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32559f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3257f089877aSRichard Tran Mills }
3258f089877aSRichard Tran Mills 
3259f089877aSRichard Tran Mills /*@
3260bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3261bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3262f089877aSRichard Tran Mills 
326320f4b53cSBarry Smith   Neighbor-wise Collective
3264f089877aSRichard Tran Mills 
3265f089877aSRichard Tran Mills   Input Parameters:
326660225df5SJacob Faibussowitsch + dm   - the `DM` object
3267bc0a1609SRichard Tran Mills . g    - the original local vector
3268bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3269f089877aSRichard Tran Mills 
3270bc0a1609SRichard Tran Mills   Output Parameter:
3271bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3272f089877aSRichard Tran Mills 
3273f089877aSRichard Tran Mills   Level: intermediate
3274f089877aSRichard Tran Mills 
327560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3276f089877aSRichard Tran Mills @*/
3277d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3278d71ae5a4SJacob Faibussowitsch {
3279f089877aSRichard Tran Mills   PetscFunctionBegin;
3280f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32819f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32829f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32839f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3285f089877aSRichard Tran Mills }
3286f089877aSRichard Tran Mills 
328747c6ae99SBarry Smith /*@
3288bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
328947c6ae99SBarry Smith 
329020f4b53cSBarry Smith   Collective
329147c6ae99SBarry Smith 
3292d8d19677SJose E. Roman   Input Parameters:
3293bb7acecfSBarry Smith + dm   - the `DM` object
329420f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
329547c6ae99SBarry Smith 
329647c6ae99SBarry Smith   Output Parameter:
3297bb7acecfSBarry Smith . dmc - the coarsened `DM`
329847c6ae99SBarry Smith 
329947c6ae99SBarry Smith   Level: developer
330047c6ae99SBarry Smith 
330173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
330273ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
330347c6ae99SBarry Smith @*/
3304d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3305d71ae5a4SJacob Faibussowitsch {
3306b17ce1afSJed Brown   DMCoarsenHookLink link;
330747c6ae99SBarry Smith 
330847c6ae99SBarry Smith   PetscFunctionBegin;
3309171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3311dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3312b9d85ea2SLisandro Dalcin   if (*dmc) {
3313a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33149566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
331543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33169566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3317644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33180598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3319656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33209566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3321b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33229566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3323b17ce1afSJed Brown     }
3324b9d85ea2SLisandro Dalcin   }
33259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33267a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3328b17ce1afSJed Brown }
3329b17ce1afSJed Brown 
3330bb9467b5SJed Brown /*@C
3331b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3332b17ce1afSJed Brown 
333320f4b53cSBarry Smith   Logically Collective; No Fortran Support
3334b17ce1afSJed Brown 
33354165533cSJose E. Roman   Input Parameters:
3336bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3337b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3338bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
333920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3340b17ce1afSJed Brown 
334120f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3342bb7acecfSBarry Smith + fine   - fine level `DM`
3343bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3344b17ce1afSJed Brown - ctx    - optional user-defined function context
3345b17ce1afSJed Brown 
334620f4b53cSBarry Smith   Calling sequence of `restricthook`:
3347bb7acecfSBarry Smith + fine      - fine level `DM`
3348bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3349c833c3b5SJed Brown . rscale    - scaling vector for restriction
3350c833c3b5SJed Brown . inject    - matrix restricting by injection
3351b17ce1afSJed Brown . coarse    - coarse level DM to update
3352b17ce1afSJed Brown - ctx       - optional user-defined function context
3353b17ce1afSJed Brown 
3354b17ce1afSJed Brown   Level: advanced
3355b17ce1afSJed Brown 
3356b17ce1afSJed Brown   Notes:
3357bb7acecfSBarry 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`.
3358b17ce1afSJed Brown 
3359b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3360b17ce1afSJed Brown 
3361b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3362bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3363b17ce1afSJed Brown 
3364bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3365bb7acecfSBarry Smith 
33661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3367b17ce1afSJed Brown @*/
3368a4e35b19SJacob 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)
3369d71ae5a4SJacob Faibussowitsch {
3370b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3371b17ce1afSJed Brown 
3372b17ce1afSJed Brown   PetscFunctionBegin;
3373b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33741e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33753ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33761e3d8eccSJed Brown   }
33779566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3378b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3379b17ce1afSJed Brown   link->restricthook = restricthook;
3380b17ce1afSJed Brown   link->ctx          = ctx;
33810298fd71SBarry Smith   link->next         = NULL;
3382b17ce1afSJed Brown   *p                 = link;
33833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3384b17ce1afSJed Brown }
3385b17ce1afSJed Brown 
3386dc822a44SJed Brown /*@C
3387bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3388dc822a44SJed Brown 
338920f4b53cSBarry Smith   Logically Collective; No Fortran Support
3390dc822a44SJed Brown 
33914165533cSJose E. Roman   Input Parameters:
3392bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3393dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3394bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
339520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3396dc822a44SJed Brown 
3397dc822a44SJed Brown   Level: advanced
3398dc822a44SJed Brown 
339973ff1848SBarry Smith   Notes:
340073ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
340173ff1848SBarry Smith 
340273ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3403dc822a44SJed Brown 
34041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3405dc822a44SJed Brown @*/
3406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3407d71ae5a4SJacob Faibussowitsch {
3408dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3409dc822a44SJed Brown 
3410dc822a44SJed Brown   PetscFunctionBegin;
3411dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3412dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3413dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3414dc822a44SJed Brown       link = *p;
3415dc822a44SJed Brown       *p   = link->next;
34169566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3417dc822a44SJed Brown       break;
3418dc822a44SJed Brown     }
3419dc822a44SJed Brown   }
34203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3421dc822a44SJed Brown }
3422dc822a44SJed Brown 
3423b17ce1afSJed Brown /*@
3424bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3425b17ce1afSJed Brown 
3426b17ce1afSJed Brown   Collective if any hooks are
3427b17ce1afSJed Brown 
34284165533cSJose E. Roman   Input Parameters:
3429bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3430bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3431e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3432bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
343320f4b53cSBarry Smith - coarse  - coarser `DM` to update
3434b17ce1afSJed Brown 
3435b17ce1afSJed Brown   Level: developer
3436b17ce1afSJed Brown 
343773ff1848SBarry Smith   Developer Note:
3438bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3439bb7acecfSBarry Smith 
34401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3441b17ce1afSJed Brown @*/
3442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3443d71ae5a4SJacob Faibussowitsch {
3444b17ce1afSJed Brown   DMCoarsenHookLink link;
3445b17ce1afSJed Brown 
3446b17ce1afSJed Brown   PetscFunctionBegin;
3447b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34481baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3449b17ce1afSJed Brown   }
34503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
345147c6ae99SBarry Smith }
345247c6ae99SBarry Smith 
3453bb9467b5SJed Brown /*@C
345473ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34555dbd56e3SPeter Brune 
345620f4b53cSBarry Smith   Logically Collective; No Fortran Support
34575dbd56e3SPeter Brune 
34584165533cSJose E. Roman   Input Parameters:
3459bb7acecfSBarry Smith + global       - global `DM`
3460bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34615dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
346220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34635dbd56e3SPeter Brune 
346420f4b53cSBarry Smith   Calling sequence of `ddhook`:
3465bb7acecfSBarry Smith + global - global `DM`
346673ff1848SBarry Smith . block  - subdomain `DM`
3467ec4806b8SPeter Brune - ctx    - optional user-defined function context
3468ec4806b8SPeter Brune 
346920f4b53cSBarry Smith   Calling sequence of `restricthook`:
3470bb7acecfSBarry Smith + global - global `DM`
347173ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
347273ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
347373ff1848SBarry Smith . block  - subdomain `DM`
34745dbd56e3SPeter Brune - ctx    - optional user-defined function context
34755dbd56e3SPeter Brune 
34765dbd56e3SPeter Brune   Level: advanced
34775dbd56e3SPeter Brune 
34785dbd56e3SPeter Brune   Notes:
347973ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34805dbd56e3SPeter Brune 
34815dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34825dbd56e3SPeter Brune 
34835dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3484bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34855dbd56e3SPeter Brune 
348673ff1848SBarry Smith   Developer Note:
348773ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
348873ff1848SBarry Smith 
348973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
34905dbd56e3SPeter Brune @*/
3491a4e35b19SJacob 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)
3492d71ae5a4SJacob Faibussowitsch {
3493be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34945dbd56e3SPeter Brune 
34955dbd56e3SPeter Brune   PetscFunctionBegin;
34965dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3497b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34983ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3499b3a6b972SJed Brown   }
35009566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35015dbd56e3SPeter Brune   link->restricthook = restricthook;
3502be081cd6SPeter Brune   link->ddhook       = ddhook;
35035dbd56e3SPeter Brune   link->ctx          = ctx;
35040298fd71SBarry Smith   link->next         = NULL;
35055dbd56e3SPeter Brune   *p                 = link;
35063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35075dbd56e3SPeter Brune }
35085dbd56e3SPeter Brune 
3509b3a6b972SJed Brown /*@C
351073ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3511b3a6b972SJed Brown 
351220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3513b3a6b972SJed Brown 
35144165533cSJose E. Roman   Input Parameters:
3515bb7acecfSBarry Smith + global       - global `DM`
3516bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3517b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
351820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3519b3a6b972SJed Brown 
3520b3a6b972SJed Brown   Level: advanced
3521b3a6b972SJed Brown 
352273ff1848SBarry Smith   Note:
352373ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
352473ff1848SBarry Smith 
352573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
352673ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3527b3a6b972SJed Brown @*/
3528d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3529d71ae5a4SJacob Faibussowitsch {
3530b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3531b3a6b972SJed Brown 
3532b3a6b972SJed Brown   PetscFunctionBegin;
3533b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3534b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3535b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3536b3a6b972SJed Brown       link = *p;
3537b3a6b972SJed Brown       *p   = link->next;
35389566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3539b3a6b972SJed Brown       break;
3540b3a6b972SJed Brown     }
3541b3a6b972SJed Brown   }
35423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3543b3a6b972SJed Brown }
3544b3a6b972SJed Brown 
35455dbd56e3SPeter Brune /*@
354673ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35475dbd56e3SPeter Brune 
35485dbd56e3SPeter Brune   Collective if any hooks are
35495dbd56e3SPeter Brune 
35504165533cSJose E. Roman   Input Parameters:
3551a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3552a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3553a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3554a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35555dbd56e3SPeter Brune 
35565dbd56e3SPeter Brune   Level: developer
35575dbd56e3SPeter Brune 
355873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35595dbd56e3SPeter Brune @*/
3560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3561d71ae5a4SJacob Faibussowitsch {
3562be081cd6SPeter Brune   DMSubDomainHookLink link;
35635dbd56e3SPeter Brune 
35645dbd56e3SPeter Brune   PetscFunctionBegin;
3565be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35661baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35675dbd56e3SPeter Brune   }
35683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35695dbd56e3SPeter Brune }
35705dbd56e3SPeter Brune 
35715fe1f584SPeter Brune /*@
3572bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35735fe1f584SPeter Brune 
35745fe1f584SPeter Brune   Not Collective
35755fe1f584SPeter Brune 
35765fe1f584SPeter Brune   Input Parameter:
3577bb7acecfSBarry Smith . dm - the `DM` object
35785fe1f584SPeter Brune 
35795fe1f584SPeter Brune   Output Parameter:
35806a7d9d85SPeter Brune . level - number of coarsenings
35815fe1f584SPeter Brune 
35825fe1f584SPeter Brune   Level: developer
35835fe1f584SPeter Brune 
35841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35855fe1f584SPeter Brune @*/
3586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3587d71ae5a4SJacob Faibussowitsch {
35885fe1f584SPeter Brune   PetscFunctionBegin;
35895fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35904f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35915fe1f584SPeter Brune   *level = dm->leveldown;
35923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35935fe1f584SPeter Brune }
35945fe1f584SPeter Brune 
35959a64c4a8SMatthew G. Knepley /*@
3596bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35979a64c4a8SMatthew G. Knepley 
359820f4b53cSBarry Smith   Collective
35999a64c4a8SMatthew G. Knepley 
36009a64c4a8SMatthew G. Knepley   Input Parameters:
3601bb7acecfSBarry Smith + dm    - the `DM` object
36029a64c4a8SMatthew G. Knepley - level - number of coarsenings
36039a64c4a8SMatthew G. Knepley 
36049a64c4a8SMatthew G. Knepley   Level: developer
36059a64c4a8SMatthew G. Knepley 
3606bb7acecfSBarry Smith   Note:
3607bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3608bb7acecfSBarry Smith 
360942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36109a64c4a8SMatthew G. Knepley @*/
3611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3612d71ae5a4SJacob Faibussowitsch {
36139a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36149a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36159a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36179a64c4a8SMatthew G. Knepley }
36189a64c4a8SMatthew G. Knepley 
361947c6ae99SBarry Smith /*@C
3620bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
362147c6ae99SBarry Smith 
362220f4b53cSBarry Smith   Collective
362347c6ae99SBarry Smith 
3624d8d19677SJose E. Roman   Input Parameters:
3625bb7acecfSBarry Smith + dm      - the `DM` object
362647c6ae99SBarry Smith - nlevels - the number of levels of refinement
362747c6ae99SBarry Smith 
362847c6ae99SBarry Smith   Output Parameter:
3629bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
363047c6ae99SBarry Smith 
363147c6ae99SBarry Smith   Level: developer
363247c6ae99SBarry Smith 
36331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363447c6ae99SBarry Smith @*/
3635d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3636d71ae5a4SJacob Faibussowitsch {
363747c6ae99SBarry Smith   PetscFunctionBegin;
3638171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36397a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36403ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36414f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3642213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
364347c6ae99SBarry Smith     PetscInt i;
364447c6ae99SBarry Smith 
36459566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
364648a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3647213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364947c6ae99SBarry Smith }
365047c6ae99SBarry Smith 
365147c6ae99SBarry Smith /*@C
3652bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
365347c6ae99SBarry Smith 
365420f4b53cSBarry Smith   Collective
365547c6ae99SBarry Smith 
3656d8d19677SJose E. Roman   Input Parameters:
3657bb7acecfSBarry Smith + dm      - the `DM` object
365847c6ae99SBarry Smith - nlevels - the number of levels of coarsening
365947c6ae99SBarry Smith 
366047c6ae99SBarry Smith   Output Parameter:
3661bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
366247c6ae99SBarry Smith 
366347c6ae99SBarry Smith   Level: developer
366447c6ae99SBarry Smith 
36651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
366647c6ae99SBarry Smith @*/
3667d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3668d71ae5a4SJacob Faibussowitsch {
366947c6ae99SBarry Smith   PetscFunctionBegin;
3670171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36717a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36723ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36734f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3674213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
367547c6ae99SBarry Smith     PetscInt i;
367647c6ae99SBarry Smith 
36779566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
367848a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3679213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368147c6ae99SBarry Smith }
368247c6ae99SBarry Smith 
36831a266240SBarry Smith /*@C
3684bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36851a266240SBarry Smith 
3686bb7acecfSBarry Smith   Logically Collective if the function is collective
36871a266240SBarry Smith 
36881a266240SBarry Smith   Input Parameters:
3689bb7acecfSBarry Smith + dm      - the `DM` object
36901a266240SBarry Smith - destroy - the destroy function
36911a266240SBarry Smith 
36921a266240SBarry Smith   Level: intermediate
36931a266240SBarry Smith 
36941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3695f07f9ceaSJed Brown @*/
3696d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3697d71ae5a4SJacob Faibussowitsch {
36981a266240SBarry Smith   PetscFunctionBegin;
3699171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37001a266240SBarry Smith   dm->ctxdestroy = destroy;
37013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37021a266240SBarry Smith }
37031a266240SBarry Smith 
3704b07ff414SBarry Smith /*@
3705bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
370647c6ae99SBarry Smith 
370747c6ae99SBarry Smith   Not Collective
370847c6ae99SBarry Smith 
370947c6ae99SBarry Smith   Input Parameters:
3710bb7acecfSBarry Smith + dm  - the `DM` object
371147c6ae99SBarry Smith - ctx - the user context
371247c6ae99SBarry Smith 
371347c6ae99SBarry Smith   Level: intermediate
371447c6ae99SBarry Smith 
3715e33b3f0fSStefano Zampini   Notes:
371635cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3717e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3718e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3719bb7acecfSBarry Smith 
372060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
372147c6ae99SBarry Smith @*/
3722d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3723d71ae5a4SJacob Faibussowitsch {
372447c6ae99SBarry Smith   PetscFunctionBegin;
3725171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372647c6ae99SBarry Smith   dm->ctx = ctx;
37273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372847c6ae99SBarry Smith }
372947c6ae99SBarry Smith 
373047c6ae99SBarry Smith /*@
3731bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
373247c6ae99SBarry Smith 
373347c6ae99SBarry Smith   Not Collective
373447c6ae99SBarry Smith 
373547c6ae99SBarry Smith   Input Parameter:
3736bb7acecfSBarry Smith . dm - the `DM` object
373747c6ae99SBarry Smith 
373847c6ae99SBarry Smith   Output Parameter:
373947c6ae99SBarry Smith . ctx - the user context
374047c6ae99SBarry Smith 
374147c6ae99SBarry Smith   Level: intermediate
374247c6ae99SBarry Smith 
3743bb7acecfSBarry Smith   Note:
374435cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3745bb7acecfSBarry Smith 
374642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
374747c6ae99SBarry Smith @*/
3748d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3749d71ae5a4SJacob Faibussowitsch {
375047c6ae99SBarry Smith   PetscFunctionBegin;
3751171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37521b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
375447c6ae99SBarry Smith }
375547c6ae99SBarry Smith 
375608da532bSDmitry Karpeev /*@C
3757bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
375808da532bSDmitry Karpeev 
375920f4b53cSBarry Smith   Logically Collective
376008da532bSDmitry Karpeev 
3761d8d19677SJose E. Roman   Input Parameters:
376208da532bSDmitry Karpeev + dm - the DM object
376320f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
376408da532bSDmitry Karpeev 
376508da532bSDmitry Karpeev   Level: intermediate
376608da532bSDmitry Karpeev 
37671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3768db781477SPatrick Sanan          `DMSetJacobian()`
376908da532bSDmitry Karpeev @*/
3770d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3771d71ae5a4SJacob Faibussowitsch {
377208da532bSDmitry Karpeev   PetscFunctionBegin;
37735a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377408da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377608da532bSDmitry Karpeev }
377708da532bSDmitry Karpeev 
377808da532bSDmitry Karpeev /*@
3779bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
378008da532bSDmitry Karpeev 
378108da532bSDmitry Karpeev   Not Collective
378208da532bSDmitry Karpeev 
378308da532bSDmitry Karpeev   Input Parameter:
3784bb7acecfSBarry Smith . dm - the `DM` object to destroy
378508da532bSDmitry Karpeev 
378608da532bSDmitry Karpeev   Output Parameter:
3787bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
378808da532bSDmitry Karpeev 
378908da532bSDmitry Karpeev   Level: developer
379008da532bSDmitry Karpeev 
37911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379208da532bSDmitry Karpeev @*/
3793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3794d71ae5a4SJacob Faibussowitsch {
379508da532bSDmitry Karpeev   PetscFunctionBegin;
37965a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37974f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
379808da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380008da532bSDmitry Karpeev }
380108da532bSDmitry Karpeev 
380208da532bSDmitry Karpeev /*@C
3803bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
380408da532bSDmitry Karpeev 
380520f4b53cSBarry Smith   Logically Collective
380608da532bSDmitry Karpeev 
3807f899ff85SJose E. Roman   Input Parameter:
3808bb7acecfSBarry Smith . dm - the `DM` object
380908da532bSDmitry Karpeev 
381060225df5SJacob Faibussowitsch   Output Parameters:
381108da532bSDmitry Karpeev + xl - lower bound
381208da532bSDmitry Karpeev - xu - upper bound
381308da532bSDmitry Karpeev 
3814907376e6SBarry Smith   Level: advanced
3815907376e6SBarry Smith 
381620f4b53cSBarry Smith   Note:
381795452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
381808da532bSDmitry Karpeev 
38191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
382008da532bSDmitry Karpeev @*/
3821d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3822d71ae5a4SJacob Faibussowitsch {
382308da532bSDmitry Karpeev   PetscFunctionBegin;
38245a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38265a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3827dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
382908da532bSDmitry Karpeev }
383008da532bSDmitry Karpeev 
3831b0ae01b7SPeter Brune /*@
3832bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3833b0ae01b7SPeter Brune 
3834b0ae01b7SPeter Brune   Not Collective
3835b0ae01b7SPeter Brune 
3836b0ae01b7SPeter Brune   Input Parameter:
3837b0ae01b7SPeter Brune . dm - the DM object
3838b0ae01b7SPeter Brune 
3839b0ae01b7SPeter Brune   Output Parameter:
3840bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3841b0ae01b7SPeter Brune 
3842b0ae01b7SPeter Brune   Level: developer
3843b0ae01b7SPeter Brune 
38441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3845b0ae01b7SPeter Brune @*/
3846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3847d71ae5a4SJacob Faibussowitsch {
3848b0ae01b7SPeter Brune   PetscFunctionBegin;
38495a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38504f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3851b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3853b0ae01b7SPeter Brune }
3854b0ae01b7SPeter Brune 
38553ad4599aSBarry Smith /*@
3856bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38573ad4599aSBarry Smith 
38583ad4599aSBarry Smith   Not Collective
38593ad4599aSBarry Smith 
38603ad4599aSBarry Smith   Input Parameter:
3861bb7acecfSBarry Smith . dm - the `DM` object
38623ad4599aSBarry Smith 
38633ad4599aSBarry Smith   Output Parameter:
3864bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38653ad4599aSBarry Smith 
38663ad4599aSBarry Smith   Level: developer
38673ad4599aSBarry Smith 
38681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38693ad4599aSBarry Smith @*/
3870d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3871d71ae5a4SJacob Faibussowitsch {
38723ad4599aSBarry Smith   PetscFunctionBegin;
38735a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38744f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38753ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38773ad4599aSBarry Smith }
38783ad4599aSBarry Smith 
3879a7058e45SLawrence Mitchell /*@
3880bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3881a7058e45SLawrence Mitchell 
3882a7058e45SLawrence Mitchell   Not Collective
3883a7058e45SLawrence Mitchell 
3884a7058e45SLawrence Mitchell   Input Parameter:
3885bb7acecfSBarry Smith . dm - the `DM` object
3886a7058e45SLawrence Mitchell 
3887a7058e45SLawrence Mitchell   Output Parameter:
3888bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3889a7058e45SLawrence Mitchell 
3890a7058e45SLawrence Mitchell   Level: developer
3891a7058e45SLawrence Mitchell 
38921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3893a7058e45SLawrence Mitchell @*/
3894d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3895d71ae5a4SJacob Faibussowitsch {
3896a7058e45SLawrence Mitchell   PetscFunctionBegin;
38975a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38984f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3899dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3900ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3902a7058e45SLawrence Mitchell }
3903a7058e45SLawrence Mitchell 
39040298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3905264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3906264ace61SBarry Smith 
3907264ace61SBarry Smith /*@C
3908bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3909264ace61SBarry Smith 
391020f4b53cSBarry Smith   Collective
3911264ace61SBarry Smith 
3912264ace61SBarry Smith   Input Parameters:
3913bb7acecfSBarry Smith + dm     - The `DM` object
3914bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3915264ace61SBarry Smith 
3916264ace61SBarry Smith   Options Database Key:
3917bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3918264ace61SBarry Smith 
3919264ace61SBarry Smith   Level: intermediate
3920264ace61SBarry Smith 
3921bb7acecfSBarry Smith   Note:
39220462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3923bb7acecfSBarry Smith 
39241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3925264ace61SBarry Smith @*/
3926d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3927d71ae5a4SJacob Faibussowitsch {
3928264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3929264ace61SBarry Smith   PetscBool match;
3930264ace61SBarry Smith 
3931264ace61SBarry Smith   PetscFunctionBegin;
3932264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39339566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39343ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3935264ace61SBarry Smith 
39369566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39379566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39387a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3939264ace61SBarry Smith 
3940dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39419566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39429566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39439566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3945264ace61SBarry Smith }
3946264ace61SBarry Smith 
3947264ace61SBarry Smith /*@C
3948bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3949264ace61SBarry Smith 
3950264ace61SBarry Smith   Not Collective
3951264ace61SBarry Smith 
3952264ace61SBarry Smith   Input Parameter:
3953bb7acecfSBarry Smith . dm - The `DM`
3954264ace61SBarry Smith 
3955264ace61SBarry Smith   Output Parameter:
3956bb7acecfSBarry Smith . type - The `DMType` name
3957264ace61SBarry Smith 
3958264ace61SBarry Smith   Level: intermediate
3959264ace61SBarry Smith 
39601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3961264ace61SBarry Smith @*/
3962d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3963d71ae5a4SJacob Faibussowitsch {
3964264ace61SBarry Smith   PetscFunctionBegin;
3965264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39664f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39679566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3968264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3970264ace61SBarry Smith }
3971264ace61SBarry Smith 
397267a56275SMatthew G Knepley /*@C
3973bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
397467a56275SMatthew G Knepley 
397520f4b53cSBarry Smith   Collective
397667a56275SMatthew G Knepley 
397767a56275SMatthew G Knepley   Input Parameters:
3978bb7acecfSBarry Smith + dm      - the `DM`
3979bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
398067a56275SMatthew G Knepley 
398167a56275SMatthew G Knepley   Output Parameter:
3982bb7acecfSBarry Smith . M - pointer to new `DM`
398367a56275SMatthew G Knepley 
398420f4b53cSBarry Smith   Level: intermediate
398520f4b53cSBarry Smith 
398673ff1848SBarry Smith   Note:
3987bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3988bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3989bb7acecfSBarry Smith   of the input `DM`.
399067a56275SMatthew G Knepley 
39911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
399267a56275SMatthew G Knepley @*/
3993d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3994d71ae5a4SJacob Faibussowitsch {
399567a56275SMatthew G Knepley   DM        B;
399667a56275SMatthew G Knepley   char      convname[256];
3997c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
399867a56275SMatthew G Knepley 
399967a56275SMatthew G Knepley   PetscFunctionBegin;
400067a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400167a56275SMatthew G Knepley   PetscValidType(dm, 1);
40024f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40049566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4005c067b6caSMatthew G. Knepley   if (sametype) {
4006c067b6caSMatthew G. Knepley     *M = dm;
40079566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40083ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4009c067b6caSMatthew G. Knepley   } else {
40100298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
401167a56275SMatthew G Knepley 
401267a56275SMatthew G Knepley     /*
401367a56275SMatthew G Knepley        Order of precedence:
401467a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
401567a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
401667a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
401767a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
401867a56275SMatthew G Knepley        5) Use a really basic converter.
401967a56275SMatthew G Knepley     */
402067a56275SMatthew G Knepley 
402167a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40229566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40239566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40249566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40259566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40269566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40279566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
402867a56275SMatthew G Knepley     if (conv) goto foundconv;
402967a56275SMatthew G Knepley 
403067a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40319566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40329566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40339566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40349566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40359566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40369566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40379566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40389566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
403967a56275SMatthew G Knepley     if (conv) {
40409566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
404167a56275SMatthew G Knepley       goto foundconv;
404267a56275SMatthew G Knepley     }
404367a56275SMatthew G Knepley 
404467a56275SMatthew G Knepley #if 0
404567a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
404667a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40479566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
404867a56275SMatthew G Knepley     if (conv) goto foundconv;
404967a56275SMatthew G Knepley 
405067a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
405167a56275SMatthew G Knepley     if (dm->ops->convert) {
405267a56275SMatthew G Knepley       conv = dm->ops->convert;
405367a56275SMatthew G Knepley     }
405467a56275SMatthew G Knepley     if (conv) goto foundconv;
405567a56275SMatthew G Knepley #endif
405667a56275SMatthew G Knepley 
405767a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
405898921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
405967a56275SMatthew G Knepley 
406067a56275SMatthew G Knepley   foundconv:
40619566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40629566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
406312fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
406490b157c4SStefano Zampini     {
40654fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40666858538eSMatthew G. Knepley 
40674fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40684fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4069c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40709566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40719566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40729566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40739566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
407412fa691eSMatthew G. Knepley     }
40759566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
407667a56275SMatthew G Knepley   }
40779566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
407967a56275SMatthew G Knepley }
4080264ace61SBarry Smith 
4081264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4082264ace61SBarry Smith 
4083264ace61SBarry Smith /*@C
4084bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40851c84c290SBarry Smith 
40861c84c290SBarry Smith   Not Collective
40871c84c290SBarry Smith 
40881c84c290SBarry Smith   Input Parameters:
408920f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
409020f4b53cSBarry Smith - function - The creation routine itself
409120f4b53cSBarry Smith 
409220f4b53cSBarry Smith   Level: advanced
40931c84c290SBarry Smith 
409473ff1848SBarry Smith   Note:
409520f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40961c84c290SBarry Smith 
409760225df5SJacob Faibussowitsch   Example Usage:
40981c84c290SBarry Smith .vb
4099bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41001c84c290SBarry Smith .ve
41011c84c290SBarry Smith 
410220f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41031c84c290SBarry Smith .vb
41041c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41051c84c290SBarry Smith     DMSetType(DM,"my_da");
41061c84c290SBarry Smith .ve
41071c84c290SBarry Smith   or at runtime via the option
41081c84c290SBarry Smith .vb
41091c84c290SBarry Smith     -da_type my_da
41101c84c290SBarry Smith .ve
4111264ace61SBarry Smith 
41121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4113264ace61SBarry Smith @*/
4114d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4115d71ae5a4SJacob Faibussowitsch {
4116264ace61SBarry Smith   PetscFunctionBegin;
41179566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41189566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4120264ace61SBarry Smith }
4121264ace61SBarry Smith 
4122b859378eSBarry Smith /*@C
4123bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4124b859378eSBarry Smith 
412520f4b53cSBarry Smith   Collective
4126b859378eSBarry Smith 
4127b859378eSBarry Smith   Input Parameters:
4128bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4129bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4130bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4131bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4132b859378eSBarry Smith 
4133b859378eSBarry Smith   Level: intermediate
4134b859378eSBarry Smith 
4135b859378eSBarry Smith   Notes:
413655849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4137b859378eSBarry Smith 
4138bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4139bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4140bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4141cd7e8a5eSksagiyam 
41421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4143b859378eSBarry Smith @*/
4144d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4145d71ae5a4SJacob Faibussowitsch {
41469331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4147b859378eSBarry Smith 
4148b859378eSBarry Smith   PetscFunctionBegin;
4149b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4150b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41519566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41529566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41539566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41549566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41559331c7a4SMatthew G. Knepley   if (isbinary) {
41569331c7a4SMatthew G. Knepley     PetscInt classid;
41579331c7a4SMatthew G. Knepley     char     type[256];
4158b859378eSBarry Smith 
41599566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41607a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41619566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41629566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4163dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41649331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4165dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41669331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4169b859378eSBarry Smith }
4170b859378eSBarry Smith 
41717da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41727da65231SMatthew G Knepley 
41732ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41742ecf6ec3SMatthew G. Knepley {
41752ecf6ec3SMatthew G. Knepley   PetscInt f;
41762ecf6ec3SMatthew G. Knepley 
41772ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41782ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41792ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41802ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41812ecf6ec3SMatthew G. Knepley }
41822ecf6ec3SMatthew G. Knepley 
4183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4184d71ae5a4SJacob Faibussowitsch {
41851d47ebbbSSatish Balay   PetscInt f;
41861b30c384SMatthew G Knepley 
41877da65231SMatthew G Knepley   PetscFunctionBegin;
418863a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
418948a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41917da65231SMatthew G Knepley }
41927da65231SMatthew G Knepley 
41935962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41945962854dSMatthew G. Knepley {
41955962854dSMatthew G. Knepley   PetscInt f;
41965962854dSMatthew G. Knepley 
41975962854dSMatthew G. Knepley   PetscFunctionBegin;
41985962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41995962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42005962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42015962854dSMatthew G. Knepley }
42025962854dSMatthew G. Knepley 
4203d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4204d71ae5a4SJacob Faibussowitsch {
42051b30c384SMatthew G Knepley   PetscInt f, g;
42067da65231SMatthew G Knepley 
42077da65231SMatthew G Knepley   PetscFunctionBegin;
420863a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42091d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42109566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
421148a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42129566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42137da65231SMatthew G Knepley   }
42143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42157da65231SMatthew G Knepley }
4216e7c4fc90SDmitry Karpeev 
4217d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4218d71ae5a4SJacob Faibussowitsch {
42190c5b8624SToby Isaac   PetscInt           localSize, bs;
42200c5b8624SToby Isaac   PetscMPIInt        size;
42210c5b8624SToby Isaac   Vec                x, xglob;
42220c5b8624SToby Isaac   const PetscScalar *xarray;
4223e759306cSMatthew G. Knepley 
4224e759306cSMatthew G. Knepley   PetscFunctionBegin;
42259566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42269566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42279566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
422893ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42299566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42300c5b8624SToby Isaac   if (size > 1) {
42319566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42329566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42339566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42349566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42350c5b8624SToby Isaac   } else {
42360c5b8624SToby Isaac     xglob = x;
42370c5b8624SToby Isaac   }
42389566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42390c5b8624SToby Isaac   if (size > 1) {
42409566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42419566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42420c5b8624SToby Isaac   }
42439566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4245e759306cSMatthew G. Knepley }
4246e759306cSMatthew G. Knepley 
424788ed4aceSMatthew G Knepley /*@
4248bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4249061576a5SJed Brown 
4250061576a5SJed Brown   Input Parameter:
4251bb7acecfSBarry Smith . dm - The `DM`
4252061576a5SJed Brown 
4253061576a5SJed Brown   Output Parameter:
4254bb7acecfSBarry Smith . section - The `PetscSection`
4255061576a5SJed Brown 
425620f4b53cSBarry Smith   Options Database Key:
4257bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4258061576a5SJed Brown 
4259061576a5SJed Brown   Level: advanced
4260061576a5SJed Brown 
4261061576a5SJed Brown   Notes:
4262bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4263061576a5SJed Brown 
4264bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4265061576a5SJed Brown 
42661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4267061576a5SJed Brown @*/
4268d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4269d71ae5a4SJacob Faibussowitsch {
4270061576a5SJed Brown   PetscFunctionBegin;
42719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4273061576a5SJed Brown }
4274061576a5SJed Brown 
4275061576a5SJed Brown /*@
4276bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
427788ed4aceSMatthew G Knepley 
427888ed4aceSMatthew G Knepley   Input Parameter:
4279bb7acecfSBarry Smith . dm - The `DM`
428088ed4aceSMatthew G Knepley 
428188ed4aceSMatthew G Knepley   Output Parameter:
4282bb7acecfSBarry Smith . section - The `PetscSection`
428388ed4aceSMatthew G Knepley 
428420f4b53cSBarry Smith   Options Database Key:
4285bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4286e5893cccSMatthew G. Knepley 
428788ed4aceSMatthew G Knepley   Level: intermediate
428888ed4aceSMatthew G Knepley 
4289bb7acecfSBarry Smith   Note:
4290bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
429188ed4aceSMatthew G Knepley 
42921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
429388ed4aceSMatthew G Knepley @*/
4294d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4295d71ae5a4SJacob Faibussowitsch {
429688ed4aceSMatthew G Knepley   PetscFunctionBegin;
429788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42984f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42991bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4300e5e52638SMatthew G. Knepley     PetscInt d;
4301e5e52638SMatthew G. Knepley 
430245480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
430345480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
430445480ffeSMatthew G. Knepley       PetscViewer       viewer;
430545480ffeSMatthew G. Knepley       PetscViewerFormat format;
430645480ffeSMatthew G. Knepley       PetscBool         flg;
430745480ffeSMatthew G. Knepley 
43089566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43099566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
431045480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43119566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43129566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
431345480ffeSMatthew G. Knepley       }
431445480ffeSMatthew G. Knepley       if (flg) {
43159566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43169566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4317cd791dc2SBarry Smith         PetscCall(PetscOptionsRestoreViewer(&viewer));
431845480ffeSMatthew G. Knepley       }
431945480ffeSMatthew G. Knepley     }
4320dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43219566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43222f0f8703SMatthew G. Knepley   }
43231bb6d2a8SBarry Smith   *section = dm->localSection;
43243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432588ed4aceSMatthew G Knepley }
432688ed4aceSMatthew G Knepley 
432788ed4aceSMatthew G Knepley /*@
4328bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4329061576a5SJed Brown 
4330061576a5SJed Brown   Input Parameters:
4331bb7acecfSBarry Smith + dm      - The `DM`
4332bb7acecfSBarry Smith - section - The `PetscSection`
4333061576a5SJed Brown 
4334061576a5SJed Brown   Level: advanced
4335061576a5SJed Brown 
4336061576a5SJed Brown   Notes:
4337bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4338061576a5SJed Brown 
4339bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4340061576a5SJed Brown 
43411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4342061576a5SJed Brown @*/
4343d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4344d71ae5a4SJacob Faibussowitsch {
4345061576a5SJed Brown   PetscFunctionBegin;
43469566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4348061576a5SJed Brown }
4349061576a5SJed Brown 
4350061576a5SJed Brown /*@
4351bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
435288ed4aceSMatthew G Knepley 
435388ed4aceSMatthew G Knepley   Input Parameters:
4354bb7acecfSBarry Smith + dm      - The `DM`
4355bb7acecfSBarry Smith - section - The `PetscSection`
435688ed4aceSMatthew G Knepley 
435788ed4aceSMatthew G Knepley   Level: intermediate
435888ed4aceSMatthew G Knepley 
4359bb7acecfSBarry Smith   Note:
4360bb7acecfSBarry Smith   Any existing Section will be destroyed
436188ed4aceSMatthew G Knepley 
43621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
436388ed4aceSMatthew G Knepley @*/
4364d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4365d71ae5a4SJacob Faibussowitsch {
4366c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4367af122d2aSMatthew G Knepley   PetscInt f;
436888ed4aceSMatthew G Knepley 
436988ed4aceSMatthew G Knepley   PetscFunctionBegin;
437088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4371b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43739566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43741bb6d2a8SBarry Smith   dm->localSection = section;
43759566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4376af122d2aSMatthew G Knepley   if (numFields) {
43779566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4378af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43790f21e855SMatthew G. Knepley       PetscObject disc;
4380af122d2aSMatthew G Knepley       const char *name;
4381af122d2aSMatthew G Knepley 
43829566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43839566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43849566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4385af122d2aSMatthew G Knepley     }
4386af122d2aSMatthew G Knepley   }
438750350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
438850350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43899566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
439050350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
439152947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
439250350c8eSStefano Zampini 
439350350c8eSStefano Zampini   /* Clear scratch vectors */
439450350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
439550350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
439650350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
439750350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
439988ed4aceSMatthew G Knepley }
440088ed4aceSMatthew G Knepley 
4401adc21957SMatthew G. Knepley /*@C
4402d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4403adc21957SMatthew G. Knepley 
4404adc21957SMatthew G. Knepley   Input Parameter:
4405adc21957SMatthew G. Knepley . dm - The `DM`
4406adc21957SMatthew G. Knepley 
44079cde84edSJose E. Roman   Output Parameters:
4408adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4409b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4410adc21957SMatthew G. Knepley 
4411adc21957SMatthew G. Knepley   Level: developer
4412adc21957SMatthew G. Knepley 
4413adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4414adc21957SMatthew G. Knepley @*/
4415adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4416adc21957SMatthew G. Knepley {
4417adc21957SMatthew G. Knepley   PetscFunctionBegin;
4418adc21957SMatthew G. Knepley   *perm        = NULL;
4419adc21957SMatthew G. Knepley   *blockStarts = NULL;
4420adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4421adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4422adc21957SMatthew G. Knepley }
4423adc21957SMatthew G. Knepley 
44249435951eSToby Isaac /*@
4425bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44269435951eSToby Isaac 
442720f4b53cSBarry Smith   not Collective
4428e228b242SToby Isaac 
44299435951eSToby Isaac   Input Parameter:
4430bb7acecfSBarry Smith . dm - The `DM`
44319435951eSToby Isaac 
4432d8d19677SJose E. Roman   Output Parameters:
443320f4b53cSBarry 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.
443420f4b53cSBarry 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.
443579769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44369435951eSToby Isaac 
44379435951eSToby Isaac   Level: advanced
44389435951eSToby Isaac 
4439bb7acecfSBarry Smith   Note:
4440bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44419435951eSToby Isaac 
44421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44439435951eSToby Isaac @*/
4444d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4445d71ae5a4SJacob Faibussowitsch {
44469435951eSToby Isaac   PetscFunctionBegin;
44479435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4448dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44493b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44503b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
445179769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44539435951eSToby Isaac }
44549435951eSToby Isaac 
44559435951eSToby Isaac /*@
4456bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44579435951eSToby Isaac 
445820f4b53cSBarry Smith   Collective
4459e228b242SToby Isaac 
44609435951eSToby Isaac   Input Parameters:
4461bb7acecfSBarry Smith + dm      - The `DM`
4462bb7acecfSBarry 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).
446320f4b53cSBarry 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).
446420f4b53cSBarry 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).
44659435951eSToby Isaac 
44669435951eSToby Isaac   Level: advanced
44679435951eSToby Isaac 
446820f4b53cSBarry Smith   Notes:
446920f4b53cSBarry 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()`.
447020f4b53cSBarry Smith 
447120f4b53cSBarry 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.
447220f4b53cSBarry Smith 
4473bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44749435951eSToby Isaac 
44751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44769435951eSToby Isaac @*/
4477d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4478d71ae5a4SJacob Faibussowitsch {
4479e228b242SToby Isaac   PetscMPIInt result;
44809435951eSToby Isaac 
44819435951eSToby Isaac   PetscFunctionBegin;
44829435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4483e228b242SToby Isaac   if (section) {
4484e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44859566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44867a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4487e228b242SToby Isaac   }
4488e228b242SToby Isaac   if (mat) {
4489e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44909566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44917a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4492e228b242SToby Isaac   }
449379769bd5SJed Brown   if (bias) {
449479769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44959566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
449679769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
449779769bd5SJed Brown   }
44989566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44999566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45003b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45029566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45033b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45059566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
450679769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45089435951eSToby Isaac }
45099435951eSToby Isaac 
4510497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4511507e4973SMatthew G. Knepley /*
4512bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4513507e4973SMatthew G. Knepley 
4514507e4973SMatthew G. Knepley   Input Parameters:
4515bb7acecfSBarry Smith + dm - The `DM`
4516bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4517bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4518507e4973SMatthew G. Knepley 
4519507e4973SMatthew G. Knepley   Level: intermediate
4520507e4973SMatthew G. Knepley 
45211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4522507e4973SMatthew G. Knepley */
4523d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4524d71ae5a4SJacob Faibussowitsch {
4525507e4973SMatthew G. Knepley   MPI_Comm        comm;
4526507e4973SMatthew G. Knepley   PetscLayout     layout;
4527507e4973SMatthew G. Knepley   const PetscInt *ranges;
4528507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4529507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4530507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4531507e4973SMatthew G. Knepley 
4532507e4973SMatthew G. Knepley   PetscFunctionBegin;
45339566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4534507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45359566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45379566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45389566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45399566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45409566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45419566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45429566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4544507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4545f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4546507e4973SMatthew G. Knepley 
45479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4553507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45549371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45559371c9d4SSatish 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));
45569371c9d4SSatish Balay       valid = PETSC_FALSE;
45579371c9d4SSatish Balay     }
45589371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45599371c9d4SSatish 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));
45609371c9d4SSatish Balay       valid = PETSC_FALSE;
45619371c9d4SSatish Balay     }
4562507e4973SMatthew G. Knepley     if (gdof < 0) {
4563507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4564507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4565507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4566507e4973SMatthew G. Knepley 
45679566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4568507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45699371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45709371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45719371c9d4SSatish Balay           valid = PETSC_FALSE;
45729371c9d4SSatish Balay           break;
45739371c9d4SSatish Balay         }
4574507e4973SMatthew G. Knepley       }
4575507e4973SMatthew G. Knepley     }
4576507e4973SMatthew G. Knepley   }
45779566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45789566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45791c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4580507e4973SMatthew G. Knepley   if (!gvalid) {
45819566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4582507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4583507e4973SMatthew G. Knepley   }
45843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4585507e4973SMatthew G. Knepley }
4586f741bcd2SMatthew G. Knepley #endif
4587507e4973SMatthew G. Knepley 
45885f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45896725e60dSJed Brown {
45906725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45914d86920dSPierre Jolivet 
45926725e60dSJed Brown   PetscFunctionBegin;
45936725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45944f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45955f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45966725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45976725e60dSJed Brown   else *sf = dm->sf;
45983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45996725e60dSJed Brown }
46006725e60dSJed Brown 
460188ed4aceSMatthew G Knepley /*@
4602bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
460388ed4aceSMatthew G Knepley 
460420f4b53cSBarry Smith   Collective
46058b1ab98fSJed Brown 
460688ed4aceSMatthew G Knepley   Input Parameter:
4607bb7acecfSBarry Smith . dm - The `DM`
460888ed4aceSMatthew G Knepley 
460988ed4aceSMatthew G Knepley   Output Parameter:
4610bb7acecfSBarry Smith . section - The `PetscSection`
461188ed4aceSMatthew G Knepley 
461288ed4aceSMatthew G Knepley   Level: intermediate
461388ed4aceSMatthew G Knepley 
4614bb7acecfSBarry Smith   Note:
4615bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
461688ed4aceSMatthew G Knepley 
46171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
461888ed4aceSMatthew G Knepley @*/
4619d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4620d71ae5a4SJacob Faibussowitsch {
462188ed4aceSMatthew G Knepley   PetscFunctionBegin;
462288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46234f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46241bb6d2a8SBarry Smith   if (!dm->globalSection) {
4625fd59a867SMatthew G. Knepley     PetscSection s;
46266725e60dSJed Brown     PetscSF      sf;
4627fd59a867SMatthew G. Knepley 
46289566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46297a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46307a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46315f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4632eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46339566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46359566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
463688ed4aceSMatthew G Knepley   }
46371bb6d2a8SBarry Smith   *section = dm->globalSection;
46383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
463988ed4aceSMatthew G Knepley }
464088ed4aceSMatthew G Knepley 
4641b21d0597SMatthew G Knepley /*@
4642bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4643b21d0597SMatthew G Knepley 
4644b21d0597SMatthew G Knepley   Input Parameters:
4645bb7acecfSBarry Smith + dm      - The `DM`
46462fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4647b21d0597SMatthew G Knepley 
4648b21d0597SMatthew G Knepley   Level: intermediate
4649b21d0597SMatthew G Knepley 
4650bb7acecfSBarry Smith   Note:
4651bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4652b21d0597SMatthew G Knepley 
46531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4654b21d0597SMatthew G Knepley @*/
4655d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4656d71ae5a4SJacob Faibussowitsch {
4657b21d0597SMatthew G Knepley   PetscFunctionBegin;
4658b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46595080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46621bb6d2a8SBarry Smith   dm->globalSection = section;
4663497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46649566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4665507e4973SMatthew G. Knepley #endif
466650350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
466750350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
466852947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
466950350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
467050350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4672b21d0597SMatthew G Knepley }
4673b21d0597SMatthew G Knepley 
467488ed4aceSMatthew G Knepley /*@
4675bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4676bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
467788ed4aceSMatthew G Knepley 
467888ed4aceSMatthew G Knepley   Input Parameter:
4679bb7acecfSBarry Smith . dm - The `DM`
468088ed4aceSMatthew G Knepley 
468188ed4aceSMatthew G Knepley   Output Parameter:
4682bb7acecfSBarry Smith . sf - The `PetscSF`
468388ed4aceSMatthew G Knepley 
468488ed4aceSMatthew G Knepley   Level: intermediate
468588ed4aceSMatthew G Knepley 
4686bb7acecfSBarry Smith   Note:
4687bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
468888ed4aceSMatthew G Knepley 
46891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
469088ed4aceSMatthew G Knepley @*/
4691d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4692d71ae5a4SJacob Faibussowitsch {
469388ed4aceSMatthew G Knepley   PetscInt nroots;
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   PetscFunctionBegin;
469688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46974f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
469848a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46999566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
470088ed4aceSMatthew G Knepley   if (nroots < 0) {
470188ed4aceSMatthew G Knepley     PetscSection section, gSection;
470288ed4aceSMatthew G Knepley 
47039566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
470431ea6d37SMatthew G Knepley     if (section) {
47059566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47069566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
470731ea6d37SMatthew G Knepley     } else {
47080298fd71SBarry Smith       *sf = NULL;
47093ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
471031ea6d37SMatthew G Knepley     }
471188ed4aceSMatthew G Knepley   }
47121bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471488ed4aceSMatthew G Knepley }
471588ed4aceSMatthew G Knepley 
471688ed4aceSMatthew G Knepley /*@
4717bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
471888ed4aceSMatthew G Knepley 
471988ed4aceSMatthew G Knepley   Input Parameters:
4720bb7acecfSBarry Smith + dm - The `DM`
4721bb7acecfSBarry Smith - sf - The `PetscSF`
472288ed4aceSMatthew G Knepley 
472388ed4aceSMatthew G Knepley   Level: intermediate
472488ed4aceSMatthew G Knepley 
4725bb7acecfSBarry Smith   Note:
4726bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
472788ed4aceSMatthew G Knepley 
47281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
472988ed4aceSMatthew G Knepley @*/
4730d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4731d71ae5a4SJacob Faibussowitsch {
473288ed4aceSMatthew G Knepley   PetscFunctionBegin;
473388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4734b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47369566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47371bb6d2a8SBarry Smith   dm->sectionSF = sf;
47383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473988ed4aceSMatthew G Knepley }
474088ed4aceSMatthew G Knepley 
474188ed4aceSMatthew G Knepley /*@C
4742bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
474388ed4aceSMatthew G Knepley   describing the data layout.
474488ed4aceSMatthew G Knepley 
474588ed4aceSMatthew G Knepley   Input Parameters:
4746bb7acecfSBarry Smith + dm            - The `DM`
4747bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4748bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
474988ed4aceSMatthew G Knepley 
47501bb6d2a8SBarry Smith   Level: developer
47511bb6d2a8SBarry Smith 
4752bb7acecfSBarry Smith   Note:
4753bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4754bb7acecfSBarry Smith 
475573ff1848SBarry Smith   Developer Note:
4756bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4757bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4758b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4759b6971eaeSBarry Smith   they create, this returns nothing
47601bb6d2a8SBarry Smith 
47611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
476288ed4aceSMatthew G Knepley @*/
4763d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4764d71ae5a4SJacob Faibussowitsch {
476588ed4aceSMatthew G Knepley   PetscFunctionBegin;
476688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47679566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
476988ed4aceSMatthew G Knepley }
4770af122d2aSMatthew G Knepley 
4771b21d0597SMatthew G Knepley /*@
4772bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4773bb7acecfSBarry Smith 
4774bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4775b21d0597SMatthew G Knepley 
4776b21d0597SMatthew G Knepley   Input Parameter:
4777bb7acecfSBarry Smith . dm - The `DM`
4778b21d0597SMatthew G Knepley 
4779b21d0597SMatthew G Knepley   Output Parameter:
4780bb7acecfSBarry Smith . sf - The `PetscSF`
4781b21d0597SMatthew G Knepley 
4782b21d0597SMatthew G Knepley   Level: intermediate
4783b21d0597SMatthew G Knepley 
4784bb7acecfSBarry Smith   Note:
4785bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4786b21d0597SMatthew G Knepley 
47871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4788b21d0597SMatthew G Knepley @*/
4789d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4790d71ae5a4SJacob Faibussowitsch {
4791b21d0597SMatthew G Knepley   PetscFunctionBegin;
4792b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47934f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4794b21d0597SMatthew G Knepley   *sf = dm->sf;
47953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4796b21d0597SMatthew G Knepley }
4797b21d0597SMatthew G Knepley 
4798057b4bcdSMatthew G Knepley /*@
4799bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4800bb7acecfSBarry Smith 
480120f4b53cSBarry Smith   Collective
4802057b4bcdSMatthew G Knepley 
4803057b4bcdSMatthew G Knepley   Input Parameters:
4804bb7acecfSBarry Smith + dm - The `DM`
4805bb7acecfSBarry Smith - sf - The `PetscSF`
4806057b4bcdSMatthew G Knepley 
4807057b4bcdSMatthew G Knepley   Level: intermediate
4808057b4bcdSMatthew G Knepley 
48091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4810057b4bcdSMatthew G Knepley @*/
4811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4812d71ae5a4SJacob Faibussowitsch {
4813057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4814057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4815b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48179566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4818057b4bcdSMatthew G Knepley   dm->sf = sf;
48193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4820057b4bcdSMatthew G Knepley }
4821057b4bcdSMatthew G Knepley 
48224f37162bSMatthew G. Knepley /*@
4823bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48244f37162bSMatthew G. Knepley 
48254f37162bSMatthew G. Knepley   Input Parameter:
4826bb7acecfSBarry Smith . dm - The `DM`
48274f37162bSMatthew G. Knepley 
48284f37162bSMatthew G. Knepley   Output Parameter:
4829bb7acecfSBarry Smith . sf - The `PetscSF`
48304f37162bSMatthew G. Knepley 
48314f37162bSMatthew G. Knepley   Level: intermediate
48324f37162bSMatthew G. Knepley 
4833bb7acecfSBarry Smith   Note:
4834bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48354f37162bSMatthew G. Knepley 
48361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48374f37162bSMatthew G. Knepley @*/
4838d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4839d71ae5a4SJacob Faibussowitsch {
48404f37162bSMatthew G. Knepley   PetscFunctionBegin;
48414f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48424f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48434f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48454f37162bSMatthew G. Knepley }
48464f37162bSMatthew G. Knepley 
48474f37162bSMatthew G. Knepley /*@
48484f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48494f37162bSMatthew G. Knepley 
48504f37162bSMatthew G. Knepley   Input Parameters:
48514f37162bSMatthew G. Knepley + dm - The DM
48524f37162bSMatthew G. Knepley - sf - The PetscSF
48534f37162bSMatthew G. Knepley 
48544f37162bSMatthew G. Knepley   Level: intermediate
48554f37162bSMatthew G. Knepley 
48561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48574f37162bSMatthew G. Knepley @*/
4858d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4859d71ae5a4SJacob Faibussowitsch {
48604f37162bSMatthew G. Knepley   PetscFunctionBegin;
48614f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48624f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48649566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48654f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48674f37162bSMatthew G. Knepley }
48684f37162bSMatthew G. Knepley 
4869d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4870d71ae5a4SJacob Faibussowitsch {
487134aa8a36SMatthew G. Knepley   PetscClassId id;
487234aa8a36SMatthew G. Knepley 
487334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48749566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
487534aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48769566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
487734aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48789566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
487917c1d62eSMatthew G. Knepley   } else {
48809566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488134aa8a36SMatthew G. Knepley   }
48823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488334aa8a36SMatthew G. Knepley }
488434aa8a36SMatthew G. Knepley 
4885d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4886d71ae5a4SJacob Faibussowitsch {
488744a7f3ddSMatthew G. Knepley   RegionField *tmpr;
488844a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
488944a7f3ddSMatthew G. Knepley 
489044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48913ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48929566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
489344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48949371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48959371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48969371c9d4SSatish Balay     tmpr[f].label       = NULL;
48979371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48989371c9d4SSatish Balay   }
48999566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
490044a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
490144a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490344a7f3ddSMatthew G. Knepley }
490444a7f3ddSMatthew G. Knepley 
490544a7f3ddSMatthew G. Knepley /*@
490620f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
490744a7f3ddSMatthew G. Knepley 
490820f4b53cSBarry Smith   Logically Collective
490944a7f3ddSMatthew G. Knepley 
491044a7f3ddSMatthew G. Knepley   Input Parameter:
491120f4b53cSBarry Smith . dm - The `DM`
491244a7f3ddSMatthew G. Knepley 
491344a7f3ddSMatthew G. Knepley   Level: intermediate
491444a7f3ddSMatthew G. Knepley 
49151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
491644a7f3ddSMatthew G. Knepley @*/
4917d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4918d71ae5a4SJacob Faibussowitsch {
491944a7f3ddSMatthew G. Knepley   PetscInt f;
492044a7f3ddSMatthew G. Knepley 
492144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
492244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
492344a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49249566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49259566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
492644a7f3ddSMatthew G. Knepley   }
49279566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
492844a7f3ddSMatthew G. Knepley   dm->fields = NULL;
492944a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
493144a7f3ddSMatthew G. Knepley }
493244a7f3ddSMatthew G. Knepley 
4933689b5837SMatthew G. Knepley /*@
493420f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4935689b5837SMatthew G. Knepley 
493620f4b53cSBarry Smith   Not Collective
4937689b5837SMatthew G. Knepley 
4938689b5837SMatthew G. Knepley   Input Parameter:
493920f4b53cSBarry Smith . dm - The `DM`
4940689b5837SMatthew G. Knepley 
4941689b5837SMatthew G. Knepley   Output Parameter:
494260225df5SJacob Faibussowitsch . numFields - The number of fields
4943689b5837SMatthew G. Knepley 
4944689b5837SMatthew G. Knepley   Level: intermediate
4945689b5837SMatthew G. Knepley 
49461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4947689b5837SMatthew G. Knepley @*/
4948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4949d71ae5a4SJacob Faibussowitsch {
49500f21e855SMatthew G. Knepley   PetscFunctionBegin;
49510f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49524f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
495344a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4955af122d2aSMatthew G Knepley }
4956af122d2aSMatthew G Knepley 
4957689b5837SMatthew G. Knepley /*@
495820f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4959689b5837SMatthew G. Knepley 
496020f4b53cSBarry Smith   Logically Collective
4961689b5837SMatthew G. Knepley 
4962689b5837SMatthew G. Knepley   Input Parameters:
496320f4b53cSBarry Smith + dm        - The `DM`
496460225df5SJacob Faibussowitsch - numFields - The number of fields
4965689b5837SMatthew G. Knepley 
4966689b5837SMatthew G. Knepley   Level: intermediate
4967689b5837SMatthew G. Knepley 
49681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4969689b5837SMatthew G. Knepley @*/
4970d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4971d71ae5a4SJacob Faibussowitsch {
49720f21e855SMatthew G. Knepley   PetscInt Nf, f;
4973af122d2aSMatthew G Knepley 
4974af122d2aSMatthew G Knepley   PetscFunctionBegin;
4975af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49769566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49770f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49780f21e855SMatthew G. Knepley     PetscContainer obj;
49790f21e855SMatthew G. Knepley 
49809566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49819566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49829566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4983af122d2aSMatthew G Knepley   }
49843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4985af122d2aSMatthew G Knepley }
4986af122d2aSMatthew G Knepley 
4987c1929be8SMatthew G. Knepley /*@
4988bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4989c1929be8SMatthew G. Knepley 
499020f4b53cSBarry Smith   Not Collective
4991c1929be8SMatthew G. Knepley 
4992c1929be8SMatthew G. Knepley   Input Parameters:
4993bb7acecfSBarry Smith + dm - The `DM`
4994c1929be8SMatthew G. Knepley - f  - The field number
4995c1929be8SMatthew G. Knepley 
499644a7f3ddSMatthew G. Knepley   Output Parameters:
499720f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
499820f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4999c1929be8SMatthew G. Knepley 
500044a7f3ddSMatthew G. Knepley   Level: intermediate
5001c1929be8SMatthew G. Knepley 
50021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5003c1929be8SMatthew G. Knepley @*/
5004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5005d71ae5a4SJacob Faibussowitsch {
5006af122d2aSMatthew G Knepley   PetscFunctionBegin;
5007af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50084f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50097a8be351SBarry 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);
501044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5011bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5013decb47aaSMatthew G. Knepley }
5014decb47aaSMatthew G. Knepley 
5015083401c6SMatthew G. Knepley /* Does not clear the DS */
5016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5017d71ae5a4SJacob Faibussowitsch {
5018083401c6SMatthew G. Knepley   PetscFunctionBegin;
50199566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50209566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50219566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5022083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5023bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50249566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5025bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5027083401c6SMatthew G. Knepley }
5028083401c6SMatthew G. Knepley 
502946560f82SMatthew G. Knepley /*@C
5030bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5031bb7acecfSBarry Smith   the field numbering.
5032c1929be8SMatthew G. Knepley 
503320f4b53cSBarry Smith   Logically Collective
5034c1929be8SMatthew G. Knepley 
5035c1929be8SMatthew G. Knepley   Input Parameters:
5036bb7acecfSBarry Smith + dm    - The `DM`
5037c1929be8SMatthew G. Knepley . f     - The field number
503820f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5039bb7acecfSBarry Smith - disc  - The discretization object
5040c1929be8SMatthew G. Knepley 
504144a7f3ddSMatthew G. Knepley   Level: intermediate
5042c1929be8SMatthew G. Knepley 
50431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5044c1929be8SMatthew G. Knepley @*/
5045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5046d71ae5a4SJacob Faibussowitsch {
5047decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5048decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5049e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5050bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50517a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5052bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5053bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50549566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
505644a7f3ddSMatthew G. Knepley }
505744a7f3ddSMatthew G. Knepley 
505846560f82SMatthew G. Knepley /*@C
5059bb7acecfSBarry 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)
5060bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
506144a7f3ddSMatthew G. Knepley 
506220f4b53cSBarry Smith   Logically Collective
506344a7f3ddSMatthew G. Knepley 
506444a7f3ddSMatthew G. Knepley   Input Parameters:
5065bb7acecfSBarry Smith + dm    - The `DM`
506620f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5067bb7acecfSBarry Smith - disc  - The discretization object
506844a7f3ddSMatthew G. Knepley 
506944a7f3ddSMatthew G. Knepley   Level: intermediate
507044a7f3ddSMatthew G. Knepley 
5071bb7acecfSBarry Smith   Notes:
5072bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5073bb7acecfSBarry Smith 
5074da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5075bb7acecfSBarry 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
5076bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5077bb7acecfSBarry Smith 
50781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
507944a7f3ddSMatthew G. Knepley @*/
5080d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5081d71ae5a4SJacob Faibussowitsch {
508244a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
508344a7f3ddSMatthew G. Knepley 
508444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
508544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5086064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5087bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50889566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
508944a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5090bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5092bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5093bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50949566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5096af122d2aSMatthew G Knepley }
50976636e97aSMatthew G Knepley 
5098e5e52638SMatthew G. Knepley /*@
5099e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5100e0b68406SMatthew Knepley 
510120f4b53cSBarry Smith   Logically Collective
5102e0b68406SMatthew Knepley 
5103e0b68406SMatthew Knepley   Input Parameters:
5104bb7acecfSBarry Smith + dm          - The `DM`
5105e0b68406SMatthew Knepley . f           - The field index
5106bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5107e0b68406SMatthew Knepley 
5108e0b68406SMatthew Knepley   Level: intermediate
5109e0b68406SMatthew Knepley 
51101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5111e0b68406SMatthew Knepley @*/
5112d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5113d71ae5a4SJacob Faibussowitsch {
5114e0b68406SMatthew Knepley   PetscFunctionBegin;
511563a3b9bcSJacob 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);
5116e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5118e0b68406SMatthew Knepley }
5119e0b68406SMatthew Knepley 
5120e0b68406SMatthew Knepley /*@
5121e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5122e0b68406SMatthew Knepley 
512320f4b53cSBarry Smith   Not Collective
5124e0b68406SMatthew Knepley 
5125e0b68406SMatthew Knepley   Input Parameters:
5126bb7acecfSBarry Smith + dm - The `DM`
5127e0b68406SMatthew Knepley - f  - The field index
5128e0b68406SMatthew Knepley 
5129e0b68406SMatthew Knepley   Output Parameter:
5130e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5131e0b68406SMatthew Knepley 
5132e0b68406SMatthew Knepley   Level: intermediate
5133e0b68406SMatthew Knepley 
513460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5135e0b68406SMatthew Knepley @*/
5136d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5137d71ae5a4SJacob Faibussowitsch {
5138e0b68406SMatthew Knepley   PetscFunctionBegin;
513963a3b9bcSJacob 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);
5140e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5142e0b68406SMatthew Knepley }
5143e0b68406SMatthew Knepley 
5144e0b68406SMatthew Knepley /*@
5145bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5146e5e52638SMatthew G. Knepley 
514720f4b53cSBarry Smith   Collective
5148e5e52638SMatthew G. Knepley 
5149e5e52638SMatthew G. Knepley   Input Parameter:
5150bb7acecfSBarry Smith . dm - The `DM`
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley   Output Parameter:
5153bb7acecfSBarry Smith . newdm - The `DM`
5154e5e52638SMatthew G. Knepley 
5155e5e52638SMatthew G. Knepley   Level: advanced
5156e5e52638SMatthew G. Knepley 
51571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5158e5e52638SMatthew G. Knepley @*/
5159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5160d71ae5a4SJacob Faibussowitsch {
5161e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5162e5e52638SMatthew G. Knepley 
5163e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51643ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51659566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51669566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5167e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5168e5e52638SMatthew G. Knepley     DMLabel     label;
5169e5e52638SMatthew G. Knepley     PetscObject field;
517034aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5171e5e52638SMatthew G. Knepley 
51729566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51739566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51749566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51759566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
517634aa8a36SMatthew G. Knepley   }
51773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517834aa8a36SMatthew G. Knepley }
517934aa8a36SMatthew G. Knepley 
518034aa8a36SMatthew G. Knepley /*@
518134aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
518234aa8a36SMatthew G. Knepley 
518320f4b53cSBarry Smith   Not Collective
518434aa8a36SMatthew G. Knepley 
518534aa8a36SMatthew G. Knepley   Input Parameters:
518620f4b53cSBarry Smith + dm - The `DM` object
518720f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
518834aa8a36SMatthew G. Knepley 
5189d8d19677SJose E. Roman   Output Parameters:
519034aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
519134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
519234aa8a36SMatthew G. Knepley 
519334aa8a36SMatthew G. Knepley   Level: developer
519434aa8a36SMatthew G. Knepley 
519520f4b53cSBarry Smith   Notes:
519620f4b53cSBarry Smith .vb
519720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
520020f4b53cSBarry Smith .ve
520120f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
520220f4b53cSBarry Smith 
52031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
520434aa8a36SMatthew G. Knepley @*/
5205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5206d71ae5a4SJacob Faibussowitsch {
520734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
520834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52094f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52104f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
521134aa8a36SMatthew G. Knepley   if (f < 0) {
521234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
521334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
521434aa8a36SMatthew G. Knepley   } else {
521534aa8a36SMatthew G. Knepley     PetscInt Nf;
521634aa8a36SMatthew G. Knepley 
52179566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52187a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
521934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
522034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
522134aa8a36SMatthew G. Knepley   }
52223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
522334aa8a36SMatthew G. Knepley }
522434aa8a36SMatthew G. Knepley 
522534aa8a36SMatthew G. Knepley /*@
522634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
522734aa8a36SMatthew G. Knepley 
522820f4b53cSBarry Smith   Not Collective
522934aa8a36SMatthew G. Knepley 
523034aa8a36SMatthew G. Knepley   Input Parameters:
523120f4b53cSBarry Smith + dm         - The `DM` object
523234aa8a36SMatthew G. Knepley . f          - The field number
523334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
523434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
523534aa8a36SMatthew G. Knepley 
523634aa8a36SMatthew G. Knepley   Level: developer
523734aa8a36SMatthew G. Knepley 
523820f4b53cSBarry Smith   Notes:
523920f4b53cSBarry Smith .vb
524020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
524120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
524220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
524320f4b53cSBarry Smith .ve
524420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
524520f4b53cSBarry Smith 
52461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
524734aa8a36SMatthew G. Knepley @*/
5248d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5249d71ae5a4SJacob Faibussowitsch {
525034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
525134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
525234aa8a36SMatthew G. Knepley   if (f < 0) {
525334aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
525434aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
525534aa8a36SMatthew G. Knepley   } else {
525634aa8a36SMatthew G. Knepley     PetscInt Nf;
525734aa8a36SMatthew G. Knepley 
52589566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52597a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
526034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
526134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5262e5e52638SMatthew G. Knepley   }
52633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5264e5e52638SMatthew G. Knepley }
5265e5e52638SMatthew G. Knepley 
5266b0441da4SMatthew G. Knepley /*@
5267b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5268b0441da4SMatthew G. Knepley 
5269b0441da4SMatthew G. Knepley   Not collective
5270b0441da4SMatthew G. Knepley 
5271f899ff85SJose E. Roman   Input Parameter:
527220f4b53cSBarry Smith . dm - The `DM` object
5273b0441da4SMatthew G. Knepley 
5274d8d19677SJose E. Roman   Output Parameters:
5275b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5276b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5277b0441da4SMatthew G. Knepley 
5278b0441da4SMatthew G. Knepley   Level: developer
5279b0441da4SMatthew G. Knepley 
528020f4b53cSBarry Smith   Notes:
528120f4b53cSBarry Smith .vb
528220f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
528320f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
528420f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
528520f4b53cSBarry Smith .ve
528620f4b53cSBarry Smith 
52871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5288b0441da4SMatthew G. Knepley @*/
5289d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5290d71ae5a4SJacob Faibussowitsch {
5291b0441da4SMatthew G. Knepley   PetscInt Nf;
5292b0441da4SMatthew G. Knepley 
5293b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5294b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52954f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52964f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52979566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5298b0441da4SMatthew G. Knepley   if (!Nf) {
52999566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5300b0441da4SMatthew G. Knepley   } else {
53019566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5302b0441da4SMatthew G. Knepley   }
53033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5304b0441da4SMatthew G. Knepley }
5305b0441da4SMatthew G. Knepley 
5306b0441da4SMatthew G. Knepley /*@
5307b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5308b0441da4SMatthew G. Knepley 
530920f4b53cSBarry Smith   Not Collective
5310b0441da4SMatthew G. Knepley 
5311b0441da4SMatthew G. Knepley   Input Parameters:
531220f4b53cSBarry Smith + dm         - The `DM` object
5313b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5314b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5315b0441da4SMatthew G. Knepley 
5316b0441da4SMatthew G. Knepley   Level: developer
5317b0441da4SMatthew G. Knepley 
531820f4b53cSBarry Smith   Notes:
531920f4b53cSBarry Smith .vb
532020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
532120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
532220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
532320f4b53cSBarry Smith .ve
532420f4b53cSBarry Smith 
53251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5326b0441da4SMatthew G. Knepley @*/
5327d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5328d71ae5a4SJacob Faibussowitsch {
5329b0441da4SMatthew G. Knepley   PetscInt Nf;
5330b0441da4SMatthew G. Knepley 
5331b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5332b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53339566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5334b0441da4SMatthew G. Knepley   if (!Nf) {
53359566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5336b0441da4SMatthew G. Knepley   } else {
53379566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5338e5e52638SMatthew G. Knepley   }
53393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5340e5e52638SMatthew G. Knepley }
5341e5e52638SMatthew G. Knepley 
5342d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5343d71ae5a4SJacob Faibussowitsch {
5344799db056SMatthew G. Knepley   DM           plex;
5345799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5346799db056SMatthew G. Knepley   const char **names;
5347799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5348799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5349799db056SMatthew G. Knepley   size_t       len;
5350799db056SMatthew G. Knepley   MPI_Comm     comm;
5351799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5352783e2ec8SMatthew G. Knepley 
5353783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5354799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5355799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5356799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5357799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5358799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5359799db056SMatthew G. Knepley     PetscDS  dsBC;
5360799db056SMatthew G. Knepley     PetscInt numBd;
5361799db056SMatthew G. Knepley 
536207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5363799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5364799db056SMatthew G. Knepley     maxLabels += numBd;
5365799db056SMatthew G. Knepley   }
5366799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5367799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5368799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5369799db056SMatthew G. Knepley     PetscDS  dsBC;
5370799db056SMatthew G. Knepley     PetscInt numBd, bd;
5371799db056SMatthew G. Knepley 
537207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5373799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5374799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5375799db056SMatthew G. Knepley       DMLabel      label;
5376799db056SMatthew G. Knepley       PetscInt     field;
5377799db056SMatthew G. Knepley       PetscObject  obj;
5378799db056SMatthew G. Knepley       PetscClassId id;
5379799db056SMatthew G. Knepley 
5380799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53819566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53829566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5383799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53849371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53859371c9d4SSatish Balay         if (labels[l] == label) break;
5386799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5387783e2ec8SMatthew G. Knepley     }
5388799db056SMatthew G. Knepley   }
5389799db056SMatthew G. Knepley   /* Get label names */
5390799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5391799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53929371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53939371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53949371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53959371c9d4SSatish Balay   }
5396799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5397712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5398799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5399c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5400799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5401799db056SMatthew G. Knepley   /* Put all names on all processes */
5402799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5403799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5404799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5405799db056SMatthew G. Knepley   gNl = displs[size];
54069371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54079371c9d4SSatish Balay     counts[p] *= gmaxLen;
54089371c9d4SSatish Balay     displs[p] *= gmaxLen;
54099371c9d4SSatish Balay   }
5410799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5411799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5412799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5413799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5414799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5415799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5416799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54179371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54189371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
54199566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5420799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54219566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5422799db056SMatthew G. Knepley     ++gl;
5423783e2ec8SMatthew G. Knepley   }
5424799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5426783e2ec8SMatthew G. Knepley }
5427783e2ec8SMatthew G. Knepley 
5428d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5429d71ae5a4SJacob Faibussowitsch {
5430e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5431e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5432e5e52638SMatthew G. Knepley 
5433e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54343ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54359566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5436e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54379371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54389371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54399371c9d4SSatish Balay     tmpd[s].label  = NULL;
54409371c9d4SSatish Balay     tmpd[s].fields = NULL;
54419371c9d4SSatish Balay   }
54429566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5443e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5444e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5446e5e52638SMatthew G. Knepley }
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley /*@
544920f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5450e5e52638SMatthew G. Knepley 
545120f4b53cSBarry Smith   Not Collective
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Input Parameter:
545420f4b53cSBarry Smith . dm - The `DM`
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Output Parameter:
545720f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   Level: intermediate
5460e5e52638SMatthew G. Knepley 
54611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5462e5e52638SMatthew G. Knepley @*/
5463d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5464d71ae5a4SJacob Faibussowitsch {
5465e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5466e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54674f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5468e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5470e5e52638SMatthew G. Knepley }
5471e5e52638SMatthew G. Knepley 
5472e5e52638SMatthew G. Knepley /*@
547320f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5474e5e52638SMatthew G. Knepley 
547520f4b53cSBarry Smith   Logically Collective
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley   Input Parameter:
547820f4b53cSBarry Smith . dm - The `DM`
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Level: intermediate
5481e5e52638SMatthew G. Knepley 
54821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5483e5e52638SMatthew G. Knepley @*/
5484d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5485d71ae5a4SJacob Faibussowitsch {
5486e5e52638SMatthew G. Knepley   PetscInt s;
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5489e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5490e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54919566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
549207218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54939566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54949566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5495e5e52638SMatthew G. Knepley   }
54969566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5497e5e52638SMatthew G. Knepley   dm->probs = NULL;
5498e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5500e5e52638SMatthew G. Knepley }
5501e5e52638SMatthew G. Knepley 
5502e5e52638SMatthew G. Knepley /*@
550320f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5504e5e52638SMatthew G. Knepley 
550520f4b53cSBarry Smith   Not Collective
5506e5e52638SMatthew G. Knepley 
5507e5e52638SMatthew G. Knepley   Input Parameter:
550820f4b53cSBarry Smith . dm - The `DM`
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley   Output Parameter:
551107218a29SMatthew G. Knepley . ds - The default `PetscDS`
5512e5e52638SMatthew G. Knepley 
5513e5e52638SMatthew G. Knepley   Level: intermediate
5514e5e52638SMatthew G. Knepley 
55151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5516e5e52638SMatthew G. Knepley @*/
551707218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5518d71ae5a4SJacob Faibussowitsch {
5519e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5520e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55214f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
552207218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
552307218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5525e5e52638SMatthew G. Knepley }
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley /*@
552820f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5529e5e52638SMatthew G. Knepley 
553020f4b53cSBarry Smith   Not Collective
5531e5e52638SMatthew G. Knepley 
5532e5e52638SMatthew G. Knepley   Input Parameters:
553320f4b53cSBarry Smith + dm    - The `DM`
553420f4b53cSBarry Smith - point - Cell for the `PetscDS`
5535e5e52638SMatthew G. Knepley 
553607218a29SMatthew G. Knepley   Output Parameters:
553707218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
553807218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5539e5e52638SMatthew G. Knepley 
5540e5e52638SMatthew G. Knepley   Level: developer
5541e5e52638SMatthew G. Knepley 
55421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5543e5e52638SMatthew G. Knepley @*/
554407218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5545d71ae5a4SJacob Faibussowitsch {
554607218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5547e5e52638SMatthew G. Knepley   PetscInt s;
5548e5e52638SMatthew G. Knepley 
5549e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5550e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55514f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55524f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
555363a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
555407218a29SMatthew G. Knepley   if (ds) *ds = NULL;
555507218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5556e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5557e5e52638SMatthew G. Knepley     PetscInt val;
5558e5e52638SMatthew G. Knepley 
55599371c9d4SSatish Balay     if (!dm->probs[s].label) {
556007218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55619371c9d4SSatish Balay     } else {
55629566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55639371c9d4SSatish Balay       if (val >= 0) {
556407218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
556507218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55669371c9d4SSatish Balay         break;
55679371c9d4SSatish Balay       }
5568e5e52638SMatthew G. Knepley     }
5569e5e52638SMatthew G. Knepley   }
557007218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5572e5e52638SMatthew G. Knepley }
5573e5e52638SMatthew G. Knepley 
5574e5e52638SMatthew G. Knepley /*@
557520f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5576e5e52638SMatthew G. Knepley 
557720f4b53cSBarry Smith   Not Collective
5578e5e52638SMatthew G. Knepley 
5579e5e52638SMatthew G. Knepley   Input Parameters:
558020f4b53cSBarry Smith + dm    - The `DM`
558120f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5582e5e52638SMatthew G. Knepley 
5583b3cf3223SMatthew G. Knepley   Output Parameters:
558420f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
558507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
558607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5587e5e52638SMatthew G. Knepley 
5588e5e52638SMatthew G. Knepley   Level: advanced
5589e5e52638SMatthew G. Knepley 
559020f4b53cSBarry Smith   Note:
559120f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
559220f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
559307218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
559420f4b53cSBarry Smith 
55951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5596e5e52638SMatthew G. Knepley @*/
559707218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5598d71ae5a4SJacob Faibussowitsch {
5599e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5600e5e52638SMatthew G. Knepley 
5601e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5602e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5603e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56049371c9d4SSatish Balay   if (fields) {
56054f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56069371c9d4SSatish Balay     *fields = NULL;
56079371c9d4SSatish Balay   }
56089371c9d4SSatish Balay   if (ds) {
56094f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56109371c9d4SSatish Balay     *ds = NULL;
56119371c9d4SSatish Balay   }
561207218a29SMatthew G. Knepley   if (dsIn) {
56134f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
561407218a29SMatthew G. Knepley     *dsIn = NULL;
561507218a29SMatthew G. Knepley   }
5616e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5617154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5618b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5619b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
562007218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56213ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5622b3cf3223SMatthew G. Knepley     }
5623e5e52638SMatthew G. Knepley   }
56243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5625e5e52638SMatthew G. Knepley }
5626e5e52638SMatthew G. Knepley 
5627e5e52638SMatthew G. Knepley /*@
5628bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5629083401c6SMatthew G. Knepley 
563020f4b53cSBarry Smith   Collective
5631083401c6SMatthew G. Knepley 
5632083401c6SMatthew G. Knepley   Input Parameters:
5633bb7acecfSBarry Smith + dm     - The `DM`
563420f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
563507218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
563607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
563707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5638083401c6SMatthew G. Knepley 
563920f4b53cSBarry Smith   Level: advanced
564020f4b53cSBarry Smith 
5641bb7acecfSBarry Smith   Note:
5642bb7acecfSBarry 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,
5643083401c6SMatthew G. Knepley   the fields argument is ignored.
5644083401c6SMatthew G. Knepley 
56451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5646083401c6SMatthew G. Knepley @*/
564707218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5648d71ae5a4SJacob Faibussowitsch {
5649083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5650083401c6SMatthew G. Knepley 
5651083401c6SMatthew G. Knepley   PetscFunctionBegin;
5652083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5653083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
565407218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5655064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
565607218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5657083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5658083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56599566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
566007218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5661083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
566207218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56633ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5664083401c6SMatthew G. Knepley     }
5665083401c6SMatthew G. Knepley   }
56669566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56679566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56689566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
567007218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5671083401c6SMatthew G. Knepley   if (!label) {
5672083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5673083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5674083401c6SMatthew G. Knepley     Nds = 0;
5675083401c6SMatthew G. Knepley   }
5676083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5677083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5678083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
567907218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5681083401c6SMatthew G. Knepley }
5682083401c6SMatthew G. Knepley 
5683083401c6SMatthew G. Knepley /*@
568420f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5685e5e52638SMatthew G. Knepley 
568620f4b53cSBarry Smith   Not Collective
5687e5e52638SMatthew G. Knepley 
5688e5e52638SMatthew G. Knepley   Input Parameters:
568920f4b53cSBarry Smith + dm  - The `DM`
5690e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5691e5e52638SMatthew G. Knepley 
5692e5e52638SMatthew G. Knepley   Output Parameters:
569320f4b53cSBarry Smith + label  - The region label, or `NULL`
569420f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
569507218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
569607218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5697e5e52638SMatthew G. Knepley 
5698e5e52638SMatthew G. Knepley   Level: advanced
5699e5e52638SMatthew G. Knepley 
57001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5701e5e52638SMatthew G. Knepley @*/
570207218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5703d71ae5a4SJacob Faibussowitsch {
5704e5e52638SMatthew G. Knepley   PetscInt Nds;
5705e5e52638SMatthew G. Knepley 
5706e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5707e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57089566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
570963a3b9bcSJacob 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);
5710e5e52638SMatthew G. Knepley   if (label) {
57114f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5712e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5713e5e52638SMatthew G. Knepley   }
5714b3cf3223SMatthew G. Knepley   if (fields) {
57154f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5716b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5717b3cf3223SMatthew G. Knepley   }
5718e5e52638SMatthew G. Knepley   if (ds) {
57194f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5720e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5721e5e52638SMatthew G. Knepley   }
572207218a29SMatthew G. Knepley   if (dsIn) {
57234f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
572407218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
572507218a29SMatthew G. Knepley   }
57263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5727e5e52638SMatthew G. Knepley }
5728e5e52638SMatthew G. Knepley 
5729e5e52638SMatthew G. Knepley /*@
573020f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5731e5e52638SMatthew G. Knepley 
573220f4b53cSBarry Smith   Not Collective
5733e5e52638SMatthew G. Knepley 
5734e5e52638SMatthew G. Knepley   Input Parameters:
573520f4b53cSBarry Smith + dm     - The `DM`
5736083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
573720f4b53cSBarry Smith . label  - The region label, or `NULL`
573807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
573907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
574007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5741e5e52638SMatthew G. Knepley 
5742e5e52638SMatthew G. Knepley   Level: advanced
5743e5e52638SMatthew G. Knepley 
57441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5745e5e52638SMatthew G. Knepley @*/
574607218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5747d71ae5a4SJacob Faibussowitsch {
5748083401c6SMatthew G. Knepley   PetscInt Nds;
5749e5e52638SMatthew G. Knepley 
5750e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5751e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5752ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57539566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
575463a3b9bcSJacob 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);
57559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57569566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5757083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5758083401c6SMatthew G. Knepley   if (fields) {
5759083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57609566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5762083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5763e5e52638SMatthew G. Knepley   }
5764083401c6SMatthew G. Knepley   if (ds) {
5765083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57669566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57679566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5768083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5769083401c6SMatthew G. Knepley   }
577007218a29SMatthew G. Knepley   if (dsIn) {
577107218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
577207218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
577307218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
577407218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
577507218a29SMatthew G. Knepley   }
57763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5777e5e52638SMatthew G. Knepley }
5778e5e52638SMatthew G. Knepley 
5779e5e52638SMatthew G. Knepley /*@
578020f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57811d3af9e0SMatthew G. Knepley 
578220f4b53cSBarry Smith   Not Collective
57831d3af9e0SMatthew G. Knepley 
57841d3af9e0SMatthew G. Knepley   Input Parameters:
578520f4b53cSBarry Smith + dm - The `DM`
578620f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57871d3af9e0SMatthew G. Knepley 
57881d3af9e0SMatthew G. Knepley   Output Parameter:
57891d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57901d3af9e0SMatthew G. Knepley 
57911d3af9e0SMatthew G. Knepley   Level: advanced
57921d3af9e0SMatthew G. Knepley 
57931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57941d3af9e0SMatthew G. Knepley @*/
5795d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5796d71ae5a4SJacob Faibussowitsch {
57971d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57981d3af9e0SMatthew G. Knepley 
57991d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58001d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58011d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58024f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58039566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58049371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58059371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58061d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58071d3af9e0SMatthew G. Knepley   else *num = n;
58083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58091d3af9e0SMatthew G. Knepley }
58101d3af9e0SMatthew G. Knepley 
58112df84da0SMatthew G. Knepley /*@C
5812bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58132df84da0SMatthew G. Knepley 
581420f4b53cSBarry Smith   Not Collective
58152df84da0SMatthew G. Knepley 
5816f1a722f8SMatthew G. Knepley   Input Parameters:
5817bb7acecfSBarry Smith + dm     - The `DM`
58182df84da0SMatthew G. Knepley . Nc     - The number of components for the field
581920f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5820bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58212df84da0SMatthew G. Knepley 
58222df84da0SMatthew G. Knepley   Output Parameter:
5823bb7acecfSBarry Smith . fem - The `PetscFE`
58242df84da0SMatthew G. Knepley 
582520f4b53cSBarry Smith   Level: intermediate
582620f4b53cSBarry Smith 
5827bb7acecfSBarry Smith   Note:
5828bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58292df84da0SMatthew G. Knepley 
58301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58312df84da0SMatthew G. Knepley @*/
5832d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5833d71ae5a4SJacob Faibussowitsch {
58342df84da0SMatthew G. Knepley   DMPolytopeType ct;
58352df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58362df84da0SMatthew G. Knepley 
58372df84da0SMatthew G. Knepley   PetscFunctionBegin;
58382df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58392df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58404f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58412df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58424f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58439566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58469566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58482df84da0SMatthew G. Knepley }
58492df84da0SMatthew G. Knepley 
58501d3af9e0SMatthew G. Knepley /*@
5851bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5852e5e52638SMatthew G. Knepley 
585320f4b53cSBarry Smith   Collective
5854e5e52638SMatthew G. Knepley 
5855e5e52638SMatthew G. Knepley   Input Parameter:
5856bb7acecfSBarry Smith . dm - The `DM`
5857e5e52638SMatthew G. Knepley 
585820f4b53cSBarry Smith   Options Database Key:
5859bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
586045480ffeSMatthew G. Knepley 
586120f4b53cSBarry Smith   Level: intermediate
586220f4b53cSBarry Smith 
58631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5864e5e52638SMatthew G. Knepley @*/
5865d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5866d71ae5a4SJacob Faibussowitsch {
5867e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5868083401c6SMatthew G. Knepley   PetscDS   dsDef;
5869083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5870f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5871f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5872e5e52638SMatthew G. Knepley 
5873e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5874e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58753ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5878083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58799566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5880083401c6SMatthew G. Knepley   Nl   = 0;
5881083401c6SMatthew G. Knepley   Ndef = 0;
5882083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5883083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5884083401c6SMatthew G. Knepley     PetscInt l;
5885083401c6SMatthew G. Knepley 
5886f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5887f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5888f918ec44SMatthew G. Knepley     {
5889f918ec44SMatthew G. Knepley       PetscClassId id;
5890f918ec44SMatthew G. Knepley 
58919566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5892f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5893f918ec44SMatthew G. Knepley         Ceed ceed;
5894f918ec44SMatthew G. Knepley 
58959566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58969566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5897f918ec44SMatthew G. Knepley       }
5898f918ec44SMatthew G. Knepley     }
5899f918ec44SMatthew G. Knepley #endif
59009371c9d4SSatish Balay     if (!label) {
59019371c9d4SSatish Balay       ++Ndef;
59029371c9d4SSatish Balay       continue;
59039371c9d4SSatish Balay     }
59049371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59059371c9d4SSatish Balay       if (label == labelSet[l]) break;
5906083401c6SMatthew G. Knepley     if (l < Nl) continue;
5907083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5908083401c6SMatthew G. Knepley   }
5909083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
591007218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5911083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5912b3cf3223SMatthew G. Knepley     IS        fields;
5913b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5914b3cf3223SMatthew G. Knepley 
59159371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59169371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59177a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59199371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59209371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59219566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59239566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59249566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
592588f0c812SMatthew G. Knepley 
59269566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
592707218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59289566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59302df9ee95SMatthew G. Knepley   }
593107218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59329566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5933083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5934083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59350122748bSMatthew G. Knepley     DM              plex;
5936083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5937083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5938083401c6SMatthew G. Knepley     PetscInt       *fields;
5939083401c6SMatthew G. Knepley     const PetscInt *cells;
5940083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59410122748bSMatthew G. Knepley 
59429566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59439566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59449566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59459566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59465fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5947083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5948083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5949083401c6SMatthew G. Knepley       IS      pointIS;
5950083401c6SMatthew G. Knepley 
59519566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59529566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59539566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59549566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5955083401c6SMatthew G. Knepley     }
59569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5957083401c6SMatthew G. Knepley 
59589566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59599566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59609566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59619566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59629566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59649566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5965083401c6SMatthew G. Knepley 
59669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59679371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59689371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59699566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59719566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59729566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5973083401c6SMatthew G. Knepley 
59749566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
597507218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59769566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59779566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59789566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59799566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59809566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5981083401c6SMatthew G. Knepley   }
5982083401c6SMatthew G. Knepley   /* Create label DSes
5983083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5984083401c6SMatthew G. Knepley   */
5985083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5986083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5987083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
598807218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5989083401c6SMatthew G. Knepley     IS        fields;
5990083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5991083401c6SMatthew G. Knepley 
59929566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59939371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59949371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59959566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59969371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59979371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59989566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59999566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60009566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60019566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60029566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6003083401c6SMatthew G. Knepley     {
6004083401c6SMatthew G. Knepley       DMPolytopeType ct;
6005083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60065fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60070122748bSMatthew G. Knepley 
60089566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6009665f567fSMatthew G. Knepley       if (lStart >= 0) {
60109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6011412e9a14SMatthew G. Knepley         switch (ct) {
6012412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6013412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6014412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6015d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6016d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6017d71ae5a4SJacob Faibussowitsch           break;
6018d71ae5a4SJacob Faibussowitsch         default:
6019d71ae5a4SJacob Faibussowitsch           break;
6020412e9a14SMatthew G. Knepley         }
6021665f567fSMatthew G. Knepley       }
6022712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
602307218a29SMatthew G. Knepley       if (isCohesive) {
602407218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
602507218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
602607218a29SMatthew G. Knepley       }
60275fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60285fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60295fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60309566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60319566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
603207218a29SMatthew G. Knepley             if (dsIn) {
603307218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
603407218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
603507218a29SMatthew G. Knepley             }
60365fedec97SMatthew G. Knepley           }
60375fedec97SMatthew G. Knepley           ++nf;
60385fedec97SMatthew G. Knepley         }
60395fedec97SMatthew G. Knepley       }
6040e5e52638SMatthew G. Knepley     }
604107218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
604207218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60439566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
604407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6045e5e52638SMatthew G. Knepley   }
60469566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6047e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6048083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6049083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
605007218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6051083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6052083401c6SMatthew G. Knepley     const PetscInt *fld;
60535fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60545fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6055e5e52638SMatthew G. Knepley 
60569566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60579566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60589566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60599566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6060083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6061083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60625fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6063e5e52638SMatthew G. Knepley       PetscClassId id;
6064e5e52638SMatthew G. Knepley 
60655fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60669566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60675fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
606807218a29SMatthew G. Knepley       if (isCohesive) {
606907218a29SMatthew G. Knepley         if (!isCohesiveField) {
607007218a29SMatthew G. Knepley           PetscObject bdDisc;
607107218a29SMatthew G. Knepley 
607207218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
607307218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
607407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
607507218a29SMatthew G. Knepley         } else {
60769566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
607707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
607807218a29SMatthew G. Knepley         }
607907218a29SMatthew G. Knepley       } else {
608007218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
608107218a29SMatthew G. Knepley       }
6082083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60839566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6084e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6085e5e52638SMatthew G. Knepley     }
60869566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6087e5e52638SMatthew G. Knepley   }
6088f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6090f9244615SMatthew G. Knepley   if (flg) {
60913b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60923b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
609307218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60943b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60953b4aee56SMatthew G. Knepley 
60969566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
609707218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
609807218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
609907218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
610007218a29SMatthew G. Knepley       }
61013b4aee56SMatthew G. Knepley     }
6102f9244615SMatthew G. Knepley   }
6103e5e52638SMatthew G. Knepley   /* Setup DSes */
6104e5e52638SMatthew G. Knepley   if (doSetup) {
610507218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6106cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6107cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6108cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6109cf1363e4SMatthew G. Knepley       }
611007218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
611107218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
611207218a29SMatthew G. Knepley     }
6113e5e52638SMatthew G. Knepley   }
61143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6115e5e52638SMatthew G. Knepley }
6116e5e52638SMatthew G. Knepley 
6117e5e52638SMatthew G. Knepley /*@
6118d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6119d2b2dc1eSMatthew G. Knepley 
6120d2b2dc1eSMatthew G. Knepley   Input Parameters:
6121d2b2dc1eSMatthew G. Knepley + dm     - The DM
6122d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6123d2b2dc1eSMatthew G. Knepley 
6124d2b2dc1eSMatthew G. Knepley   Level: developer
6125d2b2dc1eSMatthew G. Knepley 
6126d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6127d2b2dc1eSMatthew G. Knepley @*/
6128d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6129d2b2dc1eSMatthew G. Knepley {
6130d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6131d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6132d2b2dc1eSMatthew G. Knepley 
6133d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6134d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6135d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6136d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6137d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6138d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6139d2b2dc1eSMatthew G. Knepley 
6140d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6141d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6142d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6143d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6144d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6145d2b2dc1eSMatthew G. Knepley 
6146d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6147d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6148d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6149d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6150d2b2dc1eSMatthew G. Knepley   }
6151d2b2dc1eSMatthew G. Knepley   if (tensor) {
6152d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6153d2b2dc1eSMatthew G. Knepley   } else {
6154d2b2dc1eSMatthew G. Knepley     PetscSection s;
6155d2b2dc1eSMatthew G. Knepley 
6156d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6157d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6158d2b2dc1eSMatthew G. Knepley   }
6159d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6160d2b2dc1eSMatthew G. Knepley }
6161d2b2dc1eSMatthew G. Knepley 
6162d2b2dc1eSMatthew G. Knepley /*@
6163bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61647f96f943SMatthew G. Knepley 
616520f4b53cSBarry Smith   Collective
6166f2cacb80SMatthew G. Knepley 
61677f96f943SMatthew G. Knepley   Input Parameters:
6168bb7acecfSBarry Smith + dm   - The `DM`
61697f96f943SMatthew G. Knepley - time - The time
61707f96f943SMatthew G. Knepley 
61717f96f943SMatthew G. Knepley   Output Parameters:
617220f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
617320f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
617420f4b53cSBarry Smith 
617520f4b53cSBarry Smith   Level: developer
61767f96f943SMatthew G. Knepley 
6177bb7acecfSBarry Smith   Note:
6178bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61797f96f943SMatthew G. Knepley 
61801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61817f96f943SMatthew G. Knepley @*/
6182d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6183d71ae5a4SJacob Faibussowitsch {
61847f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61857f96f943SMatthew G. Knepley   void   **ectxs;
6186f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61877f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61887f96f943SMatthew G. Knepley 
61897f96f943SMatthew G. Knepley   PetscFunctionBegin;
6190f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6191f60fa741SMatthew G. Knepley   if (u) {
6192f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6193f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6194f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6195f60fa741SMatthew G. Knepley   }
6196f60fa741SMatthew G. Knepley   if (u_t) {
6197f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6198f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6199f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6200f60fa741SMatthew G. Knepley   }
62019566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62039566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62047f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62057f96f943SMatthew G. Knepley     PetscDS         ds;
62067f96f943SMatthew G. Knepley     DMLabel         label;
62077f96f943SMatthew G. Knepley     IS              fieldIS;
62087f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62097f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62107f96f943SMatthew G. Knepley 
621107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62129566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62149566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62159566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6216f2cacb80SMatthew G. Knepley     if (u) {
6217f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6218f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6219f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62207f96f943SMatthew G. Knepley     }
6221f2cacb80SMatthew G. Knepley     if (u_t) {
62229566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62239566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6224f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6225f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6226f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6227f2cacb80SMatthew G. Knepley     }
62289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6229f2cacb80SMatthew G. Knepley   }
6230f2cacb80SMatthew G. Knepley   if (u) {
62319566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62329566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6233f2cacb80SMatthew G. Knepley   }
6234f2cacb80SMatthew G. Knepley   if (u_t) {
62359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62369566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6237f2cacb80SMatthew G. Knepley   }
62389566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6239f60fa741SMatthew G. Knepley   if (u) {
6240f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6241f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6242f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6243f60fa741SMatthew G. Knepley   }
6244f60fa741SMatthew G. Knepley   if (u_t) {
6245f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6246f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6247f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6248f60fa741SMatthew G. Knepley   }
62493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62507f96f943SMatthew G. Knepley }
62517f96f943SMatthew G. Knepley 
625207218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6253d71ae5a4SJacob Faibussowitsch {
625407218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
625545480ffeSMatthew G. Knepley 
625645480ffeSMatthew G. Knepley   PetscFunctionBegin;
62579566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
625807218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
625907218a29SMatthew G. Knepley   if (dsIn) {
626007218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
626107218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
626245480ffeSMatthew G. Knepley   }
626307218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62649566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
626507218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626745480ffeSMatthew G. Knepley }
626845480ffeSMatthew G. Knepley 
62697f96f943SMatthew G. Knepley /*@
6270bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6271e5e52638SMatthew G. Knepley 
627220f4b53cSBarry Smith   Collective
6273e5e52638SMatthew G. Knepley 
6274e5e52638SMatthew G. Knepley   Input Parameter:
6275bb7acecfSBarry Smith . dm - The `DM`
6276e5e52638SMatthew G. Knepley 
6277e5e52638SMatthew G. Knepley   Output Parameter:
6278bb7acecfSBarry Smith . newdm - The `DM`
6279e5e52638SMatthew G. Knepley 
6280e5e52638SMatthew G. Knepley   Level: advanced
6281e5e52638SMatthew G. Knepley 
62821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6283e5e52638SMatthew G. Knepley @*/
6284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6285d71ae5a4SJacob Faibussowitsch {
6286e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6287e5e52638SMatthew G. Knepley 
6288e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62893ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62909566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62919566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6292e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6293e5e52638SMatthew G. Knepley     DMLabel  label;
6294b3cf3223SMatthew G. Knepley     IS       fields;
629507218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6296783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6297e5e52638SMatthew G. Knepley 
629807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6299b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
630007218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6301d5b43468SJose E. Roman     /* Complete new labels in the new DS */
630207218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63039566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6304783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6305b8025e53SMatthew G. Knepley       PetscWeakForm wf;
630645480ffeSMatthew G. Knepley       DMLabel       label;
6307783e2ec8SMatthew G. Knepley       PetscInt      field;
6308783e2ec8SMatthew G. Knepley 
63099566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63109566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6311783e2ec8SMatthew G. Knepley     }
6312e5e52638SMatthew G. Knepley   }
6313799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6315e5e52638SMatthew G. Knepley }
6316e5e52638SMatthew G. Knepley 
6317e5e52638SMatthew G. Knepley /*@
6318bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6319e5e52638SMatthew G. Knepley 
632020f4b53cSBarry Smith   Collective
6321e5e52638SMatthew G. Knepley 
6322e5e52638SMatthew G. Knepley   Input Parameter:
6323bb7acecfSBarry Smith . dm - The `DM`
6324e5e52638SMatthew G. Knepley 
6325e5e52638SMatthew G. Knepley   Output Parameter:
6326bb7acecfSBarry Smith . newdm - The `DM`
6327e5e52638SMatthew G. Knepley 
6328e5e52638SMatthew G. Knepley   Level: advanced
6329e5e52638SMatthew G. Knepley 
633073ff1848SBarry Smith   Developer Note:
6331bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6332bb7acecfSBarry Smith 
63331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6334e5e52638SMatthew G. Knepley @*/
6335d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6336d71ae5a4SJacob Faibussowitsch {
6337e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63389566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63399566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6341e5e52638SMatthew G. Knepley }
6342e5e52638SMatthew G. Knepley 
6343c73cfb54SMatthew G. Knepley /*@
6344bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6345c73cfb54SMatthew G. Knepley 
634620f4b53cSBarry Smith   Not Collective
6347c73cfb54SMatthew G. Knepley 
6348c73cfb54SMatthew G. Knepley   Input Parameter:
6349bb7acecfSBarry Smith . dm - The `DM`
6350c73cfb54SMatthew G. Knepley 
6351c73cfb54SMatthew G. Knepley   Output Parameter:
6352c73cfb54SMatthew G. Knepley . dim - The topological dimension
6353c73cfb54SMatthew G. Knepley 
6354c73cfb54SMatthew G. Knepley   Level: beginner
6355c73cfb54SMatthew G. Knepley 
63561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6357c73cfb54SMatthew G. Knepley @*/
6358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6359d71ae5a4SJacob Faibussowitsch {
6360c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6361c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63624f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6363c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6365c73cfb54SMatthew G. Knepley }
6366c73cfb54SMatthew G. Knepley 
6367c73cfb54SMatthew G. Knepley /*@
6368bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6369c73cfb54SMatthew G. Knepley 
637020f4b53cSBarry Smith   Collective
6371c73cfb54SMatthew G. Knepley 
6372c73cfb54SMatthew G. Knepley   Input Parameters:
6373bb7acecfSBarry Smith + dm  - The `DM`
6374c73cfb54SMatthew G. Knepley - dim - The topological dimension
6375c73cfb54SMatthew G. Knepley 
6376c73cfb54SMatthew G. Knepley   Level: beginner
6377c73cfb54SMatthew G. Knepley 
63781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6379c73cfb54SMatthew G. Knepley @*/
6380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6381d71ae5a4SJacob Faibussowitsch {
6382e5e52638SMatthew G. Knepley   PetscDS  ds;
638345480ffeSMatthew G. Knepley   PetscInt Nds, n;
6384f17e8794SMatthew G. Knepley 
6385c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6386c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6387c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6388c73cfb54SMatthew G. Knepley   dm->dim = dim;
6389d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63909566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
639145480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
639207218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63939566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
639445480ffeSMatthew G. Knepley     }
6395d17bd122SMatthew G. Knepley   }
63963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6397c73cfb54SMatthew G. Knepley }
6398c73cfb54SMatthew G. Knepley 
6399793f3fe5SMatthew G. Knepley /*@
6400793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6401793f3fe5SMatthew G. Knepley 
640220f4b53cSBarry Smith   Collective
6403793f3fe5SMatthew G. Knepley 
6404793f3fe5SMatthew G. Knepley   Input Parameters:
6405bb7acecfSBarry Smith + dm  - the `DM`
6406793f3fe5SMatthew G. Knepley - dim - the dimension
6407793f3fe5SMatthew G. Knepley 
6408793f3fe5SMatthew G. Knepley   Output Parameters:
6409793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6410aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6411793f3fe5SMatthew G. Knepley 
641220f4b53cSBarry Smith   Level: intermediate
641320f4b53cSBarry Smith 
6414793f3fe5SMatthew G. Knepley   Note:
6415793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6416a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6417793f3fe5SMatthew G. Knepley   then the interval is empty.
6418793f3fe5SMatthew G. Knepley 
64191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6420793f3fe5SMatthew G. Knepley @*/
6421d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6422d71ae5a4SJacob Faibussowitsch {
6423793f3fe5SMatthew G. Knepley   PetscInt d;
6424793f3fe5SMatthew G. Knepley 
6425793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6426793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64279566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64287a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6429dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6431793f3fe5SMatthew G. Knepley }
6432793f3fe5SMatthew G. Knepley 
64336636e97aSMatthew G Knepley /*@
6434bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6435f4d763aaSMatthew G. Knepley 
643620f4b53cSBarry Smith   Collective
64378f700142SStefano Zampini 
6438f4d763aaSMatthew G. Knepley   Input Parameter:
6439bb7acecfSBarry Smith . dm - The original `DM`
6440f4d763aaSMatthew G. Knepley 
6441f4d763aaSMatthew G. Knepley   Output Parameter:
6442bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6443f4d763aaSMatthew G. Knepley 
6444f4d763aaSMatthew G. Knepley   Level: intermediate
6445f4d763aaSMatthew G. Knepley 
6446bb7acecfSBarry Smith   Note:
6447bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6448bb7acecfSBarry 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
6449bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6450bb7acecfSBarry Smith 
64511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6452f4d763aaSMatthew G. Knepley @*/
6453d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6454d71ae5a4SJacob Faibussowitsch {
6455c26acbdeSMatthew G. Knepley   PetscSection section;
6456eb9d3e4dSMatthew G. Knepley   IS           perm;
6457eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
645814f150ffSMatthew G. Knepley 
645914f150ffSMatthew G. Knepley   PetscFunctionBegin;
646014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64614f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64629566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64639566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6464eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6465eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6466eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6467eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6468c26acbdeSMatthew G. Knepley     *odm = dm;
64693ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6470c26acbdeSMatthew G. Knepley   }
647114f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6472c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
647314f150ffSMatthew G. Knepley     PetscSF      sf;
6474eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
647514f150ffSMatthew G. Knepley 
64769566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64779566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64789566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64799566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64809566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64819566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6482eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64839566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64849566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
648514f150ffSMatthew G. Knepley   }
648614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
648814f150ffSMatthew G. Knepley }
6489f4d763aaSMatthew G. Knepley 
6490f4d763aaSMatthew G. Knepley /*@
6491cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6492f4d763aaSMatthew G. Knepley 
6493f4d763aaSMatthew G. Knepley   Input Parameter:
6494bb7acecfSBarry Smith . dm - The original `DM`
6495f4d763aaSMatthew G. Knepley 
6496cdb7a50dSMatthew G. Knepley   Output Parameters:
6497cdb7a50dSMatthew G. Knepley + num - The output sequence number
6498cdb7a50dSMatthew G. Knepley - val - The output sequence value
6499f4d763aaSMatthew G. Knepley 
6500f4d763aaSMatthew G. Knepley   Level: intermediate
6501f4d763aaSMatthew G. Knepley 
6502bb7acecfSBarry Smith   Note:
6503bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6504bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6505bb7acecfSBarry Smith 
650673ff1848SBarry Smith   Developer Note:
6507bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6508bb7acecfSBarry Smith   not directly related to the `DM`.
6509f4d763aaSMatthew G. Knepley 
65101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6511f4d763aaSMatthew G. Knepley @*/
6512d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6513d71ae5a4SJacob Faibussowitsch {
6514f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6515f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65169371c9d4SSatish Balay   if (num) {
65174f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65189371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65199371c9d4SSatish Balay   }
65209371c9d4SSatish Balay   if (val) {
65214f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65229371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65239371c9d4SSatish Balay   }
65243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6525f4d763aaSMatthew G. Knepley }
6526f4d763aaSMatthew G. Knepley 
6527f4d763aaSMatthew G. Knepley /*@
6528cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6529f4d763aaSMatthew G. Knepley 
6530f4d763aaSMatthew G. Knepley   Input Parameters:
6531bb7acecfSBarry Smith + dm  - The original `DM`
6532cdb7a50dSMatthew G. Knepley . num - The output sequence number
6533cdb7a50dSMatthew G. Knepley - val - The output sequence value
6534f4d763aaSMatthew G. Knepley 
6535f4d763aaSMatthew G. Knepley   Level: intermediate
6536f4d763aaSMatthew G. Knepley 
6537bb7acecfSBarry Smith   Note:
6538bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6539bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6540f4d763aaSMatthew G. Knepley 
65411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6542f4d763aaSMatthew G. Knepley @*/
6543d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6544d71ae5a4SJacob Faibussowitsch {
6545f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6546f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6547f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6548cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6550cdb7a50dSMatthew G. Knepley }
6551cdb7a50dSMatthew G. Knepley 
6552cdb7a50dSMatthew G. Knepley /*@C
6553bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6554cdb7a50dSMatthew G. Knepley 
6555cdb7a50dSMatthew G. Knepley   Input Parameters:
6556bb7acecfSBarry Smith + dm     - The original `DM`
6557a4e35b19SJacob Faibussowitsch . viewer - The viewer to get it from
6558cdb7a50dSMatthew G. Knepley . name   - The sequence name
6559cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6560cdb7a50dSMatthew G. Knepley 
6561cdb7a50dSMatthew G. Knepley   Output Parameter:
6562cdb7a50dSMatthew G. Knepley . val - The output sequence value
6563cdb7a50dSMatthew G. Knepley 
6564cdb7a50dSMatthew G. Knepley   Level: intermediate
6565cdb7a50dSMatthew G. Knepley 
6566bb7acecfSBarry Smith   Note:
6567bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6568bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6569bb7acecfSBarry Smith 
657073ff1848SBarry Smith   Developer Note:
6571bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6572cdb7a50dSMatthew G. Knepley 
65731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6574cdb7a50dSMatthew G. Knepley @*/
6575d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6576d71ae5a4SJacob Faibussowitsch {
6577cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6578cdb7a50dSMatthew G. Knepley 
6579cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6580cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6581cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
65824f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65839566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6584cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6585cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6586cdb7a50dSMatthew G. Knepley     PetscScalar value;
6587cdb7a50dSMatthew G. Knepley 
65889566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65894aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6590cdb7a50dSMatthew G. Knepley #endif
6591cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6593f4d763aaSMatthew G. Knepley }
65948e4ac7eaSMatthew G. Knepley 
65958e4ac7eaSMatthew G. Knepley /*@
6596bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
65978e4ac7eaSMatthew G. Knepley 
659820f4b53cSBarry Smith   Not Collective
65998e4ac7eaSMatthew G. Knepley 
66008e4ac7eaSMatthew G. Knepley   Input Parameter:
6601bb7acecfSBarry Smith . dm - The `DM`
66028e4ac7eaSMatthew G. Knepley 
66038e4ac7eaSMatthew G. Knepley   Output Parameter:
6604bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66058e4ac7eaSMatthew G. Knepley 
66068e4ac7eaSMatthew G. Knepley   Level: beginner
66078e4ac7eaSMatthew G. Knepley 
66081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66098e4ac7eaSMatthew G. Knepley @*/
6610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6611d71ae5a4SJacob Faibussowitsch {
66128e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66138e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66144f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66158e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66178e4ac7eaSMatthew G. Knepley }
66188e4ac7eaSMatthew G. Knepley 
66198e4ac7eaSMatthew G. Knepley /*@
6620bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66218e4ac7eaSMatthew G. Knepley 
662220f4b53cSBarry Smith   Collective
66238e4ac7eaSMatthew G. Knepley 
66248e4ac7eaSMatthew G. Knepley   Input Parameters:
6625bb7acecfSBarry Smith + dm         - The `DM`
6626bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66278e4ac7eaSMatthew G. Knepley 
662873ff1848SBarry Smith   Level: beginner
662973ff1848SBarry Smith 
6630bb7acecfSBarry Smith   Note:
6631bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66325d3b26e6SMatthew G. Knepley 
66331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66348e4ac7eaSMatthew G. Knepley @*/
6635d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6636d71ae5a4SJacob Faibussowitsch {
66378e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66388e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66398833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66408e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66428e4ac7eaSMatthew G. Knepley }
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac /*@C
6645bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6646c58f1c22SToby Isaac 
6647c58f1c22SToby Isaac   Not Collective
6648c58f1c22SToby Isaac 
6649c58f1c22SToby Isaac   Input Parameters:
6650bb7acecfSBarry Smith + dm   - The `DM` object
6651c58f1c22SToby Isaac - name - The label name
6652c58f1c22SToby Isaac 
6653c58f1c22SToby Isaac   Level: intermediate
6654c58f1c22SToby Isaac 
66551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6656c58f1c22SToby Isaac @*/
6657d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6658d71ae5a4SJacob Faibussowitsch {
66595d80c0bfSVaclav Hapla   PetscBool flg;
66605d80c0bfSVaclav Hapla   DMLabel   label;
6661c58f1c22SToby Isaac 
6662c58f1c22SToby Isaac   PetscFunctionBegin;
6663c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66644f572ea9SToby Isaac   PetscAssertPointer(name, 2);
66659566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6666c58f1c22SToby Isaac   if (!flg) {
66679566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
66689566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
66699566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6670c58f1c22SToby Isaac   }
66713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6672c58f1c22SToby Isaac }
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac /*@C
6675bb7acecfSBarry 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.
66760fdc7489SMatthew Knepley 
66770fdc7489SMatthew Knepley   Not Collective
66780fdc7489SMatthew Knepley 
66790fdc7489SMatthew Knepley   Input Parameters:
6680bb7acecfSBarry Smith + dm   - The `DM` object
66810fdc7489SMatthew Knepley . l    - The index for the label
66820fdc7489SMatthew Knepley - name - The label name
66830fdc7489SMatthew Knepley 
66840fdc7489SMatthew Knepley   Level: intermediate
66850fdc7489SMatthew Knepley 
66861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
66870fdc7489SMatthew Knepley @*/
6688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6689d71ae5a4SJacob Faibussowitsch {
66900fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
66910fdc7489SMatthew Knepley   DMLabel     label;
66920fdc7489SMatthew Knepley   PetscInt    Nl, m;
66930fdc7489SMatthew Knepley   PetscBool   flg, match;
66940fdc7489SMatthew Knepley   const char *lname;
66950fdc7489SMatthew Knepley 
66960fdc7489SMatthew Knepley   PetscFunctionBegin;
66970fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66984f572ea9SToby Isaac   PetscAssertPointer(name, 3);
66999566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67000fdc7489SMatthew Knepley   if (!flg) {
67019566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67029566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67039566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67040fdc7489SMatthew Knepley   }
67059566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
670663a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67070fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67099566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67100fdc7489SMatthew Knepley     if (match) break;
67110fdc7489SMatthew Knepley   }
67123ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67130fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67140fdc7489SMatthew Knepley   else prev->next = orig->next;
67150fdc7489SMatthew Knepley   if (!l) {
67160fdc7489SMatthew Knepley     orig->next = dm->labels;
67170fdc7489SMatthew Knepley     dm->labels = orig;
67180fdc7489SMatthew Knepley   } else {
6719fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67200fdc7489SMatthew Knepley     orig->next = prev->next;
67210fdc7489SMatthew Knepley     prev->next = orig;
67220fdc7489SMatthew Knepley   }
67233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67240fdc7489SMatthew Knepley }
67250fdc7489SMatthew Knepley 
67260fdc7489SMatthew Knepley /*@C
6727bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   Not Collective
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac   Input Parameters:
6732bb7acecfSBarry Smith + dm    - The `DM` object
6733c58f1c22SToby Isaac . name  - The label name
6734c58f1c22SToby Isaac - point - The mesh point
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Output Parameter:
6737c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac   Level: beginner
6740c58f1c22SToby Isaac 
67411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6742c58f1c22SToby Isaac @*/
6743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6744d71ae5a4SJacob Faibussowitsch {
6745c58f1c22SToby Isaac   DMLabel label;
6746c58f1c22SToby Isaac 
6747c58f1c22SToby Isaac   PetscFunctionBegin;
6748c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67494f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67509566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67517a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6754c58f1c22SToby Isaac }
6755c58f1c22SToby Isaac 
6756c58f1c22SToby Isaac /*@C
6757bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6758c58f1c22SToby Isaac 
6759c58f1c22SToby Isaac   Not Collective
6760c58f1c22SToby Isaac 
6761c58f1c22SToby Isaac   Input Parameters:
6762bb7acecfSBarry Smith + dm    - The `DM` object
6763c58f1c22SToby Isaac . name  - The label name
6764c58f1c22SToby Isaac . point - The mesh point
6765c58f1c22SToby Isaac - value - The label value for this point
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   Output Parameter:
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Level: beginner
6770c58f1c22SToby Isaac 
67711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6772c58f1c22SToby Isaac @*/
6773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6774d71ae5a4SJacob Faibussowitsch {
6775c58f1c22SToby Isaac   DMLabel label;
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac   PetscFunctionBegin;
6778c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67794f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67809566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6781c58f1c22SToby Isaac   if (!label) {
67829566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
67839566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6784c58f1c22SToby Isaac   }
67859566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
67863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6787c58f1c22SToby Isaac }
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac /*@C
6790bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   Not Collective
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac   Input Parameters:
6795bb7acecfSBarry Smith + dm    - The `DM` object
6796c58f1c22SToby Isaac . name  - The label name
6797c58f1c22SToby Isaac . point - The mesh point
6798c58f1c22SToby Isaac - value - The label value for this point
6799c58f1c22SToby Isaac 
6800c58f1c22SToby Isaac   Level: beginner
6801c58f1c22SToby Isaac 
68021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6803c58f1c22SToby Isaac @*/
6804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6805d71ae5a4SJacob Faibussowitsch {
6806c58f1c22SToby Isaac   DMLabel label;
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac   PetscFunctionBegin;
6809c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68104f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68123ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68139566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6815c58f1c22SToby Isaac }
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac /*@C
6818bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6819c58f1c22SToby Isaac 
6820c58f1c22SToby Isaac   Not Collective
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac   Input Parameters:
6823bb7acecfSBarry Smith + dm   - The `DM` object
6824c58f1c22SToby Isaac - name - The label name
6825c58f1c22SToby Isaac 
6826c58f1c22SToby Isaac   Output Parameter:
6827c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac   Level: beginner
6830c58f1c22SToby Isaac 
683173ff1848SBarry Smith   Developer Note:
6832bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6833bb7acecfSBarry Smith 
68341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6835c58f1c22SToby Isaac @*/
6836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6837d71ae5a4SJacob Faibussowitsch {
6838c58f1c22SToby Isaac   DMLabel label;
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac   PetscFunctionBegin;
6841c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68424f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68434f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68449566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6845c58f1c22SToby Isaac   *size = 0;
68463ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68479566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6849c58f1c22SToby Isaac }
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac /*@C
6852bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6853c58f1c22SToby Isaac 
6854c58f1c22SToby Isaac   Not Collective
6855c58f1c22SToby Isaac 
6856c58f1c22SToby Isaac   Input Parameters:
685760225df5SJacob Faibussowitsch + dm   - The `DM` object
6858c58f1c22SToby Isaac - name - The label name
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   Output Parameter:
686120f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac   Level: beginner
6864c58f1c22SToby Isaac 
68651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6866c58f1c22SToby Isaac @*/
6867d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6868d71ae5a4SJacob Faibussowitsch {
6869c58f1c22SToby Isaac   DMLabel label;
6870c58f1c22SToby Isaac 
6871c58f1c22SToby Isaac   PetscFunctionBegin;
6872c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68734f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68744f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
68759566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6876c58f1c22SToby Isaac   *ids = NULL;
6877dab2e251SBlaise Bourdin   if (label) {
68789566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6879dab2e251SBlaise Bourdin   } else {
6880dab2e251SBlaise Bourdin     /* returning an empty IS */
68819566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6882dab2e251SBlaise Bourdin   }
68833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6884c58f1c22SToby Isaac }
6885c58f1c22SToby Isaac 
6886c58f1c22SToby Isaac /*@C
6887c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6888c58f1c22SToby Isaac 
6889c58f1c22SToby Isaac   Not Collective
6890c58f1c22SToby Isaac 
6891c58f1c22SToby Isaac   Input Parameters:
6892bb7acecfSBarry Smith + dm    - The `DM` object
6893b6971eaeSBarry Smith . name  - The label name of the stratum
6894c58f1c22SToby Isaac - value - The stratum value
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Output Parameter:
6897bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac   Level: beginner
6900c58f1c22SToby Isaac 
69011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6902c58f1c22SToby Isaac @*/
6903d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6904d71ae5a4SJacob Faibussowitsch {
6905c58f1c22SToby Isaac   DMLabel label;
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac   PetscFunctionBegin;
6908c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69094f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69104f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6912c58f1c22SToby Isaac   *size = 0;
69133ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69149566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6916c58f1c22SToby Isaac }
6917c58f1c22SToby Isaac 
6918c58f1c22SToby Isaac /*@C
6919c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6920c58f1c22SToby Isaac 
6921c58f1c22SToby Isaac   Not Collective
6922c58f1c22SToby Isaac 
6923c58f1c22SToby Isaac   Input Parameters:
6924bb7acecfSBarry Smith + dm    - The `DM` object
6925c58f1c22SToby Isaac . name  - The label name
6926c58f1c22SToby Isaac - value - The stratum value
6927c58f1c22SToby Isaac 
6928c58f1c22SToby Isaac   Output Parameter:
692920f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   Level: beginner
6932c58f1c22SToby Isaac 
69331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6934c58f1c22SToby Isaac @*/
6935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6936d71ae5a4SJacob Faibussowitsch {
6937c58f1c22SToby Isaac   DMLabel label;
6938c58f1c22SToby Isaac 
6939c58f1c22SToby Isaac   PetscFunctionBegin;
6940c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69414f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69424f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69439566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6944c58f1c22SToby Isaac   *points = NULL;
69453ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69469566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6948c58f1c22SToby Isaac }
6949c58f1c22SToby Isaac 
69504de306b1SToby Isaac /*@C
69519044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69524de306b1SToby Isaac 
69534de306b1SToby Isaac   Not Collective
69544de306b1SToby Isaac 
69554de306b1SToby Isaac   Input Parameters:
6956bb7acecfSBarry Smith + dm     - The `DM` object
69574de306b1SToby Isaac . name   - The label name
69584de306b1SToby Isaac . value  - The stratum value
69594de306b1SToby Isaac - points - The stratum points
69604de306b1SToby Isaac 
69614de306b1SToby Isaac   Level: beginner
69624de306b1SToby Isaac 
69631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
69644de306b1SToby Isaac @*/
6965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6966d71ae5a4SJacob Faibussowitsch {
69674de306b1SToby Isaac   DMLabel label;
69684de306b1SToby Isaac 
69694de306b1SToby Isaac   PetscFunctionBegin;
69704de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69714f572ea9SToby Isaac   PetscAssertPointer(name, 2);
6972292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
69739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
69743ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69759566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
69763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69774de306b1SToby Isaac }
69784de306b1SToby Isaac 
6979c58f1c22SToby Isaac /*@C
6980bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6981c58f1c22SToby Isaac 
6982c58f1c22SToby Isaac   Not Collective
6983c58f1c22SToby Isaac 
6984c58f1c22SToby Isaac   Input Parameters:
6985bb7acecfSBarry Smith + dm    - The `DM` object
6986c58f1c22SToby Isaac . name  - The label name
6987c58f1c22SToby Isaac - value - The label value for this point
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Output Parameter:
6990c58f1c22SToby Isaac 
6991c58f1c22SToby Isaac   Level: beginner
6992c58f1c22SToby Isaac 
69931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6994c58f1c22SToby Isaac @*/
6995d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6996d71ae5a4SJacob Faibussowitsch {
6997c58f1c22SToby Isaac   DMLabel label;
6998c58f1c22SToby Isaac 
6999c58f1c22SToby Isaac   PetscFunctionBegin;
7000c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70014f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70029566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70033ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70049566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7006c58f1c22SToby Isaac }
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac /*@
7009bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7010c58f1c22SToby Isaac 
7011c58f1c22SToby Isaac   Not Collective
7012c58f1c22SToby Isaac 
7013c58f1c22SToby Isaac   Input Parameter:
7014bb7acecfSBarry Smith . dm - The `DM` object
7015c58f1c22SToby Isaac 
7016c58f1c22SToby Isaac   Output Parameter:
7017c58f1c22SToby Isaac . numLabels - the number of Labels
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   Level: intermediate
7020c58f1c22SToby Isaac 
70211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7022c58f1c22SToby Isaac @*/
7023d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7024d71ae5a4SJacob Faibussowitsch {
70255d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7026c58f1c22SToby Isaac   PetscInt    n    = 0;
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   PetscFunctionBegin;
7029c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70304f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70319371c9d4SSatish Balay   while (next) {
70329371c9d4SSatish Balay     ++n;
70339371c9d4SSatish Balay     next = next->next;
70349371c9d4SSatish Balay   }
7035c58f1c22SToby Isaac   *numLabels = n;
70363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7037c58f1c22SToby Isaac }
7038c58f1c22SToby Isaac 
7039c58f1c22SToby Isaac /*@C
7040c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   Not Collective
7043c58f1c22SToby Isaac 
7044c58f1c22SToby Isaac   Input Parameters:
7045bb7acecfSBarry Smith + dm - The `DM` object
7046c58f1c22SToby Isaac - n  - the label number
7047c58f1c22SToby Isaac 
7048c58f1c22SToby Isaac   Output Parameter:
7049c58f1c22SToby Isaac . name - the label name
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac   Level: intermediate
7052c58f1c22SToby Isaac 
705373ff1848SBarry Smith   Developer Note:
7054bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7055bb7acecfSBarry Smith 
70561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7057c58f1c22SToby Isaac @*/
7058d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7059d71ae5a4SJacob Faibussowitsch {
70605d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7061c58f1c22SToby Isaac   PetscInt    l    = 0;
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   PetscFunctionBegin;
7064c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70654f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7066c58f1c22SToby Isaac   while (next) {
7067c58f1c22SToby Isaac     if (l == n) {
70689566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
70693ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7070c58f1c22SToby Isaac     }
7071c58f1c22SToby Isaac     ++l;
7072c58f1c22SToby Isaac     next = next->next;
7073c58f1c22SToby Isaac   }
707463a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7075c58f1c22SToby Isaac }
7076c58f1c22SToby Isaac 
7077c58f1c22SToby Isaac /*@C
7078bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac   Not Collective
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Input Parameters:
7083bb7acecfSBarry Smith + dm   - The `DM` object
7084c58f1c22SToby Isaac - name - The label name
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Output Parameter:
7087bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Level: intermediate
7090c58f1c22SToby Isaac 
70911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7092c58f1c22SToby Isaac @*/
7093d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7094d71ae5a4SJacob Faibussowitsch {
70955d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7096d67d17b1SMatthew G. Knepley   const char *lname;
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac   PetscFunctionBegin;
7099c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71004f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71014f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7102c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7103c58f1c22SToby Isaac   while (next) {
71049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71059566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7106c58f1c22SToby Isaac     if (*hasLabel) break;
7107c58f1c22SToby Isaac     next = next->next;
7108c58f1c22SToby Isaac   }
71093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7110c58f1c22SToby Isaac }
7111c58f1c22SToby Isaac 
7112a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7113c58f1c22SToby Isaac /*@C
711420f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7115c58f1c22SToby Isaac 
7116c58f1c22SToby Isaac   Not Collective
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   Input Parameters:
7119bb7acecfSBarry Smith + dm   - The `DM` object
7120c58f1c22SToby Isaac - name - The label name
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac   Output Parameter:
712320f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7124c58f1c22SToby Isaac 
7125bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7126bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7127bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7128bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7129bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7130bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7131bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71326d7c9049SMatthew G. Knepley 
7133c58f1c22SToby Isaac   Level: intermediate
7134c58f1c22SToby Isaac 
713560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7136c58f1c22SToby Isaac @*/
7137d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7138d71ae5a4SJacob Faibussowitsch {
71395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7140c58f1c22SToby Isaac   PetscBool   hasLabel;
7141d67d17b1SMatthew G. Knepley   const char *lname;
7142c58f1c22SToby Isaac 
7143c58f1c22SToby Isaac   PetscFunctionBegin;
7144c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71454f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71464f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7147c58f1c22SToby Isaac   *label = NULL;
7148c58f1c22SToby Isaac   while (next) {
71499566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71509566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7151c58f1c22SToby Isaac     if (hasLabel) {
7152c58f1c22SToby Isaac       *label = next->label;
7153c58f1c22SToby Isaac       break;
7154c58f1c22SToby Isaac     }
7155c58f1c22SToby Isaac     next = next->next;
7156c58f1c22SToby Isaac   }
71573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7158c58f1c22SToby Isaac }
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac /*@C
7161bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Not Collective
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac   Input Parameters:
7166bb7acecfSBarry Smith + dm - The `DM` object
7167c58f1c22SToby Isaac - n  - the label number
7168c58f1c22SToby Isaac 
7169c58f1c22SToby Isaac   Output Parameter:
7170c58f1c22SToby Isaac . label - the label
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   Level: intermediate
7173c58f1c22SToby Isaac 
71741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7175c58f1c22SToby Isaac @*/
7176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7177d71ae5a4SJacob Faibussowitsch {
71785d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7179c58f1c22SToby Isaac   PetscInt    l    = 0;
7180c58f1c22SToby Isaac 
7181c58f1c22SToby Isaac   PetscFunctionBegin;
7182c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71834f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7184c58f1c22SToby Isaac   while (next) {
7185c58f1c22SToby Isaac     if (l == n) {
7186c58f1c22SToby Isaac       *label = next->label;
71873ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7188c58f1c22SToby Isaac     }
7189c58f1c22SToby Isaac     ++l;
7190c58f1c22SToby Isaac     next = next->next;
7191c58f1c22SToby Isaac   }
719263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7193c58f1c22SToby Isaac }
7194c58f1c22SToby Isaac 
7195c58f1c22SToby Isaac /*@C
7196bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7197c58f1c22SToby Isaac 
7198c58f1c22SToby Isaac   Not Collective
7199c58f1c22SToby Isaac 
7200c58f1c22SToby Isaac   Input Parameters:
7201bb7acecfSBarry Smith + dm    - The `DM` object
7202bb7acecfSBarry Smith - label - The `DMLabel`
7203c58f1c22SToby Isaac 
7204c58f1c22SToby Isaac   Level: developer
7205c58f1c22SToby Isaac 
720660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7207c58f1c22SToby Isaac @*/
7208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7209d71ae5a4SJacob Faibussowitsch {
72105d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7211c58f1c22SToby Isaac   PetscBool   hasLabel;
7212d67d17b1SMatthew G. Knepley   const char *lname;
72135d80c0bfSVaclav Hapla   PetscBool   flg;
7214c58f1c22SToby Isaac 
7215c58f1c22SToby Isaac   PetscFunctionBegin;
7216c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72179566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72189566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72197a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72209566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7221c58f1c22SToby Isaac   tmpLabel->label  = label;
7222c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72235d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72245d80c0bfSVaclav Hapla   *p = tmpLabel;
72259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72269566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72275d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72289566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7229ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7231c58f1c22SToby Isaac }
7232c58f1c22SToby Isaac 
7233a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7234c58f1c22SToby Isaac /*@C
72354a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72364a7ee7d0SMatthew G. Knepley 
72374a7ee7d0SMatthew G. Knepley   Not Collective
72384a7ee7d0SMatthew G. Knepley 
72394a7ee7d0SMatthew G. Knepley   Input Parameters:
7240bb7acecfSBarry Smith + dm    - The `DM` object
7241bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72424a7ee7d0SMatthew G. Knepley 
7243bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7244bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7245bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7246bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7247bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7248bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7249bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72504a7ee7d0SMatthew G. Knepley 
72514a7ee7d0SMatthew G. Knepley   Level: intermediate
72524a7ee7d0SMatthew G. Knepley 
72531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72544a7ee7d0SMatthew G. Knepley @*/
7255d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7256d71ae5a4SJacob Faibussowitsch {
72574a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72584a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72594a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72604a7ee7d0SMatthew G. Knepley 
72614a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
72624a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72634a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
72649566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
72654a7ee7d0SMatthew G. Knepley   while (next) {
72669566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72679566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
72684a7ee7d0SMatthew G. Knepley     if (hasLabel) {
72699566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
72709566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
72714a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
72729566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
72734a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
72749566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
72754a7ee7d0SMatthew G. Knepley       next->label = label;
72764a7ee7d0SMatthew G. Knepley       break;
72774a7ee7d0SMatthew G. Knepley     }
72784a7ee7d0SMatthew G. Knepley     next = next->next;
72794a7ee7d0SMatthew G. Knepley   }
72803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
72814a7ee7d0SMatthew G. Knepley }
72824a7ee7d0SMatthew G. Knepley 
72834a7ee7d0SMatthew G. Knepley /*@C
7284bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7285c58f1c22SToby Isaac 
7286c58f1c22SToby Isaac   Not Collective
7287c58f1c22SToby Isaac 
7288c58f1c22SToby Isaac   Input Parameters:
7289bb7acecfSBarry Smith + dm   - The `DM` object
7290c58f1c22SToby Isaac - name - The label name
7291c58f1c22SToby Isaac 
7292c58f1c22SToby Isaac   Output Parameter:
729320f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7294bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7295c58f1c22SToby Isaac 
7296c58f1c22SToby Isaac   Level: developer
7297c58f1c22SToby Isaac 
72981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7299c58f1c22SToby Isaac @*/
7300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7301d71ae5a4SJacob Faibussowitsch {
730295d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7303c58f1c22SToby Isaac   PetscBool   hasLabel;
7304d67d17b1SMatthew G. Knepley   const char *lname;
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac   PetscFunctionBegin;
7307c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73084f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7309e5472504SVaclav Hapla   if (label) {
73104f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7311c58f1c22SToby Isaac     *label = NULL;
7312e5472504SVaclav Hapla   }
73135d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73149566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73159566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7316c58f1c22SToby Isaac     if (hasLabel) {
731795d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73189566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
731995d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73209566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7321ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
732295d578d6SVaclav Hapla       if (label) *label = link->label;
73239566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73249566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7325c58f1c22SToby Isaac       break;
7326c58f1c22SToby Isaac     }
7327c58f1c22SToby Isaac   }
73283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7329c58f1c22SToby Isaac }
7330c58f1c22SToby Isaac 
7331306894acSVaclav Hapla /*@
7332bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7333306894acSVaclav Hapla 
7334306894acSVaclav Hapla   Not Collective
7335306894acSVaclav Hapla 
7336306894acSVaclav Hapla   Input Parameters:
7337bb7acecfSBarry Smith + dm           - The `DM` object
7338bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
733920f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7340306894acSVaclav Hapla 
7341306894acSVaclav Hapla   Level: developer
7342306894acSVaclav Hapla 
7343bb7acecfSBarry Smith   Note:
7344306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7345bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7346306894acSVaclav Hapla   *label nullified.
7347306894acSVaclav Hapla 
73481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7349306894acSVaclav Hapla @*/
7350d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7351d71ae5a4SJacob Faibussowitsch {
735243e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7353306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7354306894acSVaclav Hapla 
7355306894acSVaclav Hapla   PetscFunctionBegin;
7356306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73574f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73583ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7359306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7360306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73615d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
736243e45a93SVaclav Hapla     if (*label == link->label) {
7363306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
736443e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7365306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7366ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
736743e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
73689566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
73699566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7370306894acSVaclav Hapla       break;
7371306894acSVaclav Hapla     }
7372306894acSVaclav Hapla   }
73737a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
73743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7375306894acSVaclav Hapla }
7376306894acSVaclav Hapla 
7377c58f1c22SToby Isaac /*@C
7378c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7379c58f1c22SToby Isaac 
7380c58f1c22SToby Isaac   Not Collective
7381c58f1c22SToby Isaac 
7382c58f1c22SToby Isaac   Input Parameters:
7383bb7acecfSBarry Smith + dm   - The `DM` object
7384c58f1c22SToby Isaac - name - The label name
7385c58f1c22SToby Isaac 
7386c58f1c22SToby Isaac   Output Parameter:
7387c58f1c22SToby Isaac . output - The flag for output
7388c58f1c22SToby Isaac 
7389c58f1c22SToby Isaac   Level: developer
7390c58f1c22SToby Isaac 
73911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7392c58f1c22SToby Isaac @*/
7393d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7394d71ae5a4SJacob Faibussowitsch {
73955d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7396d67d17b1SMatthew G. Knepley   const char *lname;
7397c58f1c22SToby Isaac 
7398c58f1c22SToby Isaac   PetscFunctionBegin;
7399c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74004f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74014f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7402c58f1c22SToby Isaac   while (next) {
7403c58f1c22SToby Isaac     PetscBool flg;
7404c58f1c22SToby Isaac 
74059566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74069566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74079371c9d4SSatish Balay     if (flg) {
74089371c9d4SSatish Balay       *output = next->output;
74093ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74109371c9d4SSatish Balay     }
7411c58f1c22SToby Isaac     next = next->next;
7412c58f1c22SToby Isaac   }
741398921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7414c58f1c22SToby Isaac }
7415c58f1c22SToby Isaac 
7416c58f1c22SToby Isaac /*@C
7417bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7418c58f1c22SToby Isaac 
7419c58f1c22SToby Isaac   Not Collective
7420c58f1c22SToby Isaac 
7421c58f1c22SToby Isaac   Input Parameters:
7422bb7acecfSBarry Smith + dm     - The `DM` object
7423c58f1c22SToby Isaac . name   - The label name
7424bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   Level: developer
7427c58f1c22SToby Isaac 
74281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7429c58f1c22SToby Isaac @*/
7430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7431d71ae5a4SJacob Faibussowitsch {
74325d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7433d67d17b1SMatthew G. Knepley   const char *lname;
7434c58f1c22SToby Isaac 
7435c58f1c22SToby Isaac   PetscFunctionBegin;
7436c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74374f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7438c58f1c22SToby Isaac   while (next) {
7439c58f1c22SToby Isaac     PetscBool flg;
7440c58f1c22SToby Isaac 
74419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74429566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74439371c9d4SSatish Balay     if (flg) {
74449371c9d4SSatish Balay       next->output = output;
74453ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74469371c9d4SSatish Balay     }
7447c58f1c22SToby Isaac     next = next->next;
7448c58f1c22SToby Isaac   }
744998921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7450c58f1c22SToby Isaac }
7451c58f1c22SToby Isaac 
7452c58f1c22SToby Isaac /*@
7453bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7454c58f1c22SToby Isaac 
745520f4b53cSBarry Smith   Collective
7456c58f1c22SToby Isaac 
7457d8d19677SJose E. Roman   Input Parameters:
7458bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7459bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7460bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7461bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7462bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac   Level: intermediate
7465c58f1c22SToby Isaac 
7466bb7acecfSBarry Smith   Note:
74672cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7468c58f1c22SToby Isaac 
74691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7470c58f1c22SToby Isaac @*/
7471d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7472d71ae5a4SJacob Faibussowitsch {
74732cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7474c58f1c22SToby Isaac   const char *name;
7475c58f1c22SToby Isaac   PetscBool   flg;
74765d80c0bfSVaclav Hapla   DMLabelLink link;
7477c58f1c22SToby Isaac 
74785d80c0bfSVaclav Hapla   PetscFunctionBegin;
74795d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
74805d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
74815d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
74825d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
74837a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
74843ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
74855d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
74865d80c0bfSVaclav Hapla     label = link->label;
74879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
74885d80c0bfSVaclav Hapla     if (!all) {
74899566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7490c58f1c22SToby Isaac       if (flg) continue;
74919566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
74927d5acc75SStefano Zampini       if (flg) continue;
74939566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7494ba2698f1SMatthew G. Knepley       if (flg) continue;
74955d80c0bfSVaclav Hapla     }
74969566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
74972cbb9b06SVaclav Hapla     if (labelOld) {
74982cbb9b06SVaclav Hapla       switch (emode) {
7499d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7500d71ae5a4SJacob Faibussowitsch         continue;
7501d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7502d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7503d71ae5a4SJacob Faibussowitsch         break;
7504d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7505d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7506d71ae5a4SJacob Faibussowitsch       default:
7507d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75082cbb9b06SVaclav Hapla       }
75092cbb9b06SVaclav Hapla     }
75105d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75119566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75125d80c0bfSVaclav Hapla     } else {
75135d80c0bfSVaclav Hapla       labelNew = label;
75145d80c0bfSVaclav Hapla     }
75159566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75169566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7517c58f1c22SToby Isaac   }
75183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7519c58f1c22SToby Isaac }
7520461a15a0SLisandro Dalcin 
7521609dae6eSVaclav Hapla /*@C
7522b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7523609dae6eSVaclav Hapla 
752420f4b53cSBarry Smith   Collective; No Fortran Support
7525609dae6eSVaclav Hapla 
7526609dae6eSVaclav Hapla   Input Parameters:
7527bb7acecfSBarry Smith + dm0 - First `DM` object
7528bb7acecfSBarry Smith - dm1 - Second `DM` object
7529609dae6eSVaclav Hapla 
7530a4e35b19SJacob Faibussowitsch   Output Parameters:
75315efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
753220f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7533609dae6eSVaclav Hapla 
7534609dae6eSVaclav Hapla   Level: intermediate
7535609dae6eSVaclav Hapla 
7536609dae6eSVaclav Hapla   Notes:
7537bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7538bb7acecfSBarry Smith 
753920f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7540bb7acecfSBarry Smith 
754120f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7542609dae6eSVaclav Hapla 
75435efe38ccSVaclav Hapla   The output message is set independently on each rank.
7544bb7acecfSBarry Smith 
7545bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7546bb7acecfSBarry Smith 
754720f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7548bb7acecfSBarry Smith 
754920f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7550609dae6eSVaclav Hapla 
7551609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7552bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7553609dae6eSVaclav Hapla 
75541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7555609dae6eSVaclav Hapla @*/
7556d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7557d71ae5a4SJacob Faibussowitsch {
75585efe38ccSVaclav Hapla   PetscInt    n, i;
7559609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
75605efe38ccSVaclav Hapla   PetscBool   eq;
7561609dae6eSVaclav Hapla   MPI_Comm    comm;
75625efe38ccSVaclav Hapla   PetscMPIInt rank;
7563609dae6eSVaclav Hapla 
7564609dae6eSVaclav Hapla   PetscFunctionBegin;
7565609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7566609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7567609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
75684f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
75694f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
75709566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
75719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
75725efe38ccSVaclav Hapla   {
75735efe38ccSVaclav Hapla     PetscInt n1;
75745efe38ccSVaclav Hapla 
75759566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
75769566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
75775efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
757848a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7579712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
75805efe38ccSVaclav Hapla     if (!eq) goto finish;
75815efe38ccSVaclav Hapla   }
75825efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7583609dae6eSVaclav Hapla     DMLabel     l0, l1;
7584609dae6eSVaclav Hapla     const char *name;
7585609dae6eSVaclav Hapla     char       *msgInner;
7586609dae6eSVaclav Hapla 
7587609dae6eSVaclav Hapla     /* Ignore label order */
75889566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
75899566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
75909566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7591609dae6eSVaclav Hapla     if (!l1) {
759263a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
75935efe38ccSVaclav Hapla       eq = PETSC_FALSE;
75945efe38ccSVaclav Hapla       break;
7595609dae6eSVaclav Hapla     }
75969566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
75979566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
75989566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
75995efe38ccSVaclav Hapla     if (!eq) break;
7600609dae6eSVaclav Hapla   }
7601712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7602609dae6eSVaclav Hapla finish:
76035efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7604609dae6eSVaclav Hapla   if (message) {
7605609dae6eSVaclav Hapla     *message = NULL;
760648a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76075efe38ccSVaclav Hapla   } else {
760848a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76099566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76105efe38ccSVaclav Hapla   }
76115efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76125efe38ccSVaclav Hapla   if (equal) *equal = eq;
76137a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7615609dae6eSVaclav Hapla }
7616609dae6eSVaclav Hapla 
7617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7618d71ae5a4SJacob Faibussowitsch {
7619461a15a0SLisandro Dalcin   PetscFunctionBegin;
76204f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7621461a15a0SLisandro Dalcin   if (!*label) {
76229566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76239566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7624461a15a0SLisandro Dalcin   }
76259566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7627461a15a0SLisandro Dalcin }
7628461a15a0SLisandro Dalcin 
76290fdc7489SMatthew Knepley /*
76300fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76310fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76320fdc7489SMatthew Knepley   (label, id) pair in the DM.
76330fdc7489SMatthew Knepley 
76340fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76350fdc7489SMatthew Knepley   each label.
76360fdc7489SMatthew Knepley */
7637d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7638d71ae5a4SJacob Faibussowitsch {
76390fdc7489SMatthew Knepley   DMUniversalLabel ul;
76400fdc7489SMatthew Knepley   PetscBool       *active;
76410fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76420fdc7489SMatthew Knepley 
76430fdc7489SMatthew Knepley   PetscFunctionBegin;
76449566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76459566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76469566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76479566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76480fdc7489SMatthew Knepley   ul->Nl = 0;
76490fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76500fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76510fdc7489SMatthew Knepley     const char *name;
76520fdc7489SMatthew Knepley 
76539566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76549566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76559566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76560fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76570fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76580fdc7489SMatthew Knepley   }
76599566063dSJacob 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));
76600fdc7489SMatthew Knepley   ul->Nv = 0;
76610fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76620fdc7489SMatthew Knepley     DMLabel     label;
76630fdc7489SMatthew Knepley     PetscInt    nv;
76640fdc7489SMatthew Knepley     const char *name;
76650fdc7489SMatthew Knepley 
76660fdc7489SMatthew Knepley     if (!active[l]) continue;
76679566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76689566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76699566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76709566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
76710fdc7489SMatthew Knepley     ul->indices[m] = l;
76720fdc7489SMatthew Knepley     ul->Nv += nv;
76730fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
76740fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
76750fdc7489SMatthew Knepley     ++m;
76760fdc7489SMatthew Knepley   }
76770fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
76780fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
76790fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
76800fdc7489SMatthew Knepley   }
76810fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76820fdc7489SMatthew Knepley     PetscInt b;
76830fdc7489SMatthew Knepley 
76840fdc7489SMatthew Knepley     ul->masks[l] = 0;
76850fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
76860fdc7489SMatthew Knepley   }
76879566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
76880fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
76890fdc7489SMatthew Knepley     DMLabel         label;
76900fdc7489SMatthew Knepley     IS              valueIS;
76910fdc7489SMatthew Knepley     const PetscInt *varr;
76920fdc7489SMatthew Knepley     PetscInt        nv, v;
76930fdc7489SMatthew Knepley 
76940fdc7489SMatthew Knepley     if (!active[l]) continue;
76959566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
76969566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
76979566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
76989566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7699ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77009566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77019566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77029566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77030fdc7489SMatthew Knepley     ++m;
77040fdc7489SMatthew Knepley   }
77059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77060fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77070fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77080fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77090fdc7489SMatthew Knepley 
77100fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77110fdc7489SMatthew Knepley       DMLabel  label;
77120649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77130fdc7489SMatthew Knepley 
77140fdc7489SMatthew Knepley       if (!active[l]) continue;
77159566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77169566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77179566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77189371c9d4SSatish Balay       if (val == defval) {
77199371c9d4SSatish Balay         ++m;
77209371c9d4SSatish Balay         continue;
77219371c9d4SSatish Balay       }
77220649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77230fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77249566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
772563a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77260fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77270fdc7489SMatthew Knepley       ++m;
77280fdc7489SMatthew Knepley     }
77299566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77300fdc7489SMatthew Knepley   }
77319566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77320fdc7489SMatthew Knepley   *universal = ul;
77333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77340fdc7489SMatthew Knepley }
77350fdc7489SMatthew Knepley 
7736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7737d71ae5a4SJacob Faibussowitsch {
77380fdc7489SMatthew Knepley   PetscInt l;
77390fdc7489SMatthew Knepley 
77400fdc7489SMatthew Knepley   PetscFunctionBegin;
77419566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77429566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77439566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77449566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77459566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77460fdc7489SMatthew Knepley   *universal = NULL;
77473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77480fdc7489SMatthew Knepley }
77490fdc7489SMatthew Knepley 
7750d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7751d71ae5a4SJacob Faibussowitsch {
77520fdc7489SMatthew Knepley   PetscFunctionBegin;
77534f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77540fdc7489SMatthew Knepley   *ulabel = ul->label;
77553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77560fdc7489SMatthew Knepley }
77570fdc7489SMatthew Knepley 
7758d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7759d71ae5a4SJacob Faibussowitsch {
77600fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
77610fdc7489SMatthew Knepley 
77620fdc7489SMatthew Knepley   PetscFunctionBegin;
7763064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
77640fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77659566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
77669566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
77670fdc7489SMatthew Knepley   }
77680fdc7489SMatthew Knepley   if (preserveOrder) {
77690fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
77700fdc7489SMatthew Knepley       const char *name;
77710fdc7489SMatthew Knepley       PetscBool   match;
77720fdc7489SMatthew Knepley 
77739566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
77749566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
777563a3b9bcSJacob 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]);
77760fdc7489SMatthew Knepley     }
77770fdc7489SMatthew Knepley   }
77783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77790fdc7489SMatthew Knepley }
77800fdc7489SMatthew Knepley 
7781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7782d71ae5a4SJacob Faibussowitsch {
77830fdc7489SMatthew Knepley   PetscInt l;
77840fdc7489SMatthew Knepley 
77850fdc7489SMatthew Knepley   PetscFunctionBegin;
77860fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77870fdc7489SMatthew Knepley     DMLabel  label;
77880fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
77890fdc7489SMatthew Knepley 
77900fdc7489SMatthew Knepley     if (lval) {
77919566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
77929566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
77939566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
77940fdc7489SMatthew Knepley     }
77950fdc7489SMatthew Knepley   }
77963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77970fdc7489SMatthew Knepley }
7798a8fb8f29SToby Isaac 
7799a8fb8f29SToby Isaac /*@
7800bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7801bb7acecfSBarry Smith 
780220f4b53cSBarry Smith   Not Collective
7803a8fb8f29SToby Isaac 
7804a8fb8f29SToby Isaac   Input Parameter:
7805bb7acecfSBarry Smith . dm - The `DM` object
7806a8fb8f29SToby Isaac 
7807a8fb8f29SToby Isaac   Output Parameter:
7808bb7acecfSBarry Smith . cdm - The coarse `DM`
7809a8fb8f29SToby Isaac 
7810a8fb8f29SToby Isaac   Level: intermediate
7811a8fb8f29SToby Isaac 
78121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7813a8fb8f29SToby Isaac @*/
7814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7815d71ae5a4SJacob Faibussowitsch {
7816a8fb8f29SToby Isaac   PetscFunctionBegin;
7817a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78184f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7819a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7821a8fb8f29SToby Isaac }
7822a8fb8f29SToby Isaac 
7823a8fb8f29SToby Isaac /*@
7824bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7825a8fb8f29SToby Isaac 
7826a8fb8f29SToby Isaac   Input Parameters:
7827bb7acecfSBarry Smith + dm  - The `DM` object
7828bb7acecfSBarry Smith - cdm - The coarse `DM`
7829a8fb8f29SToby Isaac 
7830a8fb8f29SToby Isaac   Level: intermediate
7831a8fb8f29SToby Isaac 
7832bb7acecfSBarry Smith   Note:
7833bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7834bb7acecfSBarry Smith 
78351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7836a8fb8f29SToby Isaac @*/
7837d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7838d71ae5a4SJacob Faibussowitsch {
7839a8fb8f29SToby Isaac   PetscFunctionBegin;
7840a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7841a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
784289d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78439566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78449566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7845a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7847a8fb8f29SToby Isaac }
7848a8fb8f29SToby Isaac 
784988bdff64SToby Isaac /*@
7850bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
785188bdff64SToby Isaac 
785288bdff64SToby Isaac   Input Parameter:
7853bb7acecfSBarry Smith . dm - The `DM` object
785488bdff64SToby Isaac 
785588bdff64SToby Isaac   Output Parameter:
7856bb7acecfSBarry Smith . fdm - The fine `DM`
785788bdff64SToby Isaac 
785888bdff64SToby Isaac   Level: intermediate
785988bdff64SToby Isaac 
78601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
786188bdff64SToby Isaac @*/
7862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7863d71ae5a4SJacob Faibussowitsch {
786488bdff64SToby Isaac   PetscFunctionBegin;
786588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78664f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
786788bdff64SToby Isaac   *fdm = dm->fineMesh;
78683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
786988bdff64SToby Isaac }
787088bdff64SToby Isaac 
787188bdff64SToby Isaac /*@
7872bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
787388bdff64SToby Isaac 
787488bdff64SToby Isaac   Input Parameters:
7875bb7acecfSBarry Smith + dm  - The `DM` object
7876bb7acecfSBarry Smith - fdm - The fine `DM`
787788bdff64SToby Isaac 
7878bb7acecfSBarry Smith   Level: developer
787988bdff64SToby Isaac 
7880bb7acecfSBarry Smith   Note:
7881bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7882bb7acecfSBarry Smith 
78831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
788488bdff64SToby Isaac @*/
7885d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7886d71ae5a4SJacob Faibussowitsch {
788788bdff64SToby Isaac   PetscFunctionBegin;
788888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
788988bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
789089d734beSBarry Smith   if (dm == fdm) fdm = NULL;
78919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
78929566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
789388bdff64SToby Isaac   dm->fineMesh = fdm;
78943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
789588bdff64SToby Isaac }
789688bdff64SToby Isaac 
7897a6ba4734SToby Isaac /*@C
7898bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7899a6ba4734SToby Isaac 
790020f4b53cSBarry Smith   Collective
7901783e2ec8SMatthew G. Knepley 
7902a6ba4734SToby Isaac   Input Parameters:
7903bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7904bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7905a6ba4734SToby Isaac . name     - The BC name
790645480ffeSMatthew G. Knepley . label    - The label defining constrained points
7907bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
790845480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7909a6ba4734SToby Isaac . field    - The field to constrain
791045480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7911a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7912a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
791356cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7914a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7915a6ba4734SToby Isaac 
791645480ffeSMatthew G. Knepley   Output Parameter:
791745480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
791845480ffeSMatthew G. Knepley 
7919a6ba4734SToby Isaac   Options Database Keys:
7920a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7921a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7922a6ba4734SToby Isaac 
792320f4b53cSBarry Smith   Level: intermediate
792420f4b53cSBarry Smith 
7925bb7acecfSBarry Smith   Notes:
7926*aeebefc2SPierre Jolivet   Both bcFunc and bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
792773ff1848SBarry Smith .vb
792873ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
792973ff1848SBarry Smith .ve
793056cf3b9cSMatthew G. Knepley 
7931a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
793256cf3b9cSMatthew G. Knepley 
793320f4b53cSBarry Smith .vb
793420f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
793520f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
793620f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
793720f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
793820f4b53cSBarry Smith .ve
793956cf3b9cSMatthew G. Knepley + dim - the spatial dimension
794056cf3b9cSMatthew G. Knepley . Nf - the number of fields
794156cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
794256cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
794356cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
794456cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
794556cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
794656cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
794756cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
794856cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
794956cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
795056cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
795156cf3b9cSMatthew G. Knepley . t - current time
795256cf3b9cSMatthew G. Knepley . x - coordinates of the current point
795356cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
795456cf3b9cSMatthew G. Knepley . constants - constant parameters
795556cf3b9cSMatthew G. Knepley - bcval - output values at the current point
795656cf3b9cSMatthew G. Knepley 
79571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7958a6ba4734SToby Isaac @*/
7959d71ae5a4SJacob 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)
7960d71ae5a4SJacob Faibussowitsch {
7961e5e52638SMatthew G. Knepley   PetscDS ds;
7962a6ba4734SToby Isaac 
7963a6ba4734SToby Isaac   PetscFunctionBegin;
7964a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7965783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
796645480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
796745480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
796845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
796945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
797001a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
79719566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7972799db056SMatthew G. Knepley   /* Complete label */
7973799db056SMatthew G. Knepley   if (label) {
7974799db056SMatthew G. Knepley     PetscObject  obj;
7975799db056SMatthew G. Knepley     PetscClassId id;
7976799db056SMatthew G. Knepley 
7977799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7978799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7979799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7980799db056SMatthew G. Knepley       DM plex;
7981799db056SMatthew G. Knepley 
7982799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7983799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7984799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7985799db056SMatthew G. Knepley     }
7986799db056SMatthew G. Knepley   }
79879566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
79883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7989a6ba4734SToby Isaac }
7990a6ba4734SToby Isaac 
799145480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7992d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7993d71ae5a4SJacob Faibussowitsch {
7994e5e52638SMatthew G. Knepley   PetscDS     ds;
7995dff059c6SToby Isaac   DMBoundary *lastnext;
7996e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7997e6f8dbb6SToby Isaac 
7998e6f8dbb6SToby Isaac   PetscFunctionBegin;
79999566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8000e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
800147a1f5adSToby Isaac   if (dm->boundary) {
800247a1f5adSToby Isaac     DMBoundary next = dm->boundary;
800347a1f5adSToby Isaac 
800447a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80053ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
800647a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
800747a1f5adSToby Isaac     while (next) {
800847a1f5adSToby Isaac       DMBoundary b = next;
800947a1f5adSToby Isaac 
801047a1f5adSToby Isaac       next = b->next;
80119566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8012a6ba4734SToby Isaac     }
801347a1f5adSToby Isaac     dm->boundary = NULL;
8014a6ba4734SToby Isaac   }
801547a1f5adSToby Isaac 
8016f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8017e6f8dbb6SToby Isaac   while (dsbound) {
8018e6f8dbb6SToby Isaac     DMBoundary dmbound;
8019e6f8dbb6SToby Isaac 
80209566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8021e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
802245480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
802347a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8024dff059c6SToby Isaac     *lastnext = dmbound;
8025f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8026dff059c6SToby Isaac     dsbound   = dsbound->next;
8027a6ba4734SToby Isaac   }
80283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8029a6ba4734SToby Isaac }
8030a6ba4734SToby Isaac 
8031bb7acecfSBarry Smith /* TODO: missing manual page */
8032d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8033d71ae5a4SJacob Faibussowitsch {
8034b95f2879SToby Isaac   DMBoundary b;
8035a6ba4734SToby Isaac 
8036a6ba4734SToby Isaac   PetscFunctionBegin;
8037a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80384f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8039a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80409566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8041b95f2879SToby Isaac   b = dm->boundary;
8042a6ba4734SToby Isaac   while (b && !(*isBd)) {
8043e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8044e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8045a6ba4734SToby Isaac     PetscInt   i;
8046a6ba4734SToby Isaac 
804745480ffeSMatthew G. Knepley     if (label) {
80489566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8049a6ba4734SToby Isaac     }
8050a6ba4734SToby Isaac     b = b->next;
8051a6ba4734SToby Isaac   }
80523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8053a6ba4734SToby Isaac }
80544d6f44ffSToby Isaac 
80554d6f44ffSToby Isaac /*@C
8056bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8057a6e0b375SMatthew G. Knepley 
805820f4b53cSBarry Smith   Collective
80594d6f44ffSToby Isaac 
80604d6f44ffSToby Isaac   Input Parameters:
8061bb7acecfSBarry Smith + dm    - The `DM`
80620709b2feSToby Isaac . time  - The time
80634d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
80644d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
80654d6f44ffSToby Isaac - mode  - The insertion mode for values
80664d6f44ffSToby Isaac 
80674d6f44ffSToby Isaac   Output Parameter:
80684d6f44ffSToby Isaac . X - vector
80694d6f44ffSToby Isaac 
807020f4b53cSBarry Smith   Calling sequence of `funcs`:
80714d6f44ffSToby Isaac + dim  - The spatial dimension
80728ec8862eSJed Brown . time - The time at which to sample
80734d6f44ffSToby Isaac . x    - The coordinates
807477b739a6SMatthew Knepley . Nc   - The number of components
80754d6f44ffSToby Isaac . u    - The output field values
80764d6f44ffSToby Isaac - ctx  - optional user-defined function context
80774d6f44ffSToby Isaac 
80784d6f44ffSToby Isaac   Level: developer
80794d6f44ffSToby Isaac 
8080bb7acecfSBarry Smith   Developer Notes:
8081bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8082bb7acecfSBarry Smith 
8083bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8084bb7acecfSBarry Smith 
80851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
80864d6f44ffSToby Isaac @*/
8087a4e35b19SJacob 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)
8088d71ae5a4SJacob Faibussowitsch {
80894d6f44ffSToby Isaac   Vec localX;
80904d6f44ffSToby Isaac 
80914d6f44ffSToby Isaac   PetscFunctionBegin;
80924d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8093708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
80949566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8095f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80969566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
80979566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80989566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80999566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8100708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81024d6f44ffSToby Isaac }
81034d6f44ffSToby Isaac 
8104a6e0b375SMatthew G. Knepley /*@C
8105bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8106a6e0b375SMatthew G. Knepley 
810720f4b53cSBarry Smith   Not Collective
8108a6e0b375SMatthew G. Knepley 
8109a6e0b375SMatthew G. Knepley   Input Parameters:
8110bb7acecfSBarry Smith + dm    - The `DM`
8111a6e0b375SMatthew G. Knepley . time  - The time
8112a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8113a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8114a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8115a6e0b375SMatthew G. Knepley 
8116a6e0b375SMatthew G. Knepley   Output Parameter:
8117a6e0b375SMatthew G. Knepley . localX - vector
8118a6e0b375SMatthew G. Knepley 
811920f4b53cSBarry Smith   Calling sequence of `funcs`:
8120a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8121a4e35b19SJacob Faibussowitsch . time - The current timestep
8122a6e0b375SMatthew G. Knepley . x    - The coordinates
812377b739a6SMatthew Knepley . Nc   - The number of components
8124a6e0b375SMatthew G. Knepley . u    - The output field values
8125a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8126a6e0b375SMatthew G. Knepley 
8127a6e0b375SMatthew G. Knepley   Level: developer
8128a6e0b375SMatthew G. Knepley 
8129bb7acecfSBarry Smith   Developer Notes:
8130bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8131bb7acecfSBarry Smith 
8132bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8133bb7acecfSBarry Smith 
81341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8135a6e0b375SMatthew G. Knepley @*/
8136a4e35b19SJacob 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)
8137d71ae5a4SJacob Faibussowitsch {
81384d6f44ffSToby Isaac   PetscFunctionBegin;
81394d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8140064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8141fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81434d6f44ffSToby Isaac }
81444d6f44ffSToby Isaac 
8145a6e0b375SMatthew G. Knepley /*@C
8146bb7acecfSBarry 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.
8147a6e0b375SMatthew G. Knepley 
814820f4b53cSBarry Smith   Collective
8149a6e0b375SMatthew G. Knepley 
8150a6e0b375SMatthew G. Knepley   Input Parameters:
8151bb7acecfSBarry Smith + dm     - The `DM`
8152a6e0b375SMatthew G. Knepley . time   - The time
8153a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8154a4e35b19SJacob Faibussowitsch . ids    - The ids
8155a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8156a4e35b19SJacob Faibussowitsch . comps  - The components
8157bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8158a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8159bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8160a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8161a6e0b375SMatthew G. Knepley 
8162a6e0b375SMatthew G. Knepley   Output Parameter:
8163a6e0b375SMatthew G. Knepley . X - vector
8164a6e0b375SMatthew G. Knepley 
816520f4b53cSBarry Smith   Calling sequence of `funcs`:
8166a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8167a4e35b19SJacob Faibussowitsch . time - The current timestep
8168a6e0b375SMatthew G. Knepley . x    - The coordinates
816977b739a6SMatthew Knepley . Nc   - The number of components
8170a6e0b375SMatthew G. Knepley . u    - The output field values
8171a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8172a6e0b375SMatthew G. Knepley 
8173a6e0b375SMatthew G. Knepley   Level: developer
8174a6e0b375SMatthew G. Knepley 
8175bb7acecfSBarry Smith   Developer Notes:
8176bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8177bb7acecfSBarry Smith 
8178bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8179bb7acecfSBarry Smith 
81801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8181a6e0b375SMatthew G. Knepley @*/
8182a4e35b19SJacob 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)
8183d71ae5a4SJacob Faibussowitsch {
81842c53366bSMatthew G. Knepley   Vec localX;
81852c53366bSMatthew G. Knepley 
81862c53366bSMatthew G. Knepley   PetscFunctionBegin;
81872c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81889566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8189f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81909566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81919566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81929566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81939566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
81943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81952c53366bSMatthew G. Knepley }
81962c53366bSMatthew G. Knepley 
8197a6e0b375SMatthew G. Knepley /*@C
8198bb7acecfSBarry 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.
8199a6e0b375SMatthew G. Knepley 
820020f4b53cSBarry Smith   Not Collective
8201a6e0b375SMatthew G. Knepley 
8202a6e0b375SMatthew G. Knepley   Input Parameters:
8203bb7acecfSBarry Smith + dm     - The `DM`
8204a6e0b375SMatthew G. Knepley . time   - The time
8205bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8206a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8207a4e35b19SJacob Faibussowitsch . ids    - The ids
8208a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8209a4e35b19SJacob Faibussowitsch . comps  - The components
8210a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8211a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8212a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8213a6e0b375SMatthew G. Knepley 
8214a6e0b375SMatthew G. Knepley   Output Parameter:
8215a6e0b375SMatthew G. Knepley . localX - vector
8216a6e0b375SMatthew G. Knepley 
821720f4b53cSBarry Smith   Calling sequence of `funcs`:
8218a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8219a4e35b19SJacob Faibussowitsch . time - The current time
8220a6e0b375SMatthew G. Knepley . x    - The coordinates
822177b739a6SMatthew Knepley . Nc   - The number of components
8222a6e0b375SMatthew G. Knepley . u    - The output field values
8223a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8224a6e0b375SMatthew G. Knepley 
8225a6e0b375SMatthew G. Knepley   Level: developer
8226a6e0b375SMatthew G. Knepley 
8227bb7acecfSBarry Smith   Developer Notes:
8228bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8229bb7acecfSBarry Smith 
8230bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8231bb7acecfSBarry Smith 
82321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8233a6e0b375SMatthew G. Knepley @*/
8234a4e35b19SJacob 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)
8235d71ae5a4SJacob Faibussowitsch {
82364d6f44ffSToby Isaac   PetscFunctionBegin;
82374d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8238064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8239fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82414d6f44ffSToby Isaac }
82422716604bSToby Isaac 
8243a6e0b375SMatthew G. Knepley /*@C
8244bb7acecfSBarry 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.
8245a6e0b375SMatthew G. Knepley 
824620f4b53cSBarry Smith   Not Collective
8247a6e0b375SMatthew G. Knepley 
8248a6e0b375SMatthew G. Knepley   Input Parameters:
8249bb7acecfSBarry Smith + dm     - The `DM`
8250a6e0b375SMatthew G. Knepley . time   - The time
825120f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8252a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8253a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8254a6e0b375SMatthew G. Knepley 
8255a6e0b375SMatthew G. Knepley   Output Parameter:
8256a6e0b375SMatthew G. Knepley . localX - The output vector
8257a6e0b375SMatthew G. Knepley 
825820f4b53cSBarry Smith   Calling sequence of `funcs`:
8259a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8260a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8261a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8262a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8263a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8264a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8265a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8266a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8267a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8268a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8269a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8270a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8271a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8272a6e0b375SMatthew G. Knepley . t            - The current time
8273a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8274a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8275a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8276a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8277a6e0b375SMatthew G. Knepley 
827873ff1848SBarry Smith   Level: intermediate
827973ff1848SBarry Smith 
8280bb7acecfSBarry Smith   Note:
8281bb7acecfSBarry 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.
8282bb7acecfSBarry 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
8283bb7acecfSBarry 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
8284a6e0b375SMatthew 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.
8285a6e0b375SMatthew G. Knepley 
8286bb7acecfSBarry Smith   Developer Notes:
8287bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8288bb7acecfSBarry Smith 
8289bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8290bb7acecfSBarry Smith 
8291a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8292a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8293a6e0b375SMatthew G. Knepley @*/
8294a4e35b19SJacob 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)
8295d71ae5a4SJacob Faibussowitsch {
82968c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82978c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8298eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
82998c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8300fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83028c6c5593SMatthew G. Knepley }
83038c6c5593SMatthew G. Knepley 
8304a6e0b375SMatthew G. Knepley /*@C
8305a6e0b375SMatthew 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.
8306a6e0b375SMatthew G. Knepley 
830720f4b53cSBarry Smith   Not Collective
8308a6e0b375SMatthew G. Knepley 
8309a6e0b375SMatthew G. Knepley   Input Parameters:
8310bb7acecfSBarry Smith + dm     - The `DM`
8311a6e0b375SMatthew G. Knepley . time   - The time
8312bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8313a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8314a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8315bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
831620f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8317a6e0b375SMatthew G. Knepley . localU - The input field vector
8318a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8319a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8320a6e0b375SMatthew G. Knepley 
8321a6e0b375SMatthew G. Knepley   Output Parameter:
8322a6e0b375SMatthew G. Knepley . localX - The output vector
8323a6e0b375SMatthew G. Knepley 
832420f4b53cSBarry Smith   Calling sequence of `funcs`:
8325a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8326a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8327a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8328a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8329a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8330a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8331a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8332a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8333a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8334a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8335a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8336a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8337a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8338a6e0b375SMatthew G. Knepley . t            - The current time
8339a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8340a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8341a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8342a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8343a6e0b375SMatthew G. Knepley 
834473ff1848SBarry Smith   Level: intermediate
834573ff1848SBarry Smith 
8346bb7acecfSBarry Smith   Note:
8347bb7acecfSBarry 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.
8348bb7acecfSBarry 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
8349bb7acecfSBarry 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
8350a6e0b375SMatthew 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.
8351a6e0b375SMatthew G. Knepley 
8352bb7acecfSBarry Smith   Developer Notes:
8353bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8354bb7acecfSBarry Smith 
8355bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8356bb7acecfSBarry Smith 
83571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8358a6e0b375SMatthew G. Knepley @*/
8359a4e35b19SJacob 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)
8360d71ae5a4SJacob Faibussowitsch {
83618c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83628c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8363064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8364064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8365fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
83663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83678c6c5593SMatthew G. Knepley }
83688c6c5593SMatthew G. Knepley 
83692716604bSToby Isaac /*@C
8370d29d7c6eSMatthew 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.
8371d29d7c6eSMatthew G. Knepley 
837220f4b53cSBarry Smith   Not Collective
8373d29d7c6eSMatthew G. Knepley 
8374d29d7c6eSMatthew G. Knepley   Input Parameters:
8375bb7acecfSBarry Smith + dm     - The `DM`
8376d29d7c6eSMatthew G. Knepley . time   - The time
8377bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8378d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8379d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8380bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
838120f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8382d29d7c6eSMatthew G. Knepley . U      - The input field vector
8383d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8384d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8385d29d7c6eSMatthew G. Knepley 
8386d29d7c6eSMatthew G. Knepley   Output Parameter:
8387d29d7c6eSMatthew G. Knepley . X - The output vector
8388d29d7c6eSMatthew G. Knepley 
838920f4b53cSBarry Smith   Calling sequence of `funcs`:
8390d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8391d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8392d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8393d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8394d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8395d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8396d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8397d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8398d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8399d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8400d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8401d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8402d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8403d29d7c6eSMatthew G. Knepley . t            - The current time
8404d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8405d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8406d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8407d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8408d29d7c6eSMatthew G. Knepley 
840973ff1848SBarry Smith   Level: intermediate
841073ff1848SBarry Smith 
8411bb7acecfSBarry Smith   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
8415d29d7c6eSMatthew 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.
8416d29d7c6eSMatthew G. Knepley 
8417bb7acecfSBarry Smith   Developer Notes:
8418bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8419bb7acecfSBarry Smith 
8420bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8421bb7acecfSBarry Smith 
84221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8423d29d7c6eSMatthew G. Knepley @*/
8424a4e35b19SJacob 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)
8425d71ae5a4SJacob Faibussowitsch {
8426d29d7c6eSMatthew G. Knepley   DM  dmIn;
8427d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8428d29d7c6eSMatthew G. Knepley 
8429d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8430d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8431d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8432d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8433d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8434f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
843572fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
843672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8437d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8438d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8439d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8440d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8441d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8443d29d7c6eSMatthew G. Knepley }
8444d29d7c6eSMatthew G. Knepley 
8445d29d7c6eSMatthew G. Knepley /*@C
8446ece3a9fcSMatthew 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.
8447ece3a9fcSMatthew G. Knepley 
844820f4b53cSBarry Smith   Not Collective
8449ece3a9fcSMatthew G. Knepley 
8450ece3a9fcSMatthew G. Knepley   Input Parameters:
8451bb7acecfSBarry Smith + dm     - The `DM`
8452ece3a9fcSMatthew G. Knepley . time   - The time
8453bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8454ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8455ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8456bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
845720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8458ece3a9fcSMatthew G. Knepley . localU - The input field vector
8459ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8460ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8461ece3a9fcSMatthew G. Knepley 
8462ece3a9fcSMatthew G. Knepley   Output Parameter:
8463ece3a9fcSMatthew G. Knepley . localX - The output vector
8464ece3a9fcSMatthew G. Knepley 
846520f4b53cSBarry Smith   Calling sequence of `funcs`:
8466ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8467ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8468ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8469ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8470ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8471ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8472ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8473ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8474ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8475ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8476ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8477ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8478ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8479ece3a9fcSMatthew G. Knepley . t            - The current time
8480ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8481ece3a9fcSMatthew G. Knepley . n            - The face normal
8482ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8483ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8484ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8485ece3a9fcSMatthew G. Knepley 
848673ff1848SBarry Smith   Level: intermediate
848773ff1848SBarry Smith 
8488ece3a9fcSMatthew G. Knepley   Note:
8489bb7acecfSBarry 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.
8490bb7acecfSBarry 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
8491bb7acecfSBarry 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
8492ece3a9fcSMatthew 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.
8493ece3a9fcSMatthew G. Knepley 
8494bb7acecfSBarry Smith   Developer Notes:
8495bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8496bb7acecfSBarry Smith 
8497bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8498bb7acecfSBarry Smith 
84991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8500ece3a9fcSMatthew G. Knepley @*/
8501a4e35b19SJacob 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)
8502d71ae5a4SJacob Faibussowitsch {
8503ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8504ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8505064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8506064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8507fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8509ece3a9fcSMatthew G. Knepley }
8510ece3a9fcSMatthew G. Knepley 
8511ece3a9fcSMatthew G. Knepley /*@C
85122716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85132716604bSToby Isaac 
851420f4b53cSBarry Smith   Collective
8515bb7acecfSBarry Smith 
85162716604bSToby Isaac   Input Parameters:
8517bb7acecfSBarry Smith + dm    - The `DM`
85180709b2feSToby Isaac . time  - The time
85192716604bSToby Isaac . funcs - The functions to evaluate for each field component
85202716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8521574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85222716604bSToby Isaac 
85232716604bSToby Isaac   Output Parameter:
85242716604bSToby Isaac . diff - The diff ||u - u_h||_2
85252716604bSToby Isaac 
85262716604bSToby Isaac   Level: developer
85272716604bSToby Isaac 
8528bb7acecfSBarry Smith   Developer Notes:
8529bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8530bb7acecfSBarry Smith 
8531bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8532bb7acecfSBarry Smith 
85331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85342716604bSToby Isaac @*/
8535d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8536d71ae5a4SJacob Faibussowitsch {
85372716604bSToby Isaac   PetscFunctionBegin;
85382716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8539b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8540fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85422716604bSToby Isaac }
8543b698f381SToby Isaac 
8544b698f381SToby Isaac /*@C
8545b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8546b698f381SToby Isaac 
854720f4b53cSBarry Smith   Collective
8548d083f849SBarry Smith 
8549b698f381SToby Isaac   Input Parameters:
8550bb7acecfSBarry Smith + dm    - The `DM`
8551a4e35b19SJacob Faibussowitsch . time  - The time
8552b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8553b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8554574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8555b698f381SToby Isaac - n     - The vector to project along
8556b698f381SToby Isaac 
8557b698f381SToby Isaac   Output Parameter:
8558b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8559b698f381SToby Isaac 
8560b698f381SToby Isaac   Level: developer
8561b698f381SToby Isaac 
8562bb7acecfSBarry Smith   Developer Notes:
8563bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8564bb7acecfSBarry Smith 
8565bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8566bb7acecfSBarry Smith 
85671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8568b698f381SToby Isaac @*/
8569d71ae5a4SJacob 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)
8570d71ae5a4SJacob Faibussowitsch {
8571b698f381SToby Isaac   PetscFunctionBegin;
8572b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8573b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8574fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
85753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8576b698f381SToby Isaac }
8577b698f381SToby Isaac 
85782a16baeaSToby Isaac /*@C
85792a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
85802a16baeaSToby Isaac 
858120f4b53cSBarry Smith   Collective
8582d083f849SBarry Smith 
85832a16baeaSToby Isaac   Input Parameters:
8584bb7acecfSBarry Smith + dm    - The `DM`
85852a16baeaSToby Isaac . time  - The time
85862a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
85872a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8588574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85892a16baeaSToby Isaac 
85902a16baeaSToby Isaac   Output Parameter:
85912a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
85922a16baeaSToby Isaac 
85932a16baeaSToby Isaac   Level: developer
85942a16baeaSToby Isaac 
8595bb7acecfSBarry Smith   Developer Notes:
8596bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8597bb7acecfSBarry Smith 
8598bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8599bb7acecfSBarry Smith 
860042747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86012a16baeaSToby Isaac @*/
8602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8603d71ae5a4SJacob Faibussowitsch {
86042a16baeaSToby Isaac   PetscFunctionBegin;
86052a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86062a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8607fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86092a16baeaSToby Isaac }
86102a16baeaSToby Isaac 
8611df0b854cSToby Isaac /*@C
8612bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8613502a2867SDave May 
8614502a2867SDave May   Not Collective
8615502a2867SDave May 
8616502a2867SDave May   Input Parameter:
8617bb7acecfSBarry Smith . dm - The `DM`
8618502a2867SDave May 
86190a19bb7dSprj-   Output Parameters:
86200a19bb7dSprj- + nranks - the number of neighbours
86210a19bb7dSprj- - ranks  - the neighbors ranks
8622502a2867SDave May 
86239bdbcad8SBarry Smith   Level: beginner
86249bdbcad8SBarry Smith 
8625bb7acecfSBarry Smith   Note:
8626bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8627502a2867SDave May 
86281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8629502a2867SDave May @*/
8630d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8631d71ae5a4SJacob Faibussowitsch {
8632502a2867SDave May   PetscFunctionBegin;
8633502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8634fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8636502a2867SDave May }
8637502a2867SDave May 
8638531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8639531c7667SBarry Smith 
8640531c7667SBarry Smith /*
8641531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86422b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8643531c7667SBarry Smith */
864466976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8645d71ae5a4SJacob Faibussowitsch {
8646531c7667SBarry Smith   PetscFunctionBegin;
8647531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8648531c7667SBarry Smith     Vec x1local;
8649531c7667SBarry Smith     DM  dm;
86509566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86517a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86529566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86539566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86549566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8655531c7667SBarry Smith     x1 = x1local;
8656531c7667SBarry Smith   }
86579566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8658531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8659531c7667SBarry Smith     DM dm;
86609566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86619566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8662531c7667SBarry Smith   }
86633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8664531c7667SBarry Smith }
8665531c7667SBarry Smith 
8666531c7667SBarry Smith /*@
8667bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8668531c7667SBarry Smith 
8669a4e35b19SJacob Faibussowitsch   Input Parameters:
8670a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8671a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8672531c7667SBarry Smith 
86739bdbcad8SBarry Smith   Level: advanced
86749bdbcad8SBarry Smith 
867573ff1848SBarry Smith   Developer Note:
867673ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8677531c7667SBarry Smith 
86781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8679531c7667SBarry Smith @*/
8680d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8681d71ae5a4SJacob Faibussowitsch {
8682531c7667SBarry Smith   PetscFunctionBegin;
8683531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
86843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8685531c7667SBarry Smith }
86868320bc6fSPatrick Sanan 
86878320bc6fSPatrick Sanan /*@
8688bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
86898320bc6fSPatrick Sanan 
86908320bc6fSPatrick Sanan   Collective
86918320bc6fSPatrick Sanan 
86928320bc6fSPatrick Sanan   Input Parameters:
8693bb7acecfSBarry Smith + dm1 - the first `DM`
8694bb7acecfSBarry Smith - dm2 - the second `DM`
86958320bc6fSPatrick Sanan 
86968320bc6fSPatrick Sanan   Output Parameters:
8697bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8698bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
86998320bc6fSPatrick Sanan 
870020f4b53cSBarry Smith   Level: advanced
870120f4b53cSBarry Smith 
87028320bc6fSPatrick Sanan   Notes:
8703bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87043d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87058320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8706bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87073d862458SPatrick Sanan   decomposition, but hold different data.
87088320bc6fSPatrick Sanan 
87098320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8710bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87118320bc6fSPatrick Sanan 
8712bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87138320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87148320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8715bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87168320bc6fSPatrick Sanan 
8717bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8718bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87198320bc6fSPatrick Sanan .vb
87208320bc6fSPatrick Sanan   ...
87219566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87228320bc6fSPatrick Sanan   if (set && compatible)  {
87239566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87249566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87259566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87268320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87278320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87288320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87298320bc6fSPatrick Sanan       }
87308320bc6fSPatrick Sanan     }
87319566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87329566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87338320bc6fSPatrick Sanan   } else {
87348320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87358320bc6fSPatrick Sanan   }
87368320bc6fSPatrick Sanan   ...
87378320bc6fSPatrick Sanan .ve
87388320bc6fSPatrick Sanan 
8739bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87408320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87418320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87428320bc6fSPatrick Sanan   always check the "set" output parameter.
87438320bc6fSPatrick Sanan 
8744bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87458320bc6fSPatrick Sanan 
8746bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87478320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87488320bc6fSPatrick Sanan   incompatible.
87498320bc6fSPatrick Sanan 
87508320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8751bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87528320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87538320bc6fSPatrick Sanan 
875473ff1848SBarry Smith   Developer Note:
8755bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87563d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8757a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8758bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
87598320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
87603d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8761bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
87628320bc6fSPatrick Sanan 
87631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
87648320bc6fSPatrick Sanan @*/
8765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8766d71ae5a4SJacob Faibussowitsch {
87678320bc6fSPatrick Sanan   PetscMPIInt compareResult;
87688320bc6fSPatrick Sanan   DMType      type, type2;
87698320bc6fSPatrick Sanan   PetscBool   sameType;
87708320bc6fSPatrick Sanan 
87718320bc6fSPatrick Sanan   PetscFunctionBegin;
8772a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
87738320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
87748320bc6fSPatrick Sanan 
87758320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8776a5bc1bf3SBarry Smith   if (dm1 == dm2) {
87778320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87788320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
87793ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87808320bc6fSPatrick Sanan   }
87818320bc6fSPatrick Sanan 
87828320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
87838320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
87848320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
87858320bc6fSPatrick Sanan      determined by the implementation-specific logic */
87869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
87878320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
87888320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
87898320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
87903ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
87918320bc6fSPatrick Sanan   }
87928320bc6fSPatrick Sanan 
87938320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8794a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8795dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
87963ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
87978320bc6fSPatrick Sanan   }
87988320bc6fSPatrick Sanan 
8799a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88008320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88019566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88029566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88039566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88048320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8805dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88068320bc6fSPatrick Sanan   } else {
88078320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88088320bc6fSPatrick Sanan   }
88093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88108320bc6fSPatrick Sanan }
8811c0f0dcc3SMatthew G. Knepley 
8812c0f0dcc3SMatthew G. Knepley /*@C
8813bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8814c0f0dcc3SMatthew G. Knepley 
881520f4b53cSBarry Smith   Logically Collective
8816c0f0dcc3SMatthew G. Knepley 
8817c0f0dcc3SMatthew G. Knepley   Input Parameters:
881860225df5SJacob Faibussowitsch + dm             - the `DM`
8819c0f0dcc3SMatthew G. Knepley . f              - the monitor function
882020f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
882120f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8822c0f0dcc3SMatthew G. Knepley 
882320f4b53cSBarry Smith   Options Database Key:
882460225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8825c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8826c0f0dcc3SMatthew G. Knepley 
88279bdbcad8SBarry Smith   Level: intermediate
88289bdbcad8SBarry Smith 
8829bb7acecfSBarry Smith   Note:
8830c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8831bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8832c0f0dcc3SMatthew G. Knepley   order in which they were set.
8833c0f0dcc3SMatthew G. Knepley 
883473ff1848SBarry Smith   Fortran Note:
8835bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8836bb7acecfSBarry Smith 
883773ff1848SBarry Smith   Developer Note:
8838bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8839c0f0dcc3SMatthew G. Knepley 
88401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8841c0f0dcc3SMatthew G. Knepley @*/
8842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8843d71ae5a4SJacob Faibussowitsch {
8844c0f0dcc3SMatthew G. Knepley   PetscInt m;
8845c0f0dcc3SMatthew G. Knepley 
8846c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8847c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8848c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8849c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8850c0f0dcc3SMatthew G. Knepley 
88519566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88523ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8853c0f0dcc3SMatthew G. Knepley   }
88547a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8855c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8856c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8857c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8859c0f0dcc3SMatthew G. Knepley }
8860c0f0dcc3SMatthew G. Knepley 
8861c0f0dcc3SMatthew G. Knepley /*@
8862bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8863c0f0dcc3SMatthew G. Knepley 
886420f4b53cSBarry Smith   Logically Collective
8865c0f0dcc3SMatthew G. Knepley 
8866c0f0dcc3SMatthew G. Knepley   Input Parameter:
8867c0f0dcc3SMatthew G. Knepley . dm - the DM
8868c0f0dcc3SMatthew G. Knepley 
8869c0f0dcc3SMatthew G. Knepley   Options Database Key:
8870c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8871bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8872c0f0dcc3SMatthew G. Knepley   set via the options database
8873c0f0dcc3SMatthew G. Knepley 
88749bdbcad8SBarry Smith   Level: intermediate
88759bdbcad8SBarry Smith 
8876bb7acecfSBarry Smith   Note:
8877bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8878c0f0dcc3SMatthew G. Knepley 
88791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8880c0f0dcc3SMatthew G. Knepley @*/
8881d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8882d71ae5a4SJacob Faibussowitsch {
8883c0f0dcc3SMatthew G. Knepley   PetscInt m;
8884c0f0dcc3SMatthew G. Knepley 
8885c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8886c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8887c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
88889566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8889c0f0dcc3SMatthew G. Knepley   }
8890c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
88913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8892c0f0dcc3SMatthew G. Knepley }
8893c0f0dcc3SMatthew G. Knepley 
8894c0f0dcc3SMatthew G. Knepley /*@C
8895c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8896c0f0dcc3SMatthew G. Knepley 
889720f4b53cSBarry Smith   Collective
8898c0f0dcc3SMatthew G. Knepley 
8899c0f0dcc3SMatthew G. Knepley   Input Parameters:
8900bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8901c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8902c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8903c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8904c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8905bb7acecfSBarry 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
8906c0f0dcc3SMatthew G. Knepley 
8907c0f0dcc3SMatthew G. Knepley   Output Parameter:
8908c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8909c0f0dcc3SMatthew G. Knepley 
8910c0f0dcc3SMatthew G. Knepley   Level: developer
8911c0f0dcc3SMatthew G. Knepley 
89121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8913db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
891460225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8915db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8916c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8917db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8918bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8919c0f0dcc3SMatthew G. Knepley @*/
8920d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8921d71ae5a4SJacob Faibussowitsch {
8922c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8923c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8924c0f0dcc3SMatthew G. Knepley 
8925c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8926c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8928c0f0dcc3SMatthew G. Knepley   if (*flg) {
8929c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8930c0f0dcc3SMatthew G. Knepley 
89319566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8932cd791dc2SBarry Smith     PetscCall(PetscOptionsRestoreViewer(&viewer));
89339566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89349566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8935c0f0dcc3SMatthew G. Knepley   }
89363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8937c0f0dcc3SMatthew G. Knepley }
8938c0f0dcc3SMatthew G. Knepley 
8939c0f0dcc3SMatthew G. Knepley /*@
8940c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8941c0f0dcc3SMatthew G. Knepley 
894220f4b53cSBarry Smith   Collective
8943c0f0dcc3SMatthew G. Knepley 
89442fe279fdSBarry Smith   Input Parameter:
8945bb7acecfSBarry Smith . dm - The `DM`
8946c0f0dcc3SMatthew G. Knepley 
8947c0f0dcc3SMatthew G. Knepley   Level: developer
8948c0f0dcc3SMatthew G. Knepley 
894973ff1848SBarry Smith   Developer Note:
8950a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8951a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8952a4e35b19SJacob Faibussowitsch   concept of discretization.
8953bb7acecfSBarry Smith 
89541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8955c0f0dcc3SMatthew G. Knepley @*/
8956d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8957d71ae5a4SJacob Faibussowitsch {
8958c0f0dcc3SMatthew G. Knepley   PetscInt m;
8959c0f0dcc3SMatthew G. Knepley 
8960c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
89613ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8962c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
896348a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
89643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8965c0f0dcc3SMatthew G. Knepley }
89662e4af2aeSMatthew G. Knepley 
89672e4af2aeSMatthew G. Knepley /*@
8968bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
89692e4af2aeSMatthew G. Knepley 
897020f4b53cSBarry Smith   Collective
89712e4af2aeSMatthew G. Knepley 
89722e4af2aeSMatthew G. Knepley   Input Parameters:
8973bb7acecfSBarry Smith + dm  - The `DM`
89746b867d5aSJose E. Roman - sol - The solution vector
89752e4af2aeSMatthew G. Knepley 
89766b867d5aSJose E. Roman   Input/Output Parameter:
897720f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
89786b867d5aSJose E. Roman            contains the error in each field
89796b867d5aSJose E. Roman 
89806b867d5aSJose E. Roman   Output Parameter:
898120f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
898220f4b53cSBarry Smith 
898320f4b53cSBarry Smith   Level: developer
89842e4af2aeSMatthew G. Knepley 
8985bb7acecfSBarry Smith   Note:
8986bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
89872e4af2aeSMatthew G. Knepley 
89881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
89892e4af2aeSMatthew G. Knepley @*/
8990d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8991d71ae5a4SJacob Faibussowitsch {
89922e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
89932e4af2aeSMatthew G. Knepley   void    **ctxs;
89942e4af2aeSMatthew G. Knepley   PetscReal time;
89952e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
89962e4af2aeSMatthew G. Knepley 
89972e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
89989566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
89999566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90009566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90012e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90022e4af2aeSMatthew G. Knepley     PetscDS         ds;
90032e4af2aeSMatthew G. Knepley     DMLabel         label;
90042e4af2aeSMatthew G. Knepley     IS              fieldIS;
90052e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90062e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90072e4af2aeSMatthew G. Knepley 
900807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90099566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90109566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90112e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90122e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90139566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90142e4af2aeSMatthew G. Knepley     }
90159566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90162e4af2aeSMatthew G. Knepley   }
9017ad540459SPierre 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);
90189566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90199566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90202e4af2aeSMatthew G. Knepley   if (errorVec) {
90212e4af2aeSMatthew G. Knepley     DM             edm;
90222e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90232e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90242e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90252e4af2aeSMatthew G. Knepley 
90269566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90279566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90302e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90319566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90322e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90332e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90342e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90352e4af2aeSMatthew G. Knepley       const char     *name;
90362e4af2aeSMatthew G. Knepley 
90379566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90389566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90399566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90409566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90419566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90429566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90439566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90449566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90452e4af2aeSMatthew G. Knepley     }
90469566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90472e4af2aeSMatthew G. Knepley 
90489566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90499566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90509566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90522e4af2aeSMatthew G. Knepley   }
90539566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90552e4af2aeSMatthew G. Knepley }
90569a2a23afSMatthew G. Knepley 
90579a2a23afSMatthew G. Knepley /*@
9058bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
90599a2a23afSMatthew G. Knepley 
906020f4b53cSBarry Smith   Not Collective
90619a2a23afSMatthew G. Knepley 
90629a2a23afSMatthew G. Knepley   Input Parameter:
9063bb7acecfSBarry Smith . dm - The `DM`
90649a2a23afSMatthew G. Knepley 
90659a2a23afSMatthew G. Knepley   Output Parameter:
9066a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
90679a2a23afSMatthew G. Knepley 
90689a2a23afSMatthew G. Knepley   Level: advanced
90699a2a23afSMatthew G. Knepley 
9070e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
90719a2a23afSMatthew G. Knepley @*/
9072d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9073d71ae5a4SJacob Faibussowitsch {
90749a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90759a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90769566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
90773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90789a2a23afSMatthew G. Knepley }
90799a2a23afSMatthew G. Knepley 
90809a2a23afSMatthew G. Knepley /*@
9081ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
90829a2a23afSMatthew G. Knepley 
908320f4b53cSBarry Smith   Not Collective
90849a2a23afSMatthew G. Knepley 
90859a2a23afSMatthew G. Knepley   Input Parameters:
9086bb7acecfSBarry Smith + dm    - The `DM`
9087bb7acecfSBarry Smith . label - The `DMLabel`
9088ac17215fSMatthew G. Knepley . value - The label value indicating the region
9089ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
90909a2a23afSMatthew G. Knepley 
90919a2a23afSMatthew G. Knepley   Output Parameter:
9092bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
90939a2a23afSMatthew G. Knepley 
90949bdbcad8SBarry Smith   Level: advanced
90959bdbcad8SBarry Smith 
9096bb7acecfSBarry Smith   Note:
9097bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
909804c51a94SMatthew G. Knepley 
9099e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91009a2a23afSMatthew G. Knepley @*/
9101d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9102d71ae5a4SJacob Faibussowitsch {
9103ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
910404c51a94SMatthew G. Knepley   PetscBool       has;
91059a2a23afSMatthew G. Knepley 
91069a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91079a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91089a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91099a2a23afSMatthew G. Knepley   key.label = label;
91109a2a23afSMatthew G. Knepley   key.value = value;
9111ac17215fSMatthew G. Knepley   key.part  = part;
91129566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91139566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91149566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91169a2a23afSMatthew G. Knepley }
91179a2a23afSMatthew G. Knepley 
91189a2a23afSMatthew G. Knepley /*@
9119bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91209a2a23afSMatthew G. Knepley 
912120f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91229a2a23afSMatthew G. Knepley 
91239a2a23afSMatthew G. Knepley   Input Parameters:
9124bb7acecfSBarry Smith + dm    - The `DM`
9125bb7acecfSBarry Smith . label - The `DMLabel`
91269a2a23afSMatthew G. Knepley . value - The label value indicating the region
9127ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9128bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91299a2a23afSMatthew G. Knepley 
91309a2a23afSMatthew G. Knepley   Level: advanced
91319a2a23afSMatthew G. Knepley 
9132e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91339a2a23afSMatthew G. Knepley @*/
9134d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9135d71ae5a4SJacob Faibussowitsch {
91369a2a23afSMatthew G. Knepley   Vec             old;
91379a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91389a2a23afSMatthew G. Knepley 
91399a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91409a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91419a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91429a2a23afSMatthew G. Knepley   key.label = label;
91439a2a23afSMatthew G. Knepley   key.value = value;
9144ac17215fSMatthew G. Knepley   key.part  = part;
91459566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91469566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91479566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91489566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9149d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91519a2a23afSMatthew G. Knepley }
91529a2a23afSMatthew G. Knepley 
91539a2a23afSMatthew G. Knepley /*@C
9154bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91559a2a23afSMatthew G. Knepley 
915620f4b53cSBarry Smith   Not Collective
91579a2a23afSMatthew G. Knepley 
91589a2a23afSMatthew G. Knepley   Input Parameter:
9159bb7acecfSBarry Smith . dm - The `DM`
91609a2a23afSMatthew G. Knepley 
91619a2a23afSMatthew G. Knepley   Output Parameters:
9162bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9163bb7acecfSBarry Smith . values - The label values for each `Vec`
9164bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
91659a2a23afSMatthew G. Knepley 
91669bdbcad8SBarry Smith   Level: advanced
91679bdbcad8SBarry Smith 
9168bb7acecfSBarry Smith   Note:
9169bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
91709a2a23afSMatthew G. Knepley 
9171e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
91729a2a23afSMatthew G. Knepley @*/
9173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9174d71ae5a4SJacob Faibussowitsch {
91759a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
91769a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
91779a2a23afSMatthew G. Knepley 
91789a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91799a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91804f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
91814f572ea9SToby Isaac   PetscAssertPointer(values, 3);
91824f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
91839566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
91849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
91859566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
91869371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
91879371c9d4SSatish Balay     labels[i] = keys[i].label;
91889371c9d4SSatish Balay     values[i] = keys[i].value;
91899371c9d4SSatish Balay     parts[i]  = keys[i].part;
91909371c9d4SSatish Balay   }
91919566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
91923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91939a2a23afSMatthew G. Knepley }
91949a2a23afSMatthew G. Knepley 
91959a2a23afSMatthew G. Knepley /*@
9196bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
91979a2a23afSMatthew G. Knepley 
919820f4b53cSBarry Smith   Not Collective
91999a2a23afSMatthew G. Knepley 
92009a2a23afSMatthew G. Knepley   Input Parameter:
9201bb7acecfSBarry Smith . dm - The `DM`
92029a2a23afSMatthew G. Knepley 
92039a2a23afSMatthew G. Knepley   Output Parameter:
9204bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92059a2a23afSMatthew G. Knepley 
92069a2a23afSMatthew G. Knepley   Level: advanced
92079a2a23afSMatthew G. Knepley 
9208bb7acecfSBarry Smith   Note:
9209bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9210bb7acecfSBarry Smith 
9211e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92129a2a23afSMatthew G. Knepley @*/
9213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9214d71ae5a4SJacob Faibussowitsch {
92159a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92169a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9217d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9218d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9219e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9220e4d5475eSStefano Zampini 
9221e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92229566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9223d705d0eaSStefano Zampini   {
9224d705d0eaSStefano Zampini     Vec     *auxData;
9225d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9226d705d0eaSStefano Zampini 
9227d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9228d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9229d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9230d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9231d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9232e4d5475eSStefano Zampini   }
9233e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9234e4d5475eSStefano Zampini }
9235e4d5475eSStefano Zampini 
9236e4d5475eSStefano Zampini /*@
9237e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9238e4d5475eSStefano Zampini 
9239e4d5475eSStefano Zampini   Not Collective
9240e4d5475eSStefano Zampini 
9241e4d5475eSStefano Zampini   Input Parameter:
9242e4d5475eSStefano Zampini . dm - The `DM`
9243e4d5475eSStefano Zampini 
9244e4d5475eSStefano Zampini   Level: advanced
9245e4d5475eSStefano Zampini 
9246e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9247e4d5475eSStefano Zampini @*/
9248e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9249e4d5475eSStefano Zampini {
9250e4d5475eSStefano Zampini   Vec     *auxData;
9251e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9252e4d5475eSStefano Zampini 
9253e4d5475eSStefano Zampini   PetscFunctionBegin;
9254e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9255d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9256e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9257d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9258d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9259e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9260e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
92613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92629a2a23afSMatthew G. Knepley }
9263b5a892a1SMatthew G. Knepley 
9264b5a892a1SMatthew G. Knepley /*@C
9265bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9266b5a892a1SMatthew G. Knepley 
926720f4b53cSBarry Smith   Not Collective
9268b5a892a1SMatthew G. Knepley 
9269b5a892a1SMatthew G. Knepley   Input Parameters:
9270bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9271b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9272b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9273b5a892a1SMatthew G. Knepley 
9274b5a892a1SMatthew G. Knepley   Output Parameters:
9275bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9276b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9277b5a892a1SMatthew G. Knepley 
9278b5a892a1SMatthew G. Knepley   Level: advanced
9279b5a892a1SMatthew G. Knepley 
9280bb7acecfSBarry Smith   Note:
9281bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9282bb7acecfSBarry Smith 
928385036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9284bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9285bb7acecfSBarry Smith 
9286bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9287bb7acecfSBarry Smith 
92881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9289b5a892a1SMatthew G. Knepley @*/
9290d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9291d71ae5a4SJacob Faibussowitsch {
9292b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
929385036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9294b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9295b5a892a1SMatthew G. Knepley 
9296b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92979371c9d4SSatish Balay   if (!nO) {
92989371c9d4SSatish Balay     *ornt  = 0;
92999371c9d4SSatish Balay     *found = PETSC_TRUE;
93003ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93019371c9d4SSatish Balay   }
9302b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
930385036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9304b5a892a1SMatthew G. Knepley 
93059371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93069371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93079371c9d4SSatish Balay     if (c == cS) {
93089371c9d4SSatish Balay       *ornt = o;
93099371c9d4SSatish Balay       break;
93109371c9d4SSatish Balay     }
9311b5a892a1SMatthew G. Knepley   }
9312b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9314b5a892a1SMatthew G. Knepley }
9315b5a892a1SMatthew G. Knepley 
9316b5a892a1SMatthew G. Knepley /*@C
9317bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9318b5a892a1SMatthew G. Knepley 
931920f4b53cSBarry Smith   Not Collective
9320b5a892a1SMatthew G. Knepley 
9321b5a892a1SMatthew G. Knepley   Input Parameters:
9322bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9323b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9324b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9325b5a892a1SMatthew G. Knepley 
93262fe279fdSBarry Smith   Output Parameter:
9327bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9328b5a892a1SMatthew G. Knepley 
9329b5a892a1SMatthew G. Knepley   Level: advanced
9330b5a892a1SMatthew G. Knepley 
9331bb7acecfSBarry Smith   Note:
9332bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9333bb7acecfSBarry Smith 
933473ff1848SBarry Smith   Developer Note:
9335bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9336bb7acecfSBarry Smith 
93371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9338b5a892a1SMatthew G. Knepley @*/
9339d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9340d71ae5a4SJacob Faibussowitsch {
9341b5a892a1SMatthew G. Knepley   PetscBool found;
9342b5a892a1SMatthew G. Knepley 
9343b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93449566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93457a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9347b5a892a1SMatthew G. Knepley }
9348b5a892a1SMatthew G. Knepley 
9349b5a892a1SMatthew G. Knepley /*@C
9350bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9351b5a892a1SMatthew G. Knepley 
935220f4b53cSBarry Smith   Not Collective
9353b5a892a1SMatthew G. Knepley 
9354b5a892a1SMatthew G. Knepley   Input Parameters:
9355bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9356b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9357b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9358b5a892a1SMatthew G. Knepley 
9359b5a892a1SMatthew G. Knepley   Output Parameters:
9360bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9361b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9362b5a892a1SMatthew G. Knepley 
9363b5a892a1SMatthew G. Knepley   Level: advanced
9364b5a892a1SMatthew G. Knepley 
936573ff1848SBarry Smith   Notes:
9366bb7acecfSBarry Smith   An arrangement is a vertex order
9367bb7acecfSBarry Smith 
936885036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9369bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9370bb7acecfSBarry Smith 
9371bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9372bb7acecfSBarry Smith 
937385036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9374b5a892a1SMatthew G. Knepley @*/
9375d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9376d71ae5a4SJacob Faibussowitsch {
9377b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
937885036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9379b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9380b5a892a1SMatthew G. Knepley 
9381b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93829371c9d4SSatish Balay   if (!nO) {
93839371c9d4SSatish Balay     *ornt  = 0;
93849371c9d4SSatish Balay     *found = PETSC_TRUE;
93853ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93869371c9d4SSatish Balay   }
9387b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
938885036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9389b5a892a1SMatthew G. Knepley 
93909371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93919371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
93929371c9d4SSatish Balay     if (c == cS) {
93939371c9d4SSatish Balay       *ornt = o;
93949371c9d4SSatish Balay       break;
93959371c9d4SSatish Balay     }
9396b5a892a1SMatthew G. Knepley   }
9397b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9399b5a892a1SMatthew G. Knepley }
9400b5a892a1SMatthew G. Knepley 
9401b5a892a1SMatthew G. Knepley /*@C
9402bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9403b5a892a1SMatthew G. Knepley 
940420f4b53cSBarry Smith   Not Collective
9405b5a892a1SMatthew G. Knepley 
9406b5a892a1SMatthew G. Knepley   Input Parameters:
9407bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9408b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9409b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9410b5a892a1SMatthew G. Knepley 
94112fe279fdSBarry Smith   Output Parameter:
9412bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9413b5a892a1SMatthew G. Knepley 
9414b5a892a1SMatthew G. Knepley   Level: advanced
9415b5a892a1SMatthew G. Knepley 
9416bb7acecfSBarry Smith   Note:
9417bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9418bb7acecfSBarry Smith 
941973ff1848SBarry Smith   Developer Note:
9420bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9421bb7acecfSBarry Smith 
94221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9423b5a892a1SMatthew G. Knepley @*/
9424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9425d71ae5a4SJacob Faibussowitsch {
9426b5a892a1SMatthew G. Knepley   PetscBool found;
9427b5a892a1SMatthew G. Knepley 
9428b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94299566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94307a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9432b5a892a1SMatthew G. Knepley }
9433012bc364SMatthew G. Knepley 
9434012bc364SMatthew G. Knepley /*@C
9435012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9436012bc364SMatthew G. Knepley 
943720f4b53cSBarry Smith   Not Collective
9438012bc364SMatthew G. Knepley 
9439012bc364SMatthew G. Knepley   Input Parameters:
9440bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9441012bc364SMatthew G. Knepley - point - Coordinates of the point
9442012bc364SMatthew G. Knepley 
94432fe279fdSBarry Smith   Output Parameter:
9444012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9445012bc364SMatthew G. Knepley 
9446012bc364SMatthew G. Knepley   Level: advanced
9447012bc364SMatthew G. Knepley 
94481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9449012bc364SMatthew G. Knepley @*/
9450d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9451d71ae5a4SJacob Faibussowitsch {
9452012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9453012bc364SMatthew G. Knepley   PetscInt  d;
9454012bc364SMatthew G. Knepley 
9455012bc364SMatthew G. Knepley   PetscFunctionBegin;
9456012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9457012bc364SMatthew G. Knepley   switch (ct) {
9458012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9459012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9460012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
94619371c9d4SSatish Balay       if (point[d] < -1.0) {
94629371c9d4SSatish Balay         *inside = PETSC_FALSE;
94639371c9d4SSatish Balay         break;
94649371c9d4SSatish Balay       }
9465012bc364SMatthew G. Knepley       sum += point[d];
9466012bc364SMatthew G. Knepley     }
94679371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
94689371c9d4SSatish Balay       *inside = PETSC_FALSE;
94699371c9d4SSatish Balay       break;
94709371c9d4SSatish Balay     }
9471012bc364SMatthew G. Knepley     break;
9472012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9473012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9474012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
94759371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
94769371c9d4SSatish Balay         *inside = PETSC_FALSE;
9477012bc364SMatthew G. Knepley         break;
94789371c9d4SSatish Balay       }
94799371c9d4SSatish Balay     break;
9480d71ae5a4SJacob Faibussowitsch   default:
9481d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9482012bc364SMatthew G. Knepley   }
94833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9484012bc364SMatthew G. Knepley }
9485adc21957SMatthew G. Knepley 
9486adc21957SMatthew G. Knepley /*@
9487adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9488adc21957SMatthew G. Knepley 
9489adc21957SMatthew G. Knepley   Logically collective
9490adc21957SMatthew G. Knepley 
9491adc21957SMatthew G. Knepley   Input Parameters:
9492adc21957SMatthew G. Knepley + dm      - The DM
9493adc21957SMatthew G. Knepley - reorder - Flag for reordering
9494adc21957SMatthew G. Knepley 
9495adc21957SMatthew G. Knepley   Level: intermediate
9496adc21957SMatthew G. Knepley 
9497adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9498adc21957SMatthew G. Knepley @*/
9499adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9500adc21957SMatthew G. Knepley {
9501adc21957SMatthew G. Knepley   PetscFunctionBegin;
9502adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9503adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9504adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9505adc21957SMatthew G. Knepley }
9506adc21957SMatthew G. Knepley 
9507adc21957SMatthew G. Knepley /*@
9508adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9509adc21957SMatthew G. Knepley 
9510adc21957SMatthew G. Knepley   Not collective
9511adc21957SMatthew G. Knepley 
9512adc21957SMatthew G. Knepley   Input Parameter:
9513adc21957SMatthew G. Knepley . dm - The DM
9514adc21957SMatthew G. Knepley 
9515adc21957SMatthew G. Knepley   Output Parameter:
9516adc21957SMatthew G. Knepley . reorder - Flag for reordering
9517adc21957SMatthew G. Knepley 
9518adc21957SMatthew G. Knepley   Level: intermediate
9519adc21957SMatthew G. Knepley 
9520adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9521adc21957SMatthew G. Knepley @*/
9522adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9523adc21957SMatthew G. Knepley {
9524adc21957SMatthew G. Knepley   PetscFunctionBegin;
9525adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9526adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9527adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9528adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9529adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9530adc21957SMatthew G. Knepley }
9531adc21957SMatthew G. Knepley 
9532adc21957SMatthew G. Knepley /*@C
9533adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9534adc21957SMatthew G. Knepley 
9535adc21957SMatthew G. Knepley   Logically collective
9536adc21957SMatthew G. Knepley 
9537adc21957SMatthew G. Knepley   Input Parameters:
9538adc21957SMatthew G. Knepley + dm      - The DM
9539adc21957SMatthew G. Knepley - reorder - The reordering method
9540adc21957SMatthew G. Knepley 
9541adc21957SMatthew G. Knepley   Level: intermediate
9542adc21957SMatthew G. Knepley 
9543adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9544adc21957SMatthew G. Knepley @*/
9545adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9546adc21957SMatthew G. Knepley {
9547adc21957SMatthew G. Knepley   PetscFunctionBegin;
9548adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9549adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9550adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9551adc21957SMatthew G. Knepley }
9552adc21957SMatthew G. Knepley 
9553adc21957SMatthew G. Knepley /*@C
9554adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9555adc21957SMatthew G. Knepley 
9556adc21957SMatthew G. Knepley   Not collective
9557adc21957SMatthew G. Knepley 
9558adc21957SMatthew G. Knepley   Input Parameter:
9559adc21957SMatthew G. Knepley . dm - The DM
9560adc21957SMatthew G. Knepley 
9561adc21957SMatthew G. Knepley   Output Parameter:
9562adc21957SMatthew G. Knepley . reorder - The reordering method
9563adc21957SMatthew G. Knepley 
9564adc21957SMatthew G. Knepley   Level: intermediate
9565adc21957SMatthew G. Knepley 
9566adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9567adc21957SMatthew G. Knepley @*/
9568adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9569adc21957SMatthew G. Knepley {
9570adc21957SMatthew G. Knepley   PetscFunctionBegin;
9571adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9572adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9573adc21957SMatthew G. Knepley   *reorder = NULL;
9574adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9575adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9576adc21957SMatthew G. Knepley }
9577