xref: /petsc/src/dm/interface/dm.c (revision 8e9849d2db9591cba40c1f87bbee73f65cbbea93)
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 
8868e704042SBarry Smith   Note:
8878e704042SBarry Smith   For some `DMType` such as `DMDA` this cannot be called after `DMSetUp()` has been called.
8888e704042SBarry Smith 
8891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
890bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
8918e704042SBarry Smith          `DMSetOptionsPrefix()`, `DMType`, `DMPLEX`, `DMDA`, `DMSetUp()`
892d7bf68aeSBarry Smith @*/
893d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
894d71ae5a4SJacob Faibussowitsch {
8957781c08eSBarry Smith   char      typeName[256];
896ca266f36SBarry Smith   PetscBool flg;
897d7bf68aeSBarry Smith 
898d7bf68aeSBarry Smith   PetscFunctionBegin;
899171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9019566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9029566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
903dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
904d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9071baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9091baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
910863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-dm_bind_below", "Set the size threshold (in entries) below which the Vec is bound to the CPU", "VecBindToCPU", dm->bind_below, &dm->bind_below, &flg));
913eb9d3e4dSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_ignore_perm_output", "Ignore the local section permutation on output", "DMGetOutputDM", dm->ignorePermOutput, &dm->ignorePermOutput, NULL));
914dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
915f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
916dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
917d0609cedSBarry Smith   PetscOptionsEnd();
9183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
919d7bf68aeSBarry Smith }
920d7bf68aeSBarry Smith 
921ffeef943SBarry Smith /*@
922bb7acecfSBarry Smith   DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
923fe2efc57SMark 
92420f4b53cSBarry Smith   Collective
925fe2efc57SMark 
926fe2efc57SMark   Input Parameters:
927bb7acecfSBarry Smith + dm   - the `DM` object
92820f4b53cSBarry Smith . obj  - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
92960225df5SJacob Faibussowitsch - name - option string that is used to activate viewing
930fe2efc57SMark 
931fe2efc57SMark   Level: intermediate
932bb7acecfSBarry Smith 
933bb7acecfSBarry Smith   Note:
934bb7acecfSBarry Smith   See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
935bb7acecfSBarry Smith 
93660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`
937fe2efc57SMark @*/
938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
939d71ae5a4SJacob Faibussowitsch {
940fe2efc57SMark   PetscFunctionBegin;
941fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9429566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
944fe2efc57SMark }
945fe2efc57SMark 
946ffeef943SBarry Smith /*@
947bb7acecfSBarry Smith   DMView - Views a `DM`. Depending on the `PetscViewer` and its `PetscViewerFormat` it may print some ASCII information about the `DM` to the screen or a file or
948bb7acecfSBarry Smith   save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94947c6ae99SBarry Smith 
95020f4b53cSBarry Smith   Collective
95147c6ae99SBarry Smith 
952d8d19677SJose E. Roman   Input Parameters:
953bb7acecfSBarry Smith + dm - the `DM` object to view
95447c6ae99SBarry Smith - v  - the viewer
95547c6ae99SBarry Smith 
95620f4b53cSBarry Smith   Level: beginner
95720f4b53cSBarry Smith 
95873ff1848SBarry Smith   Note:
959bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
960bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
961bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
962cd7e8a5eSksagiyam 
9631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96447c6ae99SBarry Smith @*/
965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
966d71ae5a4SJacob Faibussowitsch {
96732c0f0efSBarry Smith   PetscBool         isbinary;
96876a8abe0SBarry Smith   PetscMPIInt       size;
96976a8abe0SBarry Smith   PetscViewerFormat format;
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith   PetscFunctionBegin;
972171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97348a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
974b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97574903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97674903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97774903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97874903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97974903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98074903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98174903a4fSStefano Zampini      in an error here */
98274903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9839566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
984b1b135c8SBarry Smith 
9859566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9873ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9899566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
99032c0f0efSBarry Smith   if (isbinary) {
99155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99232c0f0efSBarry Smith     char     type[256];
99332c0f0efSBarry Smith 
9949566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
995c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9969566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99732c0f0efSBarry Smith   }
998dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
100047c6ae99SBarry Smith }
100147c6ae99SBarry Smith 
100247c6ae99SBarry Smith /*@
1003bb7acecfSBarry Smith   DMCreateGlobalVector - Creates a global vector from a `DM` object. A global vector is a parallel vector that has no duplicate values shared between MPI ranks,
1004bb7acecfSBarry Smith   that is it has no ghost locations.
100547c6ae99SBarry Smith 
100620f4b53cSBarry Smith   Collective
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith   Input Parameter:
1009bb7acecfSBarry Smith . dm - the `DM` object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith   Output Parameter:
101247c6ae99SBarry Smith . vec - the global vector
101347c6ae99SBarry Smith 
1014073dac72SJed Brown   Level: beginner
101547c6ae99SBarry Smith 
10161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1017bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101847c6ae99SBarry Smith @*/
1019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1020d71ae5a4SJacob Faibussowitsch {
102147c6ae99SBarry Smith   PetscFunctionBegin;
1022171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10234f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1024dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102576bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1026c6b011d8SStefano Zampini     DM vdm;
1027c6b011d8SStefano Zampini 
10289566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10297a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1030c6b011d8SStefano Zampini   }
10313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103247c6ae99SBarry Smith }
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith /*@
1035bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith   Not Collective
103847c6ae99SBarry Smith 
103947c6ae99SBarry Smith   Input Parameter:
1040bb7acecfSBarry Smith . dm - the `DM` object
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith   Output Parameter:
104347c6ae99SBarry Smith . vec - the local vector
104447c6ae99SBarry Smith 
1045073dac72SJed Brown   Level: beginner
104647c6ae99SBarry Smith 
104720f4b53cSBarry Smith   Note:
1048bb7acecfSBarry Smith   A local vector usually has ghost locations that contain values that are owned by different MPI ranks. A global vector has no ghost locations.
1049bb7acecfSBarry Smith 
10501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1051bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105247c6ae99SBarry Smith @*/
1053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1054d71ae5a4SJacob Faibussowitsch {
105547c6ae99SBarry Smith   PetscFunctionBegin;
1056171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10574f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1058dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1060c6b011d8SStefano Zampini     DM vdm;
1061c6b011d8SStefano Zampini 
10629566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10637a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1064c6b011d8SStefano Zampini   }
10653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106647c6ae99SBarry Smith }
106747c6ae99SBarry Smith 
10681411c6eeSJed Brown /*@
1069bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10701411c6eeSJed Brown 
107120f4b53cSBarry Smith   Collective
10721411c6eeSJed Brown 
10731411c6eeSJed Brown   Input Parameter:
1074bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10751411c6eeSJed Brown 
10761411c6eeSJed Brown   Output Parameter:
10771411c6eeSJed Brown . ltog - the mapping
10781411c6eeSJed Brown 
1079bb7acecfSBarry Smith   Level: advanced
10801411c6eeSJed Brown 
10811411c6eeSJed Brown   Notes:
1082bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10831411c6eeSJed Brown 
1084bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1085bb7acecfSBarry Smith   need to use this function with those objects.
1086bb7acecfSBarry Smith 
1087bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1088bb7acecfSBarry Smith 
108960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1090bb7acecfSBarry Smith           `DMCreateMatrix()`
10911411c6eeSJed Brown @*/
1092d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1093d71ae5a4SJacob Faibussowitsch {
10940be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10951411c6eeSJed Brown 
10961411c6eeSJed Brown   PetscFunctionBegin;
10971411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10984f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
10991411c6eeSJed Brown   if (!dm->ltogmap) {
110037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110137d0c07bSMatthew G Knepley 
11029566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110337d0c07bSMatthew G Knepley     if (section) {
1104a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110537d0c07bSMatthew G Knepley       PetscInt       *ltog;
1106ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110737d0c07bSMatthew G Knepley 
11089566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11109566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11119566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1113e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111437d0c07bSMatthew G Knepley 
111537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11201a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11211a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1122ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11235227eafbSStefano Zampini 
1124e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11255227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1126e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1127e6befd46SJed Brown             cind++;
1128e6befd46SJed Brown           } else {
11295227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1130e6befd46SJed Brown           }
113137d0c07bSMatthew G Knepley         }
113237d0c07bSMatthew G Knepley       }
1133bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11349371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11359371c9d4SSatish Balay       bsLocal[1] = bs;
11369566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11379371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11389371c9d4SSatish Balay         bs = 1;
11399371c9d4SSatish Balay       } else {
11409371c9d4SSatish Balay         bs = bsMinMax[0];
11419371c9d4SSatish Balay       }
11427591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11437591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1144ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1145ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1146ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1147ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1148ca469d19SJed Brown         }
1149ccf3bd66SMatthew G. Knepley         n /= bs;
1150ccf3bd66SMatthew G. Knepley       }
11519566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1152dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115337d0c07bSMatthew G Knepley   }
11541411c6eeSJed Brown   *ltog = dm->ltogmap;
11553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11561411c6eeSJed Brown }
11571411c6eeSJed Brown 
11581411c6eeSJed Brown /*@
1159bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11601411c6eeSJed Brown 
11611411c6eeSJed Brown   Not Collective
11621411c6eeSJed Brown 
11631411c6eeSJed Brown   Input Parameter:
1164bb7acecfSBarry Smith . dm - the `DM` with block structure
11651411c6eeSJed Brown 
11661411c6eeSJed Brown   Output Parameter:
11671411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11681411c6eeSJed Brown 
11691411c6eeSJed Brown   Level: intermediate
11701411c6eeSJed Brown 
117173ff1848SBarry Smith   Notes:
1172bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1173bb7acecfSBarry Smith 
1174bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1175bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1176bb7acecfSBarry Smith 
11771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11781411c6eeSJed Brown @*/
1179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1180d71ae5a4SJacob Faibussowitsch {
11811411c6eeSJed Brown   PetscFunctionBegin;
11821411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11834f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11847a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11851411c6eeSJed Brown   *bs = dm->bs;
11863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11871411c6eeSJed Brown }
11881411c6eeSJed Brown 
1189ffeef943SBarry Smith /*@
1190bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1191bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119247c6ae99SBarry Smith 
119320f4b53cSBarry Smith   Collective
119447c6ae99SBarry Smith 
1195d8d19677SJose E. Roman   Input Parameters:
1196bb7acecfSBarry Smith + dmc - the `DM` object
1197bb7acecfSBarry Smith - dmf - the second, finer `DM` object
119847c6ae99SBarry Smith 
1199d8d19677SJose E. Roman   Output Parameters:
120047c6ae99SBarry Smith + mat - the interpolation
1201b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
120247c6ae99SBarry Smith 
120347c6ae99SBarry Smith   Level: developer
120447c6ae99SBarry Smith 
120595452b02SPatrick Sanan   Notes:
1206bb7acecfSBarry Smith   For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1207bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1208d52bd9f3SBarry Smith 
1209bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1210bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121185afcc9aSBarry Smith 
12121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121347c6ae99SBarry Smith @*/
1214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1215d71ae5a4SJacob Faibussowitsch {
121647c6ae99SBarry Smith   PetscFunctionBegin;
1217a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1218a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12194f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1221dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122447c6ae99SBarry Smith }
122547c6ae99SBarry Smith 
12263ad4599aSBarry Smith /*@
1227a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1228a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12292ed6491fSPatrick Sanan 
12302ed6491fSPatrick Sanan   Input Parameters:
1231bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1232bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12332ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   Output Parameter:
12362ed6491fSPatrick Sanan . scale - the scaled vector
12372ed6491fSPatrick Sanan 
1238bb7acecfSBarry Smith   Level: advanced
12392ed6491fSPatrick Sanan 
124073ff1848SBarry Smith   Note:
1241a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1242a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1243a4e35b19SJacob Faibussowitsch 
124473ff1848SBarry Smith   Developer Note:
1245bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1246e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1247e9c74fd6SRichard Tran Mills 
124860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12492ed6491fSPatrick Sanan @*/
1250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1251d71ae5a4SJacob Faibussowitsch {
12522ed6491fSPatrick Sanan   Vec         fine;
12532ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12549704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1255e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12569704db99SRichard Tran Mills #endif
12572ed6491fSPatrick Sanan 
12582ed6491fSPatrick Sanan   PetscFunctionBegin;
12599566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12609566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12619566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12629704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12639704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12649704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12659704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12669566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1267e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12689566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12699566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12709566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
127183aa49f4SRichard Tran Mills   }
12729704db99SRichard Tran Mills #endif
12739566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12759566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12772ed6491fSPatrick Sanan }
12782ed6491fSPatrick Sanan 
12792ed6491fSPatrick Sanan /*@
1280bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1281bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12823ad4599aSBarry Smith 
128320f4b53cSBarry Smith   Collective
12843ad4599aSBarry Smith 
1285d8d19677SJose E. Roman   Input Parameters:
1286bb7acecfSBarry Smith + dmc - the `DM` object
1287bb7acecfSBarry Smith - dmf - the second, finer `DM` object
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith   Output Parameter:
12903ad4599aSBarry Smith . mat - the restriction
12913ad4599aSBarry Smith 
12923ad4599aSBarry Smith   Level: developer
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith   Note:
1295bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1296bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12973ad4599aSBarry Smith 
12981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12993ad4599aSBarry Smith @*/
1300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1301d71ae5a4SJacob Faibussowitsch {
13023ad4599aSBarry Smith   PetscFunctionBegin;
1303a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13054f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13103ad4599aSBarry Smith }
13113ad4599aSBarry Smith 
131247c6ae99SBarry Smith /*@
1313a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
131447c6ae99SBarry Smith 
131520f4b53cSBarry Smith   Collective
131647c6ae99SBarry Smith 
1317d8d19677SJose E. Roman   Input Parameters:
1318bb7acecfSBarry Smith + dac - the `DM` object
1319bb7acecfSBarry Smith - daf - the second, finer `DM` object
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith   Output Parameter:
13226dbf9973SLawrence Mitchell . mat - the injection
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith   Level: developer
132547c6ae99SBarry Smith 
1326a4e35b19SJacob Faibussowitsch   Notes:
1327a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1328a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1329a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1330a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1331a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1332a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1333a4e35b19SJacob Faibussowitsch 
1334bb7acecfSBarry Smith   For `DMDA` objects this only works for "uniform refinement", that is the refined mesh was obtained `DMRefine()` or the coarse mesh was obtained by
1335bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133685afcc9aSBarry Smith 
13371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1338bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133947c6ae99SBarry Smith @*/
1340d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1341d71ae5a4SJacob Faibussowitsch {
134247c6ae99SBarry Smith   PetscFunctionBegin;
1343a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1344a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13454f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1347dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13489566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
135047c6ae99SBarry Smith }
135147c6ae99SBarry Smith 
1352b412c318SBarry Smith /*@
1353bb7acecfSBarry Smith   DMCreateMassMatrix - Gets the mass matrix between two `DM` objects, M_ij = \int \phi_i \psi_j where the \phi are Galerkin basis functions for a
1354bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1355bd041c0cSMatthew G. Knepley 
135620f4b53cSBarry Smith   Collective
1357bd041c0cSMatthew G. Knepley 
1358d8d19677SJose E. Roman   Input Parameters:
1359bb7acecfSBarry Smith + dmc - the target `DM` object
13608c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1361bd041c0cSMatthew G. Knepley 
1362bd041c0cSMatthew G. Knepley   Output Parameter:
1363b4937a87SMatthew G. Knepley . mat - the mass matrix
1364bd041c0cSMatthew G. Knepley 
1365bd041c0cSMatthew G. Knepley   Level: developer
1366bd041c0cSMatthew G. Knepley 
1367bb7acecfSBarry Smith   Notes:
1368bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1369bb7acecfSBarry Smith 
13708c1c0954SStefano Zampini   if `dmc` is `dmf` or `NULL`, then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1371bb7acecfSBarry Smith 
137242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1373bd041c0cSMatthew G. Knepley @*/
1374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1375d71ae5a4SJacob Faibussowitsch {
1376bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1377b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13788c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1379b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13804f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13818c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1382dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13838c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
13843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1385b4937a87SMatthew G. Knepley }
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley /*@
1388bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1389b4937a87SMatthew G. Knepley 
139020f4b53cSBarry Smith   Collective
1391b4937a87SMatthew G. Knepley 
1392b4937a87SMatthew G. Knepley   Input Parameter:
1393bb7acecfSBarry Smith . dm - the `DM` object
1394b4937a87SMatthew G. Knepley 
1395b4937a87SMatthew G. Knepley   Output Parameter:
1396*8e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
1397*8e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1398b4937a87SMatthew G. Knepley 
1399b4937a87SMatthew G. Knepley   Level: developer
1400b4937a87SMatthew G. Knepley 
1401bb7acecfSBarry Smith   Note:
1402bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1403bb7acecfSBarry Smith 
140460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1405b4937a87SMatthew G. Knepley @*/
1406*8e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1407d71ae5a4SJacob Faibussowitsch {
1408b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1409b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1410*8e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
1411*8e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
1412*8e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1414bd041c0cSMatthew G. Knepley }
1415bd041c0cSMatthew G. Knepley 
1416bd041c0cSMatthew G. Knepley /*@
1417bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1418bb7acecfSBarry Smith   of a PDE on the `DM`.
141947c6ae99SBarry Smith 
142020f4b53cSBarry Smith   Collective
142147c6ae99SBarry Smith 
1422d8d19677SJose E. Roman   Input Parameters:
1423bb7acecfSBarry Smith + dm    - the `DM` object
1424bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
142547c6ae99SBarry Smith 
142647c6ae99SBarry Smith   Output Parameter:
142747c6ae99SBarry Smith . coloring - the coloring
142847c6ae99SBarry Smith 
14291bf8429eSBarry Smith   Level: developer
14301bf8429eSBarry Smith 
1431ec5066bdSBarry Smith   Notes:
1432bb7acecfSBarry 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
1433bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1434ec5066bdSBarry Smith 
1435bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14361bf8429eSBarry 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,
14371bf8429eSBarry Smith   otherwise an error will be generated.
1438ec5066bdSBarry Smith 
14391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1440aab9d709SJed Brown @*/
1441d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1442d71ae5a4SJacob Faibussowitsch {
144347c6ae99SBarry Smith   PetscFunctionBegin;
1444171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14454f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1446dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
144847c6ae99SBarry Smith }
144947c6ae99SBarry Smith 
1450b412c318SBarry Smith /*@
1451bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
145247c6ae99SBarry Smith 
145320f4b53cSBarry Smith   Collective
145447c6ae99SBarry Smith 
145547c6ae99SBarry Smith   Input Parameter:
1456bb7acecfSBarry Smith . dm - the `DM` object
145747c6ae99SBarry Smith 
145847c6ae99SBarry Smith   Output Parameter:
145947c6ae99SBarry Smith . mat - the empty Jacobian
146047c6ae99SBarry Smith 
146120f4b53cSBarry Smith   Options Database Key:
1462bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1463f27dd7c6SMatthew G. Knepley 
146420f4b53cSBarry Smith   Level: beginner
146520f4b53cSBarry Smith 
146695452b02SPatrick Sanan   Notes:
146795452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
146894013140SBarry Smith   do not need to do it yourself.
146994013140SBarry Smith 
147094013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1471bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
147294013140SBarry Smith 
1473bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
147494013140SBarry Smith   internally by PETSc.
147594013140SBarry Smith 
1476bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1477bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
147894013140SBarry Smith 
14791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1480aab9d709SJed Brown @*/
1481d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1482d71ae5a4SJacob Faibussowitsch {
148347c6ae99SBarry Smith   PetscFunctionBegin;
1484171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14854f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14869566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1488dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1490c6b011d8SStefano Zampini     DM mdm;
1491c6b011d8SStefano Zampini 
14929566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14937a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1494c6b011d8SStefano Zampini   }
1495e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1496e5e52638SMatthew G. Knepley   if (dm->Nf) {
1497e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1498649ef022SMatthew Knepley     PetscInt     Nf, f;
1499e571a35bSMatthew G. Knepley 
15009566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1501649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1502649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15039566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15049566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15059566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1506649ef022SMatthew Knepley         break;
1507e571a35bSMatthew G. Knepley       }
1508649ef022SMatthew Knepley     }
1509649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1510649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15119566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15129566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15139566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1514e571a35bSMatthew G. Knepley       }
1515e571a35bSMatthew G. Knepley     }
1516e571a35bSMatthew G. Knepley   }
15179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
151947c6ae99SBarry Smith }
152047c6ae99SBarry Smith 
1521732e2eb9SMatthew G Knepley /*@
1522a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1523a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1524a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1525aa0f6e3cSJed Brown 
152620f4b53cSBarry Smith   Logically Collective
1527aa0f6e3cSJed Brown 
1528aa0f6e3cSJed Brown   Input Parameters:
1529bb7acecfSBarry Smith + dm   - the `DM`
1530bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1531aa0f6e3cSJed Brown 
1532aa0f6e3cSJed Brown   Level: developer
1533aa0f6e3cSJed Brown 
153473ff1848SBarry Smith   Note:
1535a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1536a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1537a4e35b19SJacob Faibussowitsch 
15381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1539aa0f6e3cSJed Brown @*/
1540d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1541d71ae5a4SJacob Faibussowitsch {
1542aa0f6e3cSJed Brown   PetscFunctionBegin;
1543aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1544aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1546aa0f6e3cSJed Brown }
1547aa0f6e3cSJed Brown 
1548aa0f6e3cSJed Brown /*@
1549bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1550732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1551732e2eb9SMatthew G Knepley 
155220f4b53cSBarry Smith   Logically Collective
1553732e2eb9SMatthew G Knepley 
1554d8d19677SJose E. Roman   Input Parameters:
1555bb7acecfSBarry Smith + dm   - the `DM`
1556bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1557732e2eb9SMatthew G Knepley 
155820f4b53cSBarry Smith   Options Database Key:
1559bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1560f27dd7c6SMatthew G. Knepley 
156120f4b53cSBarry Smith   Level: developer
156220f4b53cSBarry Smith 
15631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1564732e2eb9SMatthew G Knepley @*/
1565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1566d71ae5a4SJacob Faibussowitsch {
1567732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1568732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1569732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1571732e2eb9SMatthew G Knepley }
1572732e2eb9SMatthew G Knepley 
1573b06ff27eSHong Zhang /*@
1574bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1575bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1576b06ff27eSHong Zhang 
157720f4b53cSBarry Smith   Logically Collective
1578b06ff27eSHong Zhang 
1579d8d19677SJose E. Roman   Input Parameters:
1580bb7acecfSBarry Smith + dm   - the `DM`
1581da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1582b06ff27eSHong Zhang 
1583b06ff27eSHong Zhang   Level: developer
1584bb7acecfSBarry Smith 
15851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1586b06ff27eSHong Zhang @*/
1587d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1588d71ae5a4SJacob Faibussowitsch {
1589b06ff27eSHong Zhang   PetscFunctionBegin;
1590b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1591b06ff27eSHong Zhang   dm->structure_only = only;
15923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1593b06ff27eSHong Zhang }
1594b06ff27eSHong Zhang 
1595863027abSJed Brown /*@
1596863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1597863027abSJed Brown 
159820f4b53cSBarry Smith   Logically Collective
1599863027abSJed Brown 
1600863027abSJed Brown   Input Parameters:
1601863027abSJed Brown + dm    - the `DM`
1602863027abSJed Brown - btype - block by topological point or field node
1603863027abSJed Brown 
160420f4b53cSBarry Smith   Options Database Key:
16050e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1606863027abSJed Brown 
160720f4b53cSBarry Smith   Level: advanced
160820f4b53cSBarry Smith 
16091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1610863027abSJed Brown @*/
1611863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1612863027abSJed Brown {
1613863027abSJed Brown   PetscFunctionBegin;
1614863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1615863027abSJed Brown   dm->blocking_type = btype;
16163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1617863027abSJed Brown }
1618863027abSJed Brown 
1619863027abSJed Brown /*@
1620863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1621863027abSJed Brown 
1622863027abSJed Brown   Not Collective
1623863027abSJed Brown 
16242fe279fdSBarry Smith   Input Parameter:
1625863027abSJed Brown . dm - the `DM`
1626863027abSJed Brown 
16272fe279fdSBarry Smith   Output Parameter:
1628863027abSJed Brown . btype - block by topological point or field node
1629863027abSJed Brown 
1630863027abSJed Brown   Level: advanced
1631863027abSJed Brown 
16321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1633863027abSJed Brown @*/
1634863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1635863027abSJed Brown {
1636863027abSJed Brown   PetscFunctionBegin;
1637863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16384f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1639863027abSJed Brown   *btype = dm->blocking_type;
16403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1641863027abSJed Brown }
1642863027abSJed Brown 
1643a89ea682SMatthew G Knepley /*@C
1644bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1645a89ea682SMatthew G Knepley 
1646a89ea682SMatthew G Knepley   Not Collective
1647a89ea682SMatthew G Knepley 
1648a89ea682SMatthew G Knepley   Input Parameters:
1649bb7acecfSBarry Smith + dm    - the `DM` object
1650a5b23f4aSJose E. Roman . count - The minimum size
165120f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1652a89ea682SMatthew G Knepley 
1653a89ea682SMatthew G Knepley   Output Parameter:
165460225df5SJacob Faibussowitsch . mem - the work array
1655a89ea682SMatthew G Knepley 
1656a89ea682SMatthew G Knepley   Level: developer
1657a89ea682SMatthew G Knepley 
165873ff1848SBarry Smith   Notes:
1659da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1660bb7acecfSBarry Smith 
1661bb7acecfSBarry Smith   The array may contain nonzero values
1662bb7acecfSBarry Smith 
16631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1664a89ea682SMatthew G Knepley @*/
1665d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1666d71ae5a4SJacob Faibussowitsch {
1667aa1993deSMatthew G Knepley   DMWorkLink  link;
166869291d52SBarry Smith   PetscMPIInt dsize;
1669a89ea682SMatthew G Knepley 
1670a89ea682SMatthew G Knepley   PetscFunctionBegin;
1671a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16724f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1673442f3b32SStefano Zampini   if (!count) {
1674442f3b32SStefano Zampini     *(void **)mem = NULL;
1675442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1676442f3b32SStefano Zampini   }
1677aa1993deSMatthew G Knepley   if (dm->workin) {
1678aa1993deSMatthew G Knepley     link       = dm->workin;
1679aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1680aa1993deSMatthew G Knepley   } else {
16814dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1682a89ea682SMatthew G Knepley   }
1683b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1684b77fa653SStefano Zampini      Get size directly */
1685b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1686b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1687b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1688b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1689b77fa653SStefano Zampini #endif
1690b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1691b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1692b77fa653SStefano Zampini #endif
1693b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1694b77fa653SStefano Zampini 
16955056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16969566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1698854ce69bSBarry Smith     link->bytes = dsize * count;
1699aa1993deSMatthew G Knepley   }
1700aa1993deSMatthew G Knepley   link->next  = dm->workout;
1701aa1993deSMatthew G Knepley   dm->workout = link;
1702cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
170300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
170400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
170500d952a4SJed Brown #endif
1706aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1708a89ea682SMatthew G Knepley }
1709a89ea682SMatthew G Knepley 
1710aa1993deSMatthew G Knepley /*@C
1711bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1712aa1993deSMatthew G Knepley 
1713aa1993deSMatthew G Knepley   Not Collective
1714aa1993deSMatthew G Knepley 
1715aa1993deSMatthew G Knepley   Input Parameters:
1716bb7acecfSBarry Smith + dm    - the `DM` object
1717a5b23f4aSJose E. Roman . count - The minimum size
171820f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1719aa1993deSMatthew G Knepley 
1720aa1993deSMatthew G Knepley   Output Parameter:
172160225df5SJacob Faibussowitsch . mem - the work array
1722aa1993deSMatthew G Knepley 
1723aa1993deSMatthew G Knepley   Level: developer
1724aa1993deSMatthew G Knepley 
172573ff1848SBarry Smith   Developer Note:
1726bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1727147403d9SBarry Smith 
17281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1729aa1993deSMatthew G Knepley @*/
1730d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1731d71ae5a4SJacob Faibussowitsch {
1732aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1733aa1993deSMatthew G Knepley 
1734aa1993deSMatthew G Knepley   PetscFunctionBegin;
1735aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17364f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1737a4e35b19SJacob Faibussowitsch   (void)count;
1738a4e35b19SJacob Faibussowitsch   (void)dtype;
1739442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1740aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1741aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1742aa1993deSMatthew G Knepley       *p            = link->next;
1743aa1993deSMatthew G Knepley       link->next    = dm->workin;
1744aa1993deSMatthew G Knepley       dm->workin    = link;
17450298fd71SBarry Smith       *(void **)mem = NULL;
17463ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1747aa1993deSMatthew G Knepley     }
1748aa1993deSMatthew G Knepley   }
1749aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1750aa1993deSMatthew G Knepley }
1751e7c4fc90SDmitry Karpeev 
17528cda7954SMatthew G. Knepley /*@C
1753bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1754bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17558cda7954SMatthew G. Knepley 
175620f4b53cSBarry Smith   Logically Collective; No Fortran Support
17578cda7954SMatthew G. Knepley 
17588cda7954SMatthew G. Knepley   Input Parameters:
1759bb7acecfSBarry Smith + dm     - The `DM`
17608cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17618cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17628cda7954SMatthew G. Knepley 
176320f4b53cSBarry Smith   Calling sequence of `nullsp`:
1764bb7acecfSBarry Smith + dm        - The present `DM`
1765bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1766147403d9SBarry Smith . field     - The field number in dm
1767147403d9SBarry Smith - nullSpace - The nullspace for the given field
17688cda7954SMatthew G. Knepley 
176949762cbcSSatish Balay   Level: intermediate
177049762cbcSSatish Balay 
17711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1772147403d9SBarry Smith @*/
1773a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1774d71ae5a4SJacob Faibussowitsch {
1775435a35e8SMatthew G Knepley   PetscFunctionBegin;
1776435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17777a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1778435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1780435a35e8SMatthew G Knepley }
1781435a35e8SMatthew G Knepley 
17828cda7954SMatthew G. Knepley /*@C
1783bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17848cda7954SMatthew G. Knepley 
178520f4b53cSBarry Smith   Not Collective; No Fortran Support
17868cda7954SMatthew G. Knepley 
17878cda7954SMatthew G. Knepley   Input Parameters:
1788bb7acecfSBarry Smith + dm    - The `DM`
17898cda7954SMatthew G. Knepley - field - The field number for the nullspace
17908cda7954SMatthew G. Knepley 
17918cda7954SMatthew G. Knepley   Output Parameter:
17928cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17938cda7954SMatthew G. Knepley 
179420f4b53cSBarry Smith   Calling sequence of `nullsp`:
1795147403d9SBarry Smith + dm        - The present DM
1796147403d9SBarry Smith . origField - The field number given above, in the original DM
1797147403d9SBarry Smith . field     - The field number in dm
1798147403d9SBarry Smith - nullSpace - The nullspace for the given field
17998cda7954SMatthew G. Knepley 
180049762cbcSSatish Balay   Level: intermediate
180149762cbcSSatish Balay 
18021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1803147403d9SBarry Smith @*/
1804a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1805d71ae5a4SJacob Faibussowitsch {
18060a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18070a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18084f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18097a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18100a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18120a50eb56SMatthew G. Knepley }
18130a50eb56SMatthew G. Knepley 
18148cda7954SMatthew G. Knepley /*@C
1815bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18168cda7954SMatthew G. Knepley 
181720f4b53cSBarry Smith   Logically Collective; No Fortran Support
18188cda7954SMatthew G. Knepley 
18198cda7954SMatthew G. Knepley   Input Parameters:
1820bb7acecfSBarry Smith + dm     - The `DM`
18218cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18228cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18238cda7954SMatthew G. Knepley 
182420f4b53cSBarry Smith   Calling sequence of `nullsp`:
1825bb7acecfSBarry Smith + dm        - The present `DM`
1826bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1827147403d9SBarry Smith . field     - The field number in dm
1828147403d9SBarry Smith - nullSpace - The nullspace for the given field
18298cda7954SMatthew G. Knepley 
183049762cbcSSatish Balay   Level: intermediate
183149762cbcSSatish Balay 
18321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1833bb7acecfSBarry Smith           `MatNullSpace`
1834147403d9SBarry Smith @*/
1835a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1836d71ae5a4SJacob Faibussowitsch {
1837f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1838f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18397a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1840f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1842f9d4088aSMatthew G. Knepley }
1843f9d4088aSMatthew G. Knepley 
18448cda7954SMatthew G. Knepley /*@C
1845bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18468cda7954SMatthew G. Knepley 
184720f4b53cSBarry Smith   Not Collective; No Fortran Support
18488cda7954SMatthew G. Knepley 
18498cda7954SMatthew G. Knepley   Input Parameters:
1850bb7acecfSBarry Smith + dm    - The `DM`
18518cda7954SMatthew G. Knepley - field - The field number for the nullspace
18528cda7954SMatthew G. Knepley 
18538cda7954SMatthew G. Knepley   Output Parameter:
18548cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18558cda7954SMatthew G. Knepley 
185620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1857bb7acecfSBarry Smith + dm        - The present `DM`
1858bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1859147403d9SBarry Smith . field     - The field number in dm
1860147403d9SBarry Smith - nullSpace - The nullspace for the given field
18618cda7954SMatthew G. Knepley 
186249762cbcSSatish Balay   Level: intermediate
186349762cbcSSatish Balay 
18641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1865bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1866147403d9SBarry Smith @*/
1867a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1868d71ae5a4SJacob Faibussowitsch {
1869f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1870f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18714f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18727a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1873f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1875f9d4088aSMatthew G. Knepley }
1876f9d4088aSMatthew G. Knepley 
18774f3b5142SJed Brown /*@C
1878bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18794d343eeaSMatthew G Knepley 
188020f4b53cSBarry Smith   Not Collective; No Fortran Support
18814d343eeaSMatthew G Knepley 
18824d343eeaSMatthew G Knepley   Input Parameter:
1883bb7acecfSBarry Smith . dm - the `DM` object
18844d343eeaSMatthew G Knepley 
18854d343eeaSMatthew G Knepley   Output Parameters:
188620f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
188720f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
188820f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18894d343eeaSMatthew G Knepley 
18904d343eeaSMatthew G Knepley   Level: intermediate
18914d343eeaSMatthew G Knepley 
1892bb7acecfSBarry Smith   Note:
189373ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
189473ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1895bb7acecfSBarry Smith   `PetscFree()`.
189621c9b008SJed Brown 
189773ff1848SBarry Smith   Developer Note:
1898bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1899bb7acecfSBarry Smith   likely be removed.
1900bb7acecfSBarry Smith 
19011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1902bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19034d343eeaSMatthew G Knepley @*/
1904d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1905d71ae5a4SJacob Faibussowitsch {
190637d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19074d343eeaSMatthew G Knepley 
19084d343eeaSMatthew G Knepley   PetscFunctionBegin;
19094d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
191069ca1f37SDmitry Karpeev   if (numFields) {
19114f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
191269ca1f37SDmitry Karpeev     *numFields = 0;
191369ca1f37SDmitry Karpeev   }
191437d0c07bSMatthew G Knepley   if (fieldNames) {
19154f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19160298fd71SBarry Smith     *fieldNames = NULL;
191769ca1f37SDmitry Karpeev   }
191869ca1f37SDmitry Karpeev   if (fields) {
19194f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19200298fd71SBarry Smith     *fields = NULL;
192169ca1f37SDmitry Karpeev   }
19229566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
192337d0c07bSMatthew G Knepley   if (section) {
19243a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
192537d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192637d0c07bSMatthew G Knepley 
19279566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19289566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19299566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19309566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
193137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
193237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19339566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
193437d0c07bSMatthew G Knepley     }
193537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193637d0c07bSMatthew G Knepley       PetscInt gdof;
193737d0c07bSMatthew G Knepley 
19389566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193937d0c07bSMatthew G Knepley       if (gdof > 0) {
194037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19413a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
194237d0c07bSMatthew G Knepley 
19439566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19449566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19453a544194SStefano Zampini           fpdof = fdof - fcdof;
19463a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19473a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19483a544194SStefano Zampini             fieldNc[f] = 1;
19493a544194SStefano Zampini           }
19503a544194SStefano Zampini           fieldSizes[f] += fpdof;
195137d0c07bSMatthew G Knepley         }
195237d0c07bSMatthew G Knepley       }
195337d0c07bSMatthew G Knepley     }
195437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195737d0c07bSMatthew G Knepley     }
195837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
196037d0c07bSMatthew G Knepley 
19619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
196237d0c07bSMatthew G Knepley       if (gdof > 0) {
19639566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
196437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
196537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196637d0c07bSMatthew G Knepley 
19679566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19689566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1969ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
197037d0c07bSMatthew G Knepley         }
197137d0c07bSMatthew G Knepley       }
197237d0c07bSMatthew G Knepley     }
19738865f1eaSKarl Rupp     if (numFields) *numFields = nF;
197437d0c07bSMatthew G Knepley     if (fieldNames) {
19759566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197737d0c07bSMatthew G Knepley         const char *fieldName;
197837d0c07bSMatthew G Knepley 
19799566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19809566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
198137d0c07bSMatthew G Knepley       }
198237d0c07bSMatthew G Knepley     }
198337d0c07bSMatthew G Knepley     if (fields) {
19849566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
198537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19863a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19873a544194SStefano Zampini 
19889566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19893a544194SStefano Zampini         in[0] = -fieldNc[f];
19903a544194SStefano Zampini         in[1] = fieldNc[f];
19911c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19923a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19939566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
199437d0c07bSMatthew G Knepley       }
199537d0c07bSMatthew G Knepley     }
19969566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1997dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19994d343eeaSMatthew G Knepley }
20004d343eeaSMatthew G Knepley 
200116621825SDmitry Karpeev /*@C
2002bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2003a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2004e7c4fc90SDmitry Karpeev 
200520f4b53cSBarry Smith   Not Collective; No Fortran Support
2006e7c4fc90SDmitry Karpeev 
2007e7c4fc90SDmitry Karpeev   Input Parameter:
2008bb7acecfSBarry Smith . dm - the `DM` object
2009e7c4fc90SDmitry Karpeev 
2010e7c4fc90SDmitry Karpeev   Output Parameters:
201120f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
201220f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
201320f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
201420f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2015e7c4fc90SDmitry Karpeev 
2016e7c4fc90SDmitry Karpeev   Level: intermediate
2017e7c4fc90SDmitry Karpeev 
2018a4e35b19SJacob Faibussowitsch   Notes:
2019a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2020a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2021a4e35b19SJacob Faibussowitsch 
2022a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2023a4e35b19SJacob Faibussowitsch 
202473ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
202573ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2026bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2027e7c4fc90SDmitry Karpeev 
202860225df5SJacob Faibussowitsch   Developer Notes:
2029bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2030bb7acecfSBarry Smith 
203173ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
203273ff1848SBarry Smith   decomposition is computed.
203373ff1848SBarry Smith 
203473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2035e7c4fc90SDmitry Karpeev @*/
2036d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2037d71ae5a4SJacob Faibussowitsch {
2038e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2039e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20408865f1eaSKarl Rupp   if (len) {
20414f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20428865f1eaSKarl Rupp     *len = 0;
20438865f1eaSKarl Rupp   }
20448865f1eaSKarl Rupp   if (namelist) {
20454f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2046ea78f98cSLisandro Dalcin     *namelist = NULL;
20478865f1eaSKarl Rupp   }
20488865f1eaSKarl Rupp   if (islist) {
20494f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2050ea78f98cSLisandro Dalcin     *islist = NULL;
20518865f1eaSKarl Rupp   }
20528865f1eaSKarl Rupp   if (dmlist) {
20534f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2054ea78f98cSLisandro Dalcin     *dmlist = NULL;
20558865f1eaSKarl Rupp   }
2056f3f0edfdSDmitry Karpeev   /*
2057f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2058f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2059f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2060f3f0edfdSDmitry Karpeev    */
20617a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
206216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2063435a35e8SMatthew G Knepley     PetscSection section;
2064435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2065435a35e8SMatthew G Knepley 
20669566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20679566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2068435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2069f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20709566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20719566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20729566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2073435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2074435a35e8SMatthew G Knepley         const char *fieldName;
2075435a35e8SMatthew G Knepley 
20769566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
207703dc3394SMatthew G. Knepley         if (namelist) {
20789566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20799566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2080435a35e8SMatthew G Knepley         }
208103dc3394SMatthew G. Knepley       }
2082435a35e8SMatthew G Knepley     } else {
20839566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2084e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20850298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2086e7c4fc90SDmitry Karpeev     }
2087dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208916621825SDmitry Karpeev }
209016621825SDmitry Karpeev 
20915d83a8b1SBarry Smith /*@
209220f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
209320f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2094435a35e8SMatthew G Knepley 
2095435a35e8SMatthew G Knepley   Not collective
2096435a35e8SMatthew G Knepley 
2097435a35e8SMatthew G Knepley   Input Parameters:
2098bb7acecfSBarry Smith + dm        - The `DM` object
2099bb7acecfSBarry Smith . numFields - The number of fields to select
21002adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2101435a35e8SMatthew G Knepley 
2102435a35e8SMatthew G Knepley   Output Parameters:
2103b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2104b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2105435a35e8SMatthew G Knepley 
210620f4b53cSBarry Smith   Level: intermediate
210720f4b53cSBarry Smith 
2108bb7acecfSBarry Smith   Note:
2109bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21105d3b26e6SMatthew G. Knepley 
211160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2112435a35e8SMatthew G Knepley @*/
2113d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2114d71ae5a4SJacob Faibussowitsch {
2115435a35e8SMatthew G Knepley   PetscFunctionBegin;
2116435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21174f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21184f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21194f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2120dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2122435a35e8SMatthew G Knepley }
2123435a35e8SMatthew G Knepley 
21242adcc780SMatthew G. Knepley /*@C
2125bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21262adcc780SMatthew G. Knepley 
21272adcc780SMatthew G. Knepley   Not collective
21282adcc780SMatthew G. Knepley 
2129d8d19677SJose E. Roman   Input Parameters:
2130bb7acecfSBarry Smith + dms - The `DM` objects
2131bb7acecfSBarry Smith - n   - The number of `DM`s
21322adcc780SMatthew G. Knepley 
21332adcc780SMatthew G. Knepley   Output Parameters:
2134bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2135bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21362adcc780SMatthew G. Knepley 
213720f4b53cSBarry Smith   Level: intermediate
213820f4b53cSBarry Smith 
2139bb7acecfSBarry Smith   Note:
2140bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21415d3b26e6SMatthew G. Knepley 
214273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21432adcc780SMatthew G. Knepley @*/
21445d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2145d71ae5a4SJacob Faibussowitsch {
21462adcc780SMatthew G. Knepley   PetscInt i;
21472adcc780SMatthew G. Knepley 
21482adcc780SMatthew G. Knepley   PetscFunctionBegin;
21494f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2150ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21514f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21524f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2153bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2154bb7acecfSBarry Smith   if (n) {
2155b9d85ea2SLisandro Dalcin     DM dm = dms[0];
215600045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2157dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21582adcc780SMatthew G. Knepley   }
21593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21602adcc780SMatthew G. Knepley }
21612adcc780SMatthew G. Knepley 
216216621825SDmitry Karpeev /*@C
2163a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2164a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
216516621825SDmitry Karpeev 
216620f4b53cSBarry Smith   Not Collective
216716621825SDmitry Karpeev 
216816621825SDmitry Karpeev   Input Parameter:
2169bb7acecfSBarry Smith . dm - the `DM` object
217016621825SDmitry Karpeev 
217116621825SDmitry Karpeev   Output Parameters:
217220f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
217320f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
217473ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
217573ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
217673ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
217716621825SDmitry Karpeev 
217816621825SDmitry Karpeev   Level: intermediate
217916621825SDmitry Karpeev 
218073ff1848SBarry Smith   Notes:
2181a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2182a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2183a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2184a4e35b19SJacob Faibussowitsch 
2185a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2186a4e35b19SJacob Faibussowitsch 
218773ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
218873ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2189bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
219016621825SDmitry Karpeev 
2191a4e35b19SJacob Faibussowitsch   Developer Notes:
219220f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2193bb7acecfSBarry Smith 
219473ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
219573ff1848SBarry Smith 
219673ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
219773ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
219816621825SDmitry Karpeev @*/
2199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2200d71ae5a4SJacob Faibussowitsch {
2201be081cd6SPeter Brune   DMSubDomainHookLink link;
2202be081cd6SPeter Brune   PetscInt            i, l;
220316621825SDmitry Karpeev 
220416621825SDmitry Karpeev   PetscFunctionBegin;
220516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22069371c9d4SSatish Balay   if (n) {
22074f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22089371c9d4SSatish Balay     *n = 0;
22099371c9d4SSatish Balay   }
22109371c9d4SSatish Balay   if (namelist) {
22114f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22129371c9d4SSatish Balay     *namelist = NULL;
22139371c9d4SSatish Balay   }
22149371c9d4SSatish Balay   if (innerislist) {
22154f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22169371c9d4SSatish Balay     *innerislist = NULL;
22179371c9d4SSatish Balay   }
22189371c9d4SSatish Balay   if (outerislist) {
22194f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22209371c9d4SSatish Balay     *outerislist = NULL;
22219371c9d4SSatish Balay   }
22229371c9d4SSatish Balay   if (dmlist) {
22234f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22249371c9d4SSatish Balay     *dmlist = NULL;
22259371c9d4SSatish Balay   }
2226f3f0edfdSDmitry Karpeev   /*
2227f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2228f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2229f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2230f3f0edfdSDmitry Karpeev    */
22317a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
223216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2233dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
223414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2235f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2236be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2237be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22389566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2239be081cd6SPeter Brune         }
2240648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2241e7c4fc90SDmitry Karpeev       }
224214a18fd3SPeter Brune     }
2243bb7acecfSBarry Smith     if (n) *n = l;
224414a18fd3SPeter Brune   }
22453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2246e30e807fSPeter Brune }
2247e30e807fSPeter Brune 
2248e30e807fSPeter Brune /*@C
224973ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
225073ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2251e30e807fSPeter Brune 
225220f4b53cSBarry Smith   Not Collective
2253e30e807fSPeter Brune 
2254e30e807fSPeter Brune   Input Parameters:
2255bb7acecfSBarry Smith + dm     - the `DM` object
225673ff1848SBarry Smith . n      - the number of subdomains
2257e30e807fSPeter Brune - subdms - the local subdomains
2258e30e807fSPeter Brune 
2259e30e807fSPeter Brune   Output Parameters:
22606b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2261e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2262e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2263e30e807fSPeter Brune 
226420f4b53cSBarry Smith   Level: developer
226520f4b53cSBarry Smith 
2266bb7acecfSBarry Smith   Note:
2267bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2268e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2269e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2270e30e807fSPeter Brune   solution and residual data.
2271e30e807fSPeter Brune 
227273ff1848SBarry Smith   Developer Note:
2273a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2274a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2275bb7acecfSBarry Smith 
22761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2277e30e807fSPeter Brune @*/
22785d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2279d71ae5a4SJacob Faibussowitsch {
2280e30e807fSPeter Brune   PetscFunctionBegin;
2281e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22824f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2283dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2285e7c4fc90SDmitry Karpeev }
2286e7c4fc90SDmitry Karpeev 
228747c6ae99SBarry Smith /*@
2288bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
228947c6ae99SBarry Smith 
229020f4b53cSBarry Smith   Collective
229147c6ae99SBarry Smith 
2292d8d19677SJose E. Roman   Input Parameters:
2293bb7acecfSBarry Smith + dm   - the `DM` object
2294bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
229547c6ae99SBarry Smith 
229647c6ae99SBarry Smith   Output Parameter:
229720f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2298ae0a1c52SMatthew G Knepley 
229920f4b53cSBarry Smith   Options Database Key:
2300412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2301412e9a14SMatthew G. Knepley 
230247c6ae99SBarry Smith   Level: developer
230347c6ae99SBarry Smith 
230420f4b53cSBarry Smith   Note:
230520f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
230620f4b53cSBarry Smith 
230773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
230873ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
230947c6ae99SBarry Smith @*/
2310d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2311d71ae5a4SJacob Faibussowitsch {
2312c833c3b5SJed Brown   DMRefineHookLink link;
231347c6ae99SBarry Smith 
231447c6ae99SBarry Smith   PetscFunctionBegin;
2315732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2317dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23184057135bSMatthew G Knepley   if (*dmf) {
231943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23208865f1eaSKarl Rupp 
23219566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23228865f1eaSKarl Rupp 
2323644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23240598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2325656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23268865f1eaSKarl Rupp 
23279566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2328c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23291baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2330c833c3b5SJed Brown     }
2331c833c3b5SJed Brown   }
23329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2334c833c3b5SJed Brown }
2335c833c3b5SJed Brown 
2336bb9467b5SJed Brown /*@C
2337c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2338c833c3b5SJed Brown 
233920f4b53cSBarry Smith   Logically Collective; No Fortran Support
2340c833c3b5SJed Brown 
23414165533cSJose E. Roman   Input Parameters:
2342bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2343bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2344f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
234520f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2346c833c3b5SJed Brown 
234720f4b53cSBarry Smith   Calling sequence of `refinehook`:
2348bb7acecfSBarry Smith + coarse - coarse level `DM`
2349bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2350c833c3b5SJed Brown - ctx    - optional user-defined function context
2351c833c3b5SJed Brown 
235220f4b53cSBarry Smith   Calling sequence of `interphook`:
2353bb7acecfSBarry Smith + coarse - coarse level `DM`
2354c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2355bb7acecfSBarry Smith . fine   - fine level `DM` to update
2356c833c3b5SJed Brown - ctx    - optional user-defined function context
2357c833c3b5SJed Brown 
2358c833c3b5SJed Brown   Level: advanced
2359c833c3b5SJed Brown 
2360c833c3b5SJed Brown   Notes:
2361bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2362bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2363bb7acecfSBarry Smith 
2364bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2365c833c3b5SJed Brown 
2366c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2367c833c3b5SJed Brown 
23681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2369c833c3b5SJed Brown @*/
2370a4e35b19SJacob 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)
2371d71ae5a4SJacob Faibussowitsch {
2372c833c3b5SJed Brown   DMRefineHookLink link, *p;
2373c833c3b5SJed Brown 
2374c833c3b5SJed Brown   PetscFunctionBegin;
2375c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23763d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23773ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23783d8e3701SJed Brown   }
23799566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2380c833c3b5SJed Brown   link->refinehook = refinehook;
2381c833c3b5SJed Brown   link->interphook = interphook;
2382c833c3b5SJed Brown   link->ctx        = ctx;
23830298fd71SBarry Smith   link->next       = NULL;
2384c833c3b5SJed Brown   *p               = link;
23853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2386c833c3b5SJed Brown }
2387c833c3b5SJed Brown 
23883d8e3701SJed Brown /*@C
2389bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2390bb7acecfSBarry Smith   a nonlinear problem to a finer grid
23913d8e3701SJed Brown 
239220f4b53cSBarry Smith   Logically Collective; No Fortran Support
23933d8e3701SJed Brown 
23944165533cSJose E. Roman   Input Parameters:
2395bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2396bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2397bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
239820f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23993d8e3701SJed Brown 
24003d8e3701SJed Brown   Level: advanced
24013d8e3701SJed Brown 
2402bb7acecfSBarry Smith   Note:
24033d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24043d8e3701SJed Brown 
24051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24063d8e3701SJed Brown @*/
2407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2408d71ae5a4SJacob Faibussowitsch {
24093d8e3701SJed Brown   DMRefineHookLink link, *p;
24103d8e3701SJed Brown 
24113d8e3701SJed Brown   PetscFunctionBegin;
24123d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24133d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24143d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24153d8e3701SJed Brown       link = *p;
24163d8e3701SJed Brown       *p   = link->next;
24179566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24183d8e3701SJed Brown       break;
24193d8e3701SJed Brown     }
24203d8e3701SJed Brown   }
24213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24223d8e3701SJed Brown }
24233d8e3701SJed Brown 
2424c833c3b5SJed Brown /*@
2425bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2426c833c3b5SJed Brown 
2427c833c3b5SJed Brown   Collective if any hooks are
2428c833c3b5SJed Brown 
24294165533cSJose E. Roman   Input Parameters:
2430bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2431bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2432bb7acecfSBarry Smith - fine   - finer `DM` to update
2433c833c3b5SJed Brown 
2434c833c3b5SJed Brown   Level: developer
2435c833c3b5SJed Brown 
243673ff1848SBarry Smith   Developer Note:
2437bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2438bb7acecfSBarry Smith   an API with consistent terminology.
2439bb7acecfSBarry Smith 
24401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2441c833c3b5SJed Brown @*/
2442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2443d71ae5a4SJacob Faibussowitsch {
2444c833c3b5SJed Brown   DMRefineHookLink link;
2445c833c3b5SJed Brown 
2446c833c3b5SJed Brown   PetscFunctionBegin;
2447c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24481baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24494057135bSMatthew G Knepley   }
24503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
245147c6ae99SBarry Smith }
245247c6ae99SBarry Smith 
2453eb3f98d2SBarry Smith /*@
24541f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24551f3379b2SToby Isaac 
245620f4b53cSBarry Smith   Collective
24571f3379b2SToby Isaac 
24584165533cSJose E. Roman   Input Parameters:
2459bb7acecfSBarry Smith + coarse    - coarse `DM`
2460bb7acecfSBarry Smith . fine      - fine `DM`
2461bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2462bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2463bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24641f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24651f3379b2SToby Isaac 
24664165533cSJose E. Roman   Output Parameter:
24671f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24681f3379b2SToby Isaac 
24691f3379b2SToby Isaac   Level: developer
24701f3379b2SToby Isaac 
2471bb7acecfSBarry Smith   Note:
2472bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24731f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24741f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24751f3379b2SToby Isaac   slope-limiting reconstruction.
24761f3379b2SToby Isaac 
247773ff1848SBarry Smith   Developer Note:
2478bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2479bb7acecfSBarry Smith 
24801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24811f3379b2SToby Isaac @*/
2482d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2483d71ae5a4SJacob Faibussowitsch {
24841f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24851f3379b2SToby Isaac 
24861f3379b2SToby Isaac   PetscFunctionBegin;
24871f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24881f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24891f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24901f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24911f3379b2SToby Isaac 
24929566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24931f3379b2SToby Isaac   if (interpsol) {
24949566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24951f3379b2SToby Isaac   } else if (interp) {
24969566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
249798921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24991f3379b2SToby Isaac }
25001f3379b2SToby Isaac 
25011f3379b2SToby Isaac /*@
2502bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2503eb3f98d2SBarry Smith 
2504eb3f98d2SBarry Smith   Not Collective
2505eb3f98d2SBarry Smith 
2506eb3f98d2SBarry Smith   Input Parameter:
2507bb7acecfSBarry Smith . dm - the `DM` object
2508eb3f98d2SBarry Smith 
2509eb3f98d2SBarry Smith   Output Parameter:
2510eb3f98d2SBarry Smith . level - number of refinements
2511eb3f98d2SBarry Smith 
2512eb3f98d2SBarry Smith   Level: developer
2513eb3f98d2SBarry Smith 
2514bb7acecfSBarry Smith   Note:
2515bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2516bb7acecfSBarry Smith 
25171cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2518eb3f98d2SBarry Smith @*/
2519d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2520d71ae5a4SJacob Faibussowitsch {
2521eb3f98d2SBarry Smith   PetscFunctionBegin;
2522eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2523eb3f98d2SBarry Smith   *level = dm->levelup;
25243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2525eb3f98d2SBarry Smith }
2526eb3f98d2SBarry Smith 
2527fef3a512SBarry Smith /*@
2528bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2529fef3a512SBarry Smith 
2530fef3a512SBarry Smith   Not Collective
2531fef3a512SBarry Smith 
2532d8d19677SJose E. Roman   Input Parameters:
2533bb7acecfSBarry Smith + dm    - the `DM` object
2534fef3a512SBarry Smith - level - number of refinements
2535fef3a512SBarry Smith 
2536fef3a512SBarry Smith   Level: advanced
2537fef3a512SBarry Smith 
253895452b02SPatrick Sanan   Notes:
2539bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2540fef3a512SBarry Smith 
2541bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2542bb7acecfSBarry Smith 
25431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2544fef3a512SBarry Smith @*/
2545d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2546d71ae5a4SJacob Faibussowitsch {
2547fef3a512SBarry Smith   PetscFunctionBegin;
2548fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2549fef3a512SBarry Smith   dm->levelup = level;
25503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2551fef3a512SBarry Smith }
2552fef3a512SBarry Smith 
2553d410b0cfSMatthew G. Knepley /*@
2554bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2555d410b0cfSMatthew G. Knepley 
255620f4b53cSBarry Smith   Collective
2557d410b0cfSMatthew G. Knepley 
2558f1a722f8SMatthew G. Knepley   Input Parameters:
2559bb7acecfSBarry Smith + dm     - the `DM` object
2560d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2561d410b0cfSMatthew G. Knepley 
2562d410b0cfSMatthew G. Knepley   Output Parameter:
256320f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2564d410b0cfSMatthew G. Knepley 
2565d410b0cfSMatthew G. Knepley   Level: developer
2566d410b0cfSMatthew G. Knepley 
256720f4b53cSBarry Smith   Note:
256820f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
256920f4b53cSBarry Smith 
25701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2571d410b0cfSMatthew G. Knepley @*/
2572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2573d71ae5a4SJacob Faibussowitsch {
2574d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2575d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2576dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2577d410b0cfSMatthew G. Knepley   if (*dme) {
2578d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25799566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2580d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25819566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2582d410b0cfSMatthew G. Knepley   }
25833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2584d410b0cfSMatthew G. Knepley }
2585d410b0cfSMatthew G. Knepley 
2586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2587d71ae5a4SJacob Faibussowitsch {
2588ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2589ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25904f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2591ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2593ca3d3a14SMatthew G. Knepley }
2594ca3d3a14SMatthew G. Knepley 
2595d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2596d71ae5a4SJacob Faibussowitsch {
2597ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2598ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25994f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2600ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2602ca3d3a14SMatthew G. Knepley }
2603ca3d3a14SMatthew G. Knepley 
2604ca3d3a14SMatthew G. Knepley /*@
2605bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2606ca3d3a14SMatthew G. Knepley 
2607ca3d3a14SMatthew G. Knepley   Input Parameter:
260820f4b53cSBarry Smith . dm - The `DM`
2609ca3d3a14SMatthew G. Knepley 
2610ca3d3a14SMatthew G. Knepley   Output Parameter:
261120f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2612ca3d3a14SMatthew G. Knepley 
2613ca3d3a14SMatthew G. Knepley   Level: developer
2614ca3d3a14SMatthew G. Knepley 
26151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2616ca3d3a14SMatthew G. Knepley @*/
2617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2618d71ae5a4SJacob Faibussowitsch {
2619ca3d3a14SMatthew G. Knepley   Vec tv;
2620ca3d3a14SMatthew G. Knepley 
2621ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2622ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26234f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26249566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2625ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2627ca3d3a14SMatthew G. Knepley }
2628ca3d3a14SMatthew G. Knepley 
2629d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2630d71ae5a4SJacob Faibussowitsch {
2631ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2632ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2633ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2634ca3d3a14SMatthew G. Knepley 
2635ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26379566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26389566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26399566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26409566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26419566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2644ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26459566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2646ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2647ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2648ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2650ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26519566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26529566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2653ca3d3a14SMatthew G. Knepley     }
2654ca3d3a14SMatthew G. Knepley   }
26559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26569566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26579566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2658ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2659ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26609566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2661ca3d3a14SMatthew G. Knepley       if (dof) {
2662ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2663ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2664ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2665ca3d3a14SMatthew G. Knepley 
2666ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26679566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26689566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26699566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2670ca3d3a14SMatthew G. Knepley       }
2671ca3d3a14SMatthew G. Knepley     }
2672ca3d3a14SMatthew G. Knepley   }
26739566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2675ca3d3a14SMatthew G. Knepley }
2676ca3d3a14SMatthew G. Knepley 
2677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2678d71ae5a4SJacob Faibussowitsch {
2679ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2680ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2681ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2682ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2683ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2684ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2685ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26869566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2688ca3d3a14SMatthew G. Knepley }
2689ca3d3a14SMatthew G. Knepley 
2690bb9467b5SJed Brown /*@C
2691bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2692baf369e7SPeter Brune 
269320f4b53cSBarry Smith   Logically Collective
2694baf369e7SPeter Brune 
26954165533cSJose E. Roman   Input Parameters:
2696bb7acecfSBarry Smith + dm        - the `DM`
2697bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2698bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
269920f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2700baf369e7SPeter Brune 
270120f4b53cSBarry Smith   Calling sequence of `beginhook`:
2702a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2703baf369e7SPeter Brune . g    - global vector
2704baf369e7SPeter Brune . mode - mode
2705baf369e7SPeter Brune . l    - local vector
2706baf369e7SPeter Brune - ctx  - optional user-defined function context
2707baf369e7SPeter Brune 
270820f4b53cSBarry Smith   Calling sequence of `endhook`:
2709a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2710a4e35b19SJacob Faibussowitsch . g    - global vector
2711a4e35b19SJacob Faibussowitsch . mode - mode
2712a4e35b19SJacob Faibussowitsch . l    - local vector
2713baf369e7SPeter Brune - ctx  - optional user-defined function context
2714baf369e7SPeter Brune 
2715baf369e7SPeter Brune   Level: advanced
2716baf369e7SPeter Brune 
2717bb7acecfSBarry Smith   Note:
2718bb7acecfSBarry 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.
2719bb7acecfSBarry Smith 
27201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2721baf369e7SPeter Brune @*/
2722a4e35b19SJacob 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)
2723d71ae5a4SJacob Faibussowitsch {
2724baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2725baf369e7SPeter Brune 
2726baf369e7SPeter Brune   PetscFunctionBegin;
2727baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2728baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27299566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2730baf369e7SPeter Brune   link->beginhook = beginhook;
2731baf369e7SPeter Brune   link->endhook   = endhook;
2732baf369e7SPeter Brune   link->ctx       = ctx;
27330298fd71SBarry Smith   link->next      = NULL;
2734baf369e7SPeter Brune   *p              = link;
27353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2736baf369e7SPeter Brune }
2737baf369e7SPeter Brune 
2738d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2739d71ae5a4SJacob Faibussowitsch {
27404c274da1SToby Isaac   Mat          cMat;
274179769bd5SJed Brown   Vec          cVec, cBias;
27424c274da1SToby Isaac   PetscSection section, cSec;
27434c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27444c274da1SToby Isaac 
27454c274da1SToby Isaac   PetscFunctionBegin;
2746a4e35b19SJacob Faibussowitsch   (void)g;
2747a4e35b19SJacob Faibussowitsch   (void)ctx;
27484c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27499566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27504c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27515db9a05bSToby Isaac     PetscInt nRows;
27525db9a05bSToby Isaac 
27539566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27543ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27559566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27569566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27579566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27589566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27599566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27604c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27624c274da1SToby Isaac       if (dof) {
27634c274da1SToby Isaac         PetscScalar *vals;
27649566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27659566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27664c274da1SToby Isaac       }
27674c274da1SToby Isaac     }
27689566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27694c274da1SToby Isaac   }
27703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27714c274da1SToby Isaac }
27724c274da1SToby Isaac 
277347c6ae99SBarry Smith /*@
277401729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
277501729b5cSPatrick Sanan 
277620f4b53cSBarry Smith   Neighbor-wise Collective
277701729b5cSPatrick Sanan 
277801729b5cSPatrick Sanan   Input Parameters:
2779bb7acecfSBarry Smith + dm   - the `DM` object
278001729b5cSPatrick Sanan . g    - the global vector
2781bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
278201729b5cSPatrick Sanan - l    - the local vector
278301729b5cSPatrick Sanan 
278420f4b53cSBarry Smith   Level: beginner
278520f4b53cSBarry Smith 
278601729b5cSPatrick Sanan   Notes:
2787bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2788bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2789bb7acecfSBarry Smith 
2790bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
279101729b5cSPatrick Sanan 
27921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
279360225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2794bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
279501729b5cSPatrick Sanan @*/
2796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2797d71ae5a4SJacob Faibussowitsch {
279801729b5cSPatrick Sanan   PetscFunctionBegin;
27999566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28009566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
280201729b5cSPatrick Sanan }
280301729b5cSPatrick Sanan 
280401729b5cSPatrick Sanan /*@
280547c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
280647c6ae99SBarry Smith 
280720f4b53cSBarry Smith   Neighbor-wise Collective
280847c6ae99SBarry Smith 
280947c6ae99SBarry Smith   Input Parameters:
2810bb7acecfSBarry Smith + dm   - the `DM` object
281147c6ae99SBarry Smith . g    - the global vector
2812bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
281347c6ae99SBarry Smith - l    - the local vector
281447c6ae99SBarry Smith 
281501729b5cSPatrick Sanan   Level: intermediate
281647c6ae99SBarry Smith 
2817bb7acecfSBarry Smith   Notes:
2818bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2819bb7acecfSBarry Smith 
2820bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2821bb7acecfSBarry Smith 
2822bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2823bb7acecfSBarry Smith 
2824bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2825bb7acecfSBarry Smith 
282660225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
282747c6ae99SBarry Smith @*/
2828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2829d71ae5a4SJacob Faibussowitsch {
28307128ae9fSMatthew G Knepley   PetscSF                 sf;
2831baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
283247c6ae99SBarry Smith 
283347c6ae99SBarry Smith   PetscFunctionBegin;
2834171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2835baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28361baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2837baf369e7SPeter Brune   }
28389566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28397128ae9fSMatthew G Knepley   if (sf) {
2840ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2841ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2842d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28437128ae9fSMatthew G Knepley 
28447a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28459566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28469566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28479566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28489566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28499566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28507128ae9fSMatthew G Knepley   } else {
2851fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28527128ae9fSMatthew G Knepley   }
28533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
285447c6ae99SBarry Smith }
285547c6ae99SBarry Smith 
285647c6ae99SBarry Smith /*@
285747c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
285847c6ae99SBarry Smith 
285920f4b53cSBarry Smith   Neighbor-wise Collective
286047c6ae99SBarry Smith 
286147c6ae99SBarry Smith   Input Parameters:
2862bb7acecfSBarry Smith + dm   - the `DM` object
286347c6ae99SBarry Smith . g    - the global vector
2864bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
286547c6ae99SBarry Smith - l    - the local vector
286647c6ae99SBarry Smith 
286701729b5cSPatrick Sanan   Level: intermediate
286847c6ae99SBarry Smith 
2869bb7acecfSBarry Smith   Note:
2870bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2871bb7acecfSBarry Smith 
287260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
287347c6ae99SBarry Smith @*/
2874d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2875d71ae5a4SJacob Faibussowitsch {
28767128ae9fSMatthew G Knepley   PetscSF                 sf;
2877ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2878ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2879ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2880baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2881d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
288247c6ae99SBarry Smith 
288347c6ae99SBarry Smith   PetscFunctionBegin;
2884171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28859566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28869566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28877128ae9fSMatthew G Knepley   if (sf) {
28887a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28897128ae9fSMatthew G Knepley 
28909566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28919566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28929566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28949566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28959566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28967128ae9fSMatthew G Knepley   } else {
2897fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28987128ae9fSMatthew G Knepley   }
28999566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2900baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29019566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2902baf369e7SPeter Brune   }
29033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
290447c6ae99SBarry Smith }
290547c6ae99SBarry Smith 
2906d4d07f1eSToby Isaac /*@C
2907d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2908d4d07f1eSToby Isaac 
290920f4b53cSBarry Smith   Logically Collective
2910d4d07f1eSToby Isaac 
29114165533cSJose E. Roman   Input Parameters:
2912bb7acecfSBarry Smith + dm        - the `DM`
2913bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2914bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
291520f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2916d4d07f1eSToby Isaac 
291720f4b53cSBarry Smith   Calling sequence of `beginhook`:
2918a4e35b19SJacob Faibussowitsch + global - global `DM`
2919d4d07f1eSToby Isaac . l      - local vector
2920d4d07f1eSToby Isaac . mode   - mode
2921d4d07f1eSToby Isaac . g      - global vector
2922d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2923d4d07f1eSToby Isaac 
292420f4b53cSBarry Smith   Calling sequence of `endhook`:
2925bb7acecfSBarry Smith + global - global `DM`
2926d4d07f1eSToby Isaac . l      - local vector
2927d4d07f1eSToby Isaac . mode   - mode
2928d4d07f1eSToby Isaac . g      - global vector
2929d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2930d4d07f1eSToby Isaac 
2931d4d07f1eSToby Isaac   Level: advanced
2932d4d07f1eSToby Isaac 
29331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2934d4d07f1eSToby Isaac @*/
2935a4e35b19SJacob 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)
2936d71ae5a4SJacob Faibussowitsch {
2937d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2938d4d07f1eSToby Isaac 
2939d4d07f1eSToby Isaac   PetscFunctionBegin;
2940d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2941d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29429566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2943d4d07f1eSToby Isaac   link->beginhook = beginhook;
2944d4d07f1eSToby Isaac   link->endhook   = endhook;
2945d4d07f1eSToby Isaac   link->ctx       = ctx;
2946d4d07f1eSToby Isaac   link->next      = NULL;
2947d4d07f1eSToby Isaac   *p              = link;
29483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2949d4d07f1eSToby Isaac }
2950d4d07f1eSToby Isaac 
2951d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2952d71ae5a4SJacob Faibussowitsch {
29534c274da1SToby Isaac   PetscFunctionBegin;
2954a4e35b19SJacob Faibussowitsch   (void)g;
2955a4e35b19SJacob Faibussowitsch   (void)ctx;
29564c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29572b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29582b6b7d09SToby Isaac     Mat          cMat;
29592b6b7d09SToby Isaac     Vec          cVec;
29605db9a05bSToby Isaac     PetscInt     nRows;
29612b6b7d09SToby Isaac     PetscSection section, cSec;
29622b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29632b6b7d09SToby Isaac 
29642b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29652b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29665db9a05bSToby Isaac 
29679566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29683ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29699566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29709566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29724c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29744c274da1SToby Isaac       if (dof) {
29754c274da1SToby Isaac         PetscInt     d;
29764c274da1SToby Isaac         PetscScalar *vals;
29779566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29789566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29794c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29804c274da1SToby Isaac          * we just extracted */
2981ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29824c274da1SToby Isaac       }
29834c274da1SToby Isaac     }
29849566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29859566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29864c274da1SToby Isaac   }
29873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29884c274da1SToby Isaac }
298901729b5cSPatrick Sanan /*@
299001729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
299101729b5cSPatrick Sanan 
299220f4b53cSBarry Smith   Neighbor-wise Collective
299301729b5cSPatrick Sanan 
299401729b5cSPatrick Sanan   Input Parameters:
2995bb7acecfSBarry Smith + dm   - the `DM` object
299601729b5cSPatrick Sanan . l    - the local vector
2997bb7acecfSBarry 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.
299801729b5cSPatrick Sanan - g    - the global vector
299901729b5cSPatrick Sanan 
300020f4b53cSBarry Smith   Level: beginner
300120f4b53cSBarry Smith 
300201729b5cSPatrick Sanan   Notes:
300301729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3004bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
300501729b5cSPatrick Sanan 
3006bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3007bb7acecfSBarry Smith 
3008bb7acecfSBarry 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.
3009bb7acecfSBarry Smith 
3010bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
301101729b5cSPatrick Sanan 
30121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
301301729b5cSPatrick Sanan @*/
3014d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3015d71ae5a4SJacob Faibussowitsch {
301601729b5cSPatrick Sanan   PetscFunctionBegin;
30179566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30189566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
302001729b5cSPatrick Sanan }
30214c274da1SToby Isaac 
302247c6ae99SBarry Smith /*@
302301729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30249a42bb27SBarry Smith 
302520f4b53cSBarry Smith   Neighbor-wise Collective
30269a42bb27SBarry Smith 
30279a42bb27SBarry Smith   Input Parameters:
3028bb7acecfSBarry Smith + dm   - the `DM` object
3029f6813fd5SJed Brown . l    - the local vector
3030aa624791SPierre 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.
30311eb28f2eSBarry Smith - g    - the global vector
30329a42bb27SBarry Smith 
303320f4b53cSBarry Smith   Level: intermediate
303420f4b53cSBarry Smith 
303595452b02SPatrick Sanan   Notes:
3036bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3037bb7acecfSBarry Smith 
3038bb7acecfSBarry 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.
3039bb7acecfSBarry Smith 
3040bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3041bb7acecfSBarry Smith 
3042bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3043bb7acecfSBarry Smith 
3044bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30459a42bb27SBarry Smith 
30461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30479a42bb27SBarry Smith @*/
3048d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3049d71ae5a4SJacob Faibussowitsch {
30507128ae9fSMatthew G Knepley   PetscSF                 sf;
305184330215SMatthew G. Knepley   PetscSection            s, gs;
3052d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3053ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3054ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3055ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3056fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3057d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30589a42bb27SBarry Smith 
30599a42bb27SBarry Smith   PetscFunctionBegin;
3060171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3061d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30621baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3063d4d07f1eSToby Isaac   }
30649566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30659566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30669566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30677128ae9fSMatthew G Knepley   switch (mode) {
30687128ae9fSMatthew G Knepley   case INSERT_VALUES:
30697128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3070d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3071d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3072d71ae5a4SJacob Faibussowitsch     break;
30737128ae9fSMatthew G Knepley   case ADD_VALUES:
30747128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3075d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3076d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3077d71ae5a4SJacob Faibussowitsch     break;
3078d71ae5a4SJacob Faibussowitsch   default:
3079d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30807128ae9fSMatthew G Knepley   }
3081ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30829566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3083ca3d3a14SMatthew G. Knepley     if (transform) {
30849566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30859566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30869566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30879566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3088fa88e482SJed Brown     } else if (isInsert) {
30899566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3090fa88e482SJed Brown     } else {
30919566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3092fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3093ca3d3a14SMatthew G. Knepley     }
3094fa88e482SJed Brown     if (s && isInsert) {
30959566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3096fa88e482SJed Brown     } else {
30979566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3098fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3099fa88e482SJed Brown     }
3100ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31019566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
310284330215SMatthew G. Knepley     } else if (s && isInsert) {
310384330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
310484330215SMatthew G. Knepley 
31059566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31079566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
310884330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3109b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
311084330215SMatthew G. Knepley 
31119566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31129566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31139566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31149566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3117b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
311803442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31197a8be351SBarry 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);
3120b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3121b3b16f48SMatthew G. Knepley         if (!gcdof) {
312284330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3123b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3124b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
312584330215SMatthew G. Knepley           const PetscInt *cdofs;
312684330215SMatthew G. Knepley           PetscInt        cind = 0;
312784330215SMatthew G. Knepley 
31289566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3129b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31309371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31319371c9d4SSatish Balay               ++cind;
31329371c9d4SSatish Balay               continue;
31339371c9d4SSatish Balay             }
3134b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
313584330215SMatthew G. Knepley           }
31367a8be351SBarry 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);
313784330215SMatthew G. Knepley       }
3138ca3d3a14SMatthew G. Knepley     }
3139fa88e482SJed Brown     if (g_inplace) {
31409566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3141fa88e482SJed Brown     } else {
31429566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3143fa88e482SJed Brown     }
3144ca3d3a14SMatthew G. Knepley     if (transform) {
31459566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31469566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3147fa88e482SJed Brown     } else if (l_inplace) {
31489566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3149ca3d3a14SMatthew G. Knepley     } else {
31509566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3151ca3d3a14SMatthew G. Knepley     }
31527128ae9fSMatthew G Knepley   } else {
3153fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31547128ae9fSMatthew G Knepley   }
31553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31569a42bb27SBarry Smith }
31579a42bb27SBarry Smith 
31589a42bb27SBarry Smith /*@
31599a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
316047c6ae99SBarry Smith 
316120f4b53cSBarry Smith   Neighbor-wise Collective
316247c6ae99SBarry Smith 
316347c6ae99SBarry Smith   Input Parameters:
3164bb7acecfSBarry Smith + dm   - the `DM` object
3165f6813fd5SJed Brown . l    - the local vector
3166bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3167f6813fd5SJed Brown - g    - the global vector
316847c6ae99SBarry Smith 
316901729b5cSPatrick Sanan   Level: intermediate
317047c6ae99SBarry Smith 
3171bb7acecfSBarry Smith   Note:
3172bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3173bb7acecfSBarry Smith 
317460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
317547c6ae99SBarry Smith @*/
3176d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3177d71ae5a4SJacob Faibussowitsch {
31787128ae9fSMatthew G Knepley   PetscSF                 sf;
317984330215SMatthew G. Knepley   PetscSection            s;
3180d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3181ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
318247c6ae99SBarry Smith 
318347c6ae99SBarry Smith   PetscFunctionBegin;
3184171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31859566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31869566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31877128ae9fSMatthew G Knepley   switch (mode) {
31887128ae9fSMatthew G Knepley   case INSERT_VALUES:
3189d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3190d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3191d71ae5a4SJacob Faibussowitsch     break;
31927128ae9fSMatthew G Knepley   case ADD_VALUES:
3193d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3194d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3195d71ae5a4SJacob Faibussowitsch     break;
3196d71ae5a4SJacob Faibussowitsch   default:
3197d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31987128ae9fSMatthew G Knepley   }
319984330215SMatthew G. Knepley   if (sf && !isInsert) {
3200ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3201ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3202ca3d3a14SMatthew G. Knepley     Vec                tmpl;
320384330215SMatthew G. Knepley 
32049566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3205ca3d3a14SMatthew G. Knepley     if (transform) {
32069566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32079566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3208ca3d3a14SMatthew G. Knepley     } else {
32099566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3210ca3d3a14SMatthew G. Knepley     }
32119566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32129566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3213ca3d3a14SMatthew G. Knepley     if (transform) {
32149566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32159566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3216ca3d3a14SMatthew G. Knepley     } else {
32179566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3218ca3d3a14SMatthew G. Knepley     }
32199566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
322084330215SMatthew G. Knepley   } else if (s && isInsert) {
32217128ae9fSMatthew G Knepley   } else {
3222fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32237128ae9fSMatthew G Knepley   }
3224d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32259566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3226d4d07f1eSToby Isaac   }
32273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
322847c6ae99SBarry Smith }
322947c6ae99SBarry Smith 
3230f089877aSRichard Tran Mills /*@
3231a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3232a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3233a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3234f089877aSRichard Tran Mills 
323520f4b53cSBarry Smith   Neighbor-wise Collective
3236f089877aSRichard Tran Mills 
3237f089877aSRichard Tran Mills   Input Parameters:
3238bb7acecfSBarry Smith + dm   - the `DM` object
3239bc0a1609SRichard Tran Mills . g    - the original local vector
3240bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3241f089877aSRichard Tran Mills 
3242bc0a1609SRichard Tran Mills   Output Parameter:
3243bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3244f089877aSRichard Tran Mills 
3245f089877aSRichard Tran Mills   Level: intermediate
3246f089877aSRichard Tran Mills 
324773ff1848SBarry Smith   Note:
3248a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3249a4e35b19SJacob Faibussowitsch 
325060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3251f089877aSRichard Tran Mills @*/
3252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3253d71ae5a4SJacob Faibussowitsch {
3254f089877aSRichard Tran Mills   PetscFunctionBegin;
3255f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32569f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32579f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32589f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3260f089877aSRichard Tran Mills }
3261f089877aSRichard Tran Mills 
3262f089877aSRichard Tran Mills /*@
3263bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3264bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3265f089877aSRichard Tran Mills 
326620f4b53cSBarry Smith   Neighbor-wise Collective
3267f089877aSRichard Tran Mills 
3268f089877aSRichard Tran Mills   Input Parameters:
326960225df5SJacob Faibussowitsch + dm   - the `DM` object
3270bc0a1609SRichard Tran Mills . g    - the original local vector
3271bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3272f089877aSRichard Tran Mills 
3273bc0a1609SRichard Tran Mills   Output Parameter:
3274bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3275f089877aSRichard Tran Mills 
3276f089877aSRichard Tran Mills   Level: intermediate
3277f089877aSRichard Tran Mills 
327860225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3279f089877aSRichard Tran Mills @*/
3280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3281d71ae5a4SJacob Faibussowitsch {
3282f089877aSRichard Tran Mills   PetscFunctionBegin;
3283f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32849f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32859f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32869f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3288f089877aSRichard Tran Mills }
3289f089877aSRichard Tran Mills 
329047c6ae99SBarry Smith /*@
3291bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
329247c6ae99SBarry Smith 
329320f4b53cSBarry Smith   Collective
329447c6ae99SBarry Smith 
3295d8d19677SJose E. Roman   Input Parameters:
3296bb7acecfSBarry Smith + dm   - the `DM` object
329720f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
329847c6ae99SBarry Smith 
329947c6ae99SBarry Smith   Output Parameter:
3300bb7acecfSBarry Smith . dmc - the coarsened `DM`
330147c6ae99SBarry Smith 
330247c6ae99SBarry Smith   Level: developer
330347c6ae99SBarry Smith 
330473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
330573ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
330647c6ae99SBarry Smith @*/
3307d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3308d71ae5a4SJacob Faibussowitsch {
3309b17ce1afSJed Brown   DMCoarsenHookLink link;
331047c6ae99SBarry Smith 
331147c6ae99SBarry Smith   PetscFunctionBegin;
3312171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3314dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3315b9d85ea2SLisandro Dalcin   if (*dmc) {
3316a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33179566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
331843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33199566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3320644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33210598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3322656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33239566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3324b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33259566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3326b17ce1afSJed Brown     }
3327b9d85ea2SLisandro Dalcin   }
33289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33297a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3331b17ce1afSJed Brown }
3332b17ce1afSJed Brown 
3333bb9467b5SJed Brown /*@C
3334b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3335b17ce1afSJed Brown 
333620f4b53cSBarry Smith   Logically Collective; No Fortran Support
3337b17ce1afSJed Brown 
33384165533cSJose E. Roman   Input Parameters:
3339bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3340b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3341bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
334220f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3343b17ce1afSJed Brown 
334420f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3345bb7acecfSBarry Smith + fine   - fine level `DM`
3346bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3347b17ce1afSJed Brown - ctx    - optional user-defined function context
3348b17ce1afSJed Brown 
334920f4b53cSBarry Smith   Calling sequence of `restricthook`:
3350bb7acecfSBarry Smith + fine      - fine level `DM`
3351bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3352c833c3b5SJed Brown . rscale    - scaling vector for restriction
3353c833c3b5SJed Brown . inject    - matrix restricting by injection
3354b17ce1afSJed Brown . coarse    - coarse level DM to update
3355b17ce1afSJed Brown - ctx       - optional user-defined function context
3356b17ce1afSJed Brown 
3357b17ce1afSJed Brown   Level: advanced
3358b17ce1afSJed Brown 
3359b17ce1afSJed Brown   Notes:
3360bb7acecfSBarry 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`.
3361b17ce1afSJed Brown 
3362b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3363b17ce1afSJed Brown 
3364b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3365bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3366b17ce1afSJed Brown 
3367bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3368bb7acecfSBarry Smith 
33691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3370b17ce1afSJed Brown @*/
3371a4e35b19SJacob 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)
3372d71ae5a4SJacob Faibussowitsch {
3373b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3374b17ce1afSJed Brown 
3375b17ce1afSJed Brown   PetscFunctionBegin;
3376b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33771e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33783ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33791e3d8eccSJed Brown   }
33809566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3381b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3382b17ce1afSJed Brown   link->restricthook = restricthook;
3383b17ce1afSJed Brown   link->ctx          = ctx;
33840298fd71SBarry Smith   link->next         = NULL;
3385b17ce1afSJed Brown   *p                 = link;
33863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3387b17ce1afSJed Brown }
3388b17ce1afSJed Brown 
3389dc822a44SJed Brown /*@C
3390bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3391dc822a44SJed Brown 
339220f4b53cSBarry Smith   Logically Collective; No Fortran Support
3393dc822a44SJed Brown 
33944165533cSJose E. Roman   Input Parameters:
3395bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3396dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3397bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
339820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3399dc822a44SJed Brown 
3400dc822a44SJed Brown   Level: advanced
3401dc822a44SJed Brown 
340273ff1848SBarry Smith   Notes:
340373ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
340473ff1848SBarry Smith 
340573ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3406dc822a44SJed Brown 
34071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3408dc822a44SJed Brown @*/
3409d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3410d71ae5a4SJacob Faibussowitsch {
3411dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3412dc822a44SJed Brown 
3413dc822a44SJed Brown   PetscFunctionBegin;
3414dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3415dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3416dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3417dc822a44SJed Brown       link = *p;
3418dc822a44SJed Brown       *p   = link->next;
34199566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3420dc822a44SJed Brown       break;
3421dc822a44SJed Brown     }
3422dc822a44SJed Brown   }
34233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3424dc822a44SJed Brown }
3425dc822a44SJed Brown 
3426b17ce1afSJed Brown /*@
3427bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3428b17ce1afSJed Brown 
3429b17ce1afSJed Brown   Collective if any hooks are
3430b17ce1afSJed Brown 
34314165533cSJose E. Roman   Input Parameters:
3432bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3433bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3434e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3435bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
343620f4b53cSBarry Smith - coarse  - coarser `DM` to update
3437b17ce1afSJed Brown 
3438b17ce1afSJed Brown   Level: developer
3439b17ce1afSJed Brown 
344073ff1848SBarry Smith   Developer Note:
3441bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3442bb7acecfSBarry Smith 
34431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3444b17ce1afSJed Brown @*/
3445d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3446d71ae5a4SJacob Faibussowitsch {
3447b17ce1afSJed Brown   DMCoarsenHookLink link;
3448b17ce1afSJed Brown 
3449b17ce1afSJed Brown   PetscFunctionBegin;
3450b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34511baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3452b17ce1afSJed Brown   }
34533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
345447c6ae99SBarry Smith }
345547c6ae99SBarry Smith 
3456bb9467b5SJed Brown /*@C
345773ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34585dbd56e3SPeter Brune 
345920f4b53cSBarry Smith   Logically Collective; No Fortran Support
34605dbd56e3SPeter Brune 
34614165533cSJose E. Roman   Input Parameters:
3462bb7acecfSBarry Smith + global       - global `DM`
3463bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34645dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
346520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34665dbd56e3SPeter Brune 
346720f4b53cSBarry Smith   Calling sequence of `ddhook`:
3468bb7acecfSBarry Smith + global - global `DM`
346973ff1848SBarry Smith . block  - subdomain `DM`
3470ec4806b8SPeter Brune - ctx    - optional user-defined function context
3471ec4806b8SPeter Brune 
347220f4b53cSBarry Smith   Calling sequence of `restricthook`:
3473bb7acecfSBarry Smith + global - global `DM`
347473ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
347573ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
347673ff1848SBarry Smith . block  - subdomain `DM`
34775dbd56e3SPeter Brune - ctx    - optional user-defined function context
34785dbd56e3SPeter Brune 
34795dbd56e3SPeter Brune   Level: advanced
34805dbd56e3SPeter Brune 
34815dbd56e3SPeter Brune   Notes:
348273ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34835dbd56e3SPeter Brune 
34845dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34855dbd56e3SPeter Brune 
34865dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3487bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
34885dbd56e3SPeter Brune 
348973ff1848SBarry Smith   Developer Note:
349073ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
349173ff1848SBarry Smith 
349273ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
34935dbd56e3SPeter Brune @*/
3494a4e35b19SJacob 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)
3495d71ae5a4SJacob Faibussowitsch {
3496be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34975dbd56e3SPeter Brune 
34985dbd56e3SPeter Brune   PetscFunctionBegin;
34995dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3500b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35013ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3502b3a6b972SJed Brown   }
35039566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35045dbd56e3SPeter Brune   link->restricthook = restricthook;
3505be081cd6SPeter Brune   link->ddhook       = ddhook;
35065dbd56e3SPeter Brune   link->ctx          = ctx;
35070298fd71SBarry Smith   link->next         = NULL;
35085dbd56e3SPeter Brune   *p                 = link;
35093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35105dbd56e3SPeter Brune }
35115dbd56e3SPeter Brune 
3512b3a6b972SJed Brown /*@C
351373ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3514b3a6b972SJed Brown 
351520f4b53cSBarry Smith   Logically Collective; No Fortran Support
3516b3a6b972SJed Brown 
35174165533cSJose E. Roman   Input Parameters:
3518bb7acecfSBarry Smith + global       - global `DM`
3519bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3520b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
352120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3522b3a6b972SJed Brown 
3523b3a6b972SJed Brown   Level: advanced
3524b3a6b972SJed Brown 
352573ff1848SBarry Smith   Note:
352673ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
352773ff1848SBarry Smith 
352873ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
352973ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3530b3a6b972SJed Brown @*/
3531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3532d71ae5a4SJacob Faibussowitsch {
3533b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3534b3a6b972SJed Brown 
3535b3a6b972SJed Brown   PetscFunctionBegin;
3536b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3537b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3538b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3539b3a6b972SJed Brown       link = *p;
3540b3a6b972SJed Brown       *p   = link->next;
35419566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3542b3a6b972SJed Brown       break;
3543b3a6b972SJed Brown     }
3544b3a6b972SJed Brown   }
35453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3546b3a6b972SJed Brown }
3547b3a6b972SJed Brown 
35485dbd56e3SPeter Brune /*@
354973ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35505dbd56e3SPeter Brune 
35515dbd56e3SPeter Brune   Collective if any hooks are
35525dbd56e3SPeter Brune 
35534165533cSJose E. Roman   Input Parameters:
3554a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3555a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3556a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3557a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35585dbd56e3SPeter Brune 
35595dbd56e3SPeter Brune   Level: developer
35605dbd56e3SPeter Brune 
356173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35625dbd56e3SPeter Brune @*/
3563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3564d71ae5a4SJacob Faibussowitsch {
3565be081cd6SPeter Brune   DMSubDomainHookLink link;
35665dbd56e3SPeter Brune 
35675dbd56e3SPeter Brune   PetscFunctionBegin;
3568be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35691baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35705dbd56e3SPeter Brune   }
35713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35725dbd56e3SPeter Brune }
35735dbd56e3SPeter Brune 
35745fe1f584SPeter Brune /*@
3575bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35765fe1f584SPeter Brune 
35775fe1f584SPeter Brune   Not Collective
35785fe1f584SPeter Brune 
35795fe1f584SPeter Brune   Input Parameter:
3580bb7acecfSBarry Smith . dm - the `DM` object
35815fe1f584SPeter Brune 
35825fe1f584SPeter Brune   Output Parameter:
35836a7d9d85SPeter Brune . level - number of coarsenings
35845fe1f584SPeter Brune 
35855fe1f584SPeter Brune   Level: developer
35865fe1f584SPeter Brune 
35871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35885fe1f584SPeter Brune @*/
3589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3590d71ae5a4SJacob Faibussowitsch {
35915fe1f584SPeter Brune   PetscFunctionBegin;
35925fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35934f572ea9SToby Isaac   PetscAssertPointer(level, 2);
35945fe1f584SPeter Brune   *level = dm->leveldown;
35953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35965fe1f584SPeter Brune }
35975fe1f584SPeter Brune 
35989a64c4a8SMatthew G. Knepley /*@
3599bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36009a64c4a8SMatthew G. Knepley 
360120f4b53cSBarry Smith   Collective
36029a64c4a8SMatthew G. Knepley 
36039a64c4a8SMatthew G. Knepley   Input Parameters:
3604bb7acecfSBarry Smith + dm    - the `DM` object
36059a64c4a8SMatthew G. Knepley - level - number of coarsenings
36069a64c4a8SMatthew G. Knepley 
36079a64c4a8SMatthew G. Knepley   Level: developer
36089a64c4a8SMatthew G. Knepley 
3609bb7acecfSBarry Smith   Note:
3610bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3611bb7acecfSBarry Smith 
361242747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36139a64c4a8SMatthew G. Knepley @*/
3614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3615d71ae5a4SJacob Faibussowitsch {
36169a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36179a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36189a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36209a64c4a8SMatthew G. Knepley }
36219a64c4a8SMatthew G. Knepley 
3622cc4c1da9SBarry Smith /*@
3623bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
362447c6ae99SBarry Smith 
362520f4b53cSBarry Smith   Collective
362647c6ae99SBarry Smith 
3627d8d19677SJose E. Roman   Input Parameters:
3628bb7acecfSBarry Smith + dm      - the `DM` object
362947c6ae99SBarry Smith - nlevels - the number of levels of refinement
363047c6ae99SBarry Smith 
363147c6ae99SBarry Smith   Output Parameter:
3632bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
363347c6ae99SBarry Smith 
363447c6ae99SBarry Smith   Level: developer
363547c6ae99SBarry Smith 
36361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363747c6ae99SBarry Smith @*/
3638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3639d71ae5a4SJacob Faibussowitsch {
364047c6ae99SBarry Smith   PetscFunctionBegin;
3641171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36427a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36433ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36444f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3645213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
364647c6ae99SBarry Smith     PetscInt i;
364747c6ae99SBarry Smith 
36489566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
364948a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3650213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365247c6ae99SBarry Smith }
365347c6ae99SBarry Smith 
3654cc4c1da9SBarry Smith /*@
3655bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
365647c6ae99SBarry Smith 
365720f4b53cSBarry Smith   Collective
365847c6ae99SBarry Smith 
3659d8d19677SJose E. Roman   Input Parameters:
3660bb7acecfSBarry Smith + dm      - the `DM` object
366147c6ae99SBarry Smith - nlevels - the number of levels of coarsening
366247c6ae99SBarry Smith 
366347c6ae99SBarry Smith   Output Parameter:
3664bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
366547c6ae99SBarry Smith 
366647c6ae99SBarry Smith   Level: developer
366747c6ae99SBarry Smith 
36681cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
366947c6ae99SBarry Smith @*/
3670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3671d71ae5a4SJacob Faibussowitsch {
367247c6ae99SBarry Smith   PetscFunctionBegin;
3673171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36747a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36753ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36764f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3677213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
367847c6ae99SBarry Smith     PetscInt i;
367947c6ae99SBarry Smith 
36809566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
368148a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3682213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368447c6ae99SBarry Smith }
368547c6ae99SBarry Smith 
36861a266240SBarry Smith /*@C
3687bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36881a266240SBarry Smith 
3689bb7acecfSBarry Smith   Logically Collective if the function is collective
36901a266240SBarry Smith 
36911a266240SBarry Smith   Input Parameters:
3692bb7acecfSBarry Smith + dm      - the `DM` object
36931a266240SBarry Smith - destroy - the destroy function
36941a266240SBarry Smith 
36951a266240SBarry Smith   Level: intermediate
36961a266240SBarry Smith 
36971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3698f07f9ceaSJed Brown @*/
3699d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3700d71ae5a4SJacob Faibussowitsch {
37011a266240SBarry Smith   PetscFunctionBegin;
3702171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37031a266240SBarry Smith   dm->ctxdestroy = destroy;
37043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37051a266240SBarry Smith }
37061a266240SBarry Smith 
3707b07ff414SBarry Smith /*@
3708bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
370947c6ae99SBarry Smith 
371047c6ae99SBarry Smith   Not Collective
371147c6ae99SBarry Smith 
371247c6ae99SBarry Smith   Input Parameters:
3713bb7acecfSBarry Smith + dm  - the `DM` object
371447c6ae99SBarry Smith - ctx - the user context
371547c6ae99SBarry Smith 
371647c6ae99SBarry Smith   Level: intermediate
371747c6ae99SBarry Smith 
3718e33b3f0fSStefano Zampini   Notes:
371935cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3720e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3721e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3722bb7acecfSBarry Smith 
372360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
372447c6ae99SBarry Smith @*/
3725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3726d71ae5a4SJacob Faibussowitsch {
372747c6ae99SBarry Smith   PetscFunctionBegin;
3728171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372947c6ae99SBarry Smith   dm->ctx = ctx;
37303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373147c6ae99SBarry Smith }
373247c6ae99SBarry Smith 
373347c6ae99SBarry Smith /*@
3734bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
373547c6ae99SBarry Smith 
373647c6ae99SBarry Smith   Not Collective
373747c6ae99SBarry Smith 
373847c6ae99SBarry Smith   Input Parameter:
3739bb7acecfSBarry Smith . dm - the `DM` object
374047c6ae99SBarry Smith 
374147c6ae99SBarry Smith   Output Parameter:
374247c6ae99SBarry Smith . ctx - the user context
374347c6ae99SBarry Smith 
374447c6ae99SBarry Smith   Level: intermediate
374547c6ae99SBarry Smith 
3746bb7acecfSBarry Smith   Note:
374735cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3748bb7acecfSBarry Smith 
374942747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
375047c6ae99SBarry Smith @*/
3751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3752d71ae5a4SJacob Faibussowitsch {
375347c6ae99SBarry Smith   PetscFunctionBegin;
3754171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37551b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
375747c6ae99SBarry Smith }
375847c6ae99SBarry Smith 
375908da532bSDmitry Karpeev /*@C
3760bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
376108da532bSDmitry Karpeev 
376220f4b53cSBarry Smith   Logically Collective
376308da532bSDmitry Karpeev 
3764d8d19677SJose E. Roman   Input Parameters:
376508da532bSDmitry Karpeev + dm - the DM object
376620f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
376708da532bSDmitry Karpeev 
376808da532bSDmitry Karpeev   Level: intermediate
376908da532bSDmitry Karpeev 
37701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3771db781477SPatrick Sanan          `DMSetJacobian()`
377208da532bSDmitry Karpeev @*/
3773d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3774d71ae5a4SJacob Faibussowitsch {
377508da532bSDmitry Karpeev   PetscFunctionBegin;
37765a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377908da532bSDmitry Karpeev }
378008da532bSDmitry Karpeev 
378108da532bSDmitry Karpeev /*@
3782bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
378308da532bSDmitry Karpeev 
378408da532bSDmitry Karpeev   Not Collective
378508da532bSDmitry Karpeev 
378608da532bSDmitry Karpeev   Input Parameter:
3787bb7acecfSBarry Smith . dm - the `DM` object to destroy
378808da532bSDmitry Karpeev 
378908da532bSDmitry Karpeev   Output Parameter:
3790bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
379108da532bSDmitry Karpeev 
379208da532bSDmitry Karpeev   Level: developer
379308da532bSDmitry Karpeev 
37941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379508da532bSDmitry Karpeev @*/
3796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3797d71ae5a4SJacob Faibussowitsch {
379808da532bSDmitry Karpeev   PetscFunctionBegin;
37995a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38004f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
380108da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
380308da532bSDmitry Karpeev }
380408da532bSDmitry Karpeev 
3805cc4c1da9SBarry Smith /*@
3806bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
380708da532bSDmitry Karpeev 
380820f4b53cSBarry Smith   Logically Collective
380908da532bSDmitry Karpeev 
3810f899ff85SJose E. Roman   Input Parameter:
3811bb7acecfSBarry Smith . dm - the `DM` object
381208da532bSDmitry Karpeev 
381360225df5SJacob Faibussowitsch   Output Parameters:
381408da532bSDmitry Karpeev + xl - lower bound
381508da532bSDmitry Karpeev - xu - upper bound
381608da532bSDmitry Karpeev 
3817907376e6SBarry Smith   Level: advanced
3818907376e6SBarry Smith 
381920f4b53cSBarry Smith   Note:
382095452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
382108da532bSDmitry Karpeev 
38221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
382308da532bSDmitry Karpeev @*/
3824d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3825d71ae5a4SJacob Faibussowitsch {
382608da532bSDmitry Karpeev   PetscFunctionBegin;
38275a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38295a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3830dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
383208da532bSDmitry Karpeev }
383308da532bSDmitry Karpeev 
3834b0ae01b7SPeter Brune /*@
3835bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3836b0ae01b7SPeter Brune 
3837b0ae01b7SPeter Brune   Not Collective
3838b0ae01b7SPeter Brune 
3839b0ae01b7SPeter Brune   Input Parameter:
3840b0ae01b7SPeter Brune . dm - the DM object
3841b0ae01b7SPeter Brune 
3842b0ae01b7SPeter Brune   Output Parameter:
3843bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3844b0ae01b7SPeter Brune 
3845b0ae01b7SPeter Brune   Level: developer
3846b0ae01b7SPeter Brune 
38471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3848b0ae01b7SPeter Brune @*/
3849d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3850d71ae5a4SJacob Faibussowitsch {
3851b0ae01b7SPeter Brune   PetscFunctionBegin;
38525a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38534f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3854b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3856b0ae01b7SPeter Brune }
3857b0ae01b7SPeter Brune 
38583ad4599aSBarry Smith /*@
3859bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38603ad4599aSBarry Smith 
38613ad4599aSBarry Smith   Not Collective
38623ad4599aSBarry Smith 
38633ad4599aSBarry Smith   Input Parameter:
3864bb7acecfSBarry Smith . dm - the `DM` object
38653ad4599aSBarry Smith 
38663ad4599aSBarry Smith   Output Parameter:
3867bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38683ad4599aSBarry Smith 
38693ad4599aSBarry Smith   Level: developer
38703ad4599aSBarry Smith 
38711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38723ad4599aSBarry Smith @*/
3873d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3874d71ae5a4SJacob Faibussowitsch {
38753ad4599aSBarry Smith   PetscFunctionBegin;
38765a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38774f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38783ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38803ad4599aSBarry Smith }
38813ad4599aSBarry Smith 
3882a7058e45SLawrence Mitchell /*@
3883bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3884a7058e45SLawrence Mitchell 
3885a7058e45SLawrence Mitchell   Not Collective
3886a7058e45SLawrence Mitchell 
3887a7058e45SLawrence Mitchell   Input Parameter:
3888bb7acecfSBarry Smith . dm - the `DM` object
3889a7058e45SLawrence Mitchell 
3890a7058e45SLawrence Mitchell   Output Parameter:
3891bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3892a7058e45SLawrence Mitchell 
3893a7058e45SLawrence Mitchell   Level: developer
3894a7058e45SLawrence Mitchell 
38951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3896a7058e45SLawrence Mitchell @*/
3897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3898d71ae5a4SJacob Faibussowitsch {
3899a7058e45SLawrence Mitchell   PetscFunctionBegin;
39005a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39014f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3902dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3903ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3905a7058e45SLawrence Mitchell }
3906a7058e45SLawrence Mitchell 
39070298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3908264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3909264ace61SBarry Smith 
3910cc4c1da9SBarry Smith /*@
3911bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3912264ace61SBarry Smith 
391320f4b53cSBarry Smith   Collective
3914264ace61SBarry Smith 
3915264ace61SBarry Smith   Input Parameters:
3916bb7acecfSBarry Smith + dm     - The `DM` object
3917bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3918264ace61SBarry Smith 
3919264ace61SBarry Smith   Options Database Key:
3920bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3921264ace61SBarry Smith 
3922264ace61SBarry Smith   Level: intermediate
3923264ace61SBarry Smith 
3924bb7acecfSBarry Smith   Note:
39250462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3926bb7acecfSBarry Smith 
39271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3928264ace61SBarry Smith @*/
3929d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3930d71ae5a4SJacob Faibussowitsch {
3931264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3932264ace61SBarry Smith   PetscBool match;
3933264ace61SBarry Smith 
3934264ace61SBarry Smith   PetscFunctionBegin;
3935264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39369566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39373ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3938264ace61SBarry Smith 
39399566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39409566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39417a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3942264ace61SBarry Smith 
3943dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39449566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39459566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39469566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3948264ace61SBarry Smith }
3949264ace61SBarry Smith 
3950cc4c1da9SBarry Smith /*@
3951bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3952264ace61SBarry Smith 
3953264ace61SBarry Smith   Not Collective
3954264ace61SBarry Smith 
3955264ace61SBarry Smith   Input Parameter:
3956bb7acecfSBarry Smith . dm - The `DM`
3957264ace61SBarry Smith 
3958264ace61SBarry Smith   Output Parameter:
3959bb7acecfSBarry Smith . type - The `DMType` name
3960264ace61SBarry Smith 
3961264ace61SBarry Smith   Level: intermediate
3962264ace61SBarry Smith 
39631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3964264ace61SBarry Smith @*/
3965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3966d71ae5a4SJacob Faibussowitsch {
3967264ace61SBarry Smith   PetscFunctionBegin;
3968264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39694f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39709566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3971264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3973264ace61SBarry Smith }
3974264ace61SBarry Smith 
39755d83a8b1SBarry Smith /*@
3976bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
397767a56275SMatthew G Knepley 
397820f4b53cSBarry Smith   Collective
397967a56275SMatthew G Knepley 
398067a56275SMatthew G Knepley   Input Parameters:
3981bb7acecfSBarry Smith + dm      - the `DM`
3982bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
398367a56275SMatthew G Knepley 
398467a56275SMatthew G Knepley   Output Parameter:
3985bb7acecfSBarry Smith . M - pointer to new `DM`
398667a56275SMatthew G Knepley 
398720f4b53cSBarry Smith   Level: intermediate
398820f4b53cSBarry Smith 
398973ff1848SBarry Smith   Note:
3990bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3991bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3992bb7acecfSBarry Smith   of the input `DM`.
399367a56275SMatthew G Knepley 
39941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
399567a56275SMatthew G Knepley @*/
3996d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3997d71ae5a4SJacob Faibussowitsch {
399867a56275SMatthew G Knepley   DM        B;
399967a56275SMatthew G Knepley   char      convname[256];
4000c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
400167a56275SMatthew G Knepley 
400267a56275SMatthew G Knepley   PetscFunctionBegin;
400367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400467a56275SMatthew G Knepley   PetscValidType(dm, 1);
40054f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40069566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40079566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4008c067b6caSMatthew G. Knepley   if (sametype) {
4009c067b6caSMatthew G. Knepley     *M = dm;
40109566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40113ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4012c067b6caSMatthew G. Knepley   } else {
40130298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
401467a56275SMatthew G Knepley 
401567a56275SMatthew G Knepley     /*
401667a56275SMatthew G Knepley        Order of precedence:
401767a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
401867a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
401967a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
402067a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
402167a56275SMatthew G Knepley        5) Use a really basic converter.
402267a56275SMatthew G Knepley     */
402367a56275SMatthew G Knepley 
402467a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40259566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40269566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40279566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40289566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40299566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40309566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
403167a56275SMatthew G Knepley     if (conv) goto foundconv;
403267a56275SMatthew G Knepley 
403367a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40349566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40359566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40369566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40379566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40389566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40399566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40409566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40419566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
404267a56275SMatthew G Knepley     if (conv) {
40439566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
404467a56275SMatthew G Knepley       goto foundconv;
404567a56275SMatthew G Knepley     }
404667a56275SMatthew G Knepley 
404767a56275SMatthew G Knepley #if 0
404867a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
404967a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40509566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
405167a56275SMatthew G Knepley     if (conv) goto foundconv;
405267a56275SMatthew G Knepley 
405367a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
405467a56275SMatthew G Knepley     if (dm->ops->convert) {
405567a56275SMatthew G Knepley       conv = dm->ops->convert;
405667a56275SMatthew G Knepley     }
405767a56275SMatthew G Knepley     if (conv) goto foundconv;
405867a56275SMatthew G Knepley #endif
405967a56275SMatthew G Knepley 
406067a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
406198921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
406267a56275SMatthew G Knepley 
406367a56275SMatthew G Knepley   foundconv:
40649566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40659566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
406612fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
406790b157c4SStefano Zampini     {
40684fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40696858538eSMatthew G. Knepley 
40704fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40714fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4072c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40739566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40749566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40759566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40769566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
407712fa691eSMatthew G. Knepley     }
40789566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
407967a56275SMatthew G Knepley   }
40809566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
408267a56275SMatthew G Knepley }
4083264ace61SBarry Smith 
4084264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4085264ace61SBarry Smith 
4086264ace61SBarry Smith /*@C
4087bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40881c84c290SBarry Smith 
4089cc4c1da9SBarry Smith   Not Collective, No Fortran Support
40901c84c290SBarry Smith 
40911c84c290SBarry Smith   Input Parameters:
409220f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
409320f4b53cSBarry Smith - function - The creation routine itself
409420f4b53cSBarry Smith 
409520f4b53cSBarry Smith   Level: advanced
40961c84c290SBarry Smith 
409773ff1848SBarry Smith   Note:
409820f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40991c84c290SBarry Smith 
410060225df5SJacob Faibussowitsch   Example Usage:
41011c84c290SBarry Smith .vb
4102bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41031c84c290SBarry Smith .ve
41041c84c290SBarry Smith 
410520f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41061c84c290SBarry Smith .vb
41071c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41081c84c290SBarry Smith     DMSetType(DM,"my_da");
41091c84c290SBarry Smith .ve
41101c84c290SBarry Smith   or at runtime via the option
41111c84c290SBarry Smith .vb
41121c84c290SBarry Smith     -da_type my_da
41131c84c290SBarry Smith .ve
4114264ace61SBarry Smith 
41151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4116264ace61SBarry Smith @*/
4117d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4118d71ae5a4SJacob Faibussowitsch {
4119264ace61SBarry Smith   PetscFunctionBegin;
41209566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41219566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4123264ace61SBarry Smith }
4124264ace61SBarry Smith 
4125ffeef943SBarry Smith /*@
4126bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4127b859378eSBarry Smith 
412820f4b53cSBarry Smith   Collective
4129b859378eSBarry Smith 
4130b859378eSBarry Smith   Input Parameters:
4131bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4132bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4133bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4134bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4135b859378eSBarry Smith 
4136b859378eSBarry Smith   Level: intermediate
4137b859378eSBarry Smith 
4138b859378eSBarry Smith   Notes:
413955849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4140b859378eSBarry Smith 
4141bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4142bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4143bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4144cd7e8a5eSksagiyam 
41451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4146b859378eSBarry Smith @*/
4147d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4148d71ae5a4SJacob Faibussowitsch {
41499331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4150b859378eSBarry Smith 
4151b859378eSBarry Smith   PetscFunctionBegin;
4152b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4153b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41549566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41559566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41569566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41589331c7a4SMatthew G. Knepley   if (isbinary) {
41599331c7a4SMatthew G. Knepley     PetscInt classid;
41609331c7a4SMatthew G. Knepley     char     type[256];
4161b859378eSBarry Smith 
41629566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41637a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41649566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41659566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4166dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41679331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4168dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41699331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4172b859378eSBarry Smith }
4173b859378eSBarry Smith 
41747da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41757da65231SMatthew G Knepley 
41762ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41772ecf6ec3SMatthew G. Knepley {
41782ecf6ec3SMatthew G. Knepley   PetscInt f;
41792ecf6ec3SMatthew G. Knepley 
41802ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41812ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41822ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41832ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41842ecf6ec3SMatthew G. Knepley }
41852ecf6ec3SMatthew G. Knepley 
4186d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4187d71ae5a4SJacob Faibussowitsch {
41881d47ebbbSSatish Balay   PetscInt f;
41891b30c384SMatthew G Knepley 
41907da65231SMatthew G Knepley   PetscFunctionBegin;
419163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
419248a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41947da65231SMatthew G Knepley }
41957da65231SMatthew G Knepley 
41965962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
41975962854dSMatthew G. Knepley {
41985962854dSMatthew G. Knepley   PetscInt f;
41995962854dSMatthew G. Knepley 
42005962854dSMatthew G. Knepley   PetscFunctionBegin;
42015962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42025962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42035962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42045962854dSMatthew G. Knepley }
42055962854dSMatthew G. Knepley 
4206d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4207d71ae5a4SJacob Faibussowitsch {
42081b30c384SMatthew G Knepley   PetscInt f, g;
42097da65231SMatthew G Knepley 
42107da65231SMatthew G Knepley   PetscFunctionBegin;
421163a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42121d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42139566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
421448a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42159566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42167da65231SMatthew G Knepley   }
42173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42187da65231SMatthew G Knepley }
4219e7c4fc90SDmitry Karpeev 
4220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4221d71ae5a4SJacob Faibussowitsch {
42220c5b8624SToby Isaac   PetscInt           localSize, bs;
42230c5b8624SToby Isaac   PetscMPIInt        size;
42240c5b8624SToby Isaac   Vec                x, xglob;
42250c5b8624SToby Isaac   const PetscScalar *xarray;
4226e759306cSMatthew G. Knepley 
4227e759306cSMatthew G. Knepley   PetscFunctionBegin;
42289566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42299566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42309566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
423193ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42329566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42330c5b8624SToby Isaac   if (size > 1) {
42349566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42359566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42369566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42379566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42380c5b8624SToby Isaac   } else {
42390c5b8624SToby Isaac     xglob = x;
42400c5b8624SToby Isaac   }
42419566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42420c5b8624SToby Isaac   if (size > 1) {
42439566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42450c5b8624SToby Isaac   }
42469566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4248e759306cSMatthew G. Knepley }
4249e759306cSMatthew G. Knepley 
425088ed4aceSMatthew G Knepley /*@
4251bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4252061576a5SJed Brown 
4253061576a5SJed Brown   Input Parameter:
4254bb7acecfSBarry Smith . dm - The `DM`
4255061576a5SJed Brown 
4256061576a5SJed Brown   Output Parameter:
4257bb7acecfSBarry Smith . section - The `PetscSection`
4258061576a5SJed Brown 
425920f4b53cSBarry Smith   Options Database Key:
4260bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4261061576a5SJed Brown 
4262061576a5SJed Brown   Level: advanced
4263061576a5SJed Brown 
4264061576a5SJed Brown   Notes:
4265bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4266061576a5SJed Brown 
4267bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4268061576a5SJed Brown 
42691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4270061576a5SJed Brown @*/
4271d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4272d71ae5a4SJacob Faibussowitsch {
4273061576a5SJed Brown   PetscFunctionBegin;
42749566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4276061576a5SJed Brown }
4277061576a5SJed Brown 
4278061576a5SJed Brown /*@
4279bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
428088ed4aceSMatthew G Knepley 
428188ed4aceSMatthew G Knepley   Input Parameter:
4282bb7acecfSBarry Smith . dm - The `DM`
428388ed4aceSMatthew G Knepley 
428488ed4aceSMatthew G Knepley   Output Parameter:
4285bb7acecfSBarry Smith . section - The `PetscSection`
428688ed4aceSMatthew G Knepley 
428720f4b53cSBarry Smith   Options Database Key:
4288bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4289e5893cccSMatthew G. Knepley 
429088ed4aceSMatthew G Knepley   Level: intermediate
429188ed4aceSMatthew G Knepley 
4292bb7acecfSBarry Smith   Note:
4293bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
429488ed4aceSMatthew G Knepley 
42951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
429688ed4aceSMatthew G Knepley @*/
4297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4298d71ae5a4SJacob Faibussowitsch {
429988ed4aceSMatthew G Knepley   PetscFunctionBegin;
430088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43014f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43021bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4303e5e52638SMatthew G. Knepley     PetscInt d;
4304e5e52638SMatthew G. Knepley 
430545480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
430645480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
430745480ffeSMatthew G. Knepley       PetscViewer       viewer;
430845480ffeSMatthew G. Knepley       PetscViewerFormat format;
430945480ffeSMatthew G. Knepley       PetscBool         flg;
431045480ffeSMatthew G. Knepley 
4311648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43129566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
431345480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43149566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43159566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
431645480ffeSMatthew G. Knepley       }
431745480ffeSMatthew G. Knepley       if (flg) {
43189566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43199566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4320648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
432145480ffeSMatthew G. Knepley       }
432245480ffeSMatthew G. Knepley     }
4323dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43249566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43252f0f8703SMatthew G. Knepley   }
43261bb6d2a8SBarry Smith   *section = dm->localSection;
43273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432888ed4aceSMatthew G Knepley }
432988ed4aceSMatthew G Knepley 
433088ed4aceSMatthew G Knepley /*@
4331bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4332061576a5SJed Brown 
4333061576a5SJed Brown   Input Parameters:
4334bb7acecfSBarry Smith + dm      - The `DM`
4335bb7acecfSBarry Smith - section - The `PetscSection`
4336061576a5SJed Brown 
4337061576a5SJed Brown   Level: advanced
4338061576a5SJed Brown 
4339061576a5SJed Brown   Notes:
4340bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4341061576a5SJed Brown 
4342bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4343061576a5SJed Brown 
43441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4345061576a5SJed Brown @*/
4346d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4347d71ae5a4SJacob Faibussowitsch {
4348061576a5SJed Brown   PetscFunctionBegin;
43499566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4351061576a5SJed Brown }
4352061576a5SJed Brown 
4353061576a5SJed Brown /*@
4354bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley   Input Parameters:
4357bb7acecfSBarry Smith + dm      - The `DM`
4358bb7acecfSBarry Smith - section - The `PetscSection`
435988ed4aceSMatthew G Knepley 
436088ed4aceSMatthew G Knepley   Level: intermediate
436188ed4aceSMatthew G Knepley 
4362bb7acecfSBarry Smith   Note:
4363bb7acecfSBarry Smith   Any existing Section will be destroyed
436488ed4aceSMatthew G Knepley 
43651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
436688ed4aceSMatthew G Knepley @*/
4367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4368d71ae5a4SJacob Faibussowitsch {
4369c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4370af122d2aSMatthew G Knepley   PetscInt f;
437188ed4aceSMatthew G Knepley 
437288ed4aceSMatthew G Knepley   PetscFunctionBegin;
437388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4374b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43759566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43769566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43771bb6d2a8SBarry Smith   dm->localSection = section;
43789566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4379af122d2aSMatthew G Knepley   if (numFields) {
43809566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4381af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43820f21e855SMatthew G. Knepley       PetscObject disc;
4383af122d2aSMatthew G Knepley       const char *name;
4384af122d2aSMatthew G Knepley 
43859566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43869566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43879566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4388af122d2aSMatthew G Knepley     }
4389af122d2aSMatthew G Knepley   }
439050350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
439150350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
43929566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
439350350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
439452947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
439550350c8eSStefano Zampini 
439650350c8eSStefano Zampini   /* Clear scratch vectors */
439750350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
439850350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
439950350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
440050350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
440288ed4aceSMatthew G Knepley }
440388ed4aceSMatthew G Knepley 
4404adc21957SMatthew G. Knepley /*@C
4405d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4406adc21957SMatthew G. Knepley 
4407adc21957SMatthew G. Knepley   Input Parameter:
4408adc21957SMatthew G. Knepley . dm - The `DM`
4409adc21957SMatthew G. Knepley 
44109cde84edSJose E. Roman   Output Parameters:
4411adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4412b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4413adc21957SMatthew G. Knepley 
4414adc21957SMatthew G. Knepley   Level: developer
4415adc21957SMatthew G. Knepley 
4416adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4417adc21957SMatthew G. Knepley @*/
4418adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4419adc21957SMatthew G. Knepley {
4420adc21957SMatthew G. Knepley   PetscFunctionBegin;
4421adc21957SMatthew G. Knepley   *perm        = NULL;
4422adc21957SMatthew G. Knepley   *blockStarts = NULL;
4423adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4424adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4425adc21957SMatthew G. Knepley }
4426adc21957SMatthew G. Knepley 
44279435951eSToby Isaac /*@
4428bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44299435951eSToby Isaac 
443020f4b53cSBarry Smith   not Collective
4431e228b242SToby Isaac 
44329435951eSToby Isaac   Input Parameter:
4433bb7acecfSBarry Smith . dm - The `DM`
44349435951eSToby Isaac 
4435d8d19677SJose E. Roman   Output Parameters:
443620f4b53cSBarry 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.
443720f4b53cSBarry 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.
443879769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44399435951eSToby Isaac 
44409435951eSToby Isaac   Level: advanced
44419435951eSToby Isaac 
4442bb7acecfSBarry Smith   Note:
4443bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44449435951eSToby Isaac 
44451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44469435951eSToby Isaac @*/
4447d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4448d71ae5a4SJacob Faibussowitsch {
44499435951eSToby Isaac   PetscFunctionBegin;
44509435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4451dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44523b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44533b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
445479769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44569435951eSToby Isaac }
44579435951eSToby Isaac 
44589435951eSToby Isaac /*@
4459bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44609435951eSToby Isaac 
446120f4b53cSBarry Smith   Collective
4462e228b242SToby Isaac 
44639435951eSToby Isaac   Input Parameters:
4464bb7acecfSBarry Smith + dm      - The `DM`
4465bb7acecfSBarry 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).
446620f4b53cSBarry 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).
446720f4b53cSBarry 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).
44689435951eSToby Isaac 
44699435951eSToby Isaac   Level: advanced
44709435951eSToby Isaac 
447120f4b53cSBarry Smith   Notes:
447220f4b53cSBarry 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()`.
447320f4b53cSBarry Smith 
447420f4b53cSBarry 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.
447520f4b53cSBarry Smith 
4476bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44779435951eSToby Isaac 
44781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44799435951eSToby Isaac @*/
4480d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4481d71ae5a4SJacob Faibussowitsch {
4482e228b242SToby Isaac   PetscMPIInt result;
44839435951eSToby Isaac 
44849435951eSToby Isaac   PetscFunctionBegin;
44859435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4486e228b242SToby Isaac   if (section) {
4487e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44889566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44897a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4490e228b242SToby Isaac   }
4491e228b242SToby Isaac   if (mat) {
4492e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44939566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44947a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4495e228b242SToby Isaac   }
449679769bd5SJed Brown   if (bias) {
449779769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44989566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
449979769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
450079769bd5SJed Brown   }
45019566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45029566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45033b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45059566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45063b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
450979769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45119435951eSToby Isaac }
45129435951eSToby Isaac 
4513497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4514507e4973SMatthew G. Knepley /*
4515bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4516507e4973SMatthew G. Knepley 
4517507e4973SMatthew G. Knepley   Input Parameters:
4518bb7acecfSBarry Smith + dm - The `DM`
4519bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4520bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4521507e4973SMatthew G. Knepley 
4522507e4973SMatthew G. Knepley   Level: intermediate
4523507e4973SMatthew G. Knepley 
45241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4525507e4973SMatthew G. Knepley */
4526d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4527d71ae5a4SJacob Faibussowitsch {
4528507e4973SMatthew G. Knepley   MPI_Comm        comm;
4529507e4973SMatthew G. Knepley   PetscLayout     layout;
4530507e4973SMatthew G. Knepley   const PetscInt *ranges;
4531507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4532507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4533507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4534507e4973SMatthew G. Knepley 
4535507e4973SMatthew G. Knepley   PetscFunctionBegin;
45369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4537507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45399566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45409566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45419566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45429566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45439566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45459566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4547507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4548f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4549507e4973SMatthew G. Knepley 
45509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4556507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45579371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45589371c9d4SSatish 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));
45599371c9d4SSatish Balay       valid = PETSC_FALSE;
45609371c9d4SSatish Balay     }
45619371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45629371c9d4SSatish 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));
45639371c9d4SSatish Balay       valid = PETSC_FALSE;
45649371c9d4SSatish Balay     }
4565507e4973SMatthew G. Knepley     if (gdof < 0) {
4566507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4567507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4568507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4569507e4973SMatthew G. Knepley 
45709566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4571507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45729371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45739371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45749371c9d4SSatish Balay           valid = PETSC_FALSE;
45759371c9d4SSatish Balay           break;
45769371c9d4SSatish Balay         }
4577507e4973SMatthew G. Knepley       }
4578507e4973SMatthew G. Knepley     }
4579507e4973SMatthew G. Knepley   }
45809566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45819566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45821c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4583507e4973SMatthew G. Knepley   if (!gvalid) {
45849566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4585507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4586507e4973SMatthew G. Knepley   }
45873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4588507e4973SMatthew G. Knepley }
4589f741bcd2SMatthew G. Knepley #endif
4590507e4973SMatthew G. Knepley 
45915f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45926725e60dSJed Brown {
45936725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45944d86920dSPierre Jolivet 
45956725e60dSJed Brown   PetscFunctionBegin;
45966725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45974f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
45985f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45996725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46006725e60dSJed Brown   else *sf = dm->sf;
46013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46026725e60dSJed Brown }
46036725e60dSJed Brown 
460488ed4aceSMatthew G Knepley /*@
4605bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
460688ed4aceSMatthew G Knepley 
460720f4b53cSBarry Smith   Collective
46088b1ab98fSJed Brown 
460988ed4aceSMatthew G Knepley   Input Parameter:
4610bb7acecfSBarry Smith . dm - The `DM`
461188ed4aceSMatthew G Knepley 
461288ed4aceSMatthew G Knepley   Output Parameter:
4613bb7acecfSBarry Smith . section - The `PetscSection`
461488ed4aceSMatthew G Knepley 
461588ed4aceSMatthew G Knepley   Level: intermediate
461688ed4aceSMatthew G Knepley 
4617bb7acecfSBarry Smith   Note:
4618bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
461988ed4aceSMatthew G Knepley 
46201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
462188ed4aceSMatthew G Knepley @*/
4622d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4623d71ae5a4SJacob Faibussowitsch {
462488ed4aceSMatthew G Knepley   PetscFunctionBegin;
462588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46264f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46271bb6d2a8SBarry Smith   if (!dm->globalSection) {
4628fd59a867SMatthew G. Knepley     PetscSection s;
46296725e60dSJed Brown     PetscSF      sf;
4630fd59a867SMatthew G. Knepley 
46319566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46327a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46337a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46345f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4635eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46369566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46379566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46389566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
463988ed4aceSMatthew G Knepley   }
46401bb6d2a8SBarry Smith   *section = dm->globalSection;
46413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
464288ed4aceSMatthew G Knepley }
464388ed4aceSMatthew G Knepley 
4644b21d0597SMatthew G Knepley /*@
4645bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4646b21d0597SMatthew G Knepley 
4647b21d0597SMatthew G Knepley   Input Parameters:
4648bb7acecfSBarry Smith + dm      - The `DM`
46492fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4650b21d0597SMatthew G Knepley 
4651b21d0597SMatthew G Knepley   Level: intermediate
4652b21d0597SMatthew G Knepley 
4653bb7acecfSBarry Smith   Note:
4654bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4655b21d0597SMatthew G Knepley 
46561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4657b21d0597SMatthew G Knepley @*/
4658d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4659d71ae5a4SJacob Faibussowitsch {
4660b21d0597SMatthew G Knepley   PetscFunctionBegin;
4661b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46625080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46649566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46651bb6d2a8SBarry Smith   dm->globalSection = section;
4666497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46679566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4668507e4973SMatthew G. Knepley #endif
466950350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
467050350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
467152947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
467250350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
467350350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4675b21d0597SMatthew G Knepley }
4676b21d0597SMatthew G Knepley 
467788ed4aceSMatthew G Knepley /*@
4678bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4679bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
468088ed4aceSMatthew G Knepley 
468188ed4aceSMatthew G Knepley   Input Parameter:
4682bb7acecfSBarry Smith . dm - The `DM`
468388ed4aceSMatthew G Knepley 
468488ed4aceSMatthew G Knepley   Output Parameter:
4685bb7acecfSBarry Smith . sf - The `PetscSF`
468688ed4aceSMatthew G Knepley 
468788ed4aceSMatthew G Knepley   Level: intermediate
468888ed4aceSMatthew G Knepley 
4689bb7acecfSBarry Smith   Note:
4690bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
469188ed4aceSMatthew G Knepley 
46921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
469388ed4aceSMatthew G Knepley @*/
4694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4695d71ae5a4SJacob Faibussowitsch {
469688ed4aceSMatthew G Knepley   PetscInt nroots;
469788ed4aceSMatthew G Knepley 
469888ed4aceSMatthew G Knepley   PetscFunctionBegin;
469988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47004f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
470148a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47029566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
470388ed4aceSMatthew G Knepley   if (nroots < 0) {
470488ed4aceSMatthew G Knepley     PetscSection section, gSection;
470588ed4aceSMatthew G Knepley 
47069566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
470731ea6d37SMatthew G Knepley     if (section) {
47089566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47099566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
471031ea6d37SMatthew G Knepley     } else {
47110298fd71SBarry Smith       *sf = NULL;
47123ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
471331ea6d37SMatthew G Knepley     }
471488ed4aceSMatthew G Knepley   }
47151bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
471788ed4aceSMatthew G Knepley }
471888ed4aceSMatthew G Knepley 
471988ed4aceSMatthew G Knepley /*@
4720bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
472188ed4aceSMatthew G Knepley 
472288ed4aceSMatthew G Knepley   Input Parameters:
4723bb7acecfSBarry Smith + dm - The `DM`
4724bb7acecfSBarry Smith - sf - The `PetscSF`
472588ed4aceSMatthew G Knepley 
472688ed4aceSMatthew G Knepley   Level: intermediate
472788ed4aceSMatthew G Knepley 
4728bb7acecfSBarry Smith   Note:
4729bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
473088ed4aceSMatthew G Knepley 
47311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
473288ed4aceSMatthew G Knepley @*/
4733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4734d71ae5a4SJacob Faibussowitsch {
473588ed4aceSMatthew G Knepley   PetscFunctionBegin;
473688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4737b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47399566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47401bb6d2a8SBarry Smith   dm->sectionSF = sf;
47413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
474288ed4aceSMatthew G Knepley }
474388ed4aceSMatthew G Knepley 
4744cc4c1da9SBarry Smith /*@
4745bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
474688ed4aceSMatthew G Knepley   describing the data layout.
474788ed4aceSMatthew G Knepley 
474888ed4aceSMatthew G Knepley   Input Parameters:
4749bb7acecfSBarry Smith + dm            - The `DM`
4750bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4751bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
475288ed4aceSMatthew G Knepley 
47531bb6d2a8SBarry Smith   Level: developer
47541bb6d2a8SBarry Smith 
4755bb7acecfSBarry Smith   Note:
4756bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4757bb7acecfSBarry Smith 
475873ff1848SBarry Smith   Developer Note:
4759bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4760bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4761b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4762b6971eaeSBarry Smith   they create, this returns nothing
47631bb6d2a8SBarry Smith 
47641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
476588ed4aceSMatthew G Knepley @*/
4766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4767d71ae5a4SJacob Faibussowitsch {
476888ed4aceSMatthew G Knepley   PetscFunctionBegin;
476988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47709566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
477288ed4aceSMatthew G Knepley }
4773af122d2aSMatthew G Knepley 
4774b21d0597SMatthew G Knepley /*@
4775bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4776bb7acecfSBarry Smith 
4777bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4778b21d0597SMatthew G Knepley 
4779b21d0597SMatthew G Knepley   Input Parameter:
4780bb7acecfSBarry Smith . dm - The `DM`
4781b21d0597SMatthew G Knepley 
4782b21d0597SMatthew G Knepley   Output Parameter:
4783bb7acecfSBarry Smith . sf - The `PetscSF`
4784b21d0597SMatthew G Knepley 
4785b21d0597SMatthew G Knepley   Level: intermediate
4786b21d0597SMatthew G Knepley 
4787bb7acecfSBarry Smith   Note:
4788bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4789b21d0597SMatthew G Knepley 
47901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4791b21d0597SMatthew G Knepley @*/
4792d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4793d71ae5a4SJacob Faibussowitsch {
4794b21d0597SMatthew G Knepley   PetscFunctionBegin;
4795b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47964f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4797b21d0597SMatthew G Knepley   *sf = dm->sf;
47983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4799b21d0597SMatthew G Knepley }
4800b21d0597SMatthew G Knepley 
4801057b4bcdSMatthew G Knepley /*@
4802bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4803bb7acecfSBarry Smith 
480420f4b53cSBarry Smith   Collective
4805057b4bcdSMatthew G Knepley 
4806057b4bcdSMatthew G Knepley   Input Parameters:
4807bb7acecfSBarry Smith + dm - The `DM`
4808bb7acecfSBarry Smith - sf - The `PetscSF`
4809057b4bcdSMatthew G Knepley 
4810057b4bcdSMatthew G Knepley   Level: intermediate
4811057b4bcdSMatthew G Knepley 
48121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4813057b4bcdSMatthew G Knepley @*/
4814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4815d71ae5a4SJacob Faibussowitsch {
4816057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4817057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4818b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48209566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4821057b4bcdSMatthew G Knepley   dm->sf = sf;
48223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4823057b4bcdSMatthew G Knepley }
4824057b4bcdSMatthew G Knepley 
48254f37162bSMatthew G. Knepley /*@
4826bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48274f37162bSMatthew G. Knepley 
48284f37162bSMatthew G. Knepley   Input Parameter:
4829bb7acecfSBarry Smith . dm - The `DM`
48304f37162bSMatthew G. Knepley 
48314f37162bSMatthew G. Knepley   Output Parameter:
4832bb7acecfSBarry Smith . sf - The `PetscSF`
48334f37162bSMatthew G. Knepley 
48344f37162bSMatthew G. Knepley   Level: intermediate
48354f37162bSMatthew G. Knepley 
4836bb7acecfSBarry Smith   Note:
4837bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48384f37162bSMatthew G. Knepley 
48391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48404f37162bSMatthew G. Knepley @*/
4841d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4842d71ae5a4SJacob Faibussowitsch {
48434f37162bSMatthew G. Knepley   PetscFunctionBegin;
48444f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48454f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48464f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48484f37162bSMatthew G. Knepley }
48494f37162bSMatthew G. Knepley 
48504f37162bSMatthew G. Knepley /*@
48514f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48524f37162bSMatthew G. Knepley 
48534f37162bSMatthew G. Knepley   Input Parameters:
48544f37162bSMatthew G. Knepley + dm - The DM
48554f37162bSMatthew G. Knepley - sf - The PetscSF
48564f37162bSMatthew G. Knepley 
48574f37162bSMatthew G. Knepley   Level: intermediate
48584f37162bSMatthew G. Knepley 
48591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48604f37162bSMatthew G. Knepley @*/
4861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4862d71ae5a4SJacob Faibussowitsch {
48634f37162bSMatthew G. Knepley   PetscFunctionBegin;
48644f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48654f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48679566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48684f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48704f37162bSMatthew G. Knepley }
48714f37162bSMatthew G. Knepley 
4872d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4873d71ae5a4SJacob Faibussowitsch {
487434aa8a36SMatthew G. Knepley   PetscClassId id;
487534aa8a36SMatthew G. Knepley 
487634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
487834aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48799566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488034aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48819566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
488217c1d62eSMatthew G. Knepley   } else {
48839566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
488434aa8a36SMatthew G. Knepley   }
48853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
488634aa8a36SMatthew G. Knepley }
488734aa8a36SMatthew G. Knepley 
4888d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4889d71ae5a4SJacob Faibussowitsch {
489044a7f3ddSMatthew G. Knepley   RegionField *tmpr;
489144a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
489244a7f3ddSMatthew G. Knepley 
489344a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
48943ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
48959566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
489644a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48979371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48989371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48999371c9d4SSatish Balay     tmpr[f].label       = NULL;
49009371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49019371c9d4SSatish Balay   }
49029566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
490344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
490444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490644a7f3ddSMatthew G. Knepley }
490744a7f3ddSMatthew G. Knepley 
490844a7f3ddSMatthew G. Knepley /*@
490920f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
491044a7f3ddSMatthew G. Knepley 
491120f4b53cSBarry Smith   Logically Collective
491244a7f3ddSMatthew G. Knepley 
491344a7f3ddSMatthew G. Knepley   Input Parameter:
491420f4b53cSBarry Smith . dm - The `DM`
491544a7f3ddSMatthew G. Knepley 
491644a7f3ddSMatthew G. Knepley   Level: intermediate
491744a7f3ddSMatthew G. Knepley 
49181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
491944a7f3ddSMatthew G. Knepley @*/
4920d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4921d71ae5a4SJacob Faibussowitsch {
492244a7f3ddSMatthew G. Knepley   PetscInt f;
492344a7f3ddSMatthew G. Knepley 
492444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
492544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
492644a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49279566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49289566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
492944a7f3ddSMatthew G. Knepley   }
49309566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
493144a7f3ddSMatthew G. Knepley   dm->fields = NULL;
493244a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
493444a7f3ddSMatthew G. Knepley }
493544a7f3ddSMatthew G. Knepley 
4936689b5837SMatthew G. Knepley /*@
493720f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4938689b5837SMatthew G. Knepley 
493920f4b53cSBarry Smith   Not Collective
4940689b5837SMatthew G. Knepley 
4941689b5837SMatthew G. Knepley   Input Parameter:
494220f4b53cSBarry Smith . dm - The `DM`
4943689b5837SMatthew G. Knepley 
4944689b5837SMatthew G. Knepley   Output Parameter:
494560225df5SJacob Faibussowitsch . numFields - The number of fields
4946689b5837SMatthew G. Knepley 
4947689b5837SMatthew G. Knepley   Level: intermediate
4948689b5837SMatthew G. Knepley 
49491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4950689b5837SMatthew G. Knepley @*/
4951d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4952d71ae5a4SJacob Faibussowitsch {
49530f21e855SMatthew G. Knepley   PetscFunctionBegin;
49540f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49554f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
495644a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4958af122d2aSMatthew G Knepley }
4959af122d2aSMatthew G Knepley 
4960689b5837SMatthew G. Knepley /*@
496120f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4962689b5837SMatthew G. Knepley 
496320f4b53cSBarry Smith   Logically Collective
4964689b5837SMatthew G. Knepley 
4965689b5837SMatthew G. Knepley   Input Parameters:
496620f4b53cSBarry Smith + dm        - The `DM`
496760225df5SJacob Faibussowitsch - numFields - The number of fields
4968689b5837SMatthew G. Knepley 
4969689b5837SMatthew G. Knepley   Level: intermediate
4970689b5837SMatthew G. Knepley 
49711cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4972689b5837SMatthew G. Knepley @*/
4973d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4974d71ae5a4SJacob Faibussowitsch {
49750f21e855SMatthew G. Knepley   PetscInt Nf, f;
4976af122d2aSMatthew G Knepley 
4977af122d2aSMatthew G Knepley   PetscFunctionBegin;
4978af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49799566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49800f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49810f21e855SMatthew G. Knepley     PetscContainer obj;
49820f21e855SMatthew G. Knepley 
49839566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49849566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49859566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4986af122d2aSMatthew G Knepley   }
49873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4988af122d2aSMatthew G Knepley }
4989af122d2aSMatthew G Knepley 
4990c1929be8SMatthew G. Knepley /*@
4991bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4992c1929be8SMatthew G. Knepley 
499320f4b53cSBarry Smith   Not Collective
4994c1929be8SMatthew G. Knepley 
4995c1929be8SMatthew G. Knepley   Input Parameters:
4996bb7acecfSBarry Smith + dm - The `DM`
4997c1929be8SMatthew G. Knepley - f  - The field number
4998c1929be8SMatthew G. Knepley 
499944a7f3ddSMatthew G. Knepley   Output Parameters:
500020f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
500120f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5002c1929be8SMatthew G. Knepley 
500344a7f3ddSMatthew G. Knepley   Level: intermediate
5004c1929be8SMatthew G. Knepley 
50051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5006c1929be8SMatthew G. Knepley @*/
5007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5008d71ae5a4SJacob Faibussowitsch {
5009af122d2aSMatthew G Knepley   PetscFunctionBegin;
5010af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50114f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50127a8be351SBarry 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);
501344a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5014bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5016decb47aaSMatthew G. Knepley }
5017decb47aaSMatthew G. Knepley 
5018083401c6SMatthew G. Knepley /* Does not clear the DS */
5019d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5020d71ae5a4SJacob Faibussowitsch {
5021083401c6SMatthew G. Knepley   PetscFunctionBegin;
50229566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50239566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50249566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5025083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5026bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50279566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5028bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5030083401c6SMatthew G. Knepley }
5031083401c6SMatthew G. Knepley 
5032ffeef943SBarry Smith /*@
5033bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5034bb7acecfSBarry Smith   the field numbering.
5035c1929be8SMatthew G. Knepley 
503620f4b53cSBarry Smith   Logically Collective
5037c1929be8SMatthew G. Knepley 
5038c1929be8SMatthew G. Knepley   Input Parameters:
5039bb7acecfSBarry Smith + dm    - The `DM`
5040c1929be8SMatthew G. Knepley . f     - The field number
504120f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5042bb7acecfSBarry Smith - disc  - The discretization object
5043c1929be8SMatthew G. Knepley 
504444a7f3ddSMatthew G. Knepley   Level: intermediate
5045c1929be8SMatthew G. Knepley 
50461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5047c1929be8SMatthew G. Knepley @*/
5048d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5049d71ae5a4SJacob Faibussowitsch {
5050decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5051decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5052e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5053bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50547a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5055bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5056bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50579566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
505944a7f3ddSMatthew G. Knepley }
506044a7f3ddSMatthew G. Knepley 
5061ffeef943SBarry Smith /*@
5062bb7acecfSBarry 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)
5063bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
506444a7f3ddSMatthew G. Knepley 
506520f4b53cSBarry Smith   Logically Collective
506644a7f3ddSMatthew G. Knepley 
506744a7f3ddSMatthew G. Knepley   Input Parameters:
5068bb7acecfSBarry Smith + dm    - The `DM`
506920f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5070bb7acecfSBarry Smith - disc  - The discretization object
507144a7f3ddSMatthew G. Knepley 
507244a7f3ddSMatthew G. Knepley   Level: intermediate
507344a7f3ddSMatthew G. Knepley 
5074bb7acecfSBarry Smith   Notes:
5075bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5076bb7acecfSBarry Smith 
5077da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5078bb7acecfSBarry 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
5079bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5080bb7acecfSBarry Smith 
50811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
508244a7f3ddSMatthew G. Knepley @*/
5083d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5084d71ae5a4SJacob Faibussowitsch {
508544a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
508644a7f3ddSMatthew G. Knepley 
508744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
508844a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5089064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5090bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50919566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
509244a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5093bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50949566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5095bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5096bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50979566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5099af122d2aSMatthew G Knepley }
51006636e97aSMatthew G Knepley 
5101e5e52638SMatthew G. Knepley /*@
5102e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5103e0b68406SMatthew Knepley 
510420f4b53cSBarry Smith   Logically Collective
5105e0b68406SMatthew Knepley 
5106e0b68406SMatthew Knepley   Input Parameters:
5107bb7acecfSBarry Smith + dm          - The `DM`
5108e0b68406SMatthew Knepley . f           - The field index
5109bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5110e0b68406SMatthew Knepley 
5111e0b68406SMatthew Knepley   Level: intermediate
5112e0b68406SMatthew Knepley 
51131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5114e0b68406SMatthew Knepley @*/
5115d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5116d71ae5a4SJacob Faibussowitsch {
5117e0b68406SMatthew Knepley   PetscFunctionBegin;
511863a3b9bcSJacob 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);
5119e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5121e0b68406SMatthew Knepley }
5122e0b68406SMatthew Knepley 
5123e0b68406SMatthew Knepley /*@
5124e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5125e0b68406SMatthew Knepley 
512620f4b53cSBarry Smith   Not Collective
5127e0b68406SMatthew Knepley 
5128e0b68406SMatthew Knepley   Input Parameters:
5129bb7acecfSBarry Smith + dm - The `DM`
5130e0b68406SMatthew Knepley - f  - The field index
5131e0b68406SMatthew Knepley 
5132e0b68406SMatthew Knepley   Output Parameter:
5133e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5134e0b68406SMatthew Knepley 
5135e0b68406SMatthew Knepley   Level: intermediate
5136e0b68406SMatthew Knepley 
513760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5138e0b68406SMatthew Knepley @*/
5139d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5140d71ae5a4SJacob Faibussowitsch {
5141e0b68406SMatthew Knepley   PetscFunctionBegin;
514263a3b9bcSJacob 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);
5143e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5145e0b68406SMatthew Knepley }
5146e0b68406SMatthew Knepley 
5147e0b68406SMatthew Knepley /*@
5148bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5149e5e52638SMatthew G. Knepley 
515020f4b53cSBarry Smith   Collective
5151e5e52638SMatthew G. Knepley 
5152e5e52638SMatthew G. Knepley   Input Parameter:
5153bb7acecfSBarry Smith . dm - The `DM`
5154e5e52638SMatthew G. Knepley 
5155e5e52638SMatthew G. Knepley   Output Parameter:
5156bb7acecfSBarry Smith . newdm - The `DM`
5157e5e52638SMatthew G. Knepley 
5158e5e52638SMatthew G. Knepley   Level: advanced
5159e5e52638SMatthew G. Knepley 
51601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5161e5e52638SMatthew G. Knepley @*/
5162d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5163d71ae5a4SJacob Faibussowitsch {
5164e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5165e5e52638SMatthew G. Knepley 
5166e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51673ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51689566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51699566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5170e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5171e5e52638SMatthew G. Knepley     DMLabel     label;
5172e5e52638SMatthew G. Knepley     PetscObject field;
517334aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5174e5e52638SMatthew G. Knepley 
51759566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51769566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51779566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51789566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
517934aa8a36SMatthew G. Knepley   }
51803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
518134aa8a36SMatthew G. Knepley }
518234aa8a36SMatthew G. Knepley 
518334aa8a36SMatthew G. Knepley /*@
518434aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
518534aa8a36SMatthew G. Knepley 
518620f4b53cSBarry Smith   Not Collective
518734aa8a36SMatthew G. Knepley 
518834aa8a36SMatthew G. Knepley   Input Parameters:
518920f4b53cSBarry Smith + dm - The `DM` object
519020f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
519134aa8a36SMatthew G. Knepley 
5192d8d19677SJose E. Roman   Output Parameters:
519334aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
519434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
519534aa8a36SMatthew G. Knepley 
519634aa8a36SMatthew G. Knepley   Level: developer
519734aa8a36SMatthew G. Knepley 
519820f4b53cSBarry Smith   Notes:
519920f4b53cSBarry Smith .vb
520020f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
520120f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
520220f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
520320f4b53cSBarry Smith .ve
520420f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
520520f4b53cSBarry Smith 
52061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
520734aa8a36SMatthew G. Knepley @*/
5208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5209d71ae5a4SJacob Faibussowitsch {
521034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
521134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52124f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52134f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
521434aa8a36SMatthew G. Knepley   if (f < 0) {
521534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
521634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
521734aa8a36SMatthew G. Knepley   } else {
521834aa8a36SMatthew G. Knepley     PetscInt Nf;
521934aa8a36SMatthew G. Knepley 
52209566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52217a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
522234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
522334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
522434aa8a36SMatthew G. Knepley   }
52253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
522634aa8a36SMatthew G. Knepley }
522734aa8a36SMatthew G. Knepley 
522834aa8a36SMatthew G. Knepley /*@
522934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
523034aa8a36SMatthew G. Knepley 
523120f4b53cSBarry Smith   Not Collective
523234aa8a36SMatthew G. Knepley 
523334aa8a36SMatthew G. Knepley   Input Parameters:
523420f4b53cSBarry Smith + dm         - The `DM` object
523534aa8a36SMatthew G. Knepley . f          - The field number
523634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
523734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
523834aa8a36SMatthew G. Knepley 
523934aa8a36SMatthew G. Knepley   Level: developer
524034aa8a36SMatthew G. Knepley 
524120f4b53cSBarry Smith   Notes:
524220f4b53cSBarry Smith .vb
524320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
524420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
524520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
524620f4b53cSBarry Smith .ve
524720f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
524820f4b53cSBarry Smith 
52491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
525034aa8a36SMatthew G. Knepley @*/
5251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5252d71ae5a4SJacob Faibussowitsch {
525334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
525434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
525534aa8a36SMatthew G. Knepley   if (f < 0) {
525634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
525734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
525834aa8a36SMatthew G. Knepley   } else {
525934aa8a36SMatthew G. Knepley     PetscInt Nf;
526034aa8a36SMatthew G. Knepley 
52619566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52627a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
526334aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
526434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5265e5e52638SMatthew G. Knepley   }
52663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5267e5e52638SMatthew G. Knepley }
5268e5e52638SMatthew G. Knepley 
5269b0441da4SMatthew G. Knepley /*@
5270b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5271b0441da4SMatthew G. Knepley 
5272b0441da4SMatthew G. Knepley   Not collective
5273b0441da4SMatthew G. Knepley 
5274f899ff85SJose E. Roman   Input Parameter:
527520f4b53cSBarry Smith . dm - The `DM` object
5276b0441da4SMatthew G. Knepley 
5277d8d19677SJose E. Roman   Output Parameters:
5278b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5279b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5280b0441da4SMatthew G. Knepley 
5281b0441da4SMatthew G. Knepley   Level: developer
5282b0441da4SMatthew G. Knepley 
528320f4b53cSBarry Smith   Notes:
528420f4b53cSBarry Smith .vb
528520f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
528620f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
528720f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
528820f4b53cSBarry Smith .ve
528920f4b53cSBarry Smith 
52901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5291b0441da4SMatthew G. Knepley @*/
5292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5293d71ae5a4SJacob Faibussowitsch {
5294b0441da4SMatthew G. Knepley   PetscInt Nf;
5295b0441da4SMatthew G. Knepley 
5296b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5297b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52984f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
52994f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53009566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5301b0441da4SMatthew G. Knepley   if (!Nf) {
53029566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5303b0441da4SMatthew G. Knepley   } else {
53049566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5305b0441da4SMatthew G. Knepley   }
53063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5307b0441da4SMatthew G. Knepley }
5308b0441da4SMatthew G. Knepley 
5309b0441da4SMatthew G. Knepley /*@
5310b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5311b0441da4SMatthew G. Knepley 
531220f4b53cSBarry Smith   Not Collective
5313b0441da4SMatthew G. Knepley 
5314b0441da4SMatthew G. Knepley   Input Parameters:
531520f4b53cSBarry Smith + dm         - The `DM` object
5316b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5317b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5318b0441da4SMatthew G. Knepley 
5319b0441da4SMatthew G. Knepley   Level: developer
5320b0441da4SMatthew G. Knepley 
532120f4b53cSBarry Smith   Notes:
532220f4b53cSBarry Smith .vb
532320f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
532420f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
532520f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
532620f4b53cSBarry Smith .ve
532720f4b53cSBarry Smith 
53281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5329b0441da4SMatthew G. Knepley @*/
5330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5331d71ae5a4SJacob Faibussowitsch {
5332b0441da4SMatthew G. Knepley   PetscInt Nf;
5333b0441da4SMatthew G. Knepley 
5334b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5335b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53369566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5337b0441da4SMatthew G. Knepley   if (!Nf) {
53389566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5339b0441da4SMatthew G. Knepley   } else {
53409566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5341e5e52638SMatthew G. Knepley   }
53423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5343e5e52638SMatthew G. Knepley }
5344e5e52638SMatthew G. Knepley 
5345d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5346d71ae5a4SJacob Faibussowitsch {
5347799db056SMatthew G. Knepley   DM           plex;
5348799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5349799db056SMatthew G. Knepley   const char **names;
5350799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5351799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5352799db056SMatthew G. Knepley   size_t       len;
5353799db056SMatthew G. Knepley   MPI_Comm     comm;
5354799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5355783e2ec8SMatthew G. Knepley 
5356783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5357799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5358799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5359799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5360799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5361799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5362799db056SMatthew G. Knepley     PetscDS  dsBC;
5363799db056SMatthew G. Knepley     PetscInt numBd;
5364799db056SMatthew G. Knepley 
536507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5366799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5367799db056SMatthew G. Knepley     maxLabels += numBd;
5368799db056SMatthew G. Knepley   }
5369799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5370799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5371799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5372799db056SMatthew G. Knepley     PetscDS  dsBC;
5373799db056SMatthew G. Knepley     PetscInt numBd, bd;
5374799db056SMatthew G. Knepley 
537507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5376799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5377799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5378799db056SMatthew G. Knepley       DMLabel      label;
5379799db056SMatthew G. Knepley       PetscInt     field;
5380799db056SMatthew G. Knepley       PetscObject  obj;
5381799db056SMatthew G. Knepley       PetscClassId id;
5382799db056SMatthew G. Knepley 
5383799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53849566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53859566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5386799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53879371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53889371c9d4SSatish Balay         if (labels[l] == label) break;
5389799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5390783e2ec8SMatthew G. Knepley     }
5391799db056SMatthew G. Knepley   }
5392799db056SMatthew G. Knepley   /* Get label names */
5393799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5394799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53959371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53969371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53979371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53989371c9d4SSatish Balay   }
5399799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5400712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5401799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5402c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5403799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5404799db056SMatthew G. Knepley   /* Put all names on all processes */
5405799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5406799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5407799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5408799db056SMatthew G. Knepley   gNl = displs[size];
54099371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54109371c9d4SSatish Balay     counts[p] *= gmaxLen;
54119371c9d4SSatish Balay     displs[p] *= gmaxLen;
54129371c9d4SSatish Balay   }
5413799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5414799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5415799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5416799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5417799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5418799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5419799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54209371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54219371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
54229566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5423799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54249566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5425799db056SMatthew G. Knepley     ++gl;
5426783e2ec8SMatthew G. Knepley   }
5427799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5429783e2ec8SMatthew G. Knepley }
5430783e2ec8SMatthew G. Knepley 
5431d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5432d71ae5a4SJacob Faibussowitsch {
5433e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5434e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5435e5e52638SMatthew G. Knepley 
5436e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54373ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54389566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5439e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54409371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54419371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54429371c9d4SSatish Balay     tmpd[s].label  = NULL;
54439371c9d4SSatish Balay     tmpd[s].fields = NULL;
54449371c9d4SSatish Balay   }
54459566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5446e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5447e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5449e5e52638SMatthew G. Knepley }
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley /*@
545220f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5453e5e52638SMatthew G. Knepley 
545420f4b53cSBarry Smith   Not Collective
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Input Parameter:
545720f4b53cSBarry Smith . dm - The `DM`
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   Output Parameter:
546020f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5461e5e52638SMatthew G. Knepley 
5462e5e52638SMatthew G. Knepley   Level: intermediate
5463e5e52638SMatthew G. Knepley 
54641cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5465e5e52638SMatthew G. Knepley @*/
5466d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5467d71ae5a4SJacob Faibussowitsch {
5468e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5469e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54704f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5471e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
54723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5473e5e52638SMatthew G. Knepley }
5474e5e52638SMatthew G. Knepley 
5475e5e52638SMatthew G. Knepley /*@
547620f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5477e5e52638SMatthew G. Knepley 
547820f4b53cSBarry Smith   Logically Collective
5479e5e52638SMatthew G. Knepley 
5480e5e52638SMatthew G. Knepley   Input Parameter:
548120f4b53cSBarry Smith . dm - The `DM`
5482e5e52638SMatthew G. Knepley 
5483e5e52638SMatthew G. Knepley   Level: intermediate
5484e5e52638SMatthew G. Knepley 
54851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5486e5e52638SMatthew G. Knepley @*/
5487d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5488d71ae5a4SJacob Faibussowitsch {
5489e5e52638SMatthew G. Knepley   PetscInt s;
5490e5e52638SMatthew G. Knepley 
5491e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5492e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5493e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54949566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
549507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
54969566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54979566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5498e5e52638SMatthew G. Knepley   }
54999566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5500e5e52638SMatthew G. Knepley   dm->probs = NULL;
5501e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5503e5e52638SMatthew G. Knepley }
5504e5e52638SMatthew G. Knepley 
5505e5e52638SMatthew G. Knepley /*@
550620f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5507e5e52638SMatthew G. Knepley 
550820f4b53cSBarry Smith   Not Collective
5509e5e52638SMatthew G. Knepley 
5510e5e52638SMatthew G. Knepley   Input Parameter:
551120f4b53cSBarry Smith . dm - The `DM`
5512e5e52638SMatthew G. Knepley 
5513e5e52638SMatthew G. Knepley   Output Parameter:
551407218a29SMatthew G. Knepley . ds - The default `PetscDS`
5515e5e52638SMatthew G. Knepley 
5516e5e52638SMatthew G. Knepley   Level: intermediate
5517e5e52638SMatthew G. Knepley 
55181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5519e5e52638SMatthew G. Knepley @*/
552007218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5521d71ae5a4SJacob Faibussowitsch {
5522e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5523e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55244f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
552507218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
552607218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5528e5e52638SMatthew G. Knepley }
5529e5e52638SMatthew G. Knepley 
5530e5e52638SMatthew G. Knepley /*@
553120f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5532e5e52638SMatthew G. Knepley 
553320f4b53cSBarry Smith   Not Collective
5534e5e52638SMatthew G. Knepley 
5535e5e52638SMatthew G. Knepley   Input Parameters:
553620f4b53cSBarry Smith + dm    - The `DM`
553720f4b53cSBarry Smith - point - Cell for the `PetscDS`
5538e5e52638SMatthew G. Knepley 
553907218a29SMatthew G. Knepley   Output Parameters:
554007218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
554107218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5542e5e52638SMatthew G. Knepley 
5543e5e52638SMatthew G. Knepley   Level: developer
5544e5e52638SMatthew G. Knepley 
55451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5546e5e52638SMatthew G. Knepley @*/
554707218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5548d71ae5a4SJacob Faibussowitsch {
554907218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5550e5e52638SMatthew G. Knepley   PetscInt s;
5551e5e52638SMatthew G. Knepley 
5552e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5553e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55544f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55554f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
555663a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
555707218a29SMatthew G. Knepley   if (ds) *ds = NULL;
555807218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5559e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5560e5e52638SMatthew G. Knepley     PetscInt val;
5561e5e52638SMatthew G. Knepley 
55629371c9d4SSatish Balay     if (!dm->probs[s].label) {
556307218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55649371c9d4SSatish Balay     } else {
55659566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55669371c9d4SSatish Balay       if (val >= 0) {
556707218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
556807218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55699371c9d4SSatish Balay         break;
55709371c9d4SSatish Balay       }
5571e5e52638SMatthew G. Knepley     }
5572e5e52638SMatthew G. Knepley   }
557307218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
55743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5575e5e52638SMatthew G. Knepley }
5576e5e52638SMatthew G. Knepley 
5577e5e52638SMatthew G. Knepley /*@
557820f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5579e5e52638SMatthew G. Knepley 
558020f4b53cSBarry Smith   Not Collective
5581e5e52638SMatthew G. Knepley 
5582e5e52638SMatthew G. Knepley   Input Parameters:
558320f4b53cSBarry Smith + dm    - The `DM`
558420f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5585e5e52638SMatthew G. Knepley 
5586b3cf3223SMatthew G. Knepley   Output Parameters:
558720f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
558807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
558907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5590e5e52638SMatthew G. Knepley 
5591e5e52638SMatthew G. Knepley   Level: advanced
5592e5e52638SMatthew G. Knepley 
559320f4b53cSBarry Smith   Note:
559420f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
559520f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
559607218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
559720f4b53cSBarry Smith 
55981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5599e5e52638SMatthew G. Knepley @*/
560007218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5601d71ae5a4SJacob Faibussowitsch {
5602e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5603e5e52638SMatthew G. Knepley 
5604e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5605e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5606e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56079371c9d4SSatish Balay   if (fields) {
56084f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56099371c9d4SSatish Balay     *fields = NULL;
56109371c9d4SSatish Balay   }
56119371c9d4SSatish Balay   if (ds) {
56124f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56139371c9d4SSatish Balay     *ds = NULL;
56149371c9d4SSatish Balay   }
561507218a29SMatthew G. Knepley   if (dsIn) {
56164f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
561707218a29SMatthew G. Knepley     *dsIn = NULL;
561807218a29SMatthew G. Knepley   }
5619e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5620154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5621b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5622b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
562307218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56243ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5625b3cf3223SMatthew G. Knepley     }
5626e5e52638SMatthew G. Knepley   }
56273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5628e5e52638SMatthew G. Knepley }
5629e5e52638SMatthew G. Knepley 
5630e5e52638SMatthew G. Knepley /*@
5631bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5632083401c6SMatthew G. Knepley 
563320f4b53cSBarry Smith   Collective
5634083401c6SMatthew G. Knepley 
5635083401c6SMatthew G. Knepley   Input Parameters:
5636bb7acecfSBarry Smith + dm     - The `DM`
563720f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
563807218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
563907218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
564007218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5641083401c6SMatthew G. Knepley 
564220f4b53cSBarry Smith   Level: advanced
564320f4b53cSBarry Smith 
5644bb7acecfSBarry Smith   Note:
5645bb7acecfSBarry 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,
5646083401c6SMatthew G. Knepley   the fields argument is ignored.
5647083401c6SMatthew G. Knepley 
56481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5649083401c6SMatthew G. Knepley @*/
565007218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5651d71ae5a4SJacob Faibussowitsch {
5652083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5653083401c6SMatthew G. Knepley 
5654083401c6SMatthew G. Knepley   PetscFunctionBegin;
5655083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5656083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
565707218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5658064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
565907218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5660083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5661083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56629566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
566307218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5664083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
566507218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56663ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5667083401c6SMatthew G. Knepley     }
5668083401c6SMatthew G. Knepley   }
56699566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56709566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
567307218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5674083401c6SMatthew G. Knepley   if (!label) {
5675083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5676083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5677083401c6SMatthew G. Knepley     Nds = 0;
5678083401c6SMatthew G. Knepley   }
5679083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5680083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5681083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
568207218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
56833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5684083401c6SMatthew G. Knepley }
5685083401c6SMatthew G. Knepley 
5686083401c6SMatthew G. Knepley /*@
568720f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5688e5e52638SMatthew G. Knepley 
568920f4b53cSBarry Smith   Not Collective
5690e5e52638SMatthew G. Knepley 
5691e5e52638SMatthew G. Knepley   Input Parameters:
569220f4b53cSBarry Smith + dm  - The `DM`
5693e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5694e5e52638SMatthew G. Knepley 
5695e5e52638SMatthew G. Knepley   Output Parameters:
569620f4b53cSBarry Smith + label  - The region label, or `NULL`
569720f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
569807218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
569907218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5700e5e52638SMatthew G. Knepley 
5701e5e52638SMatthew G. Knepley   Level: advanced
5702e5e52638SMatthew G. Knepley 
57031cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5704e5e52638SMatthew G. Knepley @*/
570507218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5706d71ae5a4SJacob Faibussowitsch {
5707e5e52638SMatthew G. Knepley   PetscInt Nds;
5708e5e52638SMatthew G. Knepley 
5709e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5710e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57119566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
571263a3b9bcSJacob 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);
5713e5e52638SMatthew G. Knepley   if (label) {
57144f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5715e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5716e5e52638SMatthew G. Knepley   }
5717b3cf3223SMatthew G. Knepley   if (fields) {
57184f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5719b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5720b3cf3223SMatthew G. Knepley   }
5721e5e52638SMatthew G. Knepley   if (ds) {
57224f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5723e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5724e5e52638SMatthew G. Knepley   }
572507218a29SMatthew G. Knepley   if (dsIn) {
57264f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
572707218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
572807218a29SMatthew G. Knepley   }
57293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5730e5e52638SMatthew G. Knepley }
5731e5e52638SMatthew G. Knepley 
5732e5e52638SMatthew G. Knepley /*@
573320f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5734e5e52638SMatthew G. Knepley 
573520f4b53cSBarry Smith   Not Collective
5736e5e52638SMatthew G. Knepley 
5737e5e52638SMatthew G. Knepley   Input Parameters:
573820f4b53cSBarry Smith + dm     - The `DM`
5739083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
574020f4b53cSBarry Smith . label  - The region label, or `NULL`
574107218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
574207218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
574307218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5744e5e52638SMatthew G. Knepley 
5745e5e52638SMatthew G. Knepley   Level: advanced
5746e5e52638SMatthew G. Knepley 
57471cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5748e5e52638SMatthew G. Knepley @*/
574907218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5750d71ae5a4SJacob Faibussowitsch {
5751083401c6SMatthew G. Knepley   PetscInt Nds;
5752e5e52638SMatthew G. Knepley 
5753e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5754e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5755ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57569566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
575763a3b9bcSJacob 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);
57589566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57599566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5760083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5761083401c6SMatthew G. Knepley   if (fields) {
5762083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57639566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5765083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5766e5e52638SMatthew G. Knepley   }
5767083401c6SMatthew G. Knepley   if (ds) {
5768083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57699566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57709566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5771083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5772083401c6SMatthew G. Knepley   }
577307218a29SMatthew G. Knepley   if (dsIn) {
577407218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
577507218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
577607218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
577707218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
577807218a29SMatthew G. Knepley   }
57793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5780e5e52638SMatthew G. Knepley }
5781e5e52638SMatthew G. Knepley 
5782e5e52638SMatthew G. Knepley /*@
578320f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
57841d3af9e0SMatthew G. Knepley 
578520f4b53cSBarry Smith   Not Collective
57861d3af9e0SMatthew G. Knepley 
57871d3af9e0SMatthew G. Knepley   Input Parameters:
578820f4b53cSBarry Smith + dm - The `DM`
578920f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
57901d3af9e0SMatthew G. Knepley 
57911d3af9e0SMatthew G. Knepley   Output Parameter:
57921d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57931d3af9e0SMatthew G. Knepley 
57941d3af9e0SMatthew G. Knepley   Level: advanced
57951d3af9e0SMatthew G. Knepley 
57961cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57971d3af9e0SMatthew G. Knepley @*/
5798d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5799d71ae5a4SJacob Faibussowitsch {
58001d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58011d3af9e0SMatthew G. Knepley 
58021d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58031d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58041d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58054f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58069566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58079371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58089371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58091d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58101d3af9e0SMatthew G. Knepley   else *num = n;
58113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58121d3af9e0SMatthew G. Knepley }
58131d3af9e0SMatthew G. Knepley 
5814cc4c1da9SBarry Smith /*@
5815bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58162df84da0SMatthew G. Knepley 
581720f4b53cSBarry Smith   Not Collective
58182df84da0SMatthew G. Knepley 
5819f1a722f8SMatthew G. Knepley   Input Parameters:
5820bb7acecfSBarry Smith + dm     - The `DM`
58212df84da0SMatthew G. Knepley . Nc     - The number of components for the field
582220f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5823bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58242df84da0SMatthew G. Knepley 
58252df84da0SMatthew G. Knepley   Output Parameter:
5826bb7acecfSBarry Smith . fem - The `PetscFE`
58272df84da0SMatthew G. Knepley 
582820f4b53cSBarry Smith   Level: intermediate
582920f4b53cSBarry Smith 
5830bb7acecfSBarry Smith   Note:
5831bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58322df84da0SMatthew G. Knepley 
58331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58342df84da0SMatthew G. Knepley @*/
5835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5836d71ae5a4SJacob Faibussowitsch {
58372df84da0SMatthew G. Knepley   DMPolytopeType ct;
58382df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58392df84da0SMatthew G. Knepley 
58402df84da0SMatthew G. Knepley   PetscFunctionBegin;
58412df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58422df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58434f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58442df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58454f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58499566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58512df84da0SMatthew G. Knepley }
58522df84da0SMatthew G. Knepley 
58531d3af9e0SMatthew G. Knepley /*@
5854bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5855e5e52638SMatthew G. Knepley 
585620f4b53cSBarry Smith   Collective
5857e5e52638SMatthew G. Knepley 
5858e5e52638SMatthew G. Knepley   Input Parameter:
5859bb7acecfSBarry Smith . dm - The `DM`
5860e5e52638SMatthew G. Knepley 
586120f4b53cSBarry Smith   Options Database Key:
5862bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
586345480ffeSMatthew G. Knepley 
586420f4b53cSBarry Smith   Level: intermediate
586520f4b53cSBarry Smith 
58661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5867e5e52638SMatthew G. Knepley @*/
5868d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5869d71ae5a4SJacob Faibussowitsch {
5870e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5871083401c6SMatthew G. Knepley   PetscDS   dsDef;
5872083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5873f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5874f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5875e5e52638SMatthew G. Knepley 
5876e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5877e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58783ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
58799566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5881083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5883083401c6SMatthew G. Knepley   Nl   = 0;
5884083401c6SMatthew G. Knepley   Ndef = 0;
5885083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5886083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5887083401c6SMatthew G. Knepley     PetscInt l;
5888083401c6SMatthew G. Knepley 
5889f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5890f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5891f918ec44SMatthew G. Knepley     {
5892f918ec44SMatthew G. Knepley       PetscClassId id;
5893f918ec44SMatthew G. Knepley 
58949566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5895f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5896f918ec44SMatthew G. Knepley         Ceed ceed;
5897f918ec44SMatthew G. Knepley 
58989566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58999566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5900f918ec44SMatthew G. Knepley       }
5901f918ec44SMatthew G. Knepley     }
5902f918ec44SMatthew G. Knepley #endif
59039371c9d4SSatish Balay     if (!label) {
59049371c9d4SSatish Balay       ++Ndef;
59059371c9d4SSatish Balay       continue;
59069371c9d4SSatish Balay     }
59079371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59089371c9d4SSatish Balay       if (label == labelSet[l]) break;
5909083401c6SMatthew G. Knepley     if (l < Nl) continue;
5910083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5911083401c6SMatthew G. Knepley   }
5912083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
591307218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5914083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5915b3cf3223SMatthew G. Knepley     IS        fields;
5916b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5917b3cf3223SMatthew G. Knepley 
59189371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59199371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59207a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59229371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59239371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59249566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59269566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59279566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
592888f0c812SMatthew G. Knepley 
59299566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
593007218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59319566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59329566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59332df9ee95SMatthew G. Knepley   }
593407218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59359566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5936083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5937083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59380122748bSMatthew G. Knepley     DM              plex;
5939083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5940083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5941083401c6SMatthew G. Knepley     PetscInt       *fields;
5942083401c6SMatthew G. Knepley     const PetscInt *cells;
5943083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59440122748bSMatthew G. Knepley 
59459566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59479566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59489566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59495fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5950083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5951083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5952083401c6SMatthew G. Knepley       IS      pointIS;
5953083401c6SMatthew G. Knepley 
59549566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59559566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59569566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59579566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5958083401c6SMatthew G. Knepley     }
59599566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5960083401c6SMatthew G. Knepley 
59619566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59629566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59639566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59649566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59659566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59669566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59679566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5968083401c6SMatthew G. Knepley 
59699566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59709371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59719371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
59729566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
59739566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
59749566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
59759566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5976083401c6SMatthew G. Knepley 
59779566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
597807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
59799566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59809566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59819566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59829566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59839566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5984083401c6SMatthew G. Knepley   }
5985083401c6SMatthew G. Knepley   /* Create label DSes
5986083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5987083401c6SMatthew G. Knepley   */
5988083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5989083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5990083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
599107218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
5992083401c6SMatthew G. Knepley     IS        fields;
5993083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5994083401c6SMatthew G. Knepley 
59959566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59969371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59979371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59989566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59999371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60009371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60019566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60029566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60039566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60049566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60059566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6006083401c6SMatthew G. Knepley     {
6007083401c6SMatthew G. Knepley       DMPolytopeType ct;
6008083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60095fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60100122748bSMatthew G. Knepley 
60119566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6012665f567fSMatthew G. Knepley       if (lStart >= 0) {
60139566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6014412e9a14SMatthew G. Knepley         switch (ct) {
6015412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6016412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6017412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6018d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6019d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6020d71ae5a4SJacob Faibussowitsch           break;
6021d71ae5a4SJacob Faibussowitsch         default:
6022d71ae5a4SJacob Faibussowitsch           break;
6023412e9a14SMatthew G. Knepley         }
6024665f567fSMatthew G. Knepley       }
6025712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
602607218a29SMatthew G. Knepley       if (isCohesive) {
602707218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
602807218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
602907218a29SMatthew G. Knepley       }
60305fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60315fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60325fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60339566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60349566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
603507218a29SMatthew G. Knepley             if (dsIn) {
603607218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
603707218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
603807218a29SMatthew G. Knepley             }
60395fedec97SMatthew G. Knepley           }
60405fedec97SMatthew G. Knepley           ++nf;
60415fedec97SMatthew G. Knepley         }
60425fedec97SMatthew G. Knepley       }
6043e5e52638SMatthew G. Knepley     }
604407218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
604507218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60469566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
604707218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6048e5e52638SMatthew G. Knepley   }
60499566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6050e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6051083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6052083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
605307218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6054083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6055083401c6SMatthew G. Knepley     const PetscInt *fld;
60565fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60575fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6058e5e52638SMatthew G. Knepley 
60599566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60609566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60619566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60629566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6063083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6064083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60655fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6066e5e52638SMatthew G. Knepley       PetscClassId id;
6067e5e52638SMatthew G. Knepley 
60685fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60699566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60705fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
607107218a29SMatthew G. Knepley       if (isCohesive) {
607207218a29SMatthew G. Knepley         if (!isCohesiveField) {
607307218a29SMatthew G. Knepley           PetscObject bdDisc;
607407218a29SMatthew G. Knepley 
607507218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
607607218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
607707218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
607807218a29SMatthew G. Knepley         } else {
60799566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
608007218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
608107218a29SMatthew G. Knepley         }
608207218a29SMatthew G. Knepley       } else {
608307218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
608407218a29SMatthew G. Knepley       }
6085083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
60869566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6087e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6088e5e52638SMatthew G. Knepley     }
60899566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6090e5e52638SMatthew G. Knepley   }
6091f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
60929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6093f9244615SMatthew G. Knepley   if (flg) {
60943b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
60953b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
609607218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
60973b4aee56SMatthew G. Knepley       PetscInt Nf, f;
60983b4aee56SMatthew G. Knepley 
60999566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
610007218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
610107218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
610207218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
610307218a29SMatthew G. Knepley       }
61043b4aee56SMatthew G. Knepley     }
6105f9244615SMatthew G. Knepley   }
6106e5e52638SMatthew G. Knepley   /* Setup DSes */
6107e5e52638SMatthew G. Knepley   if (doSetup) {
610807218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6109cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6110cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6111cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6112cf1363e4SMatthew G. Knepley       }
611307218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
611407218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
611507218a29SMatthew G. Knepley     }
6116e5e52638SMatthew G. Knepley   }
61173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6118e5e52638SMatthew G. Knepley }
6119e5e52638SMatthew G. Knepley 
6120e5e52638SMatthew G. Knepley /*@
6121d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6122d2b2dc1eSMatthew G. Knepley 
6123d2b2dc1eSMatthew G. Knepley   Input Parameters:
6124d2b2dc1eSMatthew G. Knepley + dm     - The DM
6125d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6126d2b2dc1eSMatthew G. Knepley 
6127d2b2dc1eSMatthew G. Knepley   Level: developer
6128d2b2dc1eSMatthew G. Knepley 
6129d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6130d2b2dc1eSMatthew G. Knepley @*/
6131d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6132d2b2dc1eSMatthew G. Knepley {
6133d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6134d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6135d2b2dc1eSMatthew G. Knepley 
6136d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6137d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6138d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6139d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6140d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6141d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6142d2b2dc1eSMatthew G. Knepley 
6143d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6144d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6145d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6146d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6147d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6148d2b2dc1eSMatthew G. Knepley 
6149d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6150d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6151d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6152d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6153d2b2dc1eSMatthew G. Knepley   }
6154d2b2dc1eSMatthew G. Knepley   if (tensor) {
6155d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6156d2b2dc1eSMatthew G. Knepley   } else {
6157d2b2dc1eSMatthew G. Knepley     PetscSection s;
6158d2b2dc1eSMatthew G. Knepley 
6159d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6160d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6161d2b2dc1eSMatthew G. Knepley   }
6162d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6163d2b2dc1eSMatthew G. Knepley }
6164d2b2dc1eSMatthew G. Knepley 
6165d2b2dc1eSMatthew G. Knepley /*@
6166bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61677f96f943SMatthew G. Knepley 
616820f4b53cSBarry Smith   Collective
6169f2cacb80SMatthew G. Knepley 
61707f96f943SMatthew G. Knepley   Input Parameters:
6171bb7acecfSBarry Smith + dm   - The `DM`
61727f96f943SMatthew G. Knepley - time - The time
61737f96f943SMatthew G. Knepley 
61747f96f943SMatthew G. Knepley   Output Parameters:
617520f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
617620f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
617720f4b53cSBarry Smith 
617820f4b53cSBarry Smith   Level: developer
61797f96f943SMatthew G. Knepley 
6180bb7acecfSBarry Smith   Note:
6181bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
61827f96f943SMatthew G. Knepley 
61831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
61847f96f943SMatthew G. Knepley @*/
6185d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6186d71ae5a4SJacob Faibussowitsch {
61877f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
61887f96f943SMatthew G. Knepley   void   **ectxs;
6189f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
61907f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
61917f96f943SMatthew G. Knepley 
61927f96f943SMatthew G. Knepley   PetscFunctionBegin;
6193f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6194f60fa741SMatthew G. Knepley   if (u) {
6195f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6196f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6197f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6198f60fa741SMatthew G. Knepley   }
6199f60fa741SMatthew G. Knepley   if (u_t) {
6200f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6201f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6202f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6203f60fa741SMatthew G. Knepley   }
62049566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62059566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62069566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62077f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62087f96f943SMatthew G. Knepley     PetscDS         ds;
62097f96f943SMatthew G. Knepley     DMLabel         label;
62107f96f943SMatthew G. Knepley     IS              fieldIS;
62117f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62127f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62137f96f943SMatthew G. Knepley 
621407218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62159566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62169566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62179566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62189566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6219f2cacb80SMatthew G. Knepley     if (u) {
6220f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6221f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6222f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62237f96f943SMatthew G. Knepley     }
6224f2cacb80SMatthew G. Knepley     if (u_t) {
62259566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62269566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6227f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6228f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6229f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6230f2cacb80SMatthew G. Knepley     }
62319566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6232f2cacb80SMatthew G. Knepley   }
6233f2cacb80SMatthew G. Knepley   if (u) {
62349566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6236f2cacb80SMatthew G. Knepley   }
6237f2cacb80SMatthew G. Knepley   if (u_t) {
62389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6240f2cacb80SMatthew G. Knepley   }
62419566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6242f60fa741SMatthew G. Knepley   if (u) {
6243f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6244f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6245f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6246f60fa741SMatthew G. Knepley   }
6247f60fa741SMatthew G. Knepley   if (u_t) {
6248f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6249f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6250f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6251f60fa741SMatthew G. Knepley   }
62523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62537f96f943SMatthew G. Knepley }
62547f96f943SMatthew G. Knepley 
625507218a29SMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
6256d71ae5a4SJacob Faibussowitsch {
625707218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
625845480ffeSMatthew G. Knepley 
625945480ffeSMatthew G. Knepley   PetscFunctionBegin;
62609566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
626107218a29SMatthew G. Knepley   PetscCall(PetscDSCopy(ds, dm, dsNew));
626207218a29SMatthew G. Knepley   if (dsIn) {
626307218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
626407218a29SMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, dm, dsInNew));
626545480ffeSMatthew G. Knepley   }
626607218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62679566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
626807218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
627045480ffeSMatthew G. Knepley }
627145480ffeSMatthew G. Knepley 
62727f96f943SMatthew G. Knepley /*@
6273bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6274e5e52638SMatthew G. Knepley 
627520f4b53cSBarry Smith   Collective
6276e5e52638SMatthew G. Knepley 
6277e5e52638SMatthew G. Knepley   Input Parameter:
6278bb7acecfSBarry Smith . dm - The `DM`
6279e5e52638SMatthew G. Knepley 
6280e5e52638SMatthew G. Knepley   Output Parameter:
6281bb7acecfSBarry Smith . newdm - The `DM`
6282e5e52638SMatthew G. Knepley 
6283e5e52638SMatthew G. Knepley   Level: advanced
6284e5e52638SMatthew G. Knepley 
62851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6286e5e52638SMatthew G. Knepley @*/
6287d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6288d71ae5a4SJacob Faibussowitsch {
6289e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6290e5e52638SMatthew G. Knepley 
6291e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62923ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
62939566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62949566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6295e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6296e5e52638SMatthew G. Knepley     DMLabel  label;
6297b3cf3223SMatthew G. Knepley     IS       fields;
629807218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6299783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6300e5e52638SMatthew G. Knepley 
630107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6302b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
630307218a29SMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds, dsIn));
6304d5b43468SJose E. Roman     /* Complete new labels in the new DS */
630507218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63069566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6307783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6308b8025e53SMatthew G. Knepley       PetscWeakForm wf;
630945480ffeSMatthew G. Knepley       DMLabel       label;
6310783e2ec8SMatthew G. Knepley       PetscInt      field;
6311783e2ec8SMatthew G. Knepley 
63129566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63139566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6314783e2ec8SMatthew G. Knepley     }
6315e5e52638SMatthew G. Knepley   }
6316799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6318e5e52638SMatthew G. Knepley }
6319e5e52638SMatthew G. Knepley 
6320e5e52638SMatthew G. Knepley /*@
6321bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6322e5e52638SMatthew G. Knepley 
632320f4b53cSBarry Smith   Collective
6324e5e52638SMatthew G. Knepley 
6325e5e52638SMatthew G. Knepley   Input Parameter:
6326bb7acecfSBarry Smith . dm - The `DM`
6327e5e52638SMatthew G. Knepley 
6328e5e52638SMatthew G. Knepley   Output Parameter:
6329bb7acecfSBarry Smith . newdm - The `DM`
6330e5e52638SMatthew G. Knepley 
6331e5e52638SMatthew G. Knepley   Level: advanced
6332e5e52638SMatthew G. Knepley 
633373ff1848SBarry Smith   Developer Note:
6334bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6335bb7acecfSBarry Smith 
63361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6337e5e52638SMatthew G. Knepley @*/
6338d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6339d71ae5a4SJacob Faibussowitsch {
6340e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63419566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
63429566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
63433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6344e5e52638SMatthew G. Knepley }
6345e5e52638SMatthew G. Knepley 
6346c73cfb54SMatthew G. Knepley /*@
6347bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6348c73cfb54SMatthew G. Knepley 
634920f4b53cSBarry Smith   Not Collective
6350c73cfb54SMatthew G. Knepley 
6351c73cfb54SMatthew G. Knepley   Input Parameter:
6352bb7acecfSBarry Smith . dm - The `DM`
6353c73cfb54SMatthew G. Knepley 
6354c73cfb54SMatthew G. Knepley   Output Parameter:
6355c73cfb54SMatthew G. Knepley . dim - The topological dimension
6356c73cfb54SMatthew G. Knepley 
6357c73cfb54SMatthew G. Knepley   Level: beginner
6358c73cfb54SMatthew G. Knepley 
63591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6360c73cfb54SMatthew G. Knepley @*/
6361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6362d71ae5a4SJacob Faibussowitsch {
6363c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6364c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63654f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6366c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6368c73cfb54SMatthew G. Knepley }
6369c73cfb54SMatthew G. Knepley 
6370c73cfb54SMatthew G. Knepley /*@
6371bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6372c73cfb54SMatthew G. Knepley 
637320f4b53cSBarry Smith   Collective
6374c73cfb54SMatthew G. Knepley 
6375c73cfb54SMatthew G. Knepley   Input Parameters:
6376bb7acecfSBarry Smith + dm  - The `DM`
6377c73cfb54SMatthew G. Knepley - dim - The topological dimension
6378c73cfb54SMatthew G. Knepley 
6379c73cfb54SMatthew G. Knepley   Level: beginner
6380c73cfb54SMatthew G. Knepley 
63811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6382c73cfb54SMatthew G. Knepley @*/
6383d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6384d71ae5a4SJacob Faibussowitsch {
6385e5e52638SMatthew G. Knepley   PetscDS  ds;
638645480ffeSMatthew G. Knepley   PetscInt Nds, n;
6387f17e8794SMatthew G. Knepley 
6388c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6389c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6390c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6391c73cfb54SMatthew G. Knepley   dm->dim = dim;
6392d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
63939566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
639445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
639507218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
63969566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
639745480ffeSMatthew G. Knepley     }
6398d17bd122SMatthew G. Knepley   }
63993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6400c73cfb54SMatthew G. Knepley }
6401c73cfb54SMatthew G. Knepley 
6402793f3fe5SMatthew G. Knepley /*@
6403793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6404793f3fe5SMatthew G. Knepley 
640520f4b53cSBarry Smith   Collective
6406793f3fe5SMatthew G. Knepley 
6407793f3fe5SMatthew G. Knepley   Input Parameters:
6408bb7acecfSBarry Smith + dm  - the `DM`
6409793f3fe5SMatthew G. Knepley - dim - the dimension
6410793f3fe5SMatthew G. Knepley 
6411793f3fe5SMatthew G. Knepley   Output Parameters:
6412793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6413aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6414793f3fe5SMatthew G. Knepley 
641520f4b53cSBarry Smith   Level: intermediate
641620f4b53cSBarry Smith 
6417793f3fe5SMatthew G. Knepley   Note:
6418793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6419a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6420793f3fe5SMatthew G. Knepley   then the interval is empty.
6421793f3fe5SMatthew G. Knepley 
64221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6423793f3fe5SMatthew G. Knepley @*/
6424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6425d71ae5a4SJacob Faibussowitsch {
6426793f3fe5SMatthew G. Knepley   PetscInt d;
6427793f3fe5SMatthew G. Knepley 
6428793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6429793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64309566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64317a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6432dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6434793f3fe5SMatthew G. Knepley }
6435793f3fe5SMatthew G. Knepley 
64366636e97aSMatthew G Knepley /*@
6437bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6438f4d763aaSMatthew G. Knepley 
643920f4b53cSBarry Smith   Collective
64408f700142SStefano Zampini 
6441f4d763aaSMatthew G. Knepley   Input Parameter:
6442bb7acecfSBarry Smith . dm - The original `DM`
6443f4d763aaSMatthew G. Knepley 
6444f4d763aaSMatthew G. Knepley   Output Parameter:
6445bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6446f4d763aaSMatthew G. Knepley 
6447f4d763aaSMatthew G. Knepley   Level: intermediate
6448f4d763aaSMatthew G. Knepley 
6449bb7acecfSBarry Smith   Note:
6450bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6451bb7acecfSBarry 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
6452bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6453bb7acecfSBarry Smith 
64541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6455f4d763aaSMatthew G. Knepley @*/
6456d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6457d71ae5a4SJacob Faibussowitsch {
6458c26acbdeSMatthew G. Knepley   PetscSection section;
6459eb9d3e4dSMatthew G. Knepley   IS           perm;
6460eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
646114f150ffSMatthew G. Knepley 
646214f150ffSMatthew G. Knepley   PetscFunctionBegin;
646314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64644f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64669566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6467eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6468eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6469eb9d3e4dSMatthew G. Knepley   PetscCall(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6470eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6471c26acbdeSMatthew G. Knepley     *odm = dm;
64723ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6473c26acbdeSMatthew G. Knepley   }
647414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6475c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
647614f150ffSMatthew G. Knepley     PetscSF      sf;
6477eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
647814f150ffSMatthew G. Knepley 
64799566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
64809566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
64819566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
64829566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
64839566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
64849566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6485eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
64869566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
64879566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
648814f150ffSMatthew G. Knepley   }
648914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
64903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
649114f150ffSMatthew G. Knepley }
6492f4d763aaSMatthew G. Knepley 
6493f4d763aaSMatthew G. Knepley /*@
6494cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6495f4d763aaSMatthew G. Knepley 
6496f4d763aaSMatthew G. Knepley   Input Parameter:
6497bb7acecfSBarry Smith . dm - The original `DM`
6498f4d763aaSMatthew G. Knepley 
6499cdb7a50dSMatthew G. Knepley   Output Parameters:
6500cdb7a50dSMatthew G. Knepley + num - The output sequence number
6501cdb7a50dSMatthew G. Knepley - val - The output sequence value
6502f4d763aaSMatthew G. Knepley 
6503f4d763aaSMatthew G. Knepley   Level: intermediate
6504f4d763aaSMatthew G. Knepley 
6505bb7acecfSBarry Smith   Note:
6506bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6507bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6508bb7acecfSBarry Smith 
650973ff1848SBarry Smith   Developer Note:
6510bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6511bb7acecfSBarry Smith   not directly related to the `DM`.
6512f4d763aaSMatthew G. Knepley 
65131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6514f4d763aaSMatthew G. Knepley @*/
6515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6516d71ae5a4SJacob Faibussowitsch {
6517f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6518f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65199371c9d4SSatish Balay   if (num) {
65204f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65219371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65229371c9d4SSatish Balay   }
65239371c9d4SSatish Balay   if (val) {
65244f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65259371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65269371c9d4SSatish Balay   }
65273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6528f4d763aaSMatthew G. Knepley }
6529f4d763aaSMatthew G. Knepley 
6530f4d763aaSMatthew G. Knepley /*@
6531cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6532f4d763aaSMatthew G. Knepley 
6533f4d763aaSMatthew G. Knepley   Input Parameters:
6534bb7acecfSBarry Smith + dm  - The original `DM`
6535cdb7a50dSMatthew G. Knepley . num - The output sequence number
6536cdb7a50dSMatthew G. Knepley - val - The output sequence value
6537f4d763aaSMatthew G. Knepley 
6538f4d763aaSMatthew G. Knepley   Level: intermediate
6539f4d763aaSMatthew G. Knepley 
6540bb7acecfSBarry Smith   Note:
6541bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6542bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6543f4d763aaSMatthew G. Knepley 
65441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6545f4d763aaSMatthew G. Knepley @*/
6546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6547d71ae5a4SJacob Faibussowitsch {
6548f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6549f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6550f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6551cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6553cdb7a50dSMatthew G. Knepley }
6554cdb7a50dSMatthew G. Knepley 
65555d83a8b1SBarry Smith /*@
6556bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6557cdb7a50dSMatthew G. Knepley 
6558cdb7a50dSMatthew G. Knepley   Input Parameters:
6559bb7acecfSBarry Smith + dm     - The original `DM`
6560b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6561cdb7a50dSMatthew G. Knepley . name   - The sequence name
6562cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6563cdb7a50dSMatthew G. Knepley 
6564cdb7a50dSMatthew G. Knepley   Output Parameter:
6565cdb7a50dSMatthew G. Knepley . val - The output sequence value
6566cdb7a50dSMatthew G. Knepley 
6567cdb7a50dSMatthew G. Knepley   Level: intermediate
6568cdb7a50dSMatthew G. Knepley 
6569bb7acecfSBarry Smith   Note:
6570bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6571bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6572bb7acecfSBarry Smith 
657373ff1848SBarry Smith   Developer Note:
6574bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6575cdb7a50dSMatthew G. Knepley 
65761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6577cdb7a50dSMatthew G. Knepley @*/
6578b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6579d71ae5a4SJacob Faibussowitsch {
6580cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6581cdb7a50dSMatthew G. Knepley 
6582cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6583cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6584cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6585b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
65864f572ea9SToby Isaac   PetscAssertPointer(val, 5);
65879566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6588cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6589cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6590cdb7a50dSMatthew G. Knepley     PetscScalar value;
6591cdb7a50dSMatthew G. Knepley 
65929566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
65934aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6594cdb7a50dSMatthew G. Knepley #endif
6595cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
65963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6597f4d763aaSMatthew G. Knepley }
65988e4ac7eaSMatthew G. Knepley 
65998e4ac7eaSMatthew G. Knepley /*@
6600b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6601b2033f5dSMatthew G. Knepley 
6602b2033f5dSMatthew G. Knepley   Input Parameters:
6603b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6604b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6605b2033f5dSMatthew G. Knepley - name   - The sequence name
6606b2033f5dSMatthew G. Knepley 
6607b2033f5dSMatthew G. Knepley   Output Parameter:
6608b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6609b2033f5dSMatthew G. Knepley 
6610b2033f5dSMatthew G. Knepley   Level: intermediate
6611b2033f5dSMatthew G. Knepley 
6612b2033f5dSMatthew G. Knepley   Note:
6613b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6614b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6615b2033f5dSMatthew G. Knepley 
6616b2033f5dSMatthew G. Knepley   Developer Note:
6617b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6618b2033f5dSMatthew G. Knepley 
6619b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6620b2033f5dSMatthew G. Knepley @*/
6621b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6622b2033f5dSMatthew G. Knepley {
6623b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6624b2033f5dSMatthew G. Knepley 
6625b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6626b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6627b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6628b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6629b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6630b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6631b2033f5dSMatthew G. Knepley   if (ishdf5) {
6632b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6633b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6634b2033f5dSMatthew G. Knepley #endif
6635b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6636b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6637b2033f5dSMatthew G. Knepley }
6638b2033f5dSMatthew G. Knepley 
6639b2033f5dSMatthew G. Knepley /*@
6640bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66418e4ac7eaSMatthew G. Knepley 
664220f4b53cSBarry Smith   Not Collective
66438e4ac7eaSMatthew G. Knepley 
66448e4ac7eaSMatthew G. Knepley   Input Parameter:
6645bb7acecfSBarry Smith . dm - The `DM`
66468e4ac7eaSMatthew G. Knepley 
66478e4ac7eaSMatthew G. Knepley   Output Parameter:
6648bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66498e4ac7eaSMatthew G. Knepley 
66508e4ac7eaSMatthew G. Knepley   Level: beginner
66518e4ac7eaSMatthew G. Knepley 
66521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66538e4ac7eaSMatthew G. Knepley @*/
6654d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6655d71ae5a4SJacob Faibussowitsch {
66568e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66578e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66584f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66598e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66618e4ac7eaSMatthew G. Knepley }
66628e4ac7eaSMatthew G. Knepley 
66638e4ac7eaSMatthew G. Knepley /*@
6664bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66658e4ac7eaSMatthew G. Knepley 
666620f4b53cSBarry Smith   Collective
66678e4ac7eaSMatthew G. Knepley 
66688e4ac7eaSMatthew G. Knepley   Input Parameters:
6669bb7acecfSBarry Smith + dm         - The `DM`
6670bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66718e4ac7eaSMatthew G. Knepley 
667273ff1848SBarry Smith   Level: beginner
667373ff1848SBarry Smith 
6674bb7acecfSBarry Smith   Note:
6675bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
66765d3b26e6SMatthew G. Knepley 
66771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
66788e4ac7eaSMatthew G. Knepley @*/
6679d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6680d71ae5a4SJacob Faibussowitsch {
66818e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66828e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66838833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66848e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66868e4ac7eaSMatthew G. Knepley }
6687c58f1c22SToby Isaac 
6688cc4c1da9SBarry Smith /*@
6689bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6690c58f1c22SToby Isaac 
6691c58f1c22SToby Isaac   Not Collective
6692c58f1c22SToby Isaac 
6693c58f1c22SToby Isaac   Input Parameters:
6694bb7acecfSBarry Smith + dm   - The `DM` object
6695c58f1c22SToby Isaac - name - The label name
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac   Level: intermediate
6698c58f1c22SToby Isaac 
66991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6700c58f1c22SToby Isaac @*/
6701d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6702d71ae5a4SJacob Faibussowitsch {
67035d80c0bfSVaclav Hapla   PetscBool flg;
67045d80c0bfSVaclav Hapla   DMLabel   label;
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   PetscFunctionBegin;
6707c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67084f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67099566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6710c58f1c22SToby Isaac   if (!flg) {
67119566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67129566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67139566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6714c58f1c22SToby Isaac   }
67153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6716c58f1c22SToby Isaac }
6717c58f1c22SToby Isaac 
6718cc4c1da9SBarry Smith /*@
6719bb7acecfSBarry 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.
67200fdc7489SMatthew Knepley 
67210fdc7489SMatthew Knepley   Not Collective
67220fdc7489SMatthew Knepley 
67230fdc7489SMatthew Knepley   Input Parameters:
6724bb7acecfSBarry Smith + dm   - The `DM` object
67250fdc7489SMatthew Knepley . l    - The index for the label
67260fdc7489SMatthew Knepley - name - The label name
67270fdc7489SMatthew Knepley 
67280fdc7489SMatthew Knepley   Level: intermediate
67290fdc7489SMatthew Knepley 
67301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67310fdc7489SMatthew Knepley @*/
6732d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6733d71ae5a4SJacob Faibussowitsch {
67340fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67350fdc7489SMatthew Knepley   DMLabel     label;
67360fdc7489SMatthew Knepley   PetscInt    Nl, m;
67370fdc7489SMatthew Knepley   PetscBool   flg, match;
67380fdc7489SMatthew Knepley   const char *lname;
67390fdc7489SMatthew Knepley 
67400fdc7489SMatthew Knepley   PetscFunctionBegin;
67410fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67424f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67439566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67440fdc7489SMatthew Knepley   if (!flg) {
67459566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67469566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67479566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67480fdc7489SMatthew Knepley   }
67499566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
675063a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67510fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67529566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67539566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67540fdc7489SMatthew Knepley     if (match) break;
67550fdc7489SMatthew Knepley   }
67563ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67570fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67580fdc7489SMatthew Knepley   else prev->next = orig->next;
67590fdc7489SMatthew Knepley   if (!l) {
67600fdc7489SMatthew Knepley     orig->next = dm->labels;
67610fdc7489SMatthew Knepley     dm->labels = orig;
67620fdc7489SMatthew Knepley   } else {
6763fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67640fdc7489SMatthew Knepley     orig->next = prev->next;
67650fdc7489SMatthew Knepley     prev->next = orig;
67660fdc7489SMatthew Knepley   }
67673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67680fdc7489SMatthew Knepley }
67690fdc7489SMatthew Knepley 
6770cc4c1da9SBarry Smith /*@
6771bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6772c58f1c22SToby Isaac 
6773c58f1c22SToby Isaac   Not Collective
6774c58f1c22SToby Isaac 
6775c58f1c22SToby Isaac   Input Parameters:
6776bb7acecfSBarry Smith + dm    - The `DM` object
6777c58f1c22SToby Isaac . name  - The label name
6778c58f1c22SToby Isaac - point - The mesh point
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Output Parameter:
6781c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   Level: beginner
6784c58f1c22SToby Isaac 
67851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6786c58f1c22SToby Isaac @*/
6787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6788d71ae5a4SJacob Faibussowitsch {
6789c58f1c22SToby Isaac   DMLabel label;
6790c58f1c22SToby Isaac 
6791c58f1c22SToby Isaac   PetscFunctionBegin;
6792c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67934f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67949566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67957a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
67969566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
67973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6798c58f1c22SToby Isaac }
6799c58f1c22SToby Isaac 
6800cc4c1da9SBarry Smith /*@
6801bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6802c58f1c22SToby Isaac 
6803c58f1c22SToby Isaac   Not Collective
6804c58f1c22SToby Isaac 
6805c58f1c22SToby Isaac   Input Parameters:
6806bb7acecfSBarry Smith + dm    - The `DM` object
6807c58f1c22SToby Isaac . name  - The label name
6808c58f1c22SToby Isaac . point - The mesh point
6809c58f1c22SToby Isaac - value - The label value for this point
6810c58f1c22SToby Isaac 
6811c58f1c22SToby Isaac   Output Parameter:
6812c58f1c22SToby Isaac 
6813c58f1c22SToby Isaac   Level: beginner
6814c58f1c22SToby Isaac 
68151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6816c58f1c22SToby Isaac @*/
6817d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6818d71ae5a4SJacob Faibussowitsch {
6819c58f1c22SToby Isaac   DMLabel label;
6820c58f1c22SToby Isaac 
6821c58f1c22SToby Isaac   PetscFunctionBegin;
6822c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68234f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68249566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6825c58f1c22SToby Isaac   if (!label) {
68269566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68279566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6828c58f1c22SToby Isaac   }
68299566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6831c58f1c22SToby Isaac }
6832c58f1c22SToby Isaac 
6833cc4c1da9SBarry Smith /*@
6834bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac   Not Collective
6837c58f1c22SToby Isaac 
6838c58f1c22SToby Isaac   Input Parameters:
6839bb7acecfSBarry Smith + dm    - The `DM` object
6840c58f1c22SToby Isaac . name  - The label name
6841c58f1c22SToby Isaac . point - The mesh point
6842c58f1c22SToby Isaac - value - The label value for this point
6843c58f1c22SToby Isaac 
6844c58f1c22SToby Isaac   Level: beginner
6845c58f1c22SToby Isaac 
68461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6847c58f1c22SToby Isaac @*/
6848d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6849d71ae5a4SJacob Faibussowitsch {
6850c58f1c22SToby Isaac   DMLabel label;
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   PetscFunctionBegin;
6853c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68544f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68559566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68563ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68579566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6859c58f1c22SToby Isaac }
6860c58f1c22SToby Isaac 
6861cc4c1da9SBarry Smith /*@
6862bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6863c58f1c22SToby Isaac 
6864c58f1c22SToby Isaac   Not Collective
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Input Parameters:
6867bb7acecfSBarry Smith + dm   - The `DM` object
6868c58f1c22SToby Isaac - name - The label name
6869c58f1c22SToby Isaac 
6870c58f1c22SToby Isaac   Output Parameter:
6871c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   Level: beginner
6874c58f1c22SToby Isaac 
687573ff1848SBarry Smith   Developer Note:
6876bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6877bb7acecfSBarry Smith 
68781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6879c58f1c22SToby Isaac @*/
6880d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6881d71ae5a4SJacob Faibussowitsch {
6882c58f1c22SToby Isaac   DMLabel label;
6883c58f1c22SToby Isaac 
6884c58f1c22SToby Isaac   PetscFunctionBegin;
6885c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68864f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68874f572ea9SToby Isaac   PetscAssertPointer(size, 3);
68889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6889c58f1c22SToby Isaac   *size = 0;
68903ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68919566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
68923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6893c58f1c22SToby Isaac }
6894c58f1c22SToby Isaac 
6895cc4c1da9SBarry Smith /*@
6896bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6897c58f1c22SToby Isaac 
6898c58f1c22SToby Isaac   Not Collective
6899c58f1c22SToby Isaac 
6900c58f1c22SToby Isaac   Input Parameters:
690160225df5SJacob Faibussowitsch + dm   - The `DM` object
6902c58f1c22SToby Isaac - name - The label name
6903c58f1c22SToby Isaac 
6904c58f1c22SToby Isaac   Output Parameter:
690520f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6906c58f1c22SToby Isaac 
6907c58f1c22SToby Isaac   Level: beginner
6908c58f1c22SToby Isaac 
69091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6910c58f1c22SToby Isaac @*/
6911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6912d71ae5a4SJacob Faibussowitsch {
6913c58f1c22SToby Isaac   DMLabel label;
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   PetscFunctionBegin;
6916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69174f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69184f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69199566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6920c58f1c22SToby Isaac   *ids = NULL;
6921dab2e251SBlaise Bourdin   if (label) {
69229566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6923dab2e251SBlaise Bourdin   } else {
6924dab2e251SBlaise Bourdin     /* returning an empty IS */
69259566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6926dab2e251SBlaise Bourdin   }
69273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6928c58f1c22SToby Isaac }
6929c58f1c22SToby Isaac 
6930cc4c1da9SBarry Smith /*@
6931c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac   Not Collective
6934c58f1c22SToby Isaac 
6935c58f1c22SToby Isaac   Input Parameters:
6936bb7acecfSBarry Smith + dm    - The `DM` object
6937b6971eaeSBarry Smith . name  - The label name of the stratum
6938c58f1c22SToby Isaac - value - The stratum value
6939c58f1c22SToby Isaac 
6940c58f1c22SToby Isaac   Output Parameter:
6941bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6942c58f1c22SToby Isaac 
6943c58f1c22SToby Isaac   Level: beginner
6944c58f1c22SToby Isaac 
69451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6946c58f1c22SToby Isaac @*/
6947d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6948d71ae5a4SJacob Faibussowitsch {
6949c58f1c22SToby Isaac   DMLabel label;
6950c58f1c22SToby Isaac 
6951c58f1c22SToby Isaac   PetscFunctionBegin;
6952c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69534f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69544f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69559566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6956c58f1c22SToby Isaac   *size = 0;
69573ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69589566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6960c58f1c22SToby Isaac }
6961c58f1c22SToby Isaac 
6962cc4c1da9SBarry Smith /*@
6963c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac   Not Collective
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac   Input Parameters:
6968bb7acecfSBarry Smith + dm    - The `DM` object
6969c58f1c22SToby Isaac . name  - The label name
6970c58f1c22SToby Isaac - value - The stratum value
6971c58f1c22SToby Isaac 
6972c58f1c22SToby Isaac   Output Parameter:
697320f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6974c58f1c22SToby Isaac 
6975c58f1c22SToby Isaac   Level: beginner
6976c58f1c22SToby Isaac 
69771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6978c58f1c22SToby Isaac @*/
6979d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6980d71ae5a4SJacob Faibussowitsch {
6981c58f1c22SToby Isaac   DMLabel label;
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   PetscFunctionBegin;
6984c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69854f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69864f572ea9SToby Isaac   PetscAssertPointer(points, 4);
69879566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6988c58f1c22SToby Isaac   *points = NULL;
69893ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
69913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6992c58f1c22SToby Isaac }
6993c58f1c22SToby Isaac 
6994cc4c1da9SBarry Smith /*@
69959044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
69964de306b1SToby Isaac 
69974de306b1SToby Isaac   Not Collective
69984de306b1SToby Isaac 
69994de306b1SToby Isaac   Input Parameters:
7000bb7acecfSBarry Smith + dm     - The `DM` object
70014de306b1SToby Isaac . name   - The label name
70024de306b1SToby Isaac . value  - The stratum value
70034de306b1SToby Isaac - points - The stratum points
70044de306b1SToby Isaac 
70054de306b1SToby Isaac   Level: beginner
70064de306b1SToby Isaac 
70071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70084de306b1SToby Isaac @*/
7009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7010d71ae5a4SJacob Faibussowitsch {
70114de306b1SToby Isaac   DMLabel label;
70124de306b1SToby Isaac 
70134de306b1SToby Isaac   PetscFunctionBegin;
70144de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70154f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7016292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70183ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70199566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70214de306b1SToby Isaac }
70224de306b1SToby Isaac 
7023cc4c1da9SBarry Smith /*@
7024bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7025c58f1c22SToby Isaac 
7026c58f1c22SToby Isaac   Not Collective
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Input Parameters:
7029bb7acecfSBarry Smith + dm    - The `DM` object
7030c58f1c22SToby Isaac . name  - The label name
7031c58f1c22SToby Isaac - value - The label value for this point
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac   Output Parameter:
7034c58f1c22SToby Isaac 
7035c58f1c22SToby Isaac   Level: beginner
7036c58f1c22SToby Isaac 
70371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7038c58f1c22SToby Isaac @*/
7039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7040d71ae5a4SJacob Faibussowitsch {
7041c58f1c22SToby Isaac   DMLabel label;
7042c58f1c22SToby Isaac 
7043c58f1c22SToby Isaac   PetscFunctionBegin;
7044c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70454f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70469566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70473ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70489566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7050c58f1c22SToby Isaac }
7051c58f1c22SToby Isaac 
7052c58f1c22SToby Isaac /*@
7053bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7054c58f1c22SToby Isaac 
7055c58f1c22SToby Isaac   Not Collective
7056c58f1c22SToby Isaac 
7057c58f1c22SToby Isaac   Input Parameter:
7058bb7acecfSBarry Smith . dm - The `DM` object
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac   Output Parameter:
7061c58f1c22SToby Isaac . numLabels - the number of Labels
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Level: intermediate
7064c58f1c22SToby Isaac 
70651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7066c58f1c22SToby Isaac @*/
7067d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7068d71ae5a4SJacob Faibussowitsch {
70695d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7070c58f1c22SToby Isaac   PetscInt    n    = 0;
7071c58f1c22SToby Isaac 
7072c58f1c22SToby Isaac   PetscFunctionBegin;
7073c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70744f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
70759371c9d4SSatish Balay   while (next) {
70769371c9d4SSatish Balay     ++n;
70779371c9d4SSatish Balay     next = next->next;
70789371c9d4SSatish Balay   }
7079c58f1c22SToby Isaac   *numLabels = n;
70803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7081c58f1c22SToby Isaac }
7082c58f1c22SToby Isaac 
7083cc4c1da9SBarry Smith /*@
7084c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7085c58f1c22SToby Isaac 
7086c58f1c22SToby Isaac   Not Collective
7087c58f1c22SToby Isaac 
7088c58f1c22SToby Isaac   Input Parameters:
7089bb7acecfSBarry Smith + dm - The `DM` object
7090c58f1c22SToby Isaac - n  - the label number
7091c58f1c22SToby Isaac 
7092c58f1c22SToby Isaac   Output Parameter:
7093c58f1c22SToby Isaac . name - the label name
7094c58f1c22SToby Isaac 
7095c58f1c22SToby Isaac   Level: intermediate
7096c58f1c22SToby Isaac 
709773ff1848SBarry Smith   Developer Note:
7098bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7099bb7acecfSBarry Smith 
71001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7101c58f1c22SToby Isaac @*/
7102cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7103d71ae5a4SJacob Faibussowitsch {
71045d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7105c58f1c22SToby Isaac   PetscInt    l    = 0;
7106c58f1c22SToby Isaac 
7107c58f1c22SToby Isaac   PetscFunctionBegin;
7108c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71094f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7110c58f1c22SToby Isaac   while (next) {
7111c58f1c22SToby Isaac     if (l == n) {
71129566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71133ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7114c58f1c22SToby Isaac     }
7115c58f1c22SToby Isaac     ++l;
7116c58f1c22SToby Isaac     next = next->next;
7117c58f1c22SToby Isaac   }
711863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7119c58f1c22SToby Isaac }
7120c58f1c22SToby Isaac 
7121cc4c1da9SBarry Smith /*@
7122bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7123c58f1c22SToby Isaac 
7124c58f1c22SToby Isaac   Not Collective
7125c58f1c22SToby Isaac 
7126c58f1c22SToby Isaac   Input Parameters:
7127bb7acecfSBarry Smith + dm   - The `DM` object
7128c58f1c22SToby Isaac - name - The label name
7129c58f1c22SToby Isaac 
7130c58f1c22SToby Isaac   Output Parameter:
7131bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7132c58f1c22SToby Isaac 
7133c58f1c22SToby Isaac   Level: intermediate
7134c58f1c22SToby Isaac 
71351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7136c58f1c22SToby Isaac @*/
7137d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7138d71ae5a4SJacob Faibussowitsch {
71395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7140d67d17b1SMatthew G. Knepley   const char *lname;
7141c58f1c22SToby Isaac 
7142c58f1c22SToby Isaac   PetscFunctionBegin;
7143c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71444f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71454f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7146c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7147c58f1c22SToby Isaac   while (next) {
71489566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71499566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7150c58f1c22SToby Isaac     if (*hasLabel) break;
7151c58f1c22SToby Isaac     next = next->next;
7152c58f1c22SToby Isaac   }
71533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7154c58f1c22SToby Isaac }
7155c58f1c22SToby Isaac 
7156a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7157cc4c1da9SBarry Smith /*@
715820f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Not Collective
7161c58f1c22SToby Isaac 
7162c58f1c22SToby Isaac   Input Parameters:
7163bb7acecfSBarry Smith + dm   - The `DM` object
7164c58f1c22SToby Isaac - name - The label name
7165c58f1c22SToby Isaac 
7166c58f1c22SToby Isaac   Output Parameter:
716720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7168c58f1c22SToby Isaac 
7169bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7170bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7171bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7172bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7173bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7174bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7175bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71766d7c9049SMatthew G. Knepley 
7177c58f1c22SToby Isaac   Level: intermediate
7178c58f1c22SToby Isaac 
717960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7180c58f1c22SToby Isaac @*/
7181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7182d71ae5a4SJacob Faibussowitsch {
71835d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7184c58f1c22SToby Isaac   PetscBool   hasLabel;
7185d67d17b1SMatthew G. Knepley   const char *lname;
7186c58f1c22SToby Isaac 
7187c58f1c22SToby Isaac   PetscFunctionBegin;
7188c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71894f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71904f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7191c58f1c22SToby Isaac   *label = NULL;
7192c58f1c22SToby Isaac   while (next) {
71939566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71949566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7195c58f1c22SToby Isaac     if (hasLabel) {
7196c58f1c22SToby Isaac       *label = next->label;
7197c58f1c22SToby Isaac       break;
7198c58f1c22SToby Isaac     }
7199c58f1c22SToby Isaac     next = next->next;
7200c58f1c22SToby Isaac   }
72013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7202c58f1c22SToby Isaac }
7203c58f1c22SToby Isaac 
7204cc4c1da9SBarry Smith /*@
7205bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7206c58f1c22SToby Isaac 
7207c58f1c22SToby Isaac   Not Collective
7208c58f1c22SToby Isaac 
7209c58f1c22SToby Isaac   Input Parameters:
7210bb7acecfSBarry Smith + dm - The `DM` object
7211c58f1c22SToby Isaac - n  - the label number
7212c58f1c22SToby Isaac 
7213c58f1c22SToby Isaac   Output Parameter:
7214c58f1c22SToby Isaac . label - the label
7215c58f1c22SToby Isaac 
7216c58f1c22SToby Isaac   Level: intermediate
7217c58f1c22SToby Isaac 
72181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7219c58f1c22SToby Isaac @*/
7220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7221d71ae5a4SJacob Faibussowitsch {
72225d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7223c58f1c22SToby Isaac   PetscInt    l    = 0;
7224c58f1c22SToby Isaac 
7225c58f1c22SToby Isaac   PetscFunctionBegin;
7226c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72274f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7228c58f1c22SToby Isaac   while (next) {
7229c58f1c22SToby Isaac     if (l == n) {
7230c58f1c22SToby Isaac       *label = next->label;
72313ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7232c58f1c22SToby Isaac     }
7233c58f1c22SToby Isaac     ++l;
7234c58f1c22SToby Isaac     next = next->next;
7235c58f1c22SToby Isaac   }
723663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7237c58f1c22SToby Isaac }
7238c58f1c22SToby Isaac 
7239cc4c1da9SBarry Smith /*@
7240bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   Not Collective
7243c58f1c22SToby Isaac 
7244c58f1c22SToby Isaac   Input Parameters:
7245bb7acecfSBarry Smith + dm    - The `DM` object
7246bb7acecfSBarry Smith - label - The `DMLabel`
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Level: developer
7249c58f1c22SToby Isaac 
725060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7251c58f1c22SToby Isaac @*/
7252d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7253d71ae5a4SJacob Faibussowitsch {
72545d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7255c58f1c22SToby Isaac   PetscBool   hasLabel;
7256d67d17b1SMatthew G. Knepley   const char *lname;
72575d80c0bfSVaclav Hapla   PetscBool   flg;
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac   PetscFunctionBegin;
7260c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72619566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72629566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72637a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72649566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7265c58f1c22SToby Isaac   tmpLabel->label  = label;
7266c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72675d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72685d80c0bfSVaclav Hapla   *p = tmpLabel;
72699566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
72709566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
72715d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
72729566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7273ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
72743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7275c58f1c22SToby Isaac }
7276c58f1c22SToby Isaac 
7277a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7278cc4c1da9SBarry Smith /*@
72794a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
72804a7ee7d0SMatthew G. Knepley 
72814a7ee7d0SMatthew G. Knepley   Not Collective
72824a7ee7d0SMatthew G. Knepley 
72834a7ee7d0SMatthew G. Knepley   Input Parameters:
7284bb7acecfSBarry Smith + dm    - The `DM` object
7285bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
72864a7ee7d0SMatthew G. Knepley 
7287bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7288bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7289bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7290bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7291bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7292bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7293bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72944a7ee7d0SMatthew G. Knepley 
72954a7ee7d0SMatthew G. Knepley   Level: intermediate
72964a7ee7d0SMatthew G. Knepley 
72971cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
72984a7ee7d0SMatthew G. Knepley @*/
7299d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7300d71ae5a4SJacob Faibussowitsch {
73014a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73024a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73034a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73044a7ee7d0SMatthew G. Knepley 
73054a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73064a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73074a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73089566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73094a7ee7d0SMatthew G. Knepley   while (next) {
73109566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73119566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73124a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73139566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73149566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73154a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73169566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73174a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73189566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73194a7ee7d0SMatthew G. Knepley       next->label = label;
73204a7ee7d0SMatthew G. Knepley       break;
73214a7ee7d0SMatthew G. Knepley     }
73224a7ee7d0SMatthew G. Knepley     next = next->next;
73234a7ee7d0SMatthew G. Knepley   }
73243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73254a7ee7d0SMatthew G. Knepley }
73264a7ee7d0SMatthew G. Knepley 
7327cc4c1da9SBarry Smith /*@
7328bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7329c58f1c22SToby Isaac 
7330c58f1c22SToby Isaac   Not Collective
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Input Parameters:
7333bb7acecfSBarry Smith + dm   - The `DM` object
7334c58f1c22SToby Isaac - name - The label name
7335c58f1c22SToby Isaac 
7336c58f1c22SToby Isaac   Output Parameter:
733720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7338bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7339c58f1c22SToby Isaac 
7340c58f1c22SToby Isaac   Level: developer
7341c58f1c22SToby Isaac 
73421cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7343c58f1c22SToby Isaac @*/
7344d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7345d71ae5a4SJacob Faibussowitsch {
734695d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7347c58f1c22SToby Isaac   PetscBool   hasLabel;
7348d67d17b1SMatthew G. Knepley   const char *lname;
7349c58f1c22SToby Isaac 
7350c58f1c22SToby Isaac   PetscFunctionBegin;
7351c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73524f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7353e5472504SVaclav Hapla   if (label) {
73544f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7355c58f1c22SToby Isaac     *label = NULL;
7356e5472504SVaclav Hapla   }
73575d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73589566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73599566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7360c58f1c22SToby Isaac     if (hasLabel) {
736195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73629566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
736395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73649566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7365ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
736695d578d6SVaclav Hapla       if (label) *label = link->label;
73679566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73689566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7369c58f1c22SToby Isaac       break;
7370c58f1c22SToby Isaac     }
7371c58f1c22SToby Isaac   }
73723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7373c58f1c22SToby Isaac }
7374c58f1c22SToby Isaac 
7375306894acSVaclav Hapla /*@
7376bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7377306894acSVaclav Hapla 
7378306894acSVaclav Hapla   Not Collective
7379306894acSVaclav Hapla 
7380306894acSVaclav Hapla   Input Parameters:
7381bb7acecfSBarry Smith + dm           - The `DM` object
7382bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
738320f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7384306894acSVaclav Hapla 
7385306894acSVaclav Hapla   Level: developer
7386306894acSVaclav Hapla 
7387bb7acecfSBarry Smith   Note:
7388306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7389bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7390306894acSVaclav Hapla   *label nullified.
7391306894acSVaclav Hapla 
73921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7393306894acSVaclav Hapla @*/
7394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7395d71ae5a4SJacob Faibussowitsch {
739643e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7397306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7398306894acSVaclav Hapla 
7399306894acSVaclav Hapla   PetscFunctionBegin;
7400306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74014f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74023ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7403306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7404306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74055d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
740643e45a93SVaclav Hapla     if (*label == link->label) {
7407306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
740843e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7409306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7410ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
741143e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74129566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74139566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7414306894acSVaclav Hapla       break;
7415306894acSVaclav Hapla     }
7416306894acSVaclav Hapla   }
74177a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7419306894acSVaclav Hapla }
7420306894acSVaclav Hapla 
7421cc4c1da9SBarry Smith /*@
7422c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7423c58f1c22SToby Isaac 
7424c58f1c22SToby Isaac   Not Collective
7425c58f1c22SToby Isaac 
7426c58f1c22SToby Isaac   Input Parameters:
7427bb7acecfSBarry Smith + dm   - The `DM` object
7428c58f1c22SToby Isaac - name - The label name
7429c58f1c22SToby Isaac 
7430c58f1c22SToby Isaac   Output Parameter:
7431c58f1c22SToby Isaac . output - The flag for output
7432c58f1c22SToby Isaac 
7433c58f1c22SToby Isaac   Level: developer
7434c58f1c22SToby Isaac 
74351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7436c58f1c22SToby Isaac @*/
7437d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7438d71ae5a4SJacob Faibussowitsch {
74395d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7440d67d17b1SMatthew G. Knepley   const char *lname;
7441c58f1c22SToby Isaac 
7442c58f1c22SToby Isaac   PetscFunctionBegin;
7443c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74444f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74454f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7446c58f1c22SToby Isaac   while (next) {
7447c58f1c22SToby Isaac     PetscBool flg;
7448c58f1c22SToby Isaac 
74499566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74509566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74519371c9d4SSatish Balay     if (flg) {
74529371c9d4SSatish Balay       *output = next->output;
74533ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74549371c9d4SSatish Balay     }
7455c58f1c22SToby Isaac     next = next->next;
7456c58f1c22SToby Isaac   }
745798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7458c58f1c22SToby Isaac }
7459c58f1c22SToby Isaac 
7460cc4c1da9SBarry Smith /*@
7461bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac   Not Collective
7464c58f1c22SToby Isaac 
7465c58f1c22SToby Isaac   Input Parameters:
7466bb7acecfSBarry Smith + dm     - The `DM` object
7467c58f1c22SToby Isaac . name   - The label name
7468bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7469c58f1c22SToby Isaac 
7470c58f1c22SToby Isaac   Level: developer
7471c58f1c22SToby Isaac 
74721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7473c58f1c22SToby Isaac @*/
7474d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7475d71ae5a4SJacob Faibussowitsch {
74765d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7477d67d17b1SMatthew G. Knepley   const char *lname;
7478c58f1c22SToby Isaac 
7479c58f1c22SToby Isaac   PetscFunctionBegin;
7480c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74814f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7482c58f1c22SToby Isaac   while (next) {
7483c58f1c22SToby Isaac     PetscBool flg;
7484c58f1c22SToby Isaac 
74859566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74869566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74879371c9d4SSatish Balay     if (flg) {
74889371c9d4SSatish Balay       next->output = output;
74893ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74909371c9d4SSatish Balay     }
7491c58f1c22SToby Isaac     next = next->next;
7492c58f1c22SToby Isaac   }
749398921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7494c58f1c22SToby Isaac }
7495c58f1c22SToby Isaac 
7496c58f1c22SToby Isaac /*@
7497bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7498c58f1c22SToby Isaac 
749920f4b53cSBarry Smith   Collective
7500c58f1c22SToby Isaac 
7501d8d19677SJose E. Roman   Input Parameters:
7502bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7503bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7504bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7505bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7506bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7507c58f1c22SToby Isaac 
7508c58f1c22SToby Isaac   Level: intermediate
7509c58f1c22SToby Isaac 
7510bb7acecfSBarry Smith   Note:
75112cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7512c58f1c22SToby Isaac 
75131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7514c58f1c22SToby Isaac @*/
7515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7516d71ae5a4SJacob Faibussowitsch {
75172cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7518c58f1c22SToby Isaac   const char *name;
7519c58f1c22SToby Isaac   PetscBool   flg;
75205d80c0bfSVaclav Hapla   DMLabelLink link;
7521c58f1c22SToby Isaac 
75225d80c0bfSVaclav Hapla   PetscFunctionBegin;
75235d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75245d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75255d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75265d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75277a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75283ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75295d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75305d80c0bfSVaclav Hapla     label = link->label;
75319566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75325d80c0bfSVaclav Hapla     if (!all) {
75339566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7534c58f1c22SToby Isaac       if (flg) continue;
75359566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75367d5acc75SStefano Zampini       if (flg) continue;
75379566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7538ba2698f1SMatthew G. Knepley       if (flg) continue;
75395d80c0bfSVaclav Hapla     }
75409566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75412cbb9b06SVaclav Hapla     if (labelOld) {
75422cbb9b06SVaclav Hapla       switch (emode) {
7543d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7544d71ae5a4SJacob Faibussowitsch         continue;
7545d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7546d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7547d71ae5a4SJacob Faibussowitsch         break;
7548d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7549d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7550d71ae5a4SJacob Faibussowitsch       default:
7551d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75522cbb9b06SVaclav Hapla       }
75532cbb9b06SVaclav Hapla     }
75545d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75559566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75565d80c0bfSVaclav Hapla     } else {
75575d80c0bfSVaclav Hapla       labelNew = label;
75585d80c0bfSVaclav Hapla     }
75599566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75609566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7561c58f1c22SToby Isaac   }
75623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7563c58f1c22SToby Isaac }
7564461a15a0SLisandro Dalcin 
7565609dae6eSVaclav Hapla /*@C
7566b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7567609dae6eSVaclav Hapla 
756820f4b53cSBarry Smith   Collective; No Fortran Support
7569609dae6eSVaclav Hapla 
7570609dae6eSVaclav Hapla   Input Parameters:
7571bb7acecfSBarry Smith + dm0 - First `DM` object
7572bb7acecfSBarry Smith - dm1 - Second `DM` object
7573609dae6eSVaclav Hapla 
7574a4e35b19SJacob Faibussowitsch   Output Parameters:
75755efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
757620f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7577609dae6eSVaclav Hapla 
7578609dae6eSVaclav Hapla   Level: intermediate
7579609dae6eSVaclav Hapla 
7580609dae6eSVaclav Hapla   Notes:
7581bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7582bb7acecfSBarry Smith 
758320f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7584bb7acecfSBarry Smith 
758520f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7586609dae6eSVaclav Hapla 
75875efe38ccSVaclav Hapla   The output message is set independently on each rank.
7588bb7acecfSBarry Smith 
7589bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7590bb7acecfSBarry Smith 
759120f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7592bb7acecfSBarry Smith 
759320f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7594609dae6eSVaclav Hapla 
7595609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7596bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7597609dae6eSVaclav Hapla 
7598cc4c1da9SBarry Smith   Developer Note:
7599cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7600cc4c1da9SBarry Smith 
76011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7602609dae6eSVaclav Hapla @*/
7603d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7604d71ae5a4SJacob Faibussowitsch {
76055efe38ccSVaclav Hapla   PetscInt    n, i;
7606609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76075efe38ccSVaclav Hapla   PetscBool   eq;
7608609dae6eSVaclav Hapla   MPI_Comm    comm;
76095efe38ccSVaclav Hapla   PetscMPIInt rank;
7610609dae6eSVaclav Hapla 
7611609dae6eSVaclav Hapla   PetscFunctionBegin;
7612609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7613609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7614609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76154f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76164f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76179566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76189566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76195efe38ccSVaclav Hapla   {
76205efe38ccSVaclav Hapla     PetscInt n1;
76215efe38ccSVaclav Hapla 
76229566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76239566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76245efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
762548a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7626712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76275efe38ccSVaclav Hapla     if (!eq) goto finish;
76285efe38ccSVaclav Hapla   }
76295efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7630609dae6eSVaclav Hapla     DMLabel     l0, l1;
7631609dae6eSVaclav Hapla     const char *name;
7632609dae6eSVaclav Hapla     char       *msgInner;
7633609dae6eSVaclav Hapla 
7634609dae6eSVaclav Hapla     /* Ignore label order */
76359566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76369566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76379566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7638609dae6eSVaclav Hapla     if (!l1) {
763963a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76405efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76415efe38ccSVaclav Hapla       break;
7642609dae6eSVaclav Hapla     }
76439566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76449566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76459566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76465efe38ccSVaclav Hapla     if (!eq) break;
7647609dae6eSVaclav Hapla   }
7648712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7649609dae6eSVaclav Hapla finish:
76505efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7651609dae6eSVaclav Hapla   if (message) {
7652609dae6eSVaclav Hapla     *message = NULL;
765348a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76545efe38ccSVaclav Hapla   } else {
765548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76569566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76575efe38ccSVaclav Hapla   }
76585efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76595efe38ccSVaclav Hapla   if (equal) *equal = eq;
76607a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7662609dae6eSVaclav Hapla }
7663609dae6eSVaclav Hapla 
7664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7665d71ae5a4SJacob Faibussowitsch {
7666461a15a0SLisandro Dalcin   PetscFunctionBegin;
76674f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7668461a15a0SLisandro Dalcin   if (!*label) {
76699566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
76709566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7671461a15a0SLisandro Dalcin   }
76729566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
76733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7674461a15a0SLisandro Dalcin }
7675461a15a0SLisandro Dalcin 
76760fdc7489SMatthew Knepley /*
76770fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
76780fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
76790fdc7489SMatthew Knepley   (label, id) pair in the DM.
76800fdc7489SMatthew Knepley 
76810fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
76820fdc7489SMatthew Knepley   each label.
76830fdc7489SMatthew Knepley */
7684d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7685d71ae5a4SJacob Faibussowitsch {
76860fdc7489SMatthew Knepley   DMUniversalLabel ul;
76870fdc7489SMatthew Knepley   PetscBool       *active;
76880fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
76890fdc7489SMatthew Knepley 
76900fdc7489SMatthew Knepley   PetscFunctionBegin;
76919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
76929566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
76939566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
76949566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
76950fdc7489SMatthew Knepley   ul->Nl = 0;
76960fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76970fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
76980fdc7489SMatthew Knepley     const char *name;
76990fdc7489SMatthew Knepley 
77009566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77019566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77029566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77030fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77040fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77050fdc7489SMatthew Knepley   }
77069566063dSJacob 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));
77070fdc7489SMatthew Knepley   ul->Nv = 0;
77080fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77090fdc7489SMatthew Knepley     DMLabel     label;
77100fdc7489SMatthew Knepley     PetscInt    nv;
77110fdc7489SMatthew Knepley     const char *name;
77120fdc7489SMatthew Knepley 
77130fdc7489SMatthew Knepley     if (!active[l]) continue;
77149566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77159566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77169566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77179566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77180fdc7489SMatthew Knepley     ul->indices[m] = l;
77190fdc7489SMatthew Knepley     ul->Nv += nv;
77200fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77210fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77220fdc7489SMatthew Knepley     ++m;
77230fdc7489SMatthew Knepley   }
77240fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77250fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77260fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77270fdc7489SMatthew Knepley   }
77280fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77290fdc7489SMatthew Knepley     PetscInt b;
77300fdc7489SMatthew Knepley 
77310fdc7489SMatthew Knepley     ul->masks[l] = 0;
77320fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77330fdc7489SMatthew Knepley   }
77349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77350fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77360fdc7489SMatthew Knepley     DMLabel         label;
77370fdc7489SMatthew Knepley     IS              valueIS;
77380fdc7489SMatthew Knepley     const PetscInt *varr;
77390fdc7489SMatthew Knepley     PetscInt        nv, v;
77400fdc7489SMatthew Knepley 
77410fdc7489SMatthew Knepley     if (!active[l]) continue;
77429566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77439566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77459566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7746ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77479566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77489566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77499566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77500fdc7489SMatthew Knepley     ++m;
77510fdc7489SMatthew Knepley   }
77529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77530fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77540fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77550fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77560fdc7489SMatthew Knepley 
77570fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77580fdc7489SMatthew Knepley       DMLabel  label;
77590649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77600fdc7489SMatthew Knepley 
77610fdc7489SMatthew Knepley       if (!active[l]) continue;
77629566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77649566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77659371c9d4SSatish Balay       if (val == defval) {
77669371c9d4SSatish Balay         ++m;
77679371c9d4SSatish Balay         continue;
77689371c9d4SSatish Balay       }
77690649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
77700fdc7489SMatthew Knepley       marked = PETSC_TRUE;
77719566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
777263a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
77730fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
77740fdc7489SMatthew Knepley       ++m;
77750fdc7489SMatthew Knepley     }
77769566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
77770fdc7489SMatthew Knepley   }
77789566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
77790fdc7489SMatthew Knepley   *universal = ul;
77803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77810fdc7489SMatthew Knepley }
77820fdc7489SMatthew Knepley 
7783d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7784d71ae5a4SJacob Faibussowitsch {
77850fdc7489SMatthew Knepley   PetscInt l;
77860fdc7489SMatthew Knepley 
77870fdc7489SMatthew Knepley   PetscFunctionBegin;
77889566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
77899566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
77909566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
77919566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
77929566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
77930fdc7489SMatthew Knepley   *universal = NULL;
77943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77950fdc7489SMatthew Knepley }
77960fdc7489SMatthew Knepley 
7797d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7798d71ae5a4SJacob Faibussowitsch {
77990fdc7489SMatthew Knepley   PetscFunctionBegin;
78004f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78010fdc7489SMatthew Knepley   *ulabel = ul->label;
78023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78030fdc7489SMatthew Knepley }
78040fdc7489SMatthew Knepley 
7805d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7806d71ae5a4SJacob Faibussowitsch {
78070fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78080fdc7489SMatthew Knepley 
78090fdc7489SMatthew Knepley   PetscFunctionBegin;
7810064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78110fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78129566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78139566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78140fdc7489SMatthew Knepley   }
78150fdc7489SMatthew Knepley   if (preserveOrder) {
78160fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78170fdc7489SMatthew Knepley       const char *name;
78180fdc7489SMatthew Knepley       PetscBool   match;
78190fdc7489SMatthew Knepley 
78209566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78219566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
782263a3b9bcSJacob 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]);
78230fdc7489SMatthew Knepley     }
78240fdc7489SMatthew Knepley   }
78253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78260fdc7489SMatthew Knepley }
78270fdc7489SMatthew Knepley 
7828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7829d71ae5a4SJacob Faibussowitsch {
78300fdc7489SMatthew Knepley   PetscInt l;
78310fdc7489SMatthew Knepley 
78320fdc7489SMatthew Knepley   PetscFunctionBegin;
78330fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78340fdc7489SMatthew Knepley     DMLabel  label;
78350fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78360fdc7489SMatthew Knepley 
78370fdc7489SMatthew Knepley     if (lval) {
78389566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78399566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78409566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78410fdc7489SMatthew Knepley     }
78420fdc7489SMatthew Knepley   }
78433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78440fdc7489SMatthew Knepley }
7845a8fb8f29SToby Isaac 
7846a8fb8f29SToby Isaac /*@
7847bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7848bb7acecfSBarry Smith 
784920f4b53cSBarry Smith   Not Collective
7850a8fb8f29SToby Isaac 
7851a8fb8f29SToby Isaac   Input Parameter:
7852bb7acecfSBarry Smith . dm - The `DM` object
7853a8fb8f29SToby Isaac 
7854a8fb8f29SToby Isaac   Output Parameter:
7855bb7acecfSBarry Smith . cdm - The coarse `DM`
7856a8fb8f29SToby Isaac 
7857a8fb8f29SToby Isaac   Level: intermediate
7858a8fb8f29SToby Isaac 
78591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7860a8fb8f29SToby Isaac @*/
7861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7862d71ae5a4SJacob Faibussowitsch {
7863a8fb8f29SToby Isaac   PetscFunctionBegin;
7864a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78654f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7866a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7868a8fb8f29SToby Isaac }
7869a8fb8f29SToby Isaac 
7870a8fb8f29SToby Isaac /*@
7871bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7872a8fb8f29SToby Isaac 
7873a8fb8f29SToby Isaac   Input Parameters:
7874bb7acecfSBarry Smith + dm  - The `DM` object
7875bb7acecfSBarry Smith - cdm - The coarse `DM`
7876a8fb8f29SToby Isaac 
7877a8fb8f29SToby Isaac   Level: intermediate
7878a8fb8f29SToby Isaac 
7879bb7acecfSBarry Smith   Note:
7880bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7881bb7acecfSBarry Smith 
78821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7883a8fb8f29SToby Isaac @*/
7884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7885d71ae5a4SJacob Faibussowitsch {
7886a8fb8f29SToby Isaac   PetscFunctionBegin;
7887a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7888a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
788989d734beSBarry Smith   if (dm == cdm) cdm = NULL;
78909566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
78919566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7892a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
78933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7894a8fb8f29SToby Isaac }
7895a8fb8f29SToby Isaac 
789688bdff64SToby Isaac /*@
7897bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
789888bdff64SToby Isaac 
789988bdff64SToby Isaac   Input Parameter:
7900bb7acecfSBarry Smith . dm - The `DM` object
790188bdff64SToby Isaac 
790288bdff64SToby Isaac   Output Parameter:
7903bb7acecfSBarry Smith . fdm - The fine `DM`
790488bdff64SToby Isaac 
790588bdff64SToby Isaac   Level: intermediate
790688bdff64SToby Isaac 
79071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
790888bdff64SToby Isaac @*/
7909d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7910d71ae5a4SJacob Faibussowitsch {
791188bdff64SToby Isaac   PetscFunctionBegin;
791288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79134f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
791488bdff64SToby Isaac   *fdm = dm->fineMesh;
79153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
791688bdff64SToby Isaac }
791788bdff64SToby Isaac 
791888bdff64SToby Isaac /*@
7919bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
792088bdff64SToby Isaac 
792188bdff64SToby Isaac   Input Parameters:
7922bb7acecfSBarry Smith + dm  - The `DM` object
7923bb7acecfSBarry Smith - fdm - The fine `DM`
792488bdff64SToby Isaac 
7925bb7acecfSBarry Smith   Level: developer
792688bdff64SToby Isaac 
7927bb7acecfSBarry Smith   Note:
7928bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7929bb7acecfSBarry Smith 
79301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
793188bdff64SToby Isaac @*/
7932d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7933d71ae5a4SJacob Faibussowitsch {
793488bdff64SToby Isaac   PetscFunctionBegin;
793588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
793688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
793789d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79399566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
794088bdff64SToby Isaac   dm->fineMesh = fdm;
79413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
794288bdff64SToby Isaac }
794388bdff64SToby Isaac 
7944a6ba4734SToby Isaac /*@C
7945bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7946a6ba4734SToby Isaac 
794720f4b53cSBarry Smith   Collective
7948783e2ec8SMatthew G. Knepley 
7949a6ba4734SToby Isaac   Input Parameters:
7950bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7951bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7952a6ba4734SToby Isaac . name     - The BC name
795345480ffeSMatthew G. Knepley . label    - The label defining constrained points
7954bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
795545480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7956a6ba4734SToby Isaac . field    - The field to constrain
795745480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7958a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7959a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
796056cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7961a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7962a6ba4734SToby Isaac 
796345480ffeSMatthew G. Knepley   Output Parameter:
796445480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
796545480ffeSMatthew G. Knepley 
7966a6ba4734SToby Isaac   Options Database Keys:
7967a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7968a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7969a6ba4734SToby Isaac 
797020f4b53cSBarry Smith   Level: intermediate
797120f4b53cSBarry Smith 
7972bb7acecfSBarry Smith   Notes:
7973aeebefc2SPierre Jolivet   Both bcFunc and bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is\:
797473ff1848SBarry Smith .vb
797573ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
797673ff1848SBarry Smith .ve
797756cf3b9cSMatthew G. Knepley 
7978a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
797956cf3b9cSMatthew G. Knepley 
798020f4b53cSBarry Smith .vb
798120f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
798220f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
798320f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
798420f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
798520f4b53cSBarry Smith .ve
798656cf3b9cSMatthew G. Knepley + dim - the spatial dimension
798756cf3b9cSMatthew G. Knepley . Nf - the number of fields
798856cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
798956cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
799056cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
799156cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
799256cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
799356cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
799456cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
799556cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
799656cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
799756cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
799856cf3b9cSMatthew G. Knepley . t - current time
799956cf3b9cSMatthew G. Knepley . x - coordinates of the current point
800056cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
800156cf3b9cSMatthew G. Knepley . constants - constant parameters
800256cf3b9cSMatthew G. Knepley - bcval - output values at the current point
800356cf3b9cSMatthew G. Knepley 
80041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8005a6ba4734SToby Isaac @*/
8006d71ae5a4SJacob 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)
8007d71ae5a4SJacob Faibussowitsch {
8008e5e52638SMatthew G. Knepley   PetscDS ds;
8009a6ba4734SToby Isaac 
8010a6ba4734SToby Isaac   PetscFunctionBegin;
8011a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8012783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
801345480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
801445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
801545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
801645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
801701a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80189566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8019799db056SMatthew G. Knepley   /* Complete label */
8020799db056SMatthew G. Knepley   if (label) {
8021799db056SMatthew G. Knepley     PetscObject  obj;
8022799db056SMatthew G. Knepley     PetscClassId id;
8023799db056SMatthew G. Knepley 
8024799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8025799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8026799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8027799db056SMatthew G. Knepley       DM plex;
8028799db056SMatthew G. Knepley 
8029799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8030799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8031799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8032799db056SMatthew G. Knepley     }
8033799db056SMatthew G. Knepley   }
80349566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8036a6ba4734SToby Isaac }
8037a6ba4734SToby Isaac 
803845480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8039d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8040d71ae5a4SJacob Faibussowitsch {
8041e5e52638SMatthew G. Knepley   PetscDS     ds;
8042dff059c6SToby Isaac   DMBoundary *lastnext;
8043e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8044e6f8dbb6SToby Isaac 
8045e6f8dbb6SToby Isaac   PetscFunctionBegin;
80469566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8047e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
804847a1f5adSToby Isaac   if (dm->boundary) {
804947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
805047a1f5adSToby Isaac 
805147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80523ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
805347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
805447a1f5adSToby Isaac     while (next) {
805547a1f5adSToby Isaac       DMBoundary b = next;
805647a1f5adSToby Isaac 
805747a1f5adSToby Isaac       next = b->next;
80589566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8059a6ba4734SToby Isaac     }
806047a1f5adSToby Isaac     dm->boundary = NULL;
8061a6ba4734SToby Isaac   }
806247a1f5adSToby Isaac 
8063f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8064e6f8dbb6SToby Isaac   while (dsbound) {
8065e6f8dbb6SToby Isaac     DMBoundary dmbound;
8066e6f8dbb6SToby Isaac 
80679566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8068e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
806945480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
807047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8071dff059c6SToby Isaac     *lastnext = dmbound;
8072f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8073dff059c6SToby Isaac     dsbound   = dsbound->next;
8074a6ba4734SToby Isaac   }
80753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8076a6ba4734SToby Isaac }
8077a6ba4734SToby Isaac 
8078bb7acecfSBarry Smith /* TODO: missing manual page */
8079d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8080d71ae5a4SJacob Faibussowitsch {
8081b95f2879SToby Isaac   DMBoundary b;
8082a6ba4734SToby Isaac 
8083a6ba4734SToby Isaac   PetscFunctionBegin;
8084a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80854f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8086a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
80879566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8088b95f2879SToby Isaac   b = dm->boundary;
8089a6ba4734SToby Isaac   while (b && !(*isBd)) {
8090e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8091e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8092a6ba4734SToby Isaac     PetscInt   i;
8093a6ba4734SToby Isaac 
809445480ffeSMatthew G. Knepley     if (label) {
80959566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8096a6ba4734SToby Isaac     }
8097a6ba4734SToby Isaac     b = b->next;
8098a6ba4734SToby Isaac   }
80993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8100a6ba4734SToby Isaac }
81014d6f44ffSToby Isaac 
81024d6f44ffSToby Isaac /*@C
8103bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8104a6e0b375SMatthew G. Knepley 
810520f4b53cSBarry Smith   Collective
81064d6f44ffSToby Isaac 
81074d6f44ffSToby Isaac   Input Parameters:
8108bb7acecfSBarry Smith + dm    - The `DM`
81090709b2feSToby Isaac . time  - The time
81104d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
81114d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81124d6f44ffSToby Isaac - mode  - The insertion mode for values
81134d6f44ffSToby Isaac 
81144d6f44ffSToby Isaac   Output Parameter:
81154d6f44ffSToby Isaac . X - vector
81164d6f44ffSToby Isaac 
811720f4b53cSBarry Smith   Calling sequence of `funcs`:
81184d6f44ffSToby Isaac + dim  - The spatial dimension
81198ec8862eSJed Brown . time - The time at which to sample
81204d6f44ffSToby Isaac . x    - The coordinates
812177b739a6SMatthew Knepley . Nc   - The number of components
81224d6f44ffSToby Isaac . u    - The output field values
81234d6f44ffSToby Isaac - ctx  - optional user-defined function context
81244d6f44ffSToby Isaac 
81254d6f44ffSToby Isaac   Level: developer
81264d6f44ffSToby Isaac 
8127bb7acecfSBarry Smith   Developer Notes:
8128bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8129bb7acecfSBarry Smith 
8130bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8131bb7acecfSBarry Smith 
81321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81334d6f44ffSToby Isaac @*/
8134a4e35b19SJacob 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)
8135d71ae5a4SJacob Faibussowitsch {
81364d6f44ffSToby Isaac   Vec localX;
81374d6f44ffSToby Isaac 
81384d6f44ffSToby Isaac   PetscFunctionBegin;
81394d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8140708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81419566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8142f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81439566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81449566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81459566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81469566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8147708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81494d6f44ffSToby Isaac }
81504d6f44ffSToby Isaac 
8151a6e0b375SMatthew G. Knepley /*@C
8152bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8153a6e0b375SMatthew G. Knepley 
815420f4b53cSBarry Smith   Not Collective
8155a6e0b375SMatthew G. Knepley 
8156a6e0b375SMatthew G. Knepley   Input Parameters:
8157bb7acecfSBarry Smith + dm    - The `DM`
8158a6e0b375SMatthew G. Knepley . time  - The time
8159a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8160a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8161a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8162a6e0b375SMatthew G. Knepley 
8163a6e0b375SMatthew G. Knepley   Output Parameter:
8164a6e0b375SMatthew G. Knepley . localX - vector
8165a6e0b375SMatthew G. Knepley 
816620f4b53cSBarry Smith   Calling sequence of `funcs`:
8167a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8168a4e35b19SJacob Faibussowitsch . time - The current timestep
8169a6e0b375SMatthew G. Knepley . x    - The coordinates
817077b739a6SMatthew Knepley . Nc   - The number of components
8171a6e0b375SMatthew G. Knepley . u    - The output field values
8172a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8173a6e0b375SMatthew G. Knepley 
8174a6e0b375SMatthew G. Knepley   Level: developer
8175a6e0b375SMatthew G. Knepley 
8176bb7acecfSBarry Smith   Developer Notes:
8177bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8178bb7acecfSBarry Smith 
8179bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8180bb7acecfSBarry Smith 
81811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8182a6e0b375SMatthew G. Knepley @*/
8183a4e35b19SJacob 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)
8184d71ae5a4SJacob Faibussowitsch {
81854d6f44ffSToby Isaac   PetscFunctionBegin;
81864d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8187064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8188fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
81893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81904d6f44ffSToby Isaac }
81914d6f44ffSToby Isaac 
8192a6e0b375SMatthew G. Knepley /*@C
8193bb7acecfSBarry 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.
8194a6e0b375SMatthew G. Knepley 
819520f4b53cSBarry Smith   Collective
8196a6e0b375SMatthew G. Knepley 
8197a6e0b375SMatthew G. Knepley   Input Parameters:
8198bb7acecfSBarry Smith + dm     - The `DM`
8199a6e0b375SMatthew G. Knepley . time   - The time
8200a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8201a4e35b19SJacob Faibussowitsch . ids    - The ids
8202a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8203a4e35b19SJacob Faibussowitsch . comps  - The components
8204bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8205a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8206bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8207a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8208a6e0b375SMatthew G. Knepley 
8209a6e0b375SMatthew G. Knepley   Output Parameter:
8210a6e0b375SMatthew G. Knepley . X - vector
8211a6e0b375SMatthew G. Knepley 
821220f4b53cSBarry Smith   Calling sequence of `funcs`:
8213a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8214a4e35b19SJacob Faibussowitsch . time - The current timestep
8215a6e0b375SMatthew G. Knepley . x    - The coordinates
821677b739a6SMatthew Knepley . Nc   - The number of components
8217a6e0b375SMatthew G. Knepley . u    - The output field values
8218a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8219a6e0b375SMatthew G. Knepley 
8220a6e0b375SMatthew G. Knepley   Level: developer
8221a6e0b375SMatthew G. Knepley 
8222bb7acecfSBarry Smith   Developer Notes:
8223bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8224bb7acecfSBarry Smith 
8225bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8226bb7acecfSBarry Smith 
82271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8228a6e0b375SMatthew G. Knepley @*/
8229a4e35b19SJacob 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)
8230d71ae5a4SJacob Faibussowitsch {
82312c53366bSMatthew G. Knepley   Vec localX;
82322c53366bSMatthew G. Knepley 
82332c53366bSMatthew G. Knepley   PetscFunctionBegin;
82342c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82359566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8236f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82379566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82389566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82399566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82409566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82422c53366bSMatthew G. Knepley }
82432c53366bSMatthew G. Knepley 
8244a6e0b375SMatthew G. Knepley /*@C
8245bb7acecfSBarry 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.
8246a6e0b375SMatthew G. Knepley 
824720f4b53cSBarry Smith   Not Collective
8248a6e0b375SMatthew G. Knepley 
8249a6e0b375SMatthew G. Knepley   Input Parameters:
8250bb7acecfSBarry Smith + dm     - The `DM`
8251a6e0b375SMatthew G. Knepley . time   - The time
8252bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8253a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8254a4e35b19SJacob Faibussowitsch . ids    - The ids
8255a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8256a4e35b19SJacob Faibussowitsch . comps  - The components
8257a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8258a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8259a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8260a6e0b375SMatthew G. Knepley 
8261a6e0b375SMatthew G. Knepley   Output Parameter:
8262a6e0b375SMatthew G. Knepley . localX - vector
8263a6e0b375SMatthew G. Knepley 
826420f4b53cSBarry Smith   Calling sequence of `funcs`:
8265a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8266a4e35b19SJacob Faibussowitsch . time - The current time
8267a6e0b375SMatthew G. Knepley . x    - The coordinates
826877b739a6SMatthew Knepley . Nc   - The number of components
8269a6e0b375SMatthew G. Knepley . u    - The output field values
8270a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8271a6e0b375SMatthew G. Knepley 
8272a6e0b375SMatthew G. Knepley   Level: developer
8273a6e0b375SMatthew G. Knepley 
8274bb7acecfSBarry Smith   Developer Notes:
8275bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8276bb7acecfSBarry Smith 
8277bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8278bb7acecfSBarry Smith 
82791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8280a6e0b375SMatthew G. Knepley @*/
8281a4e35b19SJacob 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)
8282d71ae5a4SJacob Faibussowitsch {
82834d6f44ffSToby Isaac   PetscFunctionBegin;
82844d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8285064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8286fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
82873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82884d6f44ffSToby Isaac }
82892716604bSToby Isaac 
8290a6e0b375SMatthew G. Knepley /*@C
8291bb7acecfSBarry 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.
8292a6e0b375SMatthew G. Knepley 
829320f4b53cSBarry Smith   Not Collective
8294a6e0b375SMatthew G. Knepley 
8295a6e0b375SMatthew G. Knepley   Input Parameters:
8296bb7acecfSBarry Smith + dm     - The `DM`
8297a6e0b375SMatthew G. Knepley . time   - The time
829820f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8299a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8300a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8301a6e0b375SMatthew G. Knepley 
8302a6e0b375SMatthew G. Knepley   Output Parameter:
8303a6e0b375SMatthew G. Knepley . localX - The output vector
8304a6e0b375SMatthew G. Knepley 
830520f4b53cSBarry Smith   Calling sequence of `funcs`:
8306a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8307a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8308a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8309a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8310a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8311a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8312a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8313a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8314a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8315a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8316a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8317a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8318a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8319a6e0b375SMatthew G. Knepley . t            - The current time
8320a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8321a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8322a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8323a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8324a6e0b375SMatthew G. Knepley 
832573ff1848SBarry Smith   Level: intermediate
832673ff1848SBarry Smith 
8327bb7acecfSBarry Smith   Note:
8328bb7acecfSBarry 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.
8329bb7acecfSBarry 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
8330bb7acecfSBarry 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
8331a6e0b375SMatthew 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.
8332a6e0b375SMatthew G. Knepley 
8333bb7acecfSBarry Smith   Developer Notes:
8334bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8335bb7acecfSBarry Smith 
8336bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8337bb7acecfSBarry Smith 
8338a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8339a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8340a6e0b375SMatthew G. Knepley @*/
8341a4e35b19SJacob 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)
8342d71ae5a4SJacob Faibussowitsch {
83438c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83448c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8345eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83468c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8347fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83498c6c5593SMatthew G. Knepley }
83508c6c5593SMatthew G. Knepley 
8351a6e0b375SMatthew G. Knepley /*@C
8352a6e0b375SMatthew 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.
8353a6e0b375SMatthew G. Knepley 
835420f4b53cSBarry Smith   Not Collective
8355a6e0b375SMatthew G. Knepley 
8356a6e0b375SMatthew G. Knepley   Input Parameters:
8357bb7acecfSBarry Smith + dm     - The `DM`
8358a6e0b375SMatthew G. Knepley . time   - The time
8359bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8360a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8361a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8362bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
836320f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8364a6e0b375SMatthew G. Knepley . localU - The input field vector
8365a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8366a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8367a6e0b375SMatthew G. Knepley 
8368a6e0b375SMatthew G. Knepley   Output Parameter:
8369a6e0b375SMatthew G. Knepley . localX - The output vector
8370a6e0b375SMatthew G. Knepley 
837120f4b53cSBarry Smith   Calling sequence of `funcs`:
8372a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8373a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8374a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8375a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8376a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8377a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8378a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8379a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8380a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8381a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8382a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8383a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8384a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8385a6e0b375SMatthew G. Knepley . t            - The current time
8386a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8387a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8388a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8389a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8390a6e0b375SMatthew G. Knepley 
839173ff1848SBarry Smith   Level: intermediate
839273ff1848SBarry Smith 
8393bb7acecfSBarry Smith   Note:
8394bb7acecfSBarry 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.
8395bb7acecfSBarry 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
8396bb7acecfSBarry 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
8397a6e0b375SMatthew 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.
8398a6e0b375SMatthew G. Knepley 
8399bb7acecfSBarry Smith   Developer Notes:
8400bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8401bb7acecfSBarry Smith 
8402bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8403bb7acecfSBarry Smith 
84041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8405a6e0b375SMatthew G. Knepley @*/
8406a4e35b19SJacob 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)
8407d71ae5a4SJacob Faibussowitsch {
84088c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84098c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8410064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8411064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8412fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84148c6c5593SMatthew G. Knepley }
84158c6c5593SMatthew G. Knepley 
84162716604bSToby Isaac /*@C
8417d29d7c6eSMatthew 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.
8418d29d7c6eSMatthew G. Knepley 
841920f4b53cSBarry Smith   Not Collective
8420d29d7c6eSMatthew G. Knepley 
8421d29d7c6eSMatthew G. Knepley   Input Parameters:
8422bb7acecfSBarry Smith + dm     - The `DM`
8423d29d7c6eSMatthew G. Knepley . time   - The time
8424bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8425d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8426d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8427bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
842820f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8429d29d7c6eSMatthew G. Knepley . U      - The input field vector
8430d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8431d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8432d29d7c6eSMatthew G. Knepley 
8433d29d7c6eSMatthew G. Knepley   Output Parameter:
8434d29d7c6eSMatthew G. Knepley . X - The output vector
8435d29d7c6eSMatthew G. Knepley 
843620f4b53cSBarry Smith   Calling sequence of `funcs`:
8437d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8438d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8439d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8440d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8441d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8442d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8443d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8444d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8445d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8446d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8447d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8448d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8449d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8450d29d7c6eSMatthew G. Knepley . t            - The current time
8451d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8452d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8453d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8454d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8455d29d7c6eSMatthew G. Knepley 
845673ff1848SBarry Smith   Level: intermediate
845773ff1848SBarry Smith 
8458bb7acecfSBarry Smith   Note:
8459bb7acecfSBarry 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.
8460bb7acecfSBarry 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
8461bb7acecfSBarry 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
8462d29d7c6eSMatthew 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.
8463d29d7c6eSMatthew G. Knepley 
8464bb7acecfSBarry Smith   Developer Notes:
8465bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8466bb7acecfSBarry Smith 
8467bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8468bb7acecfSBarry Smith 
84691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8470d29d7c6eSMatthew G. Knepley @*/
8471a4e35b19SJacob 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)
8472d71ae5a4SJacob Faibussowitsch {
8473d29d7c6eSMatthew G. Knepley   DM  dmIn;
8474d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8475d29d7c6eSMatthew G. Knepley 
8476d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8477d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8478d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8479d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8480d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8481f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
848272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
848372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8484d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8485d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8486d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8487d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8488d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
84893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8490d29d7c6eSMatthew G. Knepley }
8491d29d7c6eSMatthew G. Knepley 
8492d29d7c6eSMatthew G. Knepley /*@C
8493ece3a9fcSMatthew 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.
8494ece3a9fcSMatthew G. Knepley 
849520f4b53cSBarry Smith   Not Collective
8496ece3a9fcSMatthew G. Knepley 
8497ece3a9fcSMatthew G. Knepley   Input Parameters:
8498bb7acecfSBarry Smith + dm     - The `DM`
8499ece3a9fcSMatthew G. Knepley . time   - The time
8500bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8501ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8502ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8503bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
850420f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8505ece3a9fcSMatthew G. Knepley . localU - The input field vector
8506ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8507ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8508ece3a9fcSMatthew G. Knepley 
8509ece3a9fcSMatthew G. Knepley   Output Parameter:
8510ece3a9fcSMatthew G. Knepley . localX - The output vector
8511ece3a9fcSMatthew G. Knepley 
851220f4b53cSBarry Smith   Calling sequence of `funcs`:
8513ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8514ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8515ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8516ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8517ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8518ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8519ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8520ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8521ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8522ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8523ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8524ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8525ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8526ece3a9fcSMatthew G. Knepley . t            - The current time
8527ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8528ece3a9fcSMatthew G. Knepley . n            - The face normal
8529ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8530ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8531ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8532ece3a9fcSMatthew G. Knepley 
853373ff1848SBarry Smith   Level: intermediate
853473ff1848SBarry Smith 
8535ece3a9fcSMatthew G. Knepley   Note:
8536bb7acecfSBarry 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.
8537bb7acecfSBarry 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
8538bb7acecfSBarry 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
8539ece3a9fcSMatthew 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.
8540ece3a9fcSMatthew G. Knepley 
8541bb7acecfSBarry Smith   Developer Notes:
8542bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8543bb7acecfSBarry Smith 
8544bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8545bb7acecfSBarry Smith 
85461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8547ece3a9fcSMatthew G. Knepley @*/
8548a4e35b19SJacob 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)
8549d71ae5a4SJacob Faibussowitsch {
8550ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8551ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8552064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8553064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8554fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8556ece3a9fcSMatthew G. Knepley }
8557ece3a9fcSMatthew G. Knepley 
8558ece3a9fcSMatthew G. Knepley /*@C
85592716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85602716604bSToby Isaac 
856120f4b53cSBarry Smith   Collective
8562bb7acecfSBarry Smith 
85632716604bSToby Isaac   Input Parameters:
8564bb7acecfSBarry Smith + dm    - The `DM`
85650709b2feSToby Isaac . time  - The time
85662716604bSToby Isaac . funcs - The functions to evaluate for each field component
85672716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8568574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85692716604bSToby Isaac 
85702716604bSToby Isaac   Output Parameter:
85712716604bSToby Isaac . diff - The diff ||u - u_h||_2
85722716604bSToby Isaac 
85732716604bSToby Isaac   Level: developer
85742716604bSToby Isaac 
8575bb7acecfSBarry Smith   Developer Notes:
8576bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8577bb7acecfSBarry Smith 
8578bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8579bb7acecfSBarry Smith 
85801cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
85812716604bSToby Isaac @*/
8582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8583d71ae5a4SJacob Faibussowitsch {
85842716604bSToby Isaac   PetscFunctionBegin;
85852716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8586b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8587fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
85883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
85892716604bSToby Isaac }
8590b698f381SToby Isaac 
8591b698f381SToby Isaac /*@C
8592b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8593b698f381SToby Isaac 
859420f4b53cSBarry Smith   Collective
8595d083f849SBarry Smith 
8596b698f381SToby Isaac   Input Parameters:
8597bb7acecfSBarry Smith + dm    - The `DM`
8598a4e35b19SJacob Faibussowitsch . time  - The time
8599b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8600b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8601574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8602b698f381SToby Isaac - n     - The vector to project along
8603b698f381SToby Isaac 
8604b698f381SToby Isaac   Output Parameter:
8605b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8606b698f381SToby Isaac 
8607b698f381SToby Isaac   Level: developer
8608b698f381SToby Isaac 
8609bb7acecfSBarry Smith   Developer Notes:
8610bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8611bb7acecfSBarry Smith 
8612bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8613bb7acecfSBarry Smith 
86141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8615b698f381SToby Isaac @*/
8616d71ae5a4SJacob 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)
8617d71ae5a4SJacob Faibussowitsch {
8618b698f381SToby Isaac   PetscFunctionBegin;
8619b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8620b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8621fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8623b698f381SToby Isaac }
8624b698f381SToby Isaac 
86252a16baeaSToby Isaac /*@C
86262a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86272a16baeaSToby Isaac 
862820f4b53cSBarry Smith   Collective
8629d083f849SBarry Smith 
86302a16baeaSToby Isaac   Input Parameters:
8631bb7acecfSBarry Smith + dm    - The `DM`
86322a16baeaSToby Isaac . time  - The time
86332a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86342a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8635574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86362a16baeaSToby Isaac 
86372a16baeaSToby Isaac   Output Parameter:
86382a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86392a16baeaSToby Isaac 
86402a16baeaSToby Isaac   Level: developer
86412a16baeaSToby Isaac 
8642bb7acecfSBarry Smith   Developer Notes:
8643bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8644bb7acecfSBarry Smith 
8645bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8646bb7acecfSBarry Smith 
864742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86482a16baeaSToby Isaac @*/
8649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8650d71ae5a4SJacob Faibussowitsch {
86512a16baeaSToby Isaac   PetscFunctionBegin;
86522a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86532a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8654fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86562a16baeaSToby Isaac }
86572a16baeaSToby Isaac 
8658df0b854cSToby Isaac /*@C
8659bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8660502a2867SDave May 
8661502a2867SDave May   Not Collective
8662502a2867SDave May 
8663502a2867SDave May   Input Parameter:
8664bb7acecfSBarry Smith . dm - The `DM`
8665502a2867SDave May 
86660a19bb7dSprj-   Output Parameters:
86670a19bb7dSprj- + nranks - the number of neighbours
86680a19bb7dSprj- - ranks  - the neighbors ranks
8669502a2867SDave May 
86709bdbcad8SBarry Smith   Level: beginner
86719bdbcad8SBarry Smith 
8672bb7acecfSBarry Smith   Note:
8673bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8674502a2867SDave May 
86751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8676502a2867SDave May @*/
8677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8678d71ae5a4SJacob Faibussowitsch {
8679502a2867SDave May   PetscFunctionBegin;
8680502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8681fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
86823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8683502a2867SDave May }
8684502a2867SDave May 
8685531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8686531c7667SBarry Smith 
8687531c7667SBarry Smith /*
8688531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
86892b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8690531c7667SBarry Smith */
869166976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8692d71ae5a4SJacob Faibussowitsch {
8693531c7667SBarry Smith   PetscFunctionBegin;
8694531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8695531c7667SBarry Smith     Vec x1local;
8696531c7667SBarry Smith     DM  dm;
86979566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
86987a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
86999566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
87009566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
87019566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8702531c7667SBarry Smith     x1 = x1local;
8703531c7667SBarry Smith   }
87049566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8705531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8706531c7667SBarry Smith     DM dm;
87079566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87089566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8709531c7667SBarry Smith   }
87103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8711531c7667SBarry Smith }
8712531c7667SBarry Smith 
8713531c7667SBarry Smith /*@
8714bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8715531c7667SBarry Smith 
8716a4e35b19SJacob Faibussowitsch   Input Parameters:
8717a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8718a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8719531c7667SBarry Smith 
87209bdbcad8SBarry Smith   Level: advanced
87219bdbcad8SBarry Smith 
872273ff1848SBarry Smith   Developer Note:
872373ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8724531c7667SBarry Smith 
87251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8726531c7667SBarry Smith @*/
8727d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8728d71ae5a4SJacob Faibussowitsch {
8729531c7667SBarry Smith   PetscFunctionBegin;
8730531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8732531c7667SBarry Smith }
87338320bc6fSPatrick Sanan 
87348320bc6fSPatrick Sanan /*@
8735bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87368320bc6fSPatrick Sanan 
87378320bc6fSPatrick Sanan   Collective
87388320bc6fSPatrick Sanan 
87398320bc6fSPatrick Sanan   Input Parameters:
8740bb7acecfSBarry Smith + dm1 - the first `DM`
8741bb7acecfSBarry Smith - dm2 - the second `DM`
87428320bc6fSPatrick Sanan 
87438320bc6fSPatrick Sanan   Output Parameters:
8744bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8745bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87468320bc6fSPatrick Sanan 
874720f4b53cSBarry Smith   Level: advanced
874820f4b53cSBarry Smith 
87498320bc6fSPatrick Sanan   Notes:
8750bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87513d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87528320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8753bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87543d862458SPatrick Sanan   decomposition, but hold different data.
87558320bc6fSPatrick Sanan 
87568320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8757bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87588320bc6fSPatrick Sanan 
8759bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87608320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87618320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8762bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87638320bc6fSPatrick Sanan 
8764bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8765bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87668320bc6fSPatrick Sanan .vb
87678320bc6fSPatrick Sanan   ...
87689566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87698320bc6fSPatrick Sanan   if (set && compatible)  {
87709566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
87719566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
87729566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
87738320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
87748320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
87758320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
87768320bc6fSPatrick Sanan       }
87778320bc6fSPatrick Sanan     }
87789566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
87799566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
87808320bc6fSPatrick Sanan   } else {
87818320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
87828320bc6fSPatrick Sanan   }
87838320bc6fSPatrick Sanan   ...
87848320bc6fSPatrick Sanan .ve
87858320bc6fSPatrick Sanan 
8786bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
87878320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
87888320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
87898320bc6fSPatrick Sanan   always check the "set" output parameter.
87908320bc6fSPatrick Sanan 
8791bb7acecfSBarry Smith   A `DM` is always compatible with itself.
87928320bc6fSPatrick Sanan 
8793bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
87948320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
87958320bc6fSPatrick Sanan   incompatible.
87968320bc6fSPatrick Sanan 
87978320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8798bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
87998320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
88008320bc6fSPatrick Sanan 
880173ff1848SBarry Smith   Developer Note:
8802bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
88033d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8804a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8805bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88068320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
88073d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8808bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
88098320bc6fSPatrick Sanan 
88101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
88118320bc6fSPatrick Sanan @*/
8812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8813d71ae5a4SJacob Faibussowitsch {
88148320bc6fSPatrick Sanan   PetscMPIInt compareResult;
88158320bc6fSPatrick Sanan   DMType      type, type2;
88168320bc6fSPatrick Sanan   PetscBool   sameType;
88178320bc6fSPatrick Sanan 
88188320bc6fSPatrick Sanan   PetscFunctionBegin;
8819a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
88208320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88218320bc6fSPatrick Sanan 
88228320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8823a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88248320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88258320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88263ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88278320bc6fSPatrick Sanan   }
88288320bc6fSPatrick Sanan 
88298320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88308320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88318320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88328320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88348320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88358320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88368320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88373ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88388320bc6fSPatrick Sanan   }
88398320bc6fSPatrick Sanan 
88408320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8841a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8842dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88433ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88448320bc6fSPatrick Sanan   }
88458320bc6fSPatrick Sanan 
8846a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88478320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88489566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88499566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88509566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88518320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8852dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88538320bc6fSPatrick Sanan   } else {
88548320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88558320bc6fSPatrick Sanan   }
88563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88578320bc6fSPatrick Sanan }
8858c0f0dcc3SMatthew G. Knepley 
8859c0f0dcc3SMatthew G. Knepley /*@C
8860bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8861c0f0dcc3SMatthew G. Knepley 
886220f4b53cSBarry Smith   Logically Collective
8863c0f0dcc3SMatthew G. Knepley 
8864c0f0dcc3SMatthew G. Knepley   Input Parameters:
886560225df5SJacob Faibussowitsch + dm             - the `DM`
8866c0f0dcc3SMatthew G. Knepley . f              - the monitor function
886720f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
886820f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8869c0f0dcc3SMatthew G. Knepley 
887020f4b53cSBarry Smith   Options Database Key:
887160225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8872c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8873c0f0dcc3SMatthew G. Knepley 
88749bdbcad8SBarry Smith   Level: intermediate
88759bdbcad8SBarry Smith 
8876bb7acecfSBarry Smith   Note:
8877c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8878bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8879c0f0dcc3SMatthew G. Knepley   order in which they were set.
8880c0f0dcc3SMatthew G. Knepley 
888173ff1848SBarry Smith   Fortran Note:
8882bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8883bb7acecfSBarry Smith 
888473ff1848SBarry Smith   Developer Note:
8885bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8886c0f0dcc3SMatthew G. Knepley 
88871cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8888c0f0dcc3SMatthew G. Knepley @*/
8889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8890d71ae5a4SJacob Faibussowitsch {
8891c0f0dcc3SMatthew G. Knepley   PetscInt m;
8892c0f0dcc3SMatthew G. Knepley 
8893c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8894c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8895c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8896c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8897c0f0dcc3SMatthew G. Knepley 
88989566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
88993ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8900c0f0dcc3SMatthew G. Knepley   }
89017a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8902c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8903c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8904c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
89053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8906c0f0dcc3SMatthew G. Knepley }
8907c0f0dcc3SMatthew G. Knepley 
8908c0f0dcc3SMatthew G. Knepley /*@
8909bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8910c0f0dcc3SMatthew G. Knepley 
891120f4b53cSBarry Smith   Logically Collective
8912c0f0dcc3SMatthew G. Knepley 
8913c0f0dcc3SMatthew G. Knepley   Input Parameter:
8914c0f0dcc3SMatthew G. Knepley . dm - the DM
8915c0f0dcc3SMatthew G. Knepley 
8916c0f0dcc3SMatthew G. Knepley   Options Database Key:
8917c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8918bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8919c0f0dcc3SMatthew G. Knepley   set via the options database
8920c0f0dcc3SMatthew G. Knepley 
89219bdbcad8SBarry Smith   Level: intermediate
89229bdbcad8SBarry Smith 
8923bb7acecfSBarry Smith   Note:
8924bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8925c0f0dcc3SMatthew G. Knepley 
89261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8927c0f0dcc3SMatthew G. Knepley @*/
8928d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8929d71ae5a4SJacob Faibussowitsch {
8930c0f0dcc3SMatthew G. Knepley   PetscInt m;
8931c0f0dcc3SMatthew G. Knepley 
8932c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8933c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8934c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89359566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8936c0f0dcc3SMatthew G. Knepley   }
8937c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8939c0f0dcc3SMatthew G. Knepley }
8940c0f0dcc3SMatthew G. Knepley 
8941c0f0dcc3SMatthew G. Knepley /*@C
8942c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8943c0f0dcc3SMatthew G. Knepley 
894420f4b53cSBarry Smith   Collective
8945c0f0dcc3SMatthew G. Knepley 
8946c0f0dcc3SMatthew G. Knepley   Input Parameters:
8947bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8948c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8949c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8950c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8951c0f0dcc3SMatthew G. Knepley . monitor      - the monitor function
8952bb7acecfSBarry 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
8953c0f0dcc3SMatthew G. Knepley 
8954c0f0dcc3SMatthew G. Knepley   Output Parameter:
8955c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8956c0f0dcc3SMatthew G. Knepley 
8957c0f0dcc3SMatthew G. Knepley   Level: developer
8958c0f0dcc3SMatthew G. Knepley 
8959648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8960db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
896160225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8962db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8963c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8964db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8965bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8966c0f0dcc3SMatthew G. Knepley @*/
8967d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8968d71ae5a4SJacob Faibussowitsch {
8969c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8970c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8971c0f0dcc3SMatthew G. Knepley 
8972c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8973c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8974648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8975c0f0dcc3SMatthew G. Knepley   if (*flg) {
8976c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8977c0f0dcc3SMatthew G. Knepley 
89789566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
8979648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
89809566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
89819566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8982c0f0dcc3SMatthew G. Knepley   }
89833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8984c0f0dcc3SMatthew G. Knepley }
8985c0f0dcc3SMatthew G. Knepley 
8986c0f0dcc3SMatthew G. Knepley /*@
8987c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
8988c0f0dcc3SMatthew G. Knepley 
898920f4b53cSBarry Smith   Collective
8990c0f0dcc3SMatthew G. Knepley 
89912fe279fdSBarry Smith   Input Parameter:
8992bb7acecfSBarry Smith . dm - The `DM`
8993c0f0dcc3SMatthew G. Knepley 
8994c0f0dcc3SMatthew G. Knepley   Level: developer
8995c0f0dcc3SMatthew G. Knepley 
899673ff1848SBarry Smith   Developer Note:
8997a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
8998a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
8999a4e35b19SJacob Faibussowitsch   concept of discretization.
9000bb7acecfSBarry Smith 
90011cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9002c0f0dcc3SMatthew G. Knepley @*/
9003d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9004d71ae5a4SJacob Faibussowitsch {
9005c0f0dcc3SMatthew G. Knepley   PetscInt m;
9006c0f0dcc3SMatthew G. Knepley 
9007c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
90083ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9009c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
901048a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
90113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9012c0f0dcc3SMatthew G. Knepley }
90132e4af2aeSMatthew G. Knepley 
90142e4af2aeSMatthew G. Knepley /*@
9015bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
90162e4af2aeSMatthew G. Knepley 
901720f4b53cSBarry Smith   Collective
90182e4af2aeSMatthew G. Knepley 
90192e4af2aeSMatthew G. Knepley   Input Parameters:
9020bb7acecfSBarry Smith + dm  - The `DM`
90216b867d5aSJose E. Roman - sol - The solution vector
90222e4af2aeSMatthew G. Knepley 
90236b867d5aSJose E. Roman   Input/Output Parameter:
902420f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90256b867d5aSJose E. Roman            contains the error in each field
90266b867d5aSJose E. Roman 
90276b867d5aSJose E. Roman   Output Parameter:
902820f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
902920f4b53cSBarry Smith 
903020f4b53cSBarry Smith   Level: developer
90312e4af2aeSMatthew G. Knepley 
9032bb7acecfSBarry Smith   Note:
9033bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90342e4af2aeSMatthew G. Knepley 
90351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90362e4af2aeSMatthew G. Knepley @*/
9037d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9038d71ae5a4SJacob Faibussowitsch {
90392e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90402e4af2aeSMatthew G. Knepley   void    **ctxs;
90412e4af2aeSMatthew G. Knepley   PetscReal time;
90422e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90432e4af2aeSMatthew G. Knepley 
90442e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90459566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90469566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90479566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90482e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90492e4af2aeSMatthew G. Knepley     PetscDS         ds;
90502e4af2aeSMatthew G. Knepley     DMLabel         label;
90512e4af2aeSMatthew G. Knepley     IS              fieldIS;
90522e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90532e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90542e4af2aeSMatthew G. Knepley 
905507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90569566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90579566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90582e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90592e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90609566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90612e4af2aeSMatthew G. Knepley     }
90629566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90632e4af2aeSMatthew G. Knepley   }
9064ad540459SPierre 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);
90659566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90669566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90672e4af2aeSMatthew G. Knepley   if (errorVec) {
90682e4af2aeSMatthew G. Knepley     DM             edm;
90692e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
90702e4af2aeSMatthew G. Knepley     PetscBool      simplex;
90712e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
90722e4af2aeSMatthew G. Knepley 
90739566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
90749566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
90759566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
90769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90772e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
90789566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
90792e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
90802e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
90812e4af2aeSMatthew G. Knepley       PetscQuadrature q;
90822e4af2aeSMatthew G. Knepley       const char     *name;
90832e4af2aeSMatthew G. Knepley 
90849566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
90859566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
90869566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
90879566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
90889566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
90899566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
90909566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
90919566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
90922e4af2aeSMatthew G. Knepley     }
90939566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
90942e4af2aeSMatthew G. Knepley 
90959566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
90969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
90979566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
90989566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
90992e4af2aeSMatthew G. Knepley   }
91009566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
91013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91022e4af2aeSMatthew G. Knepley }
91039a2a23afSMatthew G. Knepley 
91049a2a23afSMatthew G. Knepley /*@
9105bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91069a2a23afSMatthew G. Knepley 
910720f4b53cSBarry Smith   Not Collective
91089a2a23afSMatthew G. Knepley 
91099a2a23afSMatthew G. Knepley   Input Parameter:
9110bb7acecfSBarry Smith . dm - The `DM`
91119a2a23afSMatthew G. Knepley 
91129a2a23afSMatthew G. Knepley   Output Parameter:
9113a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
91149a2a23afSMatthew G. Knepley 
91159a2a23afSMatthew G. Knepley   Level: advanced
91169a2a23afSMatthew G. Knepley 
9117e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
91189a2a23afSMatthew G. Knepley @*/
9119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9120d71ae5a4SJacob Faibussowitsch {
91219a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91229a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91239566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91259a2a23afSMatthew G. Knepley }
91269a2a23afSMatthew G. Knepley 
91279a2a23afSMatthew G. Knepley /*@
9128ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91299a2a23afSMatthew G. Knepley 
913020f4b53cSBarry Smith   Not Collective
91319a2a23afSMatthew G. Knepley 
91329a2a23afSMatthew G. Knepley   Input Parameters:
9133bb7acecfSBarry Smith + dm    - The `DM`
9134bb7acecfSBarry Smith . label - The `DMLabel`
9135ac17215fSMatthew G. Knepley . value - The label value indicating the region
9136ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91379a2a23afSMatthew G. Knepley 
91389a2a23afSMatthew G. Knepley   Output Parameter:
9139bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91409a2a23afSMatthew G. Knepley 
91419bdbcad8SBarry Smith   Level: advanced
91429bdbcad8SBarry Smith 
9143bb7acecfSBarry Smith   Note:
9144bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
914504c51a94SMatthew G. Knepley 
9146e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91479a2a23afSMatthew G. Knepley @*/
9148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9149d71ae5a4SJacob Faibussowitsch {
9150ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
915104c51a94SMatthew G. Knepley   PetscBool       has;
91529a2a23afSMatthew G. Knepley 
91539a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91549a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91559a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91569a2a23afSMatthew G. Knepley   key.label = label;
91579a2a23afSMatthew G. Knepley   key.value = value;
9158ac17215fSMatthew G. Knepley   key.part  = part;
91599566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91609566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91619566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91639a2a23afSMatthew G. Knepley }
91649a2a23afSMatthew G. Knepley 
91659a2a23afSMatthew G. Knepley /*@
9166bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91679a2a23afSMatthew G. Knepley 
916820f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91699a2a23afSMatthew G. Knepley 
91709a2a23afSMatthew G. Knepley   Input Parameters:
9171bb7acecfSBarry Smith + dm    - The `DM`
9172bb7acecfSBarry Smith . label - The `DMLabel`
91739a2a23afSMatthew G. Knepley . value - The label value indicating the region
9174ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9175bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
91769a2a23afSMatthew G. Knepley 
91779a2a23afSMatthew G. Knepley   Level: advanced
91789a2a23afSMatthew G. Knepley 
9179e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
91809a2a23afSMatthew G. Knepley @*/
9181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9182d71ae5a4SJacob Faibussowitsch {
91839a2a23afSMatthew G. Knepley   Vec             old;
91849a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
91859a2a23afSMatthew G. Knepley 
91869a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91879a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91889a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91899a2a23afSMatthew G. Knepley   key.label = label;
91909a2a23afSMatthew G. Knepley   key.value = value;
9191ac17215fSMatthew G. Knepley   key.part  = part;
91929566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
91939566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
91949566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
91959566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9196d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
91973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91989a2a23afSMatthew G. Knepley }
91999a2a23afSMatthew G. Knepley 
9200cc4c1da9SBarry Smith /*@
9201bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
92029a2a23afSMatthew G. Knepley 
920320f4b53cSBarry Smith   Not Collective
92049a2a23afSMatthew G. Knepley 
92059a2a23afSMatthew G. Knepley   Input Parameter:
9206bb7acecfSBarry Smith . dm - The `DM`
92079a2a23afSMatthew G. Knepley 
92089a2a23afSMatthew G. Knepley   Output Parameters:
9209bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9210bb7acecfSBarry Smith . values - The label values for each `Vec`
9211bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
92129a2a23afSMatthew G. Knepley 
92139bdbcad8SBarry Smith   Level: advanced
92149bdbcad8SBarry Smith 
9215bb7acecfSBarry Smith   Note:
9216bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
92179a2a23afSMatthew G. Knepley 
9218e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
92199a2a23afSMatthew G. Knepley @*/
9220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9221d71ae5a4SJacob Faibussowitsch {
92229a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92239a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92249a2a23afSMatthew G. Knepley 
92259a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92269a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92274f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92284f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92294f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92309566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92319566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92329566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92339371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92349371c9d4SSatish Balay     labels[i] = keys[i].label;
92359371c9d4SSatish Balay     values[i] = keys[i].value;
92369371c9d4SSatish Balay     parts[i]  = keys[i].part;
92379371c9d4SSatish Balay   }
92389566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92409a2a23afSMatthew G. Knepley }
92419a2a23afSMatthew G. Knepley 
92429a2a23afSMatthew G. Knepley /*@
9243bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92449a2a23afSMatthew G. Knepley 
924520f4b53cSBarry Smith   Not Collective
92469a2a23afSMatthew G. Knepley 
92479a2a23afSMatthew G. Knepley   Input Parameter:
9248bb7acecfSBarry Smith . dm - The `DM`
92499a2a23afSMatthew G. Knepley 
92509a2a23afSMatthew G. Knepley   Output Parameter:
9251bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92529a2a23afSMatthew G. Knepley 
92539a2a23afSMatthew G. Knepley   Level: advanced
92549a2a23afSMatthew G. Knepley 
9255bb7acecfSBarry Smith   Note:
9256bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9257bb7acecfSBarry Smith 
9258e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92599a2a23afSMatthew G. Knepley @*/
9260d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9261d71ae5a4SJacob Faibussowitsch {
92629a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92639a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9264d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9265d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9266e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9267e4d5475eSStefano Zampini 
9268e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92699566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9270d705d0eaSStefano Zampini   {
9271d705d0eaSStefano Zampini     Vec     *auxData;
9272d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9273d705d0eaSStefano Zampini 
9274d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9275d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9276d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9277d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9278d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9279e4d5475eSStefano Zampini   }
9280e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9281e4d5475eSStefano Zampini }
9282e4d5475eSStefano Zampini 
9283e4d5475eSStefano Zampini /*@
9284e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9285e4d5475eSStefano Zampini 
9286e4d5475eSStefano Zampini   Not Collective
9287e4d5475eSStefano Zampini 
9288e4d5475eSStefano Zampini   Input Parameter:
9289e4d5475eSStefano Zampini . dm - The `DM`
9290e4d5475eSStefano Zampini 
9291e4d5475eSStefano Zampini   Level: advanced
9292e4d5475eSStefano Zampini 
9293e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9294e4d5475eSStefano Zampini @*/
9295e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9296e4d5475eSStefano Zampini {
9297e4d5475eSStefano Zampini   Vec     *auxData;
9298e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9299e4d5475eSStefano Zampini 
9300e4d5475eSStefano Zampini   PetscFunctionBegin;
9301e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9302d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9303e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9304d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9305d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9306e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9307e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
93083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93099a2a23afSMatthew G. Knepley }
9310b5a892a1SMatthew G. Knepley 
9311cc4c1da9SBarry Smith /*@
9312bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9313b5a892a1SMatthew G. Knepley 
931420f4b53cSBarry Smith   Not Collective
9315b5a892a1SMatthew G. Knepley 
9316b5a892a1SMatthew G. Knepley   Input Parameters:
9317bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9318b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9319b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9320b5a892a1SMatthew G. Knepley 
9321b5a892a1SMatthew G. Knepley   Output Parameters:
9322bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9323b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9324b5a892a1SMatthew G. Knepley 
9325b5a892a1SMatthew G. Knepley   Level: advanced
9326b5a892a1SMatthew G. Knepley 
9327bb7acecfSBarry Smith   Note:
9328bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9329bb7acecfSBarry Smith 
933085036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9331bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9332bb7acecfSBarry Smith 
9333bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9334bb7acecfSBarry Smith 
93351cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9336b5a892a1SMatthew G. Knepley @*/
9337d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9338d71ae5a4SJacob Faibussowitsch {
9339b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
934085036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9341b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9342b5a892a1SMatthew G. Knepley 
9343b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93449371c9d4SSatish Balay   if (!nO) {
93459371c9d4SSatish Balay     *ornt  = 0;
93469371c9d4SSatish Balay     *found = PETSC_TRUE;
93473ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93489371c9d4SSatish Balay   }
9349b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
935085036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9351b5a892a1SMatthew G. Knepley 
93529371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93539371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93549371c9d4SSatish Balay     if (c == cS) {
93559371c9d4SSatish Balay       *ornt = o;
93569371c9d4SSatish Balay       break;
93579371c9d4SSatish Balay     }
9358b5a892a1SMatthew G. Knepley   }
9359b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9361b5a892a1SMatthew G. Knepley }
9362b5a892a1SMatthew G. Knepley 
9363cc4c1da9SBarry Smith /*@
9364bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9365b5a892a1SMatthew G. Knepley 
936620f4b53cSBarry Smith   Not Collective
9367b5a892a1SMatthew G. Knepley 
9368b5a892a1SMatthew G. Knepley   Input Parameters:
9369bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9370b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9371b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9372b5a892a1SMatthew G. Knepley 
93732fe279fdSBarry Smith   Output Parameter:
9374bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9375b5a892a1SMatthew G. Knepley 
9376b5a892a1SMatthew G. Knepley   Level: advanced
9377b5a892a1SMatthew G. Knepley 
9378bb7acecfSBarry Smith   Note:
9379bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9380bb7acecfSBarry Smith 
938173ff1848SBarry Smith   Developer Note:
9382bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9383bb7acecfSBarry Smith 
93841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9385b5a892a1SMatthew G. Knepley @*/
9386d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9387d71ae5a4SJacob Faibussowitsch {
9388b5a892a1SMatthew G. Knepley   PetscBool found;
9389b5a892a1SMatthew G. Knepley 
9390b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93919566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
93927a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
93933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9394b5a892a1SMatthew G. Knepley }
9395b5a892a1SMatthew G. Knepley 
9396cc4c1da9SBarry Smith /*@
9397bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9398b5a892a1SMatthew G. Knepley 
939920f4b53cSBarry Smith   Not Collective
9400b5a892a1SMatthew G. Knepley 
9401b5a892a1SMatthew G. Knepley   Input Parameters:
9402bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9403b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9404b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9405b5a892a1SMatthew G. Knepley 
9406b5a892a1SMatthew G. Knepley   Output Parameters:
9407bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9408b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9409b5a892a1SMatthew G. Knepley 
9410b5a892a1SMatthew G. Knepley   Level: advanced
9411b5a892a1SMatthew G. Knepley 
941273ff1848SBarry Smith   Notes:
9413bb7acecfSBarry Smith   An arrangement is a vertex order
9414bb7acecfSBarry Smith 
941585036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9416bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9417bb7acecfSBarry Smith 
9418bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9419bb7acecfSBarry Smith 
942085036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9421b5a892a1SMatthew G. Knepley @*/
9422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9423d71ae5a4SJacob Faibussowitsch {
9424b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
942585036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9426b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9427b5a892a1SMatthew G. Knepley 
9428b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94299371c9d4SSatish Balay   if (!nO) {
94309371c9d4SSatish Balay     *ornt  = 0;
94319371c9d4SSatish Balay     *found = PETSC_TRUE;
94323ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94339371c9d4SSatish Balay   }
9434b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
943585036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9436b5a892a1SMatthew G. Knepley 
94379371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94389371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94399371c9d4SSatish Balay     if (c == cS) {
94409371c9d4SSatish Balay       *ornt = o;
94419371c9d4SSatish Balay       break;
94429371c9d4SSatish Balay     }
9443b5a892a1SMatthew G. Knepley   }
9444b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9446b5a892a1SMatthew G. Knepley }
9447b5a892a1SMatthew G. Knepley 
9448cc4c1da9SBarry Smith /*@
9449bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9450b5a892a1SMatthew G. Knepley 
945120f4b53cSBarry Smith   Not Collective
9452b5a892a1SMatthew G. Knepley 
9453b5a892a1SMatthew G. Knepley   Input Parameters:
9454bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9455b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9456b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9457b5a892a1SMatthew G. Knepley 
94582fe279fdSBarry Smith   Output Parameter:
9459bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9460b5a892a1SMatthew G. Knepley 
9461b5a892a1SMatthew G. Knepley   Level: advanced
9462b5a892a1SMatthew G. Knepley 
9463bb7acecfSBarry Smith   Note:
9464bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9465bb7acecfSBarry Smith 
946673ff1848SBarry Smith   Developer Note:
9467bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9468bb7acecfSBarry Smith 
94691cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9470b5a892a1SMatthew G. Knepley @*/
9471d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9472d71ae5a4SJacob Faibussowitsch {
9473b5a892a1SMatthew G. Knepley   PetscBool found;
9474b5a892a1SMatthew G. Knepley 
9475b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94769566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
94777a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9479b5a892a1SMatthew G. Knepley }
9480012bc364SMatthew G. Knepley 
9481cc4c1da9SBarry Smith /*@
9482012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9483012bc364SMatthew G. Knepley 
948420f4b53cSBarry Smith   Not Collective
9485012bc364SMatthew G. Knepley 
9486012bc364SMatthew G. Knepley   Input Parameters:
9487bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9488012bc364SMatthew G. Knepley - point - Coordinates of the point
9489012bc364SMatthew G. Knepley 
94902fe279fdSBarry Smith   Output Parameter:
9491012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9492012bc364SMatthew G. Knepley 
9493012bc364SMatthew G. Knepley   Level: advanced
9494012bc364SMatthew G. Knepley 
94951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9496012bc364SMatthew G. Knepley @*/
9497d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9498d71ae5a4SJacob Faibussowitsch {
9499012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9500012bc364SMatthew G. Knepley   PetscInt  d;
9501012bc364SMatthew G. Knepley 
9502012bc364SMatthew G. Knepley   PetscFunctionBegin;
9503012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9504012bc364SMatthew G. Knepley   switch (ct) {
9505012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9506012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9507012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
95089371c9d4SSatish Balay       if (point[d] < -1.0) {
95099371c9d4SSatish Balay         *inside = PETSC_FALSE;
95109371c9d4SSatish Balay         break;
95119371c9d4SSatish Balay       }
9512012bc364SMatthew G. Knepley       sum += point[d];
9513012bc364SMatthew G. Knepley     }
95149371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
95159371c9d4SSatish Balay       *inside = PETSC_FALSE;
95169371c9d4SSatish Balay       break;
95179371c9d4SSatish Balay     }
9518012bc364SMatthew G. Knepley     break;
9519012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9520012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9521012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95229371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95239371c9d4SSatish Balay         *inside = PETSC_FALSE;
9524012bc364SMatthew G. Knepley         break;
95259371c9d4SSatish Balay       }
95269371c9d4SSatish Balay     break;
9527d71ae5a4SJacob Faibussowitsch   default:
9528d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9529012bc364SMatthew G. Knepley   }
95303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9531012bc364SMatthew G. Knepley }
9532adc21957SMatthew G. Knepley 
9533adc21957SMatthew G. Knepley /*@
9534adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9535adc21957SMatthew G. Knepley 
9536adc21957SMatthew G. Knepley   Logically collective
9537adc21957SMatthew G. Knepley 
9538adc21957SMatthew G. Knepley   Input Parameters:
9539adc21957SMatthew G. Knepley + dm      - The DM
9540adc21957SMatthew G. Knepley - reorder - Flag for reordering
9541adc21957SMatthew G. Knepley 
9542adc21957SMatthew G. Knepley   Level: intermediate
9543adc21957SMatthew G. Knepley 
9544adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9545adc21957SMatthew G. Knepley @*/
9546adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9547adc21957SMatthew G. Knepley {
9548adc21957SMatthew G. Knepley   PetscFunctionBegin;
9549adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9550adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9551adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9552adc21957SMatthew G. Knepley }
9553adc21957SMatthew G. Knepley 
9554adc21957SMatthew G. Knepley /*@
9555adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9556adc21957SMatthew G. Knepley 
9557adc21957SMatthew G. Knepley   Not collective
9558adc21957SMatthew G. Knepley 
9559adc21957SMatthew G. Knepley   Input Parameter:
9560adc21957SMatthew G. Knepley . dm - The DM
9561adc21957SMatthew G. Knepley 
9562adc21957SMatthew G. Knepley   Output Parameter:
9563adc21957SMatthew G. Knepley . reorder - Flag for reordering
9564adc21957SMatthew G. Knepley 
9565adc21957SMatthew G. Knepley   Level: intermediate
9566adc21957SMatthew G. Knepley 
9567adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9568adc21957SMatthew G. Knepley @*/
9569adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9570adc21957SMatthew G. Knepley {
9571adc21957SMatthew G. Knepley   PetscFunctionBegin;
9572adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9573adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9574adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9575adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9576adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9577adc21957SMatthew G. Knepley }
9578adc21957SMatthew G. Knepley 
9579cc4c1da9SBarry Smith /*@
9580adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9581adc21957SMatthew G. Knepley 
9582adc21957SMatthew G. Knepley   Logically collective
9583adc21957SMatthew G. Knepley 
9584adc21957SMatthew G. Knepley   Input Parameters:
9585adc21957SMatthew G. Knepley + dm      - The DM
9586adc21957SMatthew G. Knepley - reorder - The reordering method
9587adc21957SMatthew G. Knepley 
9588adc21957SMatthew G. Knepley   Level: intermediate
9589adc21957SMatthew G. Knepley 
9590adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9591adc21957SMatthew G. Knepley @*/
9592adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9593adc21957SMatthew G. Knepley {
9594adc21957SMatthew G. Knepley   PetscFunctionBegin;
9595adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9596adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9597adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9598adc21957SMatthew G. Knepley }
9599adc21957SMatthew G. Knepley 
9600cc4c1da9SBarry Smith /*@
9601adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9602adc21957SMatthew G. Knepley 
9603adc21957SMatthew G. Knepley   Not collective
9604adc21957SMatthew G. Knepley 
9605adc21957SMatthew G. Knepley   Input Parameter:
9606adc21957SMatthew G. Knepley . dm - The DM
9607adc21957SMatthew G. Knepley 
9608adc21957SMatthew G. Knepley   Output Parameter:
9609adc21957SMatthew G. Knepley . reorder - The reordering method
9610adc21957SMatthew G. Knepley 
9611adc21957SMatthew G. Knepley   Level: intermediate
9612adc21957SMatthew G. Knepley 
9613adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9614adc21957SMatthew G. Knepley @*/
9615adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9616adc21957SMatthew G. Knepley {
9617adc21957SMatthew G. Knepley   PetscFunctionBegin;
9618adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9619adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9620adc21957SMatthew G. Knepley   *reorder = NULL;
9621adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9622adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9623adc21957SMatthew G. Knepley }
9624