xref: /petsc/src/dm/interface/dm.c (revision 49abdd8a111d9c2ef7fc48ade253ef64e07f9b37)
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));
174462c564dSBarry Smith       PetscCallMPI(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 
95849c89c76SBlaise Bourdin   Notes:
95949c89c76SBlaise Bourdin 
96049c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWERHDF5` i.e. HDF5 format can be used with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` to save multiple `DMPLEX`
961bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
962bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
963cd7e8a5eSksagiyam 
96449c89c76SBlaise Bourdin   `PetscViewer` = `PETSCVIEWEREXODUSII` i.e. ExodusII format assumes that element blocks (mapped to "Cell sets" labels)
96549c89c76SBlaise Bourdin   consists of sequentially numbered cells.
96649c89c76SBlaise Bourdin 
96749c89c76SBlaise Bourdin   If `dm` has been distributed, only the part of the `DM` on MPI rank 0 (including "ghost" cells and vertices) will be written.
96849c89c76SBlaise Bourdin 
96949c89c76SBlaise Bourdin   Only TRI, TET, QUAD, and HEX cells are supported.
97049c89c76SBlaise Bourdin 
97149c89c76SBlaise Bourdin   `DMPLEX` only represents geometry while most post-processing software expect that a mesh also provides information on the discretization space. This function assumes that the file represents Lagrange finite elements of order 1 or 2.
97249c89c76SBlaise Bourdin   The order of the mesh shall be set using `PetscViewerExodusIISetOrder()`
97349c89c76SBlaise Bourdin 
974d7c1f440SPierre Jolivet   Variable names can be set and queried using `PetscViewerExodusII[Set/Get][Nodal/Zonal]VariableNames[s]`.
97549c89c76SBlaise Bourdin 
9761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat()`, `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
97747c6ae99SBarry Smith @*/
978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
979d71ae5a4SJacob Faibussowitsch {
98032c0f0efSBarry Smith   PetscBool         isbinary;
98176a8abe0SBarry Smith   PetscMPIInt       size;
98276a8abe0SBarry Smith   PetscViewerFormat format;
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith   PetscFunctionBegin;
985171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
98648a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
987b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
98874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
98974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
99074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
99174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
99274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
99374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
99474903a4fSStefano Zampini      in an error here */
99574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9969566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
997b1b135c8SBarry Smith 
9989566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9999566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
10003ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
10019566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
10029566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
100332c0f0efSBarry Smith   if (isbinary) {
100455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
100532c0f0efSBarry Smith     char     type[256];
100632c0f0efSBarry Smith 
10079566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
1008c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
10099566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
101032c0f0efSBarry Smith   }
1011dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
10123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101347c6ae99SBarry Smith }
101447c6ae99SBarry Smith 
101547c6ae99SBarry Smith /*@
1016bb7acecfSBarry 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,
1017bb7acecfSBarry Smith   that is it has no ghost locations.
101847c6ae99SBarry Smith 
101920f4b53cSBarry Smith   Collective
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith   Input Parameter:
1022bb7acecfSBarry Smith . dm - the `DM` object
102347c6ae99SBarry Smith 
102447c6ae99SBarry Smith   Output Parameter:
102547c6ae99SBarry Smith . vec - the global vector
102647c6ae99SBarry Smith 
1027073dac72SJed Brown   Level: beginner
102847c6ae99SBarry Smith 
10291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1030bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
103147c6ae99SBarry Smith @*/
1032d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1033d71ae5a4SJacob Faibussowitsch {
103447c6ae99SBarry Smith   PetscFunctionBegin;
1035171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10364f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1037dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
103876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1039c6b011d8SStefano Zampini     DM vdm;
1040c6b011d8SStefano Zampini 
10419566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10427a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1043c6b011d8SStefano Zampini   }
10443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104547c6ae99SBarry Smith }
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith /*@
1048bb7acecfSBarry Smith   DMCreateLocalVector - Creates a local vector from a `DM` object.
104947c6ae99SBarry Smith 
105047c6ae99SBarry Smith   Not Collective
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith   Input Parameter:
1053bb7acecfSBarry Smith . dm - the `DM` object
105447c6ae99SBarry Smith 
105547c6ae99SBarry Smith   Output Parameter:
105647c6ae99SBarry Smith . vec - the local vector
105747c6ae99SBarry Smith 
1058073dac72SJed Brown   Level: beginner
105947c6ae99SBarry Smith 
106020f4b53cSBarry Smith   Note:
1061bb7acecfSBarry 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.
1062bb7acecfSBarry Smith 
10631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1064bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
106547c6ae99SBarry Smith @*/
1066d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1067d71ae5a4SJacob Faibussowitsch {
106847c6ae99SBarry Smith   PetscFunctionBegin;
1069171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10704f572ea9SToby Isaac   PetscAssertPointer(vec, 2);
1071dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
107276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1073c6b011d8SStefano Zampini     DM vdm;
1074c6b011d8SStefano Zampini 
10759566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10767a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1077c6b011d8SStefano Zampini   }
10783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107947c6ae99SBarry Smith }
108047c6ae99SBarry Smith 
10811411c6eeSJed Brown /*@
1082bb7acecfSBarry Smith   DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10831411c6eeSJed Brown 
108420f4b53cSBarry Smith   Collective
10851411c6eeSJed Brown 
10861411c6eeSJed Brown   Input Parameter:
1087bb7acecfSBarry Smith . dm - the `DM` that provides the mapping
10881411c6eeSJed Brown 
10891411c6eeSJed Brown   Output Parameter:
10901411c6eeSJed Brown . ltog - the mapping
10911411c6eeSJed Brown 
1092bb7acecfSBarry Smith   Level: advanced
10931411c6eeSJed Brown 
10941411c6eeSJed Brown   Notes:
1095bb7acecfSBarry Smith   The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10961411c6eeSJed Brown 
1097bb7acecfSBarry Smith   Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1098bb7acecfSBarry Smith   need to use this function with those objects.
1099bb7acecfSBarry Smith 
1100bb7acecfSBarry Smith   This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1101bb7acecfSBarry Smith 
110260225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1103bb7acecfSBarry Smith           `DMCreateMatrix()`
11041411c6eeSJed Brown @*/
1105d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1106d71ae5a4SJacob Faibussowitsch {
11070be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
11081411c6eeSJed Brown 
11091411c6eeSJed Brown   PetscFunctionBegin;
11101411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11114f572ea9SToby Isaac   PetscAssertPointer(ltog, 2);
11121411c6eeSJed Brown   if (!dm->ltogmap) {
111337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
111437d0c07bSMatthew G Knepley 
11159566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
111637d0c07bSMatthew G Knepley     if (section) {
1117a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
111837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1119ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
112037d0c07bSMatthew G Knepley 
11219566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11229566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
112537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1126e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
112737d0c07bSMatthew G Knepley 
112837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11309566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11319566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11329566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11331a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11341a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1135ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11365227eafbSStefano Zampini 
1137e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11385227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1139e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1140e6befd46SJed Brown             cind++;
1141e6befd46SJed Brown           } else {
11425227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1143e6befd46SJed Brown           }
114437d0c07bSMatthew G Knepley         }
114537d0c07bSMatthew G Knepley       }
1146bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11471690c2aeSBarry Smith       bsLocal[0] = bs < 0 ? PETSC_INT_MAX : bs;
11489371c9d4SSatish Balay       bsLocal[1] = bs;
11499566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11509371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11519371c9d4SSatish Balay         bs = 1;
11529371c9d4SSatish Balay       } else {
11539371c9d4SSatish Balay         bs = bsMinMax[0];
11549371c9d4SSatish Balay       }
11557591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11567591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1157ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1158ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1159ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1160ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1161ca469d19SJed Brown         }
1162ccf3bd66SMatthew G. Knepley         n /= bs;
1163ccf3bd66SMatthew G. Knepley       }
11649566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1165dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
116637d0c07bSMatthew G Knepley   }
11671411c6eeSJed Brown   *ltog = dm->ltogmap;
11683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11691411c6eeSJed Brown }
11701411c6eeSJed Brown 
11711411c6eeSJed Brown /*@
1172bb7acecfSBarry Smith   DMGetBlockSize - Gets the inherent block size associated with a `DM`
11731411c6eeSJed Brown 
11741411c6eeSJed Brown   Not Collective
11751411c6eeSJed Brown 
11761411c6eeSJed Brown   Input Parameter:
1177bb7acecfSBarry Smith . dm - the `DM` with block structure
11781411c6eeSJed Brown 
11791411c6eeSJed Brown   Output Parameter:
11801411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure
11811411c6eeSJed Brown 
11821411c6eeSJed Brown   Level: intermediate
11831411c6eeSJed Brown 
118473ff1848SBarry Smith   Notes:
1185bb7acecfSBarry Smith   This might be the number of degrees of freedom at each grid point for a structured grid.
1186bb7acecfSBarry Smith 
1187bb7acecfSBarry Smith   Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1188bb7acecfSBarry Smith   rather different locations in the vectors may have a different block size.
1189bb7acecfSBarry Smith 
11901cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11911411c6eeSJed Brown @*/
1192d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1193d71ae5a4SJacob Faibussowitsch {
11941411c6eeSJed Brown   PetscFunctionBegin;
11951411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11964f572ea9SToby Isaac   PetscAssertPointer(bs, 2);
11977a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11981411c6eeSJed Brown   *bs = dm->bs;
11993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12001411c6eeSJed Brown }
12011411c6eeSJed Brown 
1202ffeef943SBarry Smith /*@
1203bb7acecfSBarry Smith   DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1204bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
120547c6ae99SBarry Smith 
120620f4b53cSBarry Smith   Collective
120747c6ae99SBarry Smith 
1208d8d19677SJose E. Roman   Input Parameters:
1209bb7acecfSBarry Smith + dmc - the `DM` object
1210bb7acecfSBarry Smith - dmf - the second, finer `DM` object
121147c6ae99SBarry Smith 
1212d8d19677SJose E. Roman   Output Parameters:
121347c6ae99SBarry Smith + mat - the interpolation
1214b6971eaeSBarry Smith - vec - the scaling (optional, pass `NULL` if not needed), see `DMCreateInterpolationScale()`
121547c6ae99SBarry Smith 
121647c6ae99SBarry Smith   Level: developer
121747c6ae99SBarry Smith 
121895452b02SPatrick Sanan   Notes:
1219bb7acecfSBarry 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
1220bb7acecfSBarry Smith   DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1221d52bd9f3SBarry Smith 
1222bb7acecfSBarry Smith   For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1223bb7acecfSBarry Smith   vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
122485afcc9aSBarry Smith 
12251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
122647c6ae99SBarry Smith @*/
1227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1228d71ae5a4SJacob Faibussowitsch {
122947c6ae99SBarry Smith   PetscFunctionBegin;
1230a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1231a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
12324f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
12339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1234dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
123747c6ae99SBarry Smith }
123847c6ae99SBarry Smith 
12393ad4599aSBarry Smith /*@
1240a4e35b19SJacob Faibussowitsch   DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is
1241a4e35b19SJacob Faibussowitsch   the transpose of the interpolation between the `DM`.
12422ed6491fSPatrick Sanan 
12432ed6491fSPatrick Sanan   Input Parameters:
1244bb7acecfSBarry Smith + dac - `DM` that defines a coarse mesh
1245bb7acecfSBarry Smith . daf - `DM` that defines a fine mesh
12462ed6491fSPatrick Sanan - mat - the restriction (or interpolation operator) from fine to coarse
12472ed6491fSPatrick Sanan 
12482ed6491fSPatrick Sanan   Output Parameter:
12492ed6491fSPatrick Sanan . scale - the scaled vector
12502ed6491fSPatrick Sanan 
1251bb7acecfSBarry Smith   Level: advanced
12522ed6491fSPatrick Sanan 
125373ff1848SBarry Smith   Note:
1254a4e35b19SJacob Faibussowitsch   xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual)
1255a4e35b19SJacob Faibussowitsch   restriction. In other words xcoarse is the coarse representation of xfine.
1256a4e35b19SJacob Faibussowitsch 
125773ff1848SBarry Smith   Developer Note:
1258bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1259e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1260e9c74fd6SRichard Tran Mills 
126160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, `DMCreateRestriction()`, `DMCreateGlobalVector()`
12622ed6491fSPatrick Sanan @*/
1263d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1264d71ae5a4SJacob Faibussowitsch {
12652ed6491fSPatrick Sanan   Vec         fine;
12662ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12679704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1268e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12699704db99SRichard Tran Mills #endif
12702ed6491fSPatrick Sanan 
12712ed6491fSPatrick Sanan   PetscFunctionBegin;
12729566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12739566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12759704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12769704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12779704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12789704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12799566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1280e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12819566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12829566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12839566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
128483aa49f4SRichard Tran Mills   }
12859704db99SRichard Tran Mills #endif
12869566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12879566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12889566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12902ed6491fSPatrick Sanan }
12912ed6491fSPatrick Sanan 
12922ed6491fSPatrick Sanan /*@
1293bb7acecfSBarry Smith   DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1294bb7acecfSBarry Smith   `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12953ad4599aSBarry Smith 
129620f4b53cSBarry Smith   Collective
12973ad4599aSBarry Smith 
1298d8d19677SJose E. Roman   Input Parameters:
1299bb7acecfSBarry Smith + dmc - the `DM` object
1300bb7acecfSBarry Smith - dmf - the second, finer `DM` object
13013ad4599aSBarry Smith 
13023ad4599aSBarry Smith   Output Parameter:
13033ad4599aSBarry Smith . mat - the restriction
13043ad4599aSBarry Smith 
13053ad4599aSBarry Smith   Level: developer
13063ad4599aSBarry Smith 
1307bb7acecfSBarry Smith   Note:
1308bb7acecfSBarry Smith   This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1309bb7acecfSBarry Smith   matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
13103ad4599aSBarry Smith 
13111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
13123ad4599aSBarry Smith @*/
1313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1314d71ae5a4SJacob Faibussowitsch {
13153ad4599aSBarry Smith   PetscFunctionBegin;
1316a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1317a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13184f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1320dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13233ad4599aSBarry Smith }
13243ad4599aSBarry Smith 
132547c6ae99SBarry Smith /*@
1326a4e35b19SJacob Faibussowitsch   DMCreateInjection - Gets injection matrix between two `DM` objects.
132747c6ae99SBarry Smith 
132820f4b53cSBarry Smith   Collective
132947c6ae99SBarry Smith 
1330d8d19677SJose E. Roman   Input Parameters:
1331bb7acecfSBarry Smith + dac - the `DM` object
1332bb7acecfSBarry Smith - daf - the second, finer `DM` object
133347c6ae99SBarry Smith 
133447c6ae99SBarry Smith   Output Parameter:
13356dbf9973SLawrence Mitchell . mat - the injection
133647c6ae99SBarry Smith 
133747c6ae99SBarry Smith   Level: developer
133847c6ae99SBarry Smith 
1339a4e35b19SJacob Faibussowitsch   Notes:
1340a4e35b19SJacob Faibussowitsch   This is an operator that applied to a vector obtained with `DMCreateGlobalVector()` on the
1341a4e35b19SJacob Faibussowitsch   fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting
1342a4e35b19SJacob Faibussowitsch   the values on the coarse grid points. This compares to the operator obtained by
1343a4e35b19SJacob Faibussowitsch   `DMCreateRestriction()` or the transpose of the operator obtained by
1344a4e35b19SJacob Faibussowitsch   `DMCreateInterpolation()` that uses a "local weighted average" of the values around the
1345a4e35b19SJacob Faibussowitsch   coarse grid point as the coarse grid value.
1346a4e35b19SJacob Faibussowitsch 
1347bb7acecfSBarry 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
1348bb7acecfSBarry Smith   `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
134985afcc9aSBarry Smith 
13501cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1351bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
135247c6ae99SBarry Smith @*/
1353d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1354d71ae5a4SJacob Faibussowitsch {
135547c6ae99SBarry Smith   PetscFunctionBegin;
1356a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1357a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13584f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1360dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
136347c6ae99SBarry Smith }
136447c6ae99SBarry Smith 
1365b412c318SBarry Smith /*@
1366bb7acecfSBarry 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
1367bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1368bd041c0cSMatthew G. Knepley 
136920f4b53cSBarry Smith   Collective
1370bd041c0cSMatthew G. Knepley 
1371d8d19677SJose E. Roman   Input Parameters:
1372bb7acecfSBarry Smith + dmc - the target `DM` object
13738c1c0954SStefano Zampini - dmf - the source `DM` object, can be `NULL`
1374bd041c0cSMatthew G. Knepley 
1375bd041c0cSMatthew G. Knepley   Output Parameter:
1376b4937a87SMatthew G. Knepley . mat - the mass matrix
1377bd041c0cSMatthew G. Knepley 
1378bd041c0cSMatthew G. Knepley   Level: developer
1379bd041c0cSMatthew G. Knepley 
1380bb7acecfSBarry Smith   Notes:
1381bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1382bb7acecfSBarry Smith 
13838c1c0954SStefano 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()`
1384bb7acecfSBarry Smith 
138542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1386bd041c0cSMatthew G. Knepley @*/
1387d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1388d71ae5a4SJacob Faibussowitsch {
1389bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1390b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
13918c1c0954SStefano Zampini   if (!dmf) dmf = dmc;
1392b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13934f572ea9SToby Isaac   PetscAssertPointer(mat, 3);
13948c1c0954SStefano Zampini   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, dmc, dmf, 0, 0));
1395dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13968c1c0954SStefano Zampini   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, dmc, dmf, 0, 0));
13973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1398b4937a87SMatthew G. Knepley }
1399b4937a87SMatthew G. Knepley 
1400b4937a87SMatthew G. Knepley /*@
1401bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1402b4937a87SMatthew G. Knepley 
140320f4b53cSBarry Smith   Collective
1404b4937a87SMatthew G. Knepley 
1405b4937a87SMatthew G. Knepley   Input Parameter:
1406bb7acecfSBarry Smith . dm - the `DM` object
1407b4937a87SMatthew G. Knepley 
14087dcfde4dSJose E. Roman   Output Parameters:
14098e9849d2SStefano Zampini + llm - the local lumped mass matrix, which is a diagonal matrix, represented as a vector
14108e9849d2SStefano Zampini - lm  - the global lumped mass matrix, which is a diagonal matrix, represented as a vector
1411b4937a87SMatthew G. Knepley 
1412b4937a87SMatthew G. Knepley   Level: developer
1413b4937a87SMatthew G. Knepley 
1414bb7acecfSBarry Smith   Note:
1415bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1416bb7acecfSBarry Smith 
141760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1418b4937a87SMatthew G. Knepley @*/
14198e9849d2SStefano Zampini PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *llm, Vec *lm)
1420d71ae5a4SJacob Faibussowitsch {
1421b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1422b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14238e9849d2SStefano Zampini   if (llm) PetscAssertPointer(llm, 2);
14248e9849d2SStefano Zampini   if (lm) PetscAssertPointer(lm, 3);
14258e9849d2SStefano Zampini   if (llm || lm) PetscUseTypeMethod(dm, createmassmatrixlumped, llm, lm);
14263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1427bd041c0cSMatthew G. Knepley }
1428bd041c0cSMatthew G. Knepley 
1429bd041c0cSMatthew G. Knepley /*@
1430bb7acecfSBarry Smith   DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1431bb7acecfSBarry Smith   of a PDE on the `DM`.
143247c6ae99SBarry Smith 
143320f4b53cSBarry Smith   Collective
143447c6ae99SBarry Smith 
1435d8d19677SJose E. Roman   Input Parameters:
1436bb7acecfSBarry Smith + dm    - the `DM` object
1437bb7acecfSBarry Smith - ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
143847c6ae99SBarry Smith 
143947c6ae99SBarry Smith   Output Parameter:
144047c6ae99SBarry Smith . coloring - the coloring
144147c6ae99SBarry Smith 
14421bf8429eSBarry Smith   Level: developer
14431bf8429eSBarry Smith 
1444ec5066bdSBarry Smith   Notes:
1445bb7acecfSBarry 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
1446bb7acecfSBarry Smith   matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1447ec5066bdSBarry Smith 
1448bb7acecfSBarry Smith   This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14491bf8429eSBarry 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,
14501bf8429eSBarry Smith   otherwise an error will be generated.
1451ec5066bdSBarry Smith 
14521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1453aab9d709SJed Brown @*/
1454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1455d71ae5a4SJacob Faibussowitsch {
145647c6ae99SBarry Smith   PetscFunctionBegin;
1457171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14584f572ea9SToby Isaac   PetscAssertPointer(coloring, 3);
1459dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146147c6ae99SBarry Smith }
146247c6ae99SBarry Smith 
1463b412c318SBarry Smith /*@
1464bb7acecfSBarry Smith   DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
146547c6ae99SBarry Smith 
146620f4b53cSBarry Smith   Collective
146747c6ae99SBarry Smith 
146847c6ae99SBarry Smith   Input Parameter:
1469bb7acecfSBarry Smith . dm - the `DM` object
147047c6ae99SBarry Smith 
147147c6ae99SBarry Smith   Output Parameter:
147247c6ae99SBarry Smith . mat - the empty Jacobian
147347c6ae99SBarry Smith 
147420f4b53cSBarry Smith   Options Database Key:
1475bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1476f27dd7c6SMatthew G. Knepley 
147720f4b53cSBarry Smith   Level: beginner
147820f4b53cSBarry Smith 
147995452b02SPatrick Sanan   Notes:
148095452b02SPatrick Sanan   This properly preallocates the number of nonzeros in the sparse matrix so you
148194013140SBarry Smith   do not need to do it yourself.
148294013140SBarry Smith 
148394013140SBarry Smith   By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1484bb7acecfSBarry Smith   the nonzero pattern call `DMSetMatrixPreallocateOnly()`
148594013140SBarry Smith 
1486bb7acecfSBarry Smith   For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
148794013140SBarry Smith   internally by PETSc.
148894013140SBarry Smith 
1489bb7acecfSBarry Smith   For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1490bb7acecfSBarry Smith   `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
149194013140SBarry Smith 
14921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1493aab9d709SJed Brown @*/
1494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1495d71ae5a4SJacob Faibussowitsch {
149647c6ae99SBarry Smith   PetscFunctionBegin;
1497171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14984f572ea9SToby Isaac   PetscAssertPointer(mat, 2);
14999566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
15009566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1501dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
150276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1503c6b011d8SStefano Zampini     DM mdm;
1504c6b011d8SStefano Zampini 
15059566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
15067a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1507c6b011d8SStefano Zampini   }
1508e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1509e5e52638SMatthew G. Knepley   if (dm->Nf) {
1510e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1511649ef022SMatthew Knepley     PetscInt     Nf, f;
1512e571a35bSMatthew G. Knepley 
15139566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1514649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1515649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
15169566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
15179566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
15189566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1519649ef022SMatthew Knepley         break;
1520e571a35bSMatthew G. Knepley       }
1521649ef022SMatthew Knepley     }
1522649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1523649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15249566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15259566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15269566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1527e571a35bSMatthew G. Knepley       }
1528e571a35bSMatthew G. Knepley     }
1529e571a35bSMatthew G. Knepley   }
15309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
153247c6ae99SBarry Smith }
153347c6ae99SBarry Smith 
1534732e2eb9SMatthew G Knepley /*@
1535a4e35b19SJacob Faibussowitsch   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and
1536a4e35b19SJacob Faibussowitsch   `ISLocalToGlobalMapping` will be properly set, but the data structures to store values in the
1537a4e35b19SJacob Faibussowitsch   matrices will not be preallocated.
1538aa0f6e3cSJed Brown 
153920f4b53cSBarry Smith   Logically Collective
1540aa0f6e3cSJed Brown 
1541aa0f6e3cSJed Brown   Input Parameters:
1542bb7acecfSBarry Smith + dm   - the `DM`
1543bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1544aa0f6e3cSJed Brown 
1545aa0f6e3cSJed Brown   Level: developer
1546aa0f6e3cSJed Brown 
154773ff1848SBarry Smith   Note:
1548a4e35b19SJacob Faibussowitsch   This is most useful to reduce initialization costs when `MatSetPreallocationCOO()` and
1549a4e35b19SJacob Faibussowitsch   `MatSetValuesCOO()` will be used.
1550a4e35b19SJacob Faibussowitsch 
15511cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1552aa0f6e3cSJed Brown @*/
1553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1554d71ae5a4SJacob Faibussowitsch {
1555aa0f6e3cSJed Brown   PetscFunctionBegin;
1556aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1557aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1559aa0f6e3cSJed Brown }
1560aa0f6e3cSJed Brown 
1561aa0f6e3cSJed Brown /*@
1562bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1563732e2eb9SMatthew G Knepley   preallocated but the nonzero structure and zero values will not be set.
1564732e2eb9SMatthew G Knepley 
156520f4b53cSBarry Smith   Logically Collective
1566732e2eb9SMatthew G Knepley 
1567d8d19677SJose E. Roman   Input Parameters:
1568bb7acecfSBarry Smith + dm   - the `DM`
1569bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1570732e2eb9SMatthew G Knepley 
157120f4b53cSBarry Smith   Options Database Key:
1572bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1573f27dd7c6SMatthew G. Knepley 
157420f4b53cSBarry Smith   Level: developer
157520f4b53cSBarry Smith 
15761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1577732e2eb9SMatthew G Knepley @*/
1578d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1579d71ae5a4SJacob Faibussowitsch {
1580732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1581732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1582732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1584732e2eb9SMatthew G Knepley }
1585732e2eb9SMatthew G Knepley 
1586b06ff27eSHong Zhang /*@
1587bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1588bb7acecfSBarry Smith   but the array for numerical values will not be allocated.
1589b06ff27eSHong Zhang 
159020f4b53cSBarry Smith   Logically Collective
1591b06ff27eSHong Zhang 
1592d8d19677SJose E. Roman   Input Parameters:
1593bb7acecfSBarry Smith + dm   - the `DM`
1594da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1595b06ff27eSHong Zhang 
1596b06ff27eSHong Zhang   Level: developer
1597bb7acecfSBarry Smith 
15981cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1599b06ff27eSHong Zhang @*/
1600d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1601d71ae5a4SJacob Faibussowitsch {
1602b06ff27eSHong Zhang   PetscFunctionBegin;
1603b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604b06ff27eSHong Zhang   dm->structure_only = only;
16053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1606b06ff27eSHong Zhang }
1607b06ff27eSHong Zhang 
1608863027abSJed Brown /*@
1609863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1610863027abSJed Brown 
161120f4b53cSBarry Smith   Logically Collective
1612863027abSJed Brown 
1613863027abSJed Brown   Input Parameters:
1614863027abSJed Brown + dm    - the `DM`
1615863027abSJed Brown - btype - block by topological point or field node
1616863027abSJed Brown 
161720f4b53cSBarry Smith   Options Database Key:
16180e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1619863027abSJed Brown 
162020f4b53cSBarry Smith   Level: advanced
162120f4b53cSBarry Smith 
16221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1623863027abSJed Brown @*/
1624863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1625863027abSJed Brown {
1626863027abSJed Brown   PetscFunctionBegin;
1627863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1628863027abSJed Brown   dm->blocking_type = btype;
16293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1630863027abSJed Brown }
1631863027abSJed Brown 
1632863027abSJed Brown /*@
1633863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1634863027abSJed Brown 
1635863027abSJed Brown   Not Collective
1636863027abSJed Brown 
16372fe279fdSBarry Smith   Input Parameter:
1638863027abSJed Brown . dm - the `DM`
1639863027abSJed Brown 
16402fe279fdSBarry Smith   Output Parameter:
1641863027abSJed Brown . btype - block by topological point or field node
1642863027abSJed Brown 
1643863027abSJed Brown   Level: advanced
1644863027abSJed Brown 
16451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1646863027abSJed Brown @*/
1647863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1648863027abSJed Brown {
1649863027abSJed Brown   PetscFunctionBegin;
1650863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16514f572ea9SToby Isaac   PetscAssertPointer(btype, 2);
1652863027abSJed Brown   *btype = dm->blocking_type;
16533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1654863027abSJed Brown }
1655863027abSJed Brown 
1656a89ea682SMatthew G Knepley /*@C
1657bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1658a89ea682SMatthew G Knepley 
1659a89ea682SMatthew G Knepley   Not Collective
1660a89ea682SMatthew G Knepley 
1661a89ea682SMatthew G Knepley   Input Parameters:
1662bb7acecfSBarry Smith + dm    - the `DM` object
1663a5b23f4aSJose E. Roman . count - The minimum size
166420f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1665a89ea682SMatthew G Knepley 
1666a89ea682SMatthew G Knepley   Output Parameter:
166760225df5SJacob Faibussowitsch . mem - the work array
1668a89ea682SMatthew G Knepley 
1669a89ea682SMatthew G Knepley   Level: developer
1670a89ea682SMatthew G Knepley 
167173ff1848SBarry Smith   Notes:
1672da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1673bb7acecfSBarry Smith 
1674bb7acecfSBarry Smith   The array may contain nonzero values
1675bb7acecfSBarry Smith 
16761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1677a89ea682SMatthew G Knepley @*/
1678d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1679d71ae5a4SJacob Faibussowitsch {
1680aa1993deSMatthew G Knepley   DMWorkLink  link;
168169291d52SBarry Smith   PetscMPIInt dsize;
1682a89ea682SMatthew G Knepley 
1683a89ea682SMatthew G Knepley   PetscFunctionBegin;
1684a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16854f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1686442f3b32SStefano Zampini   if (!count) {
1687442f3b32SStefano Zampini     *(void **)mem = NULL;
1688442f3b32SStefano Zampini     PetscFunctionReturn(PETSC_SUCCESS);
1689442f3b32SStefano Zampini   }
1690aa1993deSMatthew G Knepley   if (dm->workin) {
1691aa1993deSMatthew G Knepley     link       = dm->workin;
1692aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1693aa1993deSMatthew G Knepley   } else {
16944dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1695a89ea682SMatthew G Knepley   }
1696b77fa653SStefano Zampini   /* Avoid MPI_Type_size for most used datatypes
1697b77fa653SStefano Zampini      Get size directly */
1698b77fa653SStefano Zampini   if (dtype == MPIU_INT) dsize = sizeof(PetscInt);
1699b77fa653SStefano Zampini   else if (dtype == MPIU_REAL) dsize = sizeof(PetscReal);
1700b77fa653SStefano Zampini #if defined(PETSC_USE_64BIT_INDICES)
1701b77fa653SStefano Zampini   else if (dtype == MPI_INT) dsize = sizeof(int);
1702b77fa653SStefano Zampini #endif
1703b77fa653SStefano Zampini #if defined(PETSC_USE_COMPLEX)
1704b77fa653SStefano Zampini   else if (dtype == MPIU_SCALAR) dsize = sizeof(PetscScalar);
1705b77fa653SStefano Zampini #endif
1706b77fa653SStefano Zampini   else PetscCallMPI(MPI_Type_size(dtype, &dsize));
1707b77fa653SStefano Zampini 
17085056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
17099566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
17109566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1711854ce69bSBarry Smith     link->bytes = dsize * count;
1712aa1993deSMatthew G Knepley   }
1713aa1993deSMatthew G Knepley   link->next  = dm->workout;
1714aa1993deSMatthew G Knepley   dm->workout = link;
1715cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
171600d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
171700d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
171800d952a4SJed Brown #endif
1719aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
17203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1721a89ea682SMatthew G Knepley }
1722a89ea682SMatthew G Knepley 
1723aa1993deSMatthew G Knepley /*@C
1724bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1725aa1993deSMatthew G Knepley 
1726aa1993deSMatthew G Knepley   Not Collective
1727aa1993deSMatthew G Knepley 
1728aa1993deSMatthew G Knepley   Input Parameters:
1729bb7acecfSBarry Smith + dm    - the `DM` object
1730a5b23f4aSJose E. Roman . count - The minimum size
173120f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1732aa1993deSMatthew G Knepley 
1733aa1993deSMatthew G Knepley   Output Parameter:
173460225df5SJacob Faibussowitsch . mem - the work array
1735aa1993deSMatthew G Knepley 
1736aa1993deSMatthew G Knepley   Level: developer
1737aa1993deSMatthew G Knepley 
173873ff1848SBarry Smith   Developer Note:
1739bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1740147403d9SBarry Smith 
17411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1742aa1993deSMatthew G Knepley @*/
1743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1744d71ae5a4SJacob Faibussowitsch {
1745aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1746aa1993deSMatthew G Knepley 
1747aa1993deSMatthew G Knepley   PetscFunctionBegin;
1748aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17494f572ea9SToby Isaac   PetscAssertPointer(mem, 4);
1750a4e35b19SJacob Faibussowitsch   (void)count;
1751a4e35b19SJacob Faibussowitsch   (void)dtype;
1752442f3b32SStefano Zampini   if (!*(void **)mem) PetscFunctionReturn(PETSC_SUCCESS);
1753aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1754aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1755aa1993deSMatthew G Knepley       *p            = link->next;
1756aa1993deSMatthew G Knepley       link->next    = dm->workin;
1757aa1993deSMatthew G Knepley       dm->workin    = link;
17580298fd71SBarry Smith       *(void **)mem = NULL;
17593ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1760aa1993deSMatthew G Knepley     }
1761aa1993deSMatthew G Knepley   }
1762aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1763aa1993deSMatthew G Knepley }
1764e7c4fc90SDmitry Karpeev 
17658cda7954SMatthew G. Knepley /*@C
1766bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1767bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17688cda7954SMatthew G. Knepley 
176920f4b53cSBarry Smith   Logically Collective; No Fortran Support
17708cda7954SMatthew G. Knepley 
17718cda7954SMatthew G. Knepley   Input Parameters:
1772bb7acecfSBarry Smith + dm     - The `DM`
17738cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17748cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17758cda7954SMatthew G. Knepley 
177620f4b53cSBarry Smith   Calling sequence of `nullsp`:
1777bb7acecfSBarry Smith + dm        - The present `DM`
1778bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1779147403d9SBarry Smith . field     - The field number in dm
1780147403d9SBarry Smith - nullSpace - The nullspace for the given field
17818cda7954SMatthew G. Knepley 
178249762cbcSSatish Balay   Level: intermediate
178349762cbcSSatish Balay 
17841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1785147403d9SBarry Smith @*/
1786a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1787d71ae5a4SJacob Faibussowitsch {
1788435a35e8SMatthew G Knepley   PetscFunctionBegin;
1789435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17907a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1791435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1793435a35e8SMatthew G Knepley }
1794435a35e8SMatthew G Knepley 
17958cda7954SMatthew G. Knepley /*@C
1796bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17978cda7954SMatthew G. Knepley 
179820f4b53cSBarry Smith   Not Collective; No Fortran Support
17998cda7954SMatthew G. Knepley 
18008cda7954SMatthew G. Knepley   Input Parameters:
1801bb7acecfSBarry Smith + dm    - The `DM`
18028cda7954SMatthew G. Knepley - field - The field number for the nullspace
18038cda7954SMatthew G. Knepley 
18048cda7954SMatthew G. Knepley   Output Parameter:
18058cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
18068cda7954SMatthew G. Knepley 
180720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1808147403d9SBarry Smith + dm        - The present DM
1809147403d9SBarry Smith . origField - The field number given above, in the original DM
1810147403d9SBarry Smith . field     - The field number in dm
1811147403d9SBarry Smith - nullSpace - The nullspace for the given field
18128cda7954SMatthew G. Knepley 
181349762cbcSSatish Balay   Level: intermediate
181449762cbcSSatish Balay 
18151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1816147403d9SBarry Smith @*/
1817a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1818d71ae5a4SJacob Faibussowitsch {
18190a50eb56SMatthew G. Knepley   PetscFunctionBegin;
18200a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18214f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18227a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
18230a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
18243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18250a50eb56SMatthew G. Knepley }
18260a50eb56SMatthew G. Knepley 
18278cda7954SMatthew G. Knepley /*@C
1828bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18298cda7954SMatthew G. Knepley 
183020f4b53cSBarry Smith   Logically Collective; No Fortran Support
18318cda7954SMatthew G. Knepley 
18328cda7954SMatthew G. Knepley   Input Parameters:
1833bb7acecfSBarry Smith + dm     - The `DM`
18348cda7954SMatthew G. Knepley . field  - The field number for the nullspace
18358cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
18368cda7954SMatthew G. Knepley 
183720f4b53cSBarry Smith   Calling sequence of `nullsp`:
1838bb7acecfSBarry Smith + dm        - The present `DM`
1839bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1840147403d9SBarry Smith . field     - The field number in dm
1841147403d9SBarry Smith - nullSpace - The nullspace for the given field
18428cda7954SMatthew G. Knepley 
184349762cbcSSatish Balay   Level: intermediate
184449762cbcSSatish Balay 
18451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1846bb7acecfSBarry Smith           `MatNullSpace`
1847147403d9SBarry Smith @*/
1848a4e35b19SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1849d71ae5a4SJacob Faibussowitsch {
1850f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1851f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18527a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1853f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1855f9d4088aSMatthew G. Knepley }
1856f9d4088aSMatthew G. Knepley 
18578cda7954SMatthew G. Knepley /*@C
1858bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18598cda7954SMatthew G. Knepley 
186020f4b53cSBarry Smith   Not Collective; No Fortran Support
18618cda7954SMatthew G. Knepley 
18628cda7954SMatthew G. Knepley   Input Parameters:
1863bb7acecfSBarry Smith + dm    - The `DM`
18648cda7954SMatthew G. Knepley - field - The field number for the nullspace
18658cda7954SMatthew G. Knepley 
18668cda7954SMatthew G. Knepley   Output Parameter:
18678cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18688cda7954SMatthew G. Knepley 
186920f4b53cSBarry Smith   Calling sequence of `nullsp`:
1870bb7acecfSBarry Smith + dm        - The present `DM`
1871bb7acecfSBarry Smith . origField - The field number given above, in the original `DM`
1872147403d9SBarry Smith . field     - The field number in dm
1873147403d9SBarry Smith - nullSpace - The nullspace for the given field
18748cda7954SMatthew G. Knepley 
187549762cbcSSatish Balay   Level: intermediate
187649762cbcSSatish Balay 
18771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1878bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1879147403d9SBarry Smith @*/
1880a4e35b19SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1881d71ae5a4SJacob Faibussowitsch {
1882f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1883f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18844f572ea9SToby Isaac   PetscAssertPointer(nullsp, 3);
18857a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1886f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1888f9d4088aSMatthew G. Knepley }
1889f9d4088aSMatthew G. Knepley 
18904f3b5142SJed Brown /*@C
1891bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18924d343eeaSMatthew G Knepley 
189320f4b53cSBarry Smith   Not Collective; No Fortran Support
18944d343eeaSMatthew G Knepley 
18954d343eeaSMatthew G Knepley   Input Parameter:
1896bb7acecfSBarry Smith . dm - the `DM` object
18974d343eeaSMatthew G Knepley 
18984d343eeaSMatthew G Knepley   Output Parameters:
189920f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
190020f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
190120f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
19024d343eeaSMatthew G Knepley 
19034d343eeaSMatthew G Knepley   Level: intermediate
19044d343eeaSMatthew G Knepley 
1905bb7acecfSBarry Smith   Note:
190673ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `fieldNames` should be freed with
190773ff1848SBarry Smith   `PetscFree()`, every entry of `fields` should be destroyed with `ISDestroy()`, and both arrays should be freed with
1908bb7acecfSBarry Smith   `PetscFree()`.
190921c9b008SJed Brown 
191073ff1848SBarry Smith   Developer Note:
1911bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1912bb7acecfSBarry Smith   likely be removed.
1913bb7acecfSBarry Smith 
19141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1915bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
19164d343eeaSMatthew G Knepley @*/
1917d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1918d71ae5a4SJacob Faibussowitsch {
191937d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19204d343eeaSMatthew G Knepley 
19214d343eeaSMatthew G Knepley   PetscFunctionBegin;
19224d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
192369ca1f37SDmitry Karpeev   if (numFields) {
19244f572ea9SToby Isaac     PetscAssertPointer(numFields, 2);
192569ca1f37SDmitry Karpeev     *numFields = 0;
192669ca1f37SDmitry Karpeev   }
192737d0c07bSMatthew G Knepley   if (fieldNames) {
19284f572ea9SToby Isaac     PetscAssertPointer(fieldNames, 3);
19290298fd71SBarry Smith     *fieldNames = NULL;
193069ca1f37SDmitry Karpeev   }
193169ca1f37SDmitry Karpeev   if (fields) {
19324f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
19330298fd71SBarry Smith     *fields = NULL;
193469ca1f37SDmitry Karpeev   }
19359566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
193637d0c07bSMatthew G Knepley   if (section) {
19373a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
193837d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
193937d0c07bSMatthew G Knepley 
19409566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19419566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19439566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
194437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
194537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
194737d0c07bSMatthew G Knepley     }
194837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
194937d0c07bSMatthew G Knepley       PetscInt gdof;
195037d0c07bSMatthew G Knepley 
19519566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195237d0c07bSMatthew G Knepley       if (gdof > 0) {
195337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19543a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
195537d0c07bSMatthew G Knepley 
19569566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19579566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19583a544194SStefano Zampini           fpdof = fdof - fcdof;
19593a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19603a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19613a544194SStefano Zampini             fieldNc[f] = 1;
19623a544194SStefano Zampini           }
19633a544194SStefano Zampini           fieldSizes[f] += fpdof;
196437d0c07bSMatthew G Knepley         }
196537d0c07bSMatthew G Knepley       }
196637d0c07bSMatthew G Knepley     }
196737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19689566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
196937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
197037d0c07bSMatthew G Knepley     }
197137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
197237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
197337d0c07bSMatthew G Knepley 
19749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
197537d0c07bSMatthew G Knepley       if (gdof > 0) {
19769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
197737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
197837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
197937d0c07bSMatthew G Knepley 
19809566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19819566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1982ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
198337d0c07bSMatthew G Knepley         }
198437d0c07bSMatthew G Knepley       }
198537d0c07bSMatthew G Knepley     }
19868865f1eaSKarl Rupp     if (numFields) *numFields = nF;
198737d0c07bSMatthew G Knepley     if (fieldNames) {
19889566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
198937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
199037d0c07bSMatthew G Knepley         const char *fieldName;
199137d0c07bSMatthew G Knepley 
19929566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19939566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
199437d0c07bSMatthew G Knepley       }
199537d0c07bSMatthew G Knepley     }
199637d0c07bSMatthew G Knepley     if (fields) {
19979566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
199837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19993a544194SStefano Zampini         PetscInt bs, in[2], out[2];
20003a544194SStefano Zampini 
20019566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
20023a544194SStefano Zampini         in[0] = -fieldNc[f];
20033a544194SStefano Zampini         in[1] = fieldNc[f];
2004462c564dSBarry Smith         PetscCallMPI(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
20053a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
20069566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
200737d0c07bSMatthew G Knepley       }
200837d0c07bSMatthew G Knepley     }
20099566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
2010dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
20113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20124d343eeaSMatthew G Knepley }
20134d343eeaSMatthew G Knepley 
201416621825SDmitry Karpeev /*@C
2015bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
2016a4e35b19SJacob Faibussowitsch   corresponding to different fields.
2017e7c4fc90SDmitry Karpeev 
201820f4b53cSBarry Smith   Not Collective; No Fortran Support
2019e7c4fc90SDmitry Karpeev 
2020e7c4fc90SDmitry Karpeev   Input Parameter:
2021bb7acecfSBarry Smith . dm - the `DM` object
2022e7c4fc90SDmitry Karpeev 
2023e7c4fc90SDmitry Karpeev   Output Parameters:
202420f4b53cSBarry Smith + len      - The number of fields (or `NULL` if not requested)
202520f4b53cSBarry Smith . namelist - The name for each field (or `NULL` if not requested)
202620f4b53cSBarry Smith . islist   - The global indices for each field (or `NULL` if not requested)
202720f4b53cSBarry Smith - dmlist   - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
2028e7c4fc90SDmitry Karpeev 
2029e7c4fc90SDmitry Karpeev   Level: intermediate
2030e7c4fc90SDmitry Karpeev 
2031a4e35b19SJacob Faibussowitsch   Notes:
2032a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding field, defined by
2033a4e35b19SJacob Faibussowitsch   `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2034a4e35b19SJacob Faibussowitsch 
2035a4e35b19SJacob Faibussowitsch   The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2036a4e35b19SJacob Faibussowitsch 
203773ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
203873ff1848SBarry Smith   `PetscFree()`, every entry of `islist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2039bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2040e7c4fc90SDmitry Karpeev 
204160225df5SJacob Faibussowitsch   Developer Notes:
2042bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2043bb7acecfSBarry Smith 
204473ff1848SBarry Smith   Unlike  `DMRefine()`, `DMCoarsen()`, and `DMCreateDomainDecomposition()` this provides no mechanism to provide hooks that are called after the
204573ff1848SBarry Smith   decomposition is computed.
204673ff1848SBarry Smith 
204773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
2048e7c4fc90SDmitry Karpeev @*/
2049d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2050d71ae5a4SJacob Faibussowitsch {
2051e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2052e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20538865f1eaSKarl Rupp   if (len) {
20544f572ea9SToby Isaac     PetscAssertPointer(len, 2);
20558865f1eaSKarl Rupp     *len = 0;
20568865f1eaSKarl Rupp   }
20578865f1eaSKarl Rupp   if (namelist) {
20584f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
2059ea78f98cSLisandro Dalcin     *namelist = NULL;
20608865f1eaSKarl Rupp   }
20618865f1eaSKarl Rupp   if (islist) {
20624f572ea9SToby Isaac     PetscAssertPointer(islist, 4);
2063ea78f98cSLisandro Dalcin     *islist = NULL;
20648865f1eaSKarl Rupp   }
20658865f1eaSKarl Rupp   if (dmlist) {
20664f572ea9SToby Isaac     PetscAssertPointer(dmlist, 5);
2067ea78f98cSLisandro Dalcin     *dmlist = NULL;
20688865f1eaSKarl Rupp   }
2069f3f0edfdSDmitry Karpeev   /*
2070f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2071f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2072f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2073f3f0edfdSDmitry Karpeev    */
20747a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
207516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2076435a35e8SMatthew G Knepley     PetscSection section;
2077435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2078435a35e8SMatthew G Knepley 
20799566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20809566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2081435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2082f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20839566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20849566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20859566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2086435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2087435a35e8SMatthew G Knepley         const char *fieldName;
2088435a35e8SMatthew G Knepley 
20899566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
209003dc3394SMatthew G. Knepley         if (namelist) {
20919566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20929566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2093435a35e8SMatthew G Knepley         }
209403dc3394SMatthew G. Knepley       }
2095435a35e8SMatthew G Knepley     } else {
20969566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2097e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20980298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2099e7c4fc90SDmitry Karpeev     }
2100dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
21013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
210216621825SDmitry Karpeev }
210316621825SDmitry Karpeev 
21045d83a8b1SBarry Smith /*@
210520f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
210620f4b53cSBarry Smith   The fields are defined by `DMCreateFieldIS()`.
2107435a35e8SMatthew G Knepley 
2108435a35e8SMatthew G Knepley   Not collective
2109435a35e8SMatthew G Knepley 
2110435a35e8SMatthew G Knepley   Input Parameters:
2111bb7acecfSBarry Smith + dm        - The `DM` object
2112bb7acecfSBarry Smith . numFields - The number of fields to select
21132adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2114435a35e8SMatthew G Knepley 
2115435a35e8SMatthew G Knepley   Output Parameters:
2116b6971eaeSBarry Smith + is    - The global indices for all the degrees of freedom in the new sub `DM`, use `NULL` if not needed
2117b6971eaeSBarry Smith - subdm - The `DM` for the subproblem, use `NULL` if not needed
2118435a35e8SMatthew G Knepley 
211920f4b53cSBarry Smith   Level: intermediate
212020f4b53cSBarry Smith 
2121bb7acecfSBarry Smith   Note:
2122bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21235d3b26e6SMatthew G. Knepley 
212460225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
2125435a35e8SMatthew G Knepley @*/
2126d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2127d71ae5a4SJacob Faibussowitsch {
2128435a35e8SMatthew G Knepley   PetscFunctionBegin;
2129435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21304f572ea9SToby Isaac   PetscAssertPointer(fields, 3);
21314f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 4);
21324f572ea9SToby Isaac   if (subdm) PetscAssertPointer(subdm, 5);
2133dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
21343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2135435a35e8SMatthew G Knepley }
2136435a35e8SMatthew G Knepley 
21372adcc780SMatthew G. Knepley /*@C
2138bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21392adcc780SMatthew G. Knepley 
21402adcc780SMatthew G. Knepley   Not collective
21412adcc780SMatthew G. Knepley 
2142d8d19677SJose E. Roman   Input Parameters:
2143bb7acecfSBarry Smith + dms - The `DM` objects
2144bb7acecfSBarry Smith - n   - The number of `DM`s
21452adcc780SMatthew G. Knepley 
21462adcc780SMatthew G. Knepley   Output Parameters:
2147bb7acecfSBarry Smith + is      - The global indices for each of subproblem within the super `DM`, or NULL
2148bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21492adcc780SMatthew G. Knepley 
215020f4b53cSBarry Smith   Level: intermediate
215120f4b53cSBarry Smith 
2152bb7acecfSBarry Smith   Note:
2153bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21545d3b26e6SMatthew G. Knepley 
215573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`, `DMCreateDomainDecomposition()`
21562adcc780SMatthew G. Knepley @*/
21575d83a8b1SBarry Smith PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS *is[], DM *superdm)
2158d71ae5a4SJacob Faibussowitsch {
21592adcc780SMatthew G. Knepley   PetscInt i;
21602adcc780SMatthew G. Knepley 
21612adcc780SMatthew G. Knepley   PetscFunctionBegin;
21624f572ea9SToby Isaac   PetscAssertPointer(dms, 1);
2163ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21644f572ea9SToby Isaac   if (is) PetscAssertPointer(is, 3);
21654f572ea9SToby Isaac   PetscAssertPointer(superdm, 4);
2166bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2167bb7acecfSBarry Smith   if (n) {
2168b9d85ea2SLisandro Dalcin     DM dm = dms[0];
216900045ab3SPierre Jolivet     PetscCheck(dm->ops->createsuperdm, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No method createsuperdm for DM of type %s", ((PetscObject)dm)->type_name);
2170dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21712adcc780SMatthew G. Knepley   }
21723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21732adcc780SMatthew G. Knepley }
21742adcc780SMatthew G. Knepley 
217516621825SDmitry Karpeev /*@C
2176a4e35b19SJacob Faibussowitsch   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a
2177a4e35b19SJacob Faibussowitsch   problem into subproblems corresponding to restrictions to pairs of nested subdomains.
217816621825SDmitry Karpeev 
217920f4b53cSBarry Smith   Not Collective
218016621825SDmitry Karpeev 
218116621825SDmitry Karpeev   Input Parameter:
2182bb7acecfSBarry Smith . dm - the `DM` object
218316621825SDmitry Karpeev 
218416621825SDmitry Karpeev   Output Parameters:
218520f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
218620f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
218773ff1848SBarry Smith . innerislist - The global indices for each inner subdomain (or `NULL`, if not requested)
218873ff1848SBarry Smith . outerislist - The global indices for each outer subdomain (or `NULL`, if not requested)
218973ff1848SBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
219016621825SDmitry Karpeev 
219116621825SDmitry Karpeev   Level: intermediate
219216621825SDmitry Karpeev 
219373ff1848SBarry Smith   Notes:
2194a4e35b19SJacob Faibussowitsch   Each `IS` contains the global indices of the dofs of the corresponding subdomains with in the
2195a4e35b19SJacob Faibussowitsch   dofs of the original `DM`. The inner subdomains conceptually define a nonoverlapping
2196a4e35b19SJacob Faibussowitsch   covering, while outer subdomains can overlap.
2197a4e35b19SJacob Faibussowitsch 
2198a4e35b19SJacob Faibussowitsch   The optional list of `DM`s define a `DM` for each subproblem.
2199a4e35b19SJacob Faibussowitsch 
220073ff1848SBarry Smith   The user is responsible for freeing all requested arrays. In particular, every entry of `namelist` should be freed with
220173ff1848SBarry Smith   `PetscFree()`, every entry of `innerislist` and `outerislist` should be destroyed with `ISDestroy()`, every entry of `dmlist` should be destroyed with `DMDestroy()`,
2202bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
220316621825SDmitry Karpeev 
2204a4e35b19SJacob Faibussowitsch   Developer Notes:
220520f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2206bb7acecfSBarry Smith 
220773ff1848SBarry Smith   The names are inconsistent, the hooks use `DMSubDomainHook` which is nothing like `DMCreateDomainDecomposition()` while `DMRefineHook` is used for `DMRefine()`.
220873ff1848SBarry Smith 
220973ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`,
221073ff1848SBarry Smith           `DMSubDomainHookAdd()`, `DMSubDomainHookRemove()`,`DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`
221116621825SDmitry Karpeev @*/
2212d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2213d71ae5a4SJacob Faibussowitsch {
2214be081cd6SPeter Brune   DMSubDomainHookLink link;
2215be081cd6SPeter Brune   PetscInt            i, l;
221616621825SDmitry Karpeev 
221716621825SDmitry Karpeev   PetscFunctionBegin;
221816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22199371c9d4SSatish Balay   if (n) {
22204f572ea9SToby Isaac     PetscAssertPointer(n, 2);
22219371c9d4SSatish Balay     *n = 0;
22229371c9d4SSatish Balay   }
22239371c9d4SSatish Balay   if (namelist) {
22244f572ea9SToby Isaac     PetscAssertPointer(namelist, 3);
22259371c9d4SSatish Balay     *namelist = NULL;
22269371c9d4SSatish Balay   }
22279371c9d4SSatish Balay   if (innerislist) {
22284f572ea9SToby Isaac     PetscAssertPointer(innerislist, 4);
22299371c9d4SSatish Balay     *innerislist = NULL;
22309371c9d4SSatish Balay   }
22319371c9d4SSatish Balay   if (outerislist) {
22324f572ea9SToby Isaac     PetscAssertPointer(outerislist, 5);
22339371c9d4SSatish Balay     *outerislist = NULL;
22349371c9d4SSatish Balay   }
22359371c9d4SSatish Balay   if (dmlist) {
22364f572ea9SToby Isaac     PetscAssertPointer(dmlist, 6);
22379371c9d4SSatish Balay     *dmlist = NULL;
22389371c9d4SSatish Balay   }
2239f3f0edfdSDmitry Karpeev   /*
2240f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2241f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2242f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2243f3f0edfdSDmitry Karpeev    */
22447a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
224516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2246dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
224714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2248f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2249be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2250be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22519566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2252be081cd6SPeter Brune         }
2253648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2254e7c4fc90SDmitry Karpeev       }
225514a18fd3SPeter Brune     }
2256bb7acecfSBarry Smith     if (n) *n = l;
225714a18fd3SPeter Brune   }
22583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2259e30e807fSPeter Brune }
2260e30e807fSPeter Brune 
2261e30e807fSPeter Brune /*@C
226273ff1848SBarry Smith   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector for subdomains created with
226373ff1848SBarry Smith   `DMCreateDomainDecomposition()`
2264e30e807fSPeter Brune 
226520f4b53cSBarry Smith   Not Collective
2266e30e807fSPeter Brune 
2267e30e807fSPeter Brune   Input Parameters:
2268bb7acecfSBarry Smith + dm     - the `DM` object
226973ff1848SBarry Smith . n      - the number of subdomains
2270e30e807fSPeter Brune - subdms - the local subdomains
2271e30e807fSPeter Brune 
2272e30e807fSPeter Brune   Output Parameters:
22736b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2274e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2275e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2276e30e807fSPeter Brune 
227720f4b53cSBarry Smith   Level: developer
227820f4b53cSBarry Smith 
2279bb7acecfSBarry Smith   Note:
2280bb7acecfSBarry Smith   This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2281e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2282e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2283e30e807fSPeter Brune   solution and residual data.
2284e30e807fSPeter Brune 
228573ff1848SBarry Smith   Developer Note:
2286a4e35b19SJacob Faibussowitsch   Can the subdms input be anything or are they exactly the `DM` obtained from
2287a4e35b19SJacob Faibussowitsch   `DMCreateDomainDecomposition()`?
2288bb7acecfSBarry Smith 
22891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2290e30e807fSPeter Brune @*/
22915d83a8b1SBarry Smith PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter *iscat[], VecScatter *oscat[], VecScatter *gscat[])
2292d71ae5a4SJacob Faibussowitsch {
2293e30e807fSPeter Brune   PetscFunctionBegin;
2294e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22954f572ea9SToby Isaac   PetscAssertPointer(subdms, 3);
2296dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2298e7c4fc90SDmitry Karpeev }
2299e7c4fc90SDmitry Karpeev 
230047c6ae99SBarry Smith /*@
2301bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
230247c6ae99SBarry Smith 
230320f4b53cSBarry Smith   Collective
230447c6ae99SBarry Smith 
2305d8d19677SJose E. Roman   Input Parameters:
2306bb7acecfSBarry Smith + dm   - the `DM` object
2307bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
230847c6ae99SBarry Smith 
230947c6ae99SBarry Smith   Output Parameter:
231020f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2311ae0a1c52SMatthew G Knepley 
231220f4b53cSBarry Smith   Options Database Key:
2313412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2314412e9a14SMatthew G. Knepley 
231547c6ae99SBarry Smith   Level: developer
231647c6ae99SBarry Smith 
231720f4b53cSBarry Smith   Note:
231820f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
231920f4b53cSBarry Smith 
232073ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
232173ff1848SBarry Smith           `DMRefineHookAdd()`, `DMRefineHookRemove()`
232247c6ae99SBarry Smith @*/
2323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2324d71ae5a4SJacob Faibussowitsch {
2325c833c3b5SJed Brown   DMRefineHookLink link;
232647c6ae99SBarry Smith 
232747c6ae99SBarry Smith   PetscFunctionBegin;
2328732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23299566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2330dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23314057135bSMatthew G Knepley   if (*dmf) {
233243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23338865f1eaSKarl Rupp 
23349566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23358865f1eaSKarl Rupp 
2336644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23370598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2338656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23398865f1eaSKarl Rupp 
23409566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2341c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23421baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2343c833c3b5SJed Brown     }
2344c833c3b5SJed Brown   }
23459566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
23463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2347c833c3b5SJed Brown }
2348c833c3b5SJed Brown 
2349bb9467b5SJed Brown /*@C
2350c833c3b5SJed Brown   DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2351c833c3b5SJed Brown 
235220f4b53cSBarry Smith   Logically Collective; No Fortran Support
2353c833c3b5SJed Brown 
23544165533cSJose E. Roman   Input Parameters:
2355bb7acecfSBarry Smith + coarse     - `DM` on which to run a hook when interpolating to a finer level
2356bb7acecfSBarry Smith . refinehook - function to run when setting up the finer level
2357f826b5fcSPierre Jolivet . interphook - function to run to update data on finer levels (once per `SNESSolve()`)
235820f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2359c833c3b5SJed Brown 
236020f4b53cSBarry Smith   Calling sequence of `refinehook`:
2361bb7acecfSBarry Smith + coarse - coarse level `DM`
2362bb7acecfSBarry Smith . fine   - fine level `DM` to interpolate problem to
2363c833c3b5SJed Brown - ctx    - optional user-defined function context
2364c833c3b5SJed Brown 
236520f4b53cSBarry Smith   Calling sequence of `interphook`:
2366bb7acecfSBarry Smith + coarse - coarse level `DM`
2367c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid
2368bb7acecfSBarry Smith . fine   - fine level `DM` to update
2369c833c3b5SJed Brown - ctx    - optional user-defined function context
2370c833c3b5SJed Brown 
2371c833c3b5SJed Brown   Level: advanced
2372c833c3b5SJed Brown 
2373c833c3b5SJed Brown   Notes:
2374bb7acecfSBarry Smith   This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2375bb7acecfSBarry Smith   passed to fine grids while grid sequencing.
2376bb7acecfSBarry Smith 
2377bb7acecfSBarry Smith   The actual interpolation is done when `DMInterpolate()` is called.
2378c833c3b5SJed Brown 
2379c833c3b5SJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
2380c833c3b5SJed Brown 
23811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2382c833c3b5SJed Brown @*/
2383a4e35b19SJacob 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)
2384d71ae5a4SJacob Faibussowitsch {
2385c833c3b5SJed Brown   DMRefineHookLink link, *p;
2386c833c3b5SJed Brown 
2387c833c3b5SJed Brown   PetscFunctionBegin;
2388c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23893d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23903ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23913d8e3701SJed Brown   }
23929566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2393c833c3b5SJed Brown   link->refinehook = refinehook;
2394c833c3b5SJed Brown   link->interphook = interphook;
2395c833c3b5SJed Brown   link->ctx        = ctx;
23960298fd71SBarry Smith   link->next       = NULL;
2397c833c3b5SJed Brown   *p               = link;
23983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2399c833c3b5SJed Brown }
2400c833c3b5SJed Brown 
24013d8e3701SJed Brown /*@C
2402bb7acecfSBarry Smith   DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2403bb7acecfSBarry Smith   a nonlinear problem to a finer grid
24043d8e3701SJed Brown 
240520f4b53cSBarry Smith   Logically Collective; No Fortran Support
24063d8e3701SJed Brown 
24074165533cSJose E. Roman   Input Parameters:
2408bb7acecfSBarry Smith + coarse     - the `DM` on which to run a hook when restricting to a coarser level
2409bb7acecfSBarry Smith . refinehook - function to run when setting up a finer level
2410bb7acecfSBarry Smith . interphook - function to run to update data on finer levels
241120f4b53cSBarry Smith - ctx        - [optional] user-defined context for provide data for the hooks (may be `NULL`)
24123d8e3701SJed Brown 
24133d8e3701SJed Brown   Level: advanced
24143d8e3701SJed Brown 
2415bb7acecfSBarry Smith   Note:
24163d8e3701SJed Brown   This function does nothing if the hook is not in the list.
24173d8e3701SJed Brown 
24181cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
24193d8e3701SJed Brown @*/
2420d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2421d71ae5a4SJacob Faibussowitsch {
24223d8e3701SJed Brown   DMRefineHookLink link, *p;
24233d8e3701SJed Brown 
24243d8e3701SJed Brown   PetscFunctionBegin;
24253d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24263d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24273d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24283d8e3701SJed Brown       link = *p;
24293d8e3701SJed Brown       *p   = link->next;
24309566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24313d8e3701SJed Brown       break;
24323d8e3701SJed Brown     }
24333d8e3701SJed Brown   }
24343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24353d8e3701SJed Brown }
24363d8e3701SJed Brown 
2437c833c3b5SJed Brown /*@
2438bb7acecfSBarry Smith   DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2439c833c3b5SJed Brown 
2440c833c3b5SJed Brown   Collective if any hooks are
2441c833c3b5SJed Brown 
24424165533cSJose E. Roman   Input Parameters:
2443bb7acecfSBarry Smith + coarse - coarser `DM` to use as a base
2444bb7acecfSBarry Smith . interp - interpolation matrix, apply using `MatInterpolate()`
2445bb7acecfSBarry Smith - fine   - finer `DM` to update
2446c833c3b5SJed Brown 
2447c833c3b5SJed Brown   Level: developer
2448c833c3b5SJed Brown 
244973ff1848SBarry Smith   Developer Note:
2450bb7acecfSBarry Smith   This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2451bb7acecfSBarry Smith   an API with consistent terminology.
2452bb7acecfSBarry Smith 
24531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2454c833c3b5SJed Brown @*/
2455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2456d71ae5a4SJacob Faibussowitsch {
2457c833c3b5SJed Brown   DMRefineHookLink link;
2458c833c3b5SJed Brown 
2459c833c3b5SJed Brown   PetscFunctionBegin;
2460c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24611baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24624057135bSMatthew G Knepley   }
24633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
246447c6ae99SBarry Smith }
246547c6ae99SBarry Smith 
2466eb3f98d2SBarry Smith /*@
24671f3379b2SToby Isaac   DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24681f3379b2SToby Isaac 
246920f4b53cSBarry Smith   Collective
24701f3379b2SToby Isaac 
24714165533cSJose E. Roman   Input Parameters:
2472bb7acecfSBarry Smith + coarse    - coarse `DM`
2473bb7acecfSBarry Smith . fine      - fine `DM`
2474bb7acecfSBarry Smith . interp    - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2475bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2476bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24771f3379b2SToby Isaac - coarseSol - solution on the coarse mesh
24781f3379b2SToby Isaac 
24794165533cSJose E. Roman   Output Parameter:
24801f3379b2SToby Isaac . fineSol - the interpolation of coarseSol to the fine mesh
24811f3379b2SToby Isaac 
24821f3379b2SToby Isaac   Level: developer
24831f3379b2SToby Isaac 
2484bb7acecfSBarry Smith   Note:
2485bb7acecfSBarry Smith   This function exists because the interpolation of a solution vector between meshes is not always a linear
24861f3379b2SToby Isaac   map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24871f3379b2SToby Isaac   out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24881f3379b2SToby Isaac   slope-limiting reconstruction.
24891f3379b2SToby Isaac 
249073ff1848SBarry Smith   Developer Note:
2491bb7acecfSBarry Smith   This doesn't just interpolate "solutions" so its API name is questionable.
2492bb7acecfSBarry Smith 
24931cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMInterpolate()`, `DMCreateInterpolation()`
24941f3379b2SToby Isaac @*/
2495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2496d71ae5a4SJacob Faibussowitsch {
24971f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24981f3379b2SToby Isaac 
24991f3379b2SToby Isaac   PetscFunctionBegin;
25001f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
25011f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
25021f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
25031f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
25041f3379b2SToby Isaac 
25059566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
25061f3379b2SToby Isaac   if (interpsol) {
25079566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
25081f3379b2SToby Isaac   } else if (interp) {
25099566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
251098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
25113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25121f3379b2SToby Isaac }
25131f3379b2SToby Isaac 
25141f3379b2SToby Isaac /*@
2515bb7acecfSBarry Smith   DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2516eb3f98d2SBarry Smith 
2517eb3f98d2SBarry Smith   Not Collective
2518eb3f98d2SBarry Smith 
2519eb3f98d2SBarry Smith   Input Parameter:
2520bb7acecfSBarry Smith . dm - the `DM` object
2521eb3f98d2SBarry Smith 
2522eb3f98d2SBarry Smith   Output Parameter:
2523eb3f98d2SBarry Smith . level - number of refinements
2524eb3f98d2SBarry Smith 
2525eb3f98d2SBarry Smith   Level: developer
2526eb3f98d2SBarry Smith 
2527bb7acecfSBarry Smith   Note:
2528bb7acecfSBarry Smith   This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2529bb7acecfSBarry Smith 
25301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2531eb3f98d2SBarry Smith @*/
2532d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2533d71ae5a4SJacob Faibussowitsch {
2534eb3f98d2SBarry Smith   PetscFunctionBegin;
2535eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2536eb3f98d2SBarry Smith   *level = dm->levelup;
25373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2538eb3f98d2SBarry Smith }
2539eb3f98d2SBarry Smith 
2540fef3a512SBarry Smith /*@
2541bb7acecfSBarry Smith   DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2542fef3a512SBarry Smith 
2543fef3a512SBarry Smith   Not Collective
2544fef3a512SBarry Smith 
2545d8d19677SJose E. Roman   Input Parameters:
2546bb7acecfSBarry Smith + dm    - the `DM` object
2547fef3a512SBarry Smith - level - number of refinements
2548fef3a512SBarry Smith 
2549fef3a512SBarry Smith   Level: advanced
2550fef3a512SBarry Smith 
255195452b02SPatrick Sanan   Notes:
2552bb7acecfSBarry Smith   This value is used by `PCMG` to determine how many multigrid levels to use
2553fef3a512SBarry Smith 
2554bb7acecfSBarry Smith   The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2555bb7acecfSBarry Smith 
25561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2557fef3a512SBarry Smith @*/
2558d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2559d71ae5a4SJacob Faibussowitsch {
2560fef3a512SBarry Smith   PetscFunctionBegin;
2561fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2562fef3a512SBarry Smith   dm->levelup = level;
25633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2564fef3a512SBarry Smith }
2565fef3a512SBarry Smith 
2566d410b0cfSMatthew G. Knepley /*@
2567bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2568d410b0cfSMatthew G. Knepley 
256920f4b53cSBarry Smith   Collective
2570d410b0cfSMatthew G. Knepley 
2571f1a722f8SMatthew G. Knepley   Input Parameters:
2572bb7acecfSBarry Smith + dm     - the `DM` object
2573d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2574d410b0cfSMatthew G. Knepley 
2575d410b0cfSMatthew G. Knepley   Output Parameter:
257620f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2577d410b0cfSMatthew G. Knepley 
2578d410b0cfSMatthew G. Knepley   Level: developer
2579d410b0cfSMatthew G. Knepley 
258020f4b53cSBarry Smith   Note:
258120f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
258220f4b53cSBarry Smith 
25831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2584d410b0cfSMatthew G. Knepley @*/
2585d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2586d71ae5a4SJacob Faibussowitsch {
2587d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2588d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2589dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2590d410b0cfSMatthew G. Knepley   if (*dme) {
2591d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25929566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2593d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25949566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2595d410b0cfSMatthew G. Knepley   }
25963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2597d410b0cfSMatthew G. Knepley }
2598d410b0cfSMatthew G. Knepley 
2599d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2600d71ae5a4SJacob Faibussowitsch {
2601ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2602ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26034f572ea9SToby Isaac   PetscAssertPointer(tdm, 2);
2604ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
26053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2606ca3d3a14SMatthew G. Knepley }
2607ca3d3a14SMatthew G. Knepley 
2608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2609d71ae5a4SJacob Faibussowitsch {
2610ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2611ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26124f572ea9SToby Isaac   PetscAssertPointer(tv, 2);
2613ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
26143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2615ca3d3a14SMatthew G. Knepley }
2616ca3d3a14SMatthew G. Knepley 
2617ca3d3a14SMatthew G. Knepley /*@
2618bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2619ca3d3a14SMatthew G. Knepley 
2620ca3d3a14SMatthew G. Knepley   Input Parameter:
262120f4b53cSBarry Smith . dm - The `DM`
2622ca3d3a14SMatthew G. Knepley 
2623ca3d3a14SMatthew G. Knepley   Output Parameter:
262420f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2625ca3d3a14SMatthew G. Knepley 
2626ca3d3a14SMatthew G. Knepley   Level: developer
2627ca3d3a14SMatthew G. Knepley 
26281cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2629ca3d3a14SMatthew G. Knepley @*/
2630d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2631d71ae5a4SJacob Faibussowitsch {
2632ca3d3a14SMatthew G. Knepley   Vec tv;
2633ca3d3a14SMatthew G. Knepley 
2634ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2635ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26364f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
26379566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2638ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
26393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2640ca3d3a14SMatthew G. Knepley }
2641ca3d3a14SMatthew G. Knepley 
2642d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2643d71ae5a4SJacob Faibussowitsch {
2644ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2645ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2646ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2647ca3d3a14SMatthew G. Knepley 
2648ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26509566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26519566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26529566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26539566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26549566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2657ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26589566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2659ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2660ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2661ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2663ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26649566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26659566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2666ca3d3a14SMatthew G. Knepley     }
2667ca3d3a14SMatthew G. Knepley   }
26689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26699566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26709566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2671ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2672ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2674ca3d3a14SMatthew G. Knepley       if (dof) {
2675ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2676ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2677ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2678ca3d3a14SMatthew G. Knepley 
2679ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26809566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26819566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26829566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2683ca3d3a14SMatthew G. Knepley       }
2684ca3d3a14SMatthew G. Knepley     }
2685ca3d3a14SMatthew G. Knepley   }
26869566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2688ca3d3a14SMatthew G. Knepley }
2689ca3d3a14SMatthew G. Knepley 
2690d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2691d71ae5a4SJacob Faibussowitsch {
2692ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2693ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2694ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2695ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2696ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2697ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2698ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26999566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
27003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2701ca3d3a14SMatthew G. Knepley }
2702ca3d3a14SMatthew G. Knepley 
2703bb9467b5SJed Brown /*@C
2704bb7acecfSBarry Smith   DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2705baf369e7SPeter Brune 
270620f4b53cSBarry Smith   Logically Collective
2707baf369e7SPeter Brune 
27084165533cSJose E. Roman   Input Parameters:
2709bb7acecfSBarry Smith + dm        - the `DM`
2710bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2711bb7acecfSBarry Smith . endhook   - function to run after `DMGlobalToLocalEnd()` has completed
271220f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2713baf369e7SPeter Brune 
271420f4b53cSBarry Smith   Calling sequence of `beginhook`:
2715a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2716baf369e7SPeter Brune . g    - global vector
2717baf369e7SPeter Brune . mode - mode
2718baf369e7SPeter Brune . l    - local vector
2719baf369e7SPeter Brune - ctx  - optional user-defined function context
2720baf369e7SPeter Brune 
272120f4b53cSBarry Smith   Calling sequence of `endhook`:
2722a4e35b19SJacob Faibussowitsch + dm   - global `DM`
2723a4e35b19SJacob Faibussowitsch . g    - global vector
2724a4e35b19SJacob Faibussowitsch . mode - mode
2725a4e35b19SJacob Faibussowitsch . l    - local vector
2726baf369e7SPeter Brune - ctx  - optional user-defined function context
2727baf369e7SPeter Brune 
2728baf369e7SPeter Brune   Level: advanced
2729baf369e7SPeter Brune 
2730bb7acecfSBarry Smith   Note:
2731bb7acecfSBarry 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.
2732bb7acecfSBarry Smith 
27331cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2734baf369e7SPeter Brune @*/
2735a4e35b19SJacob 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)
2736d71ae5a4SJacob Faibussowitsch {
2737baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2738baf369e7SPeter Brune 
2739baf369e7SPeter Brune   PetscFunctionBegin;
2740baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2741baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27429566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2743baf369e7SPeter Brune   link->beginhook = beginhook;
2744baf369e7SPeter Brune   link->endhook   = endhook;
2745baf369e7SPeter Brune   link->ctx       = ctx;
27460298fd71SBarry Smith   link->next      = NULL;
2747baf369e7SPeter Brune   *p              = link;
27483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2749baf369e7SPeter Brune }
2750baf369e7SPeter Brune 
2751d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2752d71ae5a4SJacob Faibussowitsch {
27534c274da1SToby Isaac   Mat          cMat;
275479769bd5SJed Brown   Vec          cVec, cBias;
27554c274da1SToby Isaac   PetscSection section, cSec;
27564c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27574c274da1SToby Isaac 
27584c274da1SToby Isaac   PetscFunctionBegin;
2759a4e35b19SJacob Faibussowitsch   (void)g;
2760a4e35b19SJacob Faibussowitsch   (void)ctx;
27614c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27629566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27634c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27645db9a05bSToby Isaac     PetscInt nRows;
27655db9a05bSToby Isaac 
27669566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27673ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27689566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27699566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27709566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27719566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27729566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27734c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27754c274da1SToby Isaac       if (dof) {
27764c274da1SToby Isaac         PetscScalar *vals;
27779566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27789566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27794c274da1SToby Isaac       }
27804c274da1SToby Isaac     }
27819566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27824c274da1SToby Isaac   }
27833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27844c274da1SToby Isaac }
27854c274da1SToby Isaac 
278647c6ae99SBarry Smith /*@
278701729b5cSPatrick Sanan   DMGlobalToLocal - update local vectors from global vector
278801729b5cSPatrick Sanan 
278920f4b53cSBarry Smith   Neighbor-wise Collective
279001729b5cSPatrick Sanan 
279101729b5cSPatrick Sanan   Input Parameters:
2792bb7acecfSBarry Smith + dm   - the `DM` object
279301729b5cSPatrick Sanan . g    - the global vector
2794bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
279501729b5cSPatrick Sanan - l    - the local vector
279601729b5cSPatrick Sanan 
279720f4b53cSBarry Smith   Level: beginner
279820f4b53cSBarry Smith 
279901729b5cSPatrick Sanan   Notes:
2800bb7acecfSBarry Smith   The communication involved in this update can be overlapped with computation by instead using
2801bb7acecfSBarry Smith   `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2802bb7acecfSBarry Smith 
2803bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
280401729b5cSPatrick Sanan 
28051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
280660225df5SJacob Faibussowitsch           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`,
2807bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
280801729b5cSPatrick Sanan @*/
2809d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2810d71ae5a4SJacob Faibussowitsch {
281101729b5cSPatrick Sanan   PetscFunctionBegin;
28129566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
28139566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
28143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
281501729b5cSPatrick Sanan }
281601729b5cSPatrick Sanan 
281701729b5cSPatrick Sanan /*@
281847c6ae99SBarry Smith   DMGlobalToLocalBegin - Begins updating local vectors from global vector
281947c6ae99SBarry Smith 
282020f4b53cSBarry Smith   Neighbor-wise Collective
282147c6ae99SBarry Smith 
282247c6ae99SBarry Smith   Input Parameters:
2823bb7acecfSBarry Smith + dm   - the `DM` object
282447c6ae99SBarry Smith . g    - the global vector
2825bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
282647c6ae99SBarry Smith - l    - the local vector
282747c6ae99SBarry Smith 
282801729b5cSPatrick Sanan   Level: intermediate
282947c6ae99SBarry Smith 
2830bb7acecfSBarry Smith   Notes:
2831bb7acecfSBarry Smith   The operation is completed with `DMGlobalToLocalEnd()`
2832bb7acecfSBarry Smith 
2833bb7acecfSBarry Smith   One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2834bb7acecfSBarry Smith 
2835bb7acecfSBarry Smith   `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2836bb7acecfSBarry Smith 
2837bb7acecfSBarry Smith   `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2838bb7acecfSBarry Smith 
283960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
284047c6ae99SBarry Smith @*/
2841d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2842d71ae5a4SJacob Faibussowitsch {
28437128ae9fSMatthew G Knepley   PetscSF                 sf;
2844baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
284547c6ae99SBarry Smith 
284647c6ae99SBarry Smith   PetscFunctionBegin;
2847171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2848baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28491baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2850baf369e7SPeter Brune   }
28519566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28527128ae9fSMatthew G Knepley   if (sf) {
2853ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2854ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2855d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28567128ae9fSMatthew G Knepley 
28577a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28589566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28599566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28609566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28619566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28629566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28637128ae9fSMatthew G Knepley   } else {
2864fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
28657128ae9fSMatthew G Knepley   }
28663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
286747c6ae99SBarry Smith }
286847c6ae99SBarry Smith 
286947c6ae99SBarry Smith /*@
287047c6ae99SBarry Smith   DMGlobalToLocalEnd - Ends updating local vectors from global vector
287147c6ae99SBarry Smith 
287220f4b53cSBarry Smith   Neighbor-wise Collective
287347c6ae99SBarry Smith 
287447c6ae99SBarry Smith   Input Parameters:
2875bb7acecfSBarry Smith + dm   - the `DM` object
287647c6ae99SBarry Smith . g    - the global vector
2877bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
287847c6ae99SBarry Smith - l    - the local vector
287947c6ae99SBarry Smith 
288001729b5cSPatrick Sanan   Level: intermediate
288147c6ae99SBarry Smith 
2882bb7acecfSBarry Smith   Note:
2883bb7acecfSBarry Smith   See `DMGlobalToLocalBegin()` for details.
2884bb7acecfSBarry Smith 
288560225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`
288647c6ae99SBarry Smith @*/
2887d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2888d71ae5a4SJacob Faibussowitsch {
28897128ae9fSMatthew G Knepley   PetscSF                 sf;
2890ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2891ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2892ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2893baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2894d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
289547c6ae99SBarry Smith 
289647c6ae99SBarry Smith   PetscFunctionBegin;
2897171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28989566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28999566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
29007128ae9fSMatthew G Knepley   if (sf) {
29017a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
29027128ae9fSMatthew G Knepley 
29039566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
29049566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
29059566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
29069566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
29079566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
29089566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
29097128ae9fSMatthew G Knepley   } else {
2910fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, globaltolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
29117128ae9fSMatthew G Knepley   }
29129566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2913baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
29149566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2915baf369e7SPeter Brune   }
29163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
291747c6ae99SBarry Smith }
291847c6ae99SBarry Smith 
2919d4d07f1eSToby Isaac /*@C
2920d4d07f1eSToby Isaac   DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2921d4d07f1eSToby Isaac 
292220f4b53cSBarry Smith   Logically Collective
2923d4d07f1eSToby Isaac 
29244165533cSJose E. Roman   Input Parameters:
2925bb7acecfSBarry Smith + dm        - the `DM`
2926bb7acecfSBarry Smith . beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2927bb7acecfSBarry Smith . endhook   - function to run after `DMLocalToGlobalEnd()` has completed
292820f4b53cSBarry Smith - ctx       - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2929d4d07f1eSToby Isaac 
293020f4b53cSBarry Smith   Calling sequence of `beginhook`:
2931a4e35b19SJacob Faibussowitsch + global - global `DM`
2932d4d07f1eSToby Isaac . l      - local vector
2933d4d07f1eSToby Isaac . mode   - mode
2934d4d07f1eSToby Isaac . g      - global vector
2935d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2936d4d07f1eSToby Isaac 
293720f4b53cSBarry Smith   Calling sequence of `endhook`:
2938bb7acecfSBarry Smith + global - global `DM`
2939d4d07f1eSToby Isaac . l      - local vector
2940d4d07f1eSToby Isaac . mode   - mode
2941d4d07f1eSToby Isaac . g      - global vector
2942d4d07f1eSToby Isaac - ctx    - optional user-defined function context
2943d4d07f1eSToby Isaac 
2944d4d07f1eSToby Isaac   Level: advanced
2945d4d07f1eSToby Isaac 
29461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2947d4d07f1eSToby Isaac @*/
2948a4e35b19SJacob 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)
2949d71ae5a4SJacob Faibussowitsch {
2950d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2951d4d07f1eSToby Isaac 
2952d4d07f1eSToby Isaac   PetscFunctionBegin;
2953d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2954d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29559566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2956d4d07f1eSToby Isaac   link->beginhook = beginhook;
2957d4d07f1eSToby Isaac   link->endhook   = endhook;
2958d4d07f1eSToby Isaac   link->ctx       = ctx;
2959d4d07f1eSToby Isaac   link->next      = NULL;
2960d4d07f1eSToby Isaac   *p              = link;
29613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2962d4d07f1eSToby Isaac }
2963d4d07f1eSToby Isaac 
2964d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2965d71ae5a4SJacob Faibussowitsch {
29664c274da1SToby Isaac   PetscFunctionBegin;
2967a4e35b19SJacob Faibussowitsch   (void)g;
2968a4e35b19SJacob Faibussowitsch   (void)ctx;
29694c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29702b6b7d09SToby Isaac   if (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES) {
29712b6b7d09SToby Isaac     Mat          cMat;
29722b6b7d09SToby Isaac     Vec          cVec;
29735db9a05bSToby Isaac     PetscInt     nRows;
29742b6b7d09SToby Isaac     PetscSection section, cSec;
29752b6b7d09SToby Isaac     PetscInt     pStart, pEnd, p, dof;
29762b6b7d09SToby Isaac 
29772b6b7d09SToby Isaac     PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29782b6b7d09SToby Isaac     if (!cMat) PetscFunctionReturn(PETSC_SUCCESS);
29795db9a05bSToby Isaac 
29809566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29813ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29829566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29839566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29854c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29869566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29874c274da1SToby Isaac       if (dof) {
29884c274da1SToby Isaac         PetscInt     d;
29894c274da1SToby Isaac         PetscScalar *vals;
29909566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29919566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29924c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29934c274da1SToby Isaac          * we just extracted */
2994ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29954c274da1SToby Isaac       }
29964c274da1SToby Isaac     }
29979566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29989566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29994c274da1SToby Isaac   }
30003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30014c274da1SToby Isaac }
300201729b5cSPatrick Sanan /*@
300301729b5cSPatrick Sanan   DMLocalToGlobal - updates global vectors from local vectors
300401729b5cSPatrick Sanan 
300520f4b53cSBarry Smith   Neighbor-wise Collective
300601729b5cSPatrick Sanan 
300701729b5cSPatrick Sanan   Input Parameters:
3008bb7acecfSBarry Smith + dm   - the `DM` object
300901729b5cSPatrick Sanan . l    - the local vector
3010bb7acecfSBarry 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.
301101729b5cSPatrick Sanan - g    - the global vector
301201729b5cSPatrick Sanan 
301320f4b53cSBarry Smith   Level: beginner
301420f4b53cSBarry Smith 
301501729b5cSPatrick Sanan   Notes:
301601729b5cSPatrick Sanan   The communication involved in this update can be overlapped with computation by using
3017bb7acecfSBarry Smith   `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
301801729b5cSPatrick Sanan 
3019bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3020bb7acecfSBarry Smith 
3021bb7acecfSBarry 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.
3022bb7acecfSBarry Smith 
3023bb7acecfSBarry Smith   Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
302401729b5cSPatrick Sanan 
30251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
302601729b5cSPatrick Sanan @*/
3027d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3028d71ae5a4SJacob Faibussowitsch {
302901729b5cSPatrick Sanan   PetscFunctionBegin;
30309566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30319566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
30323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
303301729b5cSPatrick Sanan }
30344c274da1SToby Isaac 
303547c6ae99SBarry Smith /*@
303601729b5cSPatrick Sanan   DMLocalToGlobalBegin - begins updating global vectors from local vectors
30379a42bb27SBarry Smith 
303820f4b53cSBarry Smith   Neighbor-wise Collective
30399a42bb27SBarry Smith 
30409a42bb27SBarry Smith   Input Parameters:
3041bb7acecfSBarry Smith + dm   - the `DM` object
3042f6813fd5SJed Brown . l    - the local vector
3043aa624791SPierre 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.
30441eb28f2eSBarry Smith - g    - the global vector
30459a42bb27SBarry Smith 
304620f4b53cSBarry Smith   Level: intermediate
304720f4b53cSBarry Smith 
304895452b02SPatrick Sanan   Notes:
3049bb7acecfSBarry Smith   In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3050bb7acecfSBarry Smith 
3051bb7acecfSBarry 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.
3052bb7acecfSBarry Smith 
3053bb7acecfSBarry Smith   Use `DMLocalToGlobalEnd()` to complete the communication process.
3054bb7acecfSBarry Smith 
3055bb7acecfSBarry Smith   `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3056bb7acecfSBarry Smith 
3057bb7acecfSBarry Smith   `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30589a42bb27SBarry Smith 
30591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30609a42bb27SBarry Smith @*/
3061d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3062d71ae5a4SJacob Faibussowitsch {
30637128ae9fSMatthew G Knepley   PetscSF                 sf;
306484330215SMatthew G. Knepley   PetscSection            s, gs;
3065d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3066ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3067ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3068ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3069fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3070d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30719a42bb27SBarry Smith 
30729a42bb27SBarry Smith   PetscFunctionBegin;
3073171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3074d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30751baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3076d4d07f1eSToby Isaac   }
30779566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30789566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30799566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30807128ae9fSMatthew G Knepley   switch (mode) {
30817128ae9fSMatthew G Knepley   case INSERT_VALUES:
30827128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3083d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3084d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3085d71ae5a4SJacob Faibussowitsch     break;
30867128ae9fSMatthew G Knepley   case ADD_VALUES:
30877128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3088d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3089d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3090d71ae5a4SJacob Faibussowitsch     break;
3091d71ae5a4SJacob Faibussowitsch   default:
3092d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30937128ae9fSMatthew G Knepley   }
3094ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30959566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3096ca3d3a14SMatthew G. Knepley     if (transform) {
30979566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30989566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30999566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
31009566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3101fa88e482SJed Brown     } else if (isInsert) {
31029566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3103fa88e482SJed Brown     } else {
31049566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3105fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3106ca3d3a14SMatthew G. Knepley     }
3107fa88e482SJed Brown     if (s && isInsert) {
31089566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3109fa88e482SJed Brown     } else {
31109566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3111fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3112fa88e482SJed Brown     }
3113ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
31149566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
311584330215SMatthew G. Knepley     } else if (s && isInsert) {
311684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
311784330215SMatthew G. Knepley 
31189566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
31199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31209566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
312184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3122b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
312384330215SMatthew G. Knepley 
31249566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31259566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31269566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31279566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31289566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31299566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3130b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
313103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31327a8be351SBarry 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);
3133b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3134b3b16f48SMatthew G. Knepley         if (!gcdof) {
313584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3136b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3137b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
313884330215SMatthew G. Knepley           const PetscInt *cdofs;
313984330215SMatthew G. Knepley           PetscInt        cind = 0;
314084330215SMatthew G. Knepley 
31419566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3142b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31439371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31449371c9d4SSatish Balay               ++cind;
31459371c9d4SSatish Balay               continue;
31469371c9d4SSatish Balay             }
3147b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
314884330215SMatthew G. Knepley           }
31497a8be351SBarry 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);
315084330215SMatthew G. Knepley       }
3151ca3d3a14SMatthew G. Knepley     }
3152fa88e482SJed Brown     if (g_inplace) {
31539566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3154fa88e482SJed Brown     } else {
31559566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3156fa88e482SJed Brown     }
3157ca3d3a14SMatthew G. Knepley     if (transform) {
31589566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31599566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3160fa88e482SJed Brown     } else if (l_inplace) {
31619566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3162ca3d3a14SMatthew G. Knepley     } else {
31639566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3164ca3d3a14SMatthew G. Knepley     }
31657128ae9fSMatthew G Knepley   } else {
3166fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalbegin, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
31677128ae9fSMatthew G Knepley   }
31683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31699a42bb27SBarry Smith }
31709a42bb27SBarry Smith 
31719a42bb27SBarry Smith /*@
31729a42bb27SBarry Smith   DMLocalToGlobalEnd - updates global vectors from local vectors
317347c6ae99SBarry Smith 
317420f4b53cSBarry Smith   Neighbor-wise Collective
317547c6ae99SBarry Smith 
317647c6ae99SBarry Smith   Input Parameters:
3177bb7acecfSBarry Smith + dm   - the `DM` object
3178f6813fd5SJed Brown . l    - the local vector
3179bb7acecfSBarry Smith . mode - `INSERT_VALUES` or `ADD_VALUES`
3180f6813fd5SJed Brown - g    - the global vector
318147c6ae99SBarry Smith 
318201729b5cSPatrick Sanan   Level: intermediate
318347c6ae99SBarry Smith 
3184bb7acecfSBarry Smith   Note:
3185bb7acecfSBarry Smith   See `DMLocalToGlobalBegin()` for full details
3186bb7acecfSBarry Smith 
318760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`
318847c6ae99SBarry Smith @*/
3189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3190d71ae5a4SJacob Faibussowitsch {
31917128ae9fSMatthew G Knepley   PetscSF                 sf;
319284330215SMatthew G. Knepley   PetscSection            s;
3193d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3194ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
319547c6ae99SBarry Smith 
319647c6ae99SBarry Smith   PetscFunctionBegin;
3197171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31989566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31999566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
32007128ae9fSMatthew G Knepley   switch (mode) {
32017128ae9fSMatthew G Knepley   case INSERT_VALUES:
3202d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3203d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3204d71ae5a4SJacob Faibussowitsch     break;
32057128ae9fSMatthew G Knepley   case ADD_VALUES:
3206d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3207d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3208d71ae5a4SJacob Faibussowitsch     break;
3209d71ae5a4SJacob Faibussowitsch   default:
3210d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
32117128ae9fSMatthew G Knepley   }
321284330215SMatthew G. Knepley   if (sf && !isInsert) {
3213ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3214ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3215ca3d3a14SMatthew G. Knepley     Vec                tmpl;
321684330215SMatthew G. Knepley 
32179566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3218ca3d3a14SMatthew G. Knepley     if (transform) {
32199566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32209566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3221ca3d3a14SMatthew G. Knepley     } else {
32229566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3223ca3d3a14SMatthew G. Knepley     }
32249566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32259566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3226ca3d3a14SMatthew G. Knepley     if (transform) {
32279566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32289566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3229ca3d3a14SMatthew G. Knepley     } else {
32309566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3231ca3d3a14SMatthew G. Knepley     }
32329566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
323384330215SMatthew G. Knepley   } else if (s && isInsert) {
32347128ae9fSMatthew G Knepley   } else {
3235fa1e479aSStefano Zampini     PetscUseTypeMethod(dm, localtoglobalend, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g);
32367128ae9fSMatthew G Knepley   }
3237d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32389566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3239d4d07f1eSToby Isaac   }
32403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
324147c6ae99SBarry Smith }
324247c6ae99SBarry Smith 
3243f089877aSRichard Tran Mills /*@
3244a4e35b19SJacob Faibussowitsch   DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include
3245a4e35b19SJacob Faibussowitsch   ghost points that contain irrelevant values) to another local vector where the ghost points
3246a4e35b19SJacob Faibussowitsch   in the second are set correctly from values on other MPI ranks.
3247f089877aSRichard Tran Mills 
324820f4b53cSBarry Smith   Neighbor-wise Collective
3249f089877aSRichard Tran Mills 
3250f089877aSRichard Tran Mills   Input Parameters:
3251bb7acecfSBarry Smith + dm   - the `DM` object
3252bc0a1609SRichard Tran Mills . g    - the original local vector
3253bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3254f089877aSRichard Tran Mills 
3255bc0a1609SRichard Tran Mills   Output Parameter:
3256bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3257f089877aSRichard Tran Mills 
3258f089877aSRichard Tran Mills   Level: intermediate
3259f089877aSRichard Tran Mills 
326073ff1848SBarry Smith   Note:
3261a4e35b19SJacob Faibussowitsch   Must be followed by `DMLocalToLocalEnd()`.
3262a4e35b19SJacob Faibussowitsch 
326360225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3264f089877aSRichard Tran Mills @*/
3265d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3266d71ae5a4SJacob Faibussowitsch {
3267f089877aSRichard Tran Mills   PetscFunctionBegin;
3268f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32699f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32709f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32719f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3273f089877aSRichard Tran Mills }
3274f089877aSRichard Tran Mills 
3275f089877aSRichard Tran Mills /*@
3276bb7acecfSBarry Smith   DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3277bb7acecfSBarry Smith   points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3278f089877aSRichard Tran Mills 
327920f4b53cSBarry Smith   Neighbor-wise Collective
3280f089877aSRichard Tran Mills 
3281f089877aSRichard Tran Mills   Input Parameters:
328260225df5SJacob Faibussowitsch + dm   - the `DM` object
3283bc0a1609SRichard Tran Mills . g    - the original local vector
3284bb7acecfSBarry Smith - mode - one of `INSERT_VALUES` or `ADD_VALUES`
3285f089877aSRichard Tran Mills 
3286bc0a1609SRichard Tran Mills   Output Parameter:
3287bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values
3288f089877aSRichard Tran Mills 
3289f089877aSRichard Tran Mills   Level: intermediate
3290f089877aSRichard Tran Mills 
329160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3292f089877aSRichard Tran Mills @*/
3293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3294d71ae5a4SJacob Faibussowitsch {
3295f089877aSRichard Tran Mills   PetscFunctionBegin;
3296f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32979f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32989f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32999f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
33003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3301f089877aSRichard Tran Mills }
3302f089877aSRichard Tran Mills 
330347c6ae99SBarry Smith /*@
3304bb7acecfSBarry Smith   DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
330547c6ae99SBarry Smith 
330620f4b53cSBarry Smith   Collective
330747c6ae99SBarry Smith 
3308d8d19677SJose E. Roman   Input Parameters:
3309bb7acecfSBarry Smith + dm   - the `DM` object
331020f4b53cSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
331147c6ae99SBarry Smith 
331247c6ae99SBarry Smith   Output Parameter:
3313bb7acecfSBarry Smith . dmc - the coarsened `DM`
331447c6ae99SBarry Smith 
331547c6ae99SBarry Smith   Level: developer
331647c6ae99SBarry Smith 
331773ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateDomainDecomposition()`,
331873ff1848SBarry Smith           `DMCoarsenHookAdd()`, `DMCoarsenHookRemove()`
331947c6ae99SBarry Smith @*/
3320d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3321d71ae5a4SJacob Faibussowitsch {
3322b17ce1afSJed Brown   DMCoarsenHookLink link;
332347c6ae99SBarry Smith 
332447c6ae99SBarry Smith   PetscFunctionBegin;
3325171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3327dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3328b9d85ea2SLisandro Dalcin   if (*dmc) {
3329a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33309566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
333143842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33329566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3333644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33340598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3335656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33369566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3337b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33389566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3339b17ce1afSJed Brown     }
3340b9d85ea2SLisandro Dalcin   }
33419566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33427a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
33433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3344b17ce1afSJed Brown }
3345b17ce1afSJed Brown 
3346bb9467b5SJed Brown /*@C
3347b17ce1afSJed Brown   DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3348b17ce1afSJed Brown 
334920f4b53cSBarry Smith   Logically Collective; No Fortran Support
3350b17ce1afSJed Brown 
33514165533cSJose E. Roman   Input Parameters:
3352bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3353b17ce1afSJed Brown . coarsenhook  - function to run when setting up a coarser level
3354bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
335520f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3356b17ce1afSJed Brown 
335720f4b53cSBarry Smith   Calling sequence of `coarsenhook`:
3358bb7acecfSBarry Smith + fine   - fine level `DM`
3359bb7acecfSBarry Smith . coarse - coarse level `DM` to restrict problem to
3360b17ce1afSJed Brown - ctx    - optional user-defined function context
3361b17ce1afSJed Brown 
336220f4b53cSBarry Smith   Calling sequence of `restricthook`:
3363bb7acecfSBarry Smith + fine      - fine level `DM`
3364bb7acecfSBarry Smith . mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3365c833c3b5SJed Brown . rscale    - scaling vector for restriction
3366c833c3b5SJed Brown . inject    - matrix restricting by injection
3367b17ce1afSJed Brown . coarse    - coarse level DM to update
3368b17ce1afSJed Brown - ctx       - optional user-defined function context
3369b17ce1afSJed Brown 
3370b17ce1afSJed Brown   Level: advanced
3371b17ce1afSJed Brown 
3372b17ce1afSJed Brown   Notes:
3373bb7acecfSBarry 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`.
3374b17ce1afSJed Brown 
3375b17ce1afSJed Brown   If this function is called multiple times, the hooks will be run in the order they are added.
3376b17ce1afSJed Brown 
3377b17ce1afSJed Brown   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3378bb7acecfSBarry Smith   extract the finest level information from its context (instead of from the `SNES`).
3379b17ce1afSJed Brown 
3380bb7acecfSBarry Smith   The hooks are automatically called by `DMRestrict()`
3381bb7acecfSBarry Smith 
33821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3383b17ce1afSJed Brown @*/
3384a4e35b19SJacob 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)
3385d71ae5a4SJacob Faibussowitsch {
3386b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3387b17ce1afSJed Brown 
3388b17ce1afSJed Brown   PetscFunctionBegin;
3389b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33901e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33913ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33921e3d8eccSJed Brown   }
33939566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3394b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3395b17ce1afSJed Brown   link->restricthook = restricthook;
3396b17ce1afSJed Brown   link->ctx          = ctx;
33970298fd71SBarry Smith   link->next         = NULL;
3398b17ce1afSJed Brown   *p                 = link;
33993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3400b17ce1afSJed Brown }
3401b17ce1afSJed Brown 
3402dc822a44SJed Brown /*@C
3403bb7acecfSBarry Smith   DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3404dc822a44SJed Brown 
340520f4b53cSBarry Smith   Logically Collective; No Fortran Support
3406dc822a44SJed Brown 
34074165533cSJose E. Roman   Input Parameters:
3408bb7acecfSBarry Smith + fine         - `DM` on which to run a hook when restricting to a coarser level
3409dc822a44SJed Brown . coarsenhook  - function to run when setting up a coarser level
3410bb7acecfSBarry Smith . restricthook - function to run to update data on coarser levels
341120f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3412dc822a44SJed Brown 
3413dc822a44SJed Brown   Level: advanced
3414dc822a44SJed Brown 
341573ff1848SBarry Smith   Notes:
341673ff1848SBarry Smith   This function does nothing if the `coarsenhook` is not in the list.
341773ff1848SBarry Smith 
341873ff1848SBarry Smith   See `DMCoarsenHookAdd()` for the calling sequence of `coarsenhook` and `restricthook`
3419dc822a44SJed Brown 
34201cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3421dc822a44SJed Brown @*/
3422d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3423d71ae5a4SJacob Faibussowitsch {
3424dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3425dc822a44SJed Brown 
3426dc822a44SJed Brown   PetscFunctionBegin;
3427dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3428dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3429dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3430dc822a44SJed Brown       link = *p;
3431dc822a44SJed Brown       *p   = link->next;
34329566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3433dc822a44SJed Brown       break;
3434dc822a44SJed Brown     }
3435dc822a44SJed Brown   }
34363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3437dc822a44SJed Brown }
3438dc822a44SJed Brown 
3439b17ce1afSJed Brown /*@
3440bb7acecfSBarry Smith   DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3441b17ce1afSJed Brown 
3442b17ce1afSJed Brown   Collective if any hooks are
3443b17ce1afSJed Brown 
34444165533cSJose E. Roman   Input Parameters:
3445bb7acecfSBarry Smith + fine    - finer `DM` from which the data is obtained
3446bb7acecfSBarry Smith . restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3447e91eccc2SStefano Zampini . rscale  - scaling vector for restriction
3448bb7acecfSBarry Smith . inject  - injection matrix, also use `MatRestrict()`
344920f4b53cSBarry Smith - coarse  - coarser `DM` to update
3450b17ce1afSJed Brown 
3451b17ce1afSJed Brown   Level: developer
3452b17ce1afSJed Brown 
345373ff1848SBarry Smith   Developer Note:
3454bb7acecfSBarry Smith   Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3455bb7acecfSBarry Smith 
34561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3457b17ce1afSJed Brown @*/
3458d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3459d71ae5a4SJacob Faibussowitsch {
3460b17ce1afSJed Brown   DMCoarsenHookLink link;
3461b17ce1afSJed Brown 
3462b17ce1afSJed Brown   PetscFunctionBegin;
3463b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34641baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3465b17ce1afSJed Brown   }
34663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
346747c6ae99SBarry Smith }
346847c6ae99SBarry Smith 
3469bb9467b5SJed Brown /*@C
347073ff1848SBarry Smith   DMSubDomainHookAdd - adds a callback to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
34715dbd56e3SPeter Brune 
347220f4b53cSBarry Smith   Logically Collective; No Fortran Support
34735dbd56e3SPeter Brune 
34744165533cSJose E. Roman   Input Parameters:
3475bb7acecfSBarry Smith + global       - global `DM`
3476bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
34775dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve)
347820f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34795dbd56e3SPeter Brune 
348020f4b53cSBarry Smith   Calling sequence of `ddhook`:
3481bb7acecfSBarry Smith + global - global `DM`
348273ff1848SBarry Smith . block  - subdomain `DM`
3483ec4806b8SPeter Brune - ctx    - optional user-defined function context
3484ec4806b8SPeter Brune 
348520f4b53cSBarry Smith   Calling sequence of `restricthook`:
3486bb7acecfSBarry Smith + global - global `DM`
348773ff1848SBarry Smith . out    - scatter to the outer (with ghost and overlap points) sub vector
348873ff1848SBarry Smith . in     - scatter to sub vector values only owned locally
348973ff1848SBarry Smith . block  - subdomain `DM`
34905dbd56e3SPeter Brune - ctx    - optional user-defined function context
34915dbd56e3SPeter Brune 
34925dbd56e3SPeter Brune   Level: advanced
34935dbd56e3SPeter Brune 
34945dbd56e3SPeter Brune   Notes:
349573ff1848SBarry Smith   This function can be used if auxiliary data needs to be set up on subdomain `DM`s.
34965dbd56e3SPeter Brune 
34975dbd56e3SPeter Brune   If this function is called multiple times, the hooks will be run in the order they are added.
34985dbd56e3SPeter Brune 
34995dbd56e3SPeter Brune   In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3500bb7acecfSBarry Smith   extract the global information from its context (instead of from the `SNES`).
35015dbd56e3SPeter Brune 
350273ff1848SBarry Smith   Developer Note:
350373ff1848SBarry Smith   It is unclear what "block solve" means within the definition of `restricthook`
350473ff1848SBarry Smith 
350573ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`, `DMCreateDomainDecomposition()`
35065dbd56e3SPeter Brune @*/
3507a4e35b19SJacob 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)
3508d71ae5a4SJacob Faibussowitsch {
3509be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
35105dbd56e3SPeter Brune 
35115dbd56e3SPeter Brune   PetscFunctionBegin;
35125dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3513b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
35143ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3515b3a6b972SJed Brown   }
35169566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35175dbd56e3SPeter Brune   link->restricthook = restricthook;
3518be081cd6SPeter Brune   link->ddhook       = ddhook;
35195dbd56e3SPeter Brune   link->ctx          = ctx;
35200298fd71SBarry Smith   link->next         = NULL;
35215dbd56e3SPeter Brune   *p                 = link;
35223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35235dbd56e3SPeter Brune }
35245dbd56e3SPeter Brune 
3525b3a6b972SJed Brown /*@C
352673ff1848SBarry Smith   DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to subdomain `DM`s with `DMCreateDomainDecomposition()`
3527b3a6b972SJed Brown 
352820f4b53cSBarry Smith   Logically Collective; No Fortran Support
3529b3a6b972SJed Brown 
35304165533cSJose E. Roman   Input Parameters:
3531bb7acecfSBarry Smith + global       - global `DM`
3532bb7acecfSBarry Smith . ddhook       - function to run to pass data to the decomposition `DM` upon its creation
3533b3a6b972SJed Brown . restricthook - function to run to update data on block solve (at the beginning of the block solve)
353420f4b53cSBarry Smith - ctx          - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3535b3a6b972SJed Brown 
3536b3a6b972SJed Brown   Level: advanced
3537b3a6b972SJed Brown 
353873ff1848SBarry Smith   Note:
353973ff1848SBarry Smith   See `DMSubDomainHookAdd()` for the calling sequences of `ddhook` and `restricthook`
354073ff1848SBarry Smith 
354173ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`,
354273ff1848SBarry Smith           `DMCreateDomainDecomposition()`
3543b3a6b972SJed Brown @*/
3544d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3545d71ae5a4SJacob Faibussowitsch {
3546b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3547b3a6b972SJed Brown 
3548b3a6b972SJed Brown   PetscFunctionBegin;
3549b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3550b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3551b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3552b3a6b972SJed Brown       link = *p;
3553b3a6b972SJed Brown       *p   = link->next;
35549566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3555b3a6b972SJed Brown       break;
3556b3a6b972SJed Brown     }
3557b3a6b972SJed Brown   }
35583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3559b3a6b972SJed Brown }
3560b3a6b972SJed Brown 
35615dbd56e3SPeter Brune /*@
356273ff1848SBarry Smith   DMSubDomainRestrict - restricts user-defined problem data to a subdomain `DM` by running hooks registered by `DMSubDomainHookAdd()`
35635dbd56e3SPeter Brune 
35645dbd56e3SPeter Brune   Collective if any hooks are
35655dbd56e3SPeter Brune 
35664165533cSJose E. Roman   Input Parameters:
3567a4e35b19SJacob Faibussowitsch + global   - The global `DM` to use as a base
3568a4e35b19SJacob Faibussowitsch . oscatter - The scatter from domain global vector filling subdomain global vector with overlap
3569a4e35b19SJacob Faibussowitsch . gscatter - The scatter from domain global vector filling subdomain local vector with ghosts
3570a4e35b19SJacob Faibussowitsch - subdm    - The subdomain `DM` to update
35715dbd56e3SPeter Brune 
35725dbd56e3SPeter Brune   Level: developer
35735dbd56e3SPeter Brune 
357473ff1848SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsenHookAdd()`, `MatRestrict()`, `DMCreateDomainDecomposition()`
35755dbd56e3SPeter Brune @*/
3576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3577d71ae5a4SJacob Faibussowitsch {
3578be081cd6SPeter Brune   DMSubDomainHookLink link;
35795dbd56e3SPeter Brune 
35805dbd56e3SPeter Brune   PetscFunctionBegin;
3581be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35821baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35835dbd56e3SPeter Brune   }
35843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35855dbd56e3SPeter Brune }
35865dbd56e3SPeter Brune 
35875fe1f584SPeter Brune /*@
3588bb7acecfSBarry Smith   DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35895fe1f584SPeter Brune 
35905fe1f584SPeter Brune   Not Collective
35915fe1f584SPeter Brune 
35925fe1f584SPeter Brune   Input Parameter:
3593bb7acecfSBarry Smith . dm - the `DM` object
35945fe1f584SPeter Brune 
35955fe1f584SPeter Brune   Output Parameter:
35966a7d9d85SPeter Brune . level - number of coarsenings
35975fe1f584SPeter Brune 
35985fe1f584SPeter Brune   Level: developer
35995fe1f584SPeter Brune 
36001cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36015fe1f584SPeter Brune @*/
3602d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3603d71ae5a4SJacob Faibussowitsch {
36045fe1f584SPeter Brune   PetscFunctionBegin;
36055fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36064f572ea9SToby Isaac   PetscAssertPointer(level, 2);
36075fe1f584SPeter Brune   *level = dm->leveldown;
36083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36095fe1f584SPeter Brune }
36105fe1f584SPeter Brune 
36119a64c4a8SMatthew G. Knepley /*@
3612bb7acecfSBarry Smith   DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36139a64c4a8SMatthew G. Knepley 
361420f4b53cSBarry Smith   Collective
36159a64c4a8SMatthew G. Knepley 
36169a64c4a8SMatthew G. Knepley   Input Parameters:
3617bb7acecfSBarry Smith + dm    - the `DM` object
36189a64c4a8SMatthew G. Knepley - level - number of coarsenings
36199a64c4a8SMatthew G. Knepley 
36209a64c4a8SMatthew G. Knepley   Level: developer
36219a64c4a8SMatthew G. Knepley 
3622bb7acecfSBarry Smith   Note:
3623bb7acecfSBarry Smith   This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3624bb7acecfSBarry Smith 
362542747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36269a64c4a8SMatthew G. Knepley @*/
3627d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3628d71ae5a4SJacob Faibussowitsch {
36299a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36309a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36319a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36339a64c4a8SMatthew G. Knepley }
36349a64c4a8SMatthew G. Knepley 
3635cc4c1da9SBarry Smith /*@
3636bb7acecfSBarry Smith   DMRefineHierarchy - Refines a `DM` object, all levels at once
363747c6ae99SBarry Smith 
363820f4b53cSBarry Smith   Collective
363947c6ae99SBarry Smith 
3640d8d19677SJose E. Roman   Input Parameters:
3641bb7acecfSBarry Smith + dm      - the `DM` object
364247c6ae99SBarry Smith - nlevels - the number of levels of refinement
364347c6ae99SBarry Smith 
364447c6ae99SBarry Smith   Output Parameter:
3645bb7acecfSBarry Smith . dmf - the refined `DM` hierarchy
364647c6ae99SBarry Smith 
364747c6ae99SBarry Smith   Level: developer
364847c6ae99SBarry Smith 
36491cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
365047c6ae99SBarry Smith @*/
3651d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3652d71ae5a4SJacob Faibussowitsch {
365347c6ae99SBarry Smith   PetscFunctionBegin;
3654171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36557a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36563ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36574f572ea9SToby Isaac   PetscAssertPointer(dmf, 3);
3658213acdd3SPierre Jolivet   if (dm->ops->refine && !dm->ops->refinehierarchy) {
365947c6ae99SBarry Smith     PetscInt i;
366047c6ae99SBarry Smith 
36619566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
366248a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3663213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
36643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
366547c6ae99SBarry Smith }
366647c6ae99SBarry Smith 
3667cc4c1da9SBarry Smith /*@
3668bb7acecfSBarry Smith   DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
366947c6ae99SBarry Smith 
367020f4b53cSBarry Smith   Collective
367147c6ae99SBarry Smith 
3672d8d19677SJose E. Roman   Input Parameters:
3673bb7acecfSBarry Smith + dm      - the `DM` object
367447c6ae99SBarry Smith - nlevels - the number of levels of coarsening
367547c6ae99SBarry Smith 
367647c6ae99SBarry Smith   Output Parameter:
3677bb7acecfSBarry Smith . dmc - the coarsened `DM` hierarchy
367847c6ae99SBarry Smith 
367947c6ae99SBarry Smith   Level: developer
368047c6ae99SBarry Smith 
36811cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
368247c6ae99SBarry Smith @*/
3683d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3684d71ae5a4SJacob Faibussowitsch {
368547c6ae99SBarry Smith   PetscFunctionBegin;
3686171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36877a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36883ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
36894f572ea9SToby Isaac   PetscAssertPointer(dmc, 3);
3690213acdd3SPierre Jolivet   if (dm->ops->coarsen && !dm->ops->coarsenhierarchy) {
369147c6ae99SBarry Smith     PetscInt i;
369247c6ae99SBarry Smith 
36939566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
369448a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3695213acdd3SPierre Jolivet   } else PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
36963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
369747c6ae99SBarry Smith }
369847c6ae99SBarry Smith 
36991a266240SBarry Smith /*@C
3700bb7acecfSBarry Smith   DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
37011a266240SBarry Smith 
3702bb7acecfSBarry Smith   Logically Collective if the function is collective
37031a266240SBarry Smith 
37041a266240SBarry Smith   Input Parameters:
3705bb7acecfSBarry Smith + dm      - the `DM` object
3706*49abdd8aSBarry Smith - destroy - the destroy function, see `PetscCtxDestroyFn` for the calling sequence
37071a266240SBarry Smith 
37081a266240SBarry Smith   Level: intermediate
37091a266240SBarry Smith 
3710*49abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`,
3711*49abdd8aSBarry Smith           `DMGetApplicationContext()`, `PetscCtxDestroyFn`
3712f07f9ceaSJed Brown @*/
3713*49abdd8aSBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscCtxDestroyFn *destroy)
3714d71ae5a4SJacob Faibussowitsch {
37151a266240SBarry Smith   PetscFunctionBegin;
3716171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37171a266240SBarry Smith   dm->ctxdestroy = destroy;
37183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37191a266240SBarry Smith }
37201a266240SBarry Smith 
3721b07ff414SBarry Smith /*@
3722bb7acecfSBarry Smith   DMSetApplicationContext - Set a user context into a `DM` object
372347c6ae99SBarry Smith 
372447c6ae99SBarry Smith   Not Collective
372547c6ae99SBarry Smith 
372647c6ae99SBarry Smith   Input Parameters:
3727bb7acecfSBarry Smith + dm  - the `DM` object
372847c6ae99SBarry Smith - ctx - the user context
372947c6ae99SBarry Smith 
373047c6ae99SBarry Smith   Level: intermediate
373147c6ae99SBarry Smith 
3732e33b3f0fSStefano Zampini   Notes:
373335cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3734e33b3f0fSStefano Zampini   In a multilevel solver, the user context is shared by all the `DM` in the hierarchy; it is thus not advisable
3735e33b3f0fSStefano Zampini   to store objects that represent discretized quantities inside the context.
3736bb7acecfSBarry Smith 
373760225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
373847c6ae99SBarry Smith @*/
3739d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3740d71ae5a4SJacob Faibussowitsch {
374147c6ae99SBarry Smith   PetscFunctionBegin;
3742171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374347c6ae99SBarry Smith   dm->ctx = ctx;
37443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
374547c6ae99SBarry Smith }
374647c6ae99SBarry Smith 
374747c6ae99SBarry Smith /*@
3748bb7acecfSBarry Smith   DMGetApplicationContext - Gets a user context from a `DM` object
374947c6ae99SBarry Smith 
375047c6ae99SBarry Smith   Not Collective
375147c6ae99SBarry Smith 
375247c6ae99SBarry Smith   Input Parameter:
3753bb7acecfSBarry Smith . dm - the `DM` object
375447c6ae99SBarry Smith 
375547c6ae99SBarry Smith   Output Parameter:
375647c6ae99SBarry Smith . ctx - the user context
375747c6ae99SBarry Smith 
375847c6ae99SBarry Smith   Level: intermediate
375947c6ae99SBarry Smith 
3760bb7acecfSBarry Smith   Note:
376135cb6cd3SPierre Jolivet   A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3762bb7acecfSBarry Smith 
376342747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`
376447c6ae99SBarry Smith @*/
3765d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3766d71ae5a4SJacob Faibussowitsch {
376747c6ae99SBarry Smith   PetscFunctionBegin;
3768171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37691b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
377147c6ae99SBarry Smith }
377247c6ae99SBarry Smith 
377308da532bSDmitry Karpeev /*@C
3774bb7acecfSBarry Smith   DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
377508da532bSDmitry Karpeev 
377620f4b53cSBarry Smith   Logically Collective
377708da532bSDmitry Karpeev 
3778d8d19677SJose E. Roman   Input Parameters:
377908da532bSDmitry Karpeev + dm - the DM object
378020f4b53cSBarry Smith - f  - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
378108da532bSDmitry Karpeev 
378208da532bSDmitry Karpeev   Level: intermediate
378308da532bSDmitry Karpeev 
37841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3785db781477SPatrick Sanan          `DMSetJacobian()`
378608da532bSDmitry Karpeev @*/
3787d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3788d71ae5a4SJacob Faibussowitsch {
378908da532bSDmitry Karpeev   PetscFunctionBegin;
37905a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379308da532bSDmitry Karpeev }
379408da532bSDmitry Karpeev 
379508da532bSDmitry Karpeev /*@
3796bb7acecfSBarry Smith   DMHasVariableBounds - does the `DM` object have a variable bounds function?
379708da532bSDmitry Karpeev 
379808da532bSDmitry Karpeev   Not Collective
379908da532bSDmitry Karpeev 
380008da532bSDmitry Karpeev   Input Parameter:
3801bb7acecfSBarry Smith . dm - the `DM` object to destroy
380208da532bSDmitry Karpeev 
380308da532bSDmitry Karpeev   Output Parameter:
3804bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the variable bounds function exists
380508da532bSDmitry Karpeev 
380608da532bSDmitry Karpeev   Level: developer
380708da532bSDmitry Karpeev 
38081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
380908da532bSDmitry Karpeev @*/
3810d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3811d71ae5a4SJacob Faibussowitsch {
381208da532bSDmitry Karpeev   PetscFunctionBegin;
38135a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38144f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
381508da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
38163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
381708da532bSDmitry Karpeev }
381808da532bSDmitry Karpeev 
3819cc4c1da9SBarry Smith /*@
3820bb7acecfSBarry Smith   DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
382108da532bSDmitry Karpeev 
382220f4b53cSBarry Smith   Logically Collective
382308da532bSDmitry Karpeev 
3824f899ff85SJose E. Roman   Input Parameter:
3825bb7acecfSBarry Smith . dm - the `DM` object
382608da532bSDmitry Karpeev 
382760225df5SJacob Faibussowitsch   Output Parameters:
382808da532bSDmitry Karpeev + xl - lower bound
382908da532bSDmitry Karpeev - xu - upper bound
383008da532bSDmitry Karpeev 
3831907376e6SBarry Smith   Level: advanced
3832907376e6SBarry Smith 
383320f4b53cSBarry Smith   Note:
383495452b02SPatrick Sanan   This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
383508da532bSDmitry Karpeev 
38361cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
383708da532bSDmitry Karpeev @*/
3838d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3839d71ae5a4SJacob Faibussowitsch {
384008da532bSDmitry Karpeev   PetscFunctionBegin;
38415a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
384208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38435a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3844dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
38453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
384608da532bSDmitry Karpeev }
384708da532bSDmitry Karpeev 
3848b0ae01b7SPeter Brune /*@
3849bb7acecfSBarry Smith   DMHasColoring - does the `DM` object have a method of providing a coloring?
3850b0ae01b7SPeter Brune 
3851b0ae01b7SPeter Brune   Not Collective
3852b0ae01b7SPeter Brune 
3853b0ae01b7SPeter Brune   Input Parameter:
3854b0ae01b7SPeter Brune . dm - the DM object
3855b0ae01b7SPeter Brune 
3856b0ae01b7SPeter Brune   Output Parameter:
3857bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3858b0ae01b7SPeter Brune 
3859b0ae01b7SPeter Brune   Level: developer
3860b0ae01b7SPeter Brune 
38611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateColoring()`
3862b0ae01b7SPeter Brune @*/
3863d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3864d71ae5a4SJacob Faibussowitsch {
3865b0ae01b7SPeter Brune   PetscFunctionBegin;
38665a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38674f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3868b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3870b0ae01b7SPeter Brune }
3871b0ae01b7SPeter Brune 
38723ad4599aSBarry Smith /*@
3873bb7acecfSBarry Smith   DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38743ad4599aSBarry Smith 
38753ad4599aSBarry Smith   Not Collective
38763ad4599aSBarry Smith 
38773ad4599aSBarry Smith   Input Parameter:
3878bb7acecfSBarry Smith . dm - the `DM` object
38793ad4599aSBarry Smith 
38803ad4599aSBarry Smith   Output Parameter:
3881bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38823ad4599aSBarry Smith 
38833ad4599aSBarry Smith   Level: developer
38843ad4599aSBarry Smith 
38851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38863ad4599aSBarry Smith @*/
3887d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3888d71ae5a4SJacob Faibussowitsch {
38893ad4599aSBarry Smith   PetscFunctionBegin;
38905a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38914f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
38923ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38943ad4599aSBarry Smith }
38953ad4599aSBarry Smith 
3896a7058e45SLawrence Mitchell /*@
3897bb7acecfSBarry Smith   DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3898a7058e45SLawrence Mitchell 
3899a7058e45SLawrence Mitchell   Not Collective
3900a7058e45SLawrence Mitchell 
3901a7058e45SLawrence Mitchell   Input Parameter:
3902bb7acecfSBarry Smith . dm - the `DM` object
3903a7058e45SLawrence Mitchell 
3904a7058e45SLawrence Mitchell   Output Parameter:
3905bb7acecfSBarry Smith . flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3906a7058e45SLawrence Mitchell 
3907a7058e45SLawrence Mitchell   Level: developer
3908a7058e45SLawrence Mitchell 
39091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3910a7058e45SLawrence Mitchell @*/
3911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3912d71ae5a4SJacob Faibussowitsch {
3913a7058e45SLawrence Mitchell   PetscFunctionBegin;
39145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39154f572ea9SToby Isaac   PetscAssertPointer(flg, 2);
3916dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3917ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
39183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3919a7058e45SLawrence Mitchell }
3920a7058e45SLawrence Mitchell 
39210298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3922264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3923264ace61SBarry Smith 
3924cc4c1da9SBarry Smith /*@
3925bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3926264ace61SBarry Smith 
392720f4b53cSBarry Smith   Collective
3928264ace61SBarry Smith 
3929264ace61SBarry Smith   Input Parameters:
3930bb7acecfSBarry Smith + dm     - The `DM` object
3931bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3932264ace61SBarry Smith 
3933264ace61SBarry Smith   Options Database Key:
3934bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3935264ace61SBarry Smith 
3936264ace61SBarry Smith   Level: intermediate
3937264ace61SBarry Smith 
3938bb7acecfSBarry Smith   Note:
39390462cc06SPierre Jolivet   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPlexCreateBoxMesh()`
3940bb7acecfSBarry Smith 
39411cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3942264ace61SBarry Smith @*/
3943d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3944d71ae5a4SJacob Faibussowitsch {
3945264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3946264ace61SBarry Smith   PetscBool match;
3947264ace61SBarry Smith 
3948264ace61SBarry Smith   PetscFunctionBegin;
3949264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
39513ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3952264ace61SBarry Smith 
39539566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39549566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39557a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3956264ace61SBarry Smith 
3957dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39589566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39599566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39609566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3962264ace61SBarry Smith }
3963264ace61SBarry Smith 
3964cc4c1da9SBarry Smith /*@
3965bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3966264ace61SBarry Smith 
3967264ace61SBarry Smith   Not Collective
3968264ace61SBarry Smith 
3969264ace61SBarry Smith   Input Parameter:
3970bb7acecfSBarry Smith . dm - The `DM`
3971264ace61SBarry Smith 
3972264ace61SBarry Smith   Output Parameter:
3973bb7acecfSBarry Smith . type - The `DMType` name
3974264ace61SBarry Smith 
3975264ace61SBarry Smith   Level: intermediate
3976264ace61SBarry Smith 
39771cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3978264ace61SBarry Smith @*/
3979d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3980d71ae5a4SJacob Faibussowitsch {
3981264ace61SBarry Smith   PetscFunctionBegin;
3982264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39834f572ea9SToby Isaac   PetscAssertPointer(type, 2);
39849566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3985264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3987264ace61SBarry Smith }
3988264ace61SBarry Smith 
39895d83a8b1SBarry Smith /*@
3990bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
399167a56275SMatthew G Knepley 
399220f4b53cSBarry Smith   Collective
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley   Input Parameters:
3995bb7acecfSBarry Smith + dm      - the `DM`
3996bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
399767a56275SMatthew G Knepley 
399867a56275SMatthew G Knepley   Output Parameter:
3999bb7acecfSBarry Smith . M - pointer to new `DM`
400067a56275SMatthew G Knepley 
400120f4b53cSBarry Smith   Level: intermediate
400220f4b53cSBarry Smith 
400373ff1848SBarry Smith   Note:
4004bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
4005bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
4006bb7acecfSBarry Smith   of the input `DM`.
400767a56275SMatthew G Knepley 
40081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
400967a56275SMatthew G Knepley @*/
4010d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
4011d71ae5a4SJacob Faibussowitsch {
401267a56275SMatthew G Knepley   DM        B;
401367a56275SMatthew G Knepley   char      convname[256];
4014c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
401567a56275SMatthew G Knepley 
401667a56275SMatthew G Knepley   PetscFunctionBegin;
401767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
401867a56275SMatthew G Knepley   PetscValidType(dm, 1);
40194f572ea9SToby Isaac   PetscAssertPointer(M, 3);
40209566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40219566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4022c067b6caSMatthew G. Knepley   if (sametype) {
4023c067b6caSMatthew G. Knepley     *M = dm;
40249566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
40253ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
4026c067b6caSMatthew G. Knepley   } else {
40270298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
402867a56275SMatthew G Knepley 
402967a56275SMatthew G Knepley     /*
403067a56275SMatthew G Knepley        Order of precedence:
403167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
403267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
403367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
403467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
403567a56275SMatthew G Knepley        5) Use a really basic converter.
403667a56275SMatthew G Knepley     */
403767a56275SMatthew G Knepley 
403867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40399566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40409566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40419566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40429566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40439566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40449566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
404567a56275SMatthew G Knepley     if (conv) goto foundconv;
404667a56275SMatthew G Knepley 
404767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40489566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40499566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40509566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40519566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40529566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40539566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40549566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40559566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
405667a56275SMatthew G Knepley     if (conv) {
40579566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
405867a56275SMatthew G Knepley       goto foundconv;
405967a56275SMatthew G Knepley     }
406067a56275SMatthew G Knepley 
406167a56275SMatthew G Knepley #if 0
406267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
406367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40649566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
406567a56275SMatthew G Knepley     if (conv) goto foundconv;
406667a56275SMatthew G Knepley 
406767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
406867a56275SMatthew G Knepley     if (dm->ops->convert) {
406967a56275SMatthew G Knepley       conv = dm->ops->convert;
407067a56275SMatthew G Knepley     }
407167a56275SMatthew G Knepley     if (conv) goto foundconv;
407267a56275SMatthew G Knepley #endif
407367a56275SMatthew G Knepley 
407467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
407598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
407667a56275SMatthew G Knepley 
407767a56275SMatthew G Knepley   foundconv:
40789566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40799566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
408012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
408190b157c4SStefano Zampini     {
40824fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40836858538eSMatthew G. Knepley 
40844fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40854fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4086c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40879566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40889566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40899566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40909566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
409112fa691eSMatthew G. Knepley     }
40929566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
409367a56275SMatthew G Knepley   }
40949566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
409667a56275SMatthew G Knepley }
4097264ace61SBarry Smith 
4098264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4099264ace61SBarry Smith 
4100264ace61SBarry Smith /*@C
4101bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
41021c84c290SBarry Smith 
4103cc4c1da9SBarry Smith   Not Collective, No Fortran Support
41041c84c290SBarry Smith 
41051c84c290SBarry Smith   Input Parameters:
410620f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
410720f4b53cSBarry Smith - function - The creation routine itself
410820f4b53cSBarry Smith 
410920f4b53cSBarry Smith   Level: advanced
41101c84c290SBarry Smith 
411173ff1848SBarry Smith   Note:
411220f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
41131c84c290SBarry Smith 
411460225df5SJacob Faibussowitsch   Example Usage:
41151c84c290SBarry Smith .vb
4116bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41171c84c290SBarry Smith .ve
41181c84c290SBarry Smith 
411920f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
41201c84c290SBarry Smith .vb
41211c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41221c84c290SBarry Smith     DMSetType(DM,"my_da");
41231c84c290SBarry Smith .ve
41241c84c290SBarry Smith   or at runtime via the option
41251c84c290SBarry Smith .vb
41261c84c290SBarry Smith     -da_type my_da
41271c84c290SBarry Smith .ve
4128264ace61SBarry Smith 
41291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4130264ace61SBarry Smith @*/
4131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4132d71ae5a4SJacob Faibussowitsch {
4133264ace61SBarry Smith   PetscFunctionBegin;
41349566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41359566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
41363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4137264ace61SBarry Smith }
4138264ace61SBarry Smith 
4139ffeef943SBarry Smith /*@
4140bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4141b859378eSBarry Smith 
414220f4b53cSBarry Smith   Collective
4143b859378eSBarry Smith 
4144b859378eSBarry Smith   Input Parameters:
4145bb7acecfSBarry Smith + newdm  - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4146bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4147bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4148bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4149b859378eSBarry Smith 
4150b859378eSBarry Smith   Level: intermediate
4151b859378eSBarry Smith 
4152b859378eSBarry Smith   Notes:
415355849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4154b859378eSBarry Smith 
4155bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4156bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4157bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4158cd7e8a5eSksagiyam 
41591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4160b859378eSBarry Smith @*/
4161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4162d71ae5a4SJacob Faibussowitsch {
41639331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4164b859378eSBarry Smith 
4165b859378eSBarry Smith   PetscFunctionBegin;
4166b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4167b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41689566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41699566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41709566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41719566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41729331c7a4SMatthew G. Knepley   if (isbinary) {
41739331c7a4SMatthew G. Knepley     PetscInt classid;
41749331c7a4SMatthew G. Knepley     char     type[256];
4175b859378eSBarry Smith 
41769566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41777a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41789566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41799566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4180dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41819331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4182dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41839331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4186b859378eSBarry Smith }
4187b859378eSBarry Smith 
41884ee01570SBarry Smith /* FEM Support */
41897da65231SMatthew G Knepley 
41902ecf6ec3SMatthew G. Knepley PetscErrorCode DMPrintCellIndices(PetscInt c, const char name[], PetscInt len, const PetscInt x[])
41912ecf6ec3SMatthew G. Knepley {
41922ecf6ec3SMatthew G. Knepley   PetscInt f;
41932ecf6ec3SMatthew G. Knepley 
41942ecf6ec3SMatthew G. Knepley   PetscFunctionBegin;
41952ecf6ec3SMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41962ecf6ec3SMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %" PetscInt_FMT " |\n", x[f]));
41972ecf6ec3SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41982ecf6ec3SMatthew G. Knepley }
41992ecf6ec3SMatthew G. Knepley 
4200d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4201d71ae5a4SJacob Faibussowitsch {
42021d47ebbbSSatish Balay   PetscInt f;
42031b30c384SMatthew G Knepley 
42047da65231SMatthew G Knepley   PetscFunctionBegin;
420563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
420648a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
42073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42087da65231SMatthew G Knepley }
42097da65231SMatthew G Knepley 
42105962854dSMatthew G. Knepley PetscErrorCode DMPrintCellVectorReal(PetscInt c, const char name[], PetscInt len, const PetscReal x[])
42115962854dSMatthew G. Knepley {
42125962854dSMatthew G. Knepley   PetscInt f;
42135962854dSMatthew G. Knepley 
42145962854dSMatthew G. Knepley   PetscFunctionBegin;
42155962854dSMatthew G. Knepley   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42165962854dSMatthew G. Knepley   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)x[f]));
42175962854dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42185962854dSMatthew G. Knepley }
42195962854dSMatthew G. Knepley 
4220d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4221d71ae5a4SJacob Faibussowitsch {
42221b30c384SMatthew G Knepley   PetscInt f, g;
42237da65231SMatthew G Knepley 
42247da65231SMatthew G Knepley   PetscFunctionBegin;
422563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42261d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42279566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
422848a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42299566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42307da65231SMatthew G Knepley   }
42313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42327da65231SMatthew G Knepley }
4233e7c4fc90SDmitry Karpeev 
4234d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4235d71ae5a4SJacob Faibussowitsch {
42360c5b8624SToby Isaac   PetscInt           localSize, bs;
42370c5b8624SToby Isaac   PetscMPIInt        size;
42380c5b8624SToby Isaac   Vec                x, xglob;
42390c5b8624SToby Isaac   const PetscScalar *xarray;
4240e759306cSMatthew G. Knepley 
4241e759306cSMatthew G. Knepley   PetscFunctionBegin;
42429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42439566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42449566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
424593ec0da9SPierre Jolivet   PetscCall(VecFilter(x, tol));
42469566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42470c5b8624SToby Isaac   if (size > 1) {
42489566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42499566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42509566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42519566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42520c5b8624SToby Isaac   } else {
42530c5b8624SToby Isaac     xglob = x;
42540c5b8624SToby Isaac   }
42559566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42560c5b8624SToby Isaac   if (size > 1) {
42579566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42589566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42590c5b8624SToby Isaac   }
42609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
42613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4262e759306cSMatthew G. Knepley }
4263e759306cSMatthew G. Knepley 
426488ed4aceSMatthew G Knepley /*@
4265bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4266061576a5SJed Brown 
4267061576a5SJed Brown   Input Parameter:
4268bb7acecfSBarry Smith . dm - The `DM`
4269061576a5SJed Brown 
4270061576a5SJed Brown   Output Parameter:
4271bb7acecfSBarry Smith . section - The `PetscSection`
4272061576a5SJed Brown 
427320f4b53cSBarry Smith   Options Database Key:
4274bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4275061576a5SJed Brown 
4276061576a5SJed Brown   Level: advanced
4277061576a5SJed Brown 
4278061576a5SJed Brown   Notes:
4279bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4280061576a5SJed Brown 
4281bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4282061576a5SJed Brown 
42831cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4284061576a5SJed Brown @*/
4285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4286d71ae5a4SJacob Faibussowitsch {
4287061576a5SJed Brown   PetscFunctionBegin;
42889566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4290061576a5SJed Brown }
4291061576a5SJed Brown 
4292061576a5SJed Brown /*@
4293bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
429488ed4aceSMatthew G Knepley 
429588ed4aceSMatthew G Knepley   Input Parameter:
4296bb7acecfSBarry Smith . dm - The `DM`
429788ed4aceSMatthew G Knepley 
429888ed4aceSMatthew G Knepley   Output Parameter:
4299bb7acecfSBarry Smith . section - The `PetscSection`
430088ed4aceSMatthew G Knepley 
430120f4b53cSBarry Smith   Options Database Key:
4302bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4303e5893cccSMatthew G. Knepley 
430488ed4aceSMatthew G Knepley   Level: intermediate
430588ed4aceSMatthew G Knepley 
4306bb7acecfSBarry Smith   Note:
4307bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
430888ed4aceSMatthew G Knepley 
43091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetGlobalSection()`
431088ed4aceSMatthew G Knepley @*/
4311d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4312d71ae5a4SJacob Faibussowitsch {
431388ed4aceSMatthew G Knepley   PetscFunctionBegin;
431488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43154f572ea9SToby Isaac   PetscAssertPointer(section, 2);
43161bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4317e5e52638SMatthew G. Knepley     PetscInt d;
4318e5e52638SMatthew G. Knepley 
431945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
432045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
432145480ffeSMatthew G. Knepley       PetscViewer       viewer;
432245480ffeSMatthew G. Knepley       PetscViewerFormat format;
432345480ffeSMatthew G. Knepley       PetscBool         flg;
432445480ffeSMatthew G. Knepley 
4325648c30bcSBarry Smith       PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43269566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
432745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43289566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43299566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
433045480ffeSMatthew G. Knepley       }
433145480ffeSMatthew G. Knepley       if (flg) {
43329566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43339566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
4334648c30bcSBarry Smith         PetscCall(PetscViewerDestroy(&viewer));
433545480ffeSMatthew G. Knepley       }
433645480ffeSMatthew G. Knepley     }
4337dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43389566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43392f0f8703SMatthew G. Knepley   }
43401bb6d2a8SBarry Smith   *section = dm->localSection;
43413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
434288ed4aceSMatthew G Knepley }
434388ed4aceSMatthew G Knepley 
434488ed4aceSMatthew G Knepley /*@
4345bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4346061576a5SJed Brown 
4347061576a5SJed Brown   Input Parameters:
4348bb7acecfSBarry Smith + dm      - The `DM`
4349bb7acecfSBarry Smith - section - The `PetscSection`
4350061576a5SJed Brown 
4351061576a5SJed Brown   Level: advanced
4352061576a5SJed Brown 
4353061576a5SJed Brown   Notes:
4354bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4355061576a5SJed Brown 
4356bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4357061576a5SJed Brown 
43581cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4359061576a5SJed Brown @*/
4360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4361d71ae5a4SJacob Faibussowitsch {
4362061576a5SJed Brown   PetscFunctionBegin;
43639566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
43643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4365061576a5SJed Brown }
4366061576a5SJed Brown 
4367061576a5SJed Brown /*@
4368bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
436988ed4aceSMatthew G Knepley 
437088ed4aceSMatthew G Knepley   Input Parameters:
4371bb7acecfSBarry Smith + dm      - The `DM`
4372bb7acecfSBarry Smith - section - The `PetscSection`
437388ed4aceSMatthew G Knepley 
437488ed4aceSMatthew G Knepley   Level: intermediate
437588ed4aceSMatthew G Knepley 
4376bb7acecfSBarry Smith   Note:
4377bb7acecfSBarry Smith   Any existing Section will be destroyed
437888ed4aceSMatthew G Knepley 
43791cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
438088ed4aceSMatthew G Knepley @*/
4381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4382d71ae5a4SJacob Faibussowitsch {
4383c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4384af122d2aSMatthew G Knepley   PetscInt f;
438588ed4aceSMatthew G Knepley 
438688ed4aceSMatthew G Knepley   PetscFunctionBegin;
438788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4388b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43899566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43909566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43911bb6d2a8SBarry Smith   dm->localSection = section;
43929566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4393af122d2aSMatthew G Knepley   if (numFields) {
43949566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4395af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43960f21e855SMatthew G. Knepley       PetscObject disc;
4397af122d2aSMatthew G Knepley       const char *name;
4398af122d2aSMatthew G Knepley 
43999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
44009566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
44019566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4402af122d2aSMatthew G Knepley     }
4403af122d2aSMatthew G Knepley   }
440450350c8eSStefano Zampini   /* The global section and the SectionSF will be rebuilt
440550350c8eSStefano Zampini      in the next call to DMGetGlobalSection() and DMGetSectionSF(). */
44069566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
440750350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
440852947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
440950350c8eSStefano Zampini 
441050350c8eSStefano Zampini   /* Clear scratch vectors */
441150350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
441250350c8eSStefano Zampini   PetscCall(DMClearLocalVectors(dm));
441350350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
441450350c8eSStefano Zampini   PetscCall(DMClearNamedLocalVectors(dm));
44153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
441688ed4aceSMatthew G Knepley }
441788ed4aceSMatthew G Knepley 
4418adc21957SMatthew G. Knepley /*@C
4419d8b4a066SPierre Jolivet   DMCreateSectionPermutation - Create a permutation of the `PetscSection` chart and optionally a block structure.
4420adc21957SMatthew G. Knepley 
4421adc21957SMatthew G. Knepley   Input Parameter:
4422adc21957SMatthew G. Knepley . dm - The `DM`
4423adc21957SMatthew G. Knepley 
44249cde84edSJose E. Roman   Output Parameters:
4425adc21957SMatthew G. Knepley + perm        - A permutation of the mesh points in the chart
4426b6971eaeSBarry Smith - blockStarts - A high bit is set for the point that begins every block, or `NULL` for default blocking
4427adc21957SMatthew G. Knepley 
4428adc21957SMatthew G. Knepley   Level: developer
4429adc21957SMatthew G. Knepley 
4430adc21957SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `PetscSection`, `DMGetLocalSection()`, `DMGetGlobalSection()`
4431adc21957SMatthew G. Knepley @*/
4432adc21957SMatthew G. Knepley PetscErrorCode DMCreateSectionPermutation(DM dm, IS *perm, PetscBT *blockStarts)
4433adc21957SMatthew G. Knepley {
4434adc21957SMatthew G. Knepley   PetscFunctionBegin;
4435adc21957SMatthew G. Knepley   *perm        = NULL;
4436adc21957SMatthew G. Knepley   *blockStarts = NULL;
4437adc21957SMatthew G. Knepley   PetscTryTypeMethod(dm, createsectionpermutation, perm, blockStarts);
4438adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4439adc21957SMatthew G. Knepley }
4440adc21957SMatthew G. Knepley 
44419435951eSToby Isaac /*@
4442bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
44439435951eSToby Isaac 
444420f4b53cSBarry Smith   not Collective
4445e228b242SToby Isaac 
44469435951eSToby Isaac   Input Parameter:
4447bb7acecfSBarry Smith . dm - The `DM`
44489435951eSToby Isaac 
4449d8d19677SJose E. Roman   Output Parameters:
445020f4b53cSBarry 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.
445120f4b53cSBarry 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.
445279769bd5SJed Brown - bias    - Vector containing bias to be added to constrained dofs
44539435951eSToby Isaac 
44549435951eSToby Isaac   Level: advanced
44559435951eSToby Isaac 
4456bb7acecfSBarry Smith   Note:
4457bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44589435951eSToby Isaac 
44591cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDefaultConstraints()`
44609435951eSToby Isaac @*/
4461d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4462d71ae5a4SJacob Faibussowitsch {
44639435951eSToby Isaac   PetscFunctionBegin;
44649435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4465dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44663b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44673b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
446879769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44709435951eSToby Isaac }
44719435951eSToby Isaac 
44729435951eSToby Isaac /*@
4473bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44749435951eSToby Isaac 
447520f4b53cSBarry Smith   Collective
4476e228b242SToby Isaac 
44779435951eSToby Isaac   Input Parameters:
4478bb7acecfSBarry Smith + dm      - The `DM`
4479bb7acecfSBarry 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).
448020f4b53cSBarry 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).
448120f4b53cSBarry 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).
44829435951eSToby Isaac 
44839435951eSToby Isaac   Level: advanced
44849435951eSToby Isaac 
448520f4b53cSBarry Smith   Notes:
448620f4b53cSBarry 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()`.
448720f4b53cSBarry Smith 
448820f4b53cSBarry 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.
448920f4b53cSBarry Smith 
4490bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44919435951eSToby Isaac 
44921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDefaultConstraints()`
44939435951eSToby Isaac @*/
4494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4495d71ae5a4SJacob Faibussowitsch {
4496e228b242SToby Isaac   PetscMPIInt result;
44979435951eSToby Isaac 
44989435951eSToby Isaac   PetscFunctionBegin;
44999435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4500e228b242SToby Isaac   if (section) {
4501e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45029566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
45037a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4504e228b242SToby Isaac   }
4505e228b242SToby Isaac   if (mat) {
4506e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
45079566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
45087a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4509e228b242SToby Isaac   }
451079769bd5SJed Brown   if (bias) {
451179769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
45129566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
451379769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
451479769bd5SJed Brown   }
45159566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45169566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
45173b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
45189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
45199566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
45203b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
45219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
45229566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
452379769bd5SJed Brown   dm->defaultConstraint.bias = bias;
45243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45259435951eSToby Isaac }
45269435951eSToby Isaac 
4527497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4528507e4973SMatthew G. Knepley /*
4529bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4530507e4973SMatthew G. Knepley 
4531507e4973SMatthew G. Knepley   Input Parameters:
4532bb7acecfSBarry Smith + dm - The `DM`
4533bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4534bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4535507e4973SMatthew G. Knepley 
4536507e4973SMatthew G. Knepley   Level: intermediate
4537507e4973SMatthew G. Knepley 
45381cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`
4539507e4973SMatthew G. Knepley */
4540d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4541d71ae5a4SJacob Faibussowitsch {
4542507e4973SMatthew G. Knepley   MPI_Comm        comm;
4543507e4973SMatthew G. Knepley   PetscLayout     layout;
4544507e4973SMatthew G. Knepley   const PetscInt *ranges;
4545507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4546507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4547507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4548507e4973SMatthew G. Knepley 
4549507e4973SMatthew G. Knepley   PetscFunctionBegin;
45509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4551507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45529566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
45539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45559566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45569566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45579566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45589566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45599566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45609566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4561507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4562f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4563507e4973SMatthew G. Knepley 
45649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45699566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4570507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45719371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45729371c9d4SSatish 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));
45739371c9d4SSatish Balay       valid = PETSC_FALSE;
45749371c9d4SSatish Balay     }
45759371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45769371c9d4SSatish 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));
45779371c9d4SSatish Balay       valid = PETSC_FALSE;
45789371c9d4SSatish Balay     }
4579507e4973SMatthew G. Knepley     if (gdof < 0) {
4580507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4581507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4582507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4583507e4973SMatthew G. Knepley 
45849566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4585507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45869371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45879371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45889371c9d4SSatish Balay           valid = PETSC_FALSE;
45899371c9d4SSatish Balay           break;
45909371c9d4SSatish Balay         }
4591507e4973SMatthew G. Knepley       }
4592507e4973SMatthew G. Knepley     }
4593507e4973SMatthew G. Knepley   }
45949566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45959566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
4596462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4597507e4973SMatthew G. Knepley   if (!gvalid) {
45989566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4599507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4600507e4973SMatthew G. Knepley   }
46013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4602507e4973SMatthew G. Knepley }
4603f741bcd2SMatthew G. Knepley #endif
4604507e4973SMatthew G. Knepley 
46055f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
46066725e60dSJed Brown {
46076725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
46084d86920dSPierre Jolivet 
46096725e60dSJed Brown   PetscFunctionBegin;
46106725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46114f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
46125f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
46136725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
46146725e60dSJed Brown   else *sf = dm->sf;
46153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46166725e60dSJed Brown }
46176725e60dSJed Brown 
461888ed4aceSMatthew G Knepley /*@
4619bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
462088ed4aceSMatthew G Knepley 
462120f4b53cSBarry Smith   Collective
46228b1ab98fSJed Brown 
462388ed4aceSMatthew G Knepley   Input Parameter:
4624bb7acecfSBarry Smith . dm - The `DM`
462588ed4aceSMatthew G Knepley 
462688ed4aceSMatthew G Knepley   Output Parameter:
4627bb7acecfSBarry Smith . section - The `PetscSection`
462888ed4aceSMatthew G Knepley 
462988ed4aceSMatthew G Knepley   Level: intermediate
463088ed4aceSMatthew G Knepley 
4631bb7acecfSBarry Smith   Note:
4632bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
463388ed4aceSMatthew G Knepley 
46341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLocalSection()`, `DMGetLocalSection()`
463588ed4aceSMatthew G Knepley @*/
4636d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4637d71ae5a4SJacob Faibussowitsch {
463888ed4aceSMatthew G Knepley   PetscFunctionBegin;
463988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46404f572ea9SToby Isaac   PetscAssertPointer(section, 2);
46411bb6d2a8SBarry Smith   if (!dm->globalSection) {
4642fd59a867SMatthew G. Knepley     PetscSection s;
46436725e60dSJed Brown     PetscSF      sf;
4644fd59a867SMatthew G. Knepley 
46459566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
46467a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
46477a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
46485f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
4649eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
46509566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
46519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
46529566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
465388ed4aceSMatthew G Knepley   }
46541bb6d2a8SBarry Smith   *section = dm->globalSection;
46553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
465688ed4aceSMatthew G Knepley }
465788ed4aceSMatthew G Knepley 
4658b21d0597SMatthew G Knepley /*@
4659bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4660b21d0597SMatthew G Knepley 
4661b21d0597SMatthew G Knepley   Input Parameters:
4662bb7acecfSBarry Smith + dm      - The `DM`
46632fe279fdSBarry Smith - section - The PetscSection, or `NULL`
4664b21d0597SMatthew G Knepley 
4665b21d0597SMatthew G Knepley   Level: intermediate
4666b21d0597SMatthew G Knepley 
4667bb7acecfSBarry Smith   Note:
4668bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4669b21d0597SMatthew G Knepley 
46701cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetGlobalSection()`, `DMSetLocalSection()`
4671b21d0597SMatthew G Knepley @*/
4672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4673d71ae5a4SJacob Faibussowitsch {
4674b21d0597SMatthew G Knepley   PetscFunctionBegin;
4675b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46765080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46779566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46789566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46791bb6d2a8SBarry Smith   dm->globalSection = section;
4680497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46819566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4682507e4973SMatthew G. Knepley #endif
468350350c8eSStefano Zampini   /* Clear global scratch vectors and sectionSF */
468450350c8eSStefano Zampini   PetscCall(PetscSFDestroy(&dm->sectionSF));
468552947b74SStefano Zampini   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
468650350c8eSStefano Zampini   PetscCall(DMClearGlobalVectors(dm));
468750350c8eSStefano Zampini   PetscCall(DMClearNamedGlobalVectors(dm));
46883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4689b21d0597SMatthew G Knepley }
4690b21d0597SMatthew G Knepley 
469188ed4aceSMatthew G Knepley /*@
4692bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4693bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
469488ed4aceSMatthew G Knepley 
469588ed4aceSMatthew G Knepley   Input Parameter:
4696bb7acecfSBarry Smith . dm - The `DM`
469788ed4aceSMatthew G Knepley 
469888ed4aceSMatthew G Knepley   Output Parameter:
4699bb7acecfSBarry Smith . sf - The `PetscSF`
470088ed4aceSMatthew G Knepley 
470188ed4aceSMatthew G Knepley   Level: intermediate
470288ed4aceSMatthew G Knepley 
4703bb7acecfSBarry Smith   Note:
4704bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
470588ed4aceSMatthew G Knepley 
47061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetSectionSF()`, `DMCreateSectionSF()`
470788ed4aceSMatthew G Knepley @*/
4708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4709d71ae5a4SJacob Faibussowitsch {
471088ed4aceSMatthew G Knepley   PetscInt nroots;
471188ed4aceSMatthew G Knepley 
471288ed4aceSMatthew G Knepley   PetscFunctionBegin;
471388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47144f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
471548a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
47169566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
471788ed4aceSMatthew G Knepley   if (nroots < 0) {
471888ed4aceSMatthew G Knepley     PetscSection section, gSection;
471988ed4aceSMatthew G Knepley 
47209566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
472131ea6d37SMatthew G Knepley     if (section) {
47229566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
47239566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
472431ea6d37SMatthew G Knepley     } else {
47250298fd71SBarry Smith       *sf = NULL;
47263ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
472731ea6d37SMatthew G Knepley     }
472888ed4aceSMatthew G Knepley   }
47291bb6d2a8SBarry Smith   *sf = dm->sectionSF;
47303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
473188ed4aceSMatthew G Knepley }
473288ed4aceSMatthew G Knepley 
473388ed4aceSMatthew G Knepley /*@
4734bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
473588ed4aceSMatthew G Knepley 
473688ed4aceSMatthew G Knepley   Input Parameters:
4737bb7acecfSBarry Smith + dm - The `DM`
4738bb7acecfSBarry Smith - sf - The `PetscSF`
473988ed4aceSMatthew G Knepley 
474088ed4aceSMatthew G Knepley   Level: intermediate
474188ed4aceSMatthew G Knepley 
4742bb7acecfSBarry Smith   Note:
4743bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
474488ed4aceSMatthew G Knepley 
47451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMCreateSectionSF()`
474688ed4aceSMatthew G Knepley @*/
4747d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4748d71ae5a4SJacob Faibussowitsch {
474988ed4aceSMatthew G Knepley   PetscFunctionBegin;
475088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4751b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47529566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47539566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
47541bb6d2a8SBarry Smith   dm->sectionSF = sf;
47553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475688ed4aceSMatthew G Knepley }
475788ed4aceSMatthew G Knepley 
4758cc4c1da9SBarry Smith /*@
4759bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
476088ed4aceSMatthew G Knepley   describing the data layout.
476188ed4aceSMatthew G Knepley 
476288ed4aceSMatthew G Knepley   Input Parameters:
4763bb7acecfSBarry Smith + dm            - The `DM`
4764bb7acecfSBarry Smith . localSection  - `PetscSection` describing the local data layout
4765bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
476688ed4aceSMatthew G Knepley 
47671bb6d2a8SBarry Smith   Level: developer
47681bb6d2a8SBarry Smith 
4769bb7acecfSBarry Smith   Note:
4770bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4771bb7acecfSBarry Smith 
477273ff1848SBarry Smith   Developer Note:
4773bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4774bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4775b6971eaeSBarry Smith   input and should just obtain them from the `DM`? Plus PETSc creation functions return the thing
4776b6971eaeSBarry Smith   they create, this returns nothing
47771bb6d2a8SBarry Smith 
47781cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
477988ed4aceSMatthew G Knepley @*/
4780d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4781d71ae5a4SJacob Faibussowitsch {
478288ed4aceSMatthew G Knepley   PetscFunctionBegin;
478388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47849566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
47853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
478688ed4aceSMatthew G Knepley }
4787af122d2aSMatthew G Knepley 
4788b21d0597SMatthew G Knepley /*@
4789bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4790bb7acecfSBarry Smith 
4791bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4792b21d0597SMatthew G Knepley 
4793b21d0597SMatthew G Knepley   Input Parameter:
4794bb7acecfSBarry Smith . dm - The `DM`
4795b21d0597SMatthew G Knepley 
4796b21d0597SMatthew G Knepley   Output Parameter:
4797bb7acecfSBarry Smith . sf - The `PetscSF`
4798b21d0597SMatthew G Knepley 
4799b21d0597SMatthew G Knepley   Level: intermediate
4800b21d0597SMatthew G Knepley 
4801bb7acecfSBarry Smith   Note:
4802bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4803b21d0597SMatthew G Knepley 
48041cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4805b21d0597SMatthew G Knepley @*/
4806d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4807d71ae5a4SJacob Faibussowitsch {
4808b21d0597SMatthew G Knepley   PetscFunctionBegin;
4809b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48104f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
4811b21d0597SMatthew G Knepley   *sf = dm->sf;
48123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4813b21d0597SMatthew G Knepley }
4814b21d0597SMatthew G Knepley 
4815057b4bcdSMatthew G Knepley /*@
4816bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4817bb7acecfSBarry Smith 
481820f4b53cSBarry Smith   Collective
4819057b4bcdSMatthew G Knepley 
4820057b4bcdSMatthew G Knepley   Input Parameters:
4821bb7acecfSBarry Smith + dm - The `DM`
4822bb7acecfSBarry Smith - sf - The `PetscSF`
4823057b4bcdSMatthew G Knepley 
4824057b4bcdSMatthew G Knepley   Level: intermediate
4825057b4bcdSMatthew G Knepley 
48261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4827057b4bcdSMatthew G Knepley @*/
4828d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4829d71ae5a4SJacob Faibussowitsch {
4830057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4831057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4832b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48339566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48349566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4835057b4bcdSMatthew G Knepley   dm->sf = sf;
48363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4837057b4bcdSMatthew G Knepley }
4838057b4bcdSMatthew G Knepley 
48394f37162bSMatthew G. Knepley /*@
4840bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
48414f37162bSMatthew G. Knepley 
48424f37162bSMatthew G. Knepley   Input Parameter:
4843bb7acecfSBarry Smith . dm - The `DM`
48444f37162bSMatthew G. Knepley 
48454f37162bSMatthew G. Knepley   Output Parameter:
4846bb7acecfSBarry Smith . sf - The `PetscSF`
48474f37162bSMatthew G. Knepley 
48484f37162bSMatthew G. Knepley   Level: intermediate
48494f37162bSMatthew G. Knepley 
4850bb7acecfSBarry Smith   Note:
4851bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
48524f37162bSMatthew G. Knepley 
48531cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48544f37162bSMatthew G. Knepley @*/
4855d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4856d71ae5a4SJacob Faibussowitsch {
48574f37162bSMatthew G. Knepley   PetscFunctionBegin;
48584f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48594f572ea9SToby Isaac   PetscAssertPointer(sf, 2);
48604f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
48613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48624f37162bSMatthew G. Knepley }
48634f37162bSMatthew G. Knepley 
48644f37162bSMatthew G. Knepley /*@
48654f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48664f37162bSMatthew G. Knepley 
48674f37162bSMatthew G. Knepley   Input Parameters:
48684f37162bSMatthew G. Knepley + dm - The DM
48694f37162bSMatthew G. Knepley - sf - The PetscSF
48704f37162bSMatthew G. Knepley 
48714f37162bSMatthew G. Knepley   Level: intermediate
48724f37162bSMatthew G. Knepley 
48731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48744f37162bSMatthew G. Knepley @*/
4875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4876d71ae5a4SJacob Faibussowitsch {
48774f37162bSMatthew G. Knepley   PetscFunctionBegin;
48784f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48794f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48809566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48819566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48824f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48844f37162bSMatthew G. Knepley }
48854f37162bSMatthew G. Knepley 
4886d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4887d71ae5a4SJacob Faibussowitsch {
488834aa8a36SMatthew G. Knepley   PetscClassId id;
488934aa8a36SMatthew G. Knepley 
489034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48919566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
489234aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48939566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
489434aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48959566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
489617c1d62eSMatthew G. Knepley   } else {
48979566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
489834aa8a36SMatthew G. Knepley   }
48993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
490034aa8a36SMatthew G. Knepley }
490134aa8a36SMatthew G. Knepley 
4902d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4903d71ae5a4SJacob Faibussowitsch {
490444a7f3ddSMatthew G. Knepley   RegionField *tmpr;
490544a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
490644a7f3ddSMatthew G. Knepley 
490744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
49083ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
49099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
491044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
49119371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
49129371c9d4SSatish Balay     tmpr[f].disc        = NULL;
49139371c9d4SSatish Balay     tmpr[f].label       = NULL;
49149371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
49159371c9d4SSatish Balay   }
49169566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
491744a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
491844a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
49193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
492044a7f3ddSMatthew G. Knepley }
492144a7f3ddSMatthew G. Knepley 
492244a7f3ddSMatthew G. Knepley /*@
492320f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
492444a7f3ddSMatthew G. Knepley 
492520f4b53cSBarry Smith   Logically Collective
492644a7f3ddSMatthew G. Knepley 
492744a7f3ddSMatthew G. Knepley   Input Parameter:
492820f4b53cSBarry Smith . dm - The `DM`
492944a7f3ddSMatthew G. Knepley 
493044a7f3ddSMatthew G. Knepley   Level: intermediate
493144a7f3ddSMatthew G. Knepley 
49321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
493344a7f3ddSMatthew G. Knepley @*/
4934d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4935d71ae5a4SJacob Faibussowitsch {
493644a7f3ddSMatthew G. Knepley   PetscInt f;
493744a7f3ddSMatthew G. Knepley 
493844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
493944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
494044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
49419566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
49429566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
494344a7f3ddSMatthew G. Knepley   }
49449566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
494544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
494644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
49473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
494844a7f3ddSMatthew G. Knepley }
494944a7f3ddSMatthew G. Knepley 
4950689b5837SMatthew G. Knepley /*@
495120f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4952689b5837SMatthew G. Knepley 
495320f4b53cSBarry Smith   Not Collective
4954689b5837SMatthew G. Knepley 
4955689b5837SMatthew G. Knepley   Input Parameter:
495620f4b53cSBarry Smith . dm - The `DM`
4957689b5837SMatthew G. Knepley 
4958689b5837SMatthew G. Knepley   Output Parameter:
495960225df5SJacob Faibussowitsch . numFields - The number of fields
4960689b5837SMatthew G. Knepley 
4961689b5837SMatthew G. Knepley   Level: intermediate
4962689b5837SMatthew G. Knepley 
49631cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetNumFields()`, `DMSetField()`
4964689b5837SMatthew G. Knepley @*/
4965d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4966d71ae5a4SJacob Faibussowitsch {
49670f21e855SMatthew G. Knepley   PetscFunctionBegin;
49680f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49694f572ea9SToby Isaac   PetscAssertPointer(numFields, 2);
497044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
49713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4972af122d2aSMatthew G Knepley }
4973af122d2aSMatthew G Knepley 
4974689b5837SMatthew G. Knepley /*@
497520f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4976689b5837SMatthew G. Knepley 
497720f4b53cSBarry Smith   Logically Collective
4978689b5837SMatthew G. Knepley 
4979689b5837SMatthew G. Knepley   Input Parameters:
498020f4b53cSBarry Smith + dm        - The `DM`
498160225df5SJacob Faibussowitsch - numFields - The number of fields
4982689b5837SMatthew G. Knepley 
4983689b5837SMatthew G. Knepley   Level: intermediate
4984689b5837SMatthew G. Knepley 
49851cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumFields()`, `DMSetField()`
4986689b5837SMatthew G. Knepley @*/
4987d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4988d71ae5a4SJacob Faibussowitsch {
49890f21e855SMatthew G. Knepley   PetscInt Nf, f;
4990af122d2aSMatthew G Knepley 
4991af122d2aSMatthew G Knepley   PetscFunctionBegin;
4992af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49939566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49940f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49950f21e855SMatthew G. Knepley     PetscContainer obj;
49960f21e855SMatthew G. Knepley 
49979566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49989566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49999566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
5000af122d2aSMatthew G Knepley   }
50013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5002af122d2aSMatthew G Knepley }
5003af122d2aSMatthew G Knepley 
5004c1929be8SMatthew G. Knepley /*@
5005bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
5006c1929be8SMatthew G. Knepley 
500720f4b53cSBarry Smith   Not Collective
5008c1929be8SMatthew G. Knepley 
5009c1929be8SMatthew G. Knepley   Input Parameters:
5010bb7acecfSBarry Smith + dm - The `DM`
5011c1929be8SMatthew G. Knepley - f  - The field number
5012c1929be8SMatthew G. Knepley 
501344a7f3ddSMatthew G. Knepley   Output Parameters:
501420f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
501520f4b53cSBarry Smith - disc  - The discretization object (pass in `NULL` if not needed)
5016c1929be8SMatthew G. Knepley 
501744a7f3ddSMatthew G. Knepley   Level: intermediate
5018c1929be8SMatthew G. Knepley 
50191cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`
5020c1929be8SMatthew G. Knepley @*/
5021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
5022d71ae5a4SJacob Faibussowitsch {
5023af122d2aSMatthew G Knepley   PetscFunctionBegin;
5024af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50254f572ea9SToby Isaac   PetscAssertPointer(disc, 4);
50267a8be351SBarry 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);
502744a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
5028bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
50293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5030decb47aaSMatthew G. Knepley }
5031decb47aaSMatthew G. Knepley 
5032083401c6SMatthew G. Knepley /* Does not clear the DS */
5033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5034d71ae5a4SJacob Faibussowitsch {
5035083401c6SMatthew G. Knepley   PetscFunctionBegin;
50369566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
50379566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
50389566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
5039083401c6SMatthew G. Knepley   dm->fields[f].label = label;
5040bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
50419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5042bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
50433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5044083401c6SMatthew G. Knepley }
5045083401c6SMatthew G. Knepley 
5046ffeef943SBarry Smith /*@
5047bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
5048bb7acecfSBarry Smith   the field numbering.
5049c1929be8SMatthew G. Knepley 
505020f4b53cSBarry Smith   Logically Collective
5051c1929be8SMatthew G. Knepley 
5052c1929be8SMatthew G. Knepley   Input Parameters:
5053bb7acecfSBarry Smith + dm    - The `DM`
5054c1929be8SMatthew G. Knepley . f     - The field number
505520f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5056bb7acecfSBarry Smith - disc  - The discretization object
5057c1929be8SMatthew G. Knepley 
505844a7f3ddSMatthew G. Knepley   Level: intermediate
5059c1929be8SMatthew G. Knepley 
50601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMGetField()`
5061c1929be8SMatthew G. Knepley @*/
5062d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5063d71ae5a4SJacob Faibussowitsch {
5064decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5065decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5066e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5067bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50687a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5069bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5070bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50719566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
50723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
507344a7f3ddSMatthew G. Knepley }
507444a7f3ddSMatthew G. Knepley 
5075ffeef943SBarry Smith /*@
5076bb7acecfSBarry 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)
5077bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
507844a7f3ddSMatthew G. Knepley 
507920f4b53cSBarry Smith   Logically Collective
508044a7f3ddSMatthew G. Knepley 
508144a7f3ddSMatthew G. Knepley   Input Parameters:
5082bb7acecfSBarry Smith + dm    - The `DM`
508320f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
5084bb7acecfSBarry Smith - disc  - The discretization object
508544a7f3ddSMatthew G. Knepley 
508644a7f3ddSMatthew G. Knepley   Level: intermediate
508744a7f3ddSMatthew G. Knepley 
5088bb7acecfSBarry Smith   Notes:
5089bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5090bb7acecfSBarry Smith 
5091da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5092bb7acecfSBarry 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
5093bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5094bb7acecfSBarry Smith 
50951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
509644a7f3ddSMatthew G. Knepley @*/
5097d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5098d71ae5a4SJacob Faibussowitsch {
509944a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
510044a7f3ddSMatthew G. Knepley 
510144a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
510244a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5103064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5104bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
51059566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
510644a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5107bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
51089566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5109bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5110bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
51119566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
51123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5113af122d2aSMatthew G Knepley }
51146636e97aSMatthew G Knepley 
5115e5e52638SMatthew G. Knepley /*@
5116e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5117e0b68406SMatthew Knepley 
511820f4b53cSBarry Smith   Logically Collective
5119e0b68406SMatthew Knepley 
5120e0b68406SMatthew Knepley   Input Parameters:
5121bb7acecfSBarry Smith + dm          - The `DM`
5122e0b68406SMatthew Knepley . f           - The field index
5123bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5124e0b68406SMatthew Knepley 
5125e0b68406SMatthew Knepley   Level: intermediate
5126e0b68406SMatthew Knepley 
51271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5128e0b68406SMatthew Knepley @*/
5129d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5130d71ae5a4SJacob Faibussowitsch {
5131e0b68406SMatthew Knepley   PetscFunctionBegin;
513263a3b9bcSJacob 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);
5133e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
51343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5135e0b68406SMatthew Knepley }
5136e0b68406SMatthew Knepley 
5137e0b68406SMatthew Knepley /*@
5138e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5139e0b68406SMatthew Knepley 
514020f4b53cSBarry Smith   Not Collective
5141e0b68406SMatthew Knepley 
5142e0b68406SMatthew Knepley   Input Parameters:
5143bb7acecfSBarry Smith + dm - The `DM`
5144e0b68406SMatthew Knepley - f  - The field index
5145e0b68406SMatthew Knepley 
5146e0b68406SMatthew Knepley   Output Parameter:
5147e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5148e0b68406SMatthew Knepley 
5149e0b68406SMatthew Knepley   Level: intermediate
5150e0b68406SMatthew Knepley 
515160225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`
5152e0b68406SMatthew Knepley @*/
5153d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5154d71ae5a4SJacob Faibussowitsch {
5155e0b68406SMatthew Knepley   PetscFunctionBegin;
515663a3b9bcSJacob 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);
5157e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
51583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5159e0b68406SMatthew Knepley }
5160e0b68406SMatthew Knepley 
5161e0b68406SMatthew Knepley /*@
5162bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5163e5e52638SMatthew G. Knepley 
516420f4b53cSBarry Smith   Collective
5165e5e52638SMatthew G. Knepley 
5166bb4b53efSMatthew G. Knepley   Input Parameters:
5167bb4b53efSMatthew G. Knepley + dm        - The `DM`
5168bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
5169bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
5170e5e52638SMatthew G. Knepley 
5171e5e52638SMatthew G. Knepley   Output Parameter:
5172bb7acecfSBarry Smith . newdm - The `DM`
5173e5e52638SMatthew G. Knepley 
5174e5e52638SMatthew G. Knepley   Level: advanced
5175e5e52638SMatthew G. Knepley 
51761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5177e5e52638SMatthew G. Knepley @*/
5178bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
5179d71ae5a4SJacob Faibussowitsch {
5180e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5181e5e52638SMatthew G. Knepley 
5182e5e52638SMatthew G. Knepley   PetscFunctionBegin;
51833ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
51849566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51859566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5186e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5187e5e52638SMatthew G. Knepley     DMLabel      label;
5188e5e52638SMatthew G. Knepley     PetscObject  field;
5189bb4b53efSMatthew G. Knepley     PetscClassId id;
519034aa8a36SMatthew G. Knepley     PetscBool    useCone, useClosure;
5191e5e52638SMatthew G. Knepley 
51929566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
5193bb4b53efSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(field, &id));
5194bb4b53efSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
5195bb4b53efSMatthew G. Knepley       PetscFE newfe;
5196bb4b53efSMatthew G. Knepley 
5197bb4b53efSMatthew G. Knepley       PetscCall(PetscFELimitDegree((PetscFE)field, minDegree, maxDegree, &newfe));
5198bb4b53efSMatthew G. Knepley       PetscCall(DMSetField(newdm, f, label, (PetscObject)newfe));
5199bb4b53efSMatthew G. Knepley       PetscCall(PetscFEDestroy(&newfe));
5200bb4b53efSMatthew G. Knepley     } else {
52019566063dSJacob Faibussowitsch       PetscCall(DMSetField(newdm, f, label, field));
5202bb4b53efSMatthew G. Knepley     }
52039566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
52049566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
520534aa8a36SMatthew G. Knepley   }
52063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
520734aa8a36SMatthew G. Knepley }
520834aa8a36SMatthew G. Knepley 
520934aa8a36SMatthew G. Knepley /*@
521034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
521134aa8a36SMatthew G. Knepley 
521220f4b53cSBarry Smith   Not Collective
521334aa8a36SMatthew G. Knepley 
521434aa8a36SMatthew G. Knepley   Input Parameters:
521520f4b53cSBarry Smith + dm - The `DM` object
521620f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
521734aa8a36SMatthew G. Knepley 
5218d8d19677SJose E. Roman   Output Parameters:
521934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
522034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
522134aa8a36SMatthew G. Knepley 
522234aa8a36SMatthew G. Knepley   Level: developer
522334aa8a36SMatthew G. Knepley 
522420f4b53cSBarry Smith   Notes:
522520f4b53cSBarry Smith .vb
522620f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
522720f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
522820f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
522920f4b53cSBarry Smith .ve
523020f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
523120f4b53cSBarry Smith 
52321cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
523334aa8a36SMatthew G. Knepley @*/
5234d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5235d71ae5a4SJacob Faibussowitsch {
523634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
523734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52384f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 3);
52394f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 4);
524034aa8a36SMatthew G. Knepley   if (f < 0) {
524134aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
524234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
524334aa8a36SMatthew G. Knepley   } else {
524434aa8a36SMatthew G. Knepley     PetscInt Nf;
524534aa8a36SMatthew G. Knepley 
52469566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52477a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
524834aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
524934aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
525034aa8a36SMatthew G. Knepley   }
52513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
525234aa8a36SMatthew G. Knepley }
525334aa8a36SMatthew G. Knepley 
525434aa8a36SMatthew G. Knepley /*@
525534aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
525634aa8a36SMatthew G. Knepley 
525720f4b53cSBarry Smith   Not Collective
525834aa8a36SMatthew G. Knepley 
525934aa8a36SMatthew G. Knepley   Input Parameters:
526020f4b53cSBarry Smith + dm         - The `DM` object
526134aa8a36SMatthew G. Knepley . f          - The field number
526234aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
526334aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
526434aa8a36SMatthew G. Knepley 
526534aa8a36SMatthew G. Knepley   Level: developer
526634aa8a36SMatthew G. Knepley 
526720f4b53cSBarry Smith   Notes:
526820f4b53cSBarry Smith .vb
526920f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
527020f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
527120f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
527220f4b53cSBarry Smith .ve
527320f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
527420f4b53cSBarry Smith 
52751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
527634aa8a36SMatthew G. Knepley @*/
5277d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5278d71ae5a4SJacob Faibussowitsch {
527934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
528034aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
528134aa8a36SMatthew G. Knepley   if (f < 0) {
528234aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
528334aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
528434aa8a36SMatthew G. Knepley   } else {
528534aa8a36SMatthew G. Knepley     PetscInt Nf;
528634aa8a36SMatthew G. Knepley 
52879566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52887a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
528934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
529034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5291e5e52638SMatthew G. Knepley   }
52923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5293e5e52638SMatthew G. Knepley }
5294e5e52638SMatthew G. Knepley 
5295b0441da4SMatthew G. Knepley /*@
5296b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5297b0441da4SMatthew G. Knepley 
5298b0441da4SMatthew G. Knepley   Not collective
5299b0441da4SMatthew G. Knepley 
5300f899ff85SJose E. Roman   Input Parameter:
530120f4b53cSBarry Smith . dm - The `DM` object
5302b0441da4SMatthew G. Knepley 
5303d8d19677SJose E. Roman   Output Parameters:
5304b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5305b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5306b0441da4SMatthew G. Knepley 
5307b0441da4SMatthew G. Knepley   Level: developer
5308b0441da4SMatthew G. Knepley 
530920f4b53cSBarry Smith   Notes:
531020f4b53cSBarry Smith .vb
531120f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
531220f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
531320f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
531420f4b53cSBarry Smith .ve
531520f4b53cSBarry Smith 
53161cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5317b0441da4SMatthew G. Knepley @*/
5318d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5319d71ae5a4SJacob Faibussowitsch {
5320b0441da4SMatthew G. Knepley   PetscInt Nf;
5321b0441da4SMatthew G. Knepley 
5322b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5323b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53244f572ea9SToby Isaac   if (useCone) PetscAssertPointer(useCone, 2);
53254f572ea9SToby Isaac   if (useClosure) PetscAssertPointer(useClosure, 3);
53269566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5327b0441da4SMatthew G. Knepley   if (!Nf) {
53289566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5329b0441da4SMatthew G. Knepley   } else {
53309566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5331b0441da4SMatthew G. Knepley   }
53323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5333b0441da4SMatthew G. Knepley }
5334b0441da4SMatthew G. Knepley 
5335b0441da4SMatthew G. Knepley /*@
5336b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5337b0441da4SMatthew G. Knepley 
533820f4b53cSBarry Smith   Not Collective
5339b0441da4SMatthew G. Knepley 
5340b0441da4SMatthew G. Knepley   Input Parameters:
534120f4b53cSBarry Smith + dm         - The `DM` object
5342b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5343b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5344b0441da4SMatthew G. Knepley 
5345b0441da4SMatthew G. Knepley   Level: developer
5346b0441da4SMatthew G. Knepley 
534720f4b53cSBarry Smith   Notes:
534820f4b53cSBarry Smith .vb
534920f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
535020f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
535120f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
535220f4b53cSBarry Smith .ve
535320f4b53cSBarry Smith 
53541cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5355b0441da4SMatthew G. Knepley @*/
5356d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5357d71ae5a4SJacob Faibussowitsch {
5358b0441da4SMatthew G. Knepley   PetscInt Nf;
5359b0441da4SMatthew G. Knepley 
5360b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5361b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53629566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5363b0441da4SMatthew G. Knepley   if (!Nf) {
53649566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5365b0441da4SMatthew G. Knepley   } else {
53669566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5367e5e52638SMatthew G. Knepley   }
53683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5369e5e52638SMatthew G. Knepley }
5370e5e52638SMatthew G. Knepley 
5371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5372d71ae5a4SJacob Faibussowitsch {
5373799db056SMatthew G. Knepley   DM           plex;
5374799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5375799db056SMatthew G. Knepley   const char **names;
5376799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5377799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5378799db056SMatthew G. Knepley   size_t       len;
5379799db056SMatthew G. Knepley   MPI_Comm     comm;
5380799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5381783e2ec8SMatthew G. Knepley 
5382783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5383799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5384799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5385799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5386799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5387799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5388799db056SMatthew G. Knepley     PetscDS  dsBC;
5389799db056SMatthew G. Knepley     PetscInt numBd;
5390799db056SMatthew G. Knepley 
539107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5392799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5393799db056SMatthew G. Knepley     maxLabels += numBd;
5394799db056SMatthew G. Knepley   }
5395799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5396799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5397799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5398799db056SMatthew G. Knepley     PetscDS  dsBC;
5399799db056SMatthew G. Knepley     PetscInt numBd, bd;
5400799db056SMatthew G. Knepley 
540107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC, NULL));
5402799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5403799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5404799db056SMatthew G. Knepley       DMLabel      label;
5405799db056SMatthew G. Knepley       PetscInt     field;
5406799db056SMatthew G. Knepley       PetscObject  obj;
5407799db056SMatthew G. Knepley       PetscClassId id;
5408799db056SMatthew G. Knepley 
5409799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
54109566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
54119566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5412799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
54139371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
54149371c9d4SSatish Balay         if (labels[l] == label) break;
5415799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5416783e2ec8SMatthew G. Knepley     }
5417799db056SMatthew G. Knepley   }
5418799db056SMatthew G. Knepley   /* Get label names */
5419799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5420799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
54219371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
54229371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
54239371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
54249371c9d4SSatish Balay   }
5425799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5426462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5427799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5428c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5429799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5430799db056SMatthew G. Knepley   /* Put all names on all processes */
5431799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5432799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5433799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5434799db056SMatthew G. Knepley   gNl = displs[size];
54359371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
54369371c9d4SSatish Balay     counts[p] *= gmaxLen;
54379371c9d4SSatish Balay     displs[p] *= gmaxLen;
54389371c9d4SSatish Balay   }
5439799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5440799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5441799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5442799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5443799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5444799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5445799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
54469371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
54475d6b2bfcSJames Wright       if (glabels[m] == glabels[gl]) goto next_label;
54489566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5449799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
54509566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5451799db056SMatthew G. Knepley     ++gl;
54525d6b2bfcSJames Wright   next_label:
54535d6b2bfcSJames Wright     continue;
5454783e2ec8SMatthew G. Knepley   }
5455799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
54563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5457783e2ec8SMatthew G. Knepley }
5458783e2ec8SMatthew G. Knepley 
5459d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5460d71ae5a4SJacob Faibussowitsch {
5461e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5462e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5463e5e52638SMatthew G. Knepley 
5464e5e52638SMatthew G. Knepley   PetscFunctionBegin;
54653ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
54669566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5467e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
54689371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
54699371c9d4SSatish Balay     tmpd[s].ds     = NULL;
54709371c9d4SSatish Balay     tmpd[s].label  = NULL;
54719371c9d4SSatish Balay     tmpd[s].fields = NULL;
54729371c9d4SSatish Balay   }
54739566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5474e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5475e5e52638SMatthew G. Knepley   dm->probs = tmpd;
54763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5477e5e52638SMatthew G. Knepley }
5478e5e52638SMatthew G. Knepley 
5479e5e52638SMatthew G. Knepley /*@
548020f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5481e5e52638SMatthew G. Knepley 
548220f4b53cSBarry Smith   Not Collective
5483e5e52638SMatthew G. Knepley 
5484e5e52638SMatthew G. Knepley   Input Parameter:
548520f4b53cSBarry Smith . dm - The `DM`
5486e5e52638SMatthew G. Knepley 
5487e5e52638SMatthew G. Knepley   Output Parameter:
548820f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5489e5e52638SMatthew G. Knepley 
5490e5e52638SMatthew G. Knepley   Level: intermediate
5491e5e52638SMatthew G. Knepley 
54921cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMGetCellDS()`
5493e5e52638SMatthew G. Knepley @*/
5494d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5495d71ae5a4SJacob Faibussowitsch {
5496e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5497e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54984f572ea9SToby Isaac   PetscAssertPointer(Nds, 2);
5499e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
55003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5501e5e52638SMatthew G. Knepley }
5502e5e52638SMatthew G. Knepley 
5503e5e52638SMatthew G. Knepley /*@
550420f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5505e5e52638SMatthew G. Knepley 
550620f4b53cSBarry Smith   Logically Collective
5507e5e52638SMatthew G. Knepley 
5508e5e52638SMatthew G. Knepley   Input Parameter:
550920f4b53cSBarry Smith . dm - The `DM`
5510e5e52638SMatthew G. Knepley 
5511e5e52638SMatthew G. Knepley   Level: intermediate
5512e5e52638SMatthew G. Knepley 
55131cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5514e5e52638SMatthew G. Knepley @*/
5515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5516d71ae5a4SJacob Faibussowitsch {
5517e5e52638SMatthew G. Knepley   PetscInt s;
5518e5e52638SMatthew G. Knepley 
5519e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5520e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5521e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
55229566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
552307218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
55249566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
55259566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5526e5e52638SMatthew G. Knepley   }
55279566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5528e5e52638SMatthew G. Knepley   dm->probs = NULL;
5529e5e52638SMatthew G. Knepley   dm->Nds   = 0;
55303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5531e5e52638SMatthew G. Knepley }
5532e5e52638SMatthew G. Knepley 
5533e5e52638SMatthew G. Knepley /*@
553420f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5535e5e52638SMatthew G. Knepley 
553620f4b53cSBarry Smith   Not Collective
5537e5e52638SMatthew G. Knepley 
5538e5e52638SMatthew G. Knepley   Input Parameter:
553920f4b53cSBarry Smith . dm - The `DM`
5540e5e52638SMatthew G. Knepley 
5541e5e52638SMatthew G. Knepley   Output Parameter:
554207218a29SMatthew G. Knepley . ds - The default `PetscDS`
5543e5e52638SMatthew G. Knepley 
5544e5e52638SMatthew G. Knepley   Level: intermediate
5545e5e52638SMatthew G. Knepley 
55461cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCellDS()`, `DMGetRegionDS()`
5547e5e52638SMatthew G. Knepley @*/
554807218a29SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *ds)
5549d71ae5a4SJacob Faibussowitsch {
5550e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5551e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55524f572ea9SToby Isaac   PetscAssertPointer(ds, 2);
555307218a29SMatthew G. Knepley   PetscCheck(dm->Nds > 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Need to call DMCreateDS() before calling DMGetDS()");
555407218a29SMatthew G. Knepley   *ds = dm->probs[0].ds;
55553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5556e5e52638SMatthew G. Knepley }
5557e5e52638SMatthew G. Knepley 
5558e5e52638SMatthew G. Knepley /*@
555920f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5560e5e52638SMatthew G. Knepley 
556120f4b53cSBarry Smith   Not Collective
5562e5e52638SMatthew G. Knepley 
5563e5e52638SMatthew G. Knepley   Input Parameters:
556420f4b53cSBarry Smith + dm    - The `DM`
556520f4b53cSBarry Smith - point - Cell for the `PetscDS`
5566e5e52638SMatthew G. Knepley 
556707218a29SMatthew G. Knepley   Output Parameters:
556807218a29SMatthew G. Knepley + ds   - The `PetscDS` defined on the given cell
556907218a29SMatthew G. Knepley - dsIn - The `PetscDS` for input on the given cell, or NULL if the same ds
5570e5e52638SMatthew G. Knepley 
5571e5e52638SMatthew G. Knepley   Level: developer
5572e5e52638SMatthew G. Knepley 
55731cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDS()`, `DMSetRegionDS()`
5574e5e52638SMatthew G. Knepley @*/
557507218a29SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *ds, PetscDS *dsIn)
5576d71ae5a4SJacob Faibussowitsch {
557707218a29SMatthew G. Knepley   PetscDS  dsDef = NULL;
5578e5e52638SMatthew G. Knepley   PetscInt s;
5579e5e52638SMatthew G. Knepley 
5580e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5581e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55824f572ea9SToby Isaac   if (ds) PetscAssertPointer(ds, 3);
55834f572ea9SToby Isaac   if (dsIn) PetscAssertPointer(dsIn, 4);
558463a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
558507218a29SMatthew G. Knepley   if (ds) *ds = NULL;
558607218a29SMatthew G. Knepley   if (dsIn) *dsIn = NULL;
5587e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5588e5e52638SMatthew G. Knepley     PetscInt val;
5589e5e52638SMatthew G. Knepley 
55909371c9d4SSatish Balay     if (!dm->probs[s].label) {
559107218a29SMatthew G. Knepley       dsDef = dm->probs[s].ds;
55929371c9d4SSatish Balay     } else {
55939566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55949371c9d4SSatish Balay       if (val >= 0) {
559507218a29SMatthew G. Knepley         if (ds) *ds = dm->probs[s].ds;
559607218a29SMatthew G. Knepley         if (dsIn) *dsIn = dm->probs[s].dsIn;
55979371c9d4SSatish Balay         break;
55989371c9d4SSatish Balay       }
5599e5e52638SMatthew G. Knepley     }
5600e5e52638SMatthew G. Knepley   }
560107218a29SMatthew G. Knepley   if (ds && !*ds) *ds = dsDef;
56023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5603e5e52638SMatthew G. Knepley }
5604e5e52638SMatthew G. Knepley 
5605e5e52638SMatthew G. Knepley /*@
560620f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5607e5e52638SMatthew G. Knepley 
560820f4b53cSBarry Smith   Not Collective
5609e5e52638SMatthew G. Knepley 
5610e5e52638SMatthew G. Knepley   Input Parameters:
561120f4b53cSBarry Smith + dm    - The `DM`
561220f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5613e5e52638SMatthew G. Knepley 
5614b3cf3223SMatthew G. Knepley   Output Parameters:
561520f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
561607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
561707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5618e5e52638SMatthew G. Knepley 
5619e5e52638SMatthew G. Knepley   Level: advanced
5620e5e52638SMatthew G. Knepley 
562120f4b53cSBarry Smith   Note:
562220f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
562320f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
562407218a29SMatthew G. Knepley   fields = `NULL` and ds = `NULL` if there is no `PetscDS` for the full domain.
562520f4b53cSBarry Smith 
56261cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5627e5e52638SMatthew G. Knepley @*/
562807218a29SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5629d71ae5a4SJacob Faibussowitsch {
5630e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5631e5e52638SMatthew G. Knepley 
5632e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5633e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5634e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
56359371c9d4SSatish Balay   if (fields) {
56364f572ea9SToby Isaac     PetscAssertPointer(fields, 3);
56379371c9d4SSatish Balay     *fields = NULL;
56389371c9d4SSatish Balay   }
56399371c9d4SSatish Balay   if (ds) {
56404f572ea9SToby Isaac     PetscAssertPointer(ds, 4);
56419371c9d4SSatish Balay     *ds = NULL;
56429371c9d4SSatish Balay   }
564307218a29SMatthew G. Knepley   if (dsIn) {
56444f572ea9SToby Isaac     PetscAssertPointer(dsIn, 5);
564507218a29SMatthew G. Knepley     *dsIn = NULL;
564607218a29SMatthew G. Knepley   }
5647e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5648154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5649b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5650b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
565107218a29SMatthew G. Knepley       if (dsIn) *dsIn = dm->probs[s].dsIn;
56523ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5653b3cf3223SMatthew G. Knepley     }
5654e5e52638SMatthew G. Knepley   }
56553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5656e5e52638SMatthew G. Knepley }
5657e5e52638SMatthew G. Knepley 
5658e5e52638SMatthew G. Knepley /*@
5659bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5660083401c6SMatthew G. Knepley 
566120f4b53cSBarry Smith   Collective
5662083401c6SMatthew G. Knepley 
5663083401c6SMatthew G. Knepley   Input Parameters:
5664bb7acecfSBarry Smith + dm     - The `DM`
566520f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
566607218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
566707218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region
566807218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5669083401c6SMatthew G. Knepley 
567020f4b53cSBarry Smith   Level: advanced
567120f4b53cSBarry Smith 
5672bb7acecfSBarry Smith   Note:
5673bb7acecfSBarry 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,
5674083401c6SMatthew G. Knepley   the fields argument is ignored.
5675083401c6SMatthew G. Knepley 
56761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5677083401c6SMatthew G. Knepley @*/
567807218a29SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5679d71ae5a4SJacob Faibussowitsch {
5680083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5681083401c6SMatthew G. Knepley 
5682083401c6SMatthew G. Knepley   PetscFunctionBegin;
5683083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5684083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
568507218a29SMatthew G. Knepley   if (fields) PetscValidHeaderSpecific(fields, IS_CLASSID, 3);
5686064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
568707218a29SMatthew G. Knepley   if (dsIn) PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 5);
5688083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5689083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
56909566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
569107218a29SMatthew G. Knepley       PetscCall(PetscDSDestroy(&dm->probs[s].dsIn));
5692083401c6SMatthew G. Knepley       dm->probs[s].ds   = ds;
569307218a29SMatthew G. Knepley       dm->probs[s].dsIn = dsIn;
56943ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5695083401c6SMatthew G. Knepley     }
5696083401c6SMatthew G. Knepley   }
56979566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56989566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
57009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
570107218a29SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)dsIn));
5702083401c6SMatthew G. Knepley   if (!label) {
5703083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5704083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5705083401c6SMatthew G. Knepley     Nds = 0;
5706083401c6SMatthew G. Knepley   }
5707083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5708083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5709083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
571007218a29SMatthew G. Knepley   dm->probs[Nds].dsIn   = dsIn;
57113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5712083401c6SMatthew G. Knepley }
5713083401c6SMatthew G. Knepley 
5714083401c6SMatthew G. Knepley /*@
571520f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5716e5e52638SMatthew G. Knepley 
571720f4b53cSBarry Smith   Not Collective
5718e5e52638SMatthew G. Knepley 
5719e5e52638SMatthew G. Knepley   Input Parameters:
572020f4b53cSBarry Smith + dm  - The `DM`
5721e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5722e5e52638SMatthew G. Knepley 
5723e5e52638SMatthew G. Knepley   Output Parameters:
572420f4b53cSBarry Smith + label  - The region label, or `NULL`
572520f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
572607218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL`
572707218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input in the given region, or `NULL`
5728e5e52638SMatthew G. Knepley 
5729e5e52638SMatthew G. Knepley   Level: advanced
5730e5e52638SMatthew G. Knepley 
57311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5732e5e52638SMatthew G. Knepley @*/
573307218a29SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds, PetscDS *dsIn)
5734d71ae5a4SJacob Faibussowitsch {
5735e5e52638SMatthew G. Knepley   PetscInt Nds;
5736e5e52638SMatthew G. Knepley 
5737e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5738e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57399566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
574063a3b9bcSJacob 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);
5741e5e52638SMatthew G. Knepley   if (label) {
57424f572ea9SToby Isaac     PetscAssertPointer(label, 3);
5743e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5744e5e52638SMatthew G. Knepley   }
5745b3cf3223SMatthew G. Knepley   if (fields) {
57464f572ea9SToby Isaac     PetscAssertPointer(fields, 4);
5747b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5748b3cf3223SMatthew G. Knepley   }
5749e5e52638SMatthew G. Knepley   if (ds) {
57504f572ea9SToby Isaac     PetscAssertPointer(ds, 5);
5751e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5752e5e52638SMatthew G. Knepley   }
575307218a29SMatthew G. Knepley   if (dsIn) {
57544f572ea9SToby Isaac     PetscAssertPointer(dsIn, 6);
575507218a29SMatthew G. Knepley     *dsIn = dm->probs[num].dsIn;
575607218a29SMatthew G. Knepley   }
57573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5758e5e52638SMatthew G. Knepley }
5759e5e52638SMatthew G. Knepley 
5760e5e52638SMatthew G. Knepley /*@
576120f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5762e5e52638SMatthew G. Knepley 
576320f4b53cSBarry Smith   Not Collective
5764e5e52638SMatthew G. Knepley 
5765e5e52638SMatthew G. Knepley   Input Parameters:
576620f4b53cSBarry Smith + dm     - The `DM`
5767083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
576820f4b53cSBarry Smith . label  - The region label, or `NULL`
576907218a29SMatthew G. Knepley . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` to prevent setting
577007218a29SMatthew G. Knepley . ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
577107218a29SMatthew G. Knepley - dsIn   - The `PetscDS` for input on the given cell, or `NULL` if it is the same `PetscDS`
5772e5e52638SMatthew G. Knepley 
5773e5e52638SMatthew G. Knepley   Level: advanced
5774e5e52638SMatthew G. Knepley 
57751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5776e5e52638SMatthew G. Knepley @*/
577707218a29SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds, PetscDS dsIn)
5778d71ae5a4SJacob Faibussowitsch {
5779083401c6SMatthew G. Knepley   PetscInt Nds;
5780e5e52638SMatthew G. Knepley 
5781e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5782e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5783ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
57849566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
578563a3b9bcSJacob 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);
57869566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
57879566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5788083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5789083401c6SMatthew G. Knepley   if (fields) {
5790083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
57919566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
57929566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5793083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5794e5e52638SMatthew G. Knepley   }
5795083401c6SMatthew G. Knepley   if (ds) {
5796083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
57979566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
57989566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5799083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5800083401c6SMatthew G. Knepley   }
580107218a29SMatthew G. Knepley   if (dsIn) {
580207218a29SMatthew G. Knepley     PetscValidHeaderSpecific(dsIn, PETSCDS_CLASSID, 6);
580307218a29SMatthew G. Knepley     PetscCall(PetscObjectReference((PetscObject)dsIn));
580407218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dm->probs[num].dsIn));
580507218a29SMatthew G. Knepley     dm->probs[num].dsIn = dsIn;
580607218a29SMatthew G. Knepley   }
58073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5808e5e52638SMatthew G. Knepley }
5809e5e52638SMatthew G. Knepley 
5810e5e52638SMatthew G. Knepley /*@
581120f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
58121d3af9e0SMatthew G. Knepley 
581320f4b53cSBarry Smith   Not Collective
58141d3af9e0SMatthew G. Knepley 
58151d3af9e0SMatthew G. Knepley   Input Parameters:
581620f4b53cSBarry Smith + dm - The `DM`
581720f4b53cSBarry Smith - ds - The `PetscDS` defined on the given region
58181d3af9e0SMatthew G. Knepley 
58191d3af9e0SMatthew G. Knepley   Output Parameter:
58201d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
58211d3af9e0SMatthew G. Knepley 
58221d3af9e0SMatthew G. Knepley   Level: advanced
58231d3af9e0SMatthew G. Knepley 
58241cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
58251d3af9e0SMatthew G. Knepley @*/
5826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5827d71ae5a4SJacob Faibussowitsch {
58281d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
58291d3af9e0SMatthew G. Knepley 
58301d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
58311d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58321d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
58334f572ea9SToby Isaac   PetscAssertPointer(num, 3);
58349566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
58359371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
58369371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
58371d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
58381d3af9e0SMatthew G. Knepley   else *num = n;
58393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58401d3af9e0SMatthew G. Knepley }
58411d3af9e0SMatthew G. Knepley 
5842cc4c1da9SBarry Smith /*@
5843bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
58442df84da0SMatthew G. Knepley 
584520f4b53cSBarry Smith   Not Collective
58462df84da0SMatthew G. Knepley 
5847f1a722f8SMatthew G. Knepley   Input Parameters:
5848bb7acecfSBarry Smith + dm     - The `DM`
58492df84da0SMatthew G. Knepley . Nc     - The number of components for the field
585020f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5851bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
58522df84da0SMatthew G. Knepley 
58532df84da0SMatthew G. Knepley   Output Parameter:
5854bb7acecfSBarry Smith . fem - The `PetscFE`
58552df84da0SMatthew G. Knepley 
585620f4b53cSBarry Smith   Level: intermediate
585720f4b53cSBarry Smith 
5858bb7acecfSBarry Smith   Note:
5859bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
58602df84da0SMatthew G. Knepley 
58611cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
58622df84da0SMatthew G. Knepley @*/
5863d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5864d71ae5a4SJacob Faibussowitsch {
58652df84da0SMatthew G. Knepley   DMPolytopeType ct;
58662df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
58672df84da0SMatthew G. Knepley 
58682df84da0SMatthew G. Knepley   PetscFunctionBegin;
58692df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58702df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
58714f572ea9SToby Isaac   if (prefix) PetscAssertPointer(prefix, 3);
58722df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
58734f572ea9SToby Isaac   PetscAssertPointer(fem, 5);
58749566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
58759566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
58769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
58779566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
58783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58792df84da0SMatthew G. Knepley }
58802df84da0SMatthew G. Knepley 
58811d3af9e0SMatthew G. Knepley /*@
5882bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5883e5e52638SMatthew G. Knepley 
588420f4b53cSBarry Smith   Collective
5885e5e52638SMatthew G. Knepley 
5886e5e52638SMatthew G. Knepley   Input Parameter:
5887bb7acecfSBarry Smith . dm - The `DM`
5888e5e52638SMatthew G. Knepley 
588920f4b53cSBarry Smith   Options Database Key:
5890bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
589145480ffeSMatthew G. Knepley 
589220f4b53cSBarry Smith   Level: intermediate
589320f4b53cSBarry Smith 
58941cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5895e5e52638SMatthew G. Knepley @*/
5896d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5897d71ae5a4SJacob Faibussowitsch {
5898e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5899083401c6SMatthew G. Knepley   PetscDS   dsDef;
5900083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5901f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5902f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5903e5e52638SMatthew G. Knepley 
5904e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5905e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59063ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
59079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
59089566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5909083401c6SMatthew G. Knepley   /* Determine how many regions we have */
59109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5911083401c6SMatthew G. Knepley   Nl   = 0;
5912083401c6SMatthew G. Knepley   Ndef = 0;
5913083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5914083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5915083401c6SMatthew G. Knepley     PetscInt l;
5916083401c6SMatthew G. Knepley 
5917f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5918f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5919f918ec44SMatthew G. Knepley     {
5920f918ec44SMatthew G. Knepley       PetscClassId id;
5921f918ec44SMatthew G. Knepley 
59229566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5923f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5924f918ec44SMatthew G. Knepley         Ceed ceed;
5925f918ec44SMatthew G. Knepley 
59269566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
59279566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5928f918ec44SMatthew G. Knepley       }
5929f918ec44SMatthew G. Knepley     }
5930f918ec44SMatthew G. Knepley #endif
59319371c9d4SSatish Balay     if (!label) {
59329371c9d4SSatish Balay       ++Ndef;
59339371c9d4SSatish Balay       continue;
59349371c9d4SSatish Balay     }
59359371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
59369371c9d4SSatish Balay       if (label == labelSet[l]) break;
5937083401c6SMatthew G. Knepley     if (l < Nl) continue;
5938083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5939083401c6SMatthew G. Knepley   }
5940083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
594107218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
5942083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5943b3cf3223SMatthew G. Knepley     IS        fields;
5944b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5945b3cf3223SMatthew G. Knepley 
59469371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59479371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
59487a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
59499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59509371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59519371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
59529566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59539566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59549566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59559566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
595688f0c812SMatthew G. Knepley 
59579566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
595807218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef, NULL));
59599566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59612df9ee95SMatthew G. Knepley   }
596207218a29SMatthew G. Knepley   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef, NULL));
59639566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5964083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5965083401c6SMatthew G. Knepley   if (Ndef && Nl) {
59660122748bSMatthew G. Knepley     DM              plex;
5967083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5968083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5969083401c6SMatthew G. Knepley     PetscInt       *fields;
5970083401c6SMatthew G. Knepley     const PetscInt *cells;
5971083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
59720122748bSMatthew G. Knepley 
59739566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
59749566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
59759566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
59769566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
59775fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5978083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5979083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5980083401c6SMatthew G. Knepley       IS      pointIS;
5981083401c6SMatthew G. Knepley 
59829566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
59839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
59849566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
59859566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5986083401c6SMatthew G. Knepley     }
59879566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5988083401c6SMatthew G. Knepley 
59899566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
59909566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
59919566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
59929566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
59939566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
59949566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
59959566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5996083401c6SMatthew G. Knepley 
59979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
59989371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
59999371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
60009566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
60019566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
60029566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
60039566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
6004083401c6SMatthew G. Knepley 
60059566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
600607218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef, NULL));
60079566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
60089566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
60099566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
60109566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
60119566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
6012083401c6SMatthew G. Knepley   }
6013083401c6SMatthew G. Knepley   /* Create label DSes
6014083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
6015083401c6SMatthew G. Knepley   */
6016083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
6017083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
6018083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
601907218a29SMatthew G. Knepley     PetscDS   ds, dsIn = NULL;
6020083401c6SMatthew G. Knepley     IS        fields;
6021083401c6SMatthew G. Knepley     PetscInt *fld, nf;
6022083401c6SMatthew G. Knepley 
60239566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
60249371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60259371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
60269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
60279371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
60289371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
60299566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
60309566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
60319566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
60329566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
60339566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
6034083401c6SMatthew G. Knepley     {
6035083401c6SMatthew G. Knepley       DMPolytopeType ct;
6036083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
60375fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
60380122748bSMatthew G. Knepley 
60399566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
6040665f567fSMatthew G. Knepley       if (lStart >= 0) {
60419566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
6042412e9a14SMatthew G. Knepley         switch (ct) {
6043412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
6044412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
6045412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
6046d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
6047d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
6048d71ae5a4SJacob Faibussowitsch           break;
6049d71ae5a4SJacob Faibussowitsch         default:
6050d71ae5a4SJacob Faibussowitsch           break;
6051412e9a14SMatthew G. Knepley         }
6052665f567fSMatthew G. Knepley       }
6053462c564dSBarry Smith       PetscCallMPI(MPIU_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
605407218a29SMatthew G. Knepley       if (isCohesive) {
605507218a29SMatthew G. Knepley         PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsIn));
605607218a29SMatthew G. Knepley         PetscCall(PetscDSSetCoordinateDimension(dsIn, dE));
605707218a29SMatthew G. Knepley       }
60585fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
60595fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
60605fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
60619566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
60629566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
606307218a29SMatthew G. Knepley             if (dsIn) {
606407218a29SMatthew G. Knepley               PetscCall(PetscDSSetDiscretization(dsIn, nf, NULL));
606507218a29SMatthew G. Knepley               PetscCall(PetscDSSetCohesive(dsIn, nf, isCohesive));
606607218a29SMatthew G. Knepley             }
60675fedec97SMatthew G. Knepley           }
60685fedec97SMatthew G. Knepley           ++nf;
60695fedec97SMatthew G. Knepley         }
60705fedec97SMatthew G. Knepley       }
6071e5e52638SMatthew G. Knepley     }
607207218a29SMatthew G. Knepley     PetscCall(DMSetRegionDS(dm, label, fields, ds, dsIn));
607307218a29SMatthew G. Knepley     PetscCall(ISDestroy(&fields));
60749566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
607507218a29SMatthew G. Knepley     PetscCall(PetscDSDestroy(&dsIn));
6076e5e52638SMatthew G. Knepley   }
60779566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
6078e5e52638SMatthew G. Knepley   /* Set fields in DSes */
6079083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
6080083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
608107218a29SMatthew G. Knepley     PetscDS         dsIn   = dm->probs[s].dsIn;
6082083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
6083083401c6SMatthew G. Knepley     const PetscInt *fld;
60845fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
60855fedec97SMatthew G. Knepley     PetscBool       isCohesive;
6086e5e52638SMatthew G. Knepley 
60879566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
60889566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
60899566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
60909566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
6091083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
6092083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
60935fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
6094e5e52638SMatthew G. Knepley       PetscClassId id;
6095e5e52638SMatthew G. Knepley 
60965fedec97SMatthew G. Knepley       /* Handle DS with no fields */
60979566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
60985fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
609907218a29SMatthew G. Knepley       if (isCohesive) {
610007218a29SMatthew G. Knepley         if (!isCohesiveField) {
610107218a29SMatthew G. Knepley           PetscObject bdDisc;
610207218a29SMatthew G. Knepley 
610307218a29SMatthew G. Knepley           PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&bdDisc));
610407218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(ds, f, bdDisc));
610507218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
610607218a29SMatthew G. Knepley         } else {
61079566063dSJacob Faibussowitsch           PetscCall(PetscDSSetDiscretization(ds, f, disc));
610807218a29SMatthew G. Knepley           PetscCall(PetscDSSetDiscretization(dsIn, f, disc));
610907218a29SMatthew G. Knepley         }
611007218a29SMatthew G. Knepley       } else {
611107218a29SMatthew G. Knepley         PetscCall(PetscDSSetDiscretization(ds, f, disc));
611207218a29SMatthew G. Knepley       }
6113083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
61149566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
6115e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
6116e5e52638SMatthew G. Knepley     }
61179566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
6118e5e52638SMatthew G. Knepley   }
6119f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
61209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
6121f9244615SMatthew G. Knepley   if (flg) {
61223b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
61233b4aee56SMatthew G. Knepley       PetscDS  ds   = dm->probs[s].ds;
612407218a29SMatthew G. Knepley       PetscDS  dsIn = dm->probs[s].dsIn;
61253b4aee56SMatthew G. Knepley       PetscInt Nf, f;
61263b4aee56SMatthew G. Knepley 
61279566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
612807218a29SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
612907218a29SMatthew G. Knepley         PetscCall(PetscDSSetJetDegree(ds, f, k));
613007218a29SMatthew G. Knepley         if (dsIn) PetscCall(PetscDSSetJetDegree(dsIn, f, k));
613107218a29SMatthew G. Knepley       }
61323b4aee56SMatthew G. Knepley     }
6133f9244615SMatthew G. Knepley   }
6134e5e52638SMatthew G. Knepley   /* Setup DSes */
6135e5e52638SMatthew G. Knepley   if (doSetup) {
613607218a29SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
6137cf1363e4SMatthew G. Knepley       if (dm->setfromoptionscalled) {
6138cf1363e4SMatthew G. Knepley         PetscCall(PetscDSSetFromOptions(dm->probs[s].ds));
6139cf1363e4SMatthew G. Knepley         if (dm->probs[s].dsIn) PetscCall(PetscDSSetFromOptions(dm->probs[s].dsIn));
6140cf1363e4SMatthew G. Knepley       }
614107218a29SMatthew G. Knepley       PetscCall(PetscDSSetUp(dm->probs[s].ds));
614207218a29SMatthew G. Knepley       if (dm->probs[s].dsIn) PetscCall(PetscDSSetUp(dm->probs[s].dsIn));
614307218a29SMatthew G. Knepley     }
6144e5e52638SMatthew G. Knepley   }
61453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6146e5e52638SMatthew G. Knepley }
6147e5e52638SMatthew G. Knepley 
6148e5e52638SMatthew G. Knepley /*@
6149d2b2dc1eSMatthew G. Knepley   DMUseTensorOrder - Use a tensor product closure ordering for the default section
6150d2b2dc1eSMatthew G. Knepley 
6151d2b2dc1eSMatthew G. Knepley   Input Parameters:
6152d2b2dc1eSMatthew G. Knepley + dm     - The DM
6153d2b2dc1eSMatthew G. Knepley - tensor - Flag for tensor order
6154d2b2dc1eSMatthew G. Knepley 
6155d2b2dc1eSMatthew G. Knepley   Level: developer
6156d2b2dc1eSMatthew G. Knepley 
6157d2b2dc1eSMatthew G. Knepley .seealso: `DMPlexSetClosurePermutationTensor()`, `PetscSectionResetClosurePermutation()`
6158d2b2dc1eSMatthew G. Knepley @*/
6159d2b2dc1eSMatthew G. Knepley PetscErrorCode DMUseTensorOrder(DM dm, PetscBool tensor)
6160d2b2dc1eSMatthew G. Knepley {
6161d2b2dc1eSMatthew G. Knepley   PetscInt  Nf;
6162d2b2dc1eSMatthew G. Knepley   PetscBool reorder = PETSC_TRUE, isPlex;
6163d2b2dc1eSMatthew G. Knepley 
6164d2b2dc1eSMatthew G. Knepley   PetscFunctionBegin;
6165d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMPLEX, &isPlex));
6166d2b2dc1eSMatthew G. Knepley   PetscCall(DMGetNumFields(dm, &Nf));
6167d2b2dc1eSMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
6168d2b2dc1eSMatthew G. Knepley     PetscObject  obj;
6169d2b2dc1eSMatthew G. Knepley     PetscClassId id;
6170d2b2dc1eSMatthew G. Knepley 
6171d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetField(dm, f, NULL, &obj));
6172d2b2dc1eSMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
6173d2b2dc1eSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
6174d2b2dc1eSMatthew G. Knepley       PetscSpace sp;
6175d2b2dc1eSMatthew G. Knepley       PetscBool  tensor;
6176d2b2dc1eSMatthew G. Knepley 
6177d2b2dc1eSMatthew G. Knepley       PetscCall(PetscFEGetBasisSpace((PetscFE)obj, &sp));
6178d2b2dc1eSMatthew G. Knepley       PetscCall(PetscSpacePolynomialGetTensor(sp, &tensor));
6179d2b2dc1eSMatthew G. Knepley       reorder = reorder && tensor ? PETSC_TRUE : PETSC_FALSE;
6180d2b2dc1eSMatthew G. Knepley     } else reorder = PETSC_FALSE;
6181d2b2dc1eSMatthew G. Knepley   }
6182d2b2dc1eSMatthew G. Knepley   if (tensor) {
6183d2b2dc1eSMatthew G. Knepley     if (reorder && isPlex) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
6184d2b2dc1eSMatthew G. Knepley   } else {
6185d2b2dc1eSMatthew G. Knepley     PetscSection s;
6186d2b2dc1eSMatthew G. Knepley 
6187d2b2dc1eSMatthew G. Knepley     PetscCall(DMGetLocalSection(dm, &s));
6188d2b2dc1eSMatthew G. Knepley     if (s) PetscCall(PetscSectionResetClosurePermutation(s));
6189d2b2dc1eSMatthew G. Knepley   }
6190d2b2dc1eSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6191d2b2dc1eSMatthew G. Knepley }
6192d2b2dc1eSMatthew G. Knepley 
6193d2b2dc1eSMatthew G. Knepley /*@
6194bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
61957f96f943SMatthew G. Knepley 
619620f4b53cSBarry Smith   Collective
6197f2cacb80SMatthew G. Knepley 
61987f96f943SMatthew G. Knepley   Input Parameters:
6199bb7acecfSBarry Smith + dm   - The `DM`
62007f96f943SMatthew G. Knepley - time - The time
62017f96f943SMatthew G. Knepley 
62027f96f943SMatthew G. Knepley   Output Parameters:
620320f4b53cSBarry Smith + u   - The vector will be filled with exact solution values, or `NULL`
620420f4b53cSBarry Smith - u_t - The vector will be filled with the time derivative of exact solution values, or `NULL`
620520f4b53cSBarry Smith 
620620f4b53cSBarry Smith   Level: developer
62077f96f943SMatthew G. Knepley 
6208bb7acecfSBarry Smith   Note:
6209bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
62107f96f943SMatthew G. Knepley 
62111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscDSSetExactSolution()`
62127f96f943SMatthew G. Knepley @*/
6213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6214d71ae5a4SJacob Faibussowitsch {
62157f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
62167f96f943SMatthew G. Knepley   void   **ectxs;
6217f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
62187f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
62197f96f943SMatthew G. Knepley 
62207f96f943SMatthew G. Knepley   PetscFunctionBegin;
6221f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6222f60fa741SMatthew G. Knepley   if (u) {
6223f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6224f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6225f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6226f60fa741SMatthew G. Knepley   }
6227f60fa741SMatthew G. Knepley   if (u_t) {
6228f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6229f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6230f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6231f60fa741SMatthew G. Knepley   }
62329566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
62339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
62349566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
62357f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
62367f96f943SMatthew G. Knepley     PetscDS         ds;
62377f96f943SMatthew G. Knepley     DMLabel         label;
62387f96f943SMatthew G. Knepley     IS              fieldIS;
62397f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
62407f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
62417f96f943SMatthew G. Knepley 
624207218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
62439566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
62449566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
62459566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
62469566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6247f2cacb80SMatthew G. Knepley     if (u) {
6248f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6249f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6250f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
62517f96f943SMatthew G. Knepley     }
6252f2cacb80SMatthew G. Knepley     if (u_t) {
62539566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
62549566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6255f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6256f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6257f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6258f2cacb80SMatthew G. Knepley     }
62599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6260f2cacb80SMatthew G. Knepley   }
6261f2cacb80SMatthew G. Knepley   if (u) {
62629566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
62639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6264f2cacb80SMatthew G. Knepley   }
6265f2cacb80SMatthew G. Knepley   if (u_t) {
62669566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
62679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6268f2cacb80SMatthew G. Knepley   }
62699566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6270f60fa741SMatthew G. Knepley   if (u) {
6271f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6272f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6273f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6274f60fa741SMatthew G. Knepley   }
6275f60fa741SMatthew G. Knepley   if (u_t) {
6276f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6277f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6278f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6279f60fa741SMatthew G. Knepley   }
62803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62817f96f943SMatthew G. Knepley }
62827f96f943SMatthew G. Knepley 
6283bb4b53efSMatthew G. Knepley static PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscInt minDegree, PetscInt maxDegree, PetscDS ds, PetscDS dsIn)
6284d71ae5a4SJacob Faibussowitsch {
628507218a29SMatthew G. Knepley   PetscDS dsNew, dsInNew = NULL;
628645480ffeSMatthew G. Knepley 
628745480ffeSMatthew G. Knepley   PetscFunctionBegin;
62889566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
6289bb4b53efSMatthew G. Knepley   PetscCall(PetscDSCopy(ds, minDegree, maxDegree, dm, dsNew));
629007218a29SMatthew G. Knepley   if (dsIn) {
629107218a29SMatthew G. Knepley     PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)dsIn), &dsInNew));
6292bb4b53efSMatthew G. Knepley     PetscCall(PetscDSCopy(dsIn, minDegree, maxDegree, dm, dsInNew));
629345480ffeSMatthew G. Knepley   }
629407218a29SMatthew G. Knepley   PetscCall(DMSetRegionDS(dm, label, fields, dsNew, dsInNew));
62959566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
629607218a29SMatthew G. Knepley   PetscCall(PetscDSDestroy(&dsInNew));
62973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
629845480ffeSMatthew G. Knepley }
629945480ffeSMatthew G. Knepley 
63007f96f943SMatthew G. Knepley /*@
6301bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6302e5e52638SMatthew G. Knepley 
630320f4b53cSBarry Smith   Collective
6304e5e52638SMatthew G. Knepley 
6305bb4b53efSMatthew G. Knepley   Input Parameters:
6306bb4b53efSMatthew G. Knepley + dm        - The `DM`
6307bb4b53efSMatthew G. Knepley . minDegree - Minimum degree for a discretization, or `PETSC_DETERMINE` for no limit
6308bb4b53efSMatthew G. Knepley - maxDegree - Maximum degree for a discretization, or `PETSC_DETERMINE` for no limit
6309e5e52638SMatthew G. Knepley 
6310e5e52638SMatthew G. Knepley   Output Parameter:
6311bb7acecfSBarry Smith . newdm - The `DM`
6312e5e52638SMatthew G. Knepley 
6313e5e52638SMatthew G. Knepley   Level: advanced
6314e5e52638SMatthew G. Knepley 
63151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6316e5e52638SMatthew G. Knepley @*/
6317bb4b53efSMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, PetscInt minDegree, PetscInt maxDegree, DM newdm)
6318d71ae5a4SJacob Faibussowitsch {
6319e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6320e5e52638SMatthew G. Knepley 
6321e5e52638SMatthew G. Knepley   PetscFunctionBegin;
63223ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
63239566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
63249566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6325e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6326e5e52638SMatthew G. Knepley     DMLabel  label;
6327b3cf3223SMatthew G. Knepley     IS       fields;
632807218a29SMatthew G. Knepley     PetscDS  ds, dsIn, newds;
6329783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6330e5e52638SMatthew G. Knepley 
633107218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds, &dsIn));
6332b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
6333bb4b53efSMatthew G. Knepley     PetscCall(DMTransferDS_Internal(newdm, label, fields, minDegree, maxDegree, ds, dsIn));
6334d5b43468SJose E. Roman     /* Complete new labels in the new DS */
633507218a29SMatthew G. Knepley     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds, NULL));
63369566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6337783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6338b8025e53SMatthew G. Knepley       PetscWeakForm wf;
633945480ffeSMatthew G. Knepley       DMLabel       label;
6340783e2ec8SMatthew G. Knepley       PetscInt      field;
6341783e2ec8SMatthew G. Knepley 
63429566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
63439566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6344783e2ec8SMatthew G. Knepley     }
6345e5e52638SMatthew G. Knepley   }
6346799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
63473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6348e5e52638SMatthew G. Knepley }
6349e5e52638SMatthew G. Knepley 
6350e5e52638SMatthew G. Knepley /*@
6351bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6352e5e52638SMatthew G. Knepley 
635320f4b53cSBarry Smith   Collective
6354e5e52638SMatthew G. Knepley 
6355e5e52638SMatthew G. Knepley   Input Parameter:
6356bb7acecfSBarry Smith . dm - The `DM`
6357e5e52638SMatthew G. Knepley 
6358e5e52638SMatthew G. Knepley   Output Parameter:
6359bb7acecfSBarry Smith . newdm - The `DM`
6360e5e52638SMatthew G. Knepley 
6361e5e52638SMatthew G. Knepley   Level: advanced
6362e5e52638SMatthew G. Knepley 
636373ff1848SBarry Smith   Developer Note:
6364bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6365bb7acecfSBarry Smith 
63661cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCopyFields()`, `DMCopyDS()`
6367e5e52638SMatthew G. Knepley @*/
6368d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6369d71ae5a4SJacob Faibussowitsch {
6370e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6371bb4b53efSMatthew G. Knepley   PetscCall(DMCopyFields(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
6372bb4b53efSMatthew G. Knepley   PetscCall(DMCopyDS(dm, PETSC_DETERMINE, PETSC_DETERMINE, newdm));
63733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6374e5e52638SMatthew G. Knepley }
6375e5e52638SMatthew G. Knepley 
6376c73cfb54SMatthew G. Knepley /*@
6377bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6378c73cfb54SMatthew G. Knepley 
637920f4b53cSBarry Smith   Not Collective
6380c73cfb54SMatthew G. Knepley 
6381c73cfb54SMatthew G. Knepley   Input Parameter:
6382bb7acecfSBarry Smith . dm - The `DM`
6383c73cfb54SMatthew G. Knepley 
6384c73cfb54SMatthew G. Knepley   Output Parameter:
6385c73cfb54SMatthew G. Knepley . dim - The topological dimension
6386c73cfb54SMatthew G. Knepley 
6387c73cfb54SMatthew G. Knepley   Level: beginner
6388c73cfb54SMatthew G. Knepley 
63891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetDimension()`, `DMCreate()`
6390c73cfb54SMatthew G. Knepley @*/
6391d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6392d71ae5a4SJacob Faibussowitsch {
6393c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6394c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63954f572ea9SToby Isaac   PetscAssertPointer(dim, 2);
6396c73cfb54SMatthew G. Knepley   *dim = dm->dim;
63973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6398c73cfb54SMatthew G. Knepley }
6399c73cfb54SMatthew G. Knepley 
6400c73cfb54SMatthew G. Knepley /*@
6401bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6402c73cfb54SMatthew G. Knepley 
640320f4b53cSBarry Smith   Collective
6404c73cfb54SMatthew G. Knepley 
6405c73cfb54SMatthew G. Knepley   Input Parameters:
6406bb7acecfSBarry Smith + dm  - The `DM`
6407c73cfb54SMatthew G. Knepley - dim - The topological dimension
6408c73cfb54SMatthew G. Knepley 
6409c73cfb54SMatthew G. Knepley   Level: beginner
6410c73cfb54SMatthew G. Knepley 
64111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetDimension()`, `DMCreate()`
6412c73cfb54SMatthew G. Knepley @*/
6413d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6414d71ae5a4SJacob Faibussowitsch {
6415e5e52638SMatthew G. Knepley   PetscDS  ds;
641645480ffeSMatthew G. Knepley   PetscInt Nds, n;
6417f17e8794SMatthew G. Knepley 
6418c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6419c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6420c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6421c73cfb54SMatthew G. Knepley   dm->dim = dim;
6422d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
64239566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
642445480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
642507218a29SMatthew G. Knepley       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds, NULL));
64269566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
642745480ffeSMatthew G. Knepley     }
6428d17bd122SMatthew G. Knepley   }
64293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6430c73cfb54SMatthew G. Knepley }
6431c73cfb54SMatthew G. Knepley 
6432793f3fe5SMatthew G. Knepley /*@
6433793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6434793f3fe5SMatthew G. Knepley 
643520f4b53cSBarry Smith   Collective
6436793f3fe5SMatthew G. Knepley 
6437793f3fe5SMatthew G. Knepley   Input Parameters:
6438bb7acecfSBarry Smith + dm  - the `DM`
6439793f3fe5SMatthew G. Knepley - dim - the dimension
6440793f3fe5SMatthew G. Knepley 
6441793f3fe5SMatthew G. Knepley   Output Parameters:
6442793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6443aa049354SPatrick Sanan - pEnd   - The first point following points of the given dimension
6444793f3fe5SMatthew G. Knepley 
644520f4b53cSBarry Smith   Level: intermediate
644620f4b53cSBarry Smith 
6447793f3fe5SMatthew G. Knepley   Note:
6448793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6449a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6450793f3fe5SMatthew G. Knepley   then the interval is empty.
6451793f3fe5SMatthew G. Knepley 
64521cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6453793f3fe5SMatthew G. Knepley @*/
6454d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6455d71ae5a4SJacob Faibussowitsch {
6456793f3fe5SMatthew G. Knepley   PetscInt d;
6457793f3fe5SMatthew G. Knepley 
6458793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6459793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64609566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
64617a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6462dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
64633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6464793f3fe5SMatthew G. Knepley }
6465793f3fe5SMatthew G. Knepley 
64666636e97aSMatthew G Knepley /*@
6467bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6468f4d763aaSMatthew G. Knepley 
646920f4b53cSBarry Smith   Collective
64708f700142SStefano Zampini 
6471f4d763aaSMatthew G. Knepley   Input Parameter:
6472bb7acecfSBarry Smith . dm - The original `DM`
6473f4d763aaSMatthew G. Knepley 
6474f4d763aaSMatthew G. Knepley   Output Parameter:
6475bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6476f4d763aaSMatthew G. Knepley 
6477f4d763aaSMatthew G. Knepley   Level: intermediate
6478f4d763aaSMatthew G. Knepley 
6479bb7acecfSBarry Smith   Note:
6480bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6481bb7acecfSBarry 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
6482bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6483bb7acecfSBarry Smith 
64841cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6485f4d763aaSMatthew G. Knepley @*/
6486d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6487d71ae5a4SJacob Faibussowitsch {
6488c26acbdeSMatthew G. Knepley   PetscSection section;
6489eb9d3e4dSMatthew G. Knepley   IS           perm;
6490eb9d3e4dSMatthew G. Knepley   PetscBool    hasConstraints, newDM, gnewDM;
649114f150ffSMatthew G. Knepley 
649214f150ffSMatthew G. Knepley   PetscFunctionBegin;
649314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64944f572ea9SToby Isaac   PetscAssertPointer(odm, 2);
64959566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
64969566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
6497eb9d3e4dSMatthew G. Knepley   PetscCall(PetscSectionGetPermutation(section, &perm));
6498eb9d3e4dSMatthew G. Knepley   newDM = hasConstraints || perm ? PETSC_TRUE : PETSC_FALSE;
6499462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(&newDM, &gnewDM, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
6500eb9d3e4dSMatthew G. Knepley   if (!gnewDM) {
6501c26acbdeSMatthew G. Knepley     *odm = dm;
65023ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6503c26acbdeSMatthew G. Knepley   }
650414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6505c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
650614f150ffSMatthew G. Knepley     PetscSF      sf;
6507eb9d3e4dSMatthew G. Knepley     PetscBool    usePerm = dm->ignorePermOutput ? PETSC_FALSE : PETSC_TRUE;
650814f150ffSMatthew G. Knepley 
65099566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
65109566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
65119566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
65129566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
65139566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
65149566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
6515eb9d3e4dSMatthew G. Knepley     PetscCall(PetscSectionCreateGlobalSection(section, sf, usePerm, PETSC_TRUE, PETSC_FALSE, &gsection));
65169566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
65179566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
651814f150ffSMatthew G. Knepley   }
651914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
65203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
652114f150ffSMatthew G. Knepley }
6522f4d763aaSMatthew G. Knepley 
6523f4d763aaSMatthew G. Knepley /*@
6524cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6525f4d763aaSMatthew G. Knepley 
6526f4d763aaSMatthew G. Knepley   Input Parameter:
6527bb7acecfSBarry Smith . dm - The original `DM`
6528f4d763aaSMatthew G. Knepley 
6529cdb7a50dSMatthew G. Knepley   Output Parameters:
6530cdb7a50dSMatthew G. Knepley + num - The output sequence number
6531cdb7a50dSMatthew G. Knepley - val - The output sequence value
6532f4d763aaSMatthew G. Knepley 
6533f4d763aaSMatthew G. Knepley   Level: intermediate
6534f4d763aaSMatthew G. Knepley 
6535bb7acecfSBarry Smith   Note:
6536bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6537bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6538bb7acecfSBarry Smith 
653973ff1848SBarry Smith   Developer Note:
6540bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6541bb7acecfSBarry Smith   not directly related to the `DM`.
6542f4d763aaSMatthew G. Knepley 
65431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6544f4d763aaSMatthew G. Knepley @*/
6545d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6546d71ae5a4SJacob Faibussowitsch {
6547f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6548f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65499371c9d4SSatish Balay   if (num) {
65504f572ea9SToby Isaac     PetscAssertPointer(num, 2);
65519371c9d4SSatish Balay     *num = dm->outputSequenceNum;
65529371c9d4SSatish Balay   }
65539371c9d4SSatish Balay   if (val) {
65544f572ea9SToby Isaac     PetscAssertPointer(val, 3);
65559371c9d4SSatish Balay     *val = dm->outputSequenceVal;
65569371c9d4SSatish Balay   }
65573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6558f4d763aaSMatthew G. Knepley }
6559f4d763aaSMatthew G. Knepley 
6560f4d763aaSMatthew G. Knepley /*@
6561cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6562f4d763aaSMatthew G. Knepley 
6563f4d763aaSMatthew G. Knepley   Input Parameters:
6564bb7acecfSBarry Smith + dm  - The original `DM`
6565cdb7a50dSMatthew G. Knepley . num - The output sequence number
6566cdb7a50dSMatthew G. Knepley - val - The output sequence value
6567f4d763aaSMatthew G. Knepley 
6568f4d763aaSMatthew G. Knepley   Level: intermediate
6569f4d763aaSMatthew G. Knepley 
6570bb7acecfSBarry Smith   Note:
6571bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6572bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6573f4d763aaSMatthew G. Knepley 
65741cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `VecView()`
6575f4d763aaSMatthew G. Knepley @*/
6576d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6577d71ae5a4SJacob Faibussowitsch {
6578f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6579f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6580f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6581cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
65823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6583cdb7a50dSMatthew G. Knepley }
6584cdb7a50dSMatthew G. Knepley 
65855d83a8b1SBarry Smith /*@
6586bb7acecfSBarry Smith   DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6587cdb7a50dSMatthew G. Knepley 
6588cdb7a50dSMatthew G. Knepley   Input Parameters:
6589bb7acecfSBarry Smith + dm     - The original `DM`
6590b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6591cdb7a50dSMatthew G. Knepley . name   - The sequence name
6592cdb7a50dSMatthew G. Knepley - num    - The output sequence number
6593cdb7a50dSMatthew G. Knepley 
6594cdb7a50dSMatthew G. Knepley   Output Parameter:
6595cdb7a50dSMatthew G. Knepley . val - The output sequence value
6596cdb7a50dSMatthew G. Knepley 
6597cdb7a50dSMatthew G. Knepley   Level: intermediate
6598cdb7a50dSMatthew G. Knepley 
6599bb7acecfSBarry Smith   Note:
6600bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6601bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6602bb7acecfSBarry Smith 
660373ff1848SBarry Smith   Developer Note:
6604bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6605cdb7a50dSMatthew G. Knepley 
66061cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6607cdb7a50dSMatthew G. Knepley @*/
6608b2033f5dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char name[], PetscInt num, PetscReal *val)
6609d71ae5a4SJacob Faibussowitsch {
6610cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6611cdb7a50dSMatthew G. Knepley 
6612cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6613cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6614cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6615b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
66164f572ea9SToby Isaac   PetscAssertPointer(val, 5);
66179566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6618cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6619cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6620cdb7a50dSMatthew G. Knepley     PetscScalar value;
6621cdb7a50dSMatthew G. Knepley 
66229566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
66234aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6624cdb7a50dSMatthew G. Knepley #endif
6625cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
66263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6627f4d763aaSMatthew G. Knepley }
66288e4ac7eaSMatthew G. Knepley 
66298e4ac7eaSMatthew G. Knepley /*@
6630b2033f5dSMatthew G. Knepley   DMGetOutputSequenceLength - Retrieve the number of sequence values from a `PetscViewer`
6631b2033f5dSMatthew G. Knepley 
6632b2033f5dSMatthew G. Knepley   Input Parameters:
6633b2033f5dSMatthew G. Knepley + dm     - The original `DM`
6634b2033f5dSMatthew G. Knepley . viewer - The `PetscViewer` to get it from
6635b2033f5dSMatthew G. Knepley - name   - The sequence name
6636b2033f5dSMatthew G. Knepley 
6637b2033f5dSMatthew G. Knepley   Output Parameter:
6638b2033f5dSMatthew G. Knepley . len - The length of the output sequence
6639b2033f5dSMatthew G. Knepley 
6640b2033f5dSMatthew G. Knepley   Level: intermediate
6641b2033f5dSMatthew G. Knepley 
6642b2033f5dSMatthew G. Knepley   Note:
6643b2033f5dSMatthew G. Knepley   This is intended for output that should appear in sequence, for instance
6644b2033f5dSMatthew G. Knepley   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6645b2033f5dSMatthew G. Knepley 
6646b2033f5dSMatthew G. Knepley   Developer Note:
6647b2033f5dSMatthew G. Knepley   It is unclear at the user API level why a `DM` is needed as input
6648b2033f5dSMatthew G. Knepley 
6649b2033f5dSMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6650b2033f5dSMatthew G. Knepley @*/
6651b2033f5dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceLength(DM dm, PetscViewer viewer, const char name[], PetscInt *len)
6652b2033f5dSMatthew G. Knepley {
6653b2033f5dSMatthew G. Knepley   PetscBool ishdf5;
6654b2033f5dSMatthew G. Knepley 
6655b2033f5dSMatthew G. Knepley   PetscFunctionBegin;
6656b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6657b2033f5dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6658b2033f5dSMatthew G. Knepley   PetscAssertPointer(name, 3);
6659b2033f5dSMatthew G. Knepley   PetscAssertPointer(len, 4);
6660b2033f5dSMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6661b2033f5dSMatthew G. Knepley   if (ishdf5) {
6662b2033f5dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6663b2033f5dSMatthew G. Knepley     PetscCall(DMSequenceGetLength_HDF5_Internal(dm, name, len, viewer));
6664b2033f5dSMatthew G. Knepley #endif
6665b2033f5dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6666b2033f5dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6667b2033f5dSMatthew G. Knepley }
6668b2033f5dSMatthew G. Knepley 
6669b2033f5dSMatthew G. Knepley /*@
6670bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66718e4ac7eaSMatthew G. Knepley 
667220f4b53cSBarry Smith   Not Collective
66738e4ac7eaSMatthew G. Knepley 
66748e4ac7eaSMatthew G. Knepley   Input Parameter:
6675bb7acecfSBarry Smith . dm - The `DM`
66768e4ac7eaSMatthew G. Knepley 
66778e4ac7eaSMatthew G. Knepley   Output Parameter:
6678bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
66798e4ac7eaSMatthew G. Knepley 
66808e4ac7eaSMatthew G. Knepley   Level: beginner
66818e4ac7eaSMatthew G. Knepley 
66821cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetUseNatural()`, `DMCreate()`
66838e4ac7eaSMatthew G. Knepley @*/
6684d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6685d71ae5a4SJacob Faibussowitsch {
66868e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66878e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66884f572ea9SToby Isaac   PetscAssertPointer(useNatural, 2);
66898e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66918e4ac7eaSMatthew G. Knepley }
66928e4ac7eaSMatthew G. Knepley 
66938e4ac7eaSMatthew G. Knepley /*@
6694bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
66958e4ac7eaSMatthew G. Knepley 
669620f4b53cSBarry Smith   Collective
66978e4ac7eaSMatthew G. Knepley 
66988e4ac7eaSMatthew G. Knepley   Input Parameters:
6699bb7acecfSBarry Smith + dm         - The `DM`
6700bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
67018e4ac7eaSMatthew G. Knepley 
670273ff1848SBarry Smith   Level: beginner
670373ff1848SBarry Smith 
6704bb7acecfSBarry Smith   Note:
6705bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
67065d3b26e6SMatthew G. Knepley 
67071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
67088e4ac7eaSMatthew G. Knepley @*/
6709d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6710d71ae5a4SJacob Faibussowitsch {
67118e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
67128e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67138833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
67148e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
67153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67168e4ac7eaSMatthew G. Knepley }
6717c58f1c22SToby Isaac 
6718cc4c1da9SBarry Smith /*@
6719bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Not Collective
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac   Input Parameters:
6724bb7acecfSBarry Smith + dm   - The `DM` object
6725c58f1c22SToby Isaac - name - The label name
6726c58f1c22SToby Isaac 
6727c58f1c22SToby Isaac   Level: intermediate
6728c58f1c22SToby Isaac 
67291cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6730c58f1c22SToby Isaac @*/
6731d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6732d71ae5a4SJacob Faibussowitsch {
67335d80c0bfSVaclav Hapla   PetscBool flg;
67345d80c0bfSVaclav Hapla   DMLabel   label;
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   PetscFunctionBegin;
6737c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67384f572ea9SToby Isaac   PetscAssertPointer(name, 2);
67399566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6740c58f1c22SToby Isaac   if (!flg) {
67419566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67429566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67439566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6744c58f1c22SToby Isaac   }
67453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6746c58f1c22SToby Isaac }
6747c58f1c22SToby Isaac 
6748cc4c1da9SBarry Smith /*@
6749bb7acecfSBarry 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.
67500fdc7489SMatthew Knepley 
67510fdc7489SMatthew Knepley   Not Collective
67520fdc7489SMatthew Knepley 
67530fdc7489SMatthew Knepley   Input Parameters:
6754bb7acecfSBarry Smith + dm   - The `DM` object
67550fdc7489SMatthew Knepley . l    - The index for the label
67560fdc7489SMatthew Knepley - name - The label name
67570fdc7489SMatthew Knepley 
67580fdc7489SMatthew Knepley   Level: intermediate
67590fdc7489SMatthew Knepley 
67601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
67610fdc7489SMatthew Knepley @*/
6762d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6763d71ae5a4SJacob Faibussowitsch {
67640fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
67650fdc7489SMatthew Knepley   DMLabel     label;
67660fdc7489SMatthew Knepley   PetscInt    Nl, m;
67670fdc7489SMatthew Knepley   PetscBool   flg, match;
67680fdc7489SMatthew Knepley   const char *lname;
67690fdc7489SMatthew Knepley 
67700fdc7489SMatthew Knepley   PetscFunctionBegin;
67710fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67724f572ea9SToby Isaac   PetscAssertPointer(name, 3);
67739566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
67740fdc7489SMatthew Knepley   if (!flg) {
67759566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
67769566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
67779566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
67780fdc7489SMatthew Knepley   }
67799566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
678063a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
67810fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
67829566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
67839566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
67840fdc7489SMatthew Knepley     if (match) break;
67850fdc7489SMatthew Knepley   }
67863ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
67870fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
67880fdc7489SMatthew Knepley   else prev->next = orig->next;
67890fdc7489SMatthew Knepley   if (!l) {
67900fdc7489SMatthew Knepley     orig->next = dm->labels;
67910fdc7489SMatthew Knepley     dm->labels = orig;
67920fdc7489SMatthew Knepley   } else {
6793fbccb6d4SPierre Jolivet     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next);
67940fdc7489SMatthew Knepley     orig->next = prev->next;
67950fdc7489SMatthew Knepley     prev->next = orig;
67960fdc7489SMatthew Knepley   }
67973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67980fdc7489SMatthew Knepley }
67990fdc7489SMatthew Knepley 
6800cc4c1da9SBarry Smith /*@
6801bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
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 
6810c58f1c22SToby Isaac   Output Parameter:
6811c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6812c58f1c22SToby Isaac 
6813c58f1c22SToby Isaac   Level: beginner
6814c58f1c22SToby Isaac 
68151cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6816c58f1c22SToby Isaac @*/
6817d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(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));
68257a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
68269566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
68273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6828c58f1c22SToby Isaac }
6829c58f1c22SToby Isaac 
6830cc4c1da9SBarry Smith /*@
6831bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6832c58f1c22SToby Isaac 
6833c58f1c22SToby Isaac   Not Collective
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Input Parameters:
6836bb7acecfSBarry Smith + dm    - The `DM` object
6837c58f1c22SToby Isaac . name  - The label name
6838c58f1c22SToby Isaac . point - The mesh point
6839c58f1c22SToby Isaac - value - The label value for this point
6840c58f1c22SToby Isaac 
6841c58f1c22SToby Isaac   Output Parameter:
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Level: beginner
6844c58f1c22SToby Isaac 
68451cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6846c58f1c22SToby Isaac @*/
6847d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6848d71ae5a4SJacob Faibussowitsch {
6849c58f1c22SToby Isaac   DMLabel label;
6850c58f1c22SToby Isaac 
6851c58f1c22SToby Isaac   PetscFunctionBegin;
6852c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68534f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68549566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6855c58f1c22SToby Isaac   if (!label) {
68569566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
68579566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6858c58f1c22SToby Isaac   }
68599566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
68603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6861c58f1c22SToby Isaac }
6862c58f1c22SToby Isaac 
6863cc4c1da9SBarry Smith /*@
6864bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Not Collective
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Input Parameters:
6869bb7acecfSBarry Smith + dm    - The `DM` object
6870c58f1c22SToby Isaac . name  - The label name
6871c58f1c22SToby Isaac . point - The mesh point
6872c58f1c22SToby Isaac - value - The label value for this point
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac   Level: beginner
6875c58f1c22SToby Isaac 
68761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6877c58f1c22SToby Isaac @*/
6878d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6879d71ae5a4SJacob Faibussowitsch {
6880c58f1c22SToby Isaac   DMLabel label;
6881c58f1c22SToby Isaac 
6882c58f1c22SToby Isaac   PetscFunctionBegin;
6883c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68844f572ea9SToby Isaac   PetscAssertPointer(name, 2);
68859566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68863ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68879566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
68883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6889c58f1c22SToby Isaac }
6890c58f1c22SToby Isaac 
6891cc4c1da9SBarry Smith /*@
6892bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   Not Collective
6895c58f1c22SToby Isaac 
6896c58f1c22SToby Isaac   Input Parameters:
6897bb7acecfSBarry Smith + dm   - The `DM` object
6898c58f1c22SToby Isaac - name - The label name
6899c58f1c22SToby Isaac 
6900c58f1c22SToby Isaac   Output Parameter:
6901c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Level: beginner
6904c58f1c22SToby Isaac 
690573ff1848SBarry Smith   Developer Note:
6906bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6907bb7acecfSBarry Smith 
69081cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6909c58f1c22SToby Isaac @*/
6910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6911d71ae5a4SJacob Faibussowitsch {
6912c58f1c22SToby Isaac   DMLabel label;
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   PetscFunctionBegin;
6915c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69164f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69174f572ea9SToby Isaac   PetscAssertPointer(size, 3);
69189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6919c58f1c22SToby Isaac   *size = 0;
69203ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69219566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
69223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6923c58f1c22SToby Isaac }
6924c58f1c22SToby Isaac 
6925cc4c1da9SBarry Smith /*@
6926bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6927c58f1c22SToby Isaac 
6928c58f1c22SToby Isaac   Not Collective
6929c58f1c22SToby Isaac 
6930c58f1c22SToby Isaac   Input Parameters:
693160225df5SJacob Faibussowitsch + dm   - The `DM` object
6932c58f1c22SToby Isaac - name - The label name
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Output Parameter:
693520f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6936c58f1c22SToby Isaac 
6937c58f1c22SToby Isaac   Level: beginner
6938c58f1c22SToby Isaac 
69391cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetValueIS()`, `DMGetLabelSize()`
6940c58f1c22SToby Isaac @*/
6941d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6942d71ae5a4SJacob Faibussowitsch {
6943c58f1c22SToby Isaac   DMLabel label;
6944c58f1c22SToby Isaac 
6945c58f1c22SToby Isaac   PetscFunctionBegin;
6946c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69474f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69484f572ea9SToby Isaac   PetscAssertPointer(ids, 3);
69499566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6950c58f1c22SToby Isaac   *ids = NULL;
6951dab2e251SBlaise Bourdin   if (label) {
69529566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6953dab2e251SBlaise Bourdin   } else {
6954dab2e251SBlaise Bourdin     /* returning an empty IS */
69559566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6956dab2e251SBlaise Bourdin   }
69573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6958c58f1c22SToby Isaac }
6959c58f1c22SToby Isaac 
6960cc4c1da9SBarry Smith /*@
6961c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6962c58f1c22SToby Isaac 
6963c58f1c22SToby Isaac   Not Collective
6964c58f1c22SToby Isaac 
6965c58f1c22SToby Isaac   Input Parameters:
6966bb7acecfSBarry Smith + dm    - The `DM` object
6967b6971eaeSBarry Smith . name  - The label name of the stratum
6968c58f1c22SToby Isaac - value - The stratum value
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Output Parameter:
6971bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac   Level: beginner
6974c58f1c22SToby Isaac 
69751cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6976c58f1c22SToby Isaac @*/
6977d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6978d71ae5a4SJacob Faibussowitsch {
6979c58f1c22SToby Isaac   DMLabel label;
6980c58f1c22SToby Isaac 
6981c58f1c22SToby Isaac   PetscFunctionBegin;
6982c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69834f572ea9SToby Isaac   PetscAssertPointer(name, 2);
69844f572ea9SToby Isaac   PetscAssertPointer(size, 4);
69859566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6986c58f1c22SToby Isaac   *size = 0;
69873ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
69889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
69893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6990c58f1c22SToby Isaac }
6991c58f1c22SToby Isaac 
6992cc4c1da9SBarry Smith /*@
6993c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6994c58f1c22SToby Isaac 
6995c58f1c22SToby Isaac   Not Collective
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   Input Parameters:
6998bb7acecfSBarry Smith + dm    - The `DM` object
6999c58f1c22SToby Isaac . name  - The label name
7000c58f1c22SToby Isaac - value - The stratum value
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   Output Parameter:
700320f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
7004c58f1c22SToby Isaac 
7005c58f1c22SToby Isaac   Level: beginner
7006c58f1c22SToby Isaac 
70071cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabelGetStratumIS()`, `DMGetStratumSize()`
7008c58f1c22SToby Isaac @*/
7009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7010d71ae5a4SJacob Faibussowitsch {
7011c58f1c22SToby Isaac   DMLabel label;
7012c58f1c22SToby Isaac 
7013c58f1c22SToby Isaac   PetscFunctionBegin;
7014c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70154f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70164f572ea9SToby Isaac   PetscAssertPointer(points, 4);
70179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
7018c58f1c22SToby Isaac   *points = NULL;
70193ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
70213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7022c58f1c22SToby Isaac }
7023c58f1c22SToby Isaac 
7024cc4c1da9SBarry Smith /*@
70259044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
70264de306b1SToby Isaac 
70274de306b1SToby Isaac   Not Collective
70284de306b1SToby Isaac 
70294de306b1SToby Isaac   Input Parameters:
7030bb7acecfSBarry Smith + dm     - The `DM` object
70314de306b1SToby Isaac . name   - The label name
70324de306b1SToby Isaac . value  - The stratum value
70334de306b1SToby Isaac - points - The stratum points
70344de306b1SToby Isaac 
70354de306b1SToby Isaac   Level: beginner
70364de306b1SToby Isaac 
70371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
70384de306b1SToby Isaac @*/
7039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
7040d71ae5a4SJacob Faibussowitsch {
70414de306b1SToby Isaac   DMLabel label;
70424de306b1SToby Isaac 
70434de306b1SToby Isaac   PetscFunctionBegin;
70444de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70454f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7046292bffcbSToby Isaac   PetscValidHeaderSpecific(points, IS_CLASSID, 4);
70479566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70483ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70499566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
70503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70514de306b1SToby Isaac }
70524de306b1SToby Isaac 
7053cc4c1da9SBarry Smith /*@
7054bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac   Not Collective
7057c58f1c22SToby Isaac 
7058c58f1c22SToby Isaac   Input Parameters:
7059bb7acecfSBarry Smith + dm    - The `DM` object
7060c58f1c22SToby Isaac . name  - The label name
7061c58f1c22SToby Isaac - value - The label value for this point
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Output Parameter:
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Level: beginner
7066c58f1c22SToby Isaac 
70671cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
7068c58f1c22SToby Isaac @*/
7069d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7070d71ae5a4SJacob Faibussowitsch {
7071c58f1c22SToby Isaac   DMLabel label;
7072c58f1c22SToby Isaac 
7073c58f1c22SToby Isaac   PetscFunctionBegin;
7074c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70754f572ea9SToby Isaac   PetscAssertPointer(name, 2);
70769566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
70773ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
70789566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
70793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7080c58f1c22SToby Isaac }
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac /*@
7083bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
7084c58f1c22SToby Isaac 
7085c58f1c22SToby Isaac   Not Collective
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac   Input Parameter:
7088bb7acecfSBarry Smith . dm - The `DM` object
7089c58f1c22SToby Isaac 
7090c58f1c22SToby Isaac   Output Parameter:
7091c58f1c22SToby Isaac . numLabels - the number of Labels
7092c58f1c22SToby Isaac 
7093c58f1c22SToby Isaac   Level: intermediate
7094c58f1c22SToby Isaac 
70951cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7096c58f1c22SToby Isaac @*/
7097d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7098d71ae5a4SJacob Faibussowitsch {
70995d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7100c58f1c22SToby Isaac   PetscInt    n    = 0;
7101c58f1c22SToby Isaac 
7102c58f1c22SToby Isaac   PetscFunctionBegin;
7103c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71044f572ea9SToby Isaac   PetscAssertPointer(numLabels, 2);
71059371c9d4SSatish Balay   while (next) {
71069371c9d4SSatish Balay     ++n;
71079371c9d4SSatish Balay     next = next->next;
71089371c9d4SSatish Balay   }
7109c58f1c22SToby Isaac   *numLabels = n;
71103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7111c58f1c22SToby Isaac }
7112c58f1c22SToby Isaac 
7113cc4c1da9SBarry Smith /*@
7114c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7115c58f1c22SToby Isaac 
7116c58f1c22SToby Isaac   Not Collective
7117c58f1c22SToby Isaac 
7118c58f1c22SToby Isaac   Input Parameters:
7119bb7acecfSBarry Smith + dm - The `DM` object
7120c58f1c22SToby Isaac - n  - the label number
7121c58f1c22SToby Isaac 
7122c58f1c22SToby Isaac   Output Parameter:
7123c58f1c22SToby Isaac . name - the label name
7124c58f1c22SToby Isaac 
7125c58f1c22SToby Isaac   Level: intermediate
7126c58f1c22SToby Isaac 
712773ff1848SBarry Smith   Developer Note:
7128bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
7129bb7acecfSBarry Smith 
71301cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7131c58f1c22SToby Isaac @*/
7132cc4c1da9SBarry Smith PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char *name[])
7133d71ae5a4SJacob Faibussowitsch {
71345d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7135c58f1c22SToby Isaac   PetscInt    l    = 0;
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac   PetscFunctionBegin;
7138c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71394f572ea9SToby Isaac   PetscAssertPointer(name, 3);
7140c58f1c22SToby Isaac   while (next) {
7141c58f1c22SToby Isaac     if (l == n) {
71429566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
71433ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7144c58f1c22SToby Isaac     }
7145c58f1c22SToby Isaac     ++l;
7146c58f1c22SToby Isaac     next = next->next;
7147c58f1c22SToby Isaac   }
714863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7149c58f1c22SToby Isaac }
7150c58f1c22SToby Isaac 
7151cc4c1da9SBarry Smith /*@
7152bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Not Collective
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Input Parameters:
7157bb7acecfSBarry Smith + dm   - The `DM` object
7158c58f1c22SToby Isaac - name - The label name
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Output Parameter:
7161bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Level: intermediate
7164c58f1c22SToby Isaac 
71651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7166c58f1c22SToby Isaac @*/
7167d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7168d71ae5a4SJacob Faibussowitsch {
71695d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7170d67d17b1SMatthew G. Knepley   const char *lname;
7171c58f1c22SToby Isaac 
7172c58f1c22SToby Isaac   PetscFunctionBegin;
7173c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71744f572ea9SToby Isaac   PetscAssertPointer(name, 2);
71754f572ea9SToby Isaac   PetscAssertPointer(hasLabel, 3);
7176c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7177c58f1c22SToby Isaac   while (next) {
71789566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71799566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
7180c58f1c22SToby Isaac     if (*hasLabel) break;
7181c58f1c22SToby Isaac     next = next->next;
7182c58f1c22SToby Isaac   }
71833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7184c58f1c22SToby Isaac }
7185c58f1c22SToby Isaac 
7186a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7187cc4c1da9SBarry Smith /*@
718820f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
7189c58f1c22SToby Isaac 
7190c58f1c22SToby Isaac   Not Collective
7191c58f1c22SToby Isaac 
7192c58f1c22SToby Isaac   Input Parameters:
7193bb7acecfSBarry Smith + dm   - The `DM` object
7194c58f1c22SToby Isaac - name - The label name
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac   Output Parameter:
719720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
7198c58f1c22SToby Isaac 
7199bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7200bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7201bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7202bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7203bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7204bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7205bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
72066d7c9049SMatthew G. Knepley 
7207c58f1c22SToby Isaac   Level: intermediate
7208c58f1c22SToby Isaac 
720960225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7210c58f1c22SToby Isaac @*/
7211d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7212d71ae5a4SJacob Faibussowitsch {
72135d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7214c58f1c22SToby Isaac   PetscBool   hasLabel;
7215d67d17b1SMatthew G. Knepley   const char *lname;
7216c58f1c22SToby Isaac 
7217c58f1c22SToby Isaac   PetscFunctionBegin;
7218c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72194f572ea9SToby Isaac   PetscAssertPointer(name, 2);
72204f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7221c58f1c22SToby Isaac   *label = NULL;
7222c58f1c22SToby Isaac   while (next) {
72239566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72249566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7225c58f1c22SToby Isaac     if (hasLabel) {
7226c58f1c22SToby Isaac       *label = next->label;
7227c58f1c22SToby Isaac       break;
7228c58f1c22SToby Isaac     }
7229c58f1c22SToby Isaac     next = next->next;
7230c58f1c22SToby Isaac   }
72313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7232c58f1c22SToby Isaac }
7233c58f1c22SToby Isaac 
7234cc4c1da9SBarry Smith /*@
7235bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7236c58f1c22SToby Isaac 
7237c58f1c22SToby Isaac   Not Collective
7238c58f1c22SToby Isaac 
7239c58f1c22SToby Isaac   Input Parameters:
7240bb7acecfSBarry Smith + dm - The `DM` object
7241c58f1c22SToby Isaac - n  - the label number
7242c58f1c22SToby Isaac 
7243c58f1c22SToby Isaac   Output Parameter:
7244c58f1c22SToby Isaac . label - the label
7245c58f1c22SToby Isaac 
7246c58f1c22SToby Isaac   Level: intermediate
7247c58f1c22SToby Isaac 
72481cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7249c58f1c22SToby Isaac @*/
7250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7251d71ae5a4SJacob Faibussowitsch {
72525d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7253c58f1c22SToby Isaac   PetscInt    l    = 0;
7254c58f1c22SToby Isaac 
7255c58f1c22SToby Isaac   PetscFunctionBegin;
7256c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72574f572ea9SToby Isaac   PetscAssertPointer(label, 3);
7258c58f1c22SToby Isaac   while (next) {
7259c58f1c22SToby Isaac     if (l == n) {
7260c58f1c22SToby Isaac       *label = next->label;
72613ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7262c58f1c22SToby Isaac     }
7263c58f1c22SToby Isaac     ++l;
7264c58f1c22SToby Isaac     next = next->next;
7265c58f1c22SToby Isaac   }
726663a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7267c58f1c22SToby Isaac }
7268c58f1c22SToby Isaac 
7269cc4c1da9SBarry Smith /*@
7270bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7271c58f1c22SToby Isaac 
7272c58f1c22SToby Isaac   Not Collective
7273c58f1c22SToby Isaac 
7274c58f1c22SToby Isaac   Input Parameters:
7275bb7acecfSBarry Smith + dm    - The `DM` object
7276bb7acecfSBarry Smith - label - The `DMLabel`
7277c58f1c22SToby Isaac 
7278c58f1c22SToby Isaac   Level: developer
7279c58f1c22SToby Isaac 
728060225df5SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7281c58f1c22SToby Isaac @*/
7282d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7283d71ae5a4SJacob Faibussowitsch {
72845d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7285c58f1c22SToby Isaac   PetscBool   hasLabel;
7286d67d17b1SMatthew G. Knepley   const char *lname;
72875d80c0bfSVaclav Hapla   PetscBool   flg;
7288c58f1c22SToby Isaac 
7289c58f1c22SToby Isaac   PetscFunctionBegin;
7290c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72919566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
72929566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
72937a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
72949566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7295c58f1c22SToby Isaac   tmpLabel->label  = label;
7296c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
72975d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
72985d80c0bfSVaclav Hapla   *p = tmpLabel;
72999566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
73009566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
73015d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
73029566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7303ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
73043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7305c58f1c22SToby Isaac }
7306c58f1c22SToby Isaac 
7307a4e35b19SJacob Faibussowitsch // PetscClangLinter pragma ignore: -fdoc-section-header-unknown
7308cc4c1da9SBarry Smith /*@
73094a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
73104a7ee7d0SMatthew G. Knepley 
73114a7ee7d0SMatthew G. Knepley   Not Collective
73124a7ee7d0SMatthew G. Knepley 
73134a7ee7d0SMatthew G. Knepley   Input Parameters:
7314bb7acecfSBarry Smith + dm    - The `DM` object
7315bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
73164a7ee7d0SMatthew G. Knepley 
7317bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7318bb7acecfSBarry Smith + "depth"       - Holds the depth (co-dimension) of each mesh point
7319bb7acecfSBarry Smith . "celltype"    - Holds the topological type of each cell
7320bb7acecfSBarry Smith . "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7321bb7acecfSBarry Smith . "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7322bb7acecfSBarry Smith . "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7323bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
73244a7ee7d0SMatthew G. Knepley 
73254a7ee7d0SMatthew G. Knepley   Level: intermediate
73264a7ee7d0SMatthew G. Knepley 
73271cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
73284a7ee7d0SMatthew G. Knepley @*/
7329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7330d71ae5a4SJacob Faibussowitsch {
73314a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
73324a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
73334a7ee7d0SMatthew G. Knepley   const char *name, *lname;
73344a7ee7d0SMatthew G. Knepley 
73354a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
73364a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73374a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
73389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
73394a7ee7d0SMatthew G. Knepley   while (next) {
73409566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73419566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
73424a7ee7d0SMatthew G. Knepley     if (hasLabel) {
73439566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
73449566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
73454a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
73469566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
73474a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
73489566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
73494a7ee7d0SMatthew G. Knepley       next->label = label;
73504a7ee7d0SMatthew G. Knepley       break;
73514a7ee7d0SMatthew G. Knepley     }
73524a7ee7d0SMatthew G. Knepley     next = next->next;
73534a7ee7d0SMatthew G. Knepley   }
73543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
73554a7ee7d0SMatthew G. Knepley }
73564a7ee7d0SMatthew G. Knepley 
7357cc4c1da9SBarry Smith /*@
7358bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7359c58f1c22SToby Isaac 
7360c58f1c22SToby Isaac   Not Collective
7361c58f1c22SToby Isaac 
7362c58f1c22SToby Isaac   Input Parameters:
7363bb7acecfSBarry Smith + dm   - The `DM` object
7364c58f1c22SToby Isaac - name - The label name
7365c58f1c22SToby Isaac 
7366c58f1c22SToby Isaac   Output Parameter:
736720f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7368bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7369c58f1c22SToby Isaac 
7370c58f1c22SToby Isaac   Level: developer
7371c58f1c22SToby Isaac 
73721cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7373c58f1c22SToby Isaac @*/
7374d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7375d71ae5a4SJacob Faibussowitsch {
737695d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7377c58f1c22SToby Isaac   PetscBool   hasLabel;
7378d67d17b1SMatthew G. Knepley   const char *lname;
7379c58f1c22SToby Isaac 
7380c58f1c22SToby Isaac   PetscFunctionBegin;
7381c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73824f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7383e5472504SVaclav Hapla   if (label) {
73844f572ea9SToby Isaac     PetscAssertPointer(label, 3);
7385c58f1c22SToby Isaac     *label = NULL;
7386e5472504SVaclav Hapla   }
73875d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
73889566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
73899566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7390c58f1c22SToby Isaac     if (hasLabel) {
739195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
73929566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
739395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
73949566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7395ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
739695d578d6SVaclav Hapla       if (label) *label = link->label;
73979566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
73989566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7399c58f1c22SToby Isaac       break;
7400c58f1c22SToby Isaac     }
7401c58f1c22SToby Isaac   }
74023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7403c58f1c22SToby Isaac }
7404c58f1c22SToby Isaac 
7405306894acSVaclav Hapla /*@
7406bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7407306894acSVaclav Hapla 
7408306894acSVaclav Hapla   Not Collective
7409306894acSVaclav Hapla 
7410306894acSVaclav Hapla   Input Parameters:
7411bb7acecfSBarry Smith + dm           - The `DM` object
7412bb7acecfSBarry Smith . label        - The `DMLabel` to be removed from the `DM`
741320f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7414306894acSVaclav Hapla 
7415306894acSVaclav Hapla   Level: developer
7416306894acSVaclav Hapla 
7417bb7acecfSBarry Smith   Note:
7418306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7419bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7420306894acSVaclav Hapla   *label nullified.
7421306894acSVaclav Hapla 
74221cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7423306894acSVaclav Hapla @*/
7424d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7425d71ae5a4SJacob Faibussowitsch {
742643e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7427306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7428306894acSVaclav Hapla 
7429306894acSVaclav Hapla   PetscFunctionBegin;
7430306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74314f572ea9SToby Isaac   PetscAssertPointer(label, 2);
74323ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7433306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7434306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
74355d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
743643e45a93SVaclav Hapla     if (*label == link->label) {
7437306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
743843e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7439306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7440ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
744143e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
74429566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
74439566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7444306894acSVaclav Hapla       break;
7445306894acSVaclav Hapla     }
7446306894acSVaclav Hapla   }
74477a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
74483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7449306894acSVaclav Hapla }
7450306894acSVaclav Hapla 
7451cc4c1da9SBarry Smith /*@
7452c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   Not Collective
7455c58f1c22SToby Isaac 
7456c58f1c22SToby Isaac   Input Parameters:
7457bb7acecfSBarry Smith + dm   - The `DM` object
7458c58f1c22SToby Isaac - name - The label name
7459c58f1c22SToby Isaac 
7460c58f1c22SToby Isaac   Output Parameter:
7461c58f1c22SToby Isaac . output - The flag for output
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac   Level: developer
7464c58f1c22SToby Isaac 
74651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7466c58f1c22SToby Isaac @*/
7467d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7468d71ae5a4SJacob Faibussowitsch {
74695d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7470d67d17b1SMatthew G. Knepley   const char *lname;
7471c58f1c22SToby Isaac 
7472c58f1c22SToby Isaac   PetscFunctionBegin;
7473c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74744f572ea9SToby Isaac   PetscAssertPointer(name, 2);
74754f572ea9SToby Isaac   PetscAssertPointer(output, 3);
7476c58f1c22SToby Isaac   while (next) {
7477c58f1c22SToby Isaac     PetscBool flg;
7478c58f1c22SToby Isaac 
74799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
74809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
74819371c9d4SSatish Balay     if (flg) {
74829371c9d4SSatish Balay       *output = next->output;
74833ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
74849371c9d4SSatish Balay     }
7485c58f1c22SToby Isaac     next = next->next;
7486c58f1c22SToby Isaac   }
748798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7488c58f1c22SToby Isaac }
7489c58f1c22SToby Isaac 
7490cc4c1da9SBarry Smith /*@
7491bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7492c58f1c22SToby Isaac 
7493c58f1c22SToby Isaac   Not Collective
7494c58f1c22SToby Isaac 
7495c58f1c22SToby Isaac   Input Parameters:
7496bb7acecfSBarry Smith + dm     - The `DM` object
7497c58f1c22SToby Isaac . name   - The label name
7498bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7499c58f1c22SToby Isaac 
7500c58f1c22SToby Isaac   Level: developer
7501c58f1c22SToby Isaac 
75021cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7503c58f1c22SToby Isaac @*/
7504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7505d71ae5a4SJacob Faibussowitsch {
75065d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7507d67d17b1SMatthew G. Knepley   const char *lname;
7508c58f1c22SToby Isaac 
7509c58f1c22SToby Isaac   PetscFunctionBegin;
7510c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75114f572ea9SToby Isaac   PetscAssertPointer(name, 2);
7512c58f1c22SToby Isaac   while (next) {
7513c58f1c22SToby Isaac     PetscBool flg;
7514c58f1c22SToby Isaac 
75159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
75169566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
75179371c9d4SSatish Balay     if (flg) {
75189371c9d4SSatish Balay       next->output = output;
75193ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
75209371c9d4SSatish Balay     }
7521c58f1c22SToby Isaac     next = next->next;
7522c58f1c22SToby Isaac   }
752398921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7524c58f1c22SToby Isaac }
7525c58f1c22SToby Isaac 
7526c58f1c22SToby Isaac /*@
7527bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7528c58f1c22SToby Isaac 
752920f4b53cSBarry Smith   Collective
7530c58f1c22SToby Isaac 
7531d8d19677SJose E. Roman   Input Parameters:
7532bb7acecfSBarry Smith + dmA   - The `DM` object with initial labels
7533bb7acecfSBarry Smith . dmB   - The `DM` object to which labels are copied
7534bb7acecfSBarry Smith . mode  - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7535bb7acecfSBarry Smith . all   - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7536bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7537c58f1c22SToby Isaac 
7538c58f1c22SToby Isaac   Level: intermediate
7539c58f1c22SToby Isaac 
7540bb7acecfSBarry Smith   Note:
75412cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7542c58f1c22SToby Isaac 
75431cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7544c58f1c22SToby Isaac @*/
7545d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7546d71ae5a4SJacob Faibussowitsch {
75472cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7548c58f1c22SToby Isaac   const char *name;
7549c58f1c22SToby Isaac   PetscBool   flg;
75505d80c0bfSVaclav Hapla   DMLabelLink link;
7551c58f1c22SToby Isaac 
75525d80c0bfSVaclav Hapla   PetscFunctionBegin;
75535d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
75545d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
75555d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
75565d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
75577a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
75583ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
75595d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
75605d80c0bfSVaclav Hapla     label = link->label;
75619566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
75625d80c0bfSVaclav Hapla     if (!all) {
75639566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7564c58f1c22SToby Isaac       if (flg) continue;
75659566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
75667d5acc75SStefano Zampini       if (flg) continue;
75679566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7568ba2698f1SMatthew G. Knepley       if (flg) continue;
75695d80c0bfSVaclav Hapla     }
75709566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
75712cbb9b06SVaclav Hapla     if (labelOld) {
75722cbb9b06SVaclav Hapla       switch (emode) {
7573d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7574d71ae5a4SJacob Faibussowitsch         continue;
7575d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7576d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7577d71ae5a4SJacob Faibussowitsch         break;
7578d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7579d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7580d71ae5a4SJacob Faibussowitsch       default:
7581d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
75822cbb9b06SVaclav Hapla       }
75832cbb9b06SVaclav Hapla     }
75845d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
75859566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
75865d80c0bfSVaclav Hapla     } else {
75875d80c0bfSVaclav Hapla       labelNew = label;
75885d80c0bfSVaclav Hapla     }
75899566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
75909566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7591c58f1c22SToby Isaac   }
75923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7593c58f1c22SToby Isaac }
7594461a15a0SLisandro Dalcin 
7595609dae6eSVaclav Hapla /*@C
7596b4028c23SStefano Zampini   DMCompareLabels - Compare labels between two `DM` objects
7597609dae6eSVaclav Hapla 
759820f4b53cSBarry Smith   Collective; No Fortran Support
7599609dae6eSVaclav Hapla 
7600609dae6eSVaclav Hapla   Input Parameters:
7601bb7acecfSBarry Smith + dm0 - First `DM` object
7602bb7acecfSBarry Smith - dm1 - Second `DM` object
7603609dae6eSVaclav Hapla 
7604a4e35b19SJacob Faibussowitsch   Output Parameters:
76055efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
760620f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7607609dae6eSVaclav Hapla 
7608609dae6eSVaclav Hapla   Level: intermediate
7609609dae6eSVaclav Hapla 
7610609dae6eSVaclav Hapla   Notes:
7611bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7612bb7acecfSBarry Smith 
761320f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7614bb7acecfSBarry Smith 
761520f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7616609dae6eSVaclav Hapla 
76175efe38ccSVaclav Hapla   The output message is set independently on each rank.
7618bb7acecfSBarry Smith 
7619bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7620bb7acecfSBarry Smith 
762120f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7622bb7acecfSBarry Smith 
762320f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7624609dae6eSVaclav Hapla 
7625609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7626bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7627609dae6eSVaclav Hapla 
7628cc4c1da9SBarry Smith   Developer Note:
7629cc4c1da9SBarry Smith   Can automatically generate the Fortran stub because `message` must be freed with `PetscFree()`
7630cc4c1da9SBarry Smith 
76311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7632609dae6eSVaclav Hapla @*/
7633d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7634d71ae5a4SJacob Faibussowitsch {
76355efe38ccSVaclav Hapla   PetscInt    n, i;
7636609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
76375efe38ccSVaclav Hapla   PetscBool   eq;
7638609dae6eSVaclav Hapla   MPI_Comm    comm;
76395efe38ccSVaclav Hapla   PetscMPIInt rank;
7640609dae6eSVaclav Hapla 
7641609dae6eSVaclav Hapla   PetscFunctionBegin;
7642609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7643609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7644609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
76454f572ea9SToby Isaac   if (equal) PetscAssertPointer(equal, 3);
76464f572ea9SToby Isaac   if (message) PetscAssertPointer(message, 4);
76479566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
76489566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
76495efe38ccSVaclav Hapla   {
76505efe38ccSVaclav Hapla     PetscInt n1;
76515efe38ccSVaclav Hapla 
76529566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
76539566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
76545efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
765548a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
7656462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
76575efe38ccSVaclav Hapla     if (!eq) goto finish;
76585efe38ccSVaclav Hapla   }
76595efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7660609dae6eSVaclav Hapla     DMLabel     l0, l1;
7661609dae6eSVaclav Hapla     const char *name;
7662609dae6eSVaclav Hapla     char       *msgInner;
7663609dae6eSVaclav Hapla 
7664609dae6eSVaclav Hapla     /* Ignore label order */
76659566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
76669566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
76679566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7668609dae6eSVaclav Hapla     if (!l1) {
766963a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
76705efe38ccSVaclav Hapla       eq = PETSC_FALSE;
76715efe38ccSVaclav Hapla       break;
7672609dae6eSVaclav Hapla     }
76739566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
76749566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
76759566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
76765efe38ccSVaclav Hapla     if (!eq) break;
7677609dae6eSVaclav Hapla   }
7678462c564dSBarry Smith   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7679609dae6eSVaclav Hapla finish:
76805efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7681609dae6eSVaclav Hapla   if (message) {
7682609dae6eSVaclav Hapla     *message = NULL;
768348a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
76845efe38ccSVaclav Hapla   } else {
768548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
76869566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
76875efe38ccSVaclav Hapla   }
76885efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
76895efe38ccSVaclav Hapla   if (equal) *equal = eq;
76907a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
76913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7692609dae6eSVaclav Hapla }
7693609dae6eSVaclav Hapla 
7694d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7695d71ae5a4SJacob Faibussowitsch {
7696461a15a0SLisandro Dalcin   PetscFunctionBegin;
76974f572ea9SToby Isaac   PetscAssertPointer(label, 2);
7698461a15a0SLisandro Dalcin   if (!*label) {
76999566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
77009566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7701461a15a0SLisandro Dalcin   }
77029566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
77033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7704461a15a0SLisandro Dalcin }
7705461a15a0SLisandro Dalcin 
77060fdc7489SMatthew Knepley /*
77070fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
77080fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
77090fdc7489SMatthew Knepley   (label, id) pair in the DM.
77100fdc7489SMatthew Knepley 
77110fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
77120fdc7489SMatthew Knepley   each label.
77130fdc7489SMatthew Knepley */
7714d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7715d71ae5a4SJacob Faibussowitsch {
77160fdc7489SMatthew Knepley   DMUniversalLabel ul;
77170fdc7489SMatthew Knepley   PetscBool       *active;
77180fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
77190fdc7489SMatthew Knepley 
77200fdc7489SMatthew Knepley   PetscFunctionBegin;
77219566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
77229566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
77239566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
77249566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
77250fdc7489SMatthew Knepley   ul->Nl = 0;
77260fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
77270fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
77280fdc7489SMatthew Knepley     const char *name;
77290fdc7489SMatthew Knepley 
77309566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77319566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
77329566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
77330fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
77340fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
77350fdc7489SMatthew Knepley   }
77369566063dSJacob 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));
77370fdc7489SMatthew Knepley   ul->Nv = 0;
77380fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77390fdc7489SMatthew Knepley     DMLabel     label;
77400fdc7489SMatthew Knepley     PetscInt    nv;
77410fdc7489SMatthew Knepley     const char *name;
77420fdc7489SMatthew Knepley 
77430fdc7489SMatthew Knepley     if (!active[l]) continue;
77449566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
77459566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77479566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
77480fdc7489SMatthew Knepley     ul->indices[m] = l;
77490fdc7489SMatthew Knepley     ul->Nv += nv;
77500fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
77510fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
77520fdc7489SMatthew Knepley     ++m;
77530fdc7489SMatthew Knepley   }
77540fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
77550fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
77560fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
77570fdc7489SMatthew Knepley   }
77580fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
77590fdc7489SMatthew Knepley     PetscInt b;
77600fdc7489SMatthew Knepley 
77610fdc7489SMatthew Knepley     ul->masks[l] = 0;
77620fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
77630fdc7489SMatthew Knepley   }
77649566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
77650fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
77660fdc7489SMatthew Knepley     DMLabel         label;
77670fdc7489SMatthew Knepley     IS              valueIS;
77680fdc7489SMatthew Knepley     const PetscInt *varr;
77690fdc7489SMatthew Knepley     PetscInt        nv, v;
77700fdc7489SMatthew Knepley 
77710fdc7489SMatthew Knepley     if (!active[l]) continue;
77729566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
77739566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
77749566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
77759566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7776ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
77779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
77789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
77799566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
77800fdc7489SMatthew Knepley     ++m;
77810fdc7489SMatthew Knepley   }
77829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
77830fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
77840fdc7489SMatthew Knepley     PetscInt  uval   = 0;
77850fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
77860fdc7489SMatthew Knepley 
77870fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
77880fdc7489SMatthew Knepley       DMLabel  label;
77890649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
77900fdc7489SMatthew Knepley 
77910fdc7489SMatthew Knepley       if (!active[l]) continue;
77929566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
77939566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
77949566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
77959371c9d4SSatish Balay       if (val == defval) {
77969371c9d4SSatish Balay         ++m;
77979371c9d4SSatish Balay         continue;
77989371c9d4SSatish Balay       }
77990649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
78000fdc7489SMatthew Knepley       marked = PETSC_TRUE;
78019566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
780263a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
78030fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
78040fdc7489SMatthew Knepley       ++m;
78050fdc7489SMatthew Knepley     }
78069566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
78070fdc7489SMatthew Knepley   }
78089566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
78090fdc7489SMatthew Knepley   *universal = ul;
78103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78110fdc7489SMatthew Knepley }
78120fdc7489SMatthew Knepley 
7813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7814d71ae5a4SJacob Faibussowitsch {
78150fdc7489SMatthew Knepley   PetscInt l;
78160fdc7489SMatthew Knepley 
78170fdc7489SMatthew Knepley   PetscFunctionBegin;
78189566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
78199566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
78209566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
78219566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
78229566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
78230fdc7489SMatthew Knepley   *universal = NULL;
78243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78250fdc7489SMatthew Knepley }
78260fdc7489SMatthew Knepley 
7827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7828d71ae5a4SJacob Faibussowitsch {
78290fdc7489SMatthew Knepley   PetscFunctionBegin;
78304f572ea9SToby Isaac   PetscAssertPointer(ulabel, 2);
78310fdc7489SMatthew Knepley   *ulabel = ul->label;
78323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78330fdc7489SMatthew Knepley }
78340fdc7489SMatthew Knepley 
7835d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7836d71ae5a4SJacob Faibussowitsch {
78370fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
78380fdc7489SMatthew Knepley 
78390fdc7489SMatthew Knepley   PetscFunctionBegin;
7840064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
78410fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
78429566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
78439566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
78440fdc7489SMatthew Knepley   }
78450fdc7489SMatthew Knepley   if (preserveOrder) {
78460fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
78470fdc7489SMatthew Knepley       const char *name;
78480fdc7489SMatthew Knepley       PetscBool   match;
78490fdc7489SMatthew Knepley 
78509566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
78519566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
785263a3b9bcSJacob 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]);
78530fdc7489SMatthew Knepley     }
78540fdc7489SMatthew Knepley   }
78553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78560fdc7489SMatthew Knepley }
78570fdc7489SMatthew Knepley 
7858d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7859d71ae5a4SJacob Faibussowitsch {
78600fdc7489SMatthew Knepley   PetscInt l;
78610fdc7489SMatthew Knepley 
78620fdc7489SMatthew Knepley   PetscFunctionBegin;
78630fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
78640fdc7489SMatthew Knepley     DMLabel  label;
78650fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
78660fdc7489SMatthew Knepley 
78670fdc7489SMatthew Knepley     if (lval) {
78689566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
78699566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
78709566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
78710fdc7489SMatthew Knepley     }
78720fdc7489SMatthew Knepley   }
78733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78740fdc7489SMatthew Knepley }
7875a8fb8f29SToby Isaac 
7876a8fb8f29SToby Isaac /*@
7877bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7878bb7acecfSBarry Smith 
787920f4b53cSBarry Smith   Not Collective
7880a8fb8f29SToby Isaac 
7881a8fb8f29SToby Isaac   Input Parameter:
7882bb7acecfSBarry Smith . dm - The `DM` object
7883a8fb8f29SToby Isaac 
7884a8fb8f29SToby Isaac   Output Parameter:
7885bb7acecfSBarry Smith . cdm - The coarse `DM`
7886a8fb8f29SToby Isaac 
7887a8fb8f29SToby Isaac   Level: intermediate
7888a8fb8f29SToby Isaac 
78891cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetCoarseDM()`, `DMCoarsen()`
7890a8fb8f29SToby Isaac @*/
7891d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7892d71ae5a4SJacob Faibussowitsch {
7893a8fb8f29SToby Isaac   PetscFunctionBegin;
7894a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
78954f572ea9SToby Isaac   PetscAssertPointer(cdm, 2);
7896a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
78973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7898a8fb8f29SToby Isaac }
7899a8fb8f29SToby Isaac 
7900a8fb8f29SToby Isaac /*@
7901bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7902a8fb8f29SToby Isaac 
7903a8fb8f29SToby Isaac   Input Parameters:
7904bb7acecfSBarry Smith + dm  - The `DM` object
7905bb7acecfSBarry Smith - cdm - The coarse `DM`
7906a8fb8f29SToby Isaac 
7907a8fb8f29SToby Isaac   Level: intermediate
7908a8fb8f29SToby Isaac 
7909bb7acecfSBarry Smith   Note:
7910bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7911bb7acecfSBarry Smith 
79121cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7913a8fb8f29SToby Isaac @*/
7914d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7915d71ae5a4SJacob Faibussowitsch {
7916a8fb8f29SToby Isaac   PetscFunctionBegin;
7917a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7918a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
791989d734beSBarry Smith   if (dm == cdm) cdm = NULL;
79209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
79219566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7922a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
79233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7924a8fb8f29SToby Isaac }
7925a8fb8f29SToby Isaac 
792688bdff64SToby Isaac /*@
7927bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
792888bdff64SToby Isaac 
792988bdff64SToby Isaac   Input Parameter:
7930bb7acecfSBarry Smith . dm - The `DM` object
793188bdff64SToby Isaac 
793288bdff64SToby Isaac   Output Parameter:
7933bb7acecfSBarry Smith . fdm - The fine `DM`
793488bdff64SToby Isaac 
793588bdff64SToby Isaac   Level: intermediate
793688bdff64SToby Isaac 
79371cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
793888bdff64SToby Isaac @*/
7939d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7940d71ae5a4SJacob Faibussowitsch {
794188bdff64SToby Isaac   PetscFunctionBegin;
794288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79434f572ea9SToby Isaac   PetscAssertPointer(fdm, 2);
794488bdff64SToby Isaac   *fdm = dm->fineMesh;
79453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
794688bdff64SToby Isaac }
794788bdff64SToby Isaac 
794888bdff64SToby Isaac /*@
7949bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
795088bdff64SToby Isaac 
795188bdff64SToby Isaac   Input Parameters:
7952bb7acecfSBarry Smith + dm  - The `DM` object
7953bb7acecfSBarry Smith - fdm - The fine `DM`
795488bdff64SToby Isaac 
7955bb7acecfSBarry Smith   Level: developer
795688bdff64SToby Isaac 
7957bb7acecfSBarry Smith   Note:
7958bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7959bb7acecfSBarry Smith 
79601cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
796188bdff64SToby Isaac @*/
7962d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7963d71ae5a4SJacob Faibussowitsch {
796488bdff64SToby Isaac   PetscFunctionBegin;
796588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
796688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
796789d734beSBarry Smith   if (dm == fdm) fdm = NULL;
79689566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
79699566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
797088bdff64SToby Isaac   dm->fineMesh = fdm;
79713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
797288bdff64SToby Isaac }
797388bdff64SToby Isaac 
7974a6ba4734SToby Isaac /*@C
7975bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7976a6ba4734SToby Isaac 
797720f4b53cSBarry Smith   Collective
7978783e2ec8SMatthew G. Knepley 
7979a6ba4734SToby Isaac   Input Parameters:
7980bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7981bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7982a6ba4734SToby Isaac . name     - The BC name
798345480ffeSMatthew G. Knepley . label    - The label defining constrained points
7984bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
798545480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7986a6ba4734SToby Isaac . field    - The field to constrain
798745480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7988a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7989a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
799056cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7991a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7992a6ba4734SToby Isaac 
799345480ffeSMatthew G. Knepley   Output Parameter:
799445480ffeSMatthew G. Knepley . bd - (Optional) Boundary number
799545480ffeSMatthew G. Knepley 
7996a6ba4734SToby Isaac   Options Database Keys:
7997a6ba4734SToby Isaac + -bc_<boundary name> <num>      - Overrides the boundary ids
7998a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7999a6ba4734SToby Isaac 
800020f4b53cSBarry Smith   Level: intermediate
800120f4b53cSBarry Smith 
8002bb7acecfSBarry Smith   Notes:
8003aeebefc2SPierre 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\:
800473ff1848SBarry Smith .vb
800573ff1848SBarry Smith  void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
800673ff1848SBarry Smith .ve
800756cf3b9cSMatthew G. Knepley 
8008a4e35b19SJacob Faibussowitsch   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is\:
800956cf3b9cSMatthew G. Knepley 
801020f4b53cSBarry Smith .vb
801120f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
801220f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
801320f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
801420f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
801520f4b53cSBarry Smith .ve
801656cf3b9cSMatthew G. Knepley + dim - the spatial dimension
801756cf3b9cSMatthew G. Knepley . Nf - the number of fields
801856cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
801956cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
802056cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
802156cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
802256cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
802356cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
802456cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
802556cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
802656cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
802756cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
802856cf3b9cSMatthew G. Knepley . t - current time
802956cf3b9cSMatthew G. Knepley . x - coordinates of the current point
803056cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
803156cf3b9cSMatthew G. Knepley . constants - constant parameters
803256cf3b9cSMatthew G. Knepley - bcval - output values at the current point
803356cf3b9cSMatthew G. Knepley 
80341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DSGetBoundary()`, `PetscDSAddBoundary()`
8035a6ba4734SToby Isaac @*/
8036d71ae5a4SJacob 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)
8037d71ae5a4SJacob Faibussowitsch {
8038e5e52638SMatthew G. Knepley   PetscDS ds;
8039a6ba4734SToby Isaac 
8040a6ba4734SToby Isaac   PetscFunctionBegin;
8041a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8042783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
804345480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
804445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
804545480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
804645480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
804701a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
80489566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8049799db056SMatthew G. Knepley   /* Complete label */
8050799db056SMatthew G. Knepley   if (label) {
8051799db056SMatthew G. Knepley     PetscObject  obj;
8052799db056SMatthew G. Knepley     PetscClassId id;
8053799db056SMatthew G. Knepley 
8054799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
8055799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
8056799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
8057799db056SMatthew G. Knepley       DM plex;
8058799db056SMatthew G. Knepley 
8059799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
8060799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
8061799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
8062799db056SMatthew G. Knepley     }
8063799db056SMatthew G. Knepley   }
80649566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
80653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8066a6ba4734SToby Isaac }
8067a6ba4734SToby Isaac 
806845480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
8069d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
8070d71ae5a4SJacob Faibussowitsch {
8071e5e52638SMatthew G. Knepley   PetscDS     ds;
8072dff059c6SToby Isaac   DMBoundary *lastnext;
8073e6f8dbb6SToby Isaac   DSBoundary  dsbound;
8074e6f8dbb6SToby Isaac 
8075e6f8dbb6SToby Isaac   PetscFunctionBegin;
80769566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
8077e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
807847a1f5adSToby Isaac   if (dm->boundary) {
807947a1f5adSToby Isaac     DMBoundary next = dm->boundary;
808047a1f5adSToby Isaac 
808147a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
80823ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
808347a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
808447a1f5adSToby Isaac     while (next) {
808547a1f5adSToby Isaac       DMBoundary b = next;
808647a1f5adSToby Isaac 
808747a1f5adSToby Isaac       next = b->next;
80889566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
8089a6ba4734SToby Isaac     }
809047a1f5adSToby Isaac     dm->boundary = NULL;
8091a6ba4734SToby Isaac   }
809247a1f5adSToby Isaac 
8093f4f49eeaSPierre Jolivet   lastnext = &dm->boundary;
8094e6f8dbb6SToby Isaac   while (dsbound) {
8095e6f8dbb6SToby Isaac     DMBoundary dmbound;
8096e6f8dbb6SToby Isaac 
80979566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
8098e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
809945480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
810047a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8101dff059c6SToby Isaac     *lastnext = dmbound;
8102f4f49eeaSPierre Jolivet     lastnext  = &dmbound->next;
8103dff059c6SToby Isaac     dsbound   = dsbound->next;
8104a6ba4734SToby Isaac   }
81053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8106a6ba4734SToby Isaac }
8107a6ba4734SToby Isaac 
8108bb7acecfSBarry Smith /* TODO: missing manual page */
8109d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8110d71ae5a4SJacob Faibussowitsch {
8111b95f2879SToby Isaac   DMBoundary b;
8112a6ba4734SToby Isaac 
8113a6ba4734SToby Isaac   PetscFunctionBegin;
8114a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81154f572ea9SToby Isaac   PetscAssertPointer(isBd, 3);
8116a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
81179566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
8118b95f2879SToby Isaac   b = dm->boundary;
811957508eceSPierre Jolivet   while (b && !*isBd) {
8120e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8121e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
8122a6ba4734SToby Isaac     PetscInt   i;
8123a6ba4734SToby Isaac 
812445480ffeSMatthew G. Knepley     if (label) {
812557508eceSPierre Jolivet       for (i = 0; i < dsb->Nv && !*isBd; ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
8126a6ba4734SToby Isaac     }
8127a6ba4734SToby Isaac     b = b->next;
8128a6ba4734SToby Isaac   }
81293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8130a6ba4734SToby Isaac }
81314d6f44ffSToby Isaac 
81324d6f44ffSToby Isaac /*@C
8133bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
8134a6e0b375SMatthew G. Knepley 
813520f4b53cSBarry Smith   Collective
81364d6f44ffSToby Isaac 
81374d6f44ffSToby Isaac   Input Parameters:
8138bb7acecfSBarry Smith + dm    - The `DM`
81390709b2feSToby Isaac . time  - The time
81404d6f44ffSToby Isaac . funcs - The coordinate functions to evaluate, one per field
81414d6f44ffSToby Isaac . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
81424d6f44ffSToby Isaac - mode  - The insertion mode for values
81434d6f44ffSToby Isaac 
81444d6f44ffSToby Isaac   Output Parameter:
81454d6f44ffSToby Isaac . X - vector
81464d6f44ffSToby Isaac 
814720f4b53cSBarry Smith   Calling sequence of `funcs`:
81484d6f44ffSToby Isaac + dim  - The spatial dimension
81498ec8862eSJed Brown . time - The time at which to sample
81504d6f44ffSToby Isaac . x    - The coordinates
815177b739a6SMatthew Knepley . Nc   - The number of components
81524d6f44ffSToby Isaac . u    - The output field values
81534d6f44ffSToby Isaac - ctx  - optional user-defined function context
81544d6f44ffSToby Isaac 
81554d6f44ffSToby Isaac   Level: developer
81564d6f44ffSToby Isaac 
8157bb7acecfSBarry Smith   Developer Notes:
8158bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8159bb7acecfSBarry Smith 
8160bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8161bb7acecfSBarry Smith 
81621cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
81634d6f44ffSToby Isaac @*/
8164a4e35b19SJacob 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)
8165d71ae5a4SJacob Faibussowitsch {
81664d6f44ffSToby Isaac   Vec localX;
81674d6f44ffSToby Isaac 
81684d6f44ffSToby Isaac   PetscFunctionBegin;
81694d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8170708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DM_ProjectFunction, dm, X, 0, 0));
81719566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8172f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
81739566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
81749566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
81759566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
81769566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
8177708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DM_ProjectFunction, dm, X, 0, 0));
81783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81794d6f44ffSToby Isaac }
81804d6f44ffSToby Isaac 
8181a6e0b375SMatthew G. Knepley /*@C
8182bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
8183a6e0b375SMatthew G. Knepley 
818420f4b53cSBarry Smith   Not Collective
8185a6e0b375SMatthew G. Knepley 
8186a6e0b375SMatthew G. Knepley   Input Parameters:
8187bb7acecfSBarry Smith + dm    - The `DM`
8188a6e0b375SMatthew G. Knepley . time  - The time
8189a6e0b375SMatthew G. Knepley . funcs - The coordinate functions to evaluate, one per field
8190a6e0b375SMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8191a6e0b375SMatthew G. Knepley - mode  - The insertion mode for values
8192a6e0b375SMatthew G. Knepley 
8193a6e0b375SMatthew G. Knepley   Output Parameter:
8194a6e0b375SMatthew G. Knepley . localX - vector
8195a6e0b375SMatthew G. Knepley 
819620f4b53cSBarry Smith   Calling sequence of `funcs`:
8197a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8198a4e35b19SJacob Faibussowitsch . time - The current timestep
8199a6e0b375SMatthew G. Knepley . x    - The coordinates
820077b739a6SMatthew Knepley . Nc   - The number of components
8201a6e0b375SMatthew G. Knepley . u    - The output field values
8202a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8203a6e0b375SMatthew G. Knepley 
8204a6e0b375SMatthew G. Knepley   Level: developer
8205a6e0b375SMatthew G. Knepley 
8206bb7acecfSBarry Smith   Developer Notes:
8207bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8208bb7acecfSBarry Smith 
8209bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8210bb7acecfSBarry Smith 
82111cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8212a6e0b375SMatthew G. Knepley @*/
8213a4e35b19SJacob 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)
8214d71ae5a4SJacob Faibussowitsch {
82154d6f44ffSToby Isaac   PetscFunctionBegin;
82164d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8217064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8218fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlocal, time, funcs, ctxs, mode, localX);
82193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82204d6f44ffSToby Isaac }
82214d6f44ffSToby Isaac 
8222a6e0b375SMatthew G. Knepley /*@C
8223bb7acecfSBarry 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.
8224a6e0b375SMatthew G. Knepley 
822520f4b53cSBarry Smith   Collective
8226a6e0b375SMatthew G. Knepley 
8227a6e0b375SMatthew G. Knepley   Input Parameters:
8228bb7acecfSBarry Smith + dm     - The `DM`
8229a6e0b375SMatthew G. Knepley . time   - The time
8230a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8231a4e35b19SJacob Faibussowitsch . ids    - The ids
8232a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8233a4e35b19SJacob Faibussowitsch . comps  - The components
8234bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8235a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8236bb7acecfSBarry Smith . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8237a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8238a6e0b375SMatthew G. Knepley 
8239a6e0b375SMatthew G. Knepley   Output Parameter:
8240a6e0b375SMatthew G. Knepley . X - vector
8241a6e0b375SMatthew G. Knepley 
824220f4b53cSBarry Smith   Calling sequence of `funcs`:
8243a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8244a4e35b19SJacob Faibussowitsch . time - The current timestep
8245a6e0b375SMatthew G. Knepley . x    - The coordinates
824677b739a6SMatthew Knepley . Nc   - The number of components
8247a6e0b375SMatthew G. Knepley . u    - The output field values
8248a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8249a6e0b375SMatthew G. Knepley 
8250a6e0b375SMatthew G. Knepley   Level: developer
8251a6e0b375SMatthew G. Knepley 
8252bb7acecfSBarry Smith   Developer Notes:
8253bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8254bb7acecfSBarry Smith 
8255bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8256bb7acecfSBarry Smith 
82571cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8258a6e0b375SMatthew G. Knepley @*/
8259a4e35b19SJacob 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)
8260d71ae5a4SJacob Faibussowitsch {
82612c53366bSMatthew G. Knepley   Vec localX;
82622c53366bSMatthew G. Knepley 
82632c53366bSMatthew G. Knepley   PetscFunctionBegin;
82642c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8266f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
82679566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
82689566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
82699566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
82709566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
82713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
82722c53366bSMatthew G. Knepley }
82732c53366bSMatthew G. Knepley 
8274a6e0b375SMatthew G. Knepley /*@C
8275bb7acecfSBarry 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.
8276a6e0b375SMatthew G. Knepley 
827720f4b53cSBarry Smith   Not Collective
8278a6e0b375SMatthew G. Knepley 
8279a6e0b375SMatthew G. Knepley   Input Parameters:
8280bb7acecfSBarry Smith + dm     - The `DM`
8281a6e0b375SMatthew G. Knepley . time   - The time
8282bb7acecfSBarry Smith . label  - The `DMLabel` selecting the portion of the mesh for projection
8283a4e35b19SJacob Faibussowitsch . numIds - The number of ids
8284a4e35b19SJacob Faibussowitsch . ids    - The ids
8285a4e35b19SJacob Faibussowitsch . Nc     - The number of components
8286a4e35b19SJacob Faibussowitsch . comps  - The components
8287a6e0b375SMatthew G. Knepley . funcs  - The coordinate functions to evaluate, one per field
8288a6e0b375SMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8289a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8290a6e0b375SMatthew G. Knepley 
8291a6e0b375SMatthew G. Knepley   Output Parameter:
8292a6e0b375SMatthew G. Knepley . localX - vector
8293a6e0b375SMatthew G. Knepley 
829420f4b53cSBarry Smith   Calling sequence of `funcs`:
8295a6e0b375SMatthew G. Knepley + dim  - The spatial dimension
8296a4e35b19SJacob Faibussowitsch . time - The current time
8297a6e0b375SMatthew G. Knepley . x    - The coordinates
829877b739a6SMatthew Knepley . Nc   - The number of components
8299a6e0b375SMatthew G. Knepley . u    - The output field values
8300a6e0b375SMatthew G. Knepley - ctx  - optional user-defined function context
8301a6e0b375SMatthew G. Knepley 
8302a6e0b375SMatthew G. Knepley   Level: developer
8303a6e0b375SMatthew G. Knepley 
8304bb7acecfSBarry Smith   Developer Notes:
8305bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8306bb7acecfSBarry Smith 
8307bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8308bb7acecfSBarry Smith 
83091cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8310a6e0b375SMatthew G. Knepley @*/
8311a4e35b19SJacob 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)
8312d71ae5a4SJacob Faibussowitsch {
83134d6f44ffSToby Isaac   PetscFunctionBegin;
83144d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8315064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8316fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfunctionlabellocal, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);
83173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83184d6f44ffSToby Isaac }
83192716604bSToby Isaac 
8320a6e0b375SMatthew G. Knepley /*@C
8321bb7acecfSBarry 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.
8322a6e0b375SMatthew G. Knepley 
832320f4b53cSBarry Smith   Not Collective
8324a6e0b375SMatthew G. Knepley 
8325a6e0b375SMatthew G. Knepley   Input Parameters:
8326bb7acecfSBarry Smith + dm     - The `DM`
8327a6e0b375SMatthew G. Knepley . time   - The time
832820f4b53cSBarry Smith . localU - The input field vector; may be `NULL` if projection is defined purely by coordinates
8329a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8330a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8331a6e0b375SMatthew G. Knepley 
8332a6e0b375SMatthew G. Knepley   Output Parameter:
8333a6e0b375SMatthew G. Knepley . localX - The output vector
8334a6e0b375SMatthew G. Knepley 
833520f4b53cSBarry Smith   Calling sequence of `funcs`:
8336a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8337a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8338a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8339a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8340a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8341a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8342a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8343a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8344a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8345a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8346a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8347a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8348a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8349a6e0b375SMatthew G. Knepley . t            - The current time
8350a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8351a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8352a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8353a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8354a6e0b375SMatthew G. Knepley 
835573ff1848SBarry Smith   Level: intermediate
835673ff1848SBarry Smith 
8357bb7acecfSBarry Smith   Note:
8358bb7acecfSBarry 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.
8359bb7acecfSBarry 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
8360bb7acecfSBarry 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
8361a6e0b375SMatthew 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.
8362a6e0b375SMatthew G. Knepley 
8363bb7acecfSBarry Smith   Developer Notes:
8364bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8365bb7acecfSBarry Smith 
8366bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8367bb7acecfSBarry Smith 
8368a4e35b19SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`,
8369a4e35b19SJacob Faibussowitsch `DMProjectFunction()`, `DMComputeL2Diff()`
8370a6e0b375SMatthew G. Knepley @*/
8371a4e35b19SJacob 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)
8372d71ae5a4SJacob Faibussowitsch {
83738c6c5593SMatthew G. Knepley   PetscFunctionBegin;
83748c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8375eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
83768c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
8377fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlocal, time, localU, funcs, mode, localX);
83783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83798c6c5593SMatthew G. Knepley }
83808c6c5593SMatthew G. Knepley 
8381a6e0b375SMatthew G. Knepley /*@C
8382a6e0b375SMatthew 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.
8383a6e0b375SMatthew G. Knepley 
838420f4b53cSBarry Smith   Not Collective
8385a6e0b375SMatthew G. Knepley 
8386a6e0b375SMatthew G. Knepley   Input Parameters:
8387bb7acecfSBarry Smith + dm     - The `DM`
8388a6e0b375SMatthew G. Knepley . time   - The time
8389bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8390a6e0b375SMatthew G. Knepley . numIds - The number of label ids to use
8391a6e0b375SMatthew G. Knepley . ids    - The label ids to use for marking
8392bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
839320f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8394a6e0b375SMatthew G. Knepley . localU - The input field vector
8395a6e0b375SMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8396a6e0b375SMatthew G. Knepley - mode   - The insertion mode for values
8397a6e0b375SMatthew G. Knepley 
8398a6e0b375SMatthew G. Knepley   Output Parameter:
8399a6e0b375SMatthew G. Knepley . localX - The output vector
8400a6e0b375SMatthew G. Knepley 
840120f4b53cSBarry Smith   Calling sequence of `funcs`:
8402a6e0b375SMatthew G. Knepley + dim          - The spatial dimension
8403a6e0b375SMatthew G. Knepley . Nf           - The number of input fields
8404a6e0b375SMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8405a6e0b375SMatthew G. Knepley . uOff         - The offset of each field in u[]
8406a6e0b375SMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8407a6e0b375SMatthew G. Knepley . u            - The field values at this point in space
8408a6e0b375SMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8409a6e0b375SMatthew G. Knepley . u_x          - The field derivatives at this point in space
8410a6e0b375SMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8411a6e0b375SMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8412a6e0b375SMatthew G. Knepley . a            - The auxiliary field values at this point in space
8413a6e0b375SMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8414a6e0b375SMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8415a6e0b375SMatthew G. Knepley . t            - The current time
8416a6e0b375SMatthew G. Knepley . x            - The coordinates of this point
8417a6e0b375SMatthew G. Knepley . numConstants - The number of constants
8418a6e0b375SMatthew G. Knepley . constants    - The value of each constant
8419a6e0b375SMatthew G. Knepley - f            - The value of the function at this point in space
8420a6e0b375SMatthew G. Knepley 
842173ff1848SBarry Smith   Level: intermediate
842273ff1848SBarry Smith 
8423bb7acecfSBarry Smith   Note:
8424bb7acecfSBarry 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.
8425bb7acecfSBarry 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
8426bb7acecfSBarry 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
8427a6e0b375SMatthew 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.
8428a6e0b375SMatthew G. Knepley 
8429bb7acecfSBarry Smith   Developer Notes:
8430bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8431bb7acecfSBarry Smith 
8432bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8433bb7acecfSBarry Smith 
84341cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8435a6e0b375SMatthew G. Knepley @*/
8436a4e35b19SJacob 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)
8437d71ae5a4SJacob Faibussowitsch {
84388c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8440064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8441064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8442fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
84433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84448c6c5593SMatthew G. Knepley }
84458c6c5593SMatthew G. Knepley 
84462716604bSToby Isaac /*@C
8447d29d7c6eSMatthew 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.
8448d29d7c6eSMatthew G. Knepley 
844920f4b53cSBarry Smith   Not Collective
8450d29d7c6eSMatthew G. Knepley 
8451d29d7c6eSMatthew G. Knepley   Input Parameters:
8452bb7acecfSBarry Smith + dm     - The `DM`
8453d29d7c6eSMatthew G. Knepley . time   - The time
8454bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain to output
8455d29d7c6eSMatthew G. Knepley . numIds - The number of label ids to use
8456d29d7c6eSMatthew G. Knepley . ids    - The label ids to use for marking
8457bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
845820f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8459d29d7c6eSMatthew G. Knepley . U      - The input field vector
8460d29d7c6eSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8461d29d7c6eSMatthew G. Knepley - mode   - The insertion mode for values
8462d29d7c6eSMatthew G. Knepley 
8463d29d7c6eSMatthew G. Knepley   Output Parameter:
8464d29d7c6eSMatthew G. Knepley . X - The output vector
8465d29d7c6eSMatthew G. Knepley 
846620f4b53cSBarry Smith   Calling sequence of `funcs`:
8467d29d7c6eSMatthew G. Knepley + dim          - The spatial dimension
8468d29d7c6eSMatthew G. Knepley . Nf           - The number of input fields
8469d29d7c6eSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8470d29d7c6eSMatthew G. Knepley . uOff         - The offset of each field in u[]
8471d29d7c6eSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8472d29d7c6eSMatthew G. Knepley . u            - The field values at this point in space
8473d29d7c6eSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8474d29d7c6eSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8475d29d7c6eSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8476d29d7c6eSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8477d29d7c6eSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8478d29d7c6eSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8479d29d7c6eSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8480d29d7c6eSMatthew G. Knepley . t            - The current time
8481d29d7c6eSMatthew G. Knepley . x            - The coordinates of this point
8482d29d7c6eSMatthew G. Knepley . numConstants - The number of constants
8483d29d7c6eSMatthew G. Knepley . constants    - The value of each constant
8484d29d7c6eSMatthew G. Knepley - f            - The value of the function at this point in space
8485d29d7c6eSMatthew G. Knepley 
848673ff1848SBarry Smith   Level: intermediate
848773ff1848SBarry Smith 
8488bb7acecfSBarry Smith   Note:
8489bb7acecfSBarry Smith   There are three different `DM`s that potentially interact in this function. The output `DM`, dm, specifies the layout of the values calculates by funcs.
8490bb7acecfSBarry Smith   The input `DM`, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8491bb7acecfSBarry Smith   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary `DM`, attached to the
8492d29d7c6eSMatthew 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.
8493d29d7c6eSMatthew G. Knepley 
8494bb7acecfSBarry Smith   Developer Notes:
8495bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8496bb7acecfSBarry Smith 
8497bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8498bb7acecfSBarry Smith 
84991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8500d29d7c6eSMatthew G. Knepley @*/
8501a4e35b19SJacob 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)
8502d71ae5a4SJacob Faibussowitsch {
8503d29d7c6eSMatthew G. Knepley   DM  dmIn;
8504d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8505d29d7c6eSMatthew G. Knepley 
8506d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8507d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8508d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8509d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8510d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8511f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
851272fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
851372fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8514d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8515d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8516d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8517d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8518d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
85193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8520d29d7c6eSMatthew G. Knepley }
8521d29d7c6eSMatthew G. Knepley 
8522d29d7c6eSMatthew G. Knepley /*@C
8523ece3a9fcSMatthew 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.
8524ece3a9fcSMatthew G. Knepley 
852520f4b53cSBarry Smith   Not Collective
8526ece3a9fcSMatthew G. Knepley 
8527ece3a9fcSMatthew G. Knepley   Input Parameters:
8528bb7acecfSBarry Smith + dm     - The `DM`
8529ece3a9fcSMatthew G. Knepley . time   - The time
8530bb7acecfSBarry Smith . label  - The `DMLabel` marking the portion of the domain boundary to output
8531ece3a9fcSMatthew G. Knepley . numIds - The number of label ids to use
8532ece3a9fcSMatthew G. Knepley . ids    - The label ids to use for marking
8533bb7acecfSBarry Smith . Nc     - The number of components to set in the output, or `PETSC_DETERMINE` for all components
853420f4b53cSBarry Smith . comps  - The components to set in the output, or `NULL` for all components
8535ece3a9fcSMatthew G. Knepley . localU - The input field vector
8536ece3a9fcSMatthew G. Knepley . funcs  - The functions to evaluate, one per field
8537ece3a9fcSMatthew G. Knepley - mode   - The insertion mode for values
8538ece3a9fcSMatthew G. Knepley 
8539ece3a9fcSMatthew G. Knepley   Output Parameter:
8540ece3a9fcSMatthew G. Knepley . localX - The output vector
8541ece3a9fcSMatthew G. Knepley 
854220f4b53cSBarry Smith   Calling sequence of `funcs`:
8543ece3a9fcSMatthew G. Knepley + dim          - The spatial dimension
8544ece3a9fcSMatthew G. Knepley . Nf           - The number of input fields
8545ece3a9fcSMatthew G. Knepley . NfAux        - The number of input auxiliary fields
8546ece3a9fcSMatthew G. Knepley . uOff         - The offset of each field in u[]
8547ece3a9fcSMatthew G. Knepley . uOff_x       - The offset of each field in u_x[]
8548ece3a9fcSMatthew G. Knepley . u            - The field values at this point in space
8549ece3a9fcSMatthew G. Knepley . u_t          - The field time derivative at this point in space (or NULL)
8550ece3a9fcSMatthew G. Knepley . u_x          - The field derivatives at this point in space
8551ece3a9fcSMatthew G. Knepley . aOff         - The offset of each auxiliary field in u[]
8552ece3a9fcSMatthew G. Knepley . aOff_x       - The offset of each auxiliary field in u_x[]
8553ece3a9fcSMatthew G. Knepley . a            - The auxiliary field values at this point in space
8554ece3a9fcSMatthew G. Knepley . a_t          - The auxiliary field time derivative at this point in space (or NULL)
8555ece3a9fcSMatthew G. Knepley . a_x          - The auxiliary field derivatives at this point in space
8556ece3a9fcSMatthew G. Knepley . t            - The current time
8557ece3a9fcSMatthew G. Knepley . x            - The coordinates of this point
8558ece3a9fcSMatthew G. Knepley . n            - The face normal
8559ece3a9fcSMatthew G. Knepley . numConstants - The number of constants
8560ece3a9fcSMatthew G. Knepley . constants    - The value of each constant
8561ece3a9fcSMatthew G. Knepley - f            - The value of the function at this point in space
8562ece3a9fcSMatthew G. Knepley 
856373ff1848SBarry Smith   Level: intermediate
856473ff1848SBarry Smith 
8565ece3a9fcSMatthew G. Knepley   Note:
8566bb7acecfSBarry 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.
8567bb7acecfSBarry 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
8568bb7acecfSBarry 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
8569ece3a9fcSMatthew 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.
8570ece3a9fcSMatthew G. Knepley 
8571bb7acecfSBarry Smith   Developer Notes:
8572bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8573bb7acecfSBarry Smith 
8574bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8575bb7acecfSBarry Smith 
85761cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8577ece3a9fcSMatthew G. Knepley @*/
8578a4e35b19SJacob 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)
8579d71ae5a4SJacob Faibussowitsch {
8580ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8581ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8582064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8583064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
8584fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, projectbdfieldlabellocal, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);
85853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8586ece3a9fcSMatthew G. Knepley }
8587ece3a9fcSMatthew G. Knepley 
8588ece3a9fcSMatthew G. Knepley /*@C
85892716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85902716604bSToby Isaac 
859120f4b53cSBarry Smith   Collective
8592bb7acecfSBarry Smith 
85932716604bSToby Isaac   Input Parameters:
8594bb7acecfSBarry Smith + dm    - The `DM`
85950709b2feSToby Isaac . time  - The time
85962716604bSToby Isaac . funcs - The functions to evaluate for each field component
85972716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8598574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
85992716604bSToby Isaac 
86002716604bSToby Isaac   Output Parameter:
86012716604bSToby Isaac . diff - The diff ||u - u_h||_2
86022716604bSToby Isaac 
86032716604bSToby Isaac   Level: developer
86042716604bSToby Isaac 
8605bb7acecfSBarry Smith   Developer Notes:
8606bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8607bb7acecfSBarry Smith 
8608bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8609bb7acecfSBarry Smith 
86101cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
86112716604bSToby Isaac @*/
8612d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8613d71ae5a4SJacob Faibussowitsch {
86142716604bSToby Isaac   PetscFunctionBegin;
86152716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8616b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8617fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2diff, time, funcs, ctxs, X, diff);
86183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86192716604bSToby Isaac }
8620b698f381SToby Isaac 
8621b698f381SToby Isaac /*@C
8622b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8623b698f381SToby Isaac 
862420f4b53cSBarry Smith   Collective
8625d083f849SBarry Smith 
8626b698f381SToby Isaac   Input Parameters:
8627bb7acecfSBarry Smith + dm    - The `DM`
8628a4e35b19SJacob Faibussowitsch . time  - The time
8629b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8630b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8631574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8632b698f381SToby Isaac - n     - The vector to project along
8633b698f381SToby Isaac 
8634b698f381SToby Isaac   Output Parameter:
8635b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8636b698f381SToby Isaac 
8637b698f381SToby Isaac   Level: developer
8638b698f381SToby Isaac 
8639bb7acecfSBarry Smith   Developer Notes:
8640bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8641bb7acecfSBarry Smith 
8642bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8643bb7acecfSBarry Smith 
86441cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8645b698f381SToby Isaac @*/
8646d71ae5a4SJacob 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)
8647d71ae5a4SJacob Faibussowitsch {
8648b698f381SToby Isaac   PetscFunctionBegin;
8649b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8650b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8651fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2gradientdiff, time, funcs, ctxs, X, n, diff);
86523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8653b698f381SToby Isaac }
8654b698f381SToby Isaac 
86552a16baeaSToby Isaac /*@C
86562a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86572a16baeaSToby Isaac 
865820f4b53cSBarry Smith   Collective
8659d083f849SBarry Smith 
86602a16baeaSToby Isaac   Input Parameters:
8661bb7acecfSBarry Smith + dm    - The `DM`
86622a16baeaSToby Isaac . time  - The time
86632a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86642a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8665574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86662a16baeaSToby Isaac 
86672a16baeaSToby Isaac   Output Parameter:
86682a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86692a16baeaSToby Isaac 
86702a16baeaSToby Isaac   Level: developer
86712a16baeaSToby Isaac 
8672bb7acecfSBarry Smith   Developer Notes:
8673bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8674bb7acecfSBarry Smith 
8675bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8676bb7acecfSBarry Smith 
867742747ad1SJacob Faibussowitsch .seealso: [](ch_dmbase), `DM`, `DMProjectFunction()`, `DMComputeL2GradientDiff()`
86782a16baeaSToby Isaac @*/
8679d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8680d71ae5a4SJacob Faibussowitsch {
86812a16baeaSToby Isaac   PetscFunctionBegin;
86822a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
86832a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
8684fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, computel2fielddiff, time, funcs, ctxs, X, diff);
86853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86862a16baeaSToby Isaac }
86872a16baeaSToby Isaac 
8688df0b854cSToby Isaac /*@C
8689bb7acecfSBarry Smith   DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8690502a2867SDave May 
8691502a2867SDave May   Not Collective
8692502a2867SDave May 
8693502a2867SDave May   Input Parameter:
8694bb7acecfSBarry Smith . dm - The `DM`
8695502a2867SDave May 
86960a19bb7dSprj-   Output Parameters:
86970a19bb7dSprj- + nranks - the number of neighbours
86980a19bb7dSprj- - ranks  - the neighbors ranks
8699502a2867SDave May 
87009bdbcad8SBarry Smith   Level: beginner
87019bdbcad8SBarry Smith 
8702bb7acecfSBarry Smith   Note:
8703bb7acecfSBarry Smith   Do not free the array, it is freed when the `DM` is destroyed.
8704502a2867SDave May 
87051cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8706502a2867SDave May @*/
8707d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8708d71ae5a4SJacob Faibussowitsch {
8709502a2867SDave May   PetscFunctionBegin;
8710502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8711fa1e479aSStefano Zampini   PetscUseTypeMethod(dm, getneighbors, nranks, ranks);
87123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8713502a2867SDave May }
8714502a2867SDave May 
8715531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8716531c7667SBarry Smith 
8717531c7667SBarry Smith /*
8718531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
87192b6f951bSStefano Zampini     This must be a different function because it requires DM which is not defined in the Mat library
8720531c7667SBarry Smith */
872166976f2fSJacob Faibussowitsch static PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8722d71ae5a4SJacob Faibussowitsch {
8723531c7667SBarry Smith   PetscFunctionBegin;
8724531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8725531c7667SBarry Smith     Vec x1local;
8726531c7667SBarry Smith     DM  dm;
87279566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87287a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
87299566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
87309566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
87319566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8732531c7667SBarry Smith     x1 = x1local;
8733531c7667SBarry Smith   }
87349566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8735531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8736531c7667SBarry Smith     DM dm;
87379566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
87389566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8739531c7667SBarry Smith   }
87403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8741531c7667SBarry Smith }
8742531c7667SBarry Smith 
8743531c7667SBarry Smith /*@
8744bb7acecfSBarry Smith   MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8745531c7667SBarry Smith 
8746a4e35b19SJacob Faibussowitsch   Input Parameters:
8747a4e35b19SJacob Faibussowitsch + coloring   - The matrix to get the `DM` from
8748a4e35b19SJacob Faibussowitsch - fdcoloring - the `MatFDColoring` object
8749531c7667SBarry Smith 
87509bdbcad8SBarry Smith   Level: advanced
87519bdbcad8SBarry Smith 
875273ff1848SBarry Smith   Developer Note:
875373ff1848SBarry Smith   This routine exists because the PETSc `Mat` library does not know about the `DM` objects
8754531c7667SBarry Smith 
87551cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8756531c7667SBarry Smith @*/
8757d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8758d71ae5a4SJacob Faibussowitsch {
8759531c7667SBarry Smith   PetscFunctionBegin;
8760531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
87613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8762531c7667SBarry Smith }
87638320bc6fSPatrick Sanan 
87648320bc6fSPatrick Sanan /*@
8765bb7acecfSBarry Smith   DMGetCompatibility - determine if two `DM`s are compatible
87668320bc6fSPatrick Sanan 
87678320bc6fSPatrick Sanan   Collective
87688320bc6fSPatrick Sanan 
87698320bc6fSPatrick Sanan   Input Parameters:
8770bb7acecfSBarry Smith + dm1 - the first `DM`
8771bb7acecfSBarry Smith - dm2 - the second `DM`
87728320bc6fSPatrick Sanan 
87738320bc6fSPatrick Sanan   Output Parameters:
8774bb7acecfSBarry Smith + compatible - whether or not the two `DM`s are compatible
8775bb7acecfSBarry Smith - set        - whether or not the compatible value was actually determined and set
87768320bc6fSPatrick Sanan 
877720f4b53cSBarry Smith   Level: advanced
877820f4b53cSBarry Smith 
87798320bc6fSPatrick Sanan   Notes:
8780bb7acecfSBarry Smith   Two `DM`s are deemed compatible if they represent the same parallel decomposition
87813d862458SPatrick Sanan   of the same topology. This implies that the section (field data) on one
87828320bc6fSPatrick Sanan   "makes sense" with respect to the topology and parallel decomposition of the other.
8783bb7acecfSBarry Smith   Loosely speaking, compatible `DM`s represent the same domain and parallel
87843d862458SPatrick Sanan   decomposition, but hold different data.
87858320bc6fSPatrick Sanan 
87868320bc6fSPatrick Sanan   Typically, one would confirm compatibility if intending to simultaneously iterate
8787bb7acecfSBarry Smith   over a pair of vectors obtained from different `DM`s.
87888320bc6fSPatrick Sanan 
8789bb7acecfSBarry Smith   For example, two `DMDA` objects are compatible if they have the same local
87908320bc6fSPatrick Sanan   and global sizes and the same stencil width. They can have different numbers
87918320bc6fSPatrick Sanan   of degrees of freedom per node. Thus, one could use the node numbering from
8792bb7acecfSBarry Smith   either `DM` in bounds for a loop over vectors derived from either `DM`.
87938320bc6fSPatrick Sanan 
8794bb7acecfSBarry Smith   Consider the operation of summing data living on a 2-dof `DMDA` to data living
8795bb7acecfSBarry Smith   on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
87968320bc6fSPatrick Sanan .vb
87978320bc6fSPatrick Sanan   ...
87989566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
87998320bc6fSPatrick Sanan   if (set && compatible)  {
88009566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
88019566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
88029566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
88038320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88048320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88058320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88068320bc6fSPatrick Sanan       }
88078320bc6fSPatrick Sanan     }
88089566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
88099566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
88108320bc6fSPatrick Sanan   } else {
88118320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88128320bc6fSPatrick Sanan   }
88138320bc6fSPatrick Sanan   ...
88148320bc6fSPatrick Sanan .ve
88158320bc6fSPatrick Sanan 
8816bb7acecfSBarry Smith   Checking compatibility might be expensive for a given implementation of `DM`,
88178320bc6fSPatrick Sanan   or might be impossible to unambiguously confirm or deny. For this reason,
88188320bc6fSPatrick Sanan   this function may decline to determine compatibility, and hence users should
88198320bc6fSPatrick Sanan   always check the "set" output parameter.
88208320bc6fSPatrick Sanan 
8821bb7acecfSBarry Smith   A `DM` is always compatible with itself.
88228320bc6fSPatrick Sanan 
8823bb7acecfSBarry Smith   In the current implementation, `DM`s which live on "unequal" communicators
88248320bc6fSPatrick Sanan   (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88258320bc6fSPatrick Sanan   incompatible.
88268320bc6fSPatrick Sanan 
88278320bc6fSPatrick Sanan   This function is labeled "Collective," as information about all subdomains
8828bb7acecfSBarry Smith   is required on each rank. However, in `DM` implementations which store all this
88298320bc6fSPatrick Sanan   information locally, this function may be merely "Logically Collective".
88308320bc6fSPatrick Sanan 
883173ff1848SBarry Smith   Developer Note:
8832bb7acecfSBarry Smith   Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
88333d862458SPatrick Sanan   iff B is compatible with A. Thus, this function checks the implementations
8834a5bc1bf3SBarry Smith   of both dm and dmc (if they are of different types), attempting to determine
8835bb7acecfSBarry Smith   compatibility. It is left to `DM` implementers to ensure that symmetry is
88368320bc6fSPatrick Sanan   preserved. The simplest way to do this is, when implementing type-specific
88373d862458SPatrick Sanan   logic for this function, is to check for existing logic in the implementation
8838bb7acecfSBarry Smith   of other `DM` types and let *set = PETSC_FALSE if found.
88398320bc6fSPatrick Sanan 
88401cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
88418320bc6fSPatrick Sanan @*/
8842d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8843d71ae5a4SJacob Faibussowitsch {
88448320bc6fSPatrick Sanan   PetscMPIInt compareResult;
88458320bc6fSPatrick Sanan   DMType      type, type2;
88468320bc6fSPatrick Sanan   PetscBool   sameType;
88478320bc6fSPatrick Sanan 
88488320bc6fSPatrick Sanan   PetscFunctionBegin;
8849a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
88508320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
88518320bc6fSPatrick Sanan 
88528320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8853a5bc1bf3SBarry Smith   if (dm1 == dm2) {
88548320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88558320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
88563ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88578320bc6fSPatrick Sanan   }
88588320bc6fSPatrick Sanan 
88598320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
88608320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
88618320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
88628320bc6fSPatrick Sanan      determined by the implementation-specific logic */
88639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
88648320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
88658320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
88668320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
88673ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
88688320bc6fSPatrick Sanan   }
88698320bc6fSPatrick Sanan 
88708320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8871a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8872dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
88733ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
88748320bc6fSPatrick Sanan   }
88758320bc6fSPatrick Sanan 
8876a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
88778320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
88789566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
88799566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
88809566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
88818320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8882dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
88838320bc6fSPatrick Sanan   } else {
88848320bc6fSPatrick Sanan     *set = PETSC_FALSE;
88858320bc6fSPatrick Sanan   }
88863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88878320bc6fSPatrick Sanan }
8888c0f0dcc3SMatthew G. Knepley 
8889c0f0dcc3SMatthew G. Knepley /*@C
8890bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8891c0f0dcc3SMatthew G. Knepley 
889220f4b53cSBarry Smith   Logically Collective
8893c0f0dcc3SMatthew G. Knepley 
8894c0f0dcc3SMatthew G. Knepley   Input Parameters:
889560225df5SJacob Faibussowitsch + dm             - the `DM`
8896c0f0dcc3SMatthew G. Knepley . f              - the monitor function
889720f4b53cSBarry Smith . mctx           - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
8898*49abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for the calling sequence
8899c0f0dcc3SMatthew G. Knepley 
890020f4b53cSBarry Smith   Options Database Key:
890160225df5SJacob Faibussowitsch . -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8902c0f0dcc3SMatthew G. Knepley                        does not cancel those set via the options database.
8903c0f0dcc3SMatthew G. Knepley 
89049bdbcad8SBarry Smith   Level: intermediate
89059bdbcad8SBarry Smith 
8906bb7acecfSBarry Smith   Note:
8907c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8908bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8909c0f0dcc3SMatthew G. Knepley   order in which they were set.
8910c0f0dcc3SMatthew G. Knepley 
891173ff1848SBarry Smith   Fortran Note:
8912bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8913bb7acecfSBarry Smith 
891473ff1848SBarry Smith   Developer Note:
8915bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8916c0f0dcc3SMatthew G. Knepley 
8917*49abdd8aSBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`, `PetscCtxDestroyFn`
8918c0f0dcc3SMatthew G. Knepley @*/
8919*49abdd8aSBarry Smith PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscCtxDestroyFn *monitordestroy)
8920d71ae5a4SJacob Faibussowitsch {
8921c0f0dcc3SMatthew G. Knepley   PetscInt m;
8922c0f0dcc3SMatthew G. Knepley 
8923c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8924c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8925c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8926c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8927c0f0dcc3SMatthew G. Knepley 
89289566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f, mctx, monitordestroy, (PetscErrorCode (*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
89293ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8930c0f0dcc3SMatthew G. Knepley   }
89317a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8932c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8933c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8934c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
89353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8936c0f0dcc3SMatthew G. Knepley }
8937c0f0dcc3SMatthew G. Knepley 
8938c0f0dcc3SMatthew G. Knepley /*@
8939bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8940c0f0dcc3SMatthew G. Knepley 
894120f4b53cSBarry Smith   Logically Collective
8942c0f0dcc3SMatthew G. Knepley 
8943c0f0dcc3SMatthew G. Knepley   Input Parameter:
8944c0f0dcc3SMatthew G. Knepley . dm - the DM
8945c0f0dcc3SMatthew G. Knepley 
8946c0f0dcc3SMatthew G. Knepley   Options Database Key:
8947c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8948bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8949c0f0dcc3SMatthew G. Knepley   set via the options database
8950c0f0dcc3SMatthew G. Knepley 
89519bdbcad8SBarry Smith   Level: intermediate
89529bdbcad8SBarry Smith 
8953bb7acecfSBarry Smith   Note:
8954bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8955c0f0dcc3SMatthew G. Knepley 
89561cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8957c0f0dcc3SMatthew G. Knepley @*/
8958d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8959d71ae5a4SJacob Faibussowitsch {
8960c0f0dcc3SMatthew G. Knepley   PetscInt m;
8961c0f0dcc3SMatthew G. Knepley 
8962c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8963c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8964c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
89659566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8966c0f0dcc3SMatthew G. Knepley   }
8967c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
89683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8969c0f0dcc3SMatthew G. Knepley }
8970c0f0dcc3SMatthew G. Knepley 
8971c0f0dcc3SMatthew G. Knepley /*@C
8972c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8973c0f0dcc3SMatthew G. Knepley 
897420f4b53cSBarry Smith   Collective
8975c0f0dcc3SMatthew G. Knepley 
8976c0f0dcc3SMatthew G. Knepley   Input Parameters:
8977bb7acecfSBarry Smith + dm           - `DM` object you wish to monitor
8978c0f0dcc3SMatthew G. Knepley . name         - the monitor type one is seeking
8979c0f0dcc3SMatthew G. Knepley . help         - message indicating what monitoring is done
8980c0f0dcc3SMatthew G. Knepley . manual       - manual page for the monitor
8981*49abdd8aSBarry Smith . monitor      - the monitor function, this must use a `PetscViewerFormat` as its context
8982bb7acecfSBarry 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
8983c0f0dcc3SMatthew G. Knepley 
8984c0f0dcc3SMatthew G. Knepley   Output Parameter:
8985c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8986c0f0dcc3SMatthew G. Knepley 
8987c0f0dcc3SMatthew G. Knepley   Level: developer
8988c0f0dcc3SMatthew G. Knepley 
8989648c30bcSBarry Smith .seealso: [](ch_dmbase), `DM`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8990db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
899160225df5SJacob Faibussowitsch           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
8992db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8993c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8994db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8995bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8996c0f0dcc3SMatthew G. Knepley @*/
8997d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8998d71ae5a4SJacob Faibussowitsch {
8999c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9000c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9001c0f0dcc3SMatthew G. Knepley 
9002c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9003c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9004648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
9005c0f0dcc3SMatthew G. Knepley   if (*flg) {
9006c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9007c0f0dcc3SMatthew G. Knepley 
90089566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
9009648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewer));
90109566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
9011*49abdd8aSBarry Smith     PetscCall(DMMonitorSet(dm, (PetscErrorCode (*)(DM, void *))monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
9012c0f0dcc3SMatthew G. Knepley   }
90133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9014c0f0dcc3SMatthew G. Knepley }
9015c0f0dcc3SMatthew G. Knepley 
9016c0f0dcc3SMatthew G. Knepley /*@
9017c0f0dcc3SMatthew G. Knepley   DMMonitor - runs the user provided monitor routines, if they exist
9018c0f0dcc3SMatthew G. Knepley 
901920f4b53cSBarry Smith   Collective
9020c0f0dcc3SMatthew G. Knepley 
90212fe279fdSBarry Smith   Input Parameter:
9022bb7acecfSBarry Smith . dm - The `DM`
9023c0f0dcc3SMatthew G. Knepley 
9024c0f0dcc3SMatthew G. Knepley   Level: developer
9025c0f0dcc3SMatthew G. Knepley 
902673ff1848SBarry Smith   Developer Note:
9027a4e35b19SJacob Faibussowitsch   Note should indicate when during the life of the `DM` the monitor is run. It appears to be
9028a4e35b19SJacob Faibussowitsch   related to the discretization process seems rather specialized since some `DM` have no
9029a4e35b19SJacob Faibussowitsch   concept of discretization.
9030bb7acecfSBarry Smith 
90311cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMMonitorSetFromOptions()`
9032c0f0dcc3SMatthew G. Knepley @*/
9033d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
9034d71ae5a4SJacob Faibussowitsch {
9035c0f0dcc3SMatthew G. Knepley   PetscInt m;
9036c0f0dcc3SMatthew G. Knepley 
9037c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
90383ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
9039c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
904048a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
90413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9042c0f0dcc3SMatthew G. Knepley }
90432e4af2aeSMatthew G. Knepley 
90442e4af2aeSMatthew G. Knepley /*@
9045bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
90462e4af2aeSMatthew G. Knepley 
904720f4b53cSBarry Smith   Collective
90482e4af2aeSMatthew G. Knepley 
90492e4af2aeSMatthew G. Knepley   Input Parameters:
9050bb7acecfSBarry Smith + dm  - The `DM`
90516b867d5aSJose E. Roman - sol - The solution vector
90522e4af2aeSMatthew G. Knepley 
90536b867d5aSJose E. Roman   Input/Output Parameter:
905420f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
90556b867d5aSJose E. Roman            contains the error in each field
90566b867d5aSJose E. Roman 
90576b867d5aSJose E. Roman   Output Parameter:
905820f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
905920f4b53cSBarry Smith 
906020f4b53cSBarry Smith   Level: developer
90612e4af2aeSMatthew G. Knepley 
9062bb7acecfSBarry Smith   Note:
9063bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
90642e4af2aeSMatthew G. Knepley 
90651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
90662e4af2aeSMatthew G. Knepley @*/
9067d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
9068d71ae5a4SJacob Faibussowitsch {
90692e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
90702e4af2aeSMatthew G. Knepley   void    **ctxs;
90712e4af2aeSMatthew G. Knepley   PetscReal time;
90722e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
90732e4af2aeSMatthew G. Knepley 
90742e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
90759566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
90769566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
90779566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
90782e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
90792e4af2aeSMatthew G. Knepley     PetscDS         ds;
90802e4af2aeSMatthew G. Knepley     DMLabel         label;
90812e4af2aeSMatthew G. Knepley     IS              fieldIS;
90822e4af2aeSMatthew G. Knepley     const PetscInt *fields;
90832e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
90842e4af2aeSMatthew G. Knepley 
908507218a29SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds, NULL));
90869566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
90879566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
90882e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
90892e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
90909566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
90912e4af2aeSMatthew G. Knepley     }
90929566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
90932e4af2aeSMatthew G. Knepley   }
9094ad540459SPierre 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);
90959566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
90969566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
90972e4af2aeSMatthew G. Knepley   if (errorVec) {
90982e4af2aeSMatthew G. Knepley     DM             edm;
90992e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
91002e4af2aeSMatthew G. Knepley     PetscBool      simplex;
91012e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
91022e4af2aeSMatthew G. Knepley 
91039566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
91049566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
91059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
91069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
91072e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
91089566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
91092e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
91102e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
91112e4af2aeSMatthew G. Knepley       PetscQuadrature q;
91122e4af2aeSMatthew G. Knepley       const char     *name;
91132e4af2aeSMatthew G. Knepley 
91149566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
91159566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
91169566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
91179566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
91189566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
91199566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
91209566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
91219566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
91222e4af2aeSMatthew G. Knepley     }
91239566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
91242e4af2aeSMatthew G. Knepley 
91259566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
91269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
91279566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
91289566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
91292e4af2aeSMatthew G. Knepley   }
91309566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
91313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91322e4af2aeSMatthew G. Knepley }
91339a2a23afSMatthew G. Knepley 
91349a2a23afSMatthew G. Knepley /*@
9135bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
91369a2a23afSMatthew G. Knepley 
913720f4b53cSBarry Smith   Not Collective
91389a2a23afSMatthew G. Knepley 
91399a2a23afSMatthew G. Knepley   Input Parameter:
9140bb7acecfSBarry Smith . dm - The `DM`
91419a2a23afSMatthew G. Knepley 
91429a2a23afSMatthew G. Knepley   Output Parameter:
9143a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
91449a2a23afSMatthew G. Knepley 
91459a2a23afSMatthew G. Knepley   Level: advanced
91469a2a23afSMatthew G. Knepley 
9147e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`
91489a2a23afSMatthew G. Knepley @*/
9149d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
9150d71ae5a4SJacob Faibussowitsch {
91519a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91529a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91539566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
91543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91559a2a23afSMatthew G. Knepley }
91569a2a23afSMatthew G. Knepley 
91579a2a23afSMatthew G. Knepley /*@
9158ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
91599a2a23afSMatthew G. Knepley 
916020f4b53cSBarry Smith   Not Collective
91619a2a23afSMatthew G. Knepley 
91629a2a23afSMatthew G. Knepley   Input Parameters:
9163bb7acecfSBarry Smith + dm    - The `DM`
9164bb7acecfSBarry Smith . label - The `DMLabel`
9165ac17215fSMatthew G. Knepley . value - The label value indicating the region
9166ac17215fSMatthew G. Knepley - part  - The equation part, or 0 if unused
91679a2a23afSMatthew G. Knepley 
91689a2a23afSMatthew G. Knepley   Output Parameter:
9169bb7acecfSBarry Smith . aux - The `Vec` holding auxiliary field data
91709a2a23afSMatthew G. Knepley 
91719bdbcad8SBarry Smith   Level: advanced
91729bdbcad8SBarry Smith 
9173bb7acecfSBarry Smith   Note:
9174bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
917504c51a94SMatthew G. Knepley 
9176e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
91779a2a23afSMatthew G. Knepley @*/
9178d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
9179d71ae5a4SJacob Faibussowitsch {
9180ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
918104c51a94SMatthew G. Knepley   PetscBool       has;
91829a2a23afSMatthew G. Knepley 
91839a2a23afSMatthew G. Knepley   PetscFunctionBegin;
91849a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
91859a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
91869a2a23afSMatthew G. Knepley   key.label = label;
91879a2a23afSMatthew G. Knepley   key.value = value;
9188ac17215fSMatthew G. Knepley   key.part  = part;
91899566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
91909566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
91919566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
91923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
91939a2a23afSMatthew G. Knepley }
91949a2a23afSMatthew G. Knepley 
91959a2a23afSMatthew G. Knepley /*@
9196bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
91979a2a23afSMatthew G. Knepley 
919820f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
91999a2a23afSMatthew G. Knepley 
92009a2a23afSMatthew G. Knepley   Input Parameters:
9201bb7acecfSBarry Smith + dm    - The `DM`
9202bb7acecfSBarry Smith . label - The `DMLabel`
92039a2a23afSMatthew G. Knepley . value - The label value indicating the region
9204ac17215fSMatthew G. Knepley . part  - The equation part, or 0 if unused
9205bb7acecfSBarry Smith - aux   - The `Vec` holding auxiliary field data
92069a2a23afSMatthew G. Knepley 
92079a2a23afSMatthew G. Knepley   Level: advanced
92089a2a23afSMatthew G. Knepley 
9209e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
92109a2a23afSMatthew G. Knepley @*/
9211d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9212d71ae5a4SJacob Faibussowitsch {
92139a2a23afSMatthew G. Knepley   Vec             old;
92149a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
92159a2a23afSMatthew G. Knepley 
92169a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92179a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92189a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
92199a2a23afSMatthew G. Knepley   key.label = label;
92209a2a23afSMatthew G. Knepley   key.value = value;
9221ac17215fSMatthew G. Knepley   key.part  = part;
92229566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
92239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
92249566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
92259566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
9226d705d0eaSStefano Zampini   PetscCall(VecDestroy(&old));
92273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92289a2a23afSMatthew G. Knepley }
92299a2a23afSMatthew G. Knepley 
9230cc4c1da9SBarry Smith /*@
9231bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
92329a2a23afSMatthew G. Knepley 
923320f4b53cSBarry Smith   Not Collective
92349a2a23afSMatthew G. Knepley 
92359a2a23afSMatthew G. Knepley   Input Parameter:
9236bb7acecfSBarry Smith . dm - The `DM`
92379a2a23afSMatthew G. Knepley 
92389a2a23afSMatthew G. Knepley   Output Parameters:
9239bb7acecfSBarry Smith + labels - The `DMLabel`s for each `Vec`
9240bb7acecfSBarry Smith . values - The label values for each `Vec`
9241bb7acecfSBarry Smith - parts  - The equation parts for each `Vec`
92429a2a23afSMatthew G. Knepley 
92439bdbcad8SBarry Smith   Level: advanced
92449bdbcad8SBarry Smith 
9245bb7acecfSBarry Smith   Note:
9246bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
92479a2a23afSMatthew G. Knepley 
9248e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`, `DMCopyAuxiliaryVec()`
92499a2a23afSMatthew G. Knepley @*/
9250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9251d71ae5a4SJacob Faibussowitsch {
92529a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
92539a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
92549a2a23afSMatthew G. Knepley 
92559a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92569a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
92574f572ea9SToby Isaac   PetscAssertPointer(labels, 2);
92584f572ea9SToby Isaac   PetscAssertPointer(values, 3);
92594f572ea9SToby Isaac   PetscAssertPointer(parts, 4);
92609566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
92619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
92629566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
92639371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
92649371c9d4SSatish Balay     labels[i] = keys[i].label;
92659371c9d4SSatish Balay     values[i] = keys[i].value;
92669371c9d4SSatish Balay     parts[i]  = keys[i].part;
92679371c9d4SSatish Balay   }
92689566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
92693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92709a2a23afSMatthew G. Knepley }
92719a2a23afSMatthew G. Knepley 
92729a2a23afSMatthew G. Knepley /*@
9273bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
92749a2a23afSMatthew G. Knepley 
927520f4b53cSBarry Smith   Not Collective
92769a2a23afSMatthew G. Knepley 
92779a2a23afSMatthew G. Knepley   Input Parameter:
9278bb7acecfSBarry Smith . dm - The `DM`
92799a2a23afSMatthew G. Knepley 
92809a2a23afSMatthew G. Knepley   Output Parameter:
9281bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
92829a2a23afSMatthew G. Knepley 
92839a2a23afSMatthew G. Knepley   Level: advanced
92849a2a23afSMatthew G. Knepley 
9285bb7acecfSBarry Smith   Note:
9286bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9287bb7acecfSBarry Smith 
9288e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMClearAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
92899a2a23afSMatthew G. Knepley @*/
9290d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9291d71ae5a4SJacob Faibussowitsch {
92929a2a23afSMatthew G. Knepley   PetscFunctionBegin;
92939a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9294d705d0eaSStefano Zampini   PetscValidHeaderSpecific(dmNew, DM_CLASSID, 2);
9295d705d0eaSStefano Zampini   if (dm == dmNew) PetscFunctionReturn(PETSC_SUCCESS);
9296e4d5475eSStefano Zampini   PetscCall(DMClearAuxiliaryVec(dmNew));
9297e4d5475eSStefano Zampini 
9298e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
92999566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
9300d705d0eaSStefano Zampini   {
9301d705d0eaSStefano Zampini     Vec     *auxData;
9302d705d0eaSStefano Zampini     PetscInt n, i, off = 0;
9303d705d0eaSStefano Zampini 
9304d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetSize(dmNew->auxData, &n));
9305d705d0eaSStefano Zampini     PetscCall(PetscMalloc1(n, &auxData));
9306d705d0eaSStefano Zampini     PetscCall(PetscHMapAuxGetVals(dmNew->auxData, &off, auxData));
9307d705d0eaSStefano Zampini     for (i = 0; i < n; ++i) PetscCall(PetscObjectReference((PetscObject)auxData[i]));
9308d705d0eaSStefano Zampini     PetscCall(PetscFree(auxData));
9309e4d5475eSStefano Zampini   }
9310e4d5475eSStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
9311e4d5475eSStefano Zampini }
9312e4d5475eSStefano Zampini 
9313e4d5475eSStefano Zampini /*@
9314e4d5475eSStefano Zampini   DMClearAuxiliaryVec - Destroys the auxiliary vector information and creates a new empty one
9315e4d5475eSStefano Zampini 
9316e4d5475eSStefano Zampini   Not Collective
9317e4d5475eSStefano Zampini 
9318e4d5475eSStefano Zampini   Input Parameter:
9319e4d5475eSStefano Zampini . dm - The `DM`
9320e4d5475eSStefano Zampini 
9321e4d5475eSStefano Zampini   Level: advanced
9322e4d5475eSStefano Zampini 
9323e4d5475eSStefano Zampini .seealso: [](ch_dmbase), `DM`, `DMCopyAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
9324e4d5475eSStefano Zampini @*/
9325e4d5475eSStefano Zampini PetscErrorCode DMClearAuxiliaryVec(DM dm)
9326e4d5475eSStefano Zampini {
9327e4d5475eSStefano Zampini   Vec     *auxData;
9328e4d5475eSStefano Zampini   PetscInt n, i, off = 0;
9329e4d5475eSStefano Zampini 
9330e4d5475eSStefano Zampini   PetscFunctionBegin;
9331e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetSize(dm->auxData, &n));
9332d705d0eaSStefano Zampini   PetscCall(PetscMalloc1(n, &auxData));
9333e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxGetVals(dm->auxData, &off, auxData));
9334d705d0eaSStefano Zampini   for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
9335d705d0eaSStefano Zampini   PetscCall(PetscFree(auxData));
9336e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxDestroy(&dm->auxData));
9337e4d5475eSStefano Zampini   PetscCall(PetscHMapAuxCreate(&dm->auxData));
93383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
93399a2a23afSMatthew G. Knepley }
9340b5a892a1SMatthew G. Knepley 
9341cc4c1da9SBarry Smith /*@
9342bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9343b5a892a1SMatthew G. Knepley 
934420f4b53cSBarry Smith   Not Collective
9345b5a892a1SMatthew G. Knepley 
9346b5a892a1SMatthew G. Knepley   Input Parameters:
9347bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9348b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9349b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9350b5a892a1SMatthew G. Knepley 
9351b5a892a1SMatthew G. Knepley   Output Parameters:
9352bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9353b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9354b5a892a1SMatthew G. Knepley 
9355b5a892a1SMatthew G. Knepley   Level: advanced
9356b5a892a1SMatthew G. Knepley 
9357bb7acecfSBarry Smith   Note:
9358bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9359bb7acecfSBarry Smith 
936085036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9361bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9362bb7acecfSBarry Smith 
9363bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9364bb7acecfSBarry Smith 
93651cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9366b5a892a1SMatthew G. Knepley @*/
9367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9368d71ae5a4SJacob Faibussowitsch {
9369b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
937085036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9371b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9372b5a892a1SMatthew G. Knepley 
9373b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
93749371c9d4SSatish Balay   if (!nO) {
93759371c9d4SSatish Balay     *ornt  = 0;
93769371c9d4SSatish Balay     *found = PETSC_TRUE;
93773ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
93789371c9d4SSatish Balay   }
9379b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
938085036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, o);
9381b5a892a1SMatthew G. Knepley 
93829371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
93839371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
93849371c9d4SSatish Balay     if (c == cS) {
93859371c9d4SSatish Balay       *ornt = o;
93869371c9d4SSatish Balay       break;
93879371c9d4SSatish Balay     }
9388b5a892a1SMatthew G. Knepley   }
9389b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
93903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9391b5a892a1SMatthew G. Knepley }
9392b5a892a1SMatthew G. Knepley 
9393cc4c1da9SBarry Smith /*@
9394bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9395b5a892a1SMatthew G. Knepley 
939620f4b53cSBarry Smith   Not Collective
9397b5a892a1SMatthew G. Knepley 
9398b5a892a1SMatthew G. Knepley   Input Parameters:
9399bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9400b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9401b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9402b5a892a1SMatthew G. Knepley 
94032fe279fdSBarry Smith   Output Parameter:
9404bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9405b5a892a1SMatthew G. Knepley 
9406b5a892a1SMatthew G. Knepley   Level: advanced
9407b5a892a1SMatthew G. Knepley 
9408bb7acecfSBarry Smith   Note:
9409bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9410bb7acecfSBarry Smith 
941173ff1848SBarry Smith   Developer Note:
9412bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9413bb7acecfSBarry Smith 
94141cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9415b5a892a1SMatthew G. Knepley @*/
9416d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9417d71ae5a4SJacob Faibussowitsch {
9418b5a892a1SMatthew G. Knepley   PetscBool found;
9419b5a892a1SMatthew G. Knepley 
9420b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94219566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
94227a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
94233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9424b5a892a1SMatthew G. Knepley }
9425b5a892a1SMatthew G. Knepley 
9426cc4c1da9SBarry Smith /*@
9427bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9428b5a892a1SMatthew G. Knepley 
942920f4b53cSBarry Smith   Not Collective
9430b5a892a1SMatthew G. Knepley 
9431b5a892a1SMatthew G. Knepley   Input Parameters:
9432bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9433b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9434b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9435b5a892a1SMatthew G. Knepley 
9436b5a892a1SMatthew G. Knepley   Output Parameters:
9437bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9438b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9439b5a892a1SMatthew G. Knepley 
9440b5a892a1SMatthew G. Knepley   Level: advanced
9441b5a892a1SMatthew G. Knepley 
944273ff1848SBarry Smith   Notes:
9443bb7acecfSBarry Smith   An arrangement is a vertex order
9444bb7acecfSBarry Smith 
944585036b15SMatthew G. Knepley   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangements(ct)`/2 to `DMPolytopeTypeGetNumArrangements(ct)`/2
9446bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9447bb7acecfSBarry Smith 
9448bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9449bb7acecfSBarry Smith 
945085036b15SMatthew G. Knepley .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangement()`
9451b5a892a1SMatthew G. Knepley @*/
9452d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9453d71ae5a4SJacob Faibussowitsch {
9454b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
945585036b15SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangements(ct) / 2;
9456b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9457b5a892a1SMatthew G. Knepley 
9458b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
94599371c9d4SSatish Balay   if (!nO) {
94609371c9d4SSatish Balay     *ornt  = 0;
94619371c9d4SSatish Balay     *found = PETSC_TRUE;
94623ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
94639371c9d4SSatish Balay   }
9464b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
946585036b15SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangement(ct, o);
9466b5a892a1SMatthew G. Knepley 
94679371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
94689371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
94699371c9d4SSatish Balay     if (c == cS) {
94709371c9d4SSatish Balay       *ornt = o;
94719371c9d4SSatish Balay       break;
94729371c9d4SSatish Balay     }
9473b5a892a1SMatthew G. Knepley   }
9474b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
94753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9476b5a892a1SMatthew G. Knepley }
9477b5a892a1SMatthew G. Knepley 
9478cc4c1da9SBarry Smith /*@
9479bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9480b5a892a1SMatthew G. Knepley 
948120f4b53cSBarry Smith   Not Collective
9482b5a892a1SMatthew G. Knepley 
9483b5a892a1SMatthew G. Knepley   Input Parameters:
9484bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9485b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9486b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9487b5a892a1SMatthew G. Knepley 
94882fe279fdSBarry Smith   Output Parameter:
9489bb7acecfSBarry Smith . ornt - The orientation (transformation) which will take the source arrangement to the target arrangement
9490b5a892a1SMatthew G. Knepley 
9491b5a892a1SMatthew G. Knepley   Level: advanced
9492b5a892a1SMatthew G. Knepley 
9493bb7acecfSBarry Smith   Note:
9494bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9495bb7acecfSBarry Smith 
949673ff1848SBarry Smith   Developer Note:
9497bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9498bb7acecfSBarry Smith 
94991cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9500b5a892a1SMatthew G. Knepley @*/
9501d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9502d71ae5a4SJacob Faibussowitsch {
9503b5a892a1SMatthew G. Knepley   PetscBool found;
9504b5a892a1SMatthew G. Knepley 
9505b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
95069566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
95077a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
95083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9509b5a892a1SMatthew G. Knepley }
9510012bc364SMatthew G. Knepley 
9511cc4c1da9SBarry Smith /*@
9512012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9513012bc364SMatthew G. Knepley 
951420f4b53cSBarry Smith   Not Collective
9515012bc364SMatthew G. Knepley 
9516012bc364SMatthew G. Knepley   Input Parameters:
9517bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9518012bc364SMatthew G. Knepley - point - Coordinates of the point
9519012bc364SMatthew G. Knepley 
95202fe279fdSBarry Smith   Output Parameter:
9521012bc364SMatthew G. Knepley . inside - Flag indicating whether the point is inside the reference cell of given type
9522012bc364SMatthew G. Knepley 
9523012bc364SMatthew G. Knepley   Level: advanced
9524012bc364SMatthew G. Knepley 
95251cc06b55SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMPolytopeType`, `DMLocatePoints()`
9526012bc364SMatthew G. Knepley @*/
9527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9528d71ae5a4SJacob Faibussowitsch {
9529012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9530012bc364SMatthew G. Knepley   PetscInt  d;
9531012bc364SMatthew G. Knepley 
9532012bc364SMatthew G. Knepley   PetscFunctionBegin;
9533012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9534012bc364SMatthew G. Knepley   switch (ct) {
9535012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9536012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9537012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
95389371c9d4SSatish Balay       if (point[d] < -1.0) {
95399371c9d4SSatish Balay         *inside = PETSC_FALSE;
95409371c9d4SSatish Balay         break;
95419371c9d4SSatish Balay       }
9542012bc364SMatthew G. Knepley       sum += point[d];
9543012bc364SMatthew G. Knepley     }
95449371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
95459371c9d4SSatish Balay       *inside = PETSC_FALSE;
95469371c9d4SSatish Balay       break;
95479371c9d4SSatish Balay     }
9548012bc364SMatthew G. Knepley     break;
9549012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9550012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9551012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
95529371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
95539371c9d4SSatish Balay         *inside = PETSC_FALSE;
9554012bc364SMatthew G. Knepley         break;
95559371c9d4SSatish Balay       }
95569371c9d4SSatish Balay     break;
9557d71ae5a4SJacob Faibussowitsch   default:
9558d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9559012bc364SMatthew G. Knepley   }
95603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9561012bc364SMatthew G. Knepley }
9562adc21957SMatthew G. Knepley 
9563adc21957SMatthew G. Knepley /*@
9564adc21957SMatthew G. Knepley   DMReorderSectionSetDefault - Set flag indicating whether the local section should be reordered by default
9565adc21957SMatthew G. Knepley 
9566adc21957SMatthew G. Knepley   Logically collective
9567adc21957SMatthew G. Knepley 
9568adc21957SMatthew G. Knepley   Input Parameters:
9569adc21957SMatthew G. Knepley + dm      - The DM
9570adc21957SMatthew G. Knepley - reorder - Flag for reordering
9571adc21957SMatthew G. Knepley 
9572adc21957SMatthew G. Knepley   Level: intermediate
9573adc21957SMatthew G. Knepley 
9574adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetDefault()`
9575adc21957SMatthew G. Knepley @*/
9576adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetDefault(DM dm, DMReorderDefaultFlag reorder)
9577adc21957SMatthew G. Knepley {
9578adc21957SMatthew G. Knepley   PetscFunctionBegin;
9579adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9580adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetDefault_C", (DM, DMReorderDefaultFlag), (dm, reorder));
9581adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9582adc21957SMatthew G. Knepley }
9583adc21957SMatthew G. Knepley 
9584adc21957SMatthew G. Knepley /*@
9585adc21957SMatthew G. Knepley   DMReorderSectionGetDefault - Get flag indicating whether the local section should be reordered by default
9586adc21957SMatthew G. Knepley 
9587adc21957SMatthew G. Knepley   Not collective
9588adc21957SMatthew G. Knepley 
9589adc21957SMatthew G. Knepley   Input Parameter:
9590adc21957SMatthew G. Knepley . dm - The DM
9591adc21957SMatthew G. Knepley 
9592adc21957SMatthew G. Knepley   Output Parameter:
9593adc21957SMatthew G. Knepley . reorder - Flag for reordering
9594adc21957SMatthew G. Knepley 
9595adc21957SMatthew G. Knepley   Level: intermediate
9596adc21957SMatthew G. Knepley 
9597adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`
9598adc21957SMatthew G. Knepley @*/
9599adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetDefault(DM dm, DMReorderDefaultFlag *reorder)
9600adc21957SMatthew G. Knepley {
9601adc21957SMatthew G. Knepley   PetscFunctionBegin;
9602adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9603adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9604adc21957SMatthew G. Knepley   *reorder = DM_REORDER_DEFAULT_NOTSET;
9605adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetDefault_C", (DM, DMReorderDefaultFlag *), (dm, reorder));
9606adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9607adc21957SMatthew G. Knepley }
9608adc21957SMatthew G. Knepley 
9609cc4c1da9SBarry Smith /*@
9610adc21957SMatthew G. Knepley   DMReorderSectionSetType - Set the type of local section reordering
9611adc21957SMatthew G. Knepley 
9612adc21957SMatthew G. Knepley   Logically collective
9613adc21957SMatthew G. Knepley 
9614adc21957SMatthew G. Knepley   Input Parameters:
9615adc21957SMatthew G. Knepley + dm      - The DM
9616adc21957SMatthew G. Knepley - reorder - The reordering method
9617adc21957SMatthew G. Knepley 
9618adc21957SMatthew G. Knepley   Level: intermediate
9619adc21957SMatthew G. Knepley 
9620adc21957SMatthew G. Knepley .seealso: `DMReorderSectionGetType()`, `DMReorderSectionSetDefault()`
9621adc21957SMatthew G. Knepley @*/
9622adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionSetType(DM dm, MatOrderingType reorder)
9623adc21957SMatthew G. Knepley {
9624adc21957SMatthew G. Knepley   PetscFunctionBegin;
9625adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9626adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionSetType_C", (DM, MatOrderingType), (dm, reorder));
9627adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9628adc21957SMatthew G. Knepley }
9629adc21957SMatthew G. Knepley 
9630cc4c1da9SBarry Smith /*@
9631adc21957SMatthew G. Knepley   DMReorderSectionGetType - Get the reordering type for the local section
9632adc21957SMatthew G. Knepley 
9633adc21957SMatthew G. Knepley   Not collective
9634adc21957SMatthew G. Knepley 
9635adc21957SMatthew G. Knepley   Input Parameter:
9636adc21957SMatthew G. Knepley . dm - The DM
9637adc21957SMatthew G. Knepley 
9638adc21957SMatthew G. Knepley   Output Parameter:
9639adc21957SMatthew G. Knepley . reorder - The reordering method
9640adc21957SMatthew G. Knepley 
9641adc21957SMatthew G. Knepley   Level: intermediate
9642adc21957SMatthew G. Knepley 
9643adc21957SMatthew G. Knepley .seealso: `DMReorderSetDefault()`, `DMReorderSectionGetDefault()`
9644adc21957SMatthew G. Knepley @*/
9645adc21957SMatthew G. Knepley PetscErrorCode DMReorderSectionGetType(DM dm, MatOrderingType *reorder)
9646adc21957SMatthew G. Knepley {
9647adc21957SMatthew G. Knepley   PetscFunctionBegin;
9648adc21957SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9649adc21957SMatthew G. Knepley   PetscAssertPointer(reorder, 2);
9650adc21957SMatthew G. Knepley   *reorder = NULL;
9651adc21957SMatthew G. Knepley   PetscTryMethod(dm, "DMReorderSectionGetType_C", (DM, MatOrderingType *), (dm, reorder));
9652adc21957SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9653adc21957SMatthew G. Knepley }
9654