xref: /petsc/src/dm/interface/dm.c (revision 648c30bcb65f74c3cbd15d7a91a7ed7c1890e25b)
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);
60377f809aSBarry Smith 
619566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
629566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
6362e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6449be4549SMatthew G. Knepley   v->setupcalled                          = PETSC_FALSE;
6549be4549SMatthew G. Knepley   v->setfromoptionscalled                 = PETSC_FALSE;
660298fd71SBarry Smith   v->ltogmap                              = NULL;
67a4ea9b21SRichard Tran Mills   v->bind_below                           = 0;
681411c6eeSJed Brown   v->bs                                   = 1;
69171400e9SBarry Smith   v->coloringtype                         = IS_COLORING_GLOBAL;
709566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
72c58f1c22SToby Isaac   v->labels                    = NULL;
7334aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7434aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
75c58f1c22SToby Isaac   v->depthLabel                = NULL;
76ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
771bb6d2a8SBarry Smith   v->localSection              = NULL;
781bb6d2a8SBarry Smith   v->globalSection             = NULL;
793b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8179769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
826858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
836858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8596173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
86435a35e8SMatthew G Knepley   {
87435a35e8SMatthew G Knepley     PetscInt i;
88435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
890298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
90f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
91435a35e8SMatthew G Knepley     }
92435a35e8SMatthew G Knepley   }
939566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
9407218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(v, NULL, NULL, ds, NULL));
959566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
969566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9714f150ffSMatthew G. Knepley   v->dmBC              = NULL;
98a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
99f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
100cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1019566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1029566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1034a7a4c06SLawrence Mitchell 
1041411c6eeSJed Brown   *dm = v;
1053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106a4121054SBarry Smith }
107a4121054SBarry Smith 
10838221697SMatthew G. Knepley /*@
109bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11038221697SMatthew G. Knepley 
111d083f849SBarry Smith   Collective
11238221697SMatthew G. Knepley 
11338221697SMatthew G. Knepley   Input Parameter:
114bb7acecfSBarry Smith . dm - The original `DM` object
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   Output Parameter:
117bb7acecfSBarry Smith . newdm - The new `DM` object
11838221697SMatthew G. Knepley 
11938221697SMatthew G. Knepley   Level: beginner
12038221697SMatthew G. Knepley 
1211cb8cacdSPatrick Sanan   Notes:
122bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
123bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
124bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1251bb6d2a8SBarry Smith 
126bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12789706ed2SPatrick Sanan 
128bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
129bb7acecfSBarry Smith 
13060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMConvert()`
13138221697SMatthew G. Knepley @*/
132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
133d71ae5a4SJacob Faibussowitsch {
13438221697SMatthew G. Knepley   PetscSF              sf;
13538221697SMatthew G. Knepley   Vec                  coords;
13638221697SMatthew G. Knepley   void                *ctx;
137ec196627SMatthew G. Knepley   MatOrderingType      otype;
138ec196627SMatthew G. Knepley   DMReorderDefaultFlag flg;
1396858538eSMatthew G. Knepley   PetscInt             dim, cdim, i;
14038221697SMatthew G. Knepley 
14138221697SMatthew G. Knepley   PetscFunctionBegin;
14238221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1434f572ea9SToby Isaac   PetscAssertPointer(newdm, 2);
1449566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1459566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
146ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
147ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
148c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
149fc214432SJed Brown   (*newdm)->prealloc_skip = dm->prealloc_skip;
1509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1529566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1539566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
156dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1573f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1589566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1599566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1609566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1619566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
162ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetDefault(dm, &flg));
163ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetDefault(*newdm, flg));
164ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionGetType(dm, &otype));
165ec196627SMatthew G. Knepley   PetscCall(DMReorderSectionSetType(*newdm, otype));
1666858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1676858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
168be4c1c3eSMatthew G. Knepley       DM           ncdm;
169be4c1c3eSMatthew G. Knepley       PetscSection cs;
1705a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
171be4c1c3eSMatthew G. Knepley 
1726858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1739566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
174712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1755a0206caSToby Isaac       if (pEndMax >= 0) {
1766858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1776858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1789566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1795e50ca95SJames Wright         if (dm->coordinates[i].dm->periodic.setup) {
1805e50ca95SJames Wright           ncdm->periodic.setup = dm->coordinates[i].dm->periodic.setup;
1815e50ca95SJames Wright           PetscCall(ncdm->periodic.setup(ncdm));
1825e50ca95SJames Wright         }
1836858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1846858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1859566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
186be4c1c3eSMatthew G. Knepley       }
187be4c1c3eSMatthew G. Knepley     }
1886858538eSMatthew G. Knepley   }
1899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
19238221697SMatthew G. Knepley   if (coords) {
1939566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
19438221697SMatthew G. Knepley   } else {
1959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1969566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
19738221697SMatthew G. Knepley   }
1986858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1996858538eSMatthew G. Knepley   if (coords) {
2006858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
2016858538eSMatthew G. Knepley   } else {
2026858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
2036858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
2046858538eSMatthew G. Knepley   }
20590b157c4SStefano Zampini   {
2064fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
2076858538eSMatthew G. Knepley 
2084fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2094fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
210c6b900c6SMatthew G. Knepley   }
21134aa8a36SMatthew G. Knepley   {
21234aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
21334aa8a36SMatthew G. Knepley 
2149566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2159566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
21634aa8a36SMatthew G. Knepley   }
2173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21838221697SMatthew G. Knepley }
21938221697SMatthew G. Knepley 
2205d83a8b1SBarry Smith /*@
2215d83a8b1SBarry Smith   DMSetVecType - Sets the type of vector to be created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2229a42bb27SBarry Smith 
22320f4b53cSBarry Smith   Logically Collective
2249a42bb27SBarry Smith 
225147403d9SBarry Smith   Input Parameters:
22632546409SMatthew G. Knepley + dm    - initial distributed array
227bb7acecfSBarry Smith - ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2289a42bb27SBarry Smith 
22920f4b53cSBarry Smith   Options Database Key:
230147403d9SBarry Smith . -dm_vec_type ctype - the type of vector to create
2319a42bb27SBarry Smith 
2329a42bb27SBarry Smith   Level: intermediate
2339a42bb27SBarry Smith 
23460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
235bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2369a42bb27SBarry Smith @*/
23732546409SMatthew G. Knepley PetscErrorCode DMSetVecType(DM dm, VecType ctype)
238d71ae5a4SJacob Faibussowitsch {
23932546409SMatthew G. Knepley   char *tmp;
24032546409SMatthew G. Knepley 
2419a42bb27SBarry Smith   PetscFunctionBegin;
24232546409SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24332546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
24432546409SMatthew G. Knepley   tmp = (char *)dm->vectype;
24532546409SMatthew G. Knepley   PetscCall(PetscStrallocpy(ctype, (char **)&dm->vectype));
24632546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
2473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2489a42bb27SBarry Smith }
2499a42bb27SBarry Smith 
2505d83a8b1SBarry Smith /*@
251bb7acecfSBarry Smith   DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
252c0dedaeaSBarry Smith 
25320f4b53cSBarry Smith   Logically Collective
254c0dedaeaSBarry Smith 
255c0dedaeaSBarry Smith   Input Parameter:
256c0dedaeaSBarry Smith . da - initial distributed array
257c0dedaeaSBarry Smith 
258c0dedaeaSBarry Smith   Output Parameter:
259c0dedaeaSBarry Smith . ctype - the vector type
260c0dedaeaSBarry Smith 
261c0dedaeaSBarry Smith   Level: intermediate
262c0dedaeaSBarry Smith 
26360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMDestroy()`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
264c0dedaeaSBarry Smith @*/
265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
266d71ae5a4SJacob Faibussowitsch {
267c0dedaeaSBarry Smith   PetscFunctionBegin;
268c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
269c0dedaeaSBarry Smith   *ctype = da->vectype;
2703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
271c0dedaeaSBarry Smith }
272c0dedaeaSBarry Smith 
2735f1ad066SMatthew G Knepley /*@
274bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2755f1ad066SMatthew G Knepley 
27620f4b53cSBarry Smith   Not Collective
2775f1ad066SMatthew G Knepley 
2785f1ad066SMatthew G Knepley   Input Parameter:
279bb7acecfSBarry Smith . v - The `Vec`
2805f1ad066SMatthew G Knepley 
2815f1ad066SMatthew G Knepley   Output Parameter:
282bb7acecfSBarry Smith . dm - The `DM`
2835f1ad066SMatthew G Knepley 
2845f1ad066SMatthew G Knepley   Level: intermediate
2855f1ad066SMatthew G Knepley 
286bb7acecfSBarry Smith   Note:
287bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
288bb7acecfSBarry Smith 
2891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2905f1ad066SMatthew G Knepley @*/
291d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
292d71ae5a4SJacob Faibussowitsch {
2935f1ad066SMatthew G Knepley   PetscFunctionBegin;
2945f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2954f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
2969566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2985f1ad066SMatthew G Knepley }
2995f1ad066SMatthew G Knepley 
3005f1ad066SMatthew G Knepley /*@
301bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
3025f1ad066SMatthew G Knepley 
30320f4b53cSBarry Smith   Not Collective
3045f1ad066SMatthew G Knepley 
3055f1ad066SMatthew G Knepley   Input Parameters:
306bb7acecfSBarry Smith + v  - The `Vec`
307bb7acecfSBarry Smith - dm - The `DM`
3085f1ad066SMatthew G Knepley 
30920f4b53cSBarry Smith   Level: developer
31020f4b53cSBarry Smith 
31173ff1848SBarry Smith   Notes:
312bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
313d9805387SMatthew G. Knepley 
314bb7acecfSBarry 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.
315bb7acecfSBarry Smith 
3161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3175f1ad066SMatthew G Knepley @*/
318d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
319d71ae5a4SJacob Faibussowitsch {
3205f1ad066SMatthew G Knepley   PetscFunctionBegin;
3215f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
322d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3239566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3255f1ad066SMatthew G Knepley }
3265f1ad066SMatthew G Knepley 
327cc4c1da9SBarry Smith /*@
328bb7acecfSBarry Smith   DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3298f1509bcSBarry Smith 
33020f4b53cSBarry Smith   Logically Collective
3318f1509bcSBarry Smith 
3328f1509bcSBarry Smith   Input Parameters:
333bb7acecfSBarry Smith + dm    - the `DM` context
3348f1509bcSBarry Smith - ctype - the matrix type
3358f1509bcSBarry Smith 
33620f4b53cSBarry Smith   Options Database Key:
3378f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3388f1509bcSBarry Smith 
3398f1509bcSBarry Smith   Level: intermediate
3408f1509bcSBarry Smith 
3411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
342bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3438f1509bcSBarry Smith @*/
344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
345d71ae5a4SJacob Faibussowitsch {
3468f1509bcSBarry Smith   PetscFunctionBegin;
3478f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3488f1509bcSBarry Smith   dm->coloringtype = ctype;
3493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3508f1509bcSBarry Smith }
3518f1509bcSBarry Smith 
352cc4c1da9SBarry Smith /*@
353bb7acecfSBarry Smith   DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
354521d9a4cSLisandro Dalcin 
35520f4b53cSBarry Smith   Logically Collective
356521d9a4cSLisandro Dalcin 
357521d9a4cSLisandro Dalcin   Input Parameter:
358bb7acecfSBarry Smith . dm - the `DM` context
3598f1509bcSBarry Smith 
3608f1509bcSBarry Smith   Output Parameter:
3618f1509bcSBarry Smith . ctype - the matrix type
3628f1509bcSBarry Smith 
36320f4b53cSBarry Smith   Options Database Key:
3648f1509bcSBarry Smith . -dm_is_coloring_type - global or local
3658f1509bcSBarry Smith 
3668f1509bcSBarry Smith   Level: intermediate
3678f1509bcSBarry Smith 
3681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
36942747ad1SJacob Faibussowitsch           `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3708f1509bcSBarry Smith @*/
371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
372d71ae5a4SJacob Faibussowitsch {
3738f1509bcSBarry Smith   PetscFunctionBegin;
3748f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3758f1509bcSBarry Smith   *ctype = dm->coloringtype;
3763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3778f1509bcSBarry Smith }
3788f1509bcSBarry Smith 
379cc4c1da9SBarry Smith /*@
380bb7acecfSBarry Smith   DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3818f1509bcSBarry Smith 
38220f4b53cSBarry Smith   Logically Collective
3838f1509bcSBarry Smith 
3848f1509bcSBarry Smith   Input Parameters:
385bb7acecfSBarry Smith + dm    - the `DM` context
386bb7acecfSBarry Smith - ctype - the matrix type, for example `MATMPIAIJ`
387521d9a4cSLisandro Dalcin 
38820f4b53cSBarry Smith   Options Database Key:
389bb7acecfSBarry Smith . -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
390521d9a4cSLisandro Dalcin 
391521d9a4cSLisandro Dalcin   Level: intermediate
392521d9a4cSLisandro Dalcin 
39342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
394521d9a4cSLisandro Dalcin @*/
395d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
396d71ae5a4SJacob Faibussowitsch {
39732546409SMatthew G. Knepley   char *tmp;
39832546409SMatthew G. Knepley 
399521d9a4cSLisandro Dalcin   PetscFunctionBegin;
400521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40132546409SMatthew G. Knepley   PetscAssertPointer(ctype, 2);
40232546409SMatthew G. Knepley   tmp = (char *)dm->mattype;
4039566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
40432546409SMatthew G. Knepley   PetscCall(PetscFree(tmp));
4053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
406521d9a4cSLisandro Dalcin }
407521d9a4cSLisandro Dalcin 
408cc4c1da9SBarry Smith /*@
40920f4b53cSBarry Smith   DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
410c0dedaeaSBarry Smith 
41120f4b53cSBarry Smith   Logically Collective
412c0dedaeaSBarry Smith 
413c0dedaeaSBarry Smith   Input Parameter:
414bb7acecfSBarry Smith . dm - the `DM` context
415c0dedaeaSBarry Smith 
416c0dedaeaSBarry Smith   Output Parameter:
417c0dedaeaSBarry Smith . ctype - the matrix type
418c0dedaeaSBarry Smith 
419c0dedaeaSBarry Smith   Level: intermediate
420c0dedaeaSBarry Smith 
42142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`
422c0dedaeaSBarry Smith @*/
423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
424d71ae5a4SJacob Faibussowitsch {
425c0dedaeaSBarry Smith   PetscFunctionBegin;
426c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
427c0dedaeaSBarry Smith   *ctype = dm->mattype;
4283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
429c0dedaeaSBarry Smith }
430c0dedaeaSBarry Smith 
431c688c046SMatthew G Knepley /*@
432bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
433c688c046SMatthew G Knepley 
43420f4b53cSBarry Smith   Not Collective
435c688c046SMatthew G Knepley 
436c688c046SMatthew G Knepley   Input Parameter:
437bb7acecfSBarry Smith . A - The `Mat`
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   Output Parameter:
440bb7acecfSBarry Smith . dm - The `DM`
441c688c046SMatthew G Knepley 
442c688c046SMatthew G Knepley   Level: intermediate
443c688c046SMatthew G Knepley 
444bb7acecfSBarry Smith   Note:
445bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
446bb7acecfSBarry Smith 
44773ff1848SBarry Smith   Developer Note:
448bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4498f1509bcSBarry Smith 
4501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
451c688c046SMatthew G Knepley @*/
452d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
453d71ae5a4SJacob Faibussowitsch {
454c688c046SMatthew G Knepley   PetscFunctionBegin;
455c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4564f572ea9SToby Isaac   PetscAssertPointer(dm, 2);
4579566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
4583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
459c688c046SMatthew G Knepley }
460c688c046SMatthew G Knepley 
461c688c046SMatthew G Knepley /*@
462bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
463c688c046SMatthew G Knepley 
46420f4b53cSBarry Smith   Not Collective
465c688c046SMatthew G Knepley 
466c688c046SMatthew G Knepley   Input Parameters:
46720f4b53cSBarry Smith + A  - The `Mat`
46820f4b53cSBarry Smith - dm - The `DM`
469c688c046SMatthew G Knepley 
470bb7acecfSBarry Smith   Level: developer
471c688c046SMatthew G Knepley 
472bb7acecfSBarry Smith   Note:
473bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
474bb7acecfSBarry Smith 
47573ff1848SBarry Smith   Developer Note:
476bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
477bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4788f1509bcSBarry Smith 
4791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
480c688c046SMatthew G Knepley @*/
481d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
482d71ae5a4SJacob Faibussowitsch {
483c688c046SMatthew G Knepley   PetscFunctionBegin;
484c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4858865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4869566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
4873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488c688c046SMatthew G Knepley }
489c688c046SMatthew G Knepley 
490cc4c1da9SBarry Smith /*@
491bb7acecfSBarry Smith   DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4929a42bb27SBarry Smith 
49320f4b53cSBarry Smith   Logically Collective
4949a42bb27SBarry Smith 
495d8d19677SJose E. Roman   Input Parameters:
49660225df5SJacob Faibussowitsch + dm     - the `DM` context
497bb7acecfSBarry Smith - prefix - the prefix to prepend
4989a42bb27SBarry Smith 
49920f4b53cSBarry Smith   Level: advanced
50020f4b53cSBarry Smith 
50120f4b53cSBarry Smith   Note:
5029a42bb27SBarry Smith   A hyphen (-) must NOT be given at the beginning of the prefix name.
5039a42bb27SBarry Smith   The first character of all runtime options is AUTOMATICALLY the hyphen.
5049a42bb27SBarry Smith 
5051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
5069a42bb27SBarry Smith @*/
507d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
508d71ae5a4SJacob Faibussowitsch {
5099a42bb27SBarry Smith   PetscFunctionBegin;
5109a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
5121baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
5131baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
5143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5159a42bb27SBarry Smith }
5169a42bb27SBarry Smith 
517cc4c1da9SBarry Smith /*@
518da81f932SPierre Jolivet   DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
519bb7acecfSBarry Smith   `DM` options in the options database.
52031697293SDave May 
52120f4b53cSBarry Smith   Logically Collective
52231697293SDave May 
52331697293SDave May   Input Parameters:
524bb7acecfSBarry Smith + dm     - the `DM` context
525bb7acecfSBarry Smith - prefix - the string to append to the current prefix
52631697293SDave May 
52720f4b53cSBarry Smith   Level: advanced
52820f4b53cSBarry Smith 
52920f4b53cSBarry Smith   Note:
530bb7acecfSBarry 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.
53131697293SDave May   A hyphen (-) must NOT be given at the beginning of the prefix name.
53231697293SDave May   The first character of all runtime options is AUTOMATICALLY the hyphen.
53331697293SDave May 
5341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
53531697293SDave May @*/
536d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
537d71ae5a4SJacob Faibussowitsch {
53831697293SDave May   PetscFunctionBegin;
53931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5409566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
5413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54231697293SDave May }
54331697293SDave May 
544cc4c1da9SBarry Smith /*@
54531697293SDave May   DMGetOptionsPrefix - Gets the prefix used for searching for all
546bb7acecfSBarry Smith   DM options in the options database.
54731697293SDave May 
54831697293SDave May   Not Collective
54931697293SDave May 
5502fe279fdSBarry Smith   Input Parameter:
551bb7acecfSBarry Smith . dm - the `DM` context
55231697293SDave May 
5532fe279fdSBarry Smith   Output Parameter:
55431697293SDave May . prefix - pointer to the prefix string used is returned
55531697293SDave May 
55631697293SDave May   Level: advanced
55731697293SDave May 
55873ff1848SBarry Smith   Fortran Note:
55920f4b53cSBarry Smith   Pass in a string 'prefix' of
56020f4b53cSBarry Smith   sufficient length to hold the prefix.
56120f4b53cSBarry Smith 
5621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
56331697293SDave May @*/
564d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
565d71ae5a4SJacob Faibussowitsch {
56631697293SDave May   PetscFunctionBegin;
56731697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5689566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
5693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57031697293SDave May }
57131697293SDave May 
57262e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
573d71ae5a4SJacob Faibussowitsch {
5746eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
57588bdff64SToby Isaac 
57688bdff64SToby Isaac   PetscFunctionBegin;
577aab5bcd8SJed Brown   *ncrefct = 0;
57888bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
57988bdff64SToby Isaac     refct--;
58088bdff64SToby Isaac     if (recurseCoarse) {
58188bdff64SToby Isaac       PetscInt coarseCount;
58288bdff64SToby Isaac 
58362e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
58488bdff64SToby Isaac       refct += coarseCount;
58588bdff64SToby Isaac     }
58688bdff64SToby Isaac   }
58788bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
58888bdff64SToby Isaac     refct--;
58988bdff64SToby Isaac     if (recurseFine) {
59088bdff64SToby Isaac       PetscInt fineCount;
59188bdff64SToby Isaac 
59262e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
59388bdff64SToby Isaac       refct += fineCount;
59488bdff64SToby Isaac     }
59588bdff64SToby Isaac   }
59688bdff64SToby Isaac   *ncrefct = refct;
5973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59888bdff64SToby Isaac }
59988bdff64SToby Isaac 
60062e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
60162e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
60262e5d2d2SJDBetteridge {
60362e5d2d2SJDBetteridge   PetscFunctionBegin;
60462e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
6053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60662e5d2d2SJDBetteridge }
60762e5d2d2SJDBetteridge 
608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
609d71ae5a4SJacob Faibussowitsch {
6105d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
611354557abSToby Isaac 
612354557abSToby Isaac   PetscFunctionBegin;
613354557abSToby Isaac   /* destroy the labels */
614354557abSToby Isaac   while (next) {
615354557abSToby Isaac     DMLabelLink tmp = next->next;
616354557abSToby Isaac 
6175d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
618ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6199566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6209566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
621354557abSToby Isaac     next = tmp;
622354557abSToby Isaac   }
6235d80c0bfSVaclav Hapla   dm->labels = NULL;
6243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
625354557abSToby Isaac }
626354557abSToby Isaac 
62766976f2fSJacob Faibussowitsch static PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
628d71ae5a4SJacob Faibussowitsch {
6296858538eSMatthew G. Knepley   PetscFunctionBegin;
6306858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6316858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6326858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6336858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6346858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6366858538eSMatthew G. Knepley }
6376858538eSMatthew G. Knepley 
6380764c050SBarry Smith /*@
639bb7acecfSBarry Smith   DMDestroy - Destroys a `DM`.
64047c6ae99SBarry Smith 
64120f4b53cSBarry Smith   Collective
64247c6ae99SBarry Smith 
64347c6ae99SBarry Smith   Input Parameter:
644bb7acecfSBarry Smith . dm - the `DM` object to destroy
64547c6ae99SBarry Smith 
64647c6ae99SBarry Smith   Level: developer
64747c6ae99SBarry Smith 
6481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
64947c6ae99SBarry Smith @*/
650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
651d71ae5a4SJacob Faibussowitsch {
6526eb26441SStefano Zampini   PetscInt cnt;
65347c6ae99SBarry Smith 
65447c6ae99SBarry Smith   PetscFunctionBegin;
6553ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
656f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*dm, DM_CLASSID, 1);
65787e657c6SBarry Smith 
65888bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
65962e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
660f4f49eeaSPierre Jolivet   --((PetscObject)*dm)->refct;
6619371c9d4SSatish Balay   if (--cnt > 0) {
6629371c9d4SSatish Balay     *dm = NULL;
6633ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6649371c9d4SSatish Balay   }
665f4f49eeaSPierre Jolivet   if (((PetscObject)*dm)->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
666f4f49eeaSPierre Jolivet   ((PetscObject)*dm)->refct = 0;
6676eb26441SStefano Zampini 
6689566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6699566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
670974ca4ecSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(*dm));
671974ca4ecSStefano Zampini   PetscCall(DMClearNamedLocalVectors(*dm));
6722348bcf4SPeter Brune 
673b17ce1afSJed Brown   /* Destroy the list of hooks */
674c833c3b5SJed Brown   {
675c833c3b5SJed Brown     DMCoarsenHookLink link, next;
676b17ce1afSJed Brown     for (link = (*dm)->coarsenhook; link; link = next) {
677b17ce1afSJed Brown       next = link->next;
6789566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
679b17ce1afSJed Brown     }
6800298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
681c833c3b5SJed Brown   }
682c833c3b5SJed Brown   {
683c833c3b5SJed Brown     DMRefineHookLink link, next;
684c833c3b5SJed Brown     for (link = (*dm)->refinehook; link; link = next) {
685c833c3b5SJed Brown       next = link->next;
6869566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
687c833c3b5SJed Brown     }
6880298fd71SBarry Smith     (*dm)->refinehook = NULL;
689c833c3b5SJed Brown   }
690be081cd6SPeter Brune   {
691be081cd6SPeter Brune     DMSubDomainHookLink link, next;
692be081cd6SPeter Brune     for (link = (*dm)->subdomainhook; link; link = next) {
693be081cd6SPeter Brune       next = link->next;
6949566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
695be081cd6SPeter Brune     }
6960298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
697be081cd6SPeter Brune   }
698baf369e7SPeter Brune   {
699baf369e7SPeter Brune     DMGlobalToLocalHookLink link, next;
700baf369e7SPeter Brune     for (link = (*dm)->gtolhook; link; link = next) {
701baf369e7SPeter Brune       next = link->next;
7029566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
703baf369e7SPeter Brune     }
7040298fd71SBarry Smith     (*dm)->gtolhook = NULL;
705baf369e7SPeter Brune   }
706d4d07f1eSToby Isaac   {
707d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link, next;
708d4d07f1eSToby Isaac     for (link = (*dm)->ltoghook; link; link = next) {
709d4d07f1eSToby Isaac       next = link->next;
7109566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
711d4d07f1eSToby Isaac     }
712d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
713d4d07f1eSToby Isaac   }
714aa1993deSMatthew G Knepley   /* Destroy the work arrays */
715aa1993deSMatthew G Knepley   {
716aa1993deSMatthew G Knepley     DMWorkLink link, next;
717936381afSPierre 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);
718aa1993deSMatthew G Knepley     for (link = (*dm)->workin; link; link = next) {
719aa1993deSMatthew G Knepley       next = link->next;
7209566063dSJacob Faibussowitsch       PetscCall(PetscFree(link->mem));
7219566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
722aa1993deSMatthew G Knepley     }
7230298fd71SBarry Smith     (*dm)->workin = NULL;
724aa1993deSMatthew G Knepley   }
725c58f1c22SToby Isaac   /* destroy the labels */
7269566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(*dm));
727f4cdcedcSVaclav Hapla   /* destroy the fields */
7289566063dSJacob Faibussowitsch   PetscCall(DMClearFields(*dm));
729f4cdcedcSVaclav Hapla   /* destroy the boundaries */
730e6f8dbb6SToby Isaac   {
731e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
732e6f8dbb6SToby Isaac     while (next) {
733e6f8dbb6SToby Isaac       DMBoundary b = next;
734e6f8dbb6SToby Isaac 
735e6f8dbb6SToby Isaac       next = b->next;
7369566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
737e6f8dbb6SToby Isaac     }
738e6f8dbb6SToby Isaac   }
739b17ce1afSJed Brown 
7409566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmksp));
7419566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmsnes));
7429566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&(*dm)->dmts));
74352536dc3SBarry Smith 
74448a46eb9SPierre Jolivet   if ((*dm)->ctx && (*dm)->ctxdestroy) PetscCall((*(*dm)->ctxdestroy)(&(*dm)->ctx));
7459566063dSJacob Faibussowitsch   PetscCall(MatFDColoringDestroy(&(*dm)->fd));
7469566063dSJacob Faibussowitsch   PetscCall(ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap));
7479566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->vectype));
7489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->mattype));
74988ed4aceSMatthew G Knepley 
7509566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->localSection));
7519566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->globalSection));
752adc21957SMatthew G. Knepley   PetscCall(PetscFree((*dm)->reorderSectionType));
7539566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7549566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7559566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7579566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
75848a46eb9SPierre Jolivet   if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7599566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
760e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(*dm));
7619566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
76248a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7636eb26441SStefano Zampini 
7649566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
76548a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7674fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7689566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7699566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7706858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7716858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7729566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7739566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
775ddedc8f6SJames Wright   for (PetscInt i = 0; i < (*dm)->periodic.num_affines; i++) {
776ddedc8f6SJames Wright     PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local[i]));
777ddedc8f6SJames Wright     PetscCall(VecDestroy(&(*dm)->periodic.affine[i]));
778ddedc8f6SJames Wright   }
779ddedc8f6SJames Wright   if ((*dm)->periodic.num_affines > 0) PetscCall(PetscFree2((*dm)->periodic.affine_to_local, (*dm)->periodic.affine));
7806636e97aSMatthew G Knepley 
7819566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7829566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
783e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
785732e2eb9SMatthew G Knepley 
786213acdd3SPierre Jolivet   PetscTryTypeMethod(*dm, destroy);
7879566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
788d2b2dc1eSMatthew G. Knepley   PetscCall(DMCeedDestroy(&(*dm)->dmceed));
789f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7909566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7919566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
792f918ec44SMatthew G. Knepley #endif
793435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
7949566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
7953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79647c6ae99SBarry Smith }
79747c6ae99SBarry Smith 
798d7bf68aeSBarry Smith /*@
799bb7acecfSBarry Smith   DMSetUp - sets up the data structures inside a `DM` object
800d7bf68aeSBarry Smith 
80120f4b53cSBarry Smith   Collective
802d7bf68aeSBarry Smith 
803d7bf68aeSBarry Smith   Input Parameter:
804bb7acecfSBarry Smith . dm - the `DM` object to setup
805d7bf68aeSBarry Smith 
806bb7acecfSBarry Smith   Level: intermediate
807d7bf68aeSBarry Smith 
808bb7acecfSBarry Smith   Note:
809bb7acecfSBarry Smith   This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
810bb7acecfSBarry Smith 
8111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
812d7bf68aeSBarry Smith @*/
813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
814d71ae5a4SJacob Faibussowitsch {
815d7bf68aeSBarry Smith   PetscFunctionBegin;
816171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8173ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
818dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8198387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
821d7bf68aeSBarry Smith }
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith /*@
824bb7acecfSBarry Smith   DMSetFromOptions - sets parameters in a `DM` from the options database
825d7bf68aeSBarry Smith 
82620f4b53cSBarry Smith   Collective
827d7bf68aeSBarry Smith 
828d7bf68aeSBarry Smith   Input Parameter:
829bb7acecfSBarry Smith . dm - the `DM` object to set options for
830d7bf68aeSBarry Smith 
83120f4b53cSBarry Smith   Options Database Keys:
832bb7acecfSBarry Smith + -dm_preallocate_only                               - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
833bb7acecfSBarry Smith . -dm_vec_type <type>                                - type of vector to create inside `DM`
834bb7acecfSBarry Smith . -dm_mat_type <type>                                - type of matrix to create inside `DM`
835a4ea9b21SRichard Tran Mills . -dm_is_coloring_type                               - <global or local>
83620f4b53cSBarry 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`
83720f4b53cSBarry Smith . -dm_plex_filename <str>                            - File containing a mesh
8389318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>                   - File containing a mesh boundary
839cd7e8a5eSksagiyam . -dm_plex_name <str>                                - Name of the mesh in the file
8405dca41c3SJed Brown . -dm_plex_shape <shape>                             - The domain shape, such as `BOX`, `SPHERE`, etc.
8419318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                                 - Cell shape
8429318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool>              - Use a reference cell domain
8439318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                                 - Set the topological dimension
844bb7acecfSBarry Smith . -dm_plex_simplex <bool>                            - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
845bb7acecfSBarry Smith . -dm_plex_interpolate <bool>                        - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8469318fe57SMatthew G. Knepley . -dm_plex_scale <sc>                                - Scale factor for mesh coordinates
847be664eb1SMatthew G. Knepley . -dm_coord_remap <bool>                             - Map coordinates using a function
848be664eb1SMatthew G. Knepley . -dm_coord_map <mapname>                            - Select a builtin coordinate map
849be664eb1SMatthew G. Knepley . -dm_coord_map_params <p0,p1,p2,...>                - Set coordinate mapping parameters
8509318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>                         - Number of faces along each dimension
8519318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>                         - Specify lower-left-bottom coordinates for the box
8529318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>                         - Specify upper-right-top coordinates for the box
853bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>                         - Specify the `DMBoundaryType` for each direction
8549318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>                         - The sphere radius
8559318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>                           - Radius of the ball
8569318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>                          - Boundary type in the z direction
8579318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>                   - Number of wedges around the cylinder
858bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>                           - Reorder the mesh using the specified algorithm
8599318fe57SMatthew G. Knepley . -dm_refine_pre <n>                                 - The number of refinements before distribution
8609318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>                      - Flag for uniform refinement before distribution
8619318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>                    - The maximum cell volume after refinement before distribution
8629318fe57SMatthew G. Knepley . -dm_refine <n>                                     - The number of refinements after distribution
863bdf63967SMatthew G. Knepley . -dm_extrude <l>                                    - Activate extrusion and specify the number of layers to extrude
864d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
865d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
866d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
869909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells                     - Flag to create finite volume ghost cells on the boundary
870909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name>               - Label name for ghost cells boundary
8719318fe57SMatthew G. Knepley . -dm_distribute <bool>                              - Flag to redistribute a mesh among processes
8729318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>                         - The size of the overlap halo
8739318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>                           - Set adjacency direction
87420f4b53cSBarry Smith . -dm_plex_adj_closure <bool>                        - Set adjacency size
875d2b2dc1eSMatthew G. Knepley . -dm_plex_use_ceed <bool>                           - Use LibCEED as the FEM backend
87620f4b53cSBarry Smith . -dm_plex_check_symmetry                            - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
877bb7acecfSBarry Smith . -dm_plex_check_skeleton                            - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
878bb7acecfSBarry 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()`
879bb7acecfSBarry Smith . -dm_plex_check_geometry                            - Check that cells have positive volume - `DMPlexCheckGeometry()`
880bb7acecfSBarry Smith . -dm_plex_check_pointsf                             - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
881bb7acecfSBarry Smith . -dm_plex_check_interface_cones                     - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
882384a6580SVaclav Hapla - -dm_plex_check_all                                 - Perform all the checks above
883d7bf68aeSBarry Smith 
88495eb5ee5SVaclav Hapla   Level: intermediate
88595eb5ee5SVaclav Hapla 
8861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
887bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
88860225df5SJacob Faibussowitsch          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`
889d7bf68aeSBarry Smith @*/
890d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
891d71ae5a4SJacob Faibussowitsch {
8927781c08eSBarry Smith   char      typeName[256];
893ca266f36SBarry Smith   PetscBool flg;
894d7bf68aeSBarry Smith 
895d7bf68aeSBarry Smith   PetscFunctionBegin;
896171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89749be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
8989566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
8999566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
900dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
901d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9041baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9061baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
907863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9099566063dSJacob 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));
910eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
911dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
912f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
913dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
914d0609cedSBarry Smith   PetscOptionsEnd();
9153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
916d7bf68aeSBarry Smith }
917d7bf68aeSBarry Smith 
918ffeef943SBarry Smith /*@
919bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
920fe2efc57SMark 
92120f4b53cSBarry Smith   Collective
922fe2efc57SMark 
923fe2efc57SMark   Input Parameters:
924bb7acecfSBarry Smith + dm   - the `DM` object
92520f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
92660225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
927fe2efc57SMark 
928fe2efc57SMark   Level: intermediate
929bb7acecfSBarry Smith 
930bb7acecfSBarry Smith   Note:
931bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
932bb7acecfSBarry Smith 
93360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
934fe2efc57SMark @*/
935d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
936d71ae5a4SJacob Faibussowitsch {
937fe2efc57SMark   PetscFunctionBegin;
938fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9399566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
941fe2efc57SMark }
942fe2efc57SMark 
943ffeef943SBarry Smith /*@
944bb7acecfSBarry 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
945bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94647c6ae99SBarry Smith 
94720f4b53cSBarry Smith   Collective
94847c6ae99SBarry Smith 
949d8d19677SJose E. Roman   Input Parameters:
950bb7acecfSBarry Smith + dm - the `DM` object to view
95147c6ae99SBarry Smith - v  - the viewer
95247c6ae99SBarry Smith 
95320f4b53cSBarry Smith   Level: beginner
95420f4b53cSBarry Smith 
95573ff1848SBarry Smith   Note:
956bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
957bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
958bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
959cd7e8a5eSksagiyam 
9601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96147c6ae99SBarry Smith @*/
962d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
963d71ae5a4SJacob Faibussowitsch {
96432c0f0efSBarry Smith   PetscBool         isbinary;
96576a8abe0SBarry Smith   PetscMPIInt       size;
96676a8abe0SBarry Smith   PetscViewerFormat format;
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith   PetscFunctionBegin;
969171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97048a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
971b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
97874903a4fSStefano Zampini      in an error here */
97974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9809566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
981b1b135c8SBarry Smith 
9829566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9843ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9859566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9869566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98732c0f0efSBarry Smith   if (isbinary) {
98855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
98932c0f0efSBarry Smith     char     type[256];
99032c0f0efSBarry Smith 
9919566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
992c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9939566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99432c0f0efSBarry Smith   }
995dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99747c6ae99SBarry Smith }
99847c6ae99SBarry Smith 
99947c6ae99SBarry Smith /*@
1000bb7acecfSBarry 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,
1001bb7acecfSBarry Smith   that is it has no ghost locations.
100247c6ae99SBarry Smith 
100320f4b53cSBarry Smith   Collective
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith   Input Parameter:
1006bb7acecfSBarry Smith . dm - the `DM` object
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith   Output Parameter:
100947c6ae99SBarry Smith . vec - the global vector
101047c6ae99SBarry Smith 
1011073dac72SJed Brown   Level: beginner
101247c6ae99SBarry Smith 
10131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1014bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101547c6ae99SBarry Smith @*/
1016d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1017d71ae5a4SJacob Faibussowitsch {
101847c6ae99SBarry Smith   PetscFunctionBegin;
1019171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10204f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1021dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1023c6b011d8SStefano Zampini     DM vdm;
1024c6b011d8SStefano Zampini 
10259566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10267a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1027c6b011d8SStefano Zampini   }
10283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102947c6ae99SBarry Smith }
103047c6ae99SBarry Smith 
103147c6ae99SBarry Smith /*@
1032bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith   Not Collective
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith   Input Parameter:
1037bb7acecfSBarry Smith . dm - the `DM` object
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith   Output Parameter:
104047c6ae99SBarry Smith . vec - the local vector
104147c6ae99SBarry Smith 
1042073dac72SJed Brown   Level: beginner
104347c6ae99SBarry Smith 
104420f4b53cSBarry Smith   Note:
1045bb7acecfSBarry 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.
1046bb7acecfSBarry Smith 
10471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1048bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
104947c6ae99SBarry Smith @*/
1050d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1051d71ae5a4SJacob Faibussowitsch {
105247c6ae99SBarry Smith   PetscFunctionBegin;
1053171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10544f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1055dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1057c6b011d8SStefano Zampini     DM vdm;
1058c6b011d8SStefano Zampini 
10599566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10607a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1061c6b011d8SStefano Zampini   }
10623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106347c6ae99SBarry Smith }
106447c6ae99SBarry Smith 
10651411c6eeSJed Brown /*@
1066bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10671411c6eeSJed Brown 
106820f4b53cSBarry Smith   Collective
10691411c6eeSJed Brown 
10701411c6eeSJed Brown   Input Parameter:
1071bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10721411c6eeSJed Brown 
10731411c6eeSJed Brown   Output Parameter:
10741411c6eeSJed Brown . ltog - the mapping
10751411c6eeSJed Brown 
1076bb7acecfSBarry Smith   Level: advanced
10771411c6eeSJed Brown 
10781411c6eeSJed Brown   Notes:
1079bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10801411c6eeSJed Brown 
1081bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1082bb7acecfSBarry Smith   need to use this function with those objects.
1083bb7acecfSBarry Smith 
1084bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1085bb7acecfSBarry Smith 
108660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1087bb7acecfSBarry Smith           `DMCreateMatrix()`
10881411c6eeSJed Brown @*/
1089d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1090d71ae5a4SJacob Faibussowitsch {
10910be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10921411c6eeSJed Brown 
10931411c6eeSJed Brown   PetscFunctionBegin;
10941411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10954f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10961411c6eeSJed Brown   if (!dm->ltogmap) {
109737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
109837d0c07bSMatthew G Knepley 
10999566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110037d0c07bSMatthew G Knepley     if (section) {
1101a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1103ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110437d0c07bSMatthew G Knepley 
11059566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11079566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11089566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
110937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1110e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111137d0c07bSMatthew G Knepley 
111237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11171a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11181a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1119ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11205227eafbSStefano Zampini 
1121e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11225227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1123e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1124e6befd46SJed Brown             cind++;
1125e6befd46SJed Brown           } else {
11265227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1127e6befd46SJed Brown           }
112837d0c07bSMatthew G Knepley         }
112937d0c07bSMatthew G Knepley       }
1130bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11319371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11329371c9d4SSatish Balay       bsLocal[1] = bs;
11339566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11349371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11359371c9d4SSatish Balay         bs = 1;
11369371c9d4SSatish Balay       } else {
11379371c9d4SSatish Balay         bs = bsMinMax[0];
11389371c9d4SSatish Balay       }
11397591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11407591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1141ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1142ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1143ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1144ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1145ca469d19SJed Brown         }
1146ccf3bd66SMatthew G. Knepley         n /= bs;
1147ccf3bd66SMatthew G. Knepley       }
11489566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1149dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115037d0c07bSMatthew G Knepley   }
11511411c6eeSJed Brown   *ltog = dm->ltogmap;
11523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11531411c6eeSJed Brown }
11541411c6eeSJed Brown 
11551411c6eeSJed Brown /*@
1156bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11571411c6eeSJed Brown 
11581411c6eeSJed Brown   Not Collective
11591411c6eeSJed Brown 
11601411c6eeSJed Brown   Input Parameter:
1161bb7acecfSBarry Smith . dm - the `DM` with block structure
11621411c6eeSJed Brown 
11631411c6eeSJed Brown   Output Parameter:
11641411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11651411c6eeSJed Brown 
11661411c6eeSJed Brown   Level: intermediate
11671411c6eeSJed Brown 
116873ff1848SBarry Smith   Notes:
1169bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1170bb7acecfSBarry Smith 
1171bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1172bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1173bb7acecfSBarry Smith 
11741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11751411c6eeSJed Brown @*/
1176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1177d71ae5a4SJacob Faibussowitsch {
11781411c6eeSJed Brown   PetscFunctionBegin;
11791411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11804f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11817a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11821411c6eeSJed Brown   *bs = dm->bs;
11833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11841411c6eeSJed Brown }
11851411c6eeSJed Brown 
1186ffeef943SBarry Smith /*@
1187bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1188bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
118947c6ae99SBarry Smith 
119020f4b53cSBarry Smith   Collective
119147c6ae99SBarry Smith 
1192d8d19677SJose E. Roman   Input Parameters:
1193bb7acecfSBarry Smith + dmc - the `DM` object
1194bb7acecfSBarry Smith - dmf - the second, finer `DM` object
119547c6ae99SBarry Smith 
1196d8d19677SJose E. Roman   Output Parameters:
119747c6ae99SBarry Smith + mat - the interpolation
1198b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
119947c6ae99SBarry Smith 
120047c6ae99SBarry Smith   Level: developer
120147c6ae99SBarry Smith 
120295452b02SPatrick Sanan   Notes:
1203bb7acecfSBarry 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
1204bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1205d52bd9f3SBarry Smith 
1206bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1207bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
120885afcc9aSBarry Smith 
12091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121047c6ae99SBarry Smith @*/
1211d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1212d71ae5a4SJacob Faibussowitsch {
121347c6ae99SBarry Smith   PetscFunctionBegin;
1214a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1215a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12164f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1218dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122147c6ae99SBarry Smith }
122247c6ae99SBarry Smith 
12233ad4599aSBarry Smith /*@
1224a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1225a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12262ed6491fSPatrick Sanan 
12272ed6491fSPatrick Sanan   Input Parameters:
1228bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1229bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12302ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12312ed6491fSPatrick Sanan 
12322ed6491fSPatrick Sanan   Output Parameter:
12332ed6491fSPatrick Sanan . scale - the scaled vector
12342ed6491fSPatrick Sanan 
1235bb7acecfSBarry Smith   Level: advanced
12362ed6491fSPatrick Sanan 
123773ff1848SBarry Smith   Note:
1238a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1239a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1240a4e35b19SJacob Faibussowitsch 
124173ff1848SBarry Smith   Developer Note:
1242bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1243e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1244e9c74fd6SRichard Tran Mills 
124560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12462ed6491fSPatrick Sanan @*/
1247d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1248d71ae5a4SJacob Faibussowitsch {
12492ed6491fSPatrick Sanan   Vec         fine;
12502ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12519704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1252e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12539704db99SRichard Tran Mills #endif
12542ed6491fSPatrick Sanan 
12552ed6491fSPatrick Sanan   PetscFunctionBegin;
12569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12579566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12589566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12599704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12609704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12619704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12629704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12639566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1264e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12659566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12669566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12679566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126883aa49f4SRichard Tran Mills   }
12699704db99SRichard Tran Mills #endif
12709566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12719566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12729566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12742ed6491fSPatrick Sanan }
12752ed6491fSPatrick Sanan 
12762ed6491fSPatrick Sanan /*@
1277bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1278bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12793ad4599aSBarry Smith 
128020f4b53cSBarry Smith   Collective
12813ad4599aSBarry Smith 
1282d8d19677SJose E. Roman   Input Parameters:
1283bb7acecfSBarry Smith + dmc - the `DM` object
1284bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12853ad4599aSBarry Smith 
12863ad4599aSBarry Smith   Output Parameter:
12873ad4599aSBarry Smith . mat - the restriction
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith   Level: developer
12903ad4599aSBarry Smith 
1291bb7acecfSBarry Smith   Note:
1292bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1293bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12943ad4599aSBarry Smith 
12951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12963ad4599aSBarry Smith @*/
1297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1298d71ae5a4SJacob Faibussowitsch {
12993ad4599aSBarry Smith   PetscFunctionBegin;
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1301a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13024f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1304dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13073ad4599aSBarry Smith }
13083ad4599aSBarry Smith 
130947c6ae99SBarry Smith /*@
1310a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
131147c6ae99SBarry Smith 
131220f4b53cSBarry Smith   Collective
131347c6ae99SBarry Smith 
1314d8d19677SJose E. Roman   Input Parameters:
1315bb7acecfSBarry Smith + dac - the `DM` object
1316bb7acecfSBarry Smith - daf - the second, finer `DM` object
131747c6ae99SBarry Smith 
131847c6ae99SBarry Smith   Output Parameter:
13196dbf9973SLawrence Mitchell . mat - the injection
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith   Level: developer
132247c6ae99SBarry Smith 
1323a4e35b19SJacob Faibussowitsch   Notes:
1324a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1325a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1326a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1327a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1328a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1329a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1330a4e35b19SJacob Faibussowitsch 
1331bb7acecfSBarry 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
1332bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133385afcc9aSBarry Smith 
13341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1335bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133647c6ae99SBarry Smith @*/
1337d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1338d71ae5a4SJacob Faibussowitsch {
133947c6ae99SBarry Smith   PetscFunctionBegin;
1340a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1341a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13424f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13439566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1344dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13459566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134747c6ae99SBarry Smith }
134847c6ae99SBarry Smith 
1349b412c318SBarry Smith /*@
1350bb7acecfSBarry 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
1351bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1352bd041c0cSMatthew G. Knepley 
135320f4b53cSBarry Smith   Collective
1354bd041c0cSMatthew G. Knepley 
1355d8d19677SJose E. Roman   Input Parameters:
1356bb7acecfSBarry Smith + dmc - the target `DM` object
1357bb7acecfSBarry Smith - dmf - the source `DM` object
1358bd041c0cSMatthew G. Knepley 
1359bd041c0cSMatthew G. Knepley   Output Parameter:
1360b4937a87SMatthew G. Knepley . mat - the mass matrix
1361bd041c0cSMatthew G. Knepley 
1362bd041c0cSMatthew G. Knepley   Level: developer
1363bd041c0cSMatthew G. Knepley 
1364bb7acecfSBarry Smith   Notes:
1365bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1366bb7acecfSBarry Smith 
136720f4b53cSBarry 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()`
1368bb7acecfSBarry Smith 
136942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1370bd041c0cSMatthew G. Knepley @*/
1371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1372d71ae5a4SJacob Faibussowitsch {
1373bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1374b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1375b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13764f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13775b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1378dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13795b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1381b4937a87SMatthew G. Knepley }
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley /*@
1384bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1385b4937a87SMatthew G. Knepley 
138620f4b53cSBarry Smith   Collective
1387b4937a87SMatthew G. Knepley 
1388b4937a87SMatthew G. Knepley   Input Parameter:
1389bb7acecfSBarry Smith . dm - the `DM` object
1390b4937a87SMatthew G. Knepley 
1391b4937a87SMatthew G. Knepley   Output Parameter:
1392bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1393b4937a87SMatthew G. Knepley 
1394b4937a87SMatthew G. Knepley   Level: developer
1395b4937a87SMatthew G. Knepley 
1396bb7acecfSBarry Smith   Note:
1397bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1398bb7acecfSBarry Smith 
139960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1400b4937a87SMatthew G. Knepley @*/
1401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1402d71ae5a4SJacob Faibussowitsch {
1403b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1404b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14054f572ea9SToby Isaac   PetscAssertPointer(lm, 2);
1406dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1408bd041c0cSMatthew G. Knepley }
1409bd041c0cSMatthew G. Knepley 
1410bd041c0cSMatthew G. Knepley /*@
1411bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1412bb7acecfSBarry Smith   of a PDE on the `DM`.
141347c6ae99SBarry Smith 
141420f4b53cSBarry Smith   Collective
141547c6ae99SBarry Smith 
1416d8d19677SJose E. Roman   Input Parameters:
1417bb7acecfSBarry Smith + dm    - the `DM` object
1418bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141947c6ae99SBarry Smith 
142047c6ae99SBarry Smith   Output Parameter:
142147c6ae99SBarry Smith . coloring - the coloring
142247c6ae99SBarry Smith 
14231bf8429eSBarry Smith   Level: developer
14241bf8429eSBarry Smith 
1425ec5066bdSBarry Smith   Notes:
1426bb7acecfSBarry 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
1427bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1428ec5066bdSBarry Smith 
1429bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14301bf8429eSBarry 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,
14311bf8429eSBarry Smith   otherwise an error will be generated.
1432ec5066bdSBarry Smith 
14331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1434aab9d709SJed Brown @*/
1435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1436d71ae5a4SJacob Faibussowitsch {
143747c6ae99SBarry Smith   PetscFunctionBegin;
1438171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14394f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1440dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
144247c6ae99SBarry Smith }
144347c6ae99SBarry Smith 
1444b412c318SBarry Smith /*@
1445bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144647c6ae99SBarry Smith 
144720f4b53cSBarry Smith   Collective
144847c6ae99SBarry Smith 
144947c6ae99SBarry Smith   Input Parameter:
1450bb7acecfSBarry Smith . dm - the `DM` object
145147c6ae99SBarry Smith 
145247c6ae99SBarry Smith   Output Parameter:
145347c6ae99SBarry Smith . mat - the empty Jacobian
145447c6ae99SBarry Smith 
145520f4b53cSBarry Smith   Options Database Key:
1456bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1457f27dd7c6SMatthew G. Knepley 
145820f4b53cSBarry Smith   Level: beginner
145920f4b53cSBarry Smith 
146095452b02SPatrick Sanan   Notes:
146195452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
146294013140SBarry Smith   do not need to do it yourself.
146394013140SBarry Smith 
146494013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1465bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146694013140SBarry Smith 
1467bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146894013140SBarry Smith   internally by PETSc.
146994013140SBarry Smith 
1470bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1471bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
147294013140SBarry Smith 
14731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1474aab9d709SJed Brown @*/
1475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1476d71ae5a4SJacob Faibussowitsch {
147747c6ae99SBarry Smith   PetscFunctionBegin;
1478171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14794f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14809566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14819566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1482dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148376bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1484c6b011d8SStefano Zampini     DM mdm;
1485c6b011d8SStefano Zampini 
14869566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14877a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1488c6b011d8SStefano Zampini   }
1489e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1490e5e52638SMatthew G. Knepley   if (dm->Nf) {
1491e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1492649ef022SMatthew Knepley     PetscInt     Nf, f;
1493e571a35bSMatthew G. Knepley 
14949566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1495649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1496649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14979566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14989566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14999566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1500649ef022SMatthew Knepley         break;
1501e571a35bSMatthew G. Knepley       }
1502649ef022SMatthew Knepley     }
1503649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1504649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15059566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15069566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15079566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1508e571a35bSMatthew G. Knepley       }
1509e571a35bSMatthew G. Knepley     }
1510e571a35bSMatthew G. Knepley   }
15119566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151347c6ae99SBarry Smith }
151447c6ae99SBarry Smith 
1515732e2eb9SMatthew G Knepley /*@
1516a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1517a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1518a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1519aa0f6e3cSJed Brown 
152020f4b53cSBarry Smith   Logically Collective
1521aa0f6e3cSJed Brown 
1522aa0f6e3cSJed Brown   Input Parameters:
1523bb7acecfSBarry Smith + dm   - the `DM`
1524bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1525aa0f6e3cSJed Brown 
1526aa0f6e3cSJed Brown   Level: developer
1527aa0f6e3cSJed Brown 
152873ff1848SBarry Smith   Note:
1529a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1530a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1531a4e35b19SJacob Faibussowitsch 
15321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1533aa0f6e3cSJed Brown @*/
1534d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1535d71ae5a4SJacob Faibussowitsch {
1536aa0f6e3cSJed Brown   PetscFunctionBegin;
1537aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1538aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1540aa0f6e3cSJed Brown }
1541aa0f6e3cSJed Brown 
1542aa0f6e3cSJed Brown /*@
1543bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1544732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1545732e2eb9SMatthew G Knepley 
154620f4b53cSBarry Smith   Logically Collective
1547732e2eb9SMatthew G Knepley 
1548d8d19677SJose E. Roman   Input Parameters:
1549bb7acecfSBarry Smith + dm   - the `DM`
1550bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1551732e2eb9SMatthew G Knepley 
155220f4b53cSBarry Smith   Options Database Key:
1553bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1554f27dd7c6SMatthew G. Knepley 
155520f4b53cSBarry Smith   Level: developer
155620f4b53cSBarry Smith 
15571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1558732e2eb9SMatthew G Knepley @*/
1559d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1560d71ae5a4SJacob Faibussowitsch {
1561732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1562732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1563732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1565732e2eb9SMatthew G Knepley }
1566732e2eb9SMatthew G Knepley 
1567b06ff27eSHong Zhang /*@
1568bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1569bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1570b06ff27eSHong Zhang 
157120f4b53cSBarry Smith   Logically Collective
1572b06ff27eSHong Zhang 
1573d8d19677SJose E. Roman   Input Parameters:
1574bb7acecfSBarry Smith + dm   - the `DM`
1575da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1576b06ff27eSHong Zhang 
1577b06ff27eSHong Zhang   Level: developer
1578bb7acecfSBarry Smith 
15791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1580b06ff27eSHong Zhang @*/
1581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1582d71ae5a4SJacob Faibussowitsch {
1583b06ff27eSHong Zhang   PetscFunctionBegin;
1584b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1585b06ff27eSHong Zhang   dm->structure_only = only;
15863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1587b06ff27eSHong Zhang }
1588b06ff27eSHong Zhang 
1589863027abSJed Brown /*@
1590863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1591863027abSJed Brown 
159220f4b53cSBarry Smith   Logically Collective
1593863027abSJed Brown 
1594863027abSJed Brown   Input Parameters:
1595863027abSJed Brown + dm    - the `DM`
1596863027abSJed Brown - btype - block by topological point or field node
1597863027abSJed Brown 
159820f4b53cSBarry Smith   Options Database Key:
15990e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1600863027abSJed Brown 
160120f4b53cSBarry Smith   Level: advanced
160220f4b53cSBarry Smith 
16031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1604863027abSJed Brown @*/
1605863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1606863027abSJed Brown {
1607863027abSJed Brown   PetscFunctionBegin;
1608863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1609863027abSJed Brown   dm->blocking_type = btype;
16103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1611863027abSJed Brown }
1612863027abSJed Brown 
1613863027abSJed Brown /*@
1614863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1615863027abSJed Brown 
1616863027abSJed Brown   Not Collective
1617863027abSJed Brown 
16182fe279fdSBarry Smith   Input Parameter:
1619863027abSJed Brown . dm - the `DM`
1620863027abSJed Brown 
16212fe279fdSBarry Smith   Output Parameter:
1622863027abSJed Brown . btype - block by topological point or field node
1623863027abSJed Brown 
1624863027abSJed Brown   Level: advanced
1625863027abSJed Brown 
16261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1627863027abSJed Brown @*/
1628863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1629863027abSJed Brown {
1630863027abSJed Brown   PetscFunctionBegin;
1631863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16324f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1633863027abSJed Brown   *btype = dm->blocking_type;
16343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1635863027abSJed Brown }
1636863027abSJed Brown 
1637a89ea682SMatthew G Knepley /*@C
1638bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1639a89ea682SMatthew G Knepley 
1640a89ea682SMatthew G Knepley   Not Collective
1641a89ea682SMatthew G Knepley 
1642a89ea682SMatthew G Knepley   Input Parameters:
1643bb7acecfSBarry Smith + dm    - the `DM` object
1644a5b23f4aSJose E. Roman . count - The minimum size
164520f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1646a89ea682SMatthew G Knepley 
1647a89ea682SMatthew G Knepley   Output Parameter:
164860225df5SJacob Faibussowitsch . mem - the work array
1649a89ea682SMatthew G Knepley 
1650a89ea682SMatthew G Knepley   Level: developer
1651a89ea682SMatthew G Knepley 
165273ff1848SBarry Smith   Notes:
1653da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1654bb7acecfSBarry Smith 
1655bb7acecfSBarry Smith   The array may contain nonzero values
1656bb7acecfSBarry Smith 
16571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1658a89ea682SMatthew G Knepley @*/
1659d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1660d71ae5a4SJacob Faibussowitsch {
1661aa1993deSMatthew G Knepley   DMWorkLink  link;
166269291d52SBarry Smith   PetscMPIInt dsize;
1663a89ea682SMatthew G Knepley 
1664a89ea682SMatthew G Knepley   PetscFunctionBegin;
1665a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16664f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1667442f3b32SStefano Zampini   if (!count) {
1668442f3b32SStefano Zampini     *(void **)mem = NULL;
1669442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1670442f3b32SStefano Zampini   }
1671aa1993deSMatthew G Knepley   if (dm->workin) {
1672aa1993deSMatthew G Knepley     link       = dm->workin;
1673aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1674aa1993deSMatthew G Knepley   } else {
16754dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1676a89ea682SMatthew G Knepley   }
1677b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1678b77fa653SStefano Zampini      Get size directly */
1679b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1680b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1681b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1682b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1683b77fa653SStefano Zampini #endif
1684b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1685b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1686b77fa653SStefano Zampini #endif
1687b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1688b77fa653SStefano Zampini 
16895056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16909566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16919566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1692854ce69bSBarry Smith     link->bytes = dsize * count;
1693aa1993deSMatthew G Knepley   }
1694aa1993deSMatthew G Knepley   link->next  = dm->workout;
1695aa1993deSMatthew G Knepley   dm->workout = link;
1696cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
169700d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
169800d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
169900d952a4SJed Brown #endif
1700aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1702a89ea682SMatthew G Knepley }
1703a89ea682SMatthew G Knepley 
1704aa1993deSMatthew G Knepley /*@C
1705bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1706aa1993deSMatthew G Knepley 
1707aa1993deSMatthew G Knepley   Not Collective
1708aa1993deSMatthew G Knepley 
1709aa1993deSMatthew G Knepley   Input Parameters:
1710bb7acecfSBarry Smith + dm    - the `DM` object
1711a5b23f4aSJose E. Roman . count - The minimum size
171220f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1713aa1993deSMatthew G Knepley 
1714aa1993deSMatthew G Knepley   Output Parameter:
171560225df5SJacob Faibussowitsch . mem - the work array
1716aa1993deSMatthew G Knepley 
1717aa1993deSMatthew G Knepley   Level: developer
1718aa1993deSMatthew G Knepley 
171973ff1848SBarry Smith   Developer Note:
1720bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1721147403d9SBarry Smith 
17221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1723aa1993deSMatthew G Knepley @*/
1724d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1725d71ae5a4SJacob Faibussowitsch {
1726aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1727aa1993deSMatthew G Knepley 
1728aa1993deSMatthew G Knepley   PetscFunctionBegin;
1729aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17304f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1731a4e35b19SJacob Faibussowitsch   (void)count;
1732a4e35b19SJacob Faibussowitsch   (void)dtype;
1733442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1734aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1735aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1736aa1993deSMatthew G Knepley       *p            = link->next;
1737aa1993deSMatthew G Knepley       link->next    = dm->workin;
1738aa1993deSMatthew G Knepley       dm->workin    = link;
17390298fd71SBarry Smith       *(void **)mem = NULL;
17403ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1741aa1993deSMatthew G Knepley     }
1742aa1993deSMatthew G Knepley   }
1743aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1744aa1993deSMatthew G Knepley }
1745e7c4fc90SDmitry Karpeev 
17468cda7954SMatthew G. Knepley /*@C
1747bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1748bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17498cda7954SMatthew G. Knepley 
175020f4b53cSBarry Smith   Logically Collective; No Fortran Support
17518cda7954SMatthew G. Knepley 
17528cda7954SMatthew G. Knepley   Input Parameters:
1753bb7acecfSBarry Smith + dm     - The `DM`
17548cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17558cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17568cda7954SMatthew G. Knepley 
175720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1758bb7acecfSBarry Smith + dm        - The present `DM`
1759bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1760147403d9SBarry Smith . field     - The field number in dm
1761147403d9SBarry Smith - nullSpace - The nullspace for the given field
17628cda7954SMatthew G. Knepley 
176349762cbcSSatish Balay   Level: intermediate
176449762cbcSSatish Balay 
17651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1766147403d9SBarry Smith @*/
1767a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1768d71ae5a4SJacob Faibussowitsch {
1769435a35e8SMatthew G Knepley   PetscFunctionBegin;
1770435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17717a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1772435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1774435a35e8SMatthew G Knepley }
1775435a35e8SMatthew G Knepley 
17768cda7954SMatthew G. Knepley /*@C
1777bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17788cda7954SMatthew G. Knepley 
177920f4b53cSBarry Smith   Not Collective; No Fortran Support
17808cda7954SMatthew G. Knepley 
17818cda7954SMatthew G. Knepley   Input Parameters:
1782bb7acecfSBarry Smith + dm    - The `DM`
17838cda7954SMatthew G. Knepley - field - The field number for the nullspace
17848cda7954SMatthew G. Knepley 
17858cda7954SMatthew G. Knepley   Output Parameter:
17868cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17878cda7954SMatthew G. Knepley 
178820f4b53cSBarry Smith   Calling sequence of `nullsp`:
1789147403d9SBarry Smith + dm        - The present DM
1790147403d9SBarry Smith . origField - The field number given above, in the original DM
1791147403d9SBarry Smith . field     - The field number in dm
1792147403d9SBarry Smith - nullSpace - The nullspace for the given field
17938cda7954SMatthew G. Knepley 
179449762cbcSSatish Balay   Level: intermediate
179549762cbcSSatish Balay 
17961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1797147403d9SBarry Smith @*/
1798a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1799d71ae5a4SJacob Faibussowitsch {
18000a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18010a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18024f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18037a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18040a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18060a50eb56SMatthew G. Knepley }
18070a50eb56SMatthew G. Knepley 
18088cda7954SMatthew G. Knepley /*@C
1809bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18108cda7954SMatthew G. Knepley 
181120f4b53cSBarry Smith   Logically Collective; No Fortran Support
18128cda7954SMatthew G. Knepley 
18138cda7954SMatthew G. Knepley   Input Parameters:
1814bb7acecfSBarry Smith + dm     - The `DM`
18158cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18168cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18178cda7954SMatthew G. Knepley 
181820f4b53cSBarry Smith   Calling sequence of `nullsp`:
1819bb7acecfSBarry Smith + dm        - The present `DM`
1820bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1821147403d9SBarry Smith . field     - The field number in dm
1822147403d9SBarry Smith - nullSpace - The nullspace for the given field
18238cda7954SMatthew G. Knepley 
182449762cbcSSatish Balay   Level: intermediate
182549762cbcSSatish Balay 
18261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1827bb7acecfSBarry Smith           `MatNullSpace`
1828147403d9SBarry Smith @*/
1829a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1830d71ae5a4SJacob Faibussowitsch {
1831f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1832f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18337a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1834f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1836f9d4088aSMatthew G. Knepley }
1837f9d4088aSMatthew G. Knepley 
18388cda7954SMatthew G. Knepley /*@C
1839bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18408cda7954SMatthew G. Knepley 
184120f4b53cSBarry Smith   Not Collective; No Fortran Support
18428cda7954SMatthew G. Knepley 
18438cda7954SMatthew G. Knepley   Input Parameters:
1844bb7acecfSBarry Smith + dm    - The `DM`
18458cda7954SMatthew G. Knepley - field - The field number for the nullspace
18468cda7954SMatthew G. Knepley 
18478cda7954SMatthew G. Knepley   Output Parameter:
18488cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18498cda7954SMatthew G. Knepley 
185020f4b53cSBarry Smith   Calling sequence of `nullsp`:
1851bb7acecfSBarry Smith + dm        - The present `DM`
1852bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1853147403d9SBarry Smith . field     - The field number in dm
1854147403d9SBarry Smith - nullSpace - The nullspace for the given field
18558cda7954SMatthew G. Knepley 
185649762cbcSSatish Balay   Level: intermediate
185749762cbcSSatish Balay 
18581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1859bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1860147403d9SBarry Smith @*/
1861a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1862d71ae5a4SJacob Faibussowitsch {
1863f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1864f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18654f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18667a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1867f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1869f9d4088aSMatthew G. Knepley }
1870f9d4088aSMatthew G. Knepley 
18714f3b5142SJed Brown /*@C
1872bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18734d343eeaSMatthew G Knepley 
187420f4b53cSBarry Smith   Not Collective; No Fortran Support
18754d343eeaSMatthew G Knepley 
18764d343eeaSMatthew G Knepley   Input Parameter:
1877bb7acecfSBarry Smith . dm - the `DM` object
18784d343eeaSMatthew G Knepley 
18794d343eeaSMatthew G Knepley   Output Parameters:
188020f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188120f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188220f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18834d343eeaSMatthew G Knepley 
18844d343eeaSMatthew G Knepley   Level: intermediate
18854d343eeaSMatthew G Knepley 
1886bb7acecfSBarry Smith   Note:
188773ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
188873ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1889bb7acecfSBarry Smith   `PetscFree()`.
189021c9b008SJed Brown 
189173ff1848SBarry Smith   Developer Note:
1892bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1893bb7acecfSBarry Smith   likely be removed.
1894bb7acecfSBarry Smith 
18951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1896bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18974d343eeaSMatthew G Knepley @*/
1898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1899d71ae5a4SJacob Faibussowitsch {
190037d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19014d343eeaSMatthew G Knepley 
19024d343eeaSMatthew G Knepley   PetscFunctionBegin;
19034d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190469ca1f37SDmitry Karpeev   if (numFields) {
19054f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
190669ca1f37SDmitry Karpeev     *numFields = 0;
190769ca1f37SDmitry Karpeev   }
190837d0c07bSMatthew G Knepley   if (fieldNames) {
19094f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19100298fd71SBarry Smith     *fieldNames = NULL;
191169ca1f37SDmitry Karpeev   }
191269ca1f37SDmitry Karpeev   if (fields) {
19134f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19140298fd71SBarry Smith     *fields = NULL;
191569ca1f37SDmitry Karpeev   }
19169566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191737d0c07bSMatthew G Knepley   if (section) {
19183a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
191937d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192037d0c07bSMatthew G Knepley 
19219566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19239566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
192837d0c07bSMatthew G Knepley     }
192937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193037d0c07bSMatthew G Knepley       PetscInt gdof;
193137d0c07bSMatthew G Knepley 
19329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193337d0c07bSMatthew G Knepley       if (gdof > 0) {
193437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19353a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193637d0c07bSMatthew G Knepley 
19379566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19389566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19393a544194SStefano Zampini           fpdof = fdof - fcdof;
19403a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19413a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19423a544194SStefano Zampini             fieldNc[f] = 1;
19433a544194SStefano Zampini           }
19443a544194SStefano Zampini           fieldSizes[f] += fpdof;
194537d0c07bSMatthew G Knepley         }
194637d0c07bSMatthew G Knepley       }
194737d0c07bSMatthew G Knepley     }
194837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19499566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195137d0c07bSMatthew G Knepley     }
195237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195437d0c07bSMatthew G Knepley 
19559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195637d0c07bSMatthew G Knepley       if (gdof > 0) {
19579566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
195837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
195937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196037d0c07bSMatthew G Knepley 
19619566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19629566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1963ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196437d0c07bSMatthew G Knepley         }
196537d0c07bSMatthew G Knepley       }
196637d0c07bSMatthew G Knepley     }
19678865f1eaSKarl Rupp     if (numFields) *numFields = nF;
196837d0c07bSMatthew G Knepley     if (fieldNames) {
19699566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197137d0c07bSMatthew G Knepley         const char *fieldName;
197237d0c07bSMatthew G Knepley 
19739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19749566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197537d0c07bSMatthew G Knepley       }
197637d0c07bSMatthew G Knepley     }
197737d0c07bSMatthew G Knepley     if (fields) {
19789566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
197937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19803a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19813a544194SStefano Zampini 
19829566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19833a544194SStefano Zampini         in[0] = -fieldNc[f];
19843a544194SStefano Zampini         in[1] = fieldNc[f];
19851c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19863a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19879566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
198837d0c07bSMatthew G Knepley       }
198937d0c07bSMatthew G Knepley     }
19909566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1991dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19934d343eeaSMatthew G Knepley }
19944d343eeaSMatthew G Knepley 
199516621825SDmitry Karpeev /*@C
1996bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1997a4e35b19SJacob Faibussowitsch   corresponding to different fields.
1998e7c4fc90SDmitry Karpeev 
199920f4b53cSBarry Smith   Not Collective; No Fortran Support
2000e7c4fc90SDmitry Karpeev 
2001e7c4fc90SDmitry Karpeev   Input Parameter:
2002bb7acecfSBarry Smith . dm - the `DM` object
2003e7c4fc90SDmitry Karpeev 
2004e7c4fc90SDmitry Karpeev   Output Parameters:
200520f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
200620f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
200720f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
200820f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2009e7c4fc90SDmitry Karpeev 
2010e7c4fc90SDmitry Karpeev   Level: intermediate
2011e7c4fc90SDmitry Karpeev 
2012a4e35b19SJacob Faibussowitsch   Notes:
2013a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2014a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2015a4e35b19SJacob Faibussowitsch 
2016a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2017a4e35b19SJacob Faibussowitsch 
201873ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
201973ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2020bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2021e7c4fc90SDmitry Karpeev 
202260225df5SJacob Faibussowitsch   Developer Notes:
2023bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2024bb7acecfSBarry Smith 
202573ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
202673ff1848SBarry Smith   decomposition is computed.
202773ff1848SBarry Smith 
202873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2029e7c4fc90SDmitry Karpeev @*/
2030d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2031d71ae5a4SJacob Faibussowitsch {
2032e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2033e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20348865f1eaSKarl Rupp   if (len) {
20354f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20368865f1eaSKarl Rupp     *len = 0;
20378865f1eaSKarl Rupp   }
20388865f1eaSKarl Rupp   if (namelist) {
20394f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2040ea78f98cSLisandro Dalcin     *namelist = NULL;
20418865f1eaSKarl Rupp   }
20428865f1eaSKarl Rupp   if (islist) {
20434f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2044ea78f98cSLisandro Dalcin     *islist = NULL;
20458865f1eaSKarl Rupp   }
20468865f1eaSKarl Rupp   if (dmlist) {
20474f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2048ea78f98cSLisandro Dalcin     *dmlist = NULL;
20498865f1eaSKarl Rupp   }
2050f3f0edfdSDmitry Karpeev   /*
2051f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2052f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2053f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2054f3f0edfdSDmitry Karpeev    */
20557a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2057435a35e8SMatthew G Knepley     PetscSection section;
2058435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2059435a35e8SMatthew G Knepley 
20609566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20619566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2062435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2063f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20649566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20659566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20669566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2067435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2068435a35e8SMatthew G Knepley         const char *fieldName;
2069435a35e8SMatthew G Knepley 
20709566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
207103dc3394SMatthew G. Knepley         if (namelist) {
20729566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20739566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2074435a35e8SMatthew G Knepley         }
207503dc3394SMatthew G. Knepley       }
2076435a35e8SMatthew G Knepley     } else {
20779566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2078e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20790298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2080e7c4fc90SDmitry Karpeev     }
2081dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208316621825SDmitry Karpeev }
208416621825SDmitry Karpeev 
20855d83a8b1SBarry Smith /*@
208620f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
208720f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2088435a35e8SMatthew G Knepley 
2089435a35e8SMatthew G Knepley   Not collective
2090435a35e8SMatthew G Knepley 
2091435a35e8SMatthew G Knepley   Input Parameters:
2092bb7acecfSBarry Smith + dm        - The `DM` object
2093bb7acecfSBarry Smith . numFields - The number of fields to select
20942adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2095435a35e8SMatthew G Knepley 
2096435a35e8SMatthew G Knepley   Output Parameters:
2097b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2098b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2099435a35e8SMatthew G Knepley 
210020f4b53cSBarry Smith   Level: intermediate
210120f4b53cSBarry Smith 
2102bb7acecfSBarry Smith   Note:
2103bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21045d3b26e6SMatthew G. Knepley 
210560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2106435a35e8SMatthew G Knepley @*/
2107d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2108d71ae5a4SJacob Faibussowitsch {
2109435a35e8SMatthew G Knepley   PetscFunctionBegin;
2110435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21114f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21124f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21134f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2114dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2116435a35e8SMatthew G Knepley }
2117435a35e8SMatthew G Knepley 
21182adcc780SMatthew G. Knepley /*@C
2119bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21202adcc780SMatthew G. Knepley 
21212adcc780SMatthew G. Knepley   Not collective
21222adcc780SMatthew G. Knepley 
2123d8d19677SJose E. Roman   Input Parameters:
2124bb7acecfSBarry Smith + dms - The `DM` objects
2125bb7acecfSBarry Smith - n   - The number of `DM`s
21262adcc780SMatthew G. Knepley 
21272adcc780SMatthew G. Knepley   Output Parameters:
2128bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2129bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21302adcc780SMatthew G. Knepley 
213120f4b53cSBarry Smith   Level: intermediate
213220f4b53cSBarry Smith 
2133bb7acecfSBarry Smith   Note:
2134bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21355d3b26e6SMatthew G. Knepley 
213673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21372adcc780SMatthew G. Knepley @*/
21385d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2139d71ae5a4SJacob Faibussowitsch {
21402adcc780SMatthew G. Knepley   PetscInt i;
21412adcc780SMatthew G. Knepley 
21422adcc780SMatthew G. Knepley   PetscFunctionBegin;
21434f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2144ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21454f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21464f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2147bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2148bb7acecfSBarry Smith   if (n) {
2149b9d85ea2SLisandro Dalcin     DM dm = dms[0];
215000045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2151dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21522adcc780SMatthew G. Knepley   }
21533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21542adcc780SMatthew G. Knepley }
21552adcc780SMatthew G. Knepley 
215616621825SDmitry Karpeev /*@C
2157a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2158a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
215916621825SDmitry Karpeev 
216020f4b53cSBarry Smith   Not Collective
216116621825SDmitry Karpeev 
216216621825SDmitry Karpeev   Input Parameter:
2163bb7acecfSBarry Smith . dm - the `DM` object
216416621825SDmitry Karpeev 
216516621825SDmitry Karpeev   Output Parameters:
216620f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
216720f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
216873ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
216973ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
217073ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
217116621825SDmitry Karpeev 
217216621825SDmitry Karpeev   Level: intermediate
217316621825SDmitry Karpeev 
217473ff1848SBarry Smith   Notes:
2175a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2176a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2177a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2178a4e35b19SJacob Faibussowitsch 
2179a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2180a4e35b19SJacob Faibussowitsch 
218173ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
218273ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2183bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
218416621825SDmitry Karpeev 
2185a4e35b19SJacob Faibussowitsch   Developer Notes:
218620f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2187bb7acecfSBarry Smith 
218873ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
218973ff1848SBarry Smith 
219073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
219173ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
219216621825SDmitry Karpeev @*/
2193d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2194d71ae5a4SJacob Faibussowitsch {
2195be081cd6SPeter Brune   DMSubDomainHookLink link;
2196be081cd6SPeter Brune   PetscInt            i, l;
219716621825SDmitry Karpeev 
219816621825SDmitry Karpeev   PetscFunctionBegin;
219916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22009371c9d4SSatish Balay   if (n) {
22014f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22029371c9d4SSatish Balay     *n = 0;
22039371c9d4SSatish Balay   }
22049371c9d4SSatish Balay   if (namelist) {
22054f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22069371c9d4SSatish Balay     *namelist = NULL;
22079371c9d4SSatish Balay   }
22089371c9d4SSatish Balay   if (innerislist) {
22094f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22109371c9d4SSatish Balay     *innerislist = NULL;
22119371c9d4SSatish Balay   }
22129371c9d4SSatish Balay   if (outerislist) {
22134f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22149371c9d4SSatish Balay     *outerislist = NULL;
22159371c9d4SSatish Balay   }
22169371c9d4SSatish Balay   if (dmlist) {
22174f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22189371c9d4SSatish Balay     *dmlist = NULL;
22199371c9d4SSatish Balay   }
2220f3f0edfdSDmitry Karpeev   /*
2221f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2222f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2223f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2224f3f0edfdSDmitry Karpeev    */
22257a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
222616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2227dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
222814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2229f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2230be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2231be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22329566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2233be081cd6SPeter Brune         }
2234648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2235e7c4fc90SDmitry Karpeev       }
223614a18fd3SPeter Brune     }
2237bb7acecfSBarry Smith     if (n) *n = l;
223814a18fd3SPeter Brune   }
22393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2240e30e807fSPeter Brune }
2241e30e807fSPeter Brune 
2242e30e807fSPeter Brune /*@C
224373ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
224473ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2245e30e807fSPeter Brune 
224620f4b53cSBarry Smith   Not Collective
2247e30e807fSPeter Brune 
2248e30e807fSPeter Brune   Input Parameters:
2249bb7acecfSBarry Smith + dm     - the `DM` object
225073ff1848SBarry Smith . n      - the number of subdomains
2251e30e807fSPeter Brune - subdms - the local subdomains
2252e30e807fSPeter Brune 
2253e30e807fSPeter Brune   Output Parameters:
22546b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2255e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2256e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2257e30e807fSPeter Brune 
225820f4b53cSBarry Smith   Level: developer
225920f4b53cSBarry Smith 
2260bb7acecfSBarry Smith   Note:
2261bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2262e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2263e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2264e30e807fSPeter Brune   solution and residual data.
2265e30e807fSPeter Brune 
226673ff1848SBarry Smith   Developer Note:
2267a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2268a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2269bb7acecfSBarry Smith 
22701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2271e30e807fSPeter Brune @*/
22725d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2273d71ae5a4SJacob Faibussowitsch {
2274e30e807fSPeter Brune   PetscFunctionBegin;
2275e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22764f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2277dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2279e7c4fc90SDmitry Karpeev }
2280e7c4fc90SDmitry Karpeev 
228147c6ae99SBarry Smith /*@
2282bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
228347c6ae99SBarry Smith 
228420f4b53cSBarry Smith   Collective
228547c6ae99SBarry Smith 
2286d8d19677SJose E. Roman   Input Parameters:
2287bb7acecfSBarry Smith + dm   - the `DM` object
2288bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
228947c6ae99SBarry Smith 
229047c6ae99SBarry Smith   Output Parameter:
229120f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2292ae0a1c52SMatthew G Knepley 
229320f4b53cSBarry Smith   Options Database Key:
2294412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2295412e9a14SMatthew G. Knepley 
229647c6ae99SBarry Smith   Level: developer
229747c6ae99SBarry Smith 
229820f4b53cSBarry Smith   Note:
229920f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
230020f4b53cSBarry Smith 
230173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
230273ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
230347c6ae99SBarry Smith @*/
2304d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2305d71ae5a4SJacob Faibussowitsch {
2306c833c3b5SJed Brown   DMRefineHookLink link;
230747c6ae99SBarry Smith 
230847c6ae99SBarry Smith   PetscFunctionBegin;
2309732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23109566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2311dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23124057135bSMatthew G Knepley   if (*dmf) {
231343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23148865f1eaSKarl Rupp 
23159566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23168865f1eaSKarl Rupp 
2317644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23180598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2319656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23208865f1eaSKarl Rupp 
23219566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2322c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23231baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2324c833c3b5SJed Brown     }
2325c833c3b5SJed Brown   }
23269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2328c833c3b5SJed Brown }
2329c833c3b5SJed Brown 
2330bb9467b5SJed Brown /*@C
2331c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2332c833c3b5SJed Brown 
233320f4b53cSBarry Smith   Logically Collective; No Fortran Support
2334c833c3b5SJed Brown 
23354165533cSJose E. Roman   Input Parameters:
2336bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2337bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2338f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
233920f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2340c833c3b5SJed Brown 
234120f4b53cSBarry Smith   Calling sequence of `refinehook`:
2342bb7acecfSBarry Smith + coarse - coarse level `DM`
2343bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2344c833c3b5SJed Brown - ctx    - optional user-defined function context
2345c833c3b5SJed Brown 
234620f4b53cSBarry Smith   Calling sequence of `interphook`:
2347bb7acecfSBarry Smith + coarse - coarse level `DM`
2348c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2349bb7acecfSBarry Smith . fine   - fine level `DM` to update
2350c833c3b5SJed Brown - ctx    - optional user-defined function context
2351c833c3b5SJed Brown 
2352c833c3b5SJed Brown   Level: advanced
2353c833c3b5SJed Brown 
2354c833c3b5SJed Brown   Notes:
2355bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2356bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2357bb7acecfSBarry Smith 
2358bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2359c833c3b5SJed Brown 
2360c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2361c833c3b5SJed Brown 
23621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2363c833c3b5SJed Brown @*/
2364a4e35b19SJacob 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)
2365d71ae5a4SJacob Faibussowitsch {
2366c833c3b5SJed Brown   DMRefineHookLink link, *p;
2367c833c3b5SJed Brown 
2368c833c3b5SJed Brown   PetscFunctionBegin;
2369c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23703d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23713ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23723d8e3701SJed Brown   }
23739566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2374c833c3b5SJed Brown   link->refinehook = refinehook;
2375c833c3b5SJed Brown   link->interphook = interphook;
2376c833c3b5SJed Brown   link->ctx        = ctx;
23770298fd71SBarry Smith   link->next       = NULL;
2378c833c3b5SJed Brown   *p               = link;
23793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2380c833c3b5SJed Brown }
2381c833c3b5SJed Brown 
23823d8e3701SJed Brown /*@C
2383bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2384bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23853d8e3701SJed Brown 
238620f4b53cSBarry Smith   Logically Collective; No Fortran Support
23873d8e3701SJed Brown 
23884165533cSJose E. Roman   Input Parameters:
2389bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2390bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2391bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
239220f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23933d8e3701SJed Brown 
23943d8e3701SJed Brown   Level: advanced
23953d8e3701SJed Brown 
2396bb7acecfSBarry Smith   Note:
23973d8e3701SJed Brown   This function does nothing if the hook is not in the list.
23983d8e3701SJed Brown 
23991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24003d8e3701SJed Brown @*/
2401d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2402d71ae5a4SJacob Faibussowitsch {
24033d8e3701SJed Brown   DMRefineHookLink link, *p;
24043d8e3701SJed Brown 
24053d8e3701SJed Brown   PetscFunctionBegin;
24063d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24073d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24083d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24093d8e3701SJed Brown       link = *p;
24103d8e3701SJed Brown       *p   = link->next;
24119566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24123d8e3701SJed Brown       break;
24133d8e3701SJed Brown     }
24143d8e3701SJed Brown   }
24153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24163d8e3701SJed Brown }
24173d8e3701SJed Brown 
2418c833c3b5SJed Brown /*@
2419bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2420c833c3b5SJed Brown 
2421c833c3b5SJed Brown   Collective if any hooks are
2422c833c3b5SJed Brown 
24234165533cSJose E. Roman   Input Parameters:
2424bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2425bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2426bb7acecfSBarry Smith - fine   - finer `DM` to update
2427c833c3b5SJed Brown 
2428c833c3b5SJed Brown   Level: developer
2429c833c3b5SJed Brown 
243073ff1848SBarry Smith   Developer Note:
2431bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2432bb7acecfSBarry Smith   an API with consistent terminology.
2433bb7acecfSBarry Smith 
24341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2435c833c3b5SJed Brown @*/
2436d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2437d71ae5a4SJacob Faibussowitsch {
2438c833c3b5SJed Brown   DMRefineHookLink link;
2439c833c3b5SJed Brown 
2440c833c3b5SJed Brown   PetscFunctionBegin;
2441c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24421baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24434057135bSMatthew G Knepley   }
24443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
244547c6ae99SBarry Smith }
244647c6ae99SBarry Smith 
2447eb3f98d2SBarry Smith /*@
24481f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24491f3379b2SToby Isaac 
245020f4b53cSBarry Smith   Collective
24511f3379b2SToby Isaac 
24524165533cSJose E. Roman   Input Parameters:
2453bb7acecfSBarry Smith + coarse    - coarse `DM`
2454bb7acecfSBarry Smith . fine      - fine `DM`
2455bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2456bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2457bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24581f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24591f3379b2SToby Isaac 
24604165533cSJose E. Roman   Output Parameter:
24611f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24621f3379b2SToby Isaac 
24631f3379b2SToby Isaac   Level: developer
24641f3379b2SToby Isaac 
2465bb7acecfSBarry Smith   Note:
2466bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24671f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24681f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24691f3379b2SToby Isaac   slope-limiting reconstruction.
24701f3379b2SToby Isaac 
247173ff1848SBarry Smith   Developer Note:
2472bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2473bb7acecfSBarry Smith 
24741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24751f3379b2SToby Isaac @*/
2476d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2477d71ae5a4SJacob Faibussowitsch {
24781f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24791f3379b2SToby Isaac 
24801f3379b2SToby Isaac   PetscFunctionBegin;
24811f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24821f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24831f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24841f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24851f3379b2SToby Isaac 
24869566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24871f3379b2SToby Isaac   if (interpsol) {
24889566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24891f3379b2SToby Isaac   } else if (interp) {
24909566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
249198921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24931f3379b2SToby Isaac }
24941f3379b2SToby Isaac 
24951f3379b2SToby Isaac /*@
2496bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2497eb3f98d2SBarry Smith 
2498eb3f98d2SBarry Smith   Not Collective
2499eb3f98d2SBarry Smith 
2500eb3f98d2SBarry Smith   Input Parameter:
2501bb7acecfSBarry Smith . dm - the `DM` object
2502eb3f98d2SBarry Smith 
2503eb3f98d2SBarry Smith   Output Parameter:
2504eb3f98d2SBarry Smith . level - number of refinements
2505eb3f98d2SBarry Smith 
2506eb3f98d2SBarry Smith   Level: developer
2507eb3f98d2SBarry Smith 
2508bb7acecfSBarry Smith   Note:
2509bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2510bb7acecfSBarry Smith 
25111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2512eb3f98d2SBarry Smith @*/
2513d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2514d71ae5a4SJacob Faibussowitsch {
2515eb3f98d2SBarry Smith   PetscFunctionBegin;
2516eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2517eb3f98d2SBarry Smith   *level = dm->levelup;
25183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2519eb3f98d2SBarry Smith }
2520eb3f98d2SBarry Smith 
2521fef3a512SBarry Smith /*@
2522bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2523fef3a512SBarry Smith 
2524fef3a512SBarry Smith   Not Collective
2525fef3a512SBarry Smith 
2526d8d19677SJose E. Roman   Input Parameters:
2527bb7acecfSBarry Smith + dm    - the `DM` object
2528fef3a512SBarry Smith - level - number of refinements
2529fef3a512SBarry Smith 
2530fef3a512SBarry Smith   Level: advanced
2531fef3a512SBarry Smith 
253295452b02SPatrick Sanan   Notes:
2533bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2534fef3a512SBarry Smith 
2535bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2536bb7acecfSBarry Smith 
25371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2538fef3a512SBarry Smith @*/
2539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2540d71ae5a4SJacob Faibussowitsch {
2541fef3a512SBarry Smith   PetscFunctionBegin;
2542fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2543fef3a512SBarry Smith   dm->levelup = level;
25443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2545fef3a512SBarry Smith }
2546fef3a512SBarry Smith 
2547d410b0cfSMatthew G. Knepley /*@
2548bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2549d410b0cfSMatthew G. Knepley 
255020f4b53cSBarry Smith   Collective
2551d410b0cfSMatthew G. Knepley 
2552f1a722f8SMatthew G. Knepley   Input Parameters:
2553bb7acecfSBarry Smith + dm     - the `DM` object
2554d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2555d410b0cfSMatthew G. Knepley 
2556d410b0cfSMatthew G. Knepley   Output Parameter:
255720f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2558d410b0cfSMatthew G. Knepley 
2559d410b0cfSMatthew G. Knepley   Level: developer
2560d410b0cfSMatthew G. Knepley 
256120f4b53cSBarry Smith   Note:
256220f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
256320f4b53cSBarry Smith 
25641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2565d410b0cfSMatthew G. Knepley @*/
2566d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2567d71ae5a4SJacob Faibussowitsch {
2568d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2569d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2570dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2571d410b0cfSMatthew G. Knepley   if (*dme) {
2572d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25739566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2574d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25759566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2576d410b0cfSMatthew G. Knepley   }
25773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2578d410b0cfSMatthew G. Knepley }
2579d410b0cfSMatthew G. Knepley 
2580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2581d71ae5a4SJacob Faibussowitsch {
2582ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2583ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25844f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2585ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2587ca3d3a14SMatthew G. Knepley }
2588ca3d3a14SMatthew G. Knepley 
2589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2590d71ae5a4SJacob Faibussowitsch {
2591ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2592ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25934f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2594ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2596ca3d3a14SMatthew G. Knepley }
2597ca3d3a14SMatthew G. Knepley 
2598ca3d3a14SMatthew G. Knepley /*@
2599bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2600ca3d3a14SMatthew G. Knepley 
2601ca3d3a14SMatthew G. Knepley   Input Parameter:
260220f4b53cSBarry Smith . dm - The `DM`
2603ca3d3a14SMatthew G. Knepley 
2604ca3d3a14SMatthew G. Knepley   Output Parameter:
260520f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2606ca3d3a14SMatthew G. Knepley 
2607ca3d3a14SMatthew G. Knepley   Level: developer
2608ca3d3a14SMatthew G. Knepley 
26091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2610ca3d3a14SMatthew G. Knepley @*/
2611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2612d71ae5a4SJacob Faibussowitsch {
2613ca3d3a14SMatthew G. Knepley   Vec tv;
2614ca3d3a14SMatthew G. Knepley 
2615ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2616ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26174f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26189566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2619ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2621ca3d3a14SMatthew G. Knepley }
2622ca3d3a14SMatthew G. Knepley 
2623d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2624d71ae5a4SJacob Faibussowitsch {
2625ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2626ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2627ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2628ca3d3a14SMatthew G. Knepley 
2629ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26309566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26349566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26359566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2638ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2640ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2641ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2642ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26439566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2644ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26459566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26469566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2647ca3d3a14SMatthew G. Knepley     }
2648ca3d3a14SMatthew G. Knepley   }
26499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26509566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26519566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2652ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2653ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2655ca3d3a14SMatthew G. Knepley       if (dof) {
2656ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2657ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2658ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2659ca3d3a14SMatthew G. Knepley 
2660ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26619566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26629566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26639566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2664ca3d3a14SMatthew G. Knepley       }
2665ca3d3a14SMatthew G. Knepley     }
2666ca3d3a14SMatthew G. Knepley   }
26679566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2669ca3d3a14SMatthew G. Knepley }
2670ca3d3a14SMatthew G. Knepley 
2671d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2672d71ae5a4SJacob Faibussowitsch {
2673ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2674ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2675ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2676ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2677ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2678ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2679ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26809566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2682ca3d3a14SMatthew G. Knepley }
2683ca3d3a14SMatthew G. Knepley 
2684bb9467b5SJed Brown /*@C
2685bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2686baf369e7SPeter Brune 
268720f4b53cSBarry Smith   Logically Collective
2688baf369e7SPeter Brune 
26894165533cSJose E. Roman   Input Parameters:
2690bb7acecfSBarry Smith + dm        - the `DM`
2691bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2692bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
269320f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2694baf369e7SPeter Brune 
269520f4b53cSBarry Smith   Calling sequence of `beginhook`:
2696a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2697baf369e7SPeter Brune . g    - global vector
2698baf369e7SPeter Brune . mode - mode
2699baf369e7SPeter Brune . l    - local vector
2700baf369e7SPeter Brune - ctx  - optional user-defined function context
2701baf369e7SPeter Brune 
270220f4b53cSBarry Smith   Calling sequence of `endhook`:
2703a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2704a4e35b19SJacob Faibussowitsch . g    - global vector
2705a4e35b19SJacob Faibussowitsch . mode - mode
2706a4e35b19SJacob Faibussowitsch . l    - local vector
2707baf369e7SPeter Brune - ctx  - optional user-defined function context
2708baf369e7SPeter Brune 
2709baf369e7SPeter Brune   Level: advanced
2710baf369e7SPeter Brune 
2711bb7acecfSBarry Smith   Note:
2712bb7acecfSBarry 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.
2713bb7acecfSBarry Smith 
27141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2715baf369e7SPeter Brune @*/
2716a4e35b19SJacob 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)
2717d71ae5a4SJacob Faibussowitsch {
2718baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2719baf369e7SPeter Brune 
2720baf369e7SPeter Brune   PetscFunctionBegin;
2721baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2722baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27239566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2724baf369e7SPeter Brune   link->beginhook = beginhook;
2725baf369e7SPeter Brune   link->endhook   = endhook;
2726baf369e7SPeter Brune   link->ctx       = ctx;
27270298fd71SBarry Smith   link->next      = NULL;
2728baf369e7SPeter Brune   *p              = link;
27293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2730baf369e7SPeter Brune }
2731baf369e7SPeter Brune 
2732d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2733d71ae5a4SJacob Faibussowitsch {
27344c274da1SToby Isaac   Mat          cMat;
273579769bd5SJed Brown   Vec          cVec, cBias;
27364c274da1SToby Isaac   PetscSection section, cSec;
27374c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27384c274da1SToby Isaac 
27394c274da1SToby Isaac   PetscFunctionBegin;
2740a4e35b19SJacob Faibussowitsch   (void)g;
2741a4e35b19SJacob Faibussowitsch   (void)ctx;
27424c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27439566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27444c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27455db9a05bSToby Isaac     PetscInt nRows;
27465db9a05bSToby Isaac 
27479566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27483ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27499566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27509566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27519566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27529566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27544c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27559566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27564c274da1SToby Isaac       if (dof) {
27574c274da1SToby Isaac         PetscScalar *vals;
27589566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27599566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27604c274da1SToby Isaac       }
27614c274da1SToby Isaac     }
27629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27634c274da1SToby Isaac   }
27643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27654c274da1SToby Isaac }
27664c274da1SToby Isaac 
276747c6ae99SBarry Smith /*@
276801729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
276901729b5cSPatrick Sanan 
277020f4b53cSBarry Smith   Neighbor-wise Collective
277101729b5cSPatrick Sanan 
277201729b5cSPatrick Sanan   Input Parameters:
2773bb7acecfSBarry Smith + dm   - the `DM` object
277401729b5cSPatrick Sanan . g    - the global vector
2775bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
277601729b5cSPatrick Sanan - l    - the local vector
277701729b5cSPatrick Sanan 
277820f4b53cSBarry Smith   Level: beginner
277920f4b53cSBarry Smith 
278001729b5cSPatrick Sanan   Notes:
2781bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2782bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2783bb7acecfSBarry Smith 
2784bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
278501729b5cSPatrick Sanan 
27861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
278760225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2788bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
278901729b5cSPatrick Sanan @*/
2790d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2791d71ae5a4SJacob Faibussowitsch {
279201729b5cSPatrick Sanan   PetscFunctionBegin;
27939566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27949566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279601729b5cSPatrick Sanan }
279701729b5cSPatrick Sanan 
279801729b5cSPatrick Sanan /*@
279947c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
280047c6ae99SBarry Smith 
280120f4b53cSBarry Smith   Neighbor-wise Collective
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith   Input Parameters:
2804bb7acecfSBarry Smith + dm   - the `DM` object
280547c6ae99SBarry Smith . g    - the global vector
2806bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
280747c6ae99SBarry Smith - l    - the local vector
280847c6ae99SBarry Smith 
280901729b5cSPatrick Sanan   Level: intermediate
281047c6ae99SBarry Smith 
2811bb7acecfSBarry Smith   Notes:
2812bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2813bb7acecfSBarry Smith 
2814bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2815bb7acecfSBarry Smith 
2816bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2817bb7acecfSBarry Smith 
2818bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2819bb7acecfSBarry Smith 
282060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
282147c6ae99SBarry Smith @*/
2822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2823d71ae5a4SJacob Faibussowitsch {
28247128ae9fSMatthew G Knepley   PetscSF                 sf;
2825baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
282647c6ae99SBarry Smith 
282747c6ae99SBarry Smith   PetscFunctionBegin;
2828171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2829baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28301baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2831baf369e7SPeter Brune   }
28329566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28337128ae9fSMatthew G Knepley   if (sf) {
2834ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2835ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2836d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28377128ae9fSMatthew G Knepley 
28387a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28399566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28409566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28419566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28429566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28439566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28447128ae9fSMatthew G Knepley   } else {
2845fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28467128ae9fSMatthew G Knepley   }
28473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
284847c6ae99SBarry Smith }
284947c6ae99SBarry Smith 
285047c6ae99SBarry Smith /*@
285147c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
285247c6ae99SBarry Smith 
285320f4b53cSBarry Smith   Neighbor-wise Collective
285447c6ae99SBarry Smith 
285547c6ae99SBarry Smith   Input Parameters:
2856bb7acecfSBarry Smith + dm   - the `DM` object
285747c6ae99SBarry Smith . g    - the global vector
2858bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
285947c6ae99SBarry Smith - l    - the local vector
286047c6ae99SBarry Smith 
286101729b5cSPatrick Sanan   Level: intermediate
286247c6ae99SBarry Smith 
2863bb7acecfSBarry Smith   Note:
2864bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2865bb7acecfSBarry Smith 
286660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
286747c6ae99SBarry Smith @*/
2868d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2869d71ae5a4SJacob Faibussowitsch {
28707128ae9fSMatthew G Knepley   PetscSF                 sf;
2871ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2872ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2873ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2874baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2875d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
287647c6ae99SBarry Smith 
287747c6ae99SBarry Smith   PetscFunctionBegin;
2878171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28799566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28809566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28817128ae9fSMatthew G Knepley   if (sf) {
28827a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28837128ae9fSMatthew G Knepley 
28849566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28859566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28869566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28889566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28899566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28907128ae9fSMatthew G Knepley   } else {
2891fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28927128ae9fSMatthew G Knepley   }
28939566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2894baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28959566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2896baf369e7SPeter Brune   }
28973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
289847c6ae99SBarry Smith }
289947c6ae99SBarry Smith 
2900d4d07f1eSToby Isaac /*@C
2901d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2902d4d07f1eSToby Isaac 
290320f4b53cSBarry Smith   Logically Collective
2904d4d07f1eSToby Isaac 
29054165533cSJose E. Roman   Input Parameters:
2906bb7acecfSBarry Smith + dm        - the `DM`
2907bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2908bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
290920f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2910d4d07f1eSToby Isaac 
291120f4b53cSBarry Smith   Calling sequence of `beginhook`:
2912a4e35b19SJacob Faibussowitsch + global - global `DM`
2913d4d07f1eSToby Isaac . l      - local vector
2914d4d07f1eSToby Isaac . mode   - mode
2915d4d07f1eSToby Isaac . g      - global vector
2916d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2917d4d07f1eSToby Isaac 
291820f4b53cSBarry Smith   Calling sequence of `endhook`:
2919bb7acecfSBarry Smith + global - global `DM`
2920d4d07f1eSToby Isaac . l      - local vector
2921d4d07f1eSToby Isaac . mode   - mode
2922d4d07f1eSToby Isaac . g      - global vector
2923d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2924d4d07f1eSToby Isaac 
2925d4d07f1eSToby Isaac   Level: advanced
2926d4d07f1eSToby Isaac 
29271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2928d4d07f1eSToby Isaac @*/
2929a4e35b19SJacob 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)
2930d71ae5a4SJacob Faibussowitsch {
2931d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2932d4d07f1eSToby Isaac 
2933d4d07f1eSToby Isaac   PetscFunctionBegin;
2934d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2935d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29369566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2937d4d07f1eSToby Isaac   link->beginhook = beginhook;
2938d4d07f1eSToby Isaac   link->endhook   = endhook;
2939d4d07f1eSToby Isaac   link->ctx       = ctx;
2940d4d07f1eSToby Isaac   link->next      = NULL;
2941d4d07f1eSToby Isaac   *p              = link;
29423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2943d4d07f1eSToby Isaac }
2944d4d07f1eSToby Isaac 
2945d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2946d71ae5a4SJacob Faibussowitsch {
29474c274da1SToby Isaac   PetscFunctionBegin;
2948a4e35b19SJacob Faibussowitsch   (void)g;
2949a4e35b19SJacob Faibussowitsch   (void)ctx;
29504c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29512b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29522b6b7d09SToby Isaac     Mat          cMat;
29532b6b7d09SToby Isaac     Vec          cVec;
29545db9a05bSToby Isaac     PetscInt     nRows;
29552b6b7d09SToby Isaac     PetscSection section, cSec;
29562b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29572b6b7d09SToby Isaac 
29582b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29592b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29605db9a05bSToby Isaac 
29619566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29623ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29639566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29649566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29664c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29684c274da1SToby Isaac       if (dof) {
29694c274da1SToby Isaac         PetscInt     d;
29704c274da1SToby Isaac         PetscScalar *vals;
29719566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29729566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29734c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29744c274da1SToby Isaac          * we just extracted */
2975ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29764c274da1SToby Isaac       }
29774c274da1SToby Isaac     }
29789566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29799566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29804c274da1SToby Isaac   }
29813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29824c274da1SToby Isaac }
298301729b5cSPatrick Sanan /*@
298401729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
298501729b5cSPatrick Sanan 
298620f4b53cSBarry Smith   Neighbor-wise Collective
298701729b5cSPatrick Sanan 
298801729b5cSPatrick Sanan   Input Parameters:
2989bb7acecfSBarry Smith + dm   - the `DM` object
299001729b5cSPatrick Sanan . l    - the local vector
2991bb7acecfSBarry 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.
299201729b5cSPatrick Sanan - g    - the global vector
299301729b5cSPatrick Sanan 
299420f4b53cSBarry Smith   Level: beginner
299520f4b53cSBarry Smith 
299601729b5cSPatrick Sanan   Notes:
299701729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
2998bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
299901729b5cSPatrick Sanan 
3000bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3001bb7acecfSBarry Smith 
3002bb7acecfSBarry 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.
3003bb7acecfSBarry Smith 
3004bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
300501729b5cSPatrick Sanan 
30061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
300701729b5cSPatrick Sanan @*/
3008d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3009d71ae5a4SJacob Faibussowitsch {
301001729b5cSPatrick Sanan   PetscFunctionBegin;
30119566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30129566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
301401729b5cSPatrick Sanan }
30154c274da1SToby Isaac 
301647c6ae99SBarry Smith /*@
301701729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30189a42bb27SBarry Smith 
301920f4b53cSBarry Smith   Neighbor-wise Collective
30209a42bb27SBarry Smith 
30219a42bb27SBarry Smith   Input Parameters:
3022bb7acecfSBarry Smith + dm   - the `DM` object
3023f6813fd5SJed Brown . l    - the local vector
3024aa624791SPierre 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.
30251eb28f2eSBarry Smith - g    - the global vector
30269a42bb27SBarry Smith 
302720f4b53cSBarry Smith   Level: intermediate
302820f4b53cSBarry Smith 
302995452b02SPatrick Sanan   Notes:
3030bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3031bb7acecfSBarry Smith 
3032bb7acecfSBarry 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.
3033bb7acecfSBarry Smith 
3034bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3035bb7acecfSBarry Smith 
3036bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3037bb7acecfSBarry Smith 
3038bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30399a42bb27SBarry Smith 
30401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30419a42bb27SBarry Smith @*/
3042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3043d71ae5a4SJacob Faibussowitsch {
30447128ae9fSMatthew G Knepley   PetscSF                 sf;
304584330215SMatthew G. Knepley   PetscSection            s, gs;
3046d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3047ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3048ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3049ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3050fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3051d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30529a42bb27SBarry Smith 
30539a42bb27SBarry Smith   PetscFunctionBegin;
3054171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3055d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30561baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3057d4d07f1eSToby Isaac   }
30589566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30599566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30617128ae9fSMatthew G Knepley   switch (mode) {
30627128ae9fSMatthew G Knepley   case INSERT_VALUES:
30637128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3064d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3065d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3066d71ae5a4SJacob Faibussowitsch     break;
30677128ae9fSMatthew G Knepley   case ADD_VALUES:
30687128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3069d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3070d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3071d71ae5a4SJacob Faibussowitsch     break;
3072d71ae5a4SJacob Faibussowitsch   default:
3073d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30747128ae9fSMatthew G Knepley   }
3075ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30769566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3077ca3d3a14SMatthew G. Knepley     if (transform) {
30789566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30799566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30809566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30819566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3082fa88e482SJed Brown     } else if (isInsert) {
30839566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3084fa88e482SJed Brown     } else {
30859566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3086fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3087ca3d3a14SMatthew G. Knepley     }
3088fa88e482SJed Brown     if (s && isInsert) {
30899566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3090fa88e482SJed Brown     } else {
30919566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3092fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3093fa88e482SJed Brown     }
3094ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30959566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
309684330215SMatthew G. Knepley     } else if (s && isInsert) {
309784330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
309884330215SMatthew G. Knepley 
30999566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31009566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31019566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
310284330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3103b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
310484330215SMatthew G. Knepley 
31059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31109566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3111b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
311203442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31137a8be351SBarry 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);
3114b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3115b3b16f48SMatthew G. Knepley         if (!gcdof) {
311684330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3117b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3118b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
311984330215SMatthew G. Knepley           const PetscInt *cdofs;
312084330215SMatthew G. Knepley           PetscInt        cind = 0;
312184330215SMatthew G. Knepley 
31229566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3123b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31249371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31259371c9d4SSatish Balay               ++cind;
31269371c9d4SSatish Balay               continue;
31279371c9d4SSatish Balay             }
3128b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
312984330215SMatthew G. Knepley           }
31307a8be351SBarry 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);
313184330215SMatthew G. Knepley       }
3132ca3d3a14SMatthew G. Knepley     }
3133fa88e482SJed Brown     if (g_inplace) {
31349566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3135fa88e482SJed Brown     } else {
31369566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3137fa88e482SJed Brown     }
3138ca3d3a14SMatthew G. Knepley     if (transform) {
31399566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31409566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3141fa88e482SJed Brown     } else if (l_inplace) {
31429566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3143ca3d3a14SMatthew G. Knepley     } else {
31449566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3145ca3d3a14SMatthew G. Knepley     }
31467128ae9fSMatthew G Knepley   } else {
3147fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31487128ae9fSMatthew G Knepley   }
31493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31509a42bb27SBarry Smith }
31519a42bb27SBarry Smith 
31529a42bb27SBarry Smith /*@
31539a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
315447c6ae99SBarry Smith 
315520f4b53cSBarry Smith   Neighbor-wise Collective
315647c6ae99SBarry Smith 
315747c6ae99SBarry Smith   Input Parameters:
3158bb7acecfSBarry Smith + dm   - the `DM` object
3159f6813fd5SJed Brown . l    - the local vector
3160bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3161f6813fd5SJed Brown - g    - the global vector
316247c6ae99SBarry Smith 
316301729b5cSPatrick Sanan   Level: intermediate
316447c6ae99SBarry Smith 
3165bb7acecfSBarry Smith   Note:
3166bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3167bb7acecfSBarry Smith 
316860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
316947c6ae99SBarry Smith @*/
3170d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3171d71ae5a4SJacob Faibussowitsch {
31727128ae9fSMatthew G Knepley   PetscSF                 sf;
317384330215SMatthew G. Knepley   PetscSection            s;
3174d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3175ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
317647c6ae99SBarry Smith 
317747c6ae99SBarry Smith   PetscFunctionBegin;
3178171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31799566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31809566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31817128ae9fSMatthew G Knepley   switch (mode) {
31827128ae9fSMatthew G Knepley   case INSERT_VALUES:
3183d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3184d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3185d71ae5a4SJacob Faibussowitsch     break;
31867128ae9fSMatthew G Knepley   case ADD_VALUES:
3187d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3188d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3189d71ae5a4SJacob Faibussowitsch     break;
3190d71ae5a4SJacob Faibussowitsch   default:
3191d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31927128ae9fSMatthew G Knepley   }
319384330215SMatthew G. Knepley   if (sf && !isInsert) {
3194ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3195ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3196ca3d3a14SMatthew G. Knepley     Vec                tmpl;
319784330215SMatthew G. Knepley 
31989566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3199ca3d3a14SMatthew G. Knepley     if (transform) {
32009566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32019566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3202ca3d3a14SMatthew G. Knepley     } else {
32039566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3204ca3d3a14SMatthew G. Knepley     }
32059566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32069566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3207ca3d3a14SMatthew G. Knepley     if (transform) {
32089566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32099566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3210ca3d3a14SMatthew G. Knepley     } else {
32119566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3212ca3d3a14SMatthew G. Knepley     }
32139566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
321484330215SMatthew G. Knepley   } else if (s && isInsert) {
32157128ae9fSMatthew G Knepley   } else {
3216fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32177128ae9fSMatthew G Knepley   }
3218d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32199566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3220d4d07f1eSToby Isaac   }
32213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
322247c6ae99SBarry Smith }
322347c6ae99SBarry Smith 
3224f089877aSRichard Tran Mills /*@
3225a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3226a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3227a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3228f089877aSRichard Tran Mills 
322920f4b53cSBarry Smith   Neighbor-wise Collective
3230f089877aSRichard Tran Mills 
3231f089877aSRichard Tran Mills   Input Parameters:
3232bb7acecfSBarry Smith + dm   - the `DM` object
3233bc0a1609SRichard Tran Mills . g    - the original local vector
3234bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3235f089877aSRichard Tran Mills 
3236bc0a1609SRichard Tran Mills   Output Parameter:
3237bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3238f089877aSRichard Tran Mills 
3239f089877aSRichard Tran Mills   Level: intermediate
3240f089877aSRichard Tran Mills 
324173ff1848SBarry Smith   Note:
3242a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3243a4e35b19SJacob Faibussowitsch 
324460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3245f089877aSRichard Tran Mills @*/
3246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3247d71ae5a4SJacob Faibussowitsch {
3248f089877aSRichard Tran Mills   PetscFunctionBegin;
3249f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32509f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32519f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32529f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3254f089877aSRichard Tran Mills }
3255f089877aSRichard Tran Mills 
3256f089877aSRichard Tran Mills /*@
3257bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3258bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3259f089877aSRichard Tran Mills 
326020f4b53cSBarry Smith   Neighbor-wise Collective
3261f089877aSRichard Tran Mills 
3262f089877aSRichard Tran Mills   Input Parameters:
326360225df5SJacob Faibussowitsch + dm   - the `DM` object
3264bc0a1609SRichard Tran Mills . g    - the original local vector
3265bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3266f089877aSRichard Tran Mills 
3267bc0a1609SRichard Tran Mills   Output Parameter:
3268bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3269f089877aSRichard Tran Mills 
3270f089877aSRichard Tran Mills   Level: intermediate
3271f089877aSRichard Tran Mills 
327260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3273f089877aSRichard Tran Mills @*/
3274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3275d71ae5a4SJacob Faibussowitsch {
3276f089877aSRichard Tran Mills   PetscFunctionBegin;
3277f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32789f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32799f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32809f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3282f089877aSRichard Tran Mills }
3283f089877aSRichard Tran Mills 
328447c6ae99SBarry Smith /*@
3285bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
328647c6ae99SBarry Smith 
328720f4b53cSBarry Smith   Collective
328847c6ae99SBarry Smith 
3289d8d19677SJose E. Roman   Input Parameters:
3290bb7acecfSBarry Smith + dm   - the `DM` object
329120f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
329247c6ae99SBarry Smith 
329347c6ae99SBarry Smith   Output Parameter:
3294bb7acecfSBarry Smith . dmc - the coarsened `DM`
329547c6ae99SBarry Smith 
329647c6ae99SBarry Smith   Level: developer
329747c6ae99SBarry Smith 
329873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
329973ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
330047c6ae99SBarry Smith @*/
3301d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3302d71ae5a4SJacob Faibussowitsch {
3303b17ce1afSJed Brown   DMCoarsenHookLink link;
330447c6ae99SBarry Smith 
330547c6ae99SBarry Smith   PetscFunctionBegin;
3306171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3308dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3309b9d85ea2SLisandro Dalcin   if (*dmc) {
3310a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33119566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
331243842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33139566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3314644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33150598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3316656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33179566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3318b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33199566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3320b17ce1afSJed Brown     }
3321b9d85ea2SLisandro Dalcin   }
33229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33237a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3325b17ce1afSJed Brown }
3326b17ce1afSJed Brown 
3327bb9467b5SJed Brown /*@C
3328b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3329b17ce1afSJed Brown 
333020f4b53cSBarry Smith   Logically Collective; No Fortran Support
3331b17ce1afSJed Brown 
33324165533cSJose E. Roman   Input Parameters:
3333bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3334b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3335bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
333620f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3337b17ce1afSJed Brown 
333820f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3339bb7acecfSBarry Smith + fine   - fine level `DM`
3340bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3341b17ce1afSJed Brown - ctx    - optional user-defined function context
3342b17ce1afSJed Brown 
334320f4b53cSBarry Smith   Calling sequence of `restricthook`:
3344bb7acecfSBarry Smith + fine      - fine level `DM`
3345bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3346c833c3b5SJed Brown . rscale    - scaling vector for restriction
3347c833c3b5SJed Brown . inject    - matrix restricting by injection
3348b17ce1afSJed Brown . coarse    - coarse level DM to update
3349b17ce1afSJed Brown - ctx       - optional user-defined function context
3350b17ce1afSJed Brown 
3351b17ce1afSJed Brown   Level: advanced
3352b17ce1afSJed Brown 
3353b17ce1afSJed Brown   Notes:
3354bb7acecfSBarry 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`.
3355b17ce1afSJed Brown 
3356b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3357b17ce1afSJed Brown 
3358b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3359bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3360b17ce1afSJed Brown 
3361bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3362bb7acecfSBarry Smith 
33631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3364b17ce1afSJed Brown @*/
3365a4e35b19SJacob 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)
3366d71ae5a4SJacob Faibussowitsch {
3367b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3368b17ce1afSJed Brown 
3369b17ce1afSJed Brown   PetscFunctionBegin;
3370b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33711e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33723ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33731e3d8eccSJed Brown   }
33749566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3375b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3376b17ce1afSJed Brown   link->restricthook = restricthook;
3377b17ce1afSJed Brown   link->ctx          = ctx;
33780298fd71SBarry Smith   link->next         = NULL;
3379b17ce1afSJed Brown   *p                 = link;
33803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3381b17ce1afSJed Brown }
3382b17ce1afSJed Brown 
3383dc822a44SJed Brown /*@C
3384bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3385dc822a44SJed Brown 
338620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3387dc822a44SJed Brown 
33884165533cSJose E. Roman   Input Parameters:
3389bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3390dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3391bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
339220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3393dc822a44SJed Brown 
3394dc822a44SJed Brown   Level: advanced
3395dc822a44SJed Brown 
339673ff1848SBarry Smith   Notes:
339773ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
339873ff1848SBarry Smith 
339973ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3400dc822a44SJed Brown 
34011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3402dc822a44SJed Brown @*/
3403d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3404d71ae5a4SJacob Faibussowitsch {
3405dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3406dc822a44SJed Brown 
3407dc822a44SJed Brown   PetscFunctionBegin;
3408dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3409dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3410dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3411dc822a44SJed Brown       link = *p;
3412dc822a44SJed Brown       *p   = link->next;
34139566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3414dc822a44SJed Brown       break;
3415dc822a44SJed Brown     }
3416dc822a44SJed Brown   }
34173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3418dc822a44SJed Brown }
3419dc822a44SJed Brown 
3420b17ce1afSJed Brown /*@
3421bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3422b17ce1afSJed Brown 
3423b17ce1afSJed Brown   Collective if any hooks are
3424b17ce1afSJed Brown 
34254165533cSJose E. Roman   Input Parameters:
3426bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3427bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3428e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3429bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
343020f4b53cSBarry Smith - coarse  - coarser `DM` to update
3431b17ce1afSJed Brown 
3432b17ce1afSJed Brown   Level: developer
3433b17ce1afSJed Brown 
343473ff1848SBarry Smith   Developer Note:
3435bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3436bb7acecfSBarry Smith 
34371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3438b17ce1afSJed Brown @*/
3439d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3440d71ae5a4SJacob Faibussowitsch {
3441b17ce1afSJed Brown   DMCoarsenHookLink link;
3442b17ce1afSJed Brown 
3443b17ce1afSJed Brown   PetscFunctionBegin;
3444b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34451baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3446b17ce1afSJed Brown   }
34473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
344847c6ae99SBarry Smith }
344947c6ae99SBarry Smith 
3450bb9467b5SJed Brown /*@C
345173ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34525dbd56e3SPeter Brune 
345320f4b53cSBarry Smith   Logically Collective; No Fortran Support
34545dbd56e3SPeter Brune 
34554165533cSJose E. Roman   Input Parameters:
3456bb7acecfSBarry Smith + global       - global `DM`
3457bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34585dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
345920f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34605dbd56e3SPeter Brune 
346120f4b53cSBarry Smith   Calling sequence of `ddhook`:
3462bb7acecfSBarry Smith + global - global `DM`
346373ff1848SBarry Smith . block  - subdomain `DM`
3464ec4806b8SPeter Brune - ctx    - optional user-defined function context
3465ec4806b8SPeter Brune 
346620f4b53cSBarry Smith   Calling sequence of `restricthook`:
3467bb7acecfSBarry Smith + global - global `DM`
346873ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
346973ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
347073ff1848SBarry Smith . block  - subdomain `DM`
34715dbd56e3SPeter Brune - ctx    - optional user-defined function context
34725dbd56e3SPeter Brune 
34735dbd56e3SPeter Brune   Level: advanced
34745dbd56e3SPeter Brune 
34755dbd56e3SPeter Brune   Notes:
347673ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34775dbd56e3SPeter Brune 
34785dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34795dbd56e3SPeter Brune 
34805dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3481bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34825dbd56e3SPeter Brune 
348373ff1848SBarry Smith   Developer Note:
348473ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
348573ff1848SBarry Smith 
348673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
34875dbd56e3SPeter Brune @*/
3488a4e35b19SJacob 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)
3489d71ae5a4SJacob Faibussowitsch {
3490be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34915dbd56e3SPeter Brune 
34925dbd56e3SPeter Brune   PetscFunctionBegin;
34935dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3494b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34953ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3496b3a6b972SJed Brown   }
34979566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34985dbd56e3SPeter Brune   link->restricthook = restricthook;
3499be081cd6SPeter Brune   link->ddhook       = ddhook;
35005dbd56e3SPeter Brune   link->ctx          = ctx;
35010298fd71SBarry Smith   link->next         = NULL;
35025dbd56e3SPeter Brune   *p                 = link;
35033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35045dbd56e3SPeter Brune }
35055dbd56e3SPeter Brune 
3506b3a6b972SJed Brown /*@C
350773ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3508b3a6b972SJed Brown 
350920f4b53cSBarry Smith   Logically Collective; No Fortran Support
3510b3a6b972SJed Brown 
35114165533cSJose E. Roman   Input Parameters:
3512bb7acecfSBarry Smith + global       - global `DM`
3513bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3514b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
351520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3516b3a6b972SJed Brown 
3517b3a6b972SJed Brown   Level: advanced
3518b3a6b972SJed Brown 
351973ff1848SBarry Smith   Note:
352073ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
352173ff1848SBarry Smith 
352273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
352373ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3524b3a6b972SJed Brown @*/
3525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3526d71ae5a4SJacob Faibussowitsch {
3527b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3528b3a6b972SJed Brown 
3529b3a6b972SJed Brown   PetscFunctionBegin;
3530b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3531b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3532b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3533b3a6b972SJed Brown       link = *p;
3534b3a6b972SJed Brown       *p   = link->next;
35359566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3536b3a6b972SJed Brown       break;
3537b3a6b972SJed Brown     }
3538b3a6b972SJed Brown   }
35393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3540b3a6b972SJed Brown }
3541b3a6b972SJed Brown 
35425dbd56e3SPeter Brune /*@
354373ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35445dbd56e3SPeter Brune 
35455dbd56e3SPeter Brune   Collective if any hooks are
35465dbd56e3SPeter Brune 
35474165533cSJose E. Roman   Input Parameters:
3548a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3549a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3550a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3551a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35525dbd56e3SPeter Brune 
35535dbd56e3SPeter Brune   Level: developer
35545dbd56e3SPeter Brune 
355573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35565dbd56e3SPeter Brune @*/
3557d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3558d71ae5a4SJacob Faibussowitsch {
3559be081cd6SPeter Brune   DMSubDomainHookLink link;
35605dbd56e3SPeter Brune 
35615dbd56e3SPeter Brune   PetscFunctionBegin;
3562be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35631baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35645dbd56e3SPeter Brune   }
35653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35665dbd56e3SPeter Brune }
35675dbd56e3SPeter Brune 
35685fe1f584SPeter Brune /*@
3569bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35705fe1f584SPeter Brune 
35715fe1f584SPeter Brune   Not Collective
35725fe1f584SPeter Brune 
35735fe1f584SPeter Brune   Input Parameter:
3574bb7acecfSBarry Smith . dm - the `DM` object
35755fe1f584SPeter Brune 
35765fe1f584SPeter Brune   Output Parameter:
35776a7d9d85SPeter Brune . level - number of coarsenings
35785fe1f584SPeter Brune 
35795fe1f584SPeter Brune   Level: developer
35805fe1f584SPeter Brune 
35811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35825fe1f584SPeter Brune @*/
3583d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3584d71ae5a4SJacob Faibussowitsch {
35855fe1f584SPeter Brune   PetscFunctionBegin;
35865fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35874f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35885fe1f584SPeter Brune   *level = dm->leveldown;
35893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35905fe1f584SPeter Brune }
35915fe1f584SPeter Brune 
35929a64c4a8SMatthew G. Knepley /*@
3593bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35949a64c4a8SMatthew G. Knepley 
359520f4b53cSBarry Smith   Collective
35969a64c4a8SMatthew G. Knepley 
35979a64c4a8SMatthew G. Knepley   Input Parameters:
3598bb7acecfSBarry Smith + dm    - the `DM` object
35999a64c4a8SMatthew G. Knepley - level - number of coarsenings
36009a64c4a8SMatthew G. Knepley 
36019a64c4a8SMatthew G. Knepley   Level: developer
36029a64c4a8SMatthew G. Knepley 
3603bb7acecfSBarry Smith   Note:
3604bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3605bb7acecfSBarry Smith 
360642747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36079a64c4a8SMatthew G. Knepley @*/
3608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3609d71ae5a4SJacob Faibussowitsch {
36109a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36119a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36129a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36149a64c4a8SMatthew G. Knepley }
36159a64c4a8SMatthew G. Knepley 
3616cc4c1da9SBarry Smith /*@
3617bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
361847c6ae99SBarry Smith 
361920f4b53cSBarry Smith   Collective
362047c6ae99SBarry Smith 
3621d8d19677SJose E. Roman   Input Parameters:
3622bb7acecfSBarry Smith + dm      - the `DM` object
362347c6ae99SBarry Smith - nlevels - the number of levels of refinement
362447c6ae99SBarry Smith 
362547c6ae99SBarry Smith   Output Parameter:
3626bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
362747c6ae99SBarry Smith 
362847c6ae99SBarry Smith   Level: developer
362947c6ae99SBarry Smith 
36301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363147c6ae99SBarry Smith @*/
3632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3633d71ae5a4SJacob Faibussowitsch {
363447c6ae99SBarry Smith   PetscFunctionBegin;
3635171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36367a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36373ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36384f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3639213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
364047c6ae99SBarry Smith     PetscInt i;
364147c6ae99SBarry Smith 
36429566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
364348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3644213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364647c6ae99SBarry Smith }
364747c6ae99SBarry Smith 
3648cc4c1da9SBarry Smith /*@
3649bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
365047c6ae99SBarry Smith 
365120f4b53cSBarry Smith   Collective
365247c6ae99SBarry Smith 
3653d8d19677SJose E. Roman   Input Parameters:
3654bb7acecfSBarry Smith + dm      - the `DM` object
365547c6ae99SBarry Smith - nlevels - the number of levels of coarsening
365647c6ae99SBarry Smith 
365747c6ae99SBarry Smith   Output Parameter:
3658bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
365947c6ae99SBarry Smith 
366047c6ae99SBarry Smith   Level: developer
366147c6ae99SBarry Smith 
36621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
366347c6ae99SBarry Smith @*/
3664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3665d71ae5a4SJacob Faibussowitsch {
366647c6ae99SBarry Smith   PetscFunctionBegin;
3667171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36687a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36693ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36704f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3671213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
367247c6ae99SBarry Smith     PetscInt i;
367347c6ae99SBarry Smith 
36749566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
367548a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3676213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
367847c6ae99SBarry Smith }
367947c6ae99SBarry Smith 
36801a266240SBarry Smith /*@C
3681bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36821a266240SBarry Smith 
3683bb7acecfSBarry Smith   Logically Collective if the function is collective
36841a266240SBarry Smith 
36851a266240SBarry Smith   Input Parameters:
3686bb7acecfSBarry Smith + dm      - the `DM` object
36871a266240SBarry Smith - destroy - the destroy function
36881a266240SBarry Smith 
36891a266240SBarry Smith   Level: intermediate
36901a266240SBarry Smith 
36911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3692f07f9ceaSJed Brown @*/
3693d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3694d71ae5a4SJacob Faibussowitsch {
36951a266240SBarry Smith   PetscFunctionBegin;
3696171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36971a266240SBarry Smith   dm->ctxdestroy = destroy;
36983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36991a266240SBarry Smith }
37001a266240SBarry Smith 
3701b07ff414SBarry Smith /*@
3702bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
370347c6ae99SBarry Smith 
370447c6ae99SBarry Smith   Not Collective
370547c6ae99SBarry Smith 
370647c6ae99SBarry Smith   Input Parameters:
3707bb7acecfSBarry Smith + dm  - the `DM` object
370847c6ae99SBarry Smith - ctx - the user context
370947c6ae99SBarry Smith 
371047c6ae99SBarry Smith   Level: intermediate
371147c6ae99SBarry Smith 
3712e33b3f0fSStefano Zampini   Notes:
371335cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3714e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3715e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3716bb7acecfSBarry Smith 
371760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
371847c6ae99SBarry Smith @*/
3719d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3720d71ae5a4SJacob Faibussowitsch {
372147c6ae99SBarry Smith   PetscFunctionBegin;
3722171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372347c6ae99SBarry Smith   dm->ctx = ctx;
37243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
372547c6ae99SBarry Smith }
372647c6ae99SBarry Smith 
372747c6ae99SBarry Smith /*@
3728bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
372947c6ae99SBarry Smith 
373047c6ae99SBarry Smith   Not Collective
373147c6ae99SBarry Smith 
373247c6ae99SBarry Smith   Input Parameter:
3733bb7acecfSBarry Smith . dm - the `DM` object
373447c6ae99SBarry Smith 
373547c6ae99SBarry Smith   Output Parameter:
373647c6ae99SBarry Smith . ctx - the user context
373747c6ae99SBarry Smith 
373847c6ae99SBarry Smith   Level: intermediate
373947c6ae99SBarry Smith 
3740bb7acecfSBarry Smith   Note:
374135cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3742bb7acecfSBarry Smith 
374342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
374447c6ae99SBarry Smith @*/
3745d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3746d71ae5a4SJacob Faibussowitsch {
374747c6ae99SBarry Smith   PetscFunctionBegin;
3748171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37491b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
375147c6ae99SBarry Smith }
375247c6ae99SBarry Smith 
375308da532bSDmitry Karpeev /*@C
3754bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
375508da532bSDmitry Karpeev 
375620f4b53cSBarry Smith   Logically Collective
375708da532bSDmitry Karpeev 
3758d8d19677SJose E. Roman   Input Parameters:
375908da532bSDmitry Karpeev + dm - the DM object
376020f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
376108da532bSDmitry Karpeev 
376208da532bSDmitry Karpeev   Level: intermediate
376308da532bSDmitry Karpeev 
37641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3765db781477SPatrick Sanan          `DMSetJacobian()`
376608da532bSDmitry Karpeev @*/
3767d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3768d71ae5a4SJacob Faibussowitsch {
376908da532bSDmitry Karpeev   PetscFunctionBegin;
37705a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377308da532bSDmitry Karpeev }
377408da532bSDmitry Karpeev 
377508da532bSDmitry Karpeev /*@
3776bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
377708da532bSDmitry Karpeev 
377808da532bSDmitry Karpeev   Not Collective
377908da532bSDmitry Karpeev 
378008da532bSDmitry Karpeev   Input Parameter:
3781bb7acecfSBarry Smith . dm - the `DM` object to destroy
378208da532bSDmitry Karpeev 
378308da532bSDmitry Karpeev   Output Parameter:
3784bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
378508da532bSDmitry Karpeev 
378608da532bSDmitry Karpeev   Level: developer
378708da532bSDmitry Karpeev 
37881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
378908da532bSDmitry Karpeev @*/
3790d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3791d71ae5a4SJacob Faibussowitsch {
379208da532bSDmitry Karpeev   PetscFunctionBegin;
37935a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37944f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
379508da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379708da532bSDmitry Karpeev }
379808da532bSDmitry Karpeev 
3799cc4c1da9SBarry Smith /*@
3800bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
380108da532bSDmitry Karpeev 
380220f4b53cSBarry Smith   Logically Collective
380308da532bSDmitry Karpeev 
3804f899ff85SJose E. Roman   Input Parameter:
3805bb7acecfSBarry Smith . dm - the `DM` object
380608da532bSDmitry Karpeev 
380760225df5SJacob Faibussowitsch   Output Parameters:
380808da532bSDmitry Karpeev + xl - lower bound
380908da532bSDmitry Karpeev - xu - upper bound
381008da532bSDmitry Karpeev 
3811907376e6SBarry Smith   Level: advanced
3812907376e6SBarry Smith 
381320f4b53cSBarry Smith   Note:
381495452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
381508da532bSDmitry Karpeev 
38161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
381708da532bSDmitry Karpeev @*/
3818d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3819d71ae5a4SJacob Faibussowitsch {
382008da532bSDmitry Karpeev   PetscFunctionBegin;
38215a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38235a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3824dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
382608da532bSDmitry Karpeev }
382708da532bSDmitry Karpeev 
3828b0ae01b7SPeter Brune /*@
3829bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3830b0ae01b7SPeter Brune 
3831b0ae01b7SPeter Brune   Not Collective
3832b0ae01b7SPeter Brune 
3833b0ae01b7SPeter Brune   Input Parameter:
3834b0ae01b7SPeter Brune . dm - the DM object
3835b0ae01b7SPeter Brune 
3836b0ae01b7SPeter Brune   Output Parameter:
3837bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3838b0ae01b7SPeter Brune 
3839b0ae01b7SPeter Brune   Level: developer
3840b0ae01b7SPeter Brune 
38411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3842b0ae01b7SPeter Brune @*/
3843d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3844d71ae5a4SJacob Faibussowitsch {
3845b0ae01b7SPeter Brune   PetscFunctionBegin;
38465a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38474f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3848b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3850b0ae01b7SPeter Brune }
3851b0ae01b7SPeter Brune 
38523ad4599aSBarry Smith /*@
3853bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38543ad4599aSBarry Smith 
38553ad4599aSBarry Smith   Not Collective
38563ad4599aSBarry Smith 
38573ad4599aSBarry Smith   Input Parameter:
3858bb7acecfSBarry Smith . dm - the `DM` object
38593ad4599aSBarry Smith 
38603ad4599aSBarry Smith   Output Parameter:
3861bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38623ad4599aSBarry Smith 
38633ad4599aSBarry Smith   Level: developer
38643ad4599aSBarry Smith 
38651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38663ad4599aSBarry Smith @*/
3867d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3868d71ae5a4SJacob Faibussowitsch {
38693ad4599aSBarry Smith   PetscFunctionBegin;
38705a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38714f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38723ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38743ad4599aSBarry Smith }
38753ad4599aSBarry Smith 
3876a7058e45SLawrence Mitchell /*@
3877bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3878a7058e45SLawrence Mitchell 
3879a7058e45SLawrence Mitchell   Not Collective
3880a7058e45SLawrence Mitchell 
3881a7058e45SLawrence Mitchell   Input Parameter:
3882bb7acecfSBarry Smith . dm - the `DM` object
3883a7058e45SLawrence Mitchell 
3884a7058e45SLawrence Mitchell   Output Parameter:
3885bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3886a7058e45SLawrence Mitchell 
3887a7058e45SLawrence Mitchell   Level: developer
3888a7058e45SLawrence Mitchell 
38891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3890a7058e45SLawrence Mitchell @*/
3891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3892d71ae5a4SJacob Faibussowitsch {
3893a7058e45SLawrence Mitchell   PetscFunctionBegin;
38945a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38954f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3896dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3897ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3899a7058e45SLawrence Mitchell }
3900a7058e45SLawrence Mitchell 
39010298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3902264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3903264ace61SBarry Smith 
3904cc4c1da9SBarry Smith /*@
3905bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3906264ace61SBarry Smith 
390720f4b53cSBarry Smith   Collective
3908264ace61SBarry Smith 
3909264ace61SBarry Smith   Input Parameters:
3910bb7acecfSBarry Smith + dm     - The `DM` object
3911bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3912264ace61SBarry Smith 
3913264ace61SBarry Smith   Options Database Key:
3914bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3915264ace61SBarry Smith 
3916264ace61SBarry Smith   Level: intermediate
3917264ace61SBarry Smith 
3918bb7acecfSBarry Smith   Note:
39190462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3920bb7acecfSBarry Smith 
39211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3922264ace61SBarry Smith @*/
3923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3924d71ae5a4SJacob Faibussowitsch {
3925264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3926264ace61SBarry Smith   PetscBool match;
3927264ace61SBarry Smith 
3928264ace61SBarry Smith   PetscFunctionBegin;
3929264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39309566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39313ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3932264ace61SBarry Smith 
39339566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39349566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39357a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3936264ace61SBarry Smith 
3937dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39389566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39399566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39409566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3942264ace61SBarry Smith }
3943264ace61SBarry Smith 
3944cc4c1da9SBarry Smith /*@
3945bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3946264ace61SBarry Smith 
3947264ace61SBarry Smith   Not Collective
3948264ace61SBarry Smith 
3949264ace61SBarry Smith   Input Parameter:
3950bb7acecfSBarry Smith . dm - The `DM`
3951264ace61SBarry Smith 
3952264ace61SBarry Smith   Output Parameter:
3953bb7acecfSBarry Smith . type - The `DMType` name
3954264ace61SBarry Smith 
3955264ace61SBarry Smith   Level: intermediate
3956264ace61SBarry Smith 
39571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3958264ace61SBarry Smith @*/
3959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3960d71ae5a4SJacob Faibussowitsch {
3961264ace61SBarry Smith   PetscFunctionBegin;
3962264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39634f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39649566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3965264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3967264ace61SBarry Smith }
3968264ace61SBarry Smith 
39695d83a8b1SBarry Smith /*@
3970bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
397167a56275SMatthew G Knepley 
397220f4b53cSBarry Smith   Collective
397367a56275SMatthew G Knepley 
397467a56275SMatthew G Knepley   Input Parameters:
3975bb7acecfSBarry Smith + dm      - the `DM`
3976bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
397767a56275SMatthew G Knepley 
397867a56275SMatthew G Knepley   Output Parameter:
3979bb7acecfSBarry Smith . M - pointer to new `DM`
398067a56275SMatthew G Knepley 
398120f4b53cSBarry Smith   Level: intermediate
398220f4b53cSBarry Smith 
398373ff1848SBarry Smith   Note:
3984bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3985bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3986bb7acecfSBarry Smith   of the input `DM`.
398767a56275SMatthew G Knepley 
39881cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
398967a56275SMatthew G Knepley @*/
3990d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3991d71ae5a4SJacob Faibussowitsch {
399267a56275SMatthew G Knepley   DM        B;
399367a56275SMatthew G Knepley   char      convname[256];
3994c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
399567a56275SMatthew G Knepley 
399667a56275SMatthew G Knepley   PetscFunctionBegin;
399767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399867a56275SMatthew G Knepley   PetscValidType(dm, 1);
39994f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40009566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40019566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4002c067b6caSMatthew G. Knepley   if (sametype) {
4003c067b6caSMatthew G. Knepley     *M = dm;
40049566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40053ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4006c067b6caSMatthew G. Knepley   } else {
40070298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
400867a56275SMatthew G Knepley 
400967a56275SMatthew G Knepley     /*
401067a56275SMatthew G Knepley        Order of precedence:
401167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
401267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
401367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
401467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
401567a56275SMatthew G Knepley        5) Use a really basic converter.
401667a56275SMatthew G Knepley     */
401767a56275SMatthew G Knepley 
401867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40199566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40209566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40219566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40229566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40239566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40249566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
402567a56275SMatthew G Knepley     if (conv) goto foundconv;
402667a56275SMatthew G Knepley 
402767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40289566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40299566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40309566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40319566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40329566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40339566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40349566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40359566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
403667a56275SMatthew G Knepley     if (conv) {
40379566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
403867a56275SMatthew G Knepley       goto foundconv;
403967a56275SMatthew G Knepley     }
404067a56275SMatthew G Knepley 
404167a56275SMatthew G Knepley #if 0
404267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
404367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40449566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
404567a56275SMatthew G Knepley     if (conv) goto foundconv;
404667a56275SMatthew G Knepley 
404767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
404867a56275SMatthew G Knepley     if (dm->ops->convert) {
404967a56275SMatthew G Knepley       conv = dm->ops->convert;
405067a56275SMatthew G Knepley     }
405167a56275SMatthew G Knepley     if (conv) goto foundconv;
405267a56275SMatthew G Knepley #endif
405367a56275SMatthew G Knepley 
405467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
405598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
405667a56275SMatthew G Knepley 
405767a56275SMatthew G Knepley   foundconv:
40589566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40599566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
406012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
406190b157c4SStefano Zampini     {
40624fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40636858538eSMatthew G. Knepley 
40644fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40654fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4066c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40679566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40689566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40699566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40709566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
407112fa691eSMatthew G. Knepley     }
40729566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
407367a56275SMatthew G Knepley   }
40749566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
407667a56275SMatthew G Knepley }
4077264ace61SBarry Smith 
4078264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4079264ace61SBarry Smith 
4080264ace61SBarry Smith /*@C
4081bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40821c84c290SBarry Smith 
4083cc4c1da9SBarry Smith   Not Collective, No Fortran Support
40841c84c290SBarry Smith 
40851c84c290SBarry Smith   Input Parameters:
408620f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
408720f4b53cSBarry Smith - function - The creation routine itself
408820f4b53cSBarry Smith 
408920f4b53cSBarry Smith   Level: advanced
40901c84c290SBarry Smith 
409173ff1848SBarry Smith   Note:
409220f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40931c84c290SBarry Smith 
409460225df5SJacob Faibussowitsch   Example Usage:
40951c84c290SBarry Smith .vb
4096bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40971c84c290SBarry Smith .ve
40981c84c290SBarry Smith 
409920f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41001c84c290SBarry Smith .vb
41011c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41021c84c290SBarry Smith     DMSetType(DM,"my_da");
41031c84c290SBarry Smith .ve
41041c84c290SBarry Smith   or at runtime via the option
41051c84c290SBarry Smith .vb
41061c84c290SBarry Smith     -da_type my_da
41071c84c290SBarry Smith .ve
4108264ace61SBarry Smith 
41091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4110264ace61SBarry Smith @*/
4111d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4112d71ae5a4SJacob Faibussowitsch {
4113264ace61SBarry Smith   PetscFunctionBegin;
41149566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41159566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4117264ace61SBarry Smith }
4118264ace61SBarry Smith 
4119ffeef943SBarry Smith /*@
4120bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4121b859378eSBarry Smith 
412220f4b53cSBarry Smith   Collective
4123b859378eSBarry Smith 
4124b859378eSBarry Smith   Input Parameters:
4125bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4126bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4127bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4128bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4129b859378eSBarry Smith 
4130b859378eSBarry Smith   Level: intermediate
4131b859378eSBarry Smith 
4132b859378eSBarry Smith   Notes:
413355849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4134b859378eSBarry Smith 
4135bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4136bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4137bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4138cd7e8a5eSksagiyam 
41391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4140b859378eSBarry Smith @*/
4141d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4142d71ae5a4SJacob Faibussowitsch {
41439331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4144b859378eSBarry Smith 
4145b859378eSBarry Smith   PetscFunctionBegin;
4146b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4147b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41489566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41499566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41519566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41529331c7a4SMatthew G. Knepley   if (isbinary) {
41539331c7a4SMatthew G. Knepley     PetscInt classid;
41549331c7a4SMatthew G. Knepley     char     type[256];
4155b859378eSBarry Smith 
41569566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41577a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41589566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41599566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4160dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41619331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4162dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41639331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4166b859378eSBarry Smith }
4167b859378eSBarry Smith 
41687da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41697da65231SMatthew G Knepley 
41702ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41712ecf6ec3SMatthew G. Knepley {
41722ecf6ec3SMatthew G. Knepley   PetscInt f;
41732ecf6ec3SMatthew G. Knepley 
41742ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41752ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41762ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41772ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41782ecf6ec3SMatthew G. Knepley }
41792ecf6ec3SMatthew G. Knepley 
4180d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4181d71ae5a4SJacob Faibussowitsch {
41821d47ebbbSSatish Balay   PetscInt f;
41831b30c384SMatthew G Knepley 
41847da65231SMatthew G Knepley   PetscFunctionBegin;
418563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
418648a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41887da65231SMatthew G Knepley }
41897da65231SMatthew G Knepley 
41905962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41915962854dSMatthew G. Knepley {
41925962854dSMatthew G. Knepley   PetscInt f;
41935962854dSMatthew G. Knepley 
41945962854dSMatthew G. Knepley   PetscFunctionBegin;
41955962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41965962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
41975962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41985962854dSMatthew G. Knepley }
41995962854dSMatthew G. Knepley 
4200d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4201d71ae5a4SJacob Faibussowitsch {
42021b30c384SMatthew G Knepley   PetscInt f, g;
42037da65231SMatthew G Knepley 
42047da65231SMatthew G Knepley   PetscFunctionBegin;
420563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42061d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42079566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
420848a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42099566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42107da65231SMatthew G Knepley   }
42113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42127da65231SMatthew G Knepley }
4213e7c4fc90SDmitry Karpeev 
4214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4215d71ae5a4SJacob Faibussowitsch {
42160c5b8624SToby Isaac   PetscInt           localSize, bs;
42170c5b8624SToby Isaac   PetscMPIInt        size;
42180c5b8624SToby Isaac   Vec                x, xglob;
42190c5b8624SToby Isaac   const PetscScalar *xarray;
4220e759306cSMatthew G. Knepley 
4221e759306cSMatthew G. Knepley   PetscFunctionBegin;
42229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42239566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42249566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
422593ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42269566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42270c5b8624SToby Isaac   if (size > 1) {
42289566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42299566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42309566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42319566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42320c5b8624SToby Isaac   } else {
42330c5b8624SToby Isaac     xglob = x;
42340c5b8624SToby Isaac   }
42359566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42360c5b8624SToby Isaac   if (size > 1) {
42379566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42389566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42390c5b8624SToby Isaac   }
42409566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4242e759306cSMatthew G. Knepley }
4243e759306cSMatthew G. Knepley 
424488ed4aceSMatthew G Knepley /*@
4245bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4246061576a5SJed Brown 
4247061576a5SJed Brown   Input Parameter:
4248bb7acecfSBarry Smith . dm - The `DM`
4249061576a5SJed Brown 
4250061576a5SJed Brown   Output Parameter:
4251bb7acecfSBarry Smith . section - The `PetscSection`
4252061576a5SJed Brown 
425320f4b53cSBarry Smith   Options Database Key:
4254bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4255061576a5SJed Brown 
4256061576a5SJed Brown   Level: advanced
4257061576a5SJed Brown 
4258061576a5SJed Brown   Notes:
4259bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4260061576a5SJed Brown 
4261bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4262061576a5SJed Brown 
42631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4264061576a5SJed Brown @*/
4265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4266d71ae5a4SJacob Faibussowitsch {
4267061576a5SJed Brown   PetscFunctionBegin;
42689566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4270061576a5SJed Brown }
4271061576a5SJed Brown 
4272061576a5SJed Brown /*@
4273bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
427488ed4aceSMatthew G Knepley 
427588ed4aceSMatthew G Knepley   Input Parameter:
4276bb7acecfSBarry Smith . dm - The `DM`
427788ed4aceSMatthew G Knepley 
427888ed4aceSMatthew G Knepley   Output Parameter:
4279bb7acecfSBarry Smith . section - The `PetscSection`
428088ed4aceSMatthew G Knepley 
428120f4b53cSBarry Smith   Options Database Key:
4282bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4283e5893cccSMatthew G. Knepley 
428488ed4aceSMatthew G Knepley   Level: intermediate
428588ed4aceSMatthew G Knepley 
4286bb7acecfSBarry Smith   Note:
4287bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
428888ed4aceSMatthew G Knepley 
42891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
429088ed4aceSMatthew G Knepley @*/
4291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4292d71ae5a4SJacob Faibussowitsch {
429388ed4aceSMatthew G Knepley   PetscFunctionBegin;
429488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42954f572ea9SToby Isaac   PetscAssertPointer(section, 2);
42961bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4297e5e52638SMatthew G. Knepley     PetscInt d;
4298e5e52638SMatthew G. Knepley 
429945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
430045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
430145480ffeSMatthew G. Knepley       PetscViewer       viewer;
430245480ffeSMatthew G. Knepley       PetscViewerFormat format;
430345480ffeSMatthew G. Knepley       PetscBool         flg;
430445480ffeSMatthew G. Knepley 
4305*648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43069566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
430745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43089566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43099566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
431045480ffeSMatthew G. Knepley       }
431145480ffeSMatthew G. Knepley       if (flg) {
43129566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43139566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4314*648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
431545480ffeSMatthew G. Knepley       }
431645480ffeSMatthew G. Knepley     }
4317dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43189566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43192f0f8703SMatthew G. Knepley   }
43201bb6d2a8SBarry Smith   *section = dm->localSection;
43213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432288ed4aceSMatthew G Knepley }
432388ed4aceSMatthew G Knepley 
432488ed4aceSMatthew G Knepley /*@
4325bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4326061576a5SJed Brown 
4327061576a5SJed Brown   Input Parameters:
4328bb7acecfSBarry Smith + dm      - The `DM`
4329bb7acecfSBarry Smith - section - The `PetscSection`
4330061576a5SJed Brown 
4331061576a5SJed Brown   Level: advanced
4332061576a5SJed Brown 
4333061576a5SJed Brown   Notes:
4334bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4335061576a5SJed Brown 
4336bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4337061576a5SJed Brown 
43381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4339061576a5SJed Brown @*/
4340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4341d71ae5a4SJacob Faibussowitsch {
4342061576a5SJed Brown   PetscFunctionBegin;
43439566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4345061576a5SJed Brown }
4346061576a5SJed Brown 
4347061576a5SJed Brown /*@
4348bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
434988ed4aceSMatthew G Knepley 
435088ed4aceSMatthew G Knepley   Input Parameters:
4351bb7acecfSBarry Smith + dm      - The `DM`
4352bb7acecfSBarry Smith - section - The `PetscSection`
435388ed4aceSMatthew G Knepley 
435488ed4aceSMatthew G Knepley   Level: intermediate
435588ed4aceSMatthew G Knepley 
4356bb7acecfSBarry Smith   Note:
4357bb7acecfSBarry Smith   Any existing Section will be destroyed
435888ed4aceSMatthew G Knepley 
43591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
436088ed4aceSMatthew G Knepley @*/
4361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4362d71ae5a4SJacob Faibussowitsch {
4363c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4364af122d2aSMatthew G Knepley   PetscInt f;
436588ed4aceSMatthew G Knepley 
436688ed4aceSMatthew G Knepley   PetscFunctionBegin;
436788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4368b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43709566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43711bb6d2a8SBarry Smith   dm->localSection = section;
43729566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4373af122d2aSMatthew G Knepley   if (numFields) {
43749566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4375af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43760f21e855SMatthew G. Knepley       PetscObject disc;
4377af122d2aSMatthew G Knepley       const char *name;
4378af122d2aSMatthew G Knepley 
43799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43809566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43819566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4382af122d2aSMatthew G Knepley     }
4383af122d2aSMatthew G Knepley   }
438450350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
438550350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43869566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
438750350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
438852947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
438950350c8eSStefano Zampini 
439050350c8eSStefano Zampini   /* Clear scratch vectors */
439150350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
439250350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
439350350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
439450350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
43953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
439688ed4aceSMatthew G Knepley }
439788ed4aceSMatthew G Knepley 
4398adc21957SMatthew G. Knepley /*@C
4399d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4400adc21957SMatthew G. Knepley 
4401adc21957SMatthew G. Knepley   Input Parameter:
4402adc21957SMatthew G. Knepley . dm - The `DM`
4403adc21957SMatthew G. Knepley 
44049cde84edSJose E. Roman   Output Parameters:
4405adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4406b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4407adc21957SMatthew G. Knepley 
4408adc21957SMatthew G. Knepley   Level: developer
4409adc21957SMatthew G. Knepley 
4410adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4411adc21957SMatthew G. Knepley @*/
4412adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4413adc21957SMatthew G. Knepley {
4414adc21957SMatthew G. Knepley   PetscFunctionBegin;
4415adc21957SMatthew G. Knepley   *perm        = NULL;
4416adc21957SMatthew G. Knepley   *blockStarts = NULL;
4417adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4418adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4419adc21957SMatthew G. Knepley }
4420adc21957SMatthew G. Knepley 
44219435951eSToby Isaac /*@
4422bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44239435951eSToby Isaac 
442420f4b53cSBarry Smith   not Collective
4425e228b242SToby Isaac 
44269435951eSToby Isaac   Input Parameter:
4427bb7acecfSBarry Smith . dm - The `DM`
44289435951eSToby Isaac 
4429d8d19677SJose E. Roman   Output Parameters:
443020f4b53cSBarry 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.
443120f4b53cSBarry 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.
443279769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44339435951eSToby Isaac 
44349435951eSToby Isaac   Level: advanced
44359435951eSToby Isaac 
4436bb7acecfSBarry Smith   Note:
4437bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44389435951eSToby Isaac 
44391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44409435951eSToby Isaac @*/
4441d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4442d71ae5a4SJacob Faibussowitsch {
44439435951eSToby Isaac   PetscFunctionBegin;
44449435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4445dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44463b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44473b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
444879769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44509435951eSToby Isaac }
44519435951eSToby Isaac 
44529435951eSToby Isaac /*@
4453bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44549435951eSToby Isaac 
445520f4b53cSBarry Smith   Collective
4456e228b242SToby Isaac 
44579435951eSToby Isaac   Input Parameters:
4458bb7acecfSBarry Smith + dm      - The `DM`
4459bb7acecfSBarry 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).
446020f4b53cSBarry 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).
446120f4b53cSBarry 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).
44629435951eSToby Isaac 
44639435951eSToby Isaac   Level: advanced
44649435951eSToby Isaac 
446520f4b53cSBarry Smith   Notes:
446620f4b53cSBarry 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()`.
446720f4b53cSBarry Smith 
446820f4b53cSBarry 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.
446920f4b53cSBarry Smith 
4470bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44719435951eSToby Isaac 
44721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44739435951eSToby Isaac @*/
4474d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4475d71ae5a4SJacob Faibussowitsch {
4476e228b242SToby Isaac   PetscMPIInt result;
44779435951eSToby Isaac 
44789435951eSToby Isaac   PetscFunctionBegin;
44799435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4480e228b242SToby Isaac   if (section) {
4481e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44829566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44837a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4484e228b242SToby Isaac   }
4485e228b242SToby Isaac   if (mat) {
4486e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44879566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44887a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4489e228b242SToby Isaac   }
449079769bd5SJed Brown   if (bias) {
449179769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
449379769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
449479769bd5SJed Brown   }
44959566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44969566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44973b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44989566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44999566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45003b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45029566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
450379769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45059435951eSToby Isaac }
45069435951eSToby Isaac 
4507497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4508507e4973SMatthew G. Knepley /*
4509bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4510507e4973SMatthew G. Knepley 
4511507e4973SMatthew G. Knepley   Input Parameters:
4512bb7acecfSBarry Smith + dm - The `DM`
4513bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4514bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4515507e4973SMatthew G. Knepley 
4516507e4973SMatthew G. Knepley   Level: intermediate
4517507e4973SMatthew G. Knepley 
45181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4519507e4973SMatthew G. Knepley */
4520d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4521d71ae5a4SJacob Faibussowitsch {
4522507e4973SMatthew G. Knepley   MPI_Comm        comm;
4523507e4973SMatthew G. Knepley   PetscLayout     layout;
4524507e4973SMatthew G. Knepley   const PetscInt *ranges;
4525507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4526507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4527507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4528507e4973SMatthew G. Knepley 
4529507e4973SMatthew G. Knepley   PetscFunctionBegin;
45309566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4531507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45329566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45349566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45359566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45369566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45379566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45389566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45399566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45409566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4541507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4542f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4543507e4973SMatthew G. Knepley 
45449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45479566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4550507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45519371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45529371c9d4SSatish 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));
45539371c9d4SSatish Balay       valid = PETSC_FALSE;
45549371c9d4SSatish Balay     }
45559371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45569371c9d4SSatish 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));
45579371c9d4SSatish Balay       valid = PETSC_FALSE;
45589371c9d4SSatish Balay     }
4559507e4973SMatthew G. Knepley     if (gdof < 0) {
4560507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4561507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4562507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4563507e4973SMatthew G. Knepley 
45649566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4565507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45669371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45679371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45689371c9d4SSatish Balay           valid = PETSC_FALSE;
45699371c9d4SSatish Balay           break;
45709371c9d4SSatish Balay         }
4571507e4973SMatthew G. Knepley       }
4572507e4973SMatthew G. Knepley     }
4573507e4973SMatthew G. Knepley   }
45749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45759566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45761c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4577507e4973SMatthew G. Knepley   if (!gvalid) {
45789566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4579507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4580507e4973SMatthew G. Knepley   }
45813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4582507e4973SMatthew G. Knepley }
4583f741bcd2SMatthew G. Knepley #endif
4584507e4973SMatthew G. Knepley 
45855f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45866725e60dSJed Brown {
45876725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45884d86920dSPierre Jolivet 
45896725e60dSJed Brown   PetscFunctionBegin;
45906725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45914f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45925f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45936725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45946725e60dSJed Brown   else *sf = dm->sf;
45953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45966725e60dSJed Brown }
45976725e60dSJed Brown 
459888ed4aceSMatthew G Knepley /*@
4599bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
460088ed4aceSMatthew G Knepley 
460120f4b53cSBarry Smith   Collective
46028b1ab98fSJed Brown 
460388ed4aceSMatthew G Knepley   Input Parameter:
4604bb7acecfSBarry Smith . dm - The `DM`
460588ed4aceSMatthew G Knepley 
460688ed4aceSMatthew G Knepley   Output Parameter:
4607bb7acecfSBarry Smith . section - The `PetscSection`
460888ed4aceSMatthew G Knepley 
460988ed4aceSMatthew G Knepley   Level: intermediate
461088ed4aceSMatthew G Knepley 
4611bb7acecfSBarry Smith   Note:
4612bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
461388ed4aceSMatthew G Knepley 
46141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
461588ed4aceSMatthew G Knepley @*/
4616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4617d71ae5a4SJacob Faibussowitsch {
461888ed4aceSMatthew G Knepley   PetscFunctionBegin;
461988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46204f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46211bb6d2a8SBarry Smith   if (!dm->globalSection) {
4622fd59a867SMatthew G. Knepley     PetscSection s;
46236725e60dSJed Brown     PetscSF      sf;
4624fd59a867SMatthew G. Knepley 
46259566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46267a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46277a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46285f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4629eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46309566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46329566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
463388ed4aceSMatthew G Knepley   }
46341bb6d2a8SBarry Smith   *section = dm->globalSection;
46353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
463688ed4aceSMatthew G Knepley }
463788ed4aceSMatthew G Knepley 
4638b21d0597SMatthew G Knepley /*@
4639bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4640b21d0597SMatthew G Knepley 
4641b21d0597SMatthew G Knepley   Input Parameters:
4642bb7acecfSBarry Smith + dm      - The `DM`
46432fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4644b21d0597SMatthew G Knepley 
4645b21d0597SMatthew G Knepley   Level: intermediate
4646b21d0597SMatthew G Knepley 
4647bb7acecfSBarry Smith   Note:
4648bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4649b21d0597SMatthew G Knepley 
46501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4651b21d0597SMatthew G Knepley @*/
4652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4653d71ae5a4SJacob Faibussowitsch {
4654b21d0597SMatthew G Knepley   PetscFunctionBegin;
4655b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46565080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46589566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46591bb6d2a8SBarry Smith   dm->globalSection = section;
4660497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46619566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4662507e4973SMatthew G. Knepley #endif
466350350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
466450350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
466552947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
466650350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
466750350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4669b21d0597SMatthew G Knepley }
4670b21d0597SMatthew G Knepley 
467188ed4aceSMatthew G Knepley /*@
4672bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4673bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
467488ed4aceSMatthew G Knepley 
467588ed4aceSMatthew G Knepley   Input Parameter:
4676bb7acecfSBarry Smith . dm - The `DM`
467788ed4aceSMatthew G Knepley 
467888ed4aceSMatthew G Knepley   Output Parameter:
4679bb7acecfSBarry Smith . sf - The `PetscSF`
468088ed4aceSMatthew G Knepley 
468188ed4aceSMatthew G Knepley   Level: intermediate
468288ed4aceSMatthew G Knepley 
4683bb7acecfSBarry Smith   Note:
4684bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
468588ed4aceSMatthew G Knepley 
46861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
468788ed4aceSMatthew G Knepley @*/
4688d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4689d71ae5a4SJacob Faibussowitsch {
469088ed4aceSMatthew G Knepley   PetscInt nroots;
469188ed4aceSMatthew G Knepley 
469288ed4aceSMatthew G Knepley   PetscFunctionBegin;
469388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46944f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
469548a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46969566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
469788ed4aceSMatthew G Knepley   if (nroots < 0) {
469888ed4aceSMatthew G Knepley     PetscSection section, gSection;
469988ed4aceSMatthew G Knepley 
47009566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
470131ea6d37SMatthew G Knepley     if (section) {
47029566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47039566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
470431ea6d37SMatthew G Knepley     } else {
47050298fd71SBarry Smith       *sf = NULL;
47063ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
470731ea6d37SMatthew G Knepley     }
470888ed4aceSMatthew G Knepley   }
47091bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471188ed4aceSMatthew G Knepley }
471288ed4aceSMatthew G Knepley 
471388ed4aceSMatthew G Knepley /*@
4714bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
471588ed4aceSMatthew G Knepley 
471688ed4aceSMatthew G Knepley   Input Parameters:
4717bb7acecfSBarry Smith + dm - The `DM`
4718bb7acecfSBarry Smith - sf - The `PetscSF`
471988ed4aceSMatthew G Knepley 
472088ed4aceSMatthew G Knepley   Level: intermediate
472188ed4aceSMatthew G Knepley 
4722bb7acecfSBarry Smith   Note:
4723bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
472488ed4aceSMatthew G Knepley 
47251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
472688ed4aceSMatthew G Knepley @*/
4727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4728d71ae5a4SJacob Faibussowitsch {
472988ed4aceSMatthew G Knepley   PetscFunctionBegin;
473088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4731b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47339566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47341bb6d2a8SBarry Smith   dm->sectionSF = sf;
47353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473688ed4aceSMatthew G Knepley }
473788ed4aceSMatthew G Knepley 
4738cc4c1da9SBarry Smith /*@
4739bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
474088ed4aceSMatthew G Knepley   describing the data layout.
474188ed4aceSMatthew G Knepley 
474288ed4aceSMatthew G Knepley   Input Parameters:
4743bb7acecfSBarry Smith + dm            - The `DM`
4744bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4745bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
474688ed4aceSMatthew G Knepley 
47471bb6d2a8SBarry Smith   Level: developer
47481bb6d2a8SBarry Smith 
4749bb7acecfSBarry Smith   Note:
4750bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4751bb7acecfSBarry Smith 
475273ff1848SBarry Smith   Developer Note:
4753bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4754bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4755b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4756b6971eaeSBarry Smith   they create, this returns nothing
47571bb6d2a8SBarry Smith 
47581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
475988ed4aceSMatthew G Knepley @*/
4760d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4761d71ae5a4SJacob Faibussowitsch {
476288ed4aceSMatthew G Knepley   PetscFunctionBegin;
476388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47649566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
476688ed4aceSMatthew G Knepley }
4767af122d2aSMatthew G Knepley 
4768b21d0597SMatthew G Knepley /*@
4769bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4770bb7acecfSBarry Smith 
4771bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4772b21d0597SMatthew G Knepley 
4773b21d0597SMatthew G Knepley   Input Parameter:
4774bb7acecfSBarry Smith . dm - The `DM`
4775b21d0597SMatthew G Knepley 
4776b21d0597SMatthew G Knepley   Output Parameter:
4777bb7acecfSBarry Smith . sf - The `PetscSF`
4778b21d0597SMatthew G Knepley 
4779b21d0597SMatthew G Knepley   Level: intermediate
4780b21d0597SMatthew G Knepley 
4781bb7acecfSBarry Smith   Note:
4782bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4783b21d0597SMatthew G Knepley 
47841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4785b21d0597SMatthew G Knepley @*/
4786d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4787d71ae5a4SJacob Faibussowitsch {
4788b21d0597SMatthew G Knepley   PetscFunctionBegin;
4789b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47904f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4791b21d0597SMatthew G Knepley   *sf = dm->sf;
47923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4793b21d0597SMatthew G Knepley }
4794b21d0597SMatthew G Knepley 
4795057b4bcdSMatthew G Knepley /*@
4796bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4797bb7acecfSBarry Smith 
479820f4b53cSBarry Smith   Collective
4799057b4bcdSMatthew G Knepley 
4800057b4bcdSMatthew G Knepley   Input Parameters:
4801bb7acecfSBarry Smith + dm - The `DM`
4802bb7acecfSBarry Smith - sf - The `PetscSF`
4803057b4bcdSMatthew G Knepley 
4804057b4bcdSMatthew G Knepley   Level: intermediate
4805057b4bcdSMatthew G Knepley 
48061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4807057b4bcdSMatthew G Knepley @*/
4808d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4809d71ae5a4SJacob Faibussowitsch {
4810057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4811057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4812b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48149566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4815057b4bcdSMatthew G Knepley   dm->sf = sf;
48163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4817057b4bcdSMatthew G Knepley }
4818057b4bcdSMatthew G Knepley 
48194f37162bSMatthew G. Knepley /*@
4820bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48214f37162bSMatthew G. Knepley 
48224f37162bSMatthew G. Knepley   Input Parameter:
4823bb7acecfSBarry Smith . dm - The `DM`
48244f37162bSMatthew G. Knepley 
48254f37162bSMatthew G. Knepley   Output Parameter:
4826bb7acecfSBarry Smith . sf - The `PetscSF`
48274f37162bSMatthew G. Knepley 
48284f37162bSMatthew G. Knepley   Level: intermediate
48294f37162bSMatthew G. Knepley 
4830bb7acecfSBarry Smith   Note:
4831bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48324f37162bSMatthew G. Knepley 
48331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48344f37162bSMatthew G. Knepley @*/
4835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4836d71ae5a4SJacob Faibussowitsch {
48374f37162bSMatthew G. Knepley   PetscFunctionBegin;
48384f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48394f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48404f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48424f37162bSMatthew G. Knepley }
48434f37162bSMatthew G. Knepley 
48444f37162bSMatthew G. Knepley /*@
48454f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48464f37162bSMatthew G. Knepley 
48474f37162bSMatthew G. Knepley   Input Parameters:
48484f37162bSMatthew G. Knepley + dm - The DM
48494f37162bSMatthew G. Knepley - sf - The PetscSF
48504f37162bSMatthew G. Knepley 
48514f37162bSMatthew G. Knepley   Level: intermediate
48524f37162bSMatthew G. Knepley 
48531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48544f37162bSMatthew G. Knepley @*/
4855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4856d71ae5a4SJacob Faibussowitsch {
48574f37162bSMatthew G. Knepley   PetscFunctionBegin;
48584f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48594f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48619566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48624f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48644f37162bSMatthew G. Knepley }
48654f37162bSMatthew G. Knepley 
4866d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4867d71ae5a4SJacob Faibussowitsch {
486834aa8a36SMatthew G. Knepley   PetscClassId id;
486934aa8a36SMatthew G. Knepley 
487034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
487234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48739566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
487434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48759566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
487617c1d62eSMatthew G. Knepley   } else {
48779566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
487834aa8a36SMatthew G. Knepley   }
48793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488034aa8a36SMatthew G. Knepley }
488134aa8a36SMatthew G. Knepley 
4882d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4883d71ae5a4SJacob Faibussowitsch {
488444a7f3ddSMatthew G. Knepley   RegionField *tmpr;
488544a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
488644a7f3ddSMatthew G. Knepley 
488744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48883ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
489044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48919371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48929371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48939371c9d4SSatish Balay     tmpr[f].label       = NULL;
48949371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48959371c9d4SSatish Balay   }
48969566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
489744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
489844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490044a7f3ddSMatthew G. Knepley }
490144a7f3ddSMatthew G. Knepley 
490244a7f3ddSMatthew G. Knepley /*@
490320f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
490444a7f3ddSMatthew G. Knepley 
490520f4b53cSBarry Smith   Logically Collective
490644a7f3ddSMatthew G. Knepley 
490744a7f3ddSMatthew G. Knepley   Input Parameter:
490820f4b53cSBarry Smith . dm - The `DM`
490944a7f3ddSMatthew G. Knepley 
491044a7f3ddSMatthew G. Knepley   Level: intermediate
491144a7f3ddSMatthew G. Knepley 
49121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
491344a7f3ddSMatthew G. Knepley @*/
4914d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4915d71ae5a4SJacob Faibussowitsch {
491644a7f3ddSMatthew G. Knepley   PetscInt f;
491744a7f3ddSMatthew G. Knepley 
491844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
491944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
492044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49219566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49229566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
492344a7f3ddSMatthew G. Knepley   }
49249566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
492544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
492644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
492844a7f3ddSMatthew G. Knepley }
492944a7f3ddSMatthew G. Knepley 
4930689b5837SMatthew G. Knepley /*@
493120f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4932689b5837SMatthew G. Knepley 
493320f4b53cSBarry Smith   Not Collective
4934689b5837SMatthew G. Knepley 
4935689b5837SMatthew G. Knepley   Input Parameter:
493620f4b53cSBarry Smith . dm - The `DM`
4937689b5837SMatthew G. Knepley 
4938689b5837SMatthew G. Knepley   Output Parameter:
493960225df5SJacob Faibussowitsch . numFields - The number of fields
4940689b5837SMatthew G. Knepley 
4941689b5837SMatthew G. Knepley   Level: intermediate
4942689b5837SMatthew G. Knepley 
49431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4944689b5837SMatthew G. Knepley @*/
4945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4946d71ae5a4SJacob Faibussowitsch {
49470f21e855SMatthew G. Knepley   PetscFunctionBegin;
49480f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49494f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
495044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4952af122d2aSMatthew G Knepley }
4953af122d2aSMatthew G Knepley 
4954689b5837SMatthew G. Knepley /*@
495520f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4956689b5837SMatthew G. Knepley 
495720f4b53cSBarry Smith   Logically Collective
4958689b5837SMatthew G. Knepley 
4959689b5837SMatthew G. Knepley   Input Parameters:
496020f4b53cSBarry Smith + dm        - The `DM`
496160225df5SJacob Faibussowitsch - numFields - The number of fields
4962689b5837SMatthew G. Knepley 
4963689b5837SMatthew G. Knepley   Level: intermediate
4964689b5837SMatthew G. Knepley 
49651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4966689b5837SMatthew G. Knepley @*/
4967d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4968d71ae5a4SJacob Faibussowitsch {
49690f21e855SMatthew G. Knepley   PetscInt Nf, f;
4970af122d2aSMatthew G Knepley 
4971af122d2aSMatthew G Knepley   PetscFunctionBegin;
4972af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49739566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49740f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49750f21e855SMatthew G. Knepley     PetscContainer obj;
49760f21e855SMatthew G. Knepley 
49779566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49789566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49799566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4980af122d2aSMatthew G Knepley   }
49813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4982af122d2aSMatthew G Knepley }
4983af122d2aSMatthew G Knepley 
4984c1929be8SMatthew G. Knepley /*@
4985bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4986c1929be8SMatthew G. Knepley 
498720f4b53cSBarry Smith   Not Collective
4988c1929be8SMatthew G. Knepley 
4989c1929be8SMatthew G. Knepley   Input Parameters:
4990bb7acecfSBarry Smith + dm - The `DM`
4991c1929be8SMatthew G. Knepley - f  - The field number
4992c1929be8SMatthew G. Knepley 
499344a7f3ddSMatthew G. Knepley   Output Parameters:
499420f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
499520f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
4996c1929be8SMatthew G. Knepley 
499744a7f3ddSMatthew G. Knepley   Level: intermediate
4998c1929be8SMatthew G. Knepley 
49991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5000c1929be8SMatthew G. Knepley @*/
5001d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5002d71ae5a4SJacob Faibussowitsch {
5003af122d2aSMatthew G Knepley   PetscFunctionBegin;
5004af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50054f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50067a8be351SBarry 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);
500744a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5008bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5010decb47aaSMatthew G. Knepley }
5011decb47aaSMatthew G. Knepley 
5012083401c6SMatthew G. Knepley /* Does not clear the DS */
5013d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5014d71ae5a4SJacob Faibussowitsch {
5015083401c6SMatthew G. Knepley   PetscFunctionBegin;
50169566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50179566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50189566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5019083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5020bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5022bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5024083401c6SMatthew G. Knepley }
5025083401c6SMatthew G. Knepley 
5026ffeef943SBarry Smith /*@
5027bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5028bb7acecfSBarry Smith   the field numbering.
5029c1929be8SMatthew G. Knepley 
503020f4b53cSBarry Smith   Logically Collective
5031c1929be8SMatthew G. Knepley 
5032c1929be8SMatthew G. Knepley   Input Parameters:
5033bb7acecfSBarry Smith + dm    - The `DM`
5034c1929be8SMatthew G. Knepley . f     - The field number
503520f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5036bb7acecfSBarry Smith - disc  - The discretization object
5037c1929be8SMatthew G. Knepley 
503844a7f3ddSMatthew G. Knepley   Level: intermediate
5039c1929be8SMatthew G. Knepley 
50401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5041c1929be8SMatthew G. Knepley @*/
5042d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5043d71ae5a4SJacob Faibussowitsch {
5044decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5045decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5046e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5047bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50487a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5049bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5050bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50519566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
505344a7f3ddSMatthew G. Knepley }
505444a7f3ddSMatthew G. Knepley 
5055ffeef943SBarry Smith /*@
5056bb7acecfSBarry 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)
5057bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
505844a7f3ddSMatthew G. Knepley 
505920f4b53cSBarry Smith   Logically Collective
506044a7f3ddSMatthew G. Knepley 
506144a7f3ddSMatthew G. Knepley   Input Parameters:
5062bb7acecfSBarry Smith + dm    - The `DM`
506320f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5064bb7acecfSBarry Smith - disc  - The discretization object
506544a7f3ddSMatthew G. Knepley 
506644a7f3ddSMatthew G. Knepley   Level: intermediate
506744a7f3ddSMatthew G. Knepley 
5068bb7acecfSBarry Smith   Notes:
5069bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5070bb7acecfSBarry Smith 
5071da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5072bb7acecfSBarry 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
5073bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5074bb7acecfSBarry Smith 
50751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
507644a7f3ddSMatthew G. Knepley @*/
5077d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5078d71ae5a4SJacob Faibussowitsch {
507944a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
508044a7f3ddSMatthew G. Knepley 
508144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
508244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5083064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5084bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50859566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
508644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5087bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50889566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5089bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5090bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50919566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5093af122d2aSMatthew G Knepley }
50946636e97aSMatthew G Knepley 
5095e5e52638SMatthew G. Knepley /*@
5096e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5097e0b68406SMatthew Knepley 
509820f4b53cSBarry Smith   Logically Collective
5099e0b68406SMatthew Knepley 
5100e0b68406SMatthew Knepley   Input Parameters:
5101bb7acecfSBarry Smith + dm          - The `DM`
5102e0b68406SMatthew Knepley . f           - The field index
5103bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5104e0b68406SMatthew Knepley 
5105e0b68406SMatthew Knepley   Level: intermediate
5106e0b68406SMatthew Knepley 
51071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5108e0b68406SMatthew Knepley @*/
5109d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5110d71ae5a4SJacob Faibussowitsch {
5111e0b68406SMatthew Knepley   PetscFunctionBegin;
511263a3b9bcSJacob 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);
5113e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5115e0b68406SMatthew Knepley }
5116e0b68406SMatthew Knepley 
5117e0b68406SMatthew Knepley /*@
5118e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5119e0b68406SMatthew Knepley 
512020f4b53cSBarry Smith   Not Collective
5121e0b68406SMatthew Knepley 
5122e0b68406SMatthew Knepley   Input Parameters:
5123bb7acecfSBarry Smith + dm - The `DM`
5124e0b68406SMatthew Knepley - f  - The field index
5125e0b68406SMatthew Knepley 
5126e0b68406SMatthew Knepley   Output Parameter:
5127e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5128e0b68406SMatthew Knepley 
5129e0b68406SMatthew Knepley   Level: intermediate
5130e0b68406SMatthew Knepley 
513160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5132e0b68406SMatthew Knepley @*/
5133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5134d71ae5a4SJacob Faibussowitsch {
5135e0b68406SMatthew Knepley   PetscFunctionBegin;
513663a3b9bcSJacob 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);
5137e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5139e0b68406SMatthew Knepley }
5140e0b68406SMatthew Knepley 
5141e0b68406SMatthew Knepley /*@
5142bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5143e5e52638SMatthew G. Knepley 
514420f4b53cSBarry Smith   Collective
5145e5e52638SMatthew G. Knepley 
5146e5e52638SMatthew G. Knepley   Input Parameter:
5147bb7acecfSBarry Smith . dm - The `DM`
5148e5e52638SMatthew G. Knepley 
5149e5e52638SMatthew G. Knepley   Output Parameter:
5150bb7acecfSBarry Smith . newdm - The `DM`
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley   Level: advanced
5153e5e52638SMatthew G. Knepley 
51541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5155e5e52638SMatthew G. Knepley @*/
5156d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5157d71ae5a4SJacob Faibussowitsch {
5158e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5159e5e52638SMatthew G. Knepley 
5160e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51613ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51629566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51639566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5164e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5165e5e52638SMatthew G. Knepley     DMLabel     label;
5166e5e52638SMatthew G. Knepley     PetscObject field;
516734aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5168e5e52638SMatthew G. Knepley 
51699566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51709566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51719566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51729566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
517334aa8a36SMatthew G. Knepley   }
51743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517534aa8a36SMatthew G. Knepley }
517634aa8a36SMatthew G. Knepley 
517734aa8a36SMatthew G. Knepley /*@
517834aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
517934aa8a36SMatthew G. Knepley 
518020f4b53cSBarry Smith   Not Collective
518134aa8a36SMatthew G. Knepley 
518234aa8a36SMatthew G. Knepley   Input Parameters:
518320f4b53cSBarry Smith + dm - The `DM` object
518420f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
518534aa8a36SMatthew G. Knepley 
5186d8d19677SJose E. Roman   Output Parameters:
518734aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
518834aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
518934aa8a36SMatthew G. Knepley 
519034aa8a36SMatthew G. Knepley   Level: developer
519134aa8a36SMatthew G. Knepley 
519220f4b53cSBarry Smith   Notes:
519320f4b53cSBarry Smith .vb
519420f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519520f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519620f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
519720f4b53cSBarry Smith .ve
519820f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
519920f4b53cSBarry Smith 
52001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
520134aa8a36SMatthew G. Knepley @*/
5202d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5203d71ae5a4SJacob Faibussowitsch {
520434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
520534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52064f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52074f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
520834aa8a36SMatthew G. Knepley   if (f < 0) {
520934aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
521034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
521134aa8a36SMatthew G. Knepley   } else {
521234aa8a36SMatthew G. Knepley     PetscInt Nf;
521334aa8a36SMatthew G. Knepley 
52149566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52157a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
521634aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
521734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
521834aa8a36SMatthew G. Knepley   }
52193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
522034aa8a36SMatthew G. Knepley }
522134aa8a36SMatthew G. Knepley 
522234aa8a36SMatthew G. Knepley /*@
522334aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
522434aa8a36SMatthew G. Knepley 
522520f4b53cSBarry Smith   Not Collective
522634aa8a36SMatthew G. Knepley 
522734aa8a36SMatthew G. Knepley   Input Parameters:
522820f4b53cSBarry Smith + dm         - The `DM` object
522934aa8a36SMatthew G. Knepley . f          - The field number
523034aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
523134aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
523234aa8a36SMatthew G. Knepley 
523334aa8a36SMatthew G. Knepley   Level: developer
523434aa8a36SMatthew G. Knepley 
523520f4b53cSBarry Smith   Notes:
523620f4b53cSBarry Smith .vb
523720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
523820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
523920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
524020f4b53cSBarry Smith .ve
524120f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
524220f4b53cSBarry Smith 
52431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
524434aa8a36SMatthew G. Knepley @*/
5245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5246d71ae5a4SJacob Faibussowitsch {
524734aa8a36SMatthew G. Knepley   PetscFunctionBegin;
524834aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
524934aa8a36SMatthew G. Knepley   if (f < 0) {
525034aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
525134aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
525234aa8a36SMatthew G. Knepley   } else {
525334aa8a36SMatthew G. Knepley     PetscInt Nf;
525434aa8a36SMatthew G. Knepley 
52559566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52567a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
525734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
525834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5259e5e52638SMatthew G. Knepley   }
52603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5261e5e52638SMatthew G. Knepley }
5262e5e52638SMatthew G. Knepley 
5263b0441da4SMatthew G. Knepley /*@
5264b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5265b0441da4SMatthew G. Knepley 
5266b0441da4SMatthew G. Knepley   Not collective
5267b0441da4SMatthew G. Knepley 
5268f899ff85SJose E. Roman   Input Parameter:
526920f4b53cSBarry Smith . dm - The `DM` object
5270b0441da4SMatthew G. Knepley 
5271d8d19677SJose E. Roman   Output Parameters:
5272b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5273b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5274b0441da4SMatthew G. Knepley 
5275b0441da4SMatthew G. Knepley   Level: developer
5276b0441da4SMatthew G. Knepley 
527720f4b53cSBarry Smith   Notes:
527820f4b53cSBarry Smith .vb
527920f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
528020f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
528120f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
528220f4b53cSBarry Smith .ve
528320f4b53cSBarry Smith 
52841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5285b0441da4SMatthew G. Knepley @*/
5286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5287d71ae5a4SJacob Faibussowitsch {
5288b0441da4SMatthew G. Knepley   PetscInt Nf;
5289b0441da4SMatthew G. Knepley 
5290b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5291b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52924f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52934f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
52949566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5295b0441da4SMatthew G. Knepley   if (!Nf) {
52969566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5297b0441da4SMatthew G. Knepley   } else {
52989566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5299b0441da4SMatthew G. Knepley   }
53003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5301b0441da4SMatthew G. Knepley }
5302b0441da4SMatthew G. Knepley 
5303b0441da4SMatthew G. Knepley /*@
5304b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5305b0441da4SMatthew G. Knepley 
530620f4b53cSBarry Smith   Not Collective
5307b0441da4SMatthew G. Knepley 
5308b0441da4SMatthew G. Knepley   Input Parameters:
530920f4b53cSBarry Smith + dm         - The `DM` object
5310b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5311b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5312b0441da4SMatthew G. Knepley 
5313b0441da4SMatthew G. Knepley   Level: developer
5314b0441da4SMatthew G. Knepley 
531520f4b53cSBarry Smith   Notes:
531620f4b53cSBarry Smith .vb
531720f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
531820f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
531920f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
532020f4b53cSBarry Smith .ve
532120f4b53cSBarry Smith 
53221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5323b0441da4SMatthew G. Knepley @*/
5324d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5325d71ae5a4SJacob Faibussowitsch {
5326b0441da4SMatthew G. Knepley   PetscInt Nf;
5327b0441da4SMatthew G. Knepley 
5328b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5329b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53309566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5331b0441da4SMatthew G. Knepley   if (!Nf) {
53329566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5333b0441da4SMatthew G. Knepley   } else {
53349566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5335e5e52638SMatthew G. Knepley   }
53363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5337e5e52638SMatthew G. Knepley }
5338e5e52638SMatthew G. Knepley 
5339d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5340d71ae5a4SJacob Faibussowitsch {
5341799db056SMatthew G. Knepley   DM           plex;
5342799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5343799db056SMatthew G. Knepley   const char **names;
5344799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5345799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5346799db056SMatthew G. Knepley   size_t       len;
5347799db056SMatthew G. Knepley   MPI_Comm     comm;
5348799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5349783e2ec8SMatthew G. Knepley 
5350783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5351799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5352799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5353799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5354799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5355799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5356799db056SMatthew G. Knepley     PetscDS  dsBC;
5357799db056SMatthew G. Knepley     PetscInt numBd;
5358799db056SMatthew G. Knepley 
535907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5360799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5361799db056SMatthew G. Knepley     maxLabels += numBd;
5362799db056SMatthew G. Knepley   }
5363799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5364799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5365799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5366799db056SMatthew G. Knepley     PetscDS  dsBC;
5367799db056SMatthew G. Knepley     PetscInt numBd, bd;
5368799db056SMatthew G. Knepley 
536907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5370799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5371799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5372799db056SMatthew G. Knepley       DMLabel      label;
5373799db056SMatthew G. Knepley       PetscInt     field;
5374799db056SMatthew G. Knepley       PetscObject  obj;
5375799db056SMatthew G. Knepley       PetscClassId id;
5376799db056SMatthew G. Knepley 
5377799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53789566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53799566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5380799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53819371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53829371c9d4SSatish Balay         if (labels[l] == label) break;
5383799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5384783e2ec8SMatthew G. Knepley     }
5385799db056SMatthew G. Knepley   }
5386799db056SMatthew G. Knepley   /* Get label names */
5387799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5388799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53899371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53909371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53919371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53929371c9d4SSatish Balay   }
5393799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5394712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5395799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5396c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5397799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5398799db056SMatthew G. Knepley   /* Put all names on all processes */
5399799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5400799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5401799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5402799db056SMatthew G. Knepley   gNl = displs[size];
54039371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54049371c9d4SSatish Balay     counts[p] *= gmaxLen;
54059371c9d4SSatish Balay     displs[p] *= gmaxLen;
54069371c9d4SSatish Balay   }
5407799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5408799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5409799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5410799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5411799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5412799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5413799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54149371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54159371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
54169566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5417799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54189566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5419799db056SMatthew G. Knepley     ++gl;
5420783e2ec8SMatthew G. Knepley   }
5421799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5423783e2ec8SMatthew G. Knepley }
5424783e2ec8SMatthew G. Knepley 
5425d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5426d71ae5a4SJacob Faibussowitsch {
5427e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5428e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54313ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54329566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5433e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54349371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54359371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54369371c9d4SSatish Balay     tmpd[s].label  = NULL;
54379371c9d4SSatish Balay     tmpd[s].fields = NULL;
54389371c9d4SSatish Balay   }
54399566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5440e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5441e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5443e5e52638SMatthew G. Knepley }
5444e5e52638SMatthew G. Knepley 
5445e5e52638SMatthew G. Knepley /*@
544620f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5447e5e52638SMatthew G. Knepley 
544820f4b53cSBarry Smith   Not Collective
5449e5e52638SMatthew G. Knepley 
5450e5e52638SMatthew G. Knepley   Input Parameter:
545120f4b53cSBarry Smith . dm - The `DM`
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Output Parameter:
545420f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Level: intermediate
5457e5e52638SMatthew G. Knepley 
54581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5459e5e52638SMatthew G. Knepley @*/
5460d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5461d71ae5a4SJacob Faibussowitsch {
5462e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5463e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54644f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5465e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5467e5e52638SMatthew G. Knepley }
5468e5e52638SMatthew G. Knepley 
5469e5e52638SMatthew G. Knepley /*@
547020f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5471e5e52638SMatthew G. Knepley 
547220f4b53cSBarry Smith   Logically Collective
5473e5e52638SMatthew G. Knepley 
5474e5e52638SMatthew G. Knepley   Input Parameter:
547520f4b53cSBarry Smith . dm - The `DM`
5476e5e52638SMatthew G. Knepley 
5477e5e52638SMatthew G. Knepley   Level: intermediate
5478e5e52638SMatthew G. Knepley 
54791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5480e5e52638SMatthew G. Knepley @*/
5481d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5482d71ae5a4SJacob Faibussowitsch {
5483e5e52638SMatthew G. Knepley   PetscInt s;
5484e5e52638SMatthew G. Knepley 
5485e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5486e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5487e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54889566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
548907218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54909566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54919566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5492e5e52638SMatthew G. Knepley   }
54939566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5494e5e52638SMatthew G. Knepley   dm->probs = NULL;
5495e5e52638SMatthew G. Knepley   dm->Nds   = 0;
54963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5497e5e52638SMatthew G. Knepley }
5498e5e52638SMatthew G. Knepley 
5499e5e52638SMatthew G. Knepley /*@
550020f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5501e5e52638SMatthew G. Knepley 
550220f4b53cSBarry Smith   Not Collective
5503e5e52638SMatthew G. Knepley 
5504e5e52638SMatthew G. Knepley   Input Parameter:
550520f4b53cSBarry Smith . dm - The `DM`
5506e5e52638SMatthew G. Knepley 
5507e5e52638SMatthew G. Knepley   Output Parameter:
550807218a29SMatthew G. Knepley . ds - The default `PetscDS`
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley   Level: intermediate
5511e5e52638SMatthew G. Knepley 
55121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5513e5e52638SMatthew G. Knepley @*/
551407218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5515d71ae5a4SJacob Faibussowitsch {
5516e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5517e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55184f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
551907218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
552007218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5522e5e52638SMatthew G. Knepley }
5523e5e52638SMatthew G. Knepley 
5524e5e52638SMatthew G. Knepley /*@
552520f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5526e5e52638SMatthew G. Knepley 
552720f4b53cSBarry Smith   Not Collective
5528e5e52638SMatthew G. Knepley 
5529e5e52638SMatthew G. Knepley   Input Parameters:
553020f4b53cSBarry Smith + dm    - The `DM`
553120f4b53cSBarry Smith - point - Cell for the `PetscDS`
5532e5e52638SMatthew G. Knepley 
553307218a29SMatthew G. Knepley   Output Parameters:
553407218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
553507218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5536e5e52638SMatthew G. Knepley 
5537e5e52638SMatthew G. Knepley   Level: developer
5538e5e52638SMatthew G. Knepley 
55391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5540e5e52638SMatthew G. Knepley @*/
554107218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5542d71ae5a4SJacob Faibussowitsch {
554307218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5544e5e52638SMatthew G. Knepley   PetscInt s;
5545e5e52638SMatthew G. Knepley 
5546e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5547e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55484f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55494f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
555063a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
555107218a29SMatthew G. Knepley   if (ds) *ds = NULL;
555207218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5553e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5554e5e52638SMatthew G. Knepley     PetscInt val;
5555e5e52638SMatthew G. Knepley 
55569371c9d4SSatish Balay     if (!dm->probs[s].label) {
555707218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55589371c9d4SSatish Balay     } else {
55599566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55609371c9d4SSatish Balay       if (val >= 0) {
556107218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
556207218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55639371c9d4SSatish Balay         break;
55649371c9d4SSatish Balay       }
5565e5e52638SMatthew G. Knepley     }
5566e5e52638SMatthew G. Knepley   }
556707218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5569e5e52638SMatthew G. Knepley }
5570e5e52638SMatthew G. Knepley 
5571e5e52638SMatthew G. Knepley /*@
557220f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5573e5e52638SMatthew G. Knepley 
557420f4b53cSBarry Smith   Not Collective
5575e5e52638SMatthew G. Knepley 
5576e5e52638SMatthew G. Knepley   Input Parameters:
557720f4b53cSBarry Smith + dm    - The `DM`
557820f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5579e5e52638SMatthew G. Knepley 
5580b3cf3223SMatthew G. Knepley   Output Parameters:
558120f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
558207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
558307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5584e5e52638SMatthew G. Knepley 
5585e5e52638SMatthew G. Knepley   Level: advanced
5586e5e52638SMatthew G. Knepley 
558720f4b53cSBarry Smith   Note:
558820f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
558920f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
559007218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
559120f4b53cSBarry Smith 
55921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5593e5e52638SMatthew G. Knepley @*/
559407218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5595d71ae5a4SJacob Faibussowitsch {
5596e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5597e5e52638SMatthew G. Knepley 
5598e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5599e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5600e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56019371c9d4SSatish Balay   if (fields) {
56024f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56039371c9d4SSatish Balay     *fields = NULL;
56049371c9d4SSatish Balay   }
56059371c9d4SSatish Balay   if (ds) {
56064f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56079371c9d4SSatish Balay     *ds = NULL;
56089371c9d4SSatish Balay   }
560907218a29SMatthew G. Knepley   if (dsIn) {
56104f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
561107218a29SMatthew G. Knepley     *dsIn = NULL;
561207218a29SMatthew G. Knepley   }
5613e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5614154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5615b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5616b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
561707218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56183ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5619b3cf3223SMatthew G. Knepley     }
5620e5e52638SMatthew G. Knepley   }
56213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5622e5e52638SMatthew G. Knepley }
5623e5e52638SMatthew G. Knepley 
5624e5e52638SMatthew G. Knepley /*@
5625bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5626083401c6SMatthew G. Knepley 
562720f4b53cSBarry Smith   Collective
5628083401c6SMatthew G. Knepley 
5629083401c6SMatthew G. Knepley   Input Parameters:
5630bb7acecfSBarry Smith + dm     - The `DM`
563120f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
563207218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
563307218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
563407218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5635083401c6SMatthew G. Knepley 
563620f4b53cSBarry Smith   Level: advanced
563720f4b53cSBarry Smith 
5638bb7acecfSBarry Smith   Note:
5639bb7acecfSBarry 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,
5640083401c6SMatthew G. Knepley   the fields argument is ignored.
5641083401c6SMatthew G. Knepley 
56421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5643083401c6SMatthew G. Knepley @*/
564407218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5645d71ae5a4SJacob Faibussowitsch {
5646083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5647083401c6SMatthew G. Knepley 
5648083401c6SMatthew G. Knepley   PetscFunctionBegin;
5649083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5650083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
565107218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5652064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
565307218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5654083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5655083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56569566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
565707218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5658083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
565907218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56603ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5661083401c6SMatthew G. Knepley     }
5662083401c6SMatthew G. Knepley   }
56639566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56659566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
566707218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5668083401c6SMatthew G. Knepley   if (!label) {
5669083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5670083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5671083401c6SMatthew G. Knepley     Nds = 0;
5672083401c6SMatthew G. Knepley   }
5673083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5674083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5675083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
567607218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5678083401c6SMatthew G. Knepley }
5679083401c6SMatthew G. Knepley 
5680083401c6SMatthew G. Knepley /*@
568120f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5682e5e52638SMatthew G. Knepley 
568320f4b53cSBarry Smith   Not Collective
5684e5e52638SMatthew G. Knepley 
5685e5e52638SMatthew G. Knepley   Input Parameters:
568620f4b53cSBarry Smith + dm  - The `DM`
5687e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5688e5e52638SMatthew G. Knepley 
5689e5e52638SMatthew G. Knepley   Output Parameters:
569020f4b53cSBarry Smith + label  - The region label, or `NULL`
569120f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
569207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
569307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5694e5e52638SMatthew G. Knepley 
5695e5e52638SMatthew G. Knepley   Level: advanced
5696e5e52638SMatthew G. Knepley 
56971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5698e5e52638SMatthew G. Knepley @*/
569907218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5700d71ae5a4SJacob Faibussowitsch {
5701e5e52638SMatthew G. Knepley   PetscInt Nds;
5702e5e52638SMatthew G. Knepley 
5703e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5704e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57059566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
570663a3b9bcSJacob 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);
5707e5e52638SMatthew G. Knepley   if (label) {
57084f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5709e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5710e5e52638SMatthew G. Knepley   }
5711b3cf3223SMatthew G. Knepley   if (fields) {
57124f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5713b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5714b3cf3223SMatthew G. Knepley   }
5715e5e52638SMatthew G. Knepley   if (ds) {
57164f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5717e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5718e5e52638SMatthew G. Knepley   }
571907218a29SMatthew G. Knepley   if (dsIn) {
57204f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
572107218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
572207218a29SMatthew G. Knepley   }
57233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5724e5e52638SMatthew G. Knepley }
5725e5e52638SMatthew G. Knepley 
5726e5e52638SMatthew G. Knepley /*@
572720f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5728e5e52638SMatthew G. Knepley 
572920f4b53cSBarry Smith   Not Collective
5730e5e52638SMatthew G. Knepley 
5731e5e52638SMatthew G. Knepley   Input Parameters:
573220f4b53cSBarry Smith + dm     - The `DM`
5733083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
573420f4b53cSBarry Smith . label  - The region label, or `NULL`
573507218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
573607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
573707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5738e5e52638SMatthew G. Knepley 
5739e5e52638SMatthew G. Knepley   Level: advanced
5740e5e52638SMatthew G. Knepley 
57411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5742e5e52638SMatthew G. Knepley @*/
574307218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5744d71ae5a4SJacob Faibussowitsch {
5745083401c6SMatthew G. Knepley   PetscInt Nds;
5746e5e52638SMatthew G. Knepley 
5747e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5748e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5749ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57509566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
575163a3b9bcSJacob 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);
57529566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57539566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5754083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5755083401c6SMatthew G. Knepley   if (fields) {
5756083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57579566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57589566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5759083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5760e5e52638SMatthew G. Knepley   }
5761083401c6SMatthew G. Knepley   if (ds) {
5762083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57639566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57649566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5765083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5766083401c6SMatthew G. Knepley   }
576707218a29SMatthew G. Knepley   if (dsIn) {
576807218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
576907218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
577007218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
577107218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
577207218a29SMatthew G. Knepley   }
57733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5774e5e52638SMatthew G. Knepley }
5775e5e52638SMatthew G. Knepley 
5776e5e52638SMatthew G. Knepley /*@
577720f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57781d3af9e0SMatthew G. Knepley 
577920f4b53cSBarry Smith   Not Collective
57801d3af9e0SMatthew G. Knepley 
57811d3af9e0SMatthew G. Knepley   Input Parameters:
578220f4b53cSBarry Smith + dm - The `DM`
578320f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57841d3af9e0SMatthew G. Knepley 
57851d3af9e0SMatthew G. Knepley   Output Parameter:
57861d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57871d3af9e0SMatthew G. Knepley 
57881d3af9e0SMatthew G. Knepley   Level: advanced
57891d3af9e0SMatthew G. Knepley 
57901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57911d3af9e0SMatthew G. Knepley @*/
5792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5793d71ae5a4SJacob Faibussowitsch {
57941d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57951d3af9e0SMatthew G. Knepley 
57961d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57971d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57981d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
57994f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58009566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58019371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58029371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58031d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58041d3af9e0SMatthew G. Knepley   else *num = n;
58053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58061d3af9e0SMatthew G. Knepley }
58071d3af9e0SMatthew G. Knepley 
5808cc4c1da9SBarry Smith /*@
5809bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58102df84da0SMatthew G. Knepley 
581120f4b53cSBarry Smith   Not Collective
58122df84da0SMatthew G. Knepley 
5813f1a722f8SMatthew G. Knepley   Input Parameters:
5814bb7acecfSBarry Smith + dm     - The `DM`
58152df84da0SMatthew G. Knepley . Nc     - The number of components for the field
581620f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5817bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58182df84da0SMatthew G. Knepley 
58192df84da0SMatthew G. Knepley   Output Parameter:
5820bb7acecfSBarry Smith . fem - The `PetscFE`
58212df84da0SMatthew G. Knepley 
582220f4b53cSBarry Smith   Level: intermediate
582320f4b53cSBarry Smith 
5824bb7acecfSBarry Smith   Note:
5825bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58262df84da0SMatthew G. Knepley 
58271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58282df84da0SMatthew G. Knepley @*/
5829d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5830d71ae5a4SJacob Faibussowitsch {
58312df84da0SMatthew G. Knepley   DMPolytopeType ct;
58322df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58332df84da0SMatthew G. Knepley 
58342df84da0SMatthew G. Knepley   PetscFunctionBegin;
58352df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58362df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58374f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58382df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58394f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58409566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58439566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58452df84da0SMatthew G. Knepley }
58462df84da0SMatthew G. Knepley 
58471d3af9e0SMatthew G. Knepley /*@
5848bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5849e5e52638SMatthew G. Knepley 
585020f4b53cSBarry Smith   Collective
5851e5e52638SMatthew G. Knepley 
5852e5e52638SMatthew G. Knepley   Input Parameter:
5853bb7acecfSBarry Smith . dm - The `DM`
5854e5e52638SMatthew G. Knepley 
585520f4b53cSBarry Smith   Options Database Key:
5856bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
585745480ffeSMatthew G. Knepley 
585820f4b53cSBarry Smith   Level: intermediate
585920f4b53cSBarry Smith 
58601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5861e5e52638SMatthew G. Knepley @*/
5862d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5863d71ae5a4SJacob Faibussowitsch {
5864e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5865083401c6SMatthew G. Knepley   PetscDS   dsDef;
5866083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5867f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5868f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5869e5e52638SMatthew G. Knepley 
5870e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5871e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58723ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58739566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58749566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5875083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58769566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5877083401c6SMatthew G. Knepley   Nl   = 0;
5878083401c6SMatthew G. Knepley   Ndef = 0;
5879083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5880083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5881083401c6SMatthew G. Knepley     PetscInt l;
5882083401c6SMatthew G. Knepley 
5883f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5884f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5885f918ec44SMatthew G. Knepley     {
5886f918ec44SMatthew G. Knepley       PetscClassId id;
5887f918ec44SMatthew G. Knepley 
58889566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5889f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5890f918ec44SMatthew G. Knepley         Ceed ceed;
5891f918ec44SMatthew G. Knepley 
58929566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58939566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5894f918ec44SMatthew G. Knepley       }
5895f918ec44SMatthew G. Knepley     }
5896f918ec44SMatthew G. Knepley #endif
58979371c9d4SSatish Balay     if (!label) {
58989371c9d4SSatish Balay       ++Ndef;
58999371c9d4SSatish Balay       continue;
59009371c9d4SSatish Balay     }
59019371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59029371c9d4SSatish Balay       if (label == labelSet[l]) break;
5903083401c6SMatthew G. Knepley     if (l < Nl) continue;
5904083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5905083401c6SMatthew G. Knepley   }
5906083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
590707218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5908083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5909b3cf3223SMatthew G. Knepley     IS        fields;
5910b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5911b3cf3223SMatthew G. Knepley 
59129371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59139371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59147a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59159566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59169371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59179371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59189566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59209566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59219566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
592288f0c812SMatthew G. Knepley 
59239566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
592407218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59259566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59269566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59272df9ee95SMatthew G. Knepley   }
592807218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59299566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5930083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5931083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59320122748bSMatthew G. Knepley     DM              plex;
5933083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5934083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5935083401c6SMatthew G. Knepley     PetscInt       *fields;
5936083401c6SMatthew G. Knepley     const PetscInt *cells;
5937083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59380122748bSMatthew G. Knepley 
59399566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59409566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59419566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59429566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59435fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5944083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5945083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5946083401c6SMatthew G. Knepley       IS      pointIS;
5947083401c6SMatthew G. Knepley 
59489566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59499566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59509566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59519566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5952083401c6SMatthew G. Knepley     }
59539566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5954083401c6SMatthew G. Knepley 
59559566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59569566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59579566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59589566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59619566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5962083401c6SMatthew G. Knepley 
59639566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59649371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59659371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59669566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59689566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59699566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5970083401c6SMatthew G. Knepley 
59719566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
597207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59739566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59749566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59759566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59769566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59779566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5978083401c6SMatthew G. Knepley   }
5979083401c6SMatthew G. Knepley   /* Create label DSes
5980083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5981083401c6SMatthew G. Knepley   */
5982083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5983083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5984083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
598507218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5986083401c6SMatthew G. Knepley     IS        fields;
5987083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5988083401c6SMatthew G. Knepley 
59899566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59909371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59919371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59929566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59939371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59949371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59959566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59979566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59989566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59999566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6000083401c6SMatthew G. Knepley     {
6001083401c6SMatthew G. Knepley       DMPolytopeType ct;
6002083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60035fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60040122748bSMatthew G. Knepley 
60059566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6006665f567fSMatthew G. Knepley       if (lStart >= 0) {
60079566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6008412e9a14SMatthew G. Knepley         switch (ct) {
6009412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6010412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6011412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6012d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6013d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6014d71ae5a4SJacob Faibussowitsch           break;
6015d71ae5a4SJacob Faibussowitsch         default:
6016d71ae5a4SJacob Faibussowitsch           break;
6017412e9a14SMatthew G. Knepley         }
6018665f567fSMatthew G. Knepley       }
6019712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
602007218a29SMatthew G. Knepley       if (isCohesive) {
602107218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
602207218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
602307218a29SMatthew G. Knepley       }
60245fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60255fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60265fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60279566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60289566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
602907218a29SMatthew G. Knepley             if (dsIn) {
603007218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
603107218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
603207218a29SMatthew G. Knepley             }
60335fedec97SMatthew G. Knepley           }
60345fedec97SMatthew G. Knepley           ++nf;
60355fedec97SMatthew G. Knepley         }
60365fedec97SMatthew G. Knepley       }
6037e5e52638SMatthew G. Knepley     }
603807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
603907218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60409566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
604107218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6042e5e52638SMatthew G. Knepley   }
60439566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6044e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6045083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6046083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
604707218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6048083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6049083401c6SMatthew G. Knepley     const PetscInt *fld;
60505fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60515fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6052e5e52638SMatthew G. Knepley 
60539566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60549566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60559566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60569566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6057083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6058083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60595fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6060e5e52638SMatthew G. Knepley       PetscClassId id;
6061e5e52638SMatthew G. Knepley 
60625fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60639566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60645fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
606507218a29SMatthew G. Knepley       if (isCohesive) {
606607218a29SMatthew G. Knepley         if (!isCohesiveField) {
606707218a29SMatthew G. Knepley           PetscObject bdDisc;
606807218a29SMatthew G. Knepley 
606907218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
607007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
607107218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
607207218a29SMatthew G. Knepley         } else {
60739566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
607407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
607507218a29SMatthew G. Knepley         }
607607218a29SMatthew G. Knepley       } else {
607707218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
607807218a29SMatthew G. Knepley       }
6079083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60809566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6081e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6082e5e52638SMatthew G. Knepley     }
60839566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6084e5e52638SMatthew G. Knepley   }
6085f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6087f9244615SMatthew G. Knepley   if (flg) {
60883b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60893b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
609007218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60913b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60923b4aee56SMatthew G. Knepley 
60939566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
609407218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
609507218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
609607218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
609707218a29SMatthew G. Knepley       }
60983b4aee56SMatthew G. Knepley     }
6099f9244615SMatthew G. Knepley   }
6100e5e52638SMatthew G. Knepley   /* Setup DSes */
6101e5e52638SMatthew G. Knepley   if (doSetup) {
610207218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6103cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6104cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6105cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6106cf1363e4SMatthew G. Knepley       }
610707218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
610807218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
610907218a29SMatthew G. Knepley     }
6110e5e52638SMatthew G. Knepley   }
61113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6112e5e52638SMatthew G. Knepley }
6113e5e52638SMatthew G. Knepley 
6114e5e52638SMatthew G. Knepley /*@
6115d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6116d2b2dc1eSMatthew G. Knepley 
6117d2b2dc1eSMatthew G. Knepley   Input Parameters:
6118d2b2dc1eSMatthew G. Knepley + dm     - The DM
6119d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6120d2b2dc1eSMatthew G. Knepley 
6121d2b2dc1eSMatthew G. Knepley   Level: developer
6122d2b2dc1eSMatthew G. Knepley 
6123d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6124d2b2dc1eSMatthew G. Knepley @*/
6125d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6126d2b2dc1eSMatthew G. Knepley {
6127d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6128d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6129d2b2dc1eSMatthew G. Knepley 
6130d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6131d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6132d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6133d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6134d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6135d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6136d2b2dc1eSMatthew G. Knepley 
6137d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6138d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6139d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6140d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6141d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6142d2b2dc1eSMatthew G. Knepley 
6143d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6144d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6145d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6146d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6147d2b2dc1eSMatthew G. Knepley   }
6148d2b2dc1eSMatthew G. Knepley   if (tensor) {
6149d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6150d2b2dc1eSMatthew G. Knepley   } else {
6151d2b2dc1eSMatthew G. Knepley     PetscSection s;
6152d2b2dc1eSMatthew G. Knepley 
6153d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6154d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6155d2b2dc1eSMatthew G. Knepley   }
6156d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6157d2b2dc1eSMatthew G. Knepley }
6158d2b2dc1eSMatthew G. Knepley 
6159d2b2dc1eSMatthew G. Knepley /*@
6160bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61617f96f943SMatthew G. Knepley 
616220f4b53cSBarry Smith   Collective
6163f2cacb80SMatthew G. Knepley 
61647f96f943SMatthew G. Knepley   Input Parameters:
6165bb7acecfSBarry Smith + dm   - The `DM`
61667f96f943SMatthew G. Knepley - time - The time
61677f96f943SMatthew G. Knepley 
61687f96f943SMatthew G. Knepley   Output Parameters:
616920f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
617020f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
617120f4b53cSBarry Smith 
617220f4b53cSBarry Smith   Level: developer
61737f96f943SMatthew G. Knepley 
6174bb7acecfSBarry Smith   Note:
6175bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61767f96f943SMatthew G. Knepley 
61771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61787f96f943SMatthew G. Knepley @*/
6179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6180d71ae5a4SJacob Faibussowitsch {
61817f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61827f96f943SMatthew G. Knepley   void   **ectxs;
6183f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61847f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61857f96f943SMatthew G. Knepley 
61867f96f943SMatthew G. Knepley   PetscFunctionBegin;
6187f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6188f60fa741SMatthew G. Knepley   if (u) {
6189f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6190f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6191f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6192f60fa741SMatthew G. Knepley   }
6193f60fa741SMatthew G. Knepley   if (u_t) {
6194f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6195f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6196f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6197f60fa741SMatthew G. Knepley   }
61989566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
61999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62009566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62017f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62027f96f943SMatthew G. Knepley     PetscDS         ds;
62037f96f943SMatthew G. Knepley     DMLabel         label;
62047f96f943SMatthew G. Knepley     IS              fieldIS;
62057f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62067f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62077f96f943SMatthew G. Knepley 
620807218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62099566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62109566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62119566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62129566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6213f2cacb80SMatthew G. Knepley     if (u) {
6214f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6215f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6216f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62177f96f943SMatthew G. Knepley     }
6218f2cacb80SMatthew G. Knepley     if (u_t) {
62199566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62209566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6221f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6222f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6223f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6224f2cacb80SMatthew G. Knepley     }
62259566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6226f2cacb80SMatthew G. Knepley   }
6227f2cacb80SMatthew G. Knepley   if (u) {
62289566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62299566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6230f2cacb80SMatthew G. Knepley   }
6231f2cacb80SMatthew G. Knepley   if (u_t) {
62329566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62339566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6234f2cacb80SMatthew G. Knepley   }
62359566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6236f60fa741SMatthew G. Knepley   if (u) {
6237f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6238f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6239f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6240f60fa741SMatthew G. Knepley   }
6241f60fa741SMatthew G. Knepley   if (u_t) {
6242f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6243f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6244f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6245f60fa741SMatthew G. Knepley   }
62463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62477f96f943SMatthew G. Knepley }
62487f96f943SMatthew G. Knepley 
624907218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6250d71ae5a4SJacob Faibussowitsch {
625107218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
625245480ffeSMatthew G. Knepley 
625345480ffeSMatthew G. Knepley   PetscFunctionBegin;
62549566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
625507218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
625607218a29SMatthew G. Knepley   if (dsIn) {
625707218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
625807218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
625945480ffeSMatthew G. Knepley   }
626007218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62619566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
626207218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
626445480ffeSMatthew G. Knepley }
626545480ffeSMatthew G. Knepley 
62667f96f943SMatthew G. Knepley /*@
6267bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6268e5e52638SMatthew G. Knepley 
626920f4b53cSBarry Smith   Collective
6270e5e52638SMatthew G. Knepley 
6271e5e52638SMatthew G. Knepley   Input Parameter:
6272bb7acecfSBarry Smith . dm - The `DM`
6273e5e52638SMatthew G. Knepley 
6274e5e52638SMatthew G. Knepley   Output Parameter:
6275bb7acecfSBarry Smith . newdm - The `DM`
6276e5e52638SMatthew G. Knepley 
6277e5e52638SMatthew G. Knepley   Level: advanced
6278e5e52638SMatthew G. Knepley 
62791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6280e5e52638SMatthew G. Knepley @*/
6281d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6282d71ae5a4SJacob Faibussowitsch {
6283e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6284e5e52638SMatthew G. Knepley 
6285e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62863ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62879566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62889566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6289e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6290e5e52638SMatthew G. Knepley     DMLabel  label;
6291b3cf3223SMatthew G. Knepley     IS       fields;
629207218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6293783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6294e5e52638SMatthew G. Knepley 
629507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6296b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
629707218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6298d5b43468SJose E. Roman     /* Complete new labels in the new DS */
629907218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63009566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6301783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6302b8025e53SMatthew G. Knepley       PetscWeakForm wf;
630345480ffeSMatthew G. Knepley       DMLabel       label;
6304783e2ec8SMatthew G. Knepley       PetscInt      field;
6305783e2ec8SMatthew G. Knepley 
63069566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63079566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6308783e2ec8SMatthew G. Knepley     }
6309e5e52638SMatthew G. Knepley   }
6310799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6312e5e52638SMatthew G. Knepley }
6313e5e52638SMatthew G. Knepley 
6314e5e52638SMatthew G. Knepley /*@
6315bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6316e5e52638SMatthew G. Knepley 
631720f4b53cSBarry Smith   Collective
6318e5e52638SMatthew G. Knepley 
6319e5e52638SMatthew G. Knepley   Input Parameter:
6320bb7acecfSBarry Smith . dm - The `DM`
6321e5e52638SMatthew G. Knepley 
6322e5e52638SMatthew G. Knepley   Output Parameter:
6323bb7acecfSBarry Smith . newdm - The `DM`
6324e5e52638SMatthew G. Knepley 
6325e5e52638SMatthew G. Knepley   Level: advanced
6326e5e52638SMatthew G. Knepley 
632773ff1848SBarry Smith   Developer Note:
6328bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6329bb7acecfSBarry Smith 
63301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6331e5e52638SMatthew G. Knepley @*/
6332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6333d71ae5a4SJacob Faibussowitsch {
6334e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63359566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63369566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6338e5e52638SMatthew G. Knepley }
6339e5e52638SMatthew G. Knepley 
6340c73cfb54SMatthew G. Knepley /*@
6341bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6342c73cfb54SMatthew G. Knepley 
634320f4b53cSBarry Smith   Not Collective
6344c73cfb54SMatthew G. Knepley 
6345c73cfb54SMatthew G. Knepley   Input Parameter:
6346bb7acecfSBarry Smith . dm - The `DM`
6347c73cfb54SMatthew G. Knepley 
6348c73cfb54SMatthew G. Knepley   Output Parameter:
6349c73cfb54SMatthew G. Knepley . dim - The topological dimension
6350c73cfb54SMatthew G. Knepley 
6351c73cfb54SMatthew G. Knepley   Level: beginner
6352c73cfb54SMatthew G. Knepley 
63531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6354c73cfb54SMatthew G. Knepley @*/
6355d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6356d71ae5a4SJacob Faibussowitsch {
6357c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6358c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63594f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6360c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6362c73cfb54SMatthew G. Knepley }
6363c73cfb54SMatthew G. Knepley 
6364c73cfb54SMatthew G. Knepley /*@
6365bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6366c73cfb54SMatthew G. Knepley 
636720f4b53cSBarry Smith   Collective
6368c73cfb54SMatthew G. Knepley 
6369c73cfb54SMatthew G. Knepley   Input Parameters:
6370bb7acecfSBarry Smith + dm  - The `DM`
6371c73cfb54SMatthew G. Knepley - dim - The topological dimension
6372c73cfb54SMatthew G. Knepley 
6373c73cfb54SMatthew G. Knepley   Level: beginner
6374c73cfb54SMatthew G. Knepley 
63751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6376c73cfb54SMatthew G. Knepley @*/
6377d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6378d71ae5a4SJacob Faibussowitsch {
6379e5e52638SMatthew G. Knepley   PetscDS  ds;
638045480ffeSMatthew G. Knepley   PetscInt Nds, n;
6381f17e8794SMatthew G. Knepley 
6382c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6383c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6384c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6385c73cfb54SMatthew G. Knepley   dm->dim = dim;
6386d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63879566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
638845480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
638907218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63909566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
639145480ffeSMatthew G. Knepley     }
6392d17bd122SMatthew G. Knepley   }
63933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6394c73cfb54SMatthew G. Knepley }
6395c73cfb54SMatthew G. Knepley 
6396793f3fe5SMatthew G. Knepley /*@
6397793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6398793f3fe5SMatthew G. Knepley 
639920f4b53cSBarry Smith   Collective
6400793f3fe5SMatthew G. Knepley 
6401793f3fe5SMatthew G. Knepley   Input Parameters:
6402bb7acecfSBarry Smith + dm  - the `DM`
6403793f3fe5SMatthew G. Knepley - dim - the dimension
6404793f3fe5SMatthew G. Knepley 
6405793f3fe5SMatthew G. Knepley   Output Parameters:
6406793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6407aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6408793f3fe5SMatthew G. Knepley 
640920f4b53cSBarry Smith   Level: intermediate
641020f4b53cSBarry Smith 
6411793f3fe5SMatthew G. Knepley   Note:
6412793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6413a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6414793f3fe5SMatthew G. Knepley   then the interval is empty.
6415793f3fe5SMatthew G. Knepley 
64161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6417793f3fe5SMatthew G. Knepley @*/
6418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6419d71ae5a4SJacob Faibussowitsch {
6420793f3fe5SMatthew G. Knepley   PetscInt d;
6421793f3fe5SMatthew G. Knepley 
6422793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6423793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64249566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64257a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6426dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6428793f3fe5SMatthew G. Knepley }
6429793f3fe5SMatthew G. Knepley 
64306636e97aSMatthew G Knepley /*@
6431bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6432f4d763aaSMatthew G. Knepley 
643320f4b53cSBarry Smith   Collective
64348f700142SStefano Zampini 
6435f4d763aaSMatthew G. Knepley   Input Parameter:
6436bb7acecfSBarry Smith . dm - The original `DM`
6437f4d763aaSMatthew G. Knepley 
6438f4d763aaSMatthew G. Knepley   Output Parameter:
6439bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6440f4d763aaSMatthew G. Knepley 
6441f4d763aaSMatthew G. Knepley   Level: intermediate
6442f4d763aaSMatthew G. Knepley 
6443bb7acecfSBarry Smith   Note:
6444bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6445bb7acecfSBarry 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
6446bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6447bb7acecfSBarry Smith 
64481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6449f4d763aaSMatthew G. Knepley @*/
6450d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6451d71ae5a4SJacob Faibussowitsch {
6452c26acbdeSMatthew G. Knepley   PetscSection section;
6453eb9d3e4dSMatthew G. Knepley   IS           perm;
6454eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
645514f150ffSMatthew G. Knepley 
645614f150ffSMatthew G. Knepley   PetscFunctionBegin;
645714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64584f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64599566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64609566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6461eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6462eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6463eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6464eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6465c26acbdeSMatthew G. Knepley     *odm = dm;
64663ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6467c26acbdeSMatthew G. Knepley   }
646814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6469c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
647014f150ffSMatthew G. Knepley     PetscSF      sf;
6471eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
647214f150ffSMatthew G. Knepley 
64739566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64749566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64759566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64769566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64779566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64789566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6479eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64809566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64819566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
648214f150ffSMatthew G. Knepley   }
648314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
648514f150ffSMatthew G. Knepley }
6486f4d763aaSMatthew G. Knepley 
6487f4d763aaSMatthew G. Knepley /*@
6488cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6489f4d763aaSMatthew G. Knepley 
6490f4d763aaSMatthew G. Knepley   Input Parameter:
6491bb7acecfSBarry Smith . dm - The original `DM`
6492f4d763aaSMatthew G. Knepley 
6493cdb7a50dSMatthew G. Knepley   Output Parameters:
6494cdb7a50dSMatthew G. Knepley + num - The output sequence number
6495cdb7a50dSMatthew G. Knepley - val - The output sequence value
6496f4d763aaSMatthew G. Knepley 
6497f4d763aaSMatthew G. Knepley   Level: intermediate
6498f4d763aaSMatthew G. Knepley 
6499bb7acecfSBarry Smith   Note:
6500bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6501bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6502bb7acecfSBarry Smith 
650373ff1848SBarry Smith   Developer Note:
6504bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6505bb7acecfSBarry Smith   not directly related to the `DM`.
6506f4d763aaSMatthew G. Knepley 
65071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6508f4d763aaSMatthew G. Knepley @*/
6509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6510d71ae5a4SJacob Faibussowitsch {
6511f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6512f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65139371c9d4SSatish Balay   if (num) {
65144f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65159371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65169371c9d4SSatish Balay   }
65179371c9d4SSatish Balay   if (val) {
65184f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65199371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65209371c9d4SSatish Balay   }
65213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6522f4d763aaSMatthew G. Knepley }
6523f4d763aaSMatthew G. Knepley 
6524f4d763aaSMatthew G. Knepley /*@
6525cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6526f4d763aaSMatthew G. Knepley 
6527f4d763aaSMatthew G. Knepley   Input Parameters:
6528bb7acecfSBarry Smith + dm  - The original `DM`
6529cdb7a50dSMatthew G. Knepley . num - The output sequence number
6530cdb7a50dSMatthew G. Knepley - val - The output sequence value
6531f4d763aaSMatthew G. Knepley 
6532f4d763aaSMatthew G. Knepley   Level: intermediate
6533f4d763aaSMatthew G. Knepley 
6534bb7acecfSBarry Smith   Note:
6535bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6536bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6537f4d763aaSMatthew G. Knepley 
65381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6539f4d763aaSMatthew G. Knepley @*/
6540d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6541d71ae5a4SJacob Faibussowitsch {
6542f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6543f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6544f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6545cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6547cdb7a50dSMatthew G. Knepley }
6548cdb7a50dSMatthew G. Knepley 
65495d83a8b1SBarry Smith /*@
6550bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6551cdb7a50dSMatthew G. Knepley 
6552cdb7a50dSMatthew G. Knepley   Input Parameters:
6553bb7acecfSBarry Smith + dm     - The original `DM`
6554b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6555cdb7a50dSMatthew G. Knepley . name   - The sequence name
6556cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6557cdb7a50dSMatthew G. Knepley 
6558cdb7a50dSMatthew G. Knepley   Output Parameter:
6559cdb7a50dSMatthew G. Knepley . val - The output sequence value
6560cdb7a50dSMatthew G. Knepley 
6561cdb7a50dSMatthew G. Knepley   Level: intermediate
6562cdb7a50dSMatthew G. Knepley 
6563bb7acecfSBarry Smith   Note:
6564bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6565bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6566bb7acecfSBarry Smith 
656773ff1848SBarry Smith   Developer Note:
6568bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6569cdb7a50dSMatthew G. Knepley 
65701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6571cdb7a50dSMatthew G. Knepley @*/
6572b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6573d71ae5a4SJacob Faibussowitsch {
6574cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6575cdb7a50dSMatthew G. Knepley 
6576cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6577cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6578cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6579b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
65804f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65819566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6582cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6583cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6584cdb7a50dSMatthew G. Knepley     PetscScalar value;
6585cdb7a50dSMatthew G. Knepley 
65869566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65874aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6588cdb7a50dSMatthew G. Knepley #endif
6589cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6591f4d763aaSMatthew G. Knepley }
65928e4ac7eaSMatthew G. Knepley 
65938e4ac7eaSMatthew G. Knepley /*@
6594b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6595b2033f5dSMatthew G. Knepley 
6596b2033f5dSMatthew G. Knepley   Input Parameters:
6597b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6598b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6599b2033f5dSMatthew G. Knepley - name   - The sequence name
6600b2033f5dSMatthew G. Knepley 
6601b2033f5dSMatthew G. Knepley   Output Parameter:
6602b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6603b2033f5dSMatthew G. Knepley 
6604b2033f5dSMatthew G. Knepley   Level: intermediate
6605b2033f5dSMatthew G. Knepley 
6606b2033f5dSMatthew G. Knepley   Note:
6607b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6608b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6609b2033f5dSMatthew G. Knepley 
6610b2033f5dSMatthew G. Knepley   Developer Note:
6611b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6612b2033f5dSMatthew G. Knepley 
6613b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6614b2033f5dSMatthew G. Knepley @*/
6615b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6616b2033f5dSMatthew G. Knepley {
6617b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6618b2033f5dSMatthew G. Knepley 
6619b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6620b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6621b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6622b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6623b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6624b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6625b2033f5dSMatthew G. Knepley   if (ishdf5) {
6626b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6627b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6628b2033f5dSMatthew G. Knepley #endif
6629b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6630b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6631b2033f5dSMatthew G. Knepley }
6632b2033f5dSMatthew G. Knepley 
6633b2033f5dSMatthew G. Knepley /*@
6634bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66358e4ac7eaSMatthew G. Knepley 
663620f4b53cSBarry Smith   Not Collective
66378e4ac7eaSMatthew G. Knepley 
66388e4ac7eaSMatthew G. Knepley   Input Parameter:
6639bb7acecfSBarry Smith . dm - The `DM`
66408e4ac7eaSMatthew G. Knepley 
66418e4ac7eaSMatthew G. Knepley   Output Parameter:
6642bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66438e4ac7eaSMatthew G. Knepley 
66448e4ac7eaSMatthew G. Knepley   Level: beginner
66458e4ac7eaSMatthew G. Knepley 
66461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66478e4ac7eaSMatthew G. Knepley @*/
6648d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6649d71ae5a4SJacob Faibussowitsch {
66508e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66518e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66524f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66538e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66558e4ac7eaSMatthew G. Knepley }
66568e4ac7eaSMatthew G. Knepley 
66578e4ac7eaSMatthew G. Knepley /*@
6658bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66598e4ac7eaSMatthew G. Knepley 
666020f4b53cSBarry Smith   Collective
66618e4ac7eaSMatthew G. Knepley 
66628e4ac7eaSMatthew G. Knepley   Input Parameters:
6663bb7acecfSBarry Smith + dm         - The `DM`
6664bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66658e4ac7eaSMatthew G. Knepley 
666673ff1848SBarry Smith   Level: beginner
666773ff1848SBarry Smith 
6668bb7acecfSBarry Smith   Note:
6669bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66705d3b26e6SMatthew G. Knepley 
66711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66728e4ac7eaSMatthew G. Knepley @*/
6673d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6674d71ae5a4SJacob Faibussowitsch {
66758e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66768e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66778833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66788e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66808e4ac7eaSMatthew G. Knepley }
6681c58f1c22SToby Isaac 
6682cc4c1da9SBarry Smith /*@
6683bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Not Collective
6686c58f1c22SToby Isaac 
6687c58f1c22SToby Isaac   Input Parameters:
6688bb7acecfSBarry Smith + dm   - The `DM` object
6689c58f1c22SToby Isaac - name - The label name
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   Level: intermediate
6692c58f1c22SToby Isaac 
66931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6694c58f1c22SToby Isaac @*/
6695d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6696d71ae5a4SJacob Faibussowitsch {
66975d80c0bfSVaclav Hapla   PetscBool flg;
66985d80c0bfSVaclav Hapla   DMLabel   label;
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   PetscFunctionBegin;
6701c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67024f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67039566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6704c58f1c22SToby Isaac   if (!flg) {
67059566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67069566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67079566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6708c58f1c22SToby Isaac   }
67093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6710c58f1c22SToby Isaac }
6711c58f1c22SToby Isaac 
6712cc4c1da9SBarry Smith /*@
6713bb7acecfSBarry 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.
67140fdc7489SMatthew Knepley 
67150fdc7489SMatthew Knepley   Not Collective
67160fdc7489SMatthew Knepley 
67170fdc7489SMatthew Knepley   Input Parameters:
6718bb7acecfSBarry Smith + dm   - The `DM` object
67190fdc7489SMatthew Knepley . l    - The index for the label
67200fdc7489SMatthew Knepley - name - The label name
67210fdc7489SMatthew Knepley 
67220fdc7489SMatthew Knepley   Level: intermediate
67230fdc7489SMatthew Knepley 
67241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67250fdc7489SMatthew Knepley @*/
6726d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6727d71ae5a4SJacob Faibussowitsch {
67280fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67290fdc7489SMatthew Knepley   DMLabel     label;
67300fdc7489SMatthew Knepley   PetscInt    Nl, m;
67310fdc7489SMatthew Knepley   PetscBool   flg, match;
67320fdc7489SMatthew Knepley   const char *lname;
67330fdc7489SMatthew Knepley 
67340fdc7489SMatthew Knepley   PetscFunctionBegin;
67350fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67364f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67379566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67380fdc7489SMatthew Knepley   if (!flg) {
67399566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67409566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67419566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67420fdc7489SMatthew Knepley   }
67439566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
674463a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67450fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67469566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67479566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67480fdc7489SMatthew Knepley     if (match) break;
67490fdc7489SMatthew Knepley   }
67503ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67510fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67520fdc7489SMatthew Knepley   else prev->next = orig->next;
67530fdc7489SMatthew Knepley   if (!l) {
67540fdc7489SMatthew Knepley     orig->next = dm->labels;
67550fdc7489SMatthew Knepley     dm->labels = orig;
67560fdc7489SMatthew Knepley   } else {
6757fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67580fdc7489SMatthew Knepley     orig->next = prev->next;
67590fdc7489SMatthew Knepley     prev->next = orig;
67600fdc7489SMatthew Knepley   }
67613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67620fdc7489SMatthew Knepley }
67630fdc7489SMatthew Knepley 
6764cc4c1da9SBarry Smith /*@
6765bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   Not Collective
6768c58f1c22SToby Isaac 
6769c58f1c22SToby Isaac   Input Parameters:
6770bb7acecfSBarry Smith + dm    - The `DM` object
6771c58f1c22SToby Isaac . name  - The label name
6772c58f1c22SToby Isaac - point - The mesh point
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac   Output Parameter:
6775c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6776c58f1c22SToby Isaac 
6777c58f1c22SToby Isaac   Level: beginner
6778c58f1c22SToby Isaac 
67791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6780c58f1c22SToby Isaac @*/
6781d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6782d71ae5a4SJacob Faibussowitsch {
6783c58f1c22SToby Isaac   DMLabel label;
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac   PetscFunctionBegin;
6786c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67874f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67897a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6792c58f1c22SToby Isaac }
6793c58f1c22SToby Isaac 
6794cc4c1da9SBarry Smith /*@
6795bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Not Collective
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Input Parameters:
6800bb7acecfSBarry Smith + dm    - The `DM` object
6801c58f1c22SToby Isaac . name  - The label name
6802c58f1c22SToby Isaac . point - The mesh point
6803c58f1c22SToby Isaac - value - The label value for this point
6804c58f1c22SToby Isaac 
6805c58f1c22SToby Isaac   Output Parameter:
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac   Level: beginner
6808c58f1c22SToby Isaac 
68091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6810c58f1c22SToby Isaac @*/
6811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6812d71ae5a4SJacob Faibussowitsch {
6813c58f1c22SToby Isaac   DMLabel label;
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   PetscFunctionBegin;
6816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6819c58f1c22SToby Isaac   if (!label) {
68209566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68219566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6822c58f1c22SToby Isaac   }
68239566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6825c58f1c22SToby Isaac }
6826c58f1c22SToby Isaac 
6827cc4c1da9SBarry Smith /*@
6828bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   Not Collective
6831c58f1c22SToby Isaac 
6832c58f1c22SToby Isaac   Input Parameters:
6833bb7acecfSBarry Smith + dm    - The `DM` object
6834c58f1c22SToby Isaac . name  - The label name
6835c58f1c22SToby Isaac . point - The mesh point
6836c58f1c22SToby Isaac - value - The label value for this point
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Level: beginner
6839c58f1c22SToby Isaac 
68401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6841c58f1c22SToby Isaac @*/
6842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6843d71ae5a4SJacob Faibussowitsch {
6844c58f1c22SToby Isaac   DMLabel label;
6845c58f1c22SToby Isaac 
6846c58f1c22SToby Isaac   PetscFunctionBegin;
6847c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68484f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68499566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68503ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68519566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6853c58f1c22SToby Isaac }
6854c58f1c22SToby Isaac 
6855cc4c1da9SBarry Smith /*@
6856bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6857c58f1c22SToby Isaac 
6858c58f1c22SToby Isaac   Not Collective
6859c58f1c22SToby Isaac 
6860c58f1c22SToby Isaac   Input Parameters:
6861bb7acecfSBarry Smith + dm   - The `DM` object
6862c58f1c22SToby Isaac - name - The label name
6863c58f1c22SToby Isaac 
6864c58f1c22SToby Isaac   Output Parameter:
6865c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6866c58f1c22SToby Isaac 
6867c58f1c22SToby Isaac   Level: beginner
6868c58f1c22SToby Isaac 
686973ff1848SBarry Smith   Developer Note:
6870bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6871bb7acecfSBarry Smith 
68721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6873c58f1c22SToby Isaac @*/
6874d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6875d71ae5a4SJacob Faibussowitsch {
6876c58f1c22SToby Isaac   DMLabel label;
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac   PetscFunctionBegin;
6879c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68804f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68814f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68829566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6883c58f1c22SToby Isaac   *size = 0;
68843ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68859566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6887c58f1c22SToby Isaac }
6888c58f1c22SToby Isaac 
6889cc4c1da9SBarry Smith /*@
6890bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6891c58f1c22SToby Isaac 
6892c58f1c22SToby Isaac   Not Collective
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Input Parameters:
689560225df5SJacob Faibussowitsch + dm   - The `DM` object
6896c58f1c22SToby Isaac - name - The label name
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac   Output Parameter:
689920f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6900c58f1c22SToby Isaac 
6901c58f1c22SToby Isaac   Level: beginner
6902c58f1c22SToby Isaac 
69031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6904c58f1c22SToby Isaac @*/
6905d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6906d71ae5a4SJacob Faibussowitsch {
6907c58f1c22SToby Isaac   DMLabel label;
6908c58f1c22SToby Isaac 
6909c58f1c22SToby Isaac   PetscFunctionBegin;
6910c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69114f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69124f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69139566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6914c58f1c22SToby Isaac   *ids = NULL;
6915dab2e251SBlaise Bourdin   if (label) {
69169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6917dab2e251SBlaise Bourdin   } else {
6918dab2e251SBlaise Bourdin     /* returning an empty IS */
69199566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6920dab2e251SBlaise Bourdin   }
69213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6922c58f1c22SToby Isaac }
6923c58f1c22SToby Isaac 
6924cc4c1da9SBarry Smith /*@
6925c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6926c58f1c22SToby Isaac 
6927c58f1c22SToby Isaac   Not Collective
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac   Input Parameters:
6930bb7acecfSBarry Smith + dm    - The `DM` object
6931b6971eaeSBarry Smith . name  - The label name of the stratum
6932c58f1c22SToby Isaac - value - The stratum value
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Output Parameter:
6935bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac   Level: beginner
6938c58f1c22SToby Isaac 
69391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6940c58f1c22SToby Isaac @*/
6941d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6942d71ae5a4SJacob Faibussowitsch {
6943c58f1c22SToby Isaac   DMLabel label;
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac   PetscFunctionBegin;
6946c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69474f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69484f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69499566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6950c58f1c22SToby Isaac   *size = 0;
69513ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6954c58f1c22SToby Isaac }
6955c58f1c22SToby Isaac 
6956cc4c1da9SBarry Smith /*@
6957c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Not Collective
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac   Input Parameters:
6962bb7acecfSBarry Smith + dm    - The `DM` object
6963c58f1c22SToby Isaac . name  - The label name
6964c58f1c22SToby Isaac - value - The stratum value
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   Output Parameter:
696720f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6968c58f1c22SToby Isaac 
6969c58f1c22SToby Isaac   Level: beginner
6970c58f1c22SToby Isaac 
69711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6972c58f1c22SToby Isaac @*/
6973d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6974d71ae5a4SJacob Faibussowitsch {
6975c58f1c22SToby Isaac   DMLabel label;
6976c58f1c22SToby Isaac 
6977c58f1c22SToby Isaac   PetscFunctionBegin;
6978c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69794f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69804f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69819566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6982c58f1c22SToby Isaac   *points = NULL;
69833ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69849566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6986c58f1c22SToby Isaac }
6987c58f1c22SToby Isaac 
6988cc4c1da9SBarry Smith /*@
69899044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69904de306b1SToby Isaac 
69914de306b1SToby Isaac   Not Collective
69924de306b1SToby Isaac 
69934de306b1SToby Isaac   Input Parameters:
6994bb7acecfSBarry Smith + dm     - The `DM` object
69954de306b1SToby Isaac . name   - The label name
69964de306b1SToby Isaac . value  - The stratum value
69974de306b1SToby Isaac - points - The stratum points
69984de306b1SToby Isaac 
69994de306b1SToby Isaac   Level: beginner
70004de306b1SToby Isaac 
70011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70024de306b1SToby Isaac @*/
7003d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7004d71ae5a4SJacob Faibussowitsch {
70054de306b1SToby Isaac   DMLabel label;
70064de306b1SToby Isaac 
70074de306b1SToby Isaac   PetscFunctionBegin;
70084de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70094f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7010292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70123ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70139566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70154de306b1SToby Isaac }
70164de306b1SToby Isaac 
7017cc4c1da9SBarry Smith /*@
7018bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   Not Collective
7021c58f1c22SToby Isaac 
7022c58f1c22SToby Isaac   Input Parameters:
7023bb7acecfSBarry Smith + dm    - The `DM` object
7024c58f1c22SToby Isaac . name  - The label name
7025c58f1c22SToby Isaac - value - The label value for this point
7026c58f1c22SToby Isaac 
7027c58f1c22SToby Isaac   Output Parameter:
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Level: beginner
7030c58f1c22SToby Isaac 
70311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7032c58f1c22SToby Isaac @*/
7033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7034d71ae5a4SJacob Faibussowitsch {
7035c58f1c22SToby Isaac   DMLabel label;
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   PetscFunctionBegin;
7038c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70394f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70409566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70413ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70429566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7044c58f1c22SToby Isaac }
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac /*@
7047bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7048c58f1c22SToby Isaac 
7049c58f1c22SToby Isaac   Not Collective
7050c58f1c22SToby Isaac 
7051c58f1c22SToby Isaac   Input Parameter:
7052bb7acecfSBarry Smith . dm - The `DM` object
7053c58f1c22SToby Isaac 
7054c58f1c22SToby Isaac   Output Parameter:
7055c58f1c22SToby Isaac . numLabels - the number of Labels
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Level: intermediate
7058c58f1c22SToby Isaac 
70591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7060c58f1c22SToby Isaac @*/
7061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7062d71ae5a4SJacob Faibussowitsch {
70635d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7064c58f1c22SToby Isaac   PetscInt    n    = 0;
7065c58f1c22SToby Isaac 
7066c58f1c22SToby Isaac   PetscFunctionBegin;
7067c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70684f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70699371c9d4SSatish Balay   while (next) {
70709371c9d4SSatish Balay     ++n;
70719371c9d4SSatish Balay     next = next->next;
70729371c9d4SSatish Balay   }
7073c58f1c22SToby Isaac   *numLabels = n;
70743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7075c58f1c22SToby Isaac }
7076c58f1c22SToby Isaac 
7077cc4c1da9SBarry Smith /*@
7078c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac   Not Collective
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Input Parameters:
7083bb7acecfSBarry Smith + dm - The `DM` object
7084c58f1c22SToby Isaac - n  - the label number
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Output Parameter:
7087c58f1c22SToby Isaac . name - the label name
7088c58f1c22SToby Isaac 
7089c58f1c22SToby Isaac   Level: intermediate
7090c58f1c22SToby Isaac 
709173ff1848SBarry Smith   Developer Note:
7092bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7093bb7acecfSBarry Smith 
70941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7095c58f1c22SToby Isaac @*/
7096cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7097d71ae5a4SJacob Faibussowitsch {
70985d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7099c58f1c22SToby Isaac   PetscInt    l    = 0;
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac   PetscFunctionBegin;
7102c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71034f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7104c58f1c22SToby Isaac   while (next) {
7105c58f1c22SToby Isaac     if (l == n) {
71069566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71073ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7108c58f1c22SToby Isaac     }
7109c58f1c22SToby Isaac     ++l;
7110c58f1c22SToby Isaac     next = next->next;
7111c58f1c22SToby Isaac   }
711263a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7113c58f1c22SToby Isaac }
7114c58f1c22SToby Isaac 
7115cc4c1da9SBarry Smith /*@
7116bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   Not Collective
7119c58f1c22SToby Isaac 
7120c58f1c22SToby Isaac   Input Parameters:
7121bb7acecfSBarry Smith + dm   - The `DM` object
7122c58f1c22SToby Isaac - name - The label name
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac   Output Parameter:
7125bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7126c58f1c22SToby Isaac 
7127c58f1c22SToby Isaac   Level: intermediate
7128c58f1c22SToby Isaac 
71291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7130c58f1c22SToby Isaac @*/
7131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7132d71ae5a4SJacob Faibussowitsch {
71335d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7134d67d17b1SMatthew G. Knepley   const char *lname;
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac   PetscFunctionBegin;
7137c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71384f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71394f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7140c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7141c58f1c22SToby Isaac   while (next) {
71429566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71439566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7144c58f1c22SToby Isaac     if (*hasLabel) break;
7145c58f1c22SToby Isaac     next = next->next;
7146c58f1c22SToby Isaac   }
71473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7148c58f1c22SToby Isaac }
7149c58f1c22SToby Isaac 
7150a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7151cc4c1da9SBarry Smith /*@
715220f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Not Collective
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Input Parameters:
7157bb7acecfSBarry Smith + dm   - The `DM` object
7158c58f1c22SToby Isaac - name - The label name
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Output Parameter:
716120f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7162c58f1c22SToby Isaac 
7163bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7164bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7165bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7166bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7167bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7168bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7169bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71706d7c9049SMatthew G. Knepley 
7171c58f1c22SToby Isaac   Level: intermediate
7172c58f1c22SToby Isaac 
717360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7174c58f1c22SToby Isaac @*/
7175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7176d71ae5a4SJacob Faibussowitsch {
71775d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7178c58f1c22SToby Isaac   PetscBool   hasLabel;
7179d67d17b1SMatthew G. Knepley   const char *lname;
7180c58f1c22SToby Isaac 
7181c58f1c22SToby Isaac   PetscFunctionBegin;
7182c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71844f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7185c58f1c22SToby Isaac   *label = NULL;
7186c58f1c22SToby Isaac   while (next) {
71879566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71889566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7189c58f1c22SToby Isaac     if (hasLabel) {
7190c58f1c22SToby Isaac       *label = next->label;
7191c58f1c22SToby Isaac       break;
7192c58f1c22SToby Isaac     }
7193c58f1c22SToby Isaac     next = next->next;
7194c58f1c22SToby Isaac   }
71953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7196c58f1c22SToby Isaac }
7197c58f1c22SToby Isaac 
7198cc4c1da9SBarry Smith /*@
7199bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   Not Collective
7202c58f1c22SToby Isaac 
7203c58f1c22SToby Isaac   Input Parameters:
7204bb7acecfSBarry Smith + dm - The `DM` object
7205c58f1c22SToby Isaac - n  - the label number
7206c58f1c22SToby Isaac 
7207c58f1c22SToby Isaac   Output Parameter:
7208c58f1c22SToby Isaac . label - the label
7209c58f1c22SToby Isaac 
7210c58f1c22SToby Isaac   Level: intermediate
7211c58f1c22SToby Isaac 
72121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7213c58f1c22SToby Isaac @*/
7214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7215d71ae5a4SJacob Faibussowitsch {
72165d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7217c58f1c22SToby Isaac   PetscInt    l    = 0;
7218c58f1c22SToby Isaac 
7219c58f1c22SToby Isaac   PetscFunctionBegin;
7220c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72214f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7222c58f1c22SToby Isaac   while (next) {
7223c58f1c22SToby Isaac     if (l == n) {
7224c58f1c22SToby Isaac       *label = next->label;
72253ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7226c58f1c22SToby Isaac     }
7227c58f1c22SToby Isaac     ++l;
7228c58f1c22SToby Isaac     next = next->next;
7229c58f1c22SToby Isaac   }
723063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7231c58f1c22SToby Isaac }
7232c58f1c22SToby Isaac 
7233cc4c1da9SBarry Smith /*@
7234bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7235c58f1c22SToby Isaac 
7236c58f1c22SToby Isaac   Not Collective
7237c58f1c22SToby Isaac 
7238c58f1c22SToby Isaac   Input Parameters:
7239bb7acecfSBarry Smith + dm    - The `DM` object
7240bb7acecfSBarry Smith - label - The `DMLabel`
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   Level: developer
7243c58f1c22SToby Isaac 
724460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7245c58f1c22SToby Isaac @*/
7246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7247d71ae5a4SJacob Faibussowitsch {
72485d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7249c58f1c22SToby Isaac   PetscBool   hasLabel;
7250d67d17b1SMatthew G. Knepley   const char *lname;
72515d80c0bfSVaclav Hapla   PetscBool   flg;
7252c58f1c22SToby Isaac 
7253c58f1c22SToby Isaac   PetscFunctionBegin;
7254c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72559566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72569566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72577a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72589566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7259c58f1c22SToby Isaac   tmpLabel->label  = label;
7260c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72615d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72625d80c0bfSVaclav Hapla   *p = tmpLabel;
72639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72649566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72655d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72669566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7267ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7269c58f1c22SToby Isaac }
7270c58f1c22SToby Isaac 
7271a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7272cc4c1da9SBarry Smith /*@
72734a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72744a7ee7d0SMatthew G. Knepley 
72754a7ee7d0SMatthew G. Knepley   Not Collective
72764a7ee7d0SMatthew G. Knepley 
72774a7ee7d0SMatthew G. Knepley   Input Parameters:
7278bb7acecfSBarry Smith + dm    - The `DM` object
7279bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72804a7ee7d0SMatthew G. Knepley 
7281bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7282bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7283bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7284bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7285bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7286bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7287bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72884a7ee7d0SMatthew G. Knepley 
72894a7ee7d0SMatthew G. Knepley   Level: intermediate
72904a7ee7d0SMatthew G. Knepley 
72911cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72924a7ee7d0SMatthew G. Knepley @*/
7293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7294d71ae5a4SJacob Faibussowitsch {
72954a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
72964a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
72974a7ee7d0SMatthew G. Knepley   const char *name, *lname;
72984a7ee7d0SMatthew G. Knepley 
72994a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73004a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73014a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73034a7ee7d0SMatthew G. Knepley   while (next) {
73049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73059566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73064a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73079566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73089566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73094a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73109566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73114a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73129566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73134a7ee7d0SMatthew G. Knepley       next->label = label;
73144a7ee7d0SMatthew G. Knepley       break;
73154a7ee7d0SMatthew G. Knepley     }
73164a7ee7d0SMatthew G. Knepley     next = next->next;
73174a7ee7d0SMatthew G. Knepley   }
73183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73194a7ee7d0SMatthew G. Knepley }
73204a7ee7d0SMatthew G. Knepley 
7321cc4c1da9SBarry Smith /*@
7322bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7323c58f1c22SToby Isaac 
7324c58f1c22SToby Isaac   Not Collective
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   Input Parameters:
7327bb7acecfSBarry Smith + dm   - The `DM` object
7328c58f1c22SToby Isaac - name - The label name
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Output Parameter:
733120f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7332bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7333c58f1c22SToby Isaac 
7334c58f1c22SToby Isaac   Level: developer
7335c58f1c22SToby Isaac 
73361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7337c58f1c22SToby Isaac @*/
7338d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7339d71ae5a4SJacob Faibussowitsch {
734095d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7341c58f1c22SToby Isaac   PetscBool   hasLabel;
7342d67d17b1SMatthew G. Knepley   const char *lname;
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   PetscFunctionBegin;
7345c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73464f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7347e5472504SVaclav Hapla   if (label) {
73484f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7349c58f1c22SToby Isaac     *label = NULL;
7350e5472504SVaclav Hapla   }
73515d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73529566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73539566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7354c58f1c22SToby Isaac     if (hasLabel) {
735595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73569566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
735795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73589566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7359ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
736095d578d6SVaclav Hapla       if (label) *label = link->label;
73619566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73629566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7363c58f1c22SToby Isaac       break;
7364c58f1c22SToby Isaac     }
7365c58f1c22SToby Isaac   }
73663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7367c58f1c22SToby Isaac }
7368c58f1c22SToby Isaac 
7369306894acSVaclav Hapla /*@
7370bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7371306894acSVaclav Hapla 
7372306894acSVaclav Hapla   Not Collective
7373306894acSVaclav Hapla 
7374306894acSVaclav Hapla   Input Parameters:
7375bb7acecfSBarry Smith + dm           - The `DM` object
7376bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
737720f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7378306894acSVaclav Hapla 
7379306894acSVaclav Hapla   Level: developer
7380306894acSVaclav Hapla 
7381bb7acecfSBarry Smith   Note:
7382306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7383bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7384306894acSVaclav Hapla   *label nullified.
7385306894acSVaclav Hapla 
73861cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7387306894acSVaclav Hapla @*/
7388d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7389d71ae5a4SJacob Faibussowitsch {
739043e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7391306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7392306894acSVaclav Hapla 
7393306894acSVaclav Hapla   PetscFunctionBegin;
7394306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73954f572ea9SToby Isaac   PetscAssertPointer(label, 2);
73963ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7397306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7398306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
73995d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
740043e45a93SVaclav Hapla     if (*label == link->label) {
7401306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
740243e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7403306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7404ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
740543e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74069566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74079566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7408306894acSVaclav Hapla       break;
7409306894acSVaclav Hapla     }
7410306894acSVaclav Hapla   }
74117a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7413306894acSVaclav Hapla }
7414306894acSVaclav Hapla 
7415cc4c1da9SBarry Smith /*@
7416c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7417c58f1c22SToby Isaac 
7418c58f1c22SToby Isaac   Not Collective
7419c58f1c22SToby Isaac 
7420c58f1c22SToby Isaac   Input Parameters:
7421bb7acecfSBarry Smith + dm   - The `DM` object
7422c58f1c22SToby Isaac - name - The label name
7423c58f1c22SToby Isaac 
7424c58f1c22SToby Isaac   Output Parameter:
7425c58f1c22SToby Isaac . output - The flag for output
7426c58f1c22SToby Isaac 
7427c58f1c22SToby Isaac   Level: developer
7428c58f1c22SToby Isaac 
74291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7430c58f1c22SToby Isaac @*/
7431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7432d71ae5a4SJacob Faibussowitsch {
74335d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7434d67d17b1SMatthew G. Knepley   const char *lname;
7435c58f1c22SToby Isaac 
7436c58f1c22SToby Isaac   PetscFunctionBegin;
7437c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74384f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74394f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7440c58f1c22SToby Isaac   while (next) {
7441c58f1c22SToby Isaac     PetscBool flg;
7442c58f1c22SToby Isaac 
74439566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74449566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74459371c9d4SSatish Balay     if (flg) {
74469371c9d4SSatish Balay       *output = next->output;
74473ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74489371c9d4SSatish Balay     }
7449c58f1c22SToby Isaac     next = next->next;
7450c58f1c22SToby Isaac   }
745198921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7452c58f1c22SToby Isaac }
7453c58f1c22SToby Isaac 
7454cc4c1da9SBarry Smith /*@
7455bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7456c58f1c22SToby Isaac 
7457c58f1c22SToby Isaac   Not Collective
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac   Input Parameters:
7460bb7acecfSBarry Smith + dm     - The `DM` object
7461c58f1c22SToby Isaac . name   - The label name
7462bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7463c58f1c22SToby Isaac 
7464c58f1c22SToby Isaac   Level: developer
7465c58f1c22SToby Isaac 
74661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7467c58f1c22SToby Isaac @*/
7468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7469d71ae5a4SJacob Faibussowitsch {
74705d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7471d67d17b1SMatthew G. Knepley   const char *lname;
7472c58f1c22SToby Isaac 
7473c58f1c22SToby Isaac   PetscFunctionBegin;
7474c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7476c58f1c22SToby Isaac   while (next) {
7477c58f1c22SToby Isaac     PetscBool flg;
7478c58f1c22SToby Isaac 
74799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74819371c9d4SSatish Balay     if (flg) {
74829371c9d4SSatish Balay       next->output = output;
74833ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74849371c9d4SSatish Balay     }
7485c58f1c22SToby Isaac     next = next->next;
7486c58f1c22SToby Isaac   }
748798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7488c58f1c22SToby Isaac }
7489c58f1c22SToby Isaac 
7490c58f1c22SToby Isaac /*@
7491bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7492c58f1c22SToby Isaac 
749320f4b53cSBarry Smith   Collective
7494c58f1c22SToby Isaac 
7495d8d19677SJose E. Roman   Input Parameters:
7496bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7497bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7498bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7499bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7500bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7501c58f1c22SToby Isaac 
7502c58f1c22SToby Isaac   Level: intermediate
7503c58f1c22SToby Isaac 
7504bb7acecfSBarry Smith   Note:
75052cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7506c58f1c22SToby Isaac 
75071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7508c58f1c22SToby Isaac @*/
7509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7510d71ae5a4SJacob Faibussowitsch {
75112cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7512c58f1c22SToby Isaac   const char *name;
7513c58f1c22SToby Isaac   PetscBool   flg;
75145d80c0bfSVaclav Hapla   DMLabelLink link;
7515c58f1c22SToby Isaac 
75165d80c0bfSVaclav Hapla   PetscFunctionBegin;
75175d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75185d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75195d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75205d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75217a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75223ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75235d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75245d80c0bfSVaclav Hapla     label = link->label;
75259566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75265d80c0bfSVaclav Hapla     if (!all) {
75279566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7528c58f1c22SToby Isaac       if (flg) continue;
75299566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75307d5acc75SStefano Zampini       if (flg) continue;
75319566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7532ba2698f1SMatthew G. Knepley       if (flg) continue;
75335d80c0bfSVaclav Hapla     }
75349566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75352cbb9b06SVaclav Hapla     if (labelOld) {
75362cbb9b06SVaclav Hapla       switch (emode) {
7537d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7538d71ae5a4SJacob Faibussowitsch         continue;
7539d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7540d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7541d71ae5a4SJacob Faibussowitsch         break;
7542d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7543d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7544d71ae5a4SJacob Faibussowitsch       default:
7545d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75462cbb9b06SVaclav Hapla       }
75472cbb9b06SVaclav Hapla     }
75485d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75499566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75505d80c0bfSVaclav Hapla     } else {
75515d80c0bfSVaclav Hapla       labelNew = label;
75525d80c0bfSVaclav Hapla     }
75539566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75549566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7555c58f1c22SToby Isaac   }
75563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7557c58f1c22SToby Isaac }
7558461a15a0SLisandro Dalcin 
7559609dae6eSVaclav Hapla /*@C
7560b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7561609dae6eSVaclav Hapla 
756220f4b53cSBarry Smith   Collective; No Fortran Support
7563609dae6eSVaclav Hapla 
7564609dae6eSVaclav Hapla   Input Parameters:
7565bb7acecfSBarry Smith + dm0 - First `DM` object
7566bb7acecfSBarry Smith - dm1 - Second `DM` object
7567609dae6eSVaclav Hapla 
7568a4e35b19SJacob Faibussowitsch   Output Parameters:
75695efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
757020f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7571609dae6eSVaclav Hapla 
7572609dae6eSVaclav Hapla   Level: intermediate
7573609dae6eSVaclav Hapla 
7574609dae6eSVaclav Hapla   Notes:
7575bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7576bb7acecfSBarry Smith 
757720f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7578bb7acecfSBarry Smith 
757920f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7580609dae6eSVaclav Hapla 
75815efe38ccSVaclav Hapla   The output message is set independently on each rank.
7582bb7acecfSBarry Smith 
7583bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7584bb7acecfSBarry Smith 
758520f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7586bb7acecfSBarry Smith 
758720f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7588609dae6eSVaclav Hapla 
7589609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7590bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7591609dae6eSVaclav Hapla 
7592cc4c1da9SBarry Smith   Developer Note:
7593cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7594cc4c1da9SBarry Smith 
75951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7596609dae6eSVaclav Hapla @*/
7597d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7598d71ae5a4SJacob Faibussowitsch {
75995efe38ccSVaclav Hapla   PetscInt    n, i;
7600609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76015efe38ccSVaclav Hapla   PetscBool   eq;
7602609dae6eSVaclav Hapla   MPI_Comm    comm;
76035efe38ccSVaclav Hapla   PetscMPIInt rank;
7604609dae6eSVaclav Hapla 
7605609dae6eSVaclav Hapla   PetscFunctionBegin;
7606609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7607609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7608609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76094f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76104f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76119566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76129566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76135efe38ccSVaclav Hapla   {
76145efe38ccSVaclav Hapla     PetscInt n1;
76155efe38ccSVaclav Hapla 
76169566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76179566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76185efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
761948a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7620712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76215efe38ccSVaclav Hapla     if (!eq) goto finish;
76225efe38ccSVaclav Hapla   }
76235efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7624609dae6eSVaclav Hapla     DMLabel     l0, l1;
7625609dae6eSVaclav Hapla     const char *name;
7626609dae6eSVaclav Hapla     char       *msgInner;
7627609dae6eSVaclav Hapla 
7628609dae6eSVaclav Hapla     /* Ignore label order */
76299566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76319566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7632609dae6eSVaclav Hapla     if (!l1) {
763363a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76345efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76355efe38ccSVaclav Hapla       break;
7636609dae6eSVaclav Hapla     }
76379566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76389566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76399566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76405efe38ccSVaclav Hapla     if (!eq) break;
7641609dae6eSVaclav Hapla   }
7642712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7643609dae6eSVaclav Hapla finish:
76445efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7645609dae6eSVaclav Hapla   if (message) {
7646609dae6eSVaclav Hapla     *message = NULL;
764748a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76485efe38ccSVaclav Hapla   } else {
764948a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76509566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76515efe38ccSVaclav Hapla   }
76525efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76535efe38ccSVaclav Hapla   if (equal) *equal = eq;
76547a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7656609dae6eSVaclav Hapla }
7657609dae6eSVaclav Hapla 
7658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7659d71ae5a4SJacob Faibussowitsch {
7660461a15a0SLisandro Dalcin   PetscFunctionBegin;
76614f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7662461a15a0SLisandro Dalcin   if (!*label) {
76639566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76649566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7665461a15a0SLisandro Dalcin   }
76669566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7668461a15a0SLisandro Dalcin }
7669461a15a0SLisandro Dalcin 
76700fdc7489SMatthew Knepley /*
76710fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76720fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76730fdc7489SMatthew Knepley   (label, id) pair in the DM.
76740fdc7489SMatthew Knepley 
76750fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76760fdc7489SMatthew Knepley   each label.
76770fdc7489SMatthew Knepley */
7678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7679d71ae5a4SJacob Faibussowitsch {
76800fdc7489SMatthew Knepley   DMUniversalLabel ul;
76810fdc7489SMatthew Knepley   PetscBool       *active;
76820fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76830fdc7489SMatthew Knepley 
76840fdc7489SMatthew Knepley   PetscFunctionBegin;
76859566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76869566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76879566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76889566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76890fdc7489SMatthew Knepley   ul->Nl = 0;
76900fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76910fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76920fdc7489SMatthew Knepley     const char *name;
76930fdc7489SMatthew Knepley 
76949566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
76959566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
76969566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
76970fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
76980fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
76990fdc7489SMatthew Knepley   }
77009566063dSJacob 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));
77010fdc7489SMatthew Knepley   ul->Nv = 0;
77020fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77030fdc7489SMatthew Knepley     DMLabel     label;
77040fdc7489SMatthew Knepley     PetscInt    nv;
77050fdc7489SMatthew Knepley     const char *name;
77060fdc7489SMatthew Knepley 
77070fdc7489SMatthew Knepley     if (!active[l]) continue;
77089566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77099566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77109566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77119566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77120fdc7489SMatthew Knepley     ul->indices[m] = l;
77130fdc7489SMatthew Knepley     ul->Nv += nv;
77140fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77150fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77160fdc7489SMatthew Knepley     ++m;
77170fdc7489SMatthew Knepley   }
77180fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77190fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77200fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77210fdc7489SMatthew Knepley   }
77220fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77230fdc7489SMatthew Knepley     PetscInt b;
77240fdc7489SMatthew Knepley 
77250fdc7489SMatthew Knepley     ul->masks[l] = 0;
77260fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77270fdc7489SMatthew Knepley   }
77289566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77290fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77300fdc7489SMatthew Knepley     DMLabel         label;
77310fdc7489SMatthew Knepley     IS              valueIS;
77320fdc7489SMatthew Knepley     const PetscInt *varr;
77330fdc7489SMatthew Knepley     PetscInt        nv, v;
77340fdc7489SMatthew Knepley 
77350fdc7489SMatthew Knepley     if (!active[l]) continue;
77369566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77379566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77399566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7740ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77419566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77429566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77439566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77440fdc7489SMatthew Knepley     ++m;
77450fdc7489SMatthew Knepley   }
77469566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77470fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77480fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77490fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77500fdc7489SMatthew Knepley 
77510fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77520fdc7489SMatthew Knepley       DMLabel  label;
77530649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77540fdc7489SMatthew Knepley 
77550fdc7489SMatthew Knepley       if (!active[l]) continue;
77569566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77579566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77589566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77599371c9d4SSatish Balay       if (val == defval) {
77609371c9d4SSatish Balay         ++m;
77619371c9d4SSatish Balay         continue;
77629371c9d4SSatish Balay       }
77630649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77640fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77659566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
776663a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77670fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77680fdc7489SMatthew Knepley       ++m;
77690fdc7489SMatthew Knepley     }
77709566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77710fdc7489SMatthew Knepley   }
77729566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77730fdc7489SMatthew Knepley   *universal = ul;
77743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77750fdc7489SMatthew Knepley }
77760fdc7489SMatthew Knepley 
7777d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7778d71ae5a4SJacob Faibussowitsch {
77790fdc7489SMatthew Knepley   PetscInt l;
77800fdc7489SMatthew Knepley 
77810fdc7489SMatthew Knepley   PetscFunctionBegin;
77829566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77839566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77849566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77859566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77869566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77870fdc7489SMatthew Knepley   *universal = NULL;
77883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77890fdc7489SMatthew Knepley }
77900fdc7489SMatthew Knepley 
7791d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7792d71ae5a4SJacob Faibussowitsch {
77930fdc7489SMatthew Knepley   PetscFunctionBegin;
77944f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
77950fdc7489SMatthew Knepley   *ulabel = ul->label;
77963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77970fdc7489SMatthew Knepley }
77980fdc7489SMatthew Knepley 
7799d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7800d71ae5a4SJacob Faibussowitsch {
78010fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78020fdc7489SMatthew Knepley 
78030fdc7489SMatthew Knepley   PetscFunctionBegin;
7804064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78050fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78069566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78079566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78080fdc7489SMatthew Knepley   }
78090fdc7489SMatthew Knepley   if (preserveOrder) {
78100fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78110fdc7489SMatthew Knepley       const char *name;
78120fdc7489SMatthew Knepley       PetscBool   match;
78130fdc7489SMatthew Knepley 
78149566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78159566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
781663a3b9bcSJacob 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]);
78170fdc7489SMatthew Knepley     }
78180fdc7489SMatthew Knepley   }
78193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78200fdc7489SMatthew Knepley }
78210fdc7489SMatthew Knepley 
7822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7823d71ae5a4SJacob Faibussowitsch {
78240fdc7489SMatthew Knepley   PetscInt l;
78250fdc7489SMatthew Knepley 
78260fdc7489SMatthew Knepley   PetscFunctionBegin;
78270fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78280fdc7489SMatthew Knepley     DMLabel  label;
78290fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78300fdc7489SMatthew Knepley 
78310fdc7489SMatthew Knepley     if (lval) {
78329566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78339566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78349566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78350fdc7489SMatthew Knepley     }
78360fdc7489SMatthew Knepley   }
78373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78380fdc7489SMatthew Knepley }
7839a8fb8f29SToby Isaac 
7840a8fb8f29SToby Isaac /*@
7841bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7842bb7acecfSBarry Smith 
784320f4b53cSBarry Smith   Not Collective
7844a8fb8f29SToby Isaac 
7845a8fb8f29SToby Isaac   Input Parameter:
7846bb7acecfSBarry Smith . dm - The `DM` object
7847a8fb8f29SToby Isaac 
7848a8fb8f29SToby Isaac   Output Parameter:
7849bb7acecfSBarry Smith . cdm - The coarse `DM`
7850a8fb8f29SToby Isaac 
7851a8fb8f29SToby Isaac   Level: intermediate
7852a8fb8f29SToby Isaac 
78531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7854a8fb8f29SToby Isaac @*/
7855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7856d71ae5a4SJacob Faibussowitsch {
7857a8fb8f29SToby Isaac   PetscFunctionBegin;
7858a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78594f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7860a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7862a8fb8f29SToby Isaac }
7863a8fb8f29SToby Isaac 
7864a8fb8f29SToby Isaac /*@
7865bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7866a8fb8f29SToby Isaac 
7867a8fb8f29SToby Isaac   Input Parameters:
7868bb7acecfSBarry Smith + dm  - The `DM` object
7869bb7acecfSBarry Smith - cdm - The coarse `DM`
7870a8fb8f29SToby Isaac 
7871a8fb8f29SToby Isaac   Level: intermediate
7872a8fb8f29SToby Isaac 
7873bb7acecfSBarry Smith   Note:
7874bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7875bb7acecfSBarry Smith 
78761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7877a8fb8f29SToby Isaac @*/
7878d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7879d71ae5a4SJacob Faibussowitsch {
7880a8fb8f29SToby Isaac   PetscFunctionBegin;
7881a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7882a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
788389d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78849566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78859566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7886a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7888a8fb8f29SToby Isaac }
7889a8fb8f29SToby Isaac 
789088bdff64SToby Isaac /*@
7891bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
789288bdff64SToby Isaac 
789388bdff64SToby Isaac   Input Parameter:
7894bb7acecfSBarry Smith . dm - The `DM` object
789588bdff64SToby Isaac 
789688bdff64SToby Isaac   Output Parameter:
7897bb7acecfSBarry Smith . fdm - The fine `DM`
789888bdff64SToby Isaac 
789988bdff64SToby Isaac   Level: intermediate
790088bdff64SToby Isaac 
79011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
790288bdff64SToby Isaac @*/
7903d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7904d71ae5a4SJacob Faibussowitsch {
790588bdff64SToby Isaac   PetscFunctionBegin;
790688bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79074f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
790888bdff64SToby Isaac   *fdm = dm->fineMesh;
79093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
791088bdff64SToby Isaac }
791188bdff64SToby Isaac 
791288bdff64SToby Isaac /*@
7913bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
791488bdff64SToby Isaac 
791588bdff64SToby Isaac   Input Parameters:
7916bb7acecfSBarry Smith + dm  - The `DM` object
7917bb7acecfSBarry Smith - fdm - The fine `DM`
791888bdff64SToby Isaac 
7919bb7acecfSBarry Smith   Level: developer
792088bdff64SToby Isaac 
7921bb7acecfSBarry Smith   Note:
7922bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7923bb7acecfSBarry Smith 
79241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
792588bdff64SToby Isaac @*/
7926d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7927d71ae5a4SJacob Faibussowitsch {
792888bdff64SToby Isaac   PetscFunctionBegin;
792988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
793088bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
793189d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79339566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
793488bdff64SToby Isaac   dm->fineMesh = fdm;
79353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
793688bdff64SToby Isaac }
793788bdff64SToby Isaac 
7938a6ba4734SToby Isaac /*@C
7939bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7940a6ba4734SToby Isaac 
794120f4b53cSBarry Smith   Collective
7942783e2ec8SMatthew G. Knepley 
7943a6ba4734SToby Isaac   Input Parameters:
7944bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7945bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7946a6ba4734SToby Isaac . name     - The BC name
794745480ffeSMatthew G. Knepley . label    - The label defining constrained points
7948bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
794945480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7950a6ba4734SToby Isaac . field    - The field to constrain
795145480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7952a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7953a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
795456cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7955a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7956a6ba4734SToby Isaac 
795745480ffeSMatthew G. Knepley   Output Parameter:
795845480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
795945480ffeSMatthew G. Knepley 
7960a6ba4734SToby Isaac   Options Database Keys:
7961a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7962a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7963a6ba4734SToby Isaac 
796420f4b53cSBarry Smith   Level: intermediate
796520f4b53cSBarry Smith 
7966bb7acecfSBarry Smith   Notes:
796737fdd005SBarry Smith   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
796873ff1848SBarry Smith .vb
796973ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
797073ff1848SBarry Smith .ve
797156cf3b9cSMatthew G. Knepley 
7972a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
797356cf3b9cSMatthew G. Knepley 
797420f4b53cSBarry Smith .vb
797520f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
797620f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
797720f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
797820f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
797920f4b53cSBarry Smith .ve
798056cf3b9cSMatthew G. Knepley + dim - the spatial dimension
798156cf3b9cSMatthew G. Knepley . Nf - the number of fields
798256cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
798356cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
798456cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
798556cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
798656cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
798756cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
798856cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
798956cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
799056cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
799156cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
799256cf3b9cSMatthew G. Knepley . t - current time
799356cf3b9cSMatthew G. Knepley . x - coordinates of the current point
799456cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
799556cf3b9cSMatthew G. Knepley . constants - constant parameters
799656cf3b9cSMatthew G. Knepley - bcval - output values at the current point
799756cf3b9cSMatthew G. Knepley 
79981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
7999a6ba4734SToby Isaac @*/
8000d71ae5a4SJacob 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)
8001d71ae5a4SJacob Faibussowitsch {
8002e5e52638SMatthew G. Knepley   PetscDS ds;
8003a6ba4734SToby Isaac 
8004a6ba4734SToby Isaac   PetscFunctionBegin;
8005a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8006783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
800745480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
800845480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
800945480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
801045480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
801101a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80129566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8013799db056SMatthew G. Knepley   /* Complete label */
8014799db056SMatthew G. Knepley   if (label) {
8015799db056SMatthew G. Knepley     PetscObject  obj;
8016799db056SMatthew G. Knepley     PetscClassId id;
8017799db056SMatthew G. Knepley 
8018799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8019799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8020799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8021799db056SMatthew G. Knepley       DM plex;
8022799db056SMatthew G. Knepley 
8023799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8024799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8025799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8026799db056SMatthew G. Knepley     }
8027799db056SMatthew G. Knepley   }
80289566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8030a6ba4734SToby Isaac }
8031a6ba4734SToby Isaac 
803245480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8033d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8034d71ae5a4SJacob Faibussowitsch {
8035e5e52638SMatthew G. Knepley   PetscDS     ds;
8036dff059c6SToby Isaac   DMBoundary *lastnext;
8037e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8038e6f8dbb6SToby Isaac 
8039e6f8dbb6SToby Isaac   PetscFunctionBegin;
80409566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8041e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
804247a1f5adSToby Isaac   if (dm->boundary) {
804347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
804447a1f5adSToby Isaac 
804547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80463ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
804747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
804847a1f5adSToby Isaac     while (next) {
804947a1f5adSToby Isaac       DMBoundary b = next;
805047a1f5adSToby Isaac 
805147a1f5adSToby Isaac       next = b->next;
80529566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8053a6ba4734SToby Isaac     }
805447a1f5adSToby Isaac     dm->boundary = NULL;
8055a6ba4734SToby Isaac   }
805647a1f5adSToby Isaac 
8057f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8058e6f8dbb6SToby Isaac   while (dsbound) {
8059e6f8dbb6SToby Isaac     DMBoundary dmbound;
8060e6f8dbb6SToby Isaac 
80619566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8062e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
806345480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
806447a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8065dff059c6SToby Isaac     *lastnext = dmbound;
8066f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8067dff059c6SToby Isaac     dsbound   = dsbound->next;
8068a6ba4734SToby Isaac   }
80693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8070a6ba4734SToby Isaac }
8071a6ba4734SToby Isaac 
8072bb7acecfSBarry Smith /* TODO: missing manual page */
8073d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8074d71ae5a4SJacob Faibussowitsch {
8075b95f2879SToby Isaac   DMBoundary b;
8076a6ba4734SToby Isaac 
8077a6ba4734SToby Isaac   PetscFunctionBegin;
8078a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80794f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8080a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80819566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8082b95f2879SToby Isaac   b = dm->boundary;
8083a6ba4734SToby Isaac   while (b && !(*isBd)) {
8084e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8085e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8086a6ba4734SToby Isaac     PetscInt   i;
8087a6ba4734SToby Isaac 
808845480ffeSMatthew G. Knepley     if (label) {
80899566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8090a6ba4734SToby Isaac     }
8091a6ba4734SToby Isaac     b = b->next;
8092a6ba4734SToby Isaac   }
80933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8094a6ba4734SToby Isaac }
80954d6f44ffSToby Isaac 
80964d6f44ffSToby Isaac /*@C
8097bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8098a6e0b375SMatthew G. Knepley 
809920f4b53cSBarry Smith   Collective
81004d6f44ffSToby Isaac 
81014d6f44ffSToby Isaac   Input Parameters:
8102bb7acecfSBarry Smith + dm    - The `DM`
81030709b2feSToby Isaac . time  - The time
81044d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
81054d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81064d6f44ffSToby Isaac - mode  - The insertion mode for values
81074d6f44ffSToby Isaac 
81084d6f44ffSToby Isaac   Output Parameter:
81094d6f44ffSToby Isaac . X - vector
81104d6f44ffSToby Isaac 
811120f4b53cSBarry Smith   Calling sequence of `funcs`:
81124d6f44ffSToby Isaac + dim  - The spatial dimension
81138ec8862eSJed Brown . time - The time at which to sample
81144d6f44ffSToby Isaac . x    - The coordinates
811577b739a6SMatthew Knepley . Nc   - The number of components
81164d6f44ffSToby Isaac . u    - The output field values
81174d6f44ffSToby Isaac - ctx  - optional user-defined function context
81184d6f44ffSToby Isaac 
81194d6f44ffSToby Isaac   Level: developer
81204d6f44ffSToby Isaac 
8121bb7acecfSBarry Smith   Developer Notes:
8122bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8123bb7acecfSBarry Smith 
8124bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8125bb7acecfSBarry Smith 
81261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81274d6f44ffSToby Isaac @*/
8128a4e35b19SJacob 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)
8129d71ae5a4SJacob Faibussowitsch {
81304d6f44ffSToby Isaac   Vec localX;
81314d6f44ffSToby Isaac 
81324d6f44ffSToby Isaac   PetscFunctionBegin;
81334d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8134708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81359566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8136f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81379566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81389566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81399566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81409566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8141708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81434d6f44ffSToby Isaac }
81444d6f44ffSToby Isaac 
8145a6e0b375SMatthew G. Knepley /*@C
8146bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8147a6e0b375SMatthew G. Knepley 
814820f4b53cSBarry Smith   Not Collective
8149a6e0b375SMatthew G. Knepley 
8150a6e0b375SMatthew G. Knepley   Input Parameters:
8151bb7acecfSBarry Smith + dm    - The `DM`
8152a6e0b375SMatthew G. Knepley . time  - The time
8153a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8154a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8155a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8156a6e0b375SMatthew G. Knepley 
8157a6e0b375SMatthew G. Knepley   Output Parameter:
8158a6e0b375SMatthew G. Knepley . localX - vector
8159a6e0b375SMatthew G. Knepley 
816020f4b53cSBarry Smith   Calling sequence of `funcs`:
8161a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8162a4e35b19SJacob Faibussowitsch . time - The current timestep
8163a6e0b375SMatthew G. Knepley . x    - The coordinates
816477b739a6SMatthew Knepley . Nc   - The number of components
8165a6e0b375SMatthew G. Knepley . u    - The output field values
8166a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8167a6e0b375SMatthew G. Knepley 
8168a6e0b375SMatthew G. Knepley   Level: developer
8169a6e0b375SMatthew G. Knepley 
8170bb7acecfSBarry Smith   Developer Notes:
8171bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8172bb7acecfSBarry Smith 
8173bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8174bb7acecfSBarry Smith 
81751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8176a6e0b375SMatthew G. Knepley @*/
8177a4e35b19SJacob 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)
8178d71ae5a4SJacob Faibussowitsch {
81794d6f44ffSToby Isaac   PetscFunctionBegin;
81804d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8181064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8182fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81844d6f44ffSToby Isaac }
81854d6f44ffSToby Isaac 
8186a6e0b375SMatthew G. Knepley /*@C
8187bb7acecfSBarry 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.
8188a6e0b375SMatthew G. Knepley 
818920f4b53cSBarry Smith   Collective
8190a6e0b375SMatthew G. Knepley 
8191a6e0b375SMatthew G. Knepley   Input Parameters:
8192bb7acecfSBarry Smith + dm     - The `DM`
8193a6e0b375SMatthew G. Knepley . time   - The time
8194a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8195a4e35b19SJacob Faibussowitsch . ids    - The ids
8196a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8197a4e35b19SJacob Faibussowitsch . comps  - The components
8198bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8199a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8200bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8201a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8202a6e0b375SMatthew G. Knepley 
8203a6e0b375SMatthew G. Knepley   Output Parameter:
8204a6e0b375SMatthew G. Knepley . X - vector
8205a6e0b375SMatthew G. Knepley 
820620f4b53cSBarry Smith   Calling sequence of `funcs`:
8207a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8208a4e35b19SJacob Faibussowitsch . time - The current timestep
8209a6e0b375SMatthew G. Knepley . x    - The coordinates
821077b739a6SMatthew Knepley . Nc   - The number of components
8211a6e0b375SMatthew G. Knepley . u    - The output field values
8212a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8213a6e0b375SMatthew G. Knepley 
8214a6e0b375SMatthew G. Knepley   Level: developer
8215a6e0b375SMatthew G. Knepley 
8216bb7acecfSBarry Smith   Developer Notes:
8217bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8218bb7acecfSBarry Smith 
8219bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8220bb7acecfSBarry Smith 
82211cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8222a6e0b375SMatthew G. Knepley @*/
8223a4e35b19SJacob 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)
8224d71ae5a4SJacob Faibussowitsch {
82252c53366bSMatthew G. Knepley   Vec localX;
82262c53366bSMatthew G. Knepley 
82272c53366bSMatthew G. Knepley   PetscFunctionBegin;
82282c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82299566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8230f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82319566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82329566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82339566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82349566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82362c53366bSMatthew G. Knepley }
82372c53366bSMatthew G. Knepley 
8238a6e0b375SMatthew G. Knepley /*@C
8239bb7acecfSBarry 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.
8240a6e0b375SMatthew G. Knepley 
824120f4b53cSBarry Smith   Not Collective
8242a6e0b375SMatthew G. Knepley 
8243a6e0b375SMatthew G. Knepley   Input Parameters:
8244bb7acecfSBarry Smith + dm     - The `DM`
8245a6e0b375SMatthew G. Knepley . time   - The time
8246bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8247a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8248a4e35b19SJacob Faibussowitsch . ids    - The ids
8249a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8250a4e35b19SJacob Faibussowitsch . comps  - The components
8251a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8252a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8253a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8254a6e0b375SMatthew G. Knepley 
8255a6e0b375SMatthew G. Knepley   Output Parameter:
8256a6e0b375SMatthew G. Knepley . localX - vector
8257a6e0b375SMatthew G. Knepley 
825820f4b53cSBarry Smith   Calling sequence of `funcs`:
8259a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8260a4e35b19SJacob Faibussowitsch . time - The current time
8261a6e0b375SMatthew G. Knepley . x    - The coordinates
826277b739a6SMatthew Knepley . Nc   - The number of components
8263a6e0b375SMatthew G. Knepley . u    - The output field values
8264a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8265a6e0b375SMatthew G. Knepley 
8266a6e0b375SMatthew G. Knepley   Level: developer
8267a6e0b375SMatthew G. Knepley 
8268bb7acecfSBarry Smith   Developer Notes:
8269bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8270bb7acecfSBarry Smith 
8271bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8272bb7acecfSBarry Smith 
82731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8274a6e0b375SMatthew G. Knepley @*/
8275a4e35b19SJacob 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)
8276d71ae5a4SJacob Faibussowitsch {
82774d6f44ffSToby Isaac   PetscFunctionBegin;
82784d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8279064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8280fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82824d6f44ffSToby Isaac }
82832716604bSToby Isaac 
8284a6e0b375SMatthew G. Knepley /*@C
8285bb7acecfSBarry 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.
8286a6e0b375SMatthew G. Knepley 
828720f4b53cSBarry Smith   Not Collective
8288a6e0b375SMatthew G. Knepley 
8289a6e0b375SMatthew G. Knepley   Input Parameters:
8290bb7acecfSBarry Smith + dm     - The `DM`
8291a6e0b375SMatthew G. Knepley . time   - The time
829220f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8293a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8294a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8295a6e0b375SMatthew G. Knepley 
8296a6e0b375SMatthew G. Knepley   Output Parameter:
8297a6e0b375SMatthew G. Knepley . localX - The output vector
8298a6e0b375SMatthew G. Knepley 
829920f4b53cSBarry Smith   Calling sequence of `funcs`:
8300a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8301a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8302a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8303a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8304a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8305a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8306a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8307a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8308a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8309a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8310a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8311a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8312a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8313a6e0b375SMatthew G. Knepley . t            - The current time
8314a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8315a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8316a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8317a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8318a6e0b375SMatthew G. Knepley 
831973ff1848SBarry Smith   Level: intermediate
832073ff1848SBarry Smith 
8321bb7acecfSBarry Smith   Note:
8322bb7acecfSBarry 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.
8323bb7acecfSBarry 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
8324bb7acecfSBarry 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
8325a6e0b375SMatthew 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.
8326a6e0b375SMatthew G. Knepley 
8327bb7acecfSBarry Smith   Developer Notes:
8328bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8329bb7acecfSBarry Smith 
8330bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8331bb7acecfSBarry Smith 
8332a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8333a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8334a6e0b375SMatthew G. Knepley @*/
8335a4e35b19SJacob 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)
8336d71ae5a4SJacob Faibussowitsch {
83378c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8339eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8341fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83438c6c5593SMatthew G. Knepley }
83448c6c5593SMatthew G. Knepley 
8345a6e0b375SMatthew G. Knepley /*@C
8346a6e0b375SMatthew 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.
8347a6e0b375SMatthew G. Knepley 
834820f4b53cSBarry Smith   Not Collective
8349a6e0b375SMatthew G. Knepley 
8350a6e0b375SMatthew G. Knepley   Input Parameters:
8351bb7acecfSBarry Smith + dm     - The `DM`
8352a6e0b375SMatthew G. Knepley . time   - The time
8353bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8354a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8355a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8356bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
835720f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8358a6e0b375SMatthew G. Knepley . localU - The input field vector
8359a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8360a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8361a6e0b375SMatthew G. Knepley 
8362a6e0b375SMatthew G. Knepley   Output Parameter:
8363a6e0b375SMatthew G. Knepley . localX - The output vector
8364a6e0b375SMatthew G. Knepley 
836520f4b53cSBarry Smith   Calling sequence of `funcs`:
8366a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8367a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8368a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8369a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8370a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8371a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8372a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8373a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8374a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8375a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8376a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8377a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8378a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8379a6e0b375SMatthew G. Knepley . t            - The current time
8380a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8381a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8382a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8383a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8384a6e0b375SMatthew G. Knepley 
838573ff1848SBarry Smith   Level: intermediate
838673ff1848SBarry Smith 
8387bb7acecfSBarry Smith   Note:
8388bb7acecfSBarry 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.
8389bb7acecfSBarry 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
8390bb7acecfSBarry 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
8391a6e0b375SMatthew 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.
8392a6e0b375SMatthew G. Knepley 
8393bb7acecfSBarry Smith   Developer Notes:
8394bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8395bb7acecfSBarry Smith 
8396bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8397bb7acecfSBarry Smith 
83981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8399a6e0b375SMatthew G. Knepley @*/
8400a4e35b19SJacob 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)
8401d71ae5a4SJacob Faibussowitsch {
84028c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84038c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8404064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8405064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8406fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84088c6c5593SMatthew G. Knepley }
84098c6c5593SMatthew G. Knepley 
84102716604bSToby Isaac /*@C
8411d29d7c6eSMatthew 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.
8412d29d7c6eSMatthew G. Knepley 
841320f4b53cSBarry Smith   Not Collective
8414d29d7c6eSMatthew G. Knepley 
8415d29d7c6eSMatthew G. Knepley   Input Parameters:
8416bb7acecfSBarry Smith + dm     - The `DM`
8417d29d7c6eSMatthew G. Knepley . time   - The time
8418bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8419d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8420d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8421bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
842220f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8423d29d7c6eSMatthew G. Knepley . U      - The input field vector
8424d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8425d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8426d29d7c6eSMatthew G. Knepley 
8427d29d7c6eSMatthew G. Knepley   Output Parameter:
8428d29d7c6eSMatthew G. Knepley . X - The output vector
8429d29d7c6eSMatthew G. Knepley 
843020f4b53cSBarry Smith   Calling sequence of `funcs`:
8431d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8432d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8433d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8434d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8435d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8436d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8437d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8438d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8439d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8440d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8441d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8442d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8443d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8444d29d7c6eSMatthew G. Knepley . t            - The current time
8445d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8446d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8447d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8448d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8449d29d7c6eSMatthew G. Knepley 
845073ff1848SBarry Smith   Level: intermediate
845173ff1848SBarry Smith 
8452bb7acecfSBarry Smith   Note:
8453bb7acecfSBarry 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.
8454bb7acecfSBarry 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
8455bb7acecfSBarry 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
8456d29d7c6eSMatthew 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.
8457d29d7c6eSMatthew G. Knepley 
8458bb7acecfSBarry Smith   Developer Notes:
8459bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8460bb7acecfSBarry Smith 
8461bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8462bb7acecfSBarry Smith 
84631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8464d29d7c6eSMatthew G. Knepley @*/
8465a4e35b19SJacob 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)
8466d71ae5a4SJacob Faibussowitsch {
8467d29d7c6eSMatthew G. Knepley   DM  dmIn;
8468d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8469d29d7c6eSMatthew G. Knepley 
8470d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8471d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8472d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8473d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8474d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8475f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
847672fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
847772fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8478d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8479d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8480d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8481d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8482d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8484d29d7c6eSMatthew G. Knepley }
8485d29d7c6eSMatthew G. Knepley 
8486d29d7c6eSMatthew G. Knepley /*@C
8487ece3a9fcSMatthew 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.
8488ece3a9fcSMatthew G. Knepley 
848920f4b53cSBarry Smith   Not Collective
8490ece3a9fcSMatthew G. Knepley 
8491ece3a9fcSMatthew G. Knepley   Input Parameters:
8492bb7acecfSBarry Smith + dm     - The `DM`
8493ece3a9fcSMatthew G. Knepley . time   - The time
8494bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8495ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8496ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8497bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
849820f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8499ece3a9fcSMatthew G. Knepley . localU - The input field vector
8500ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8501ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8502ece3a9fcSMatthew G. Knepley 
8503ece3a9fcSMatthew G. Knepley   Output Parameter:
8504ece3a9fcSMatthew G. Knepley . localX - The output vector
8505ece3a9fcSMatthew G. Knepley 
850620f4b53cSBarry Smith   Calling sequence of `funcs`:
8507ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8508ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8509ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8510ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8511ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8512ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8513ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8514ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8515ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8516ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8517ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8518ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8519ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8520ece3a9fcSMatthew G. Knepley . t            - The current time
8521ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8522ece3a9fcSMatthew G. Knepley . n            - The face normal
8523ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8524ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8525ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8526ece3a9fcSMatthew G. Knepley 
852773ff1848SBarry Smith   Level: intermediate
852873ff1848SBarry Smith 
8529ece3a9fcSMatthew G. Knepley   Note:
8530bb7acecfSBarry 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.
8531bb7acecfSBarry 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
8532bb7acecfSBarry 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
8533ece3a9fcSMatthew 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.
8534ece3a9fcSMatthew G. Knepley 
8535bb7acecfSBarry Smith   Developer Notes:
8536bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8537bb7acecfSBarry Smith 
8538bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8539bb7acecfSBarry Smith 
85401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8541ece3a9fcSMatthew G. Knepley @*/
8542a4e35b19SJacob 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)
8543d71ae5a4SJacob Faibussowitsch {
8544ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8545ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8546064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8547064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8548fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8550ece3a9fcSMatthew G. Knepley }
8551ece3a9fcSMatthew G. Knepley 
8552ece3a9fcSMatthew G. Knepley /*@C
85532716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85542716604bSToby Isaac 
855520f4b53cSBarry Smith   Collective
8556bb7acecfSBarry Smith 
85572716604bSToby Isaac   Input Parameters:
8558bb7acecfSBarry Smith + dm    - The `DM`
85590709b2feSToby Isaac . time  - The time
85602716604bSToby Isaac . funcs - The functions to evaluate for each field component
85612716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8562574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85632716604bSToby Isaac 
85642716604bSToby Isaac   Output Parameter:
85652716604bSToby Isaac . diff - The diff ||u - u_h||_2
85662716604bSToby Isaac 
85672716604bSToby Isaac   Level: developer
85682716604bSToby Isaac 
8569bb7acecfSBarry Smith   Developer Notes:
8570bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8571bb7acecfSBarry Smith 
8572bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8573bb7acecfSBarry Smith 
85741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85752716604bSToby Isaac @*/
8576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8577d71ae5a4SJacob Faibussowitsch {
85782716604bSToby Isaac   PetscFunctionBegin;
85792716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8580b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8581fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85832716604bSToby Isaac }
8584b698f381SToby Isaac 
8585b698f381SToby Isaac /*@C
8586b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8587b698f381SToby Isaac 
858820f4b53cSBarry Smith   Collective
8589d083f849SBarry Smith 
8590b698f381SToby Isaac   Input Parameters:
8591bb7acecfSBarry Smith + dm    - The `DM`
8592a4e35b19SJacob Faibussowitsch . time  - The time
8593b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8594b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8595574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8596b698f381SToby Isaac - n     - The vector to project along
8597b698f381SToby Isaac 
8598b698f381SToby Isaac   Output Parameter:
8599b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8600b698f381SToby Isaac 
8601b698f381SToby Isaac   Level: developer
8602b698f381SToby Isaac 
8603bb7acecfSBarry Smith   Developer Notes:
8604bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8605bb7acecfSBarry Smith 
8606bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8607bb7acecfSBarry Smith 
86081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8609b698f381SToby Isaac @*/
8610d71ae5a4SJacob 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)
8611d71ae5a4SJacob Faibussowitsch {
8612b698f381SToby Isaac   PetscFunctionBegin;
8613b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8614b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8615fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8617b698f381SToby Isaac }
8618b698f381SToby Isaac 
86192a16baeaSToby Isaac /*@C
86202a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86212a16baeaSToby Isaac 
862220f4b53cSBarry Smith   Collective
8623d083f849SBarry Smith 
86242a16baeaSToby Isaac   Input Parameters:
8625bb7acecfSBarry Smith + dm    - The `DM`
86262a16baeaSToby Isaac . time  - The time
86272a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86282a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8629574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86302a16baeaSToby Isaac 
86312a16baeaSToby Isaac   Output Parameter:
86322a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86332a16baeaSToby Isaac 
86342a16baeaSToby Isaac   Level: developer
86352a16baeaSToby Isaac 
8636bb7acecfSBarry Smith   Developer Notes:
8637bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8638bb7acecfSBarry Smith 
8639bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8640bb7acecfSBarry Smith 
864142747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86422a16baeaSToby Isaac @*/
8643d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8644d71ae5a4SJacob Faibussowitsch {
86452a16baeaSToby Isaac   PetscFunctionBegin;
86462a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86472a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8648fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86502a16baeaSToby Isaac }
86512a16baeaSToby Isaac 
8652df0b854cSToby Isaac /*@C
8653bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8654502a2867SDave May 
8655502a2867SDave May   Not Collective
8656502a2867SDave May 
8657502a2867SDave May   Input Parameter:
8658bb7acecfSBarry Smith . dm - The `DM`
8659502a2867SDave May 
86600a19bb7dSprj-   Output Parameters:
86610a19bb7dSprj- + nranks - the number of neighbours
86620a19bb7dSprj- - ranks  - the neighbors ranks
8663502a2867SDave May 
86649bdbcad8SBarry Smith   Level: beginner
86659bdbcad8SBarry Smith 
8666bb7acecfSBarry Smith   Note:
8667bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8668502a2867SDave May 
86691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8670502a2867SDave May @*/
8671d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8672d71ae5a4SJacob Faibussowitsch {
8673502a2867SDave May   PetscFunctionBegin;
8674502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8675fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8677502a2867SDave May }
8678502a2867SDave May 
8679531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8680531c7667SBarry Smith 
8681531c7667SBarry Smith /*
8682531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86832b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8684531c7667SBarry Smith */
868566976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8686d71ae5a4SJacob Faibussowitsch {
8687531c7667SBarry Smith   PetscFunctionBegin;
8688531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8689531c7667SBarry Smith     Vec x1local;
8690531c7667SBarry Smith     DM  dm;
86919566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86927a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86939566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
86949566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
86959566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8696531c7667SBarry Smith     x1 = x1local;
8697531c7667SBarry Smith   }
86989566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8699531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8700531c7667SBarry Smith     DM dm;
87019566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87029566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8703531c7667SBarry Smith   }
87043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8705531c7667SBarry Smith }
8706531c7667SBarry Smith 
8707531c7667SBarry Smith /*@
8708bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8709531c7667SBarry Smith 
8710a4e35b19SJacob Faibussowitsch   Input Parameters:
8711a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8712a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8713531c7667SBarry Smith 
87149bdbcad8SBarry Smith   Level: advanced
87159bdbcad8SBarry Smith 
871673ff1848SBarry Smith   Developer Note:
871773ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8718531c7667SBarry Smith 
87191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8720531c7667SBarry Smith @*/
8721d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8722d71ae5a4SJacob Faibussowitsch {
8723531c7667SBarry Smith   PetscFunctionBegin;
8724531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8726531c7667SBarry Smith }
87278320bc6fSPatrick Sanan 
87288320bc6fSPatrick Sanan /*@
8729bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87308320bc6fSPatrick Sanan 
87318320bc6fSPatrick Sanan   Collective
87328320bc6fSPatrick Sanan 
87338320bc6fSPatrick Sanan   Input Parameters:
8734bb7acecfSBarry Smith + dm1 - the first `DM`
8735bb7acecfSBarry Smith - dm2 - the second `DM`
87368320bc6fSPatrick Sanan 
87378320bc6fSPatrick Sanan   Output Parameters:
8738bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8739bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87408320bc6fSPatrick Sanan 
874120f4b53cSBarry Smith   Level: advanced
874220f4b53cSBarry Smith 
87438320bc6fSPatrick Sanan   Notes:
8744bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87453d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87468320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8747bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87483d862458SPatrick Sanan   decomposition, but hold different data.
87498320bc6fSPatrick Sanan 
87508320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8751bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87528320bc6fSPatrick Sanan 
8753bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87548320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87558320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8756bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87578320bc6fSPatrick Sanan 
8758bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8759bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87608320bc6fSPatrick Sanan .vb
87618320bc6fSPatrick Sanan   ...
87629566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87638320bc6fSPatrick Sanan   if (set && compatible)  {
87649566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87659566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87669566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87678320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87688320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87698320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87708320bc6fSPatrick Sanan       }
87718320bc6fSPatrick Sanan     }
87729566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87739566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87748320bc6fSPatrick Sanan   } else {
87758320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87768320bc6fSPatrick Sanan   }
87778320bc6fSPatrick Sanan   ...
87788320bc6fSPatrick Sanan .ve
87798320bc6fSPatrick Sanan 
8780bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87818320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87828320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87838320bc6fSPatrick Sanan   always check the "set" output parameter.
87848320bc6fSPatrick Sanan 
8785bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87868320bc6fSPatrick Sanan 
8787bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87888320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87898320bc6fSPatrick Sanan   incompatible.
87908320bc6fSPatrick Sanan 
87918320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8792bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87938320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
87948320bc6fSPatrick Sanan 
879573ff1848SBarry Smith   Developer Note:
8796bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
87973d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8798a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8799bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88008320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
88013d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8802bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
88038320bc6fSPatrick Sanan 
88041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
88058320bc6fSPatrick Sanan @*/
8806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8807d71ae5a4SJacob Faibussowitsch {
88088320bc6fSPatrick Sanan   PetscMPIInt compareResult;
88098320bc6fSPatrick Sanan   DMType      type, type2;
88108320bc6fSPatrick Sanan   PetscBool   sameType;
88118320bc6fSPatrick Sanan 
88128320bc6fSPatrick Sanan   PetscFunctionBegin;
8813a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
88148320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88158320bc6fSPatrick Sanan 
88168320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8817a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88188320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88198320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88203ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88218320bc6fSPatrick Sanan   }
88228320bc6fSPatrick Sanan 
88238320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88248320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88258320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88268320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88288320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88298320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88308320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88313ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88328320bc6fSPatrick Sanan   }
88338320bc6fSPatrick Sanan 
88348320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8835a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8836dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88373ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88388320bc6fSPatrick Sanan   }
88398320bc6fSPatrick Sanan 
8840a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88418320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88429566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88439566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88449566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88458320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8846dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88478320bc6fSPatrick Sanan   } else {
88488320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88498320bc6fSPatrick Sanan   }
88503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88518320bc6fSPatrick Sanan }
8852c0f0dcc3SMatthew G. Knepley 
8853c0f0dcc3SMatthew G. Knepley /*@C
8854bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8855c0f0dcc3SMatthew G. Knepley 
885620f4b53cSBarry Smith   Logically Collective
8857c0f0dcc3SMatthew G. Knepley 
8858c0f0dcc3SMatthew G. Knepley   Input Parameters:
885960225df5SJacob Faibussowitsch + dm             - the `DM`
8860c0f0dcc3SMatthew G. Knepley . f              - the monitor function
886120f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
886220f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8863c0f0dcc3SMatthew G. Knepley 
886420f4b53cSBarry Smith   Options Database Key:
886560225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8866c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8867c0f0dcc3SMatthew G. Knepley 
88689bdbcad8SBarry Smith   Level: intermediate
88699bdbcad8SBarry Smith 
8870bb7acecfSBarry Smith   Note:
8871c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8872bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8873c0f0dcc3SMatthew G. Knepley   order in which they were set.
8874c0f0dcc3SMatthew G. Knepley 
887573ff1848SBarry Smith   Fortran Note:
8876bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8877bb7acecfSBarry Smith 
887873ff1848SBarry Smith   Developer Note:
8879bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8880c0f0dcc3SMatthew G. Knepley 
88811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8882c0f0dcc3SMatthew G. Knepley @*/
8883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8884d71ae5a4SJacob Faibussowitsch {
8885c0f0dcc3SMatthew G. Knepley   PetscInt m;
8886c0f0dcc3SMatthew G. Knepley 
8887c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8888c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8889c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8890c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8891c0f0dcc3SMatthew G. Knepley 
88929566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88933ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8894c0f0dcc3SMatthew G. Knepley   }
88957a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8896c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8897c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8898c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
88993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8900c0f0dcc3SMatthew G. Knepley }
8901c0f0dcc3SMatthew G. Knepley 
8902c0f0dcc3SMatthew G. Knepley /*@
8903bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8904c0f0dcc3SMatthew G. Knepley 
890520f4b53cSBarry Smith   Logically Collective
8906c0f0dcc3SMatthew G. Knepley 
8907c0f0dcc3SMatthew G. Knepley   Input Parameter:
8908c0f0dcc3SMatthew G. Knepley . dm - the DM
8909c0f0dcc3SMatthew G. Knepley 
8910c0f0dcc3SMatthew G. Knepley   Options Database Key:
8911c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8912bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8913c0f0dcc3SMatthew G. Knepley   set via the options database
8914c0f0dcc3SMatthew G. Knepley 
89159bdbcad8SBarry Smith   Level: intermediate
89169bdbcad8SBarry Smith 
8917bb7acecfSBarry Smith   Note:
8918bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8919c0f0dcc3SMatthew G. Knepley 
89201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8921c0f0dcc3SMatthew G. Knepley @*/
8922d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8923d71ae5a4SJacob Faibussowitsch {
8924c0f0dcc3SMatthew G. Knepley   PetscInt m;
8925c0f0dcc3SMatthew G. Knepley 
8926c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8927c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8928c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89299566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8930c0f0dcc3SMatthew G. Knepley   }
8931c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8933c0f0dcc3SMatthew G. Knepley }
8934c0f0dcc3SMatthew G. Knepley 
8935c0f0dcc3SMatthew G. Knepley /*@C
8936c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8937c0f0dcc3SMatthew G. Knepley 
893820f4b53cSBarry Smith   Collective
8939c0f0dcc3SMatthew G. Knepley 
8940c0f0dcc3SMatthew G. Knepley   Input Parameters:
8941bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8942c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8943c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8944c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8945c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8946bb7acecfSBarry 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
8947c0f0dcc3SMatthew G. Knepley 
8948c0f0dcc3SMatthew G. Knepley   Output Parameter:
8949c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8950c0f0dcc3SMatthew G. Knepley 
8951c0f0dcc3SMatthew G. Knepley   Level: developer
8952c0f0dcc3SMatthew G. Knepley 
8953*648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8954db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
895560225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8956db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8957c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8958db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8959bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8960c0f0dcc3SMatthew G. Knepley @*/
8961d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8962d71ae5a4SJacob Faibussowitsch {
8963c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8964c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8965c0f0dcc3SMatthew G. Knepley 
8966c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8967c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8968*648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8969c0f0dcc3SMatthew G. Knepley   if (*flg) {
8970c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8971c0f0dcc3SMatthew G. Knepley 
89729566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8973*648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
89749566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89759566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8976c0f0dcc3SMatthew G. Knepley   }
89773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8978c0f0dcc3SMatthew G. Knepley }
8979c0f0dcc3SMatthew G. Knepley 
8980c0f0dcc3SMatthew G. Knepley /*@
8981c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8982c0f0dcc3SMatthew G. Knepley 
898320f4b53cSBarry Smith   Collective
8984c0f0dcc3SMatthew G. Knepley 
89852fe279fdSBarry Smith   Input Parameter:
8986bb7acecfSBarry Smith . dm - The `DM`
8987c0f0dcc3SMatthew G. Knepley 
8988c0f0dcc3SMatthew G. Knepley   Level: developer
8989c0f0dcc3SMatthew G. Knepley 
899073ff1848SBarry Smith   Developer Note:
8991a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8992a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8993a4e35b19SJacob Faibussowitsch   concept of discretization.
8994bb7acecfSBarry Smith 
89951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8996c0f0dcc3SMatthew G. Knepley @*/
8997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8998d71ae5a4SJacob Faibussowitsch {
8999c0f0dcc3SMatthew G. Knepley   PetscInt m;
9000c0f0dcc3SMatthew G. Knepley 
9001c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
90023ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9003c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
900448a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
90053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9006c0f0dcc3SMatthew G. Knepley }
90072e4af2aeSMatthew G. Knepley 
90082e4af2aeSMatthew G. Knepley /*@
9009bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
90102e4af2aeSMatthew G. Knepley 
901120f4b53cSBarry Smith   Collective
90122e4af2aeSMatthew G. Knepley 
90132e4af2aeSMatthew G. Knepley   Input Parameters:
9014bb7acecfSBarry Smith + dm  - The `DM`
90156b867d5aSJose E. Roman - sol - The solution vector
90162e4af2aeSMatthew G. Knepley 
90176b867d5aSJose E. Roman   Input/Output Parameter:
901820f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90196b867d5aSJose E. Roman            contains the error in each field
90206b867d5aSJose E. Roman 
90216b867d5aSJose E. Roman   Output Parameter:
902220f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
902320f4b53cSBarry Smith 
902420f4b53cSBarry Smith   Level: developer
90252e4af2aeSMatthew G. Knepley 
9026bb7acecfSBarry Smith   Note:
9027bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90282e4af2aeSMatthew G. Knepley 
90291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90302e4af2aeSMatthew G. Knepley @*/
9031d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9032d71ae5a4SJacob Faibussowitsch {
90332e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90342e4af2aeSMatthew G. Knepley   void    **ctxs;
90352e4af2aeSMatthew G. Knepley   PetscReal time;
90362e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90372e4af2aeSMatthew G. Knepley 
90382e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90399566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90409566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90419566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90422e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90432e4af2aeSMatthew G. Knepley     PetscDS         ds;
90442e4af2aeSMatthew G. Knepley     DMLabel         label;
90452e4af2aeSMatthew G. Knepley     IS              fieldIS;
90462e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90472e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90482e4af2aeSMatthew G. Knepley 
904907218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90509566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90519566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90522e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90532e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90549566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90552e4af2aeSMatthew G. Knepley     }
90569566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90572e4af2aeSMatthew G. Knepley   }
9058ad540459SPierre 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);
90599566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90609566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90612e4af2aeSMatthew G. Knepley   if (errorVec) {
90622e4af2aeSMatthew G. Knepley     DM             edm;
90632e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90642e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90652e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90662e4af2aeSMatthew G. Knepley 
90679566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90689566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90709566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90712e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90729566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90732e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90742e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90752e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90762e4af2aeSMatthew G. Knepley       const char     *name;
90772e4af2aeSMatthew G. Knepley 
90789566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90799566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90809566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90819566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90829566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90839566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90849566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90859566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90862e4af2aeSMatthew G. Knepley     }
90879566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90882e4af2aeSMatthew G. Knepley 
90899566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90909566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90919566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90929566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90932e4af2aeSMatthew G. Knepley   }
90949566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
90953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90962e4af2aeSMatthew G. Knepley }
90979a2a23afSMatthew G. Knepley 
90989a2a23afSMatthew G. Knepley /*@
9099bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91009a2a23afSMatthew G. Knepley 
910120f4b53cSBarry Smith   Not Collective
91029a2a23afSMatthew G. Knepley 
91039a2a23afSMatthew G. Knepley   Input Parameter:
9104bb7acecfSBarry Smith . dm - The `DM`
91059a2a23afSMatthew G. Knepley 
91069a2a23afSMatthew G. Knepley   Output Parameter:
9107a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
91089a2a23afSMatthew G. Knepley 
91099a2a23afSMatthew G. Knepley   Level: advanced
91109a2a23afSMatthew G. Knepley 
9111e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
91129a2a23afSMatthew G. Knepley @*/
9113d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9114d71ae5a4SJacob Faibussowitsch {
91159a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91169a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91179566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91199a2a23afSMatthew G. Knepley }
91209a2a23afSMatthew G. Knepley 
91219a2a23afSMatthew G. Knepley /*@
9122ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91239a2a23afSMatthew G. Knepley 
912420f4b53cSBarry Smith   Not Collective
91259a2a23afSMatthew G. Knepley 
91269a2a23afSMatthew G. Knepley   Input Parameters:
9127bb7acecfSBarry Smith + dm    - The `DM`
9128bb7acecfSBarry Smith . label - The `DMLabel`
9129ac17215fSMatthew G. Knepley . value - The label value indicating the region
9130ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91319a2a23afSMatthew G. Knepley 
91329a2a23afSMatthew G. Knepley   Output Parameter:
9133bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91349a2a23afSMatthew G. Knepley 
91359bdbcad8SBarry Smith   Level: advanced
91369bdbcad8SBarry Smith 
9137bb7acecfSBarry Smith   Note:
9138bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
913904c51a94SMatthew G. Knepley 
9140e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91419a2a23afSMatthew G. Knepley @*/
9142d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9143d71ae5a4SJacob Faibussowitsch {
9144ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
914504c51a94SMatthew G. Knepley   PetscBool       has;
91469a2a23afSMatthew G. Knepley 
91479a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91489a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91499a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91509a2a23afSMatthew G. Knepley   key.label = label;
91519a2a23afSMatthew G. Knepley   key.value = value;
9152ac17215fSMatthew G. Knepley   key.part  = part;
91539566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91549566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91559566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91579a2a23afSMatthew G. Knepley }
91589a2a23afSMatthew G. Knepley 
91599a2a23afSMatthew G. Knepley /*@
9160bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91619a2a23afSMatthew G. Knepley 
916220f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91639a2a23afSMatthew G. Knepley 
91649a2a23afSMatthew G. Knepley   Input Parameters:
9165bb7acecfSBarry Smith + dm    - The `DM`
9166bb7acecfSBarry Smith . label - The `DMLabel`
91679a2a23afSMatthew G. Knepley . value - The label value indicating the region
9168ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9169bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91709a2a23afSMatthew G. Knepley 
91719a2a23afSMatthew G. Knepley   Level: advanced
91729a2a23afSMatthew G. Knepley 
9173e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91749a2a23afSMatthew G. Knepley @*/
9175d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9176d71ae5a4SJacob Faibussowitsch {
91779a2a23afSMatthew G. Knepley   Vec             old;
91789a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91799a2a23afSMatthew G. Knepley 
91809a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91819a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91829a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91839a2a23afSMatthew G. Knepley   key.label = label;
91849a2a23afSMatthew G. Knepley   key.value = value;
9185ac17215fSMatthew G. Knepley   key.part  = part;
91869566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91879566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91889566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91899566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9190d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91929a2a23afSMatthew G. Knepley }
91939a2a23afSMatthew G. Knepley 
9194cc4c1da9SBarry Smith /*@
9195bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
91969a2a23afSMatthew G. Knepley 
919720f4b53cSBarry Smith   Not Collective
91989a2a23afSMatthew G. Knepley 
91999a2a23afSMatthew G. Knepley   Input Parameter:
9200bb7acecfSBarry Smith . dm - The `DM`
92019a2a23afSMatthew G. Knepley 
92029a2a23afSMatthew G. Knepley   Output Parameters:
9203bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9204bb7acecfSBarry Smith . values - The label values for each `Vec`
9205bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
92069a2a23afSMatthew G. Knepley 
92079bdbcad8SBarry Smith   Level: advanced
92089bdbcad8SBarry Smith 
9209bb7acecfSBarry Smith   Note:
9210bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
92119a2a23afSMatthew G. Knepley 
9212e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
92139a2a23afSMatthew G. Knepley @*/
9214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9215d71ae5a4SJacob Faibussowitsch {
92169a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92179a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92189a2a23afSMatthew G. Knepley 
92199a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92209a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92214f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92224f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92234f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92249566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92269566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92279371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92289371c9d4SSatish Balay     labels[i] = keys[i].label;
92299371c9d4SSatish Balay     values[i] = keys[i].value;
92309371c9d4SSatish Balay     parts[i]  = keys[i].part;
92319371c9d4SSatish Balay   }
92329566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92349a2a23afSMatthew G. Knepley }
92359a2a23afSMatthew G. Knepley 
92369a2a23afSMatthew G. Knepley /*@
9237bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92389a2a23afSMatthew G. Knepley 
923920f4b53cSBarry Smith   Not Collective
92409a2a23afSMatthew G. Knepley 
92419a2a23afSMatthew G. Knepley   Input Parameter:
9242bb7acecfSBarry Smith . dm - The `DM`
92439a2a23afSMatthew G. Knepley 
92449a2a23afSMatthew G. Knepley   Output Parameter:
9245bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92469a2a23afSMatthew G. Knepley 
92479a2a23afSMatthew G. Knepley   Level: advanced
92489a2a23afSMatthew G. Knepley 
9249bb7acecfSBarry Smith   Note:
9250bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9251bb7acecfSBarry Smith 
9252e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92539a2a23afSMatthew G. Knepley @*/
9254d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9255d71ae5a4SJacob Faibussowitsch {
92569a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92579a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9258d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9259d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9260e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9261e4d5475eSStefano Zampini 
9262e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92639566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9264d705d0eaSStefano Zampini   {
9265d705d0eaSStefano Zampini     Vec     *auxData;
9266d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9267d705d0eaSStefano Zampini 
9268d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9269d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9270d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9271d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9272d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9273e4d5475eSStefano Zampini   }
9274e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9275e4d5475eSStefano Zampini }
9276e4d5475eSStefano Zampini 
9277e4d5475eSStefano Zampini /*@
9278e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9279e4d5475eSStefano Zampini 
9280e4d5475eSStefano Zampini   Not Collective
9281e4d5475eSStefano Zampini 
9282e4d5475eSStefano Zampini   Input Parameter:
9283e4d5475eSStefano Zampini . dm - The `DM`
9284e4d5475eSStefano Zampini 
9285e4d5475eSStefano Zampini   Level: advanced
9286e4d5475eSStefano Zampini 
9287e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9288e4d5475eSStefano Zampini @*/
9289e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9290e4d5475eSStefano Zampini {
9291e4d5475eSStefano Zampini   Vec     *auxData;
9292e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9293e4d5475eSStefano Zampini 
9294e4d5475eSStefano Zampini   PetscFunctionBegin;
9295e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9296d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9297e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9298d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9299d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9300e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9301e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
93023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93039a2a23afSMatthew G. Knepley }
9304b5a892a1SMatthew G. Knepley 
9305cc4c1da9SBarry Smith /*@
9306bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9307b5a892a1SMatthew G. Knepley 
930820f4b53cSBarry Smith   Not Collective
9309b5a892a1SMatthew G. Knepley 
9310b5a892a1SMatthew G. Knepley   Input Parameters:
9311bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9312b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9313b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9314b5a892a1SMatthew G. Knepley 
9315b5a892a1SMatthew G. Knepley   Output Parameters:
9316bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9317b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9318b5a892a1SMatthew G. Knepley 
9319b5a892a1SMatthew G. Knepley   Level: advanced
9320b5a892a1SMatthew G. Knepley 
9321bb7acecfSBarry Smith   Note:
9322bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9323bb7acecfSBarry Smith 
932485036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9325bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9326bb7acecfSBarry Smith 
9327bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9328bb7acecfSBarry Smith 
93291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9330b5a892a1SMatthew G. Knepley @*/
9331d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9332d71ae5a4SJacob Faibussowitsch {
9333b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
933485036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9335b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9336b5a892a1SMatthew G. Knepley 
9337b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93389371c9d4SSatish Balay   if (!nO) {
93399371c9d4SSatish Balay     *ornt  = 0;
93409371c9d4SSatish Balay     *found = PETSC_TRUE;
93413ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93429371c9d4SSatish Balay   }
9343b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
934485036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9345b5a892a1SMatthew G. Knepley 
93469371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93479371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93489371c9d4SSatish Balay     if (c == cS) {
93499371c9d4SSatish Balay       *ornt = o;
93509371c9d4SSatish Balay       break;
93519371c9d4SSatish Balay     }
9352b5a892a1SMatthew G. Knepley   }
9353b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9355b5a892a1SMatthew G. Knepley }
9356b5a892a1SMatthew G. Knepley 
9357cc4c1da9SBarry Smith /*@
9358bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9359b5a892a1SMatthew G. Knepley 
936020f4b53cSBarry Smith   Not Collective
9361b5a892a1SMatthew G. Knepley 
9362b5a892a1SMatthew G. Knepley   Input Parameters:
9363bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9364b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9365b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9366b5a892a1SMatthew G. Knepley 
93672fe279fdSBarry Smith   Output Parameter:
9368bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9369b5a892a1SMatthew G. Knepley 
9370b5a892a1SMatthew G. Knepley   Level: advanced
9371b5a892a1SMatthew G. Knepley 
9372bb7acecfSBarry Smith   Note:
9373bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9374bb7acecfSBarry Smith 
937573ff1848SBarry Smith   Developer Note:
9376bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9377bb7acecfSBarry Smith 
93781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9379b5a892a1SMatthew G. Knepley @*/
9380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9381d71ae5a4SJacob Faibussowitsch {
9382b5a892a1SMatthew G. Knepley   PetscBool found;
9383b5a892a1SMatthew G. Knepley 
9384b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93859566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93867a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9388b5a892a1SMatthew G. Knepley }
9389b5a892a1SMatthew G. Knepley 
9390cc4c1da9SBarry Smith /*@
9391bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9392b5a892a1SMatthew G. Knepley 
939320f4b53cSBarry Smith   Not Collective
9394b5a892a1SMatthew G. Knepley 
9395b5a892a1SMatthew G. Knepley   Input Parameters:
9396bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9397b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9398b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9399b5a892a1SMatthew G. Knepley 
9400b5a892a1SMatthew G. Knepley   Output Parameters:
9401bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9402b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9403b5a892a1SMatthew G. Knepley 
9404b5a892a1SMatthew G. Knepley   Level: advanced
9405b5a892a1SMatthew G. Knepley 
940673ff1848SBarry Smith   Notes:
9407bb7acecfSBarry Smith   An arrangement is a vertex order
9408bb7acecfSBarry Smith 
940985036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9410bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9411bb7acecfSBarry Smith 
9412bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9413bb7acecfSBarry Smith 
941485036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9415b5a892a1SMatthew G. Knepley @*/
9416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9417d71ae5a4SJacob Faibussowitsch {
9418b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
941985036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9420b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9421b5a892a1SMatthew G. Knepley 
9422b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94239371c9d4SSatish Balay   if (!nO) {
94249371c9d4SSatish Balay     *ornt  = 0;
94259371c9d4SSatish Balay     *found = PETSC_TRUE;
94263ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94279371c9d4SSatish Balay   }
9428b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
942985036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9430b5a892a1SMatthew G. Knepley 
94319371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94329371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94339371c9d4SSatish Balay     if (c == cS) {
94349371c9d4SSatish Balay       *ornt = o;
94359371c9d4SSatish Balay       break;
94369371c9d4SSatish Balay     }
9437b5a892a1SMatthew G. Knepley   }
9438b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9440b5a892a1SMatthew G. Knepley }
9441b5a892a1SMatthew G. Knepley 
9442cc4c1da9SBarry Smith /*@
9443bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9444b5a892a1SMatthew G. Knepley 
944520f4b53cSBarry Smith   Not Collective
9446b5a892a1SMatthew G. Knepley 
9447b5a892a1SMatthew G. Knepley   Input Parameters:
9448bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9449b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9450b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9451b5a892a1SMatthew G. Knepley 
94522fe279fdSBarry Smith   Output Parameter:
9453bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9454b5a892a1SMatthew G. Knepley 
9455b5a892a1SMatthew G. Knepley   Level: advanced
9456b5a892a1SMatthew G. Knepley 
9457bb7acecfSBarry Smith   Note:
9458bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9459bb7acecfSBarry Smith 
946073ff1848SBarry Smith   Developer Note:
9461bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9462bb7acecfSBarry Smith 
94631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9464b5a892a1SMatthew G. Knepley @*/
9465d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9466d71ae5a4SJacob Faibussowitsch {
9467b5a892a1SMatthew G. Knepley   PetscBool found;
9468b5a892a1SMatthew G. Knepley 
9469b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94709566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94717a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9473b5a892a1SMatthew G. Knepley }
9474012bc364SMatthew G. Knepley 
9475cc4c1da9SBarry Smith /*@
9476012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9477012bc364SMatthew G. Knepley 
947820f4b53cSBarry Smith   Not Collective
9479012bc364SMatthew G. Knepley 
9480012bc364SMatthew G. Knepley   Input Parameters:
9481bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9482012bc364SMatthew G. Knepley - point - Coordinates of the point
9483012bc364SMatthew G. Knepley 
94842fe279fdSBarry Smith   Output Parameter:
9485012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9486012bc364SMatthew G. Knepley 
9487012bc364SMatthew G. Knepley   Level: advanced
9488012bc364SMatthew G. Knepley 
94891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9490012bc364SMatthew G. Knepley @*/
9491d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9492d71ae5a4SJacob Faibussowitsch {
9493012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9494012bc364SMatthew G. Knepley   PetscInt  d;
9495012bc364SMatthew G. Knepley 
9496012bc364SMatthew G. Knepley   PetscFunctionBegin;
9497012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9498012bc364SMatthew G. Knepley   switch (ct) {
9499012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9500012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9501012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
95029371c9d4SSatish Balay       if (point[d] < -1.0) {
95039371c9d4SSatish Balay         *inside = PETSC_FALSE;
95049371c9d4SSatish Balay         break;
95059371c9d4SSatish Balay       }
9506012bc364SMatthew G. Knepley       sum += point[d];
9507012bc364SMatthew G. Knepley     }
95089371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
95099371c9d4SSatish Balay       *inside = PETSC_FALSE;
95109371c9d4SSatish Balay       break;
95119371c9d4SSatish Balay     }
9512012bc364SMatthew G. Knepley     break;
9513012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9514012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9515012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95169371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95179371c9d4SSatish Balay         *inside = PETSC_FALSE;
9518012bc364SMatthew G. Knepley         break;
95199371c9d4SSatish Balay       }
95209371c9d4SSatish Balay     break;
9521d71ae5a4SJacob Faibussowitsch   default:
9522d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9523012bc364SMatthew G. Knepley   }
95243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9525012bc364SMatthew G. Knepley }
9526adc21957SMatthew G. Knepley 
9527adc21957SMatthew G. Knepley /*@
9528adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9529adc21957SMatthew G. Knepley 
9530adc21957SMatthew G. Knepley   Logically collective
9531adc21957SMatthew G. Knepley 
9532adc21957SMatthew G. Knepley   Input Parameters:
9533adc21957SMatthew G. Knepley + dm      - The DM
9534adc21957SMatthew G. Knepley - reorder - Flag for reordering
9535adc21957SMatthew G. Knepley 
9536adc21957SMatthew G. Knepley   Level: intermediate
9537adc21957SMatthew G. Knepley 
9538adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9539adc21957SMatthew G. Knepley @*/
9540adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9541adc21957SMatthew G. Knepley {
9542adc21957SMatthew G. Knepley   PetscFunctionBegin;
9543adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9544adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9545adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9546adc21957SMatthew G. Knepley }
9547adc21957SMatthew G. Knepley 
9548adc21957SMatthew G. Knepley /*@
9549adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9550adc21957SMatthew G. Knepley 
9551adc21957SMatthew G. Knepley   Not collective
9552adc21957SMatthew G. Knepley 
9553adc21957SMatthew G. Knepley   Input Parameter:
9554adc21957SMatthew G. Knepley . dm - The DM
9555adc21957SMatthew G. Knepley 
9556adc21957SMatthew G. Knepley   Output Parameter:
9557adc21957SMatthew G. Knepley . reorder - Flag for reordering
9558adc21957SMatthew G. Knepley 
9559adc21957SMatthew G. Knepley   Level: intermediate
9560adc21957SMatthew G. Knepley 
9561adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9562adc21957SMatthew G. Knepley @*/
9563adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9564adc21957SMatthew G. Knepley {
9565adc21957SMatthew G. Knepley   PetscFunctionBegin;
9566adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9567adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9568adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9569adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9570adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9571adc21957SMatthew G. Knepley }
9572adc21957SMatthew G. Knepley 
9573cc4c1da9SBarry Smith /*@
9574adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9575adc21957SMatthew G. Knepley 
9576adc21957SMatthew G. Knepley   Logically collective
9577adc21957SMatthew G. Knepley 
9578adc21957SMatthew G. Knepley   Input Parameters:
9579adc21957SMatthew G. Knepley + dm      - The DM
9580adc21957SMatthew G. Knepley - reorder - The reordering method
9581adc21957SMatthew G. Knepley 
9582adc21957SMatthew G. Knepley   Level: intermediate
9583adc21957SMatthew G. Knepley 
9584adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9585adc21957SMatthew G. Knepley @*/
9586adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9587adc21957SMatthew G. Knepley {
9588adc21957SMatthew G. Knepley   PetscFunctionBegin;
9589adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9590adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9591adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9592adc21957SMatthew G. Knepley }
9593adc21957SMatthew G. Knepley 
9594cc4c1da9SBarry Smith /*@
9595adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9596adc21957SMatthew G. Knepley 
9597adc21957SMatthew G. Knepley   Not collective
9598adc21957SMatthew G. Knepley 
9599adc21957SMatthew G. Knepley   Input Parameter:
9600adc21957SMatthew G. Knepley . dm - The DM
9601adc21957SMatthew G. Knepley 
9602adc21957SMatthew G. Knepley   Output Parameter:
9603adc21957SMatthew G. Knepley . reorder - The reordering method
9604adc21957SMatthew G. Knepley 
9605adc21957SMatthew G. Knepley   Level: intermediate
9606adc21957SMatthew G. Knepley 
9607adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9608adc21957SMatthew G. Knepley @*/
9609adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9610adc21957SMatthew G. Knepley {
9611adc21957SMatthew G. Knepley   PetscFunctionBegin;
9612adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9613adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9614adc21957SMatthew G. Knepley   *reorder = NULL;
9615adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9616adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9617adc21957SMatthew G. Knepley }
9618