xref: /petsc/src/dm/interface/dm.c (revision 863027ab13ef784e36250162f0e079d3059cd0d5)
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>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
10f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
11f918ec44SMatthew G. Knepley   #include <petscfeceed.h>
12f918ec44SMatthew G. Knepley #endif
13f918ec44SMatthew G. Knepley 
14cea3dcb8SSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
15cea3dcb8SSatish Balay   #include <petsc/private/valgrind/memcheck.h>
1600d952a4SJed Brown #endif
1700d952a4SJed Brown 
18732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID;
19d67d17b1SMatthew G. Knepley PetscClassId DMLABEL_CLASSID;
205b8ffe73SMark Adams 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;
2167a56275SMatthew G Knepley 
22ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[]          = {"NONE", "GHOSTED", "MIRROR", "PERIODIC", "TWIST", "DMBoundaryType", "DM_BOUNDARY_", NULL};
23d1b3049bSMatthew 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};
24*863027abSJed Brown const char *const DMBlockingTypes[]          = {"POINT", "FIELD_NODE", "DMBlockingType", "DM_BLOCKING_", NULL};
259371c9d4SSatish Balay const char *const DMPolytopeTypes[]   = {"vertex",  "segment",       "tensor_segment",      "triangle", "quadrilateral", "tensor_quad",    "tetrahedron",  "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism",
269371c9d4SSatish Balay                                          "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown",  "invalid",       "DMPolytopeType", "DM_POLYTOPE_", NULL};
272cbb9b06SVaclav Hapla const char *const DMCopyLabelsModes[] = {"replace", "keep", "fail", "DMCopyLabelsMode", "DM_COPY_LABELS_", NULL};
2860c22052SBarry Smith 
29a4121054SBarry Smith /*@
30bb7acecfSBarry Smith   DMCreate - Creates an empty `DM` object. `DM`s are the abstract objects in PETSc that mediate between meshes and discretizations and the
31bb7acecfSBarry Smith   algebraic solvers, time integrators, and optimization algorithms.
32a4121054SBarry Smith 
33d083f849SBarry Smith   Collective
34a4121054SBarry Smith 
35a4121054SBarry Smith   Input Parameter:
36bb7acecfSBarry Smith . comm - The communicator for the `DM` object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Output Parameter:
39bb7acecfSBarry Smith . dm - The `DM` object
40a4121054SBarry Smith 
41a4121054SBarry Smith   Level: beginner
42a4121054SBarry Smith 
43bb7acecfSBarry Smith   Notes:
44bb7acecfSBarry Smith   See `DMType` for a brief summary of available `DM`.
45bb7acecfSBarry Smith 
46bb7acecfSBarry Smith   The type must then be set with `DMSetType()`. If you never call `DMSetType()` it will generate an
47bb7acecfSBarry Smith   error when you try to use the dm.
48bb7acecfSBarry Smith 
49bb7acecfSBarry Smith .seealso: `DMSetType()`, `DMType`, `DMDACreate()`, `DMDA`, `DMSLICED`, `DMCOMPOSITE`, `DMPLEX`, `DMMOAB`, `DMNETWORK`
50a4121054SBarry Smith @*/
51d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreate(MPI_Comm comm, DM *dm)
52d71ae5a4SJacob Faibussowitsch {
53a4121054SBarry Smith   DM      v;
54e5e52638SMatthew G. Knepley   PetscDS ds;
55a4121054SBarry Smith 
56a4121054SBarry Smith   PetscFunctionBegin;
571411c6eeSJed Brown   PetscValidPointer(dm, 2);
580298fd71SBarry Smith   *dm = NULL;
599566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
60a4121054SBarry Smith 
619566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView));
62e7c4fc90SDmitry Karpeev 
6362e5d2d2SJDBetteridge   ((PetscObject)v)->non_cyclic_references = &DMCountNonCyclicReferences;
6462e5d2d2SJDBetteridge 
6549be4549SMatthew G. Knepley   v->setupcalled          = PETSC_FALSE;
6649be4549SMatthew G. Knepley   v->setfromoptionscalled = PETSC_FALSE;
670298fd71SBarry Smith   v->ltogmap              = NULL;
68a4ea9b21SRichard Tran Mills   v->bind_below           = 0;
691411c6eeSJed Brown   v->bs                   = 1;
70171400e9SBarry Smith   v->coloringtype         = IS_COLORING_GLOBAL;
719566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sf));
729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreate(comm, &v->sectionSF));
73c58f1c22SToby Isaac   v->labels                    = NULL;
7434aa8a36SMatthew G. Knepley   v->adjacency[0]              = PETSC_FALSE;
7534aa8a36SMatthew G. Knepley   v->adjacency[1]              = PETSC_TRUE;
76c58f1c22SToby Isaac   v->depthLabel                = NULL;
77ba2698f1SMatthew G. Knepley   v->celltypeLabel             = NULL;
781bb6d2a8SBarry Smith   v->localSection              = NULL;
791bb6d2a8SBarry Smith   v->globalSection             = NULL;
803b8ba7d1SJed Brown   v->defaultConstraint.section = NULL;
813b8ba7d1SJed Brown   v->defaultConstraint.mat     = NULL;
8279769bd5SJed Brown   v->defaultConstraint.bias    = NULL;
836858538eSMatthew G. Knepley   v->coordinates[0].dim        = PETSC_DEFAULT;
846858538eSMatthew G. Knepley   v->coordinates[1].dim        = PETSC_DEFAULT;
856858538eSMatthew G. Knepley   v->sparseLocalize            = PETSC_TRUE;
8696173672SStefano Zampini   v->dim                       = PETSC_DETERMINE;
87435a35e8SMatthew G Knepley   {
88435a35e8SMatthew G Knepley     PetscInt i;
89435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
900298fd71SBarry Smith       v->nullspaceConstructors[i]     = NULL;
91f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
92435a35e8SMatthew G Knepley     }
93435a35e8SMatthew G Knepley   }
949566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
959566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(v, NULL, NULL, ds));
969566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&ds));
979566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxCreate(&v->auxData));
9814f150ffSMatthew G. Knepley   v->dmBC              = NULL;
99a8fb8f29SToby Isaac   v->coarseMesh        = NULL;
100f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
101cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
1029566063dSJacob Faibussowitsch   PetscCall(DMSetVecType(v, VECSTANDARD));
1039566063dSJacob Faibussowitsch   PetscCall(DMSetMatType(v, MATAIJ));
1044a7a4c06SLawrence Mitchell 
1051411c6eeSJed Brown   *dm = v;
106a4121054SBarry Smith   PetscFunctionReturn(0);
107a4121054SBarry Smith }
108a4121054SBarry Smith 
10938221697SMatthew G. Knepley /*@
110bb7acecfSBarry Smith   DMClone - Creates a `DM` object with the same topology as the original.
11138221697SMatthew G. Knepley 
112d083f849SBarry Smith   Collective
11338221697SMatthew G. Knepley 
11438221697SMatthew G. Knepley   Input Parameter:
115bb7acecfSBarry Smith . dm - The original `DM` object
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   Output Parameter:
118bb7acecfSBarry Smith . newdm  - The new `DM` object
11938221697SMatthew G. Knepley 
12038221697SMatthew G. Knepley   Level: beginner
12138221697SMatthew G. Knepley 
1221cb8cacdSPatrick Sanan   Notes:
123bb7acecfSBarry Smith   For some `DM` implementations this is a shallow clone, the result of which may share (reference counted) information with its parent. For example,
124bb7acecfSBarry Smith   `DMClone()` applied to a `DMPLEX` object will result in a new `DMPLEX` that shares the topology with the original `DMPLEX`. It does not
125bb7acecfSBarry Smith   share the `PetscSection` of the original `DM`.
1261bb6d2a8SBarry Smith 
127bb7acecfSBarry Smith   The clone is considered set up if the original has been set up.
12889706ed2SPatrick Sanan 
129bb7acecfSBarry Smith   Use `DMConvert()` for a general way to create new `DM` from a given `DM`
130bb7acecfSBarry Smith 
131bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMSetType()`, `DMSetLocalSection()`, `DMSetGlobalSection()`, `DMPLEX`, `DMSetType()`, `DMConvert()`
13238221697SMatthew G. Knepley @*/
133d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClone(DM dm, DM *newdm)
134d71ae5a4SJacob Faibussowitsch {
13538221697SMatthew G. Knepley   PetscSF  sf;
13638221697SMatthew G. Knepley   Vec      coords;
13738221697SMatthew G. Knepley   void    *ctx;
1386858538eSMatthew G. Knepley   PetscInt dim, cdim, i;
13938221697SMatthew G. Knepley 
14038221697SMatthew G. Knepley   PetscFunctionBegin;
14138221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14238221697SMatthew G. Knepley   PetscValidPointer(newdm, 2);
1439566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), newdm));
1449566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE, DM_COPY_LABELS_FAIL));
145ddf8437dSMatthew G. Knepley   (*newdm)->leveldown     = dm->leveldown;
146ddf8437dSMatthew G. Knepley   (*newdm)->levelup       = dm->levelup;
147c8a6034eSMark   (*newdm)->prealloc_only = dm->prealloc_only;
1489566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->vectype));
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*newdm)->vectype));
1509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*newdm)->mattype));
1519566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*newdm)->mattype));
1529566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
1539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*newdm, dim));
154dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, clone, newdm);
1553f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
1569566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(*newdm, sf));
1589566063dSJacob Faibussowitsch   PetscCall(DMGetApplicationContext(dm, &ctx));
1599566063dSJacob Faibussowitsch   PetscCall(DMSetApplicationContext(*newdm, ctx));
1606858538eSMatthew G. Knepley   for (i = 0; i < 2; ++i) {
1616858538eSMatthew G. Knepley     if (dm->coordinates[i].dm) {
162be4c1c3eSMatthew G. Knepley       DM           ncdm;
163be4c1c3eSMatthew G. Knepley       PetscSection cs;
1645a0206caSToby Isaac       PetscInt     pEnd = -1, pEndMax = -1;
165be4c1c3eSMatthew G. Knepley 
1666858538eSMatthew G. Knepley       PetscCall(DMGetLocalSection(dm->coordinates[i].dm, &cs));
1679566063dSJacob Faibussowitsch       if (cs) PetscCall(PetscSectionGetChart(cs, NULL, &pEnd));
1689566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&pEnd, &pEndMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
1695a0206caSToby Isaac       if (pEndMax >= 0) {
1706858538eSMatthew G. Knepley         PetscCall(DMClone(dm->coordinates[i].dm, &ncdm));
1716858538eSMatthew G. Knepley         PetscCall(DMCopyDisc(dm->coordinates[i].dm, ncdm));
1729566063dSJacob Faibussowitsch         PetscCall(DMSetLocalSection(ncdm, cs));
1736858538eSMatthew G. Knepley         if (i) PetscCall(DMSetCellCoordinateDM(*newdm, ncdm));
1746858538eSMatthew G. Knepley         else PetscCall(DMSetCoordinateDM(*newdm, ncdm));
1759566063dSJacob Faibussowitsch         PetscCall(DMDestroy(&ncdm));
176be4c1c3eSMatthew G. Knepley       }
177be4c1c3eSMatthew G. Knepley     }
1786858538eSMatthew G. Knepley   }
1799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
1809566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*newdm, cdim));
1819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coords));
18238221697SMatthew G. Knepley   if (coords) {
1839566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(*newdm, coords));
18438221697SMatthew G. Knepley   } else {
1859566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinates(dm, &coords));
1869566063dSJacob Faibussowitsch     if (coords) PetscCall(DMSetCoordinates(*newdm, coords));
18738221697SMatthew G. Knepley   }
1886858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dm, &coords));
1896858538eSMatthew G. Knepley   if (coords) {
1906858538eSMatthew G. Knepley     PetscCall(DMSetCellCoordinatesLocal(*newdm, coords));
1916858538eSMatthew G. Knepley   } else {
1926858538eSMatthew G. Knepley     PetscCall(DMGetCellCoordinates(dm, &coords));
1936858538eSMatthew G. Knepley     if (coords) PetscCall(DMSetCellCoordinates(*newdm, coords));
1946858538eSMatthew G. Knepley   }
19590b157c4SStefano Zampini   {
1964fb89dddSMatthew G. Knepley     const PetscReal *maxCell, *Lstart, *L;
1976858538eSMatthew G. Knepley 
1984fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
1994fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(*newdm, maxCell, Lstart, L));
200c6b900c6SMatthew G. Knepley   }
20134aa8a36SMatthew G. Knepley   {
20234aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
20334aa8a36SMatthew G. Knepley 
2049566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure));
2059566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure));
20634aa8a36SMatthew G. Knepley   }
20738221697SMatthew G. Knepley   PetscFunctionReturn(0);
20838221697SMatthew G. Knepley }
20938221697SMatthew G. Knepley 
2109a42bb27SBarry Smith /*@C
211bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2129a42bb27SBarry Smith 
213d083f849SBarry Smith    Logically Collective on da
2149a42bb27SBarry Smith 
215147403d9SBarry Smith    Input Parameters:
2169a42bb27SBarry Smith +  da - initial distributed array
217bb7acecfSBarry Smith -  ctype - the vector type, for example `VECSTANDARD`, `VECCUDA`, or `VECVIENNACL`
2189a42bb27SBarry Smith 
2199a42bb27SBarry Smith    Options Database:
220147403d9SBarry Smith .   -dm_vec_type ctype - the type of vector to create
2219a42bb27SBarry Smith 
2229a42bb27SBarry Smith    Level: intermediate
2239a42bb27SBarry Smith 
224bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMGetVecType()`, `DMSetMatType()`, `DMGetMatType()`,
225bb7acecfSBarry Smith           `VECSTANDARD`, `VECCUDA`, `VECVIENNACL`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`
2269a42bb27SBarry Smith @*/
227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVecType(DM da, VecType ctype)
228d71ae5a4SJacob Faibussowitsch {
2299a42bb27SBarry Smith   PetscFunctionBegin;
2309a42bb27SBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
2319566063dSJacob Faibussowitsch   PetscCall(PetscFree(da->vectype));
2329566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&da->vectype));
2339a42bb27SBarry Smith   PetscFunctionReturn(0);
2349a42bb27SBarry Smith }
2359a42bb27SBarry Smith 
236c0dedaeaSBarry Smith /*@C
237bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
238c0dedaeaSBarry Smith 
239d083f849SBarry Smith    Logically Collective on da
240c0dedaeaSBarry Smith 
241c0dedaeaSBarry Smith    Input Parameter:
242c0dedaeaSBarry Smith .  da - initial distributed array
243c0dedaeaSBarry Smith 
244c0dedaeaSBarry Smith    Output Parameter:
245c0dedaeaSBarry Smith .  ctype - the vector type
246c0dedaeaSBarry Smith 
247c0dedaeaSBarry Smith    Level: intermediate
248c0dedaeaSBarry Smith 
249db781477SPatrick Sanan .seealso: `DMCreate()`, `DMDestroy()`, `DM`, `DMDAInterpolationType`, `VecType`, `DMSetMatType()`, `DMGetMatType()`, `DMSetVecType()`
250c0dedaeaSBarry Smith @*/
251d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetVecType(DM da, VecType *ctype)
252d71ae5a4SJacob Faibussowitsch {
253c0dedaeaSBarry Smith   PetscFunctionBegin;
254c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da, DM_CLASSID, 1);
255c0dedaeaSBarry Smith   *ctype = da->vectype;
256c0dedaeaSBarry Smith   PetscFunctionReturn(0);
257c0dedaeaSBarry Smith }
258c0dedaeaSBarry Smith 
2595f1ad066SMatthew G Knepley /*@
260bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   Not collective
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Input Parameter:
265bb7acecfSBarry Smith . v - The `Vec`
2665f1ad066SMatthew G Knepley 
2675f1ad066SMatthew G Knepley   Output Parameter:
268bb7acecfSBarry Smith . dm - The `DM`
2695f1ad066SMatthew G Knepley 
2705f1ad066SMatthew G Knepley   Level: intermediate
2715f1ad066SMatthew G Knepley 
272bb7acecfSBarry Smith   Note:
273bb7acecfSBarry Smith   A `Vec` may not have a `DM` associated with it.
274bb7acecfSBarry Smith 
275bb7acecfSBarry Smith .seealso: `DM`, `VecSetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
2765f1ad066SMatthew G Knepley @*/
277d71ae5a4SJacob Faibussowitsch PetscErrorCode VecGetDM(Vec v, DM *dm)
278d71ae5a4SJacob Faibussowitsch {
2795f1ad066SMatthew G Knepley   PetscFunctionBegin;
2805f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
2815f1ad066SMatthew G Knepley   PetscValidPointer(dm, 2);
2829566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)v, "__PETSc_dm", (PetscObject *)dm));
2835f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2845f1ad066SMatthew G Knepley }
2855f1ad066SMatthew G Knepley 
2865f1ad066SMatthew G Knepley /*@
287bb7acecfSBarry Smith   VecSetDM - Sets the `DM` defining the data layout of the vector.
2885f1ad066SMatthew G Knepley 
2895f1ad066SMatthew G Knepley   Not collective
2905f1ad066SMatthew G Knepley 
2915f1ad066SMatthew G Knepley   Input Parameters:
292bb7acecfSBarry Smith + v - The `Vec`
293bb7acecfSBarry Smith - dm - The `DM`
2945f1ad066SMatthew G Knepley 
295bb7acecfSBarry Smith   Note:
296bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
297d9805387SMatthew G. Knepley 
298bb7acecfSBarry 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.
299bb7acecfSBarry Smith 
300bb7acecfSBarry Smith   Level: developer
3015f1ad066SMatthew G Knepley 
302db781477SPatrick Sanan .seealso: `VecGetDM()`, `DMGetLocalVector()`, `DMGetGlobalVector()`, `DMSetVecType()`
3035f1ad066SMatthew G Knepley @*/
304d71ae5a4SJacob Faibussowitsch PetscErrorCode VecSetDM(Vec v, DM dm)
305d71ae5a4SJacob Faibussowitsch {
3065f1ad066SMatthew G Knepley   PetscFunctionBegin;
3075f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 1);
308d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3099566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)v, "__PETSc_dm", (PetscObject)dm));
3105f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
3115f1ad066SMatthew G Knepley }
3125f1ad066SMatthew G Knepley 
313521d9a4cSLisandro Dalcin /*@C
314bb7acecfSBarry Smith        DMSetISColoringType - Sets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
3158f1509bcSBarry Smith 
316d083f849SBarry Smith    Logically Collective on dm
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Input Parameters:
319bb7acecfSBarry Smith +  dm - the `DM` context
3208f1509bcSBarry Smith -  ctype - the matrix type
3218f1509bcSBarry Smith 
3228f1509bcSBarry Smith    Options Database:
3238f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith    Level: intermediate
3268f1509bcSBarry Smith 
327db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
328bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3298f1509bcSBarry Smith @*/
330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetISColoringType(DM dm, ISColoringType ctype)
331d71ae5a4SJacob Faibussowitsch {
3328f1509bcSBarry Smith   PetscFunctionBegin;
3338f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3348f1509bcSBarry Smith   dm->coloringtype = ctype;
3358f1509bcSBarry Smith   PetscFunctionReturn(0);
3368f1509bcSBarry Smith }
3378f1509bcSBarry Smith 
3388f1509bcSBarry Smith /*@C
339bb7acecfSBarry Smith        DMGetISColoringType - Gets the type of coloring, `IS_COLORING_GLOBAL` or `IS_COLORING_LOCAL` that is created by the `DM`
340521d9a4cSLisandro Dalcin 
341d083f849SBarry Smith    Logically Collective on dm
342521d9a4cSLisandro Dalcin 
343521d9a4cSLisandro Dalcin    Input Parameter:
344bb7acecfSBarry Smith .  dm - the `DM` context
3458f1509bcSBarry Smith 
3468f1509bcSBarry Smith    Output Parameter:
3478f1509bcSBarry Smith .  ctype - the matrix type
3488f1509bcSBarry Smith 
3498f1509bcSBarry Smith    Options Database:
3508f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3518f1509bcSBarry Smith 
3528f1509bcSBarry Smith    Level: intermediate
3538f1509bcSBarry Smith 
354db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`,
355bb7acecfSBarry Smith           `DMGetISColoringType()`, `ISColoringType`, `IS_COLORING_GLOBAL`, `IS_COLORING_LOCAL`
3568f1509bcSBarry Smith @*/
357d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetISColoringType(DM dm, ISColoringType *ctype)
358d71ae5a4SJacob Faibussowitsch {
3598f1509bcSBarry Smith   PetscFunctionBegin;
3608f1509bcSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3618f1509bcSBarry Smith   *ctype = dm->coloringtype;
3628f1509bcSBarry Smith   PetscFunctionReturn(0);
3638f1509bcSBarry Smith }
3648f1509bcSBarry Smith 
3658f1509bcSBarry Smith /*@C
366bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3678f1509bcSBarry Smith 
368d083f849SBarry Smith    Logically Collective on dm
3698f1509bcSBarry Smith 
3708f1509bcSBarry Smith    Input Parameters:
371bb7acecfSBarry Smith +  dm - the `DM` context
372bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
373521d9a4cSLisandro Dalcin 
374521d9a4cSLisandro Dalcin    Options Database:
375bb7acecfSBarry Smith .   -dm_mat_type ctype - the type of the matrix to create, for example mpiaij
376521d9a4cSLisandro Dalcin 
377521d9a4cSLisandro Dalcin    Level: intermediate
378521d9a4cSLisandro Dalcin 
379bb7acecfSBarry Smith .seealso: `MatType`, `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMGetMatType()`, `DMSetMatType()`, `DMGetMatType()`, `DMCreateGlobalVector()`, `DMCreateLocalVector()`
380521d9a4cSLisandro Dalcin @*/
381d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatType(DM dm, MatType ctype)
382d71ae5a4SJacob Faibussowitsch {
383521d9a4cSLisandro Dalcin   PetscFunctionBegin;
384521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3859566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->mattype));
3869566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(ctype, (char **)&dm->mattype));
387521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
388521d9a4cSLisandro Dalcin }
389521d9a4cSLisandro Dalcin 
390c0dedaeaSBarry Smith /*@C
391bb7acecfSBarry Smith        DMGetMatType - Gets the type of matrix created with `DMCreateMatrix()`
392c0dedaeaSBarry Smith 
393d083f849SBarry Smith    Logically Collective on dm
394c0dedaeaSBarry Smith 
395c0dedaeaSBarry Smith    Input Parameter:
396bb7acecfSBarry Smith .  dm - the `DM` context
397c0dedaeaSBarry Smith 
398c0dedaeaSBarry Smith    Output Parameter:
399c0dedaeaSBarry Smith .  ctype - the matrix type
400c0dedaeaSBarry Smith 
401c0dedaeaSBarry Smith    Level: intermediate
402c0dedaeaSBarry Smith 
403db781477SPatrick Sanan .seealso: `DMDACreate1d()`, `DMDACreate2d()`, `DMDACreate3d()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixPreallocateOnly()`, `MatType`, `DMSetMatType()`, `DMSetMatType()`, `DMGetMatType()`
404c0dedaeaSBarry Smith @*/
405d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetMatType(DM dm, MatType *ctype)
406d71ae5a4SJacob Faibussowitsch {
407c0dedaeaSBarry Smith   PetscFunctionBegin;
408c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409c0dedaeaSBarry Smith   *ctype = dm->mattype;
410c0dedaeaSBarry Smith   PetscFunctionReturn(0);
411c0dedaeaSBarry Smith }
412c0dedaeaSBarry Smith 
413c688c046SMatthew G Knepley /*@
414bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Not collective
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley   Input Parameter:
419bb7acecfSBarry Smith . A - The `Mat`
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Output Parameter:
422bb7acecfSBarry Smith . dm - The `DM`
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Level: intermediate
425c688c046SMatthew G Knepley 
426bb7acecfSBarry Smith   Note:
427bb7acecfSBarry Smith   A matrix may not have a `DM` associated with it
428bb7acecfSBarry Smith 
429bb7acecfSBarry Smith   Developer Note:
430bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with the `Mat` through a `PetscObjectCompose()` operation
4318f1509bcSBarry Smith 
432db781477SPatrick Sanan .seealso: `MatSetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
433c688c046SMatthew G Knepley @*/
434d71ae5a4SJacob Faibussowitsch PetscErrorCode MatGetDM(Mat A, DM *dm)
435d71ae5a4SJacob Faibussowitsch {
436c688c046SMatthew G Knepley   PetscFunctionBegin;
437c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
438c688c046SMatthew G Knepley   PetscValidPointer(dm, 2);
4399566063dSJacob Faibussowitsch   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_dm", (PetscObject *)dm));
440c688c046SMatthew G Knepley   PetscFunctionReturn(0);
441c688c046SMatthew G Knepley }
442c688c046SMatthew G Knepley 
443c688c046SMatthew G Knepley /*@
444bb7acecfSBarry Smith   MatSetDM - Sets the `DM` defining the data layout of the matrix
445c688c046SMatthew G Knepley 
446c688c046SMatthew G Knepley   Not collective
447c688c046SMatthew G Knepley 
448c688c046SMatthew G Knepley   Input Parameters:
449c688c046SMatthew G Knepley + A - The Mat
450c688c046SMatthew G Knepley - dm - The DM
451c688c046SMatthew G Knepley 
452bb7acecfSBarry Smith   Level: developer
453c688c046SMatthew G Knepley 
454bb7acecfSBarry Smith   Note:
455bb7acecfSBarry Smith   This is rarely used in practice, rather `DMCreateMatrix()` is used to create a matrix associated with a particular `DM`
456bb7acecfSBarry Smith 
457bb7acecfSBarry Smith   Developer Note:
458bb7acecfSBarry Smith   Since the `Mat` class doesn't know about the `DM` class the `DM` object is associated with
459bb7acecfSBarry Smith   the `Mat` through a `PetscObjectCompose()` operation
4608f1509bcSBarry Smith 
461db781477SPatrick Sanan .seealso: `MatGetDM()`, `DMCreateMatrix()`, `DMSetMatType()`
462c688c046SMatthew G Knepley @*/
463d71ae5a4SJacob Faibussowitsch PetscErrorCode MatSetDM(Mat A, DM dm)
464d71ae5a4SJacob Faibussowitsch {
465c688c046SMatthew G Knepley   PetscFunctionBegin;
466c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 1);
4678865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
4689566063dSJacob Faibussowitsch   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_dm", (PetscObject)dm));
469c688c046SMatthew G Knepley   PetscFunctionReturn(0);
470c688c046SMatthew G Knepley }
471c688c046SMatthew G Knepley 
4729a42bb27SBarry Smith /*@C
473bb7acecfSBarry Smith    DMSetOptionsPrefix - Sets the prefix prepended to all option names when searching through the options database
4749a42bb27SBarry Smith 
475d083f849SBarry Smith    Logically Collective on dm
4769a42bb27SBarry Smith 
477d8d19677SJose E. Roman    Input Parameters:
478bb7acecfSBarry Smith +  da - the `DM` context
479bb7acecfSBarry Smith -  prefix - the prefix to prepend
4809a42bb27SBarry Smith 
4819a42bb27SBarry Smith    Notes:
4829a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4839a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4849a42bb27SBarry Smith 
4859a42bb27SBarry Smith    Level: advanced
4869a42bb27SBarry Smith 
487bb7acecfSBarry Smith .seealso: `PetscObjectSetOptionsPrefix()`, `DMSetFromOptions()`
4889a42bb27SBarry Smith @*/
489d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOptionsPrefix(DM dm, const char prefix[])
490d71ae5a4SJacob Faibussowitsch {
4919a42bb27SBarry Smith   PetscFunctionBegin;
4929a42bb27SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4939566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
4941baa6e33SBarry Smith   if (dm->sf) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sf, prefix));
4951baa6e33SBarry Smith   if (dm->sectionSF) PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF, prefix));
4969a42bb27SBarry Smith   PetscFunctionReturn(0);
4979a42bb27SBarry Smith }
4989a42bb27SBarry Smith 
49931697293SDave May /*@C
500bb7acecfSBarry Smith    DMAppendOptionsPrefix - Appends an additional string to an already exising prefix used for searching for
501bb7acecfSBarry Smith    `DM` options in the options database.
50231697293SDave May 
503d083f849SBarry Smith    Logically Collective on dm
50431697293SDave May 
50531697293SDave May    Input Parameters:
506bb7acecfSBarry Smith +  dm - the `DM` context
507bb7acecfSBarry Smith -  prefix - the string to append to the current prefix
50831697293SDave May 
50931697293SDave May    Notes:
510bb7acecfSBarry 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.
51131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51331697293SDave May 
51431697293SDave May    Level: advanced
51531697293SDave May 
516bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMGetOptionsPrefix()`, `PetscObjectAppendOptionsPrefix()`, `DMSetFromOptions()`
51731697293SDave May @*/
518d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAppendOptionsPrefix(DM dm, const char prefix[])
519d71ae5a4SJacob Faibussowitsch {
52031697293SDave May   PetscFunctionBegin;
52131697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5229566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, prefix));
52331697293SDave May   PetscFunctionReturn(0);
52431697293SDave May }
52531697293SDave May 
52631697293SDave May /*@C
52731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
528bb7acecfSBarry Smith    DM options in the options database.
52931697293SDave May 
53031697293SDave May    Not Collective
53131697293SDave May 
53231697293SDave May    Input Parameters:
533bb7acecfSBarry Smith .  dm - the `DM` context
53431697293SDave May 
53531697293SDave May    Output Parameters:
53631697293SDave May .  prefix - pointer to the prefix string used is returned
53731697293SDave May 
538bb7acecfSBarry Smith    Fortran Note:
53995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
54031697293SDave May    sufficient length to hold the prefix.
54131697293SDave May 
54231697293SDave May    Level: advanced
54331697293SDave May 
544bb7acecfSBarry Smith .seealso: `DMSetOptionsPrefix()`, `DMAppendOptionsPrefix()`, `DMSetFromOptions()`
54531697293SDave May @*/
546d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOptionsPrefix(DM dm, const char *prefix[])
547d71ae5a4SJacob Faibussowitsch {
54831697293SDave May   PetscFunctionBegin;
54931697293SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5509566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, prefix));
55131697293SDave May   PetscFunctionReturn(0);
55231697293SDave May }
55331697293SDave May 
55462e5d2d2SJDBetteridge static PetscErrorCode DMCountNonCyclicReferences_Internal(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
555d71ae5a4SJacob Faibussowitsch {
5566eb26441SStefano Zampini   PetscInt refct = ((PetscObject)dm)->refct;
55788bdff64SToby Isaac 
55888bdff64SToby Isaac   PetscFunctionBegin;
559aab5bcd8SJed Brown   *ncrefct = 0;
56088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseCoarse) {
56388bdff64SToby Isaac       PetscInt coarseCount;
56488bdff64SToby Isaac 
56562e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE, &coarseCount));
56688bdff64SToby Isaac       refct += coarseCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
57088bdff64SToby Isaac     refct--;
57188bdff64SToby Isaac     if (recurseFine) {
57288bdff64SToby Isaac       PetscInt fineCount;
57388bdff64SToby Isaac 
57462e5d2d2SJDBetteridge       PetscCall(DMCountNonCyclicReferences_Internal(dm->fineMesh, PETSC_FALSE, PETSC_TRUE, &fineCount));
57588bdff64SToby Isaac       refct += fineCount;
57688bdff64SToby Isaac     }
57788bdff64SToby Isaac   }
57888bdff64SToby Isaac   *ncrefct = refct;
57988bdff64SToby Isaac   PetscFunctionReturn(0);
58088bdff64SToby Isaac }
58188bdff64SToby Isaac 
58262e5d2d2SJDBetteridge /* Generic wrapper for DMCountNonCyclicReferences_Internal() */
58362e5d2d2SJDBetteridge PetscErrorCode DMCountNonCyclicReferences(PetscObject dm, PetscInt *ncrefct)
58462e5d2d2SJDBetteridge {
58562e5d2d2SJDBetteridge   PetscFunctionBegin;
58662e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal((DM)dm, PETSC_TRUE, PETSC_TRUE, ncrefct));
58762e5d2d2SJDBetteridge   PetscFunctionReturn(0);
58862e5d2d2SJDBetteridge }
58962e5d2d2SJDBetteridge 
590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
591d71ae5a4SJacob Faibussowitsch {
5925d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
593354557abSToby Isaac 
594354557abSToby Isaac   PetscFunctionBegin;
595354557abSToby Isaac   /* destroy the labels */
596354557abSToby Isaac   while (next) {
597354557abSToby Isaac     DMLabelLink tmp = next->next;
598354557abSToby Isaac 
5995d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
600ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
6019566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&next->label));
6029566063dSJacob Faibussowitsch     PetscCall(PetscFree(next));
603354557abSToby Isaac     next = tmp;
604354557abSToby Isaac   }
6055d80c0bfSVaclav Hapla   dm->labels = NULL;
606354557abSToby Isaac   PetscFunctionReturn(0);
607354557abSToby Isaac }
608354557abSToby Isaac 
609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroyCoordinates_Private(DMCoordinates *c)
610d71ae5a4SJacob Faibussowitsch {
6116858538eSMatthew G. Knepley   PetscFunctionBegin;
6126858538eSMatthew G. Knepley   c->dim = PETSC_DEFAULT;
6136858538eSMatthew G. Knepley   PetscCall(DMDestroy(&c->dm));
6146858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->x));
6156858538eSMatthew G. Knepley   PetscCall(VecDestroy(&c->xl));
6166858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&c->field));
6176858538eSMatthew G. Knepley   PetscFunctionReturn(0);
6186858538eSMatthew G. Knepley }
6196858538eSMatthew G. Knepley 
6201fb7b255SJunchao Zhang /*@C
621bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
62247c6ae99SBarry Smith 
623d083f849SBarry Smith     Collective on dm
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
626bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
630bb7acecfSBarry Smith .seealso: `DMCreate()`, `DMType`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
63147c6ae99SBarry Smith @*/
632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMDestroy(DM *dm)
633d71ae5a4SJacob Faibussowitsch {
6346eb26441SStefano Zampini   PetscInt       cnt;
635dfe15315SJed Brown   DMNamedVecLink nlink, nnext;
63647c6ae99SBarry Smith 
63747c6ae99SBarry Smith   PetscFunctionBegin;
6386bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6396bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm), DM_CLASSID, 1);
64087e657c6SBarry Smith 
64188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
64262e5d2d2SJDBetteridge   PetscCall(DMCountNonCyclicReferences_Internal(*dm, PETSC_TRUE, PETSC_TRUE, &cnt));
64388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
6449371c9d4SSatish Balay   if (--cnt > 0) {
6459371c9d4SSatish Balay     *dm = NULL;
6469371c9d4SSatish Balay     PetscFunctionReturn(0);
6479371c9d4SSatish Balay   }
6486bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6496bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6506eb26441SStefano Zampini 
6519566063dSJacob Faibussowitsch   PetscCall(DMClearGlobalVectors(*dm));
6529566063dSJacob Faibussowitsch   PetscCall(DMClearLocalVectors(*dm));
6536eb26441SStefano Zampini 
654f490541aSPeter Brune   nnext              = (*dm)->namedglobal;
6550298fd71SBarry Smith   (*dm)->namedglobal = NULL;
656f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named vectors */
6572348bcf4SPeter Brune     nnext = nlink->next;
6587a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6599566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6619566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
6622348bcf4SPeter Brune   }
663f490541aSPeter Brune   nnext             = (*dm)->namedlocal;
6640298fd71SBarry Smith   (*dm)->namedlocal = NULL;
665f490541aSPeter Brune   for (nlink = nnext; nlink; nlink = nnext) { /* Destroy the named local vectors */
666f490541aSPeter Brune     nnext = nlink->next;
6677a8be351SBarry Smith     PetscCheck(nlink->status == DMVEC_STATUS_IN, ((PetscObject)*dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM still has Vec named '%s' checked out", nlink->name);
6689566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink->name));
6699566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&nlink->X));
6709566063dSJacob Faibussowitsch     PetscCall(PetscFree(nlink));
671f490541aSPeter Brune   }
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;
7177a8be351SBarry Smith     PetscCheck(!(*dm)->workout, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Work array still checked out");
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));
7529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&(*dm)->map));
7539566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&(*dm)->defaultConstraint.section));
7549566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*dm)->defaultConstraint.mat));
7559566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sf));
7569566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&(*dm)->sectionSF));
757736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
75848a46eb9SPierre Jolivet     if ((*dm)->sfNatural) PetscCall(PetscSFDestroy(&(*dm)->sfNatural));
7599566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*dm)->sfMigration));
760736995cdSBlaise Bourdin   }
7619a2a23afSMatthew G. Knepley   {
7629a2a23afSMatthew G. Knepley     Vec     *auxData;
7639a2a23afSMatthew G. Knepley     PetscInt n, i, off = 0;
7649a2a23afSMatthew G. Knepley 
7659566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetSize((*dm)->auxData, &n));
7669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &auxData));
7679566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxGetVals((*dm)->auxData, &off, auxData));
7689566063dSJacob Faibussowitsch     for (i = 0; i < n; ++i) PetscCall(VecDestroy(&auxData[i]));
7699566063dSJacob Faibussowitsch     PetscCall(PetscFree(auxData));
7709566063dSJacob Faibussowitsch     PetscCall(PetscHMapAuxDestroy(&(*dm)->auxData));
7719a2a23afSMatthew G. Knepley   }
77248a46eb9SPierre Jolivet   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) PetscCall(DMSetFineDM((*dm)->coarseMesh, NULL));
7736eb26441SStefano Zampini 
7749566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->coarseMesh));
77548a46eb9SPierre Jolivet   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) PetscCall(DMSetCoarseDM((*dm)->fineMesh, NULL));
7769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->fineMesh));
7774fb89dddSMatthew G. Knepley   PetscCall(PetscFree((*dm)->Lstart));
7789566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->L));
7799566063dSJacob Faibussowitsch   PetscCall(PetscFree((*dm)->maxCell));
7806858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[0]));
7816858538eSMatthew G. Knepley   PetscCall(DMDestroyCoordinates_Private(&(*dm)->coordinates[1]));
7829566063dSJacob Faibussowitsch   if ((*dm)->transformDestroy) PetscCall((*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx));
7839566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->transformDM));
7849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*dm)->transform));
7856725e60dSJed Brown   PetscCall(VecScatterDestroy(&(*dm)->periodic.affine_to_local));
7866725e60dSJed Brown   PetscCall(VecDestroy(&(*dm)->periodic.affine));
7876636e97aSMatthew G Knepley 
7889566063dSJacob Faibussowitsch   PetscCall(DMClearDS(*dm));
7899566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&(*dm)->dmBC));
790e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
7919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsViewOff((PetscObject)*dm));
792732e2eb9SMatthew G Knepley 
79348a46eb9SPierre Jolivet   if ((*dm)->ops->destroy) PetscCall((*(*dm)->ops->destroy)(*dm));
7949566063dSJacob Faibussowitsch   PetscCall(DMMonitorCancel(*dm));
795f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
7969566063dSJacob Faibussowitsch   PetscCallCEED(CeedElemRestrictionDestroy(&(*dm)->ceedERestrict));
7979566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*dm)->ceed));
798f918ec44SMatthew G. Knepley #endif
799435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
8009566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(dm));
80147c6ae99SBarry Smith   PetscFunctionReturn(0);
80247c6ae99SBarry Smith }
80347c6ae99SBarry Smith 
804d7bf68aeSBarry Smith /*@
805bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
806d7bf68aeSBarry Smith 
807d083f849SBarry Smith     Collective on dm
808d7bf68aeSBarry Smith 
809d7bf68aeSBarry Smith     Input Parameter:
810bb7acecfSBarry Smith .   dm - the `DM` object to setup
811d7bf68aeSBarry Smith 
812bb7acecfSBarry Smith     Level: intermediate
813d7bf68aeSBarry Smith 
814bb7acecfSBarry Smith     Note:
815bb7acecfSBarry Smith     This is usually called after various parameter setting operations and `DMSetFromOptions()` are called on the `DM`
816bb7acecfSBarry Smith 
817bb7acecfSBarry Smith .seealso: `DM`, `DMCreate()`, `DMSetType()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
818d7bf68aeSBarry Smith @*/
819d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUp(DM dm)
820d71ae5a4SJacob Faibussowitsch {
821d7bf68aeSBarry Smith   PetscFunctionBegin;
822171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8238387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
824dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8258387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
826d7bf68aeSBarry Smith   PetscFunctionReturn(0);
827d7bf68aeSBarry Smith }
828d7bf68aeSBarry Smith 
829d7bf68aeSBarry Smith /*@
830bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
831d7bf68aeSBarry Smith 
832d083f849SBarry Smith     Collective on dm
833d7bf68aeSBarry Smith 
834d7bf68aeSBarry Smith     Input Parameter:
835bb7acecfSBarry Smith .   dm - the `DM` object to set options for
836d7bf68aeSBarry Smith 
837732e2eb9SMatthew G Knepley     Options Database:
838bb7acecfSBarry Smith +   -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
839bb7acecfSBarry Smith .   -dm_vec_type <type>  - type of vector to create inside `DM`
840bb7acecfSBarry Smith .   -dm_mat_type <type>  - type of matrix to create inside `DM`
841a4ea9b21SRichard Tran Mills .   -dm_is_coloring_type - <global or local>
842bb7acecfSBarry 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`
843732e2eb9SMatthew G Knepley 
8449318fe57SMatthew G. Knepley     DMPLEX Specific creation options
8459318fe57SMatthew G. Knepley + -dm_plex_filename <str>           - File containing a mesh
8469318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
847cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8485dca41c3SJed Brown . -dm_plex_shape <shape>            - The domain shape, such as `BOX`, `SPHERE`, etc.
8499318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8509318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8519318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
852bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
853bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8549318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8559318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8569318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8579318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
858bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType `for each direction
8599318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8609318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8619318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8629318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
863bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8649318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8659318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8669318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8679318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
868bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
872d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
873d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
874909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
875909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8769318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8779318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8789318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
8799318fe57SMatthew G. Knepley - -dm_plex_adj_closure <bool>       - Set adjacency size
8809318fe57SMatthew G. Knepley 
881384a6580SVaclav Hapla     DMPLEX Specific Checks
882bb7acecfSBarry Smith +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
883bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
884bb7acecfSBarry 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()`
885bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
886bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
887bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
888384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
889d7bf68aeSBarry Smith 
89095eb5ee5SVaclav Hapla     Level: intermediate
89195eb5ee5SVaclav Hapla 
892bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
893bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
894bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
895d7bf68aeSBarry Smith @*/
896d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
897d71ae5a4SJacob Faibussowitsch {
8987781c08eSBarry Smith   char      typeName[256];
899ca266f36SBarry Smith   PetscBool flg;
900d7bf68aeSBarry Smith 
901d7bf68aeSBarry Smith   PetscFunctionBegin;
902171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90349be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9049566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9059566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
906dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
907d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9101baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9121baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
913*863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9159566063dSJacob 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));
916dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
917f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
918dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
919d0609cedSBarry Smith   PetscOptionsEnd();
920d7bf68aeSBarry Smith   PetscFunctionReturn(0);
921d7bf68aeSBarry Smith }
922d7bf68aeSBarry Smith 
923fc9bc008SSatish Balay /*@C
924bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
925fe2efc57SMark 
926bb7acecfSBarry Smith    Collective on dm
927fe2efc57SMark 
928fe2efc57SMark    Input Parameters:
929bb7acecfSBarry Smith +  dm - the `DM` object
930bb7acecfSBarry Smith .  obj - optional object that provides the prefix for the options database (if NULL then the prefix in obj is used)
931bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
932fe2efc57SMark 
933fe2efc57SMark    Level: intermediate
934bb7acecfSBarry Smith 
935bb7acecfSBarry Smith    Note:
936bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
937bb7acecfSBarry Smith 
938bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
939fe2efc57SMark @*/
940d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
941d71ae5a4SJacob Faibussowitsch {
942fe2efc57SMark   PetscFunctionBegin;
943fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9449566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
945fe2efc57SMark   PetscFunctionReturn(0);
946fe2efc57SMark }
947fe2efc57SMark 
948fe2efc57SMark /*@C
949bb7acecfSBarry 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
950bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
95147c6ae99SBarry Smith 
952d083f849SBarry Smith     Collective on dm
95347c6ae99SBarry Smith 
954d8d19677SJose E. Roman     Input Parameters:
955bb7acecfSBarry Smith +   dm - the `DM` object to view
95647c6ae99SBarry Smith -   v - the viewer
95747c6ae99SBarry Smith 
958cd7e8a5eSksagiyam     Notes:
959bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
960bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
961bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
962cd7e8a5eSksagiyam 
963224748a4SBarry Smith     Level: beginner
96447c6ae99SBarry Smith 
965bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96647c6ae99SBarry Smith 
96747c6ae99SBarry Smith @*/
968d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
969d71ae5a4SJacob Faibussowitsch {
97032c0f0efSBarry Smith   PetscBool         isbinary;
97176a8abe0SBarry Smith   PetscMPIInt       size;
97276a8abe0SBarry Smith   PetscViewerFormat format;
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith   PetscFunctionBegin;
975171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97648a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
977b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97874903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97974903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
98074903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
98174903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
98274903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
98374903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98474903a4fSStefano Zampini      in an error here */
98574903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9869566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
987b1b135c8SBarry Smith 
9889566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9899566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
99076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
9919566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9929566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
99332c0f0efSBarry Smith   if (isbinary) {
99455849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99532c0f0efSBarry Smith     char     type[256];
99632c0f0efSBarry Smith 
9979566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
9989566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, 256));
9999566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
100032c0f0efSBarry Smith   }
1001dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
100247c6ae99SBarry Smith   PetscFunctionReturn(0);
100347c6ae99SBarry Smith }
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith /*@
1006bb7acecfSBarry 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,
1007bb7acecfSBarry Smith     that is it has no ghost locations.
100847c6ae99SBarry Smith 
1009d083f849SBarry Smith     Collective on dm
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Input Parameter:
1012bb7acecfSBarry Smith .   dm - the `DM` object
101347c6ae99SBarry Smith 
101447c6ae99SBarry Smith     Output Parameter:
101547c6ae99SBarry Smith .   vec - the global vector
101647c6ae99SBarry Smith 
1017073dac72SJed Brown     Level: beginner
101847c6ae99SBarry Smith 
1019bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1020bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
102147c6ae99SBarry Smith @*/
1022d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1023d71ae5a4SJacob Faibussowitsch {
102447c6ae99SBarry Smith   PetscFunctionBegin;
1025171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1026b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1027dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1029c6b011d8SStefano Zampini     DM vdm;
1030c6b011d8SStefano Zampini 
10319566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10327a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1033c6b011d8SStefano Zampini   }
103447c6ae99SBarry Smith   PetscFunctionReturn(0);
103547c6ae99SBarry Smith }
103647c6ae99SBarry Smith 
103747c6ae99SBarry Smith /*@
1038bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103947c6ae99SBarry Smith 
104047c6ae99SBarry Smith     Not Collective
104147c6ae99SBarry Smith 
104247c6ae99SBarry Smith     Input Parameter:
1043bb7acecfSBarry Smith .   dm - the `DM` object
104447c6ae99SBarry Smith 
104547c6ae99SBarry Smith     Output Parameter:
104647c6ae99SBarry Smith .   vec - the local vector
104747c6ae99SBarry Smith 
1048073dac72SJed Brown     Level: beginner
104947c6ae99SBarry Smith 
1050bb7acecfSBarry Smith     Notes:
1051bb7acecfSBarry 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.
1052bb7acecfSBarry Smith 
1053bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1054bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105547c6ae99SBarry Smith @*/
1056d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1057d71ae5a4SJacob Faibussowitsch {
105847c6ae99SBarry Smith   PetscFunctionBegin;
1059171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1060b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1061dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
106276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1063c6b011d8SStefano Zampini     DM vdm;
1064c6b011d8SStefano Zampini 
10659566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10667a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1067c6b011d8SStefano Zampini   }
106847c6ae99SBarry Smith   PetscFunctionReturn(0);
106947c6ae99SBarry Smith }
107047c6ae99SBarry Smith 
10711411c6eeSJed Brown /*@
1072bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10731411c6eeSJed Brown 
1074d083f849SBarry Smith    Collective on dm
10751411c6eeSJed Brown 
10761411c6eeSJed Brown    Input Parameter:
1077bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10781411c6eeSJed Brown 
10791411c6eeSJed Brown    Output Parameter:
10801411c6eeSJed Brown .  ltog - the mapping
10811411c6eeSJed Brown 
1082bb7acecfSBarry Smith    Level: advanced
10831411c6eeSJed Brown 
10841411c6eeSJed Brown    Notes:
1085bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10861411c6eeSJed Brown 
1087bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1088bb7acecfSBarry Smith    need to use this function with those objects.
1089bb7acecfSBarry Smith 
1090bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1091bb7acecfSBarry Smith 
1092bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1093bb7acecfSBarry Smith           `DMCreateMatrix()`
10941411c6eeSJed Brown @*/
1095d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1096d71ae5a4SJacob Faibussowitsch {
10970be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10981411c6eeSJed Brown 
10991411c6eeSJed Brown   PetscFunctionBegin;
11001411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11011411c6eeSJed Brown   PetscValidPointer(ltog, 2);
11021411c6eeSJed Brown   if (!dm->ltogmap) {
110337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110437d0c07bSMatthew G Knepley 
11059566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110637d0c07bSMatthew G Knepley     if (section) {
1107a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110837d0c07bSMatthew G Knepley       PetscInt       *ltog;
1109ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
111037d0c07bSMatthew G Knepley 
11119566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11139566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11149566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1116e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111737d0c07bSMatthew G Knepley 
111837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11199566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11209566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11219566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11229566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11231a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11241a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1125ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11265227eafbSStefano Zampini 
1127e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11285227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1129e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1130e6befd46SJed Brown             cind++;
1131e6befd46SJed Brown           } else {
11325227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1133e6befd46SJed Brown           }
113437d0c07bSMatthew G Knepley         }
113537d0c07bSMatthew G Knepley       }
1136bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11379371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11389371c9d4SSatish Balay       bsLocal[1] = bs;
11399566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11409371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11419371c9d4SSatish Balay         bs = 1;
11429371c9d4SSatish Balay       } else {
11439371c9d4SSatish Balay         bs = bsMinMax[0];
11449371c9d4SSatish Balay       }
11457591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11467591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1147ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1148ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1149ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1150ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1151ca469d19SJed Brown         }
1152ccf3bd66SMatthew G. Knepley         n /= bs;
1153ccf3bd66SMatthew G. Knepley       }
11549566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1155dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115637d0c07bSMatthew G Knepley   }
11571411c6eeSJed Brown   *ltog = dm->ltogmap;
11581411c6eeSJed Brown   PetscFunctionReturn(0);
11591411c6eeSJed Brown }
11601411c6eeSJed Brown 
11611411c6eeSJed Brown /*@
1162bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11631411c6eeSJed Brown 
11641411c6eeSJed Brown    Not Collective
11651411c6eeSJed Brown 
11661411c6eeSJed Brown    Input Parameter:
1167bb7acecfSBarry Smith .  dm - the `DM` with block structure
11681411c6eeSJed Brown 
11691411c6eeSJed Brown    Output Parameter:
11701411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11711411c6eeSJed Brown 
11721411c6eeSJed Brown    Level: intermediate
11731411c6eeSJed Brown 
1174bb7acecfSBarry Smith    Note:
1175bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1176bb7acecfSBarry Smith 
1177bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1178bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1179bb7acecfSBarry Smith 
1180db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11811411c6eeSJed Brown @*/
1182d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1183d71ae5a4SJacob Faibussowitsch {
11841411c6eeSJed Brown   PetscFunctionBegin;
11851411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1186534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11877a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11881411c6eeSJed Brown   *bs = dm->bs;
11891411c6eeSJed Brown   PetscFunctionReturn(0);
11901411c6eeSJed Brown }
11911411c6eeSJed Brown 
119248eeb7c8SBarry Smith /*@C
1193bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1194bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119547c6ae99SBarry Smith 
1196a5bc1bf3SBarry Smith     Collective on dmc
119747c6ae99SBarry Smith 
1198d8d19677SJose E. Roman     Input Parameters:
1199bb7acecfSBarry Smith +   dmc - the `DM` object
1200bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
120147c6ae99SBarry Smith 
1202d8d19677SJose E. Roman     Output Parameters:
120347c6ae99SBarry Smith +  mat - the interpolation
1204bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Level: developer
120747c6ae99SBarry Smith 
120895452b02SPatrick Sanan     Notes:
1209bb7acecfSBarry 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
1210bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1211d52bd9f3SBarry Smith 
1212bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1213bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121485afcc9aSBarry Smith 
1215bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121647c6ae99SBarry Smith @*/
1217d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1218d71ae5a4SJacob Faibussowitsch {
121947c6ae99SBarry Smith   PetscFunctionBegin;
1220a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1221a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1222c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12239566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1224dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
122647c6ae99SBarry Smith   PetscFunctionReturn(0);
122747c6ae99SBarry Smith }
122847c6ae99SBarry Smith 
12293ad4599aSBarry Smith /*@
1230bb7acecfSBarry Smith     DMCreateInterpolationScale - Forms L = 1/(R*1) where 1 is the vector of all ones, and R is the transpose of the interpolation between the `DM`.
1231bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1232bb7acecfSBarry Smith     representation of xfine.
12332ed6491fSPatrick Sanan 
12342ed6491fSPatrick Sanan   Input Parameters:
1235bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1236bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12372ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12382ed6491fSPatrick Sanan 
12392ed6491fSPatrick Sanan   Output Parameter:
12402ed6491fSPatrick Sanan .    scale - the scaled vector
12412ed6491fSPatrick Sanan 
1242bb7acecfSBarry Smith   Level: advanced
12432ed6491fSPatrick Sanan 
1244e9c74fd6SRichard Tran Mills   Developer Notes:
1245bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1246e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1247e9c74fd6SRichard Tran Mills 
1248bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12492ed6491fSPatrick Sanan @*/
1250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1251d71ae5a4SJacob Faibussowitsch {
12522ed6491fSPatrick Sanan   Vec         fine;
12532ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12549704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1255e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12569704db99SRichard Tran Mills #endif
12572ed6491fSPatrick Sanan 
12582ed6491fSPatrick Sanan   PetscFunctionBegin;
12599566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12609566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12619566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12629704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12639704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12649704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12659704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12669566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1267e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12689566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12699566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12709566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
127183aa49f4SRichard Tran Mills   }
12729704db99SRichard Tran Mills #endif
12739566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12759566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12762ed6491fSPatrick Sanan   PetscFunctionReturn(0);
12772ed6491fSPatrick Sanan }
12782ed6491fSPatrick Sanan 
12792ed6491fSPatrick Sanan /*@
1280bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1281bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12823ad4599aSBarry Smith 
1283a5bc1bf3SBarry Smith     Collective on dmc
12843ad4599aSBarry Smith 
1285d8d19677SJose E. Roman     Input Parameters:
1286bb7acecfSBarry Smith +   dmc - the `DM` object
1287bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12883ad4599aSBarry Smith 
12893ad4599aSBarry Smith     Output Parameter:
12903ad4599aSBarry Smith .  mat - the restriction
12913ad4599aSBarry Smith 
12923ad4599aSBarry Smith     Level: developer
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith     Note:
1295bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1296bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12973ad4599aSBarry Smith 
1298bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12993ad4599aSBarry Smith @*/
1300d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1301d71ae5a4SJacob Faibussowitsch {
13023ad4599aSBarry Smith   PetscFunctionBegin;
1303a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1304a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13055a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1307dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13093ad4599aSBarry Smith   PetscFunctionReturn(0);
13103ad4599aSBarry Smith }
13113ad4599aSBarry Smith 
131247c6ae99SBarry Smith /*@
1313bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1314bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1315bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1316bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131747c6ae99SBarry Smith 
1318a5bc1bf3SBarry Smith     Collective on dac
131947c6ae99SBarry Smith 
1320d8d19677SJose E. Roman     Input Parameters:
1321bb7acecfSBarry Smith +   dac - the `DM` object
1322bb7acecfSBarry Smith -   daf - the second, finer `DM` object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
13256dbf9973SLawrence Mitchell .   mat - the injection
132647c6ae99SBarry Smith 
132747c6ae99SBarry Smith     Level: developer
132847c6ae99SBarry Smith 
1329bb7acecfSBarry Smith    Note:
1330bb7acecfSBarry 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
1331bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
133285afcc9aSBarry Smith 
1333bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1334bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133547c6ae99SBarry Smith @*/
1336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1337d71ae5a4SJacob Faibussowitsch {
133847c6ae99SBarry Smith   PetscFunctionBegin;
1339a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1340a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13415a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13429566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1343dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
134547c6ae99SBarry Smith   PetscFunctionReturn(0);
134647c6ae99SBarry Smith }
134747c6ae99SBarry Smith 
1348b412c318SBarry Smith /*@
1349bb7acecfSBarry 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
1350bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1351bd041c0cSMatthew G. Knepley 
1352a5bc1bf3SBarry Smith   Collective on dac
1353bd041c0cSMatthew G. Knepley 
1354d8d19677SJose E. Roman   Input Parameters:
1355bb7acecfSBarry Smith + dmc - the target `DM` object
1356bb7acecfSBarry Smith - dmf - the source `DM` object
1357bd041c0cSMatthew G. Knepley 
1358bd041c0cSMatthew G. Knepley   Output Parameter:
1359b4937a87SMatthew G. Knepley . mat - the mass matrix
1360bd041c0cSMatthew G. Knepley 
1361bd041c0cSMatthew G. Knepley   Level: developer
1362bd041c0cSMatthew G. Knepley 
1363bb7acecfSBarry Smith   Notes:
1364bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1365bb7acecfSBarry Smith 
1366bb7acecfSBarry Smith   if dmc is dmf then x^t M x is an approximation to the L2 norm of the vector x which is obtained by `DMCreateGlobalVector()`
1367bb7acecfSBarry Smith 
1368bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1369bd041c0cSMatthew G. Knepley @*/
1370d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1371d71ae5a4SJacob Faibussowitsch {
1372bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1373b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1374b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13755a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13765b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1377dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13785b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
1379b4937a87SMatthew G. Knepley   PetscFunctionReturn(0);
1380b4937a87SMatthew G. Knepley }
1381b4937a87SMatthew G. Knepley 
1382b4937a87SMatthew G. Knepley /*@
1383bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1384b4937a87SMatthew G. Knepley 
1385b4937a87SMatthew G. Knepley   Collective on dm
1386b4937a87SMatthew G. Knepley 
1387b4937a87SMatthew G. Knepley   Input Parameter:
1388bb7acecfSBarry Smith . dm - the `DM` object
1389b4937a87SMatthew G. Knepley 
1390b4937a87SMatthew G. Knepley   Output Parameter:
1391bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1392b4937a87SMatthew G. Knepley 
1393b4937a87SMatthew G. Knepley   Level: developer
1394b4937a87SMatthew G. Knepley 
1395bb7acecfSBarry Smith   Note:
1396bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1397bb7acecfSBarry Smith 
1398bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1399b4937a87SMatthew G. Knepley @*/
1400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1401d71ae5a4SJacob Faibussowitsch {
1402b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1403b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1404b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1405dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
1406bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1407bd041c0cSMatthew G. Knepley }
1408bd041c0cSMatthew G. Knepley 
1409bd041c0cSMatthew G. Knepley /*@
1410bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1411bb7acecfSBarry Smith     of a PDE on the `DM`.
141247c6ae99SBarry Smith 
1413d083f849SBarry Smith     Collective on dm
141447c6ae99SBarry Smith 
1415d8d19677SJose E. Roman     Input Parameters:
1416bb7acecfSBarry Smith +   dm - the `DM` object
1417bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141847c6ae99SBarry Smith 
141947c6ae99SBarry Smith     Output Parameter:
142047c6ae99SBarry Smith .   coloring - the coloring
142147c6ae99SBarry Smith 
1422ec5066bdSBarry Smith     Notes:
1423bb7acecfSBarry 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
1424bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1425ec5066bdSBarry Smith 
1426bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
1427ec5066bdSBarry Smith 
142847c6ae99SBarry Smith     Level: developer
142947c6ae99SBarry Smith 
1430bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1431aab9d709SJed Brown @*/
1432d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1433d71ae5a4SJacob Faibussowitsch {
143447c6ae99SBarry Smith   PetscFunctionBegin;
1435171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14365a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1437dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
143847c6ae99SBarry Smith   PetscFunctionReturn(0);
143947c6ae99SBarry Smith }
144047c6ae99SBarry Smith 
1441b412c318SBarry Smith /*@
1442bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144347c6ae99SBarry Smith 
1444d083f849SBarry Smith     Collective on dm
144547c6ae99SBarry Smith 
144647c6ae99SBarry Smith     Input Parameter:
1447bb7acecfSBarry Smith .   dm - the `DM` object
144847c6ae99SBarry Smith 
144947c6ae99SBarry Smith     Output Parameter:
145047c6ae99SBarry Smith .   mat - the empty Jacobian
145147c6ae99SBarry Smith 
1452073dac72SJed Brown     Level: beginner
145347c6ae99SBarry Smith 
1454f27dd7c6SMatthew G. Knepley     Options Database Keys:
1455bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1456f27dd7c6SMatthew G. Knepley 
145795452b02SPatrick Sanan     Notes:
145895452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145994013140SBarry Smith     do not need to do it yourself.
146094013140SBarry Smith 
146194013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1462bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146394013140SBarry Smith 
1464bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146594013140SBarry Smith     internally by PETSc.
146694013140SBarry Smith 
1467bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1468bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146994013140SBarry Smith 
1470bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1471aab9d709SJed Brown @*/
1472d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1473d71ae5a4SJacob Faibussowitsch {
147447c6ae99SBarry Smith   PetscFunctionBegin;
1475171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1476064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14779566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1479dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
148076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1481c6b011d8SStefano Zampini     DM mdm;
1482c6b011d8SStefano Zampini 
14839566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14847a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1485c6b011d8SStefano Zampini   }
1486e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1487e5e52638SMatthew G. Knepley   if (dm->Nf) {
1488e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1489649ef022SMatthew Knepley     PetscInt     Nf, f;
1490e571a35bSMatthew G. Knepley 
14919566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1492649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1493649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14949566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14959566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14969566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1497649ef022SMatthew Knepley         break;
1498e571a35bSMatthew G. Knepley       }
1499649ef022SMatthew Knepley     }
1500649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1501649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15029566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15039566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15049566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1505e571a35bSMatthew G. Knepley       }
1506e571a35bSMatthew G. Knepley     }
1507e571a35bSMatthew G. Knepley   }
15089566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
150947c6ae99SBarry Smith   PetscFunctionReturn(0);
151047c6ae99SBarry Smith }
151147c6ae99SBarry Smith 
1512732e2eb9SMatthew G Knepley /*@
1513bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1514bb7acecfSBarry Smith   properly set, but the data structures to store values in the matrices will not be preallocated. This is most useful to reduce initialization costs when
1515bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1516aa0f6e3cSJed Brown 
1517aa0f6e3cSJed Brown   Logically Collective on dm
1518aa0f6e3cSJed Brown 
1519aa0f6e3cSJed Brown   Input Parameters:
1520bb7acecfSBarry Smith + dm - the `DM`
1521bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1522aa0f6e3cSJed Brown 
1523aa0f6e3cSJed Brown   Level: developer
1524aa0f6e3cSJed Brown 
1525bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1526aa0f6e3cSJed Brown @*/
1527d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1528d71ae5a4SJacob Faibussowitsch {
1529aa0f6e3cSJed Brown   PetscFunctionBegin;
1530aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1531aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
1532aa0f6e3cSJed Brown   PetscFunctionReturn(0);
1533aa0f6e3cSJed Brown }
1534aa0f6e3cSJed Brown 
1535aa0f6e3cSJed Brown /*@
1536bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1537732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1538732e2eb9SMatthew G Knepley 
1539d083f849SBarry Smith   Logically Collective on dm
1540732e2eb9SMatthew G Knepley 
1541d8d19677SJose E. Roman   Input Parameters:
1542bb7acecfSBarry Smith + dm - the `DM`
1543bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1544732e2eb9SMatthew G Knepley 
1545732e2eb9SMatthew G Knepley   Level: developer
1546f27dd7c6SMatthew G. Knepley 
1547f27dd7c6SMatthew G. Knepley   Options Database Keys:
1548bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1549f27dd7c6SMatthew G. Knepley 
1550bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1551732e2eb9SMatthew G Knepley @*/
1552d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1553d71ae5a4SJacob Faibussowitsch {
1554732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1555732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1556732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1557732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1558732e2eb9SMatthew G Knepley }
1559732e2eb9SMatthew G Knepley 
1560b06ff27eSHong Zhang /*@
1561bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1562bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1563b06ff27eSHong Zhang 
1564d083f849SBarry Smith   Logically Collective on dm
1565b06ff27eSHong Zhang 
1566d8d19677SJose E. Roman   Input Parameters:
1567bb7acecfSBarry Smith + dm - the `DM`
1568bb7acecfSBarry Smith - only - `PETSC_TRUE` if you only want matrix stucture
1569b06ff27eSHong Zhang 
1570b06ff27eSHong Zhang   Level: developer
1571bb7acecfSBarry Smith 
1572bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1573b06ff27eSHong Zhang @*/
1574d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1575d71ae5a4SJacob Faibussowitsch {
1576b06ff27eSHong Zhang   PetscFunctionBegin;
1577b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1578b06ff27eSHong Zhang   dm->structure_only = only;
1579b06ff27eSHong Zhang   PetscFunctionReturn(0);
1580b06ff27eSHong Zhang }
1581b06ff27eSHong Zhang 
1582*863027abSJed Brown /*@
1583*863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1584*863027abSJed Brown 
1585*863027abSJed Brown   Logically Collective on dm
1586*863027abSJed Brown 
1587*863027abSJed Brown   Input Parameters:
1588*863027abSJed Brown + dm - the `DM`
1589*863027abSJed Brown - btype - block by topological point or field node
1590*863027abSJed Brown 
1591*863027abSJed Brown   Level: advanced
1592*863027abSJed Brown 
1593*863027abSJed Brown   Options Database Keys:
1594*863027abSJed Brown . -dm_blocking_type [point, field_node] - use topological point blocking or field node blocking
1595*863027abSJed Brown 
1596*863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1597*863027abSJed Brown @*/
1598*863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1599*863027abSJed Brown {
1600*863027abSJed Brown   PetscFunctionBegin;
1601*863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1602*863027abSJed Brown   dm->blocking_type = btype;
1603*863027abSJed Brown   PetscFunctionReturn(0);
1604*863027abSJed Brown }
1605*863027abSJed Brown 
1606*863027abSJed Brown /*@
1607*863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1608*863027abSJed Brown 
1609*863027abSJed Brown   Not Collective
1610*863027abSJed Brown 
1611*863027abSJed Brown   Input Parameters:
1612*863027abSJed Brown . dm - the `DM`
1613*863027abSJed Brown 
1614*863027abSJed Brown   Output Parameters:
1615*863027abSJed Brown . btype - block by topological point or field node
1616*863027abSJed Brown 
1617*863027abSJed Brown   Level: advanced
1618*863027abSJed Brown 
1619*863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1620*863027abSJed Brown @*/
1621*863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1622*863027abSJed Brown {
1623*863027abSJed Brown   PetscFunctionBegin;
1624*863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1625*863027abSJed Brown   PetscValidPointer(btype, 2);
1626*863027abSJed Brown   *btype = dm->blocking_type;
1627*863027abSJed Brown   PetscFunctionReturn(0);
1628*863027abSJed Brown }
1629*863027abSJed Brown 
1630a89ea682SMatthew G Knepley /*@C
1631bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1632a89ea682SMatthew G Knepley 
1633a89ea682SMatthew G Knepley   Not Collective
1634a89ea682SMatthew G Knepley 
1635a89ea682SMatthew G Knepley   Input Parameters:
1636bb7acecfSBarry Smith + dm - the `DM` object
1637a5b23f4aSJose E. Roman . count - The minimum size
1638bb7acecfSBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, or MPIU_INT)
1639a89ea682SMatthew G Knepley 
1640a89ea682SMatthew G Knepley   Output Parameter:
1641a89ea682SMatthew G Knepley . array - the work array
1642a89ea682SMatthew G Knepley 
1643a89ea682SMatthew G Knepley   Level: developer
1644a89ea682SMatthew G Knepley 
1645bb7acecfSBarry Smith   Note:
1646bb7acecfSBarry Smith   A `DM` may stash the array between instantations so using this routine may be more efficient than calling `PetscMalloc()`
1647bb7acecfSBarry Smith 
1648bb7acecfSBarry Smith   The array may contain nonzero values
1649bb7acecfSBarry Smith 
1650bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1651a89ea682SMatthew G Knepley @*/
1652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1653d71ae5a4SJacob Faibussowitsch {
1654aa1993deSMatthew G Knepley   DMWorkLink  link;
165569291d52SBarry Smith   PetscMPIInt dsize;
1656a89ea682SMatthew G Knepley 
1657a89ea682SMatthew G Knepley   PetscFunctionBegin;
1658a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1659aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1660aa1993deSMatthew G Knepley   if (dm->workin) {
1661aa1993deSMatthew G Knepley     link       = dm->workin;
1662aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1663aa1993deSMatthew G Knepley   } else {
16644dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1665a89ea682SMatthew G Knepley   }
16669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
16675056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16689566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16699566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1670854ce69bSBarry Smith     link->bytes = dsize * count;
1671aa1993deSMatthew G Knepley   }
1672aa1993deSMatthew G Knepley   link->next  = dm->workout;
1673aa1993deSMatthew G Knepley   dm->workout = link;
1674cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
167500d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
167600d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
167700d952a4SJed Brown #endif
1678aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
1679a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1680a89ea682SMatthew G Knepley }
1681a89ea682SMatthew G Knepley 
1682aa1993deSMatthew G Knepley /*@C
1683bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1684aa1993deSMatthew G Knepley 
1685aa1993deSMatthew G Knepley   Not Collective
1686aa1993deSMatthew G Knepley 
1687aa1993deSMatthew G Knepley   Input Parameters:
1688bb7acecfSBarry Smith + dm - the `DM` object
1689a5b23f4aSJose E. Roman . count - The minimum size
169069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1691aa1993deSMatthew G Knepley 
1692aa1993deSMatthew G Knepley   Output Parameter:
1693aa1993deSMatthew G Knepley . array - the work array
1694aa1993deSMatthew G Knepley 
1695aa1993deSMatthew G Knepley   Level: developer
1696aa1993deSMatthew G Knepley 
169795452b02SPatrick Sanan   Developer Notes:
1698bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1699147403d9SBarry Smith 
1700bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1701aa1993deSMatthew G Knepley @*/
1702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1703d71ae5a4SJacob Faibussowitsch {
1704aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1705aa1993deSMatthew G Knepley 
1706aa1993deSMatthew G Knepley   PetscFunctionBegin;
1707aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1708aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1709aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1710aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1711aa1993deSMatthew G Knepley       *p            = link->next;
1712aa1993deSMatthew G Knepley       link->next    = dm->workin;
1713aa1993deSMatthew G Knepley       dm->workin    = link;
17140298fd71SBarry Smith       *(void **)mem = NULL;
1715aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1716aa1993deSMatthew G Knepley     }
1717aa1993deSMatthew G Knepley   }
1718aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1719aa1993deSMatthew G Knepley }
1720e7c4fc90SDmitry Karpeev 
17218cda7954SMatthew G. Knepley /*@C
1722bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1723bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17248cda7954SMatthew G. Knepley 
1725bb7acecfSBarry Smith   Logically collective on dm
17268cda7954SMatthew G. Knepley 
17278cda7954SMatthew G. Knepley   Input Parameters:
1728bb7acecfSBarry Smith + dm     - The `DM`
17298cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17308cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17318cda7954SMatthew G. Knepley 
1732147403d9SBarry Smith   Calling sequence of nullsp:
1733147403d9SBarry Smith .vb
1734147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1735147403d9SBarry Smith .ve
1736bb7acecfSBarry Smith +  dm        - The present `DM`
1737bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1738147403d9SBarry Smith .  field     - The field number in dm
1739147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17408cda7954SMatthew G. Knepley 
174149762cbcSSatish Balay   Level: intermediate
174249762cbcSSatish Balay 
1743bb7acecfSBarry Smith   Fortran Notes:
1744bb7acecfSBarry Smith   This function is not available from Fortran.
1745bb7acecfSBarry Smith 
1746bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1747147403d9SBarry Smith @*/
1748d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1749d71ae5a4SJacob Faibussowitsch {
1750435a35e8SMatthew G Knepley   PetscFunctionBegin;
1751435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17527a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1753435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1754435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1755435a35e8SMatthew G Knepley }
1756435a35e8SMatthew G Knepley 
17578cda7954SMatthew G. Knepley /*@C
1758bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17598cda7954SMatthew G. Knepley 
17608cda7954SMatthew G. Knepley   Not collective
17618cda7954SMatthew G. Knepley 
17628cda7954SMatthew G. Knepley   Input Parameters:
1763bb7acecfSBarry Smith + dm     - The `DM`
17648cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17658cda7954SMatthew G. Knepley 
17668cda7954SMatthew G. Knepley   Output Parameter:
17678cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17688cda7954SMatthew G. Knepley 
1769147403d9SBarry Smith   Calling sequence of nullsp:
1770147403d9SBarry Smith .vb
1771147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1772147403d9SBarry Smith .ve
1773147403d9SBarry Smith +  dm        - The present DM
1774147403d9SBarry Smith .  origField - The field number given above, in the original DM
1775147403d9SBarry Smith .  field     - The field number in dm
1776147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17778cda7954SMatthew G. Knepley 
1778bb7acecfSBarry Smith   Fortran Note:
1779bb7acecfSBarry Smith   This function is not available from Fortran.
17808cda7954SMatthew G. Knepley 
178149762cbcSSatish Balay    Level: intermediate
178249762cbcSSatish Balay 
1783bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1784147403d9SBarry Smith @*/
1785d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1786d71ae5a4SJacob Faibussowitsch {
17870a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17880a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1789f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17907a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17910a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17920a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
17930a50eb56SMatthew G. Knepley }
17940a50eb56SMatthew G. Knepley 
17958cda7954SMatthew G. Knepley /*@C
1796bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17978cda7954SMatthew G. Knepley 
1798bb7acecfSBarry Smith   Logically collective on dm
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 - nullsp - A callback to create the near-nullspace
18048cda7954SMatthew G. Knepley 
1805147403d9SBarry Smith   Calling sequence of nullsp:
1806147403d9SBarry Smith .vb
1807147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1808147403d9SBarry Smith .ve
1809bb7acecfSBarry Smith +  dm        - The present `DM`
1810bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1811147403d9SBarry Smith .  field     - The field number in dm
1812147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18138cda7954SMatthew G. Knepley 
1814bb7acecfSBarry Smith   Fortran Note:
1815bb7acecfSBarry Smith   This function is not available from Fortran.
18168cda7954SMatthew G. Knepley 
181749762cbcSSatish Balay    Level: intermediate
181849762cbcSSatish Balay 
1819bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1820bb7acecfSBarry Smith           `MatNullSpace`
1821147403d9SBarry Smith @*/
1822d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1823d71ae5a4SJacob Faibussowitsch {
1824f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1825f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18267a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1827f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1828f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1829f9d4088aSMatthew G. Knepley }
1830f9d4088aSMatthew G. Knepley 
18318cda7954SMatthew G. Knepley /*@C
1832bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18338cda7954SMatthew G. Knepley 
18348cda7954SMatthew G. Knepley   Not collective
18358cda7954SMatthew G. Knepley 
18368cda7954SMatthew G. Knepley   Input Parameters:
1837bb7acecfSBarry Smith + dm     - The `DM`
18388cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18398cda7954SMatthew G. Knepley 
18408cda7954SMatthew G. Knepley   Output Parameter:
18418cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18428cda7954SMatthew G. Knepley 
1843147403d9SBarry Smith   Calling sequence of nullsp:
1844147403d9SBarry Smith .vb
1845147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1846147403d9SBarry Smith .ve
1847bb7acecfSBarry Smith +  dm        - The present `DM`
1848bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1849147403d9SBarry Smith .  field     - The field number in dm
1850147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18518cda7954SMatthew G. Knepley 
1852bb7acecfSBarry Smith   Fortran Note:
1853bb7acecfSBarry Smith   This function is not available from Fortran.
18548cda7954SMatthew G. Knepley 
185549762cbcSSatish Balay    Level: intermediate
185649762cbcSSatish Balay 
1857bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1858bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1859147403d9SBarry Smith @*/
1860d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1861d71ae5a4SJacob Faibussowitsch {
1862f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1863f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1864f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18657a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1866f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1867f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1868f9d4088aSMatthew G. Knepley }
1869f9d4088aSMatthew G. Knepley 
18704f3b5142SJed Brown /*@C
1871bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18724d343eeaSMatthew G Knepley 
18734d343eeaSMatthew G Knepley   Not collective
18744d343eeaSMatthew G Knepley 
18754d343eeaSMatthew G Knepley   Input Parameter:
1876bb7acecfSBarry Smith . dm - the `DM` object
18774d343eeaSMatthew G Knepley 
18784d343eeaSMatthew G Knepley   Output Parameters:
18790298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
1880bb7acecfSBarry Smith . fieldNames - The number of each field (or NULL if not requested)
18810298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
18824d343eeaSMatthew G Knepley 
18834d343eeaSMatthew G Knepley   Level: intermediate
18844d343eeaSMatthew G Knepley 
1885bb7acecfSBarry Smith   Note:
188621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1887bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1888bb7acecfSBarry Smith   `PetscFree()`.
188921c9b008SJed Brown 
1890bb7acecfSBarry Smith   Fortran Note:
1891bb7acecfSBarry Smith   Not available in Fortran.
1892bb7acecfSBarry Smith 
1893bb7acecfSBarry Smith   Developer Note:
1894bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1895bb7acecfSBarry Smith   likely be removed.
1896bb7acecfSBarry Smith 
1897bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1898bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18994d343eeaSMatthew G Knepley @*/
1900d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1901d71ae5a4SJacob Faibussowitsch {
190237d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
19034d343eeaSMatthew G Knepley 
19044d343eeaSMatthew G Knepley   PetscFunctionBegin;
19054d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190669ca1f37SDmitry Karpeev   if (numFields) {
1907534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
190869ca1f37SDmitry Karpeev     *numFields = 0;
190969ca1f37SDmitry Karpeev   }
191037d0c07bSMatthew G Knepley   if (fieldNames) {
191137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
19120298fd71SBarry Smith     *fieldNames = NULL;
191369ca1f37SDmitry Karpeev   }
191469ca1f37SDmitry Karpeev   if (fields) {
191569ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
19160298fd71SBarry Smith     *fields = NULL;
191769ca1f37SDmitry Karpeev   }
19189566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
191937d0c07bSMatthew G Knepley   if (section) {
19203a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
192137d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
192237d0c07bSMatthew G Knepley 
19239566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19259566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
192737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
192837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
193037d0c07bSMatthew G Knepley     }
193137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193237d0c07bSMatthew G Knepley       PetscInt gdof;
193337d0c07bSMatthew G Knepley 
19349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
193537d0c07bSMatthew G Knepley       if (gdof > 0) {
193637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19373a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
193837d0c07bSMatthew G Knepley 
19399566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19409566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19413a544194SStefano Zampini           fpdof = fdof - fcdof;
19423a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19433a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19443a544194SStefano Zampini             fieldNc[f] = 1;
19453a544194SStefano Zampini           }
19463a544194SStefano Zampini           fieldSizes[f] += fpdof;
194737d0c07bSMatthew G Knepley         }
194837d0c07bSMatthew G Knepley       }
194937d0c07bSMatthew G Knepley     }
195037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
195237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
195337d0c07bSMatthew G Knepley     }
195437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
195537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
195637d0c07bSMatthew G Knepley 
19579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
195837d0c07bSMatthew G Knepley       if (gdof > 0) {
19599566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
196037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
196137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
196237d0c07bSMatthew G Knepley 
19639566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19649566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1965ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
196637d0c07bSMatthew G Knepley         }
196737d0c07bSMatthew G Knepley       }
196837d0c07bSMatthew G Knepley     }
19698865f1eaSKarl Rupp     if (numFields) *numFields = nF;
197037d0c07bSMatthew G Knepley     if (fieldNames) {
19719566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
197237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
197337d0c07bSMatthew G Knepley         const char *fieldName;
197437d0c07bSMatthew G Knepley 
19759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19769566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
197737d0c07bSMatthew G Knepley       }
197837d0c07bSMatthew G Knepley     }
197937d0c07bSMatthew G Knepley     if (fields) {
19809566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
198137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19823a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19833a544194SStefano Zampini 
19849566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19853a544194SStefano Zampini         in[0] = -fieldNc[f];
19863a544194SStefano Zampini         in[1] = fieldNc[f];
19871c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19883a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19899566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
199037d0c07bSMatthew G Knepley       }
199137d0c07bSMatthew G Knepley     }
19929566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1993dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19944d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
19954d343eeaSMatthew G Knepley }
19964d343eeaSMatthew G Knepley 
199716621825SDmitry Karpeev /*@C
1998bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1999bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
2000bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
2001bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
2002e7c4fc90SDmitry Karpeev 
2003e7c4fc90SDmitry Karpeev   Not collective
2004e7c4fc90SDmitry Karpeev 
2005e7c4fc90SDmitry Karpeev   Input Parameter:
2006bb7acecfSBarry Smith . dm - the `DM` object
2007e7c4fc90SDmitry Karpeev 
2008e7c4fc90SDmitry Karpeev   Output Parameters:
2009bb7acecfSBarry Smith + len       - The number of fields (or NULL if not requested)
20100298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
20110298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
2012bb7acecfSBarry Smith - dmlist    - The `DM`s for each field subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
2013e7c4fc90SDmitry Karpeev 
2014e7c4fc90SDmitry Karpeev   Level: intermediate
2015e7c4fc90SDmitry Karpeev 
2016bb7acecfSBarry Smith   Note:
2017e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2018bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2019bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2020e7c4fc90SDmitry Karpeev 
2021bb7acecfSBarry Smith   Fortran Note:
2022bb7acecfSBarry Smith   Not available in Fortran.
2023bb7acecfSBarry Smith 
2024bb7acecfSBarry Smith   Developer Note:
2025bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2026bb7acecfSBarry Smith 
2027bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2028e7c4fc90SDmitry Karpeev @*/
2029d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2030d71ae5a4SJacob Faibussowitsch {
2031e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2032e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20338865f1eaSKarl Rupp   if (len) {
2034534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
20358865f1eaSKarl Rupp     *len = 0;
20368865f1eaSKarl Rupp   }
20378865f1eaSKarl Rupp   if (namelist) {
20388865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
2039ea78f98cSLisandro Dalcin     *namelist = NULL;
20408865f1eaSKarl Rupp   }
20418865f1eaSKarl Rupp   if (islist) {
20428865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
2043ea78f98cSLisandro Dalcin     *islist = NULL;
20448865f1eaSKarl Rupp   }
20458865f1eaSKarl Rupp   if (dmlist) {
20468865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
2047ea78f98cSLisandro Dalcin     *dmlist = NULL;
20488865f1eaSKarl Rupp   }
2049f3f0edfdSDmitry Karpeev   /*
2050f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2051f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2052f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2053f3f0edfdSDmitry Karpeev    */
20547a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
205516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2056435a35e8SMatthew G Knepley     PetscSection section;
2057435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2058435a35e8SMatthew G Knepley 
20599566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20609566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2061435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2062f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20639566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20649566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20659566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2066435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2067435a35e8SMatthew G Knepley         const char *fieldName;
2068435a35e8SMatthew G Knepley 
20699566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
207003dc3394SMatthew G. Knepley         if (namelist) {
20719566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20729566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2073435a35e8SMatthew G Knepley         }
207403dc3394SMatthew G. Knepley       }
2075435a35e8SMatthew G Knepley     } else {
20769566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2077e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20780298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2079e7c4fc90SDmitry Karpeev     }
2080dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
208116621825SDmitry Karpeev   PetscFunctionReturn(0);
208216621825SDmitry Karpeev }
208316621825SDmitry Karpeev 
2084412a4547SAlexis Marboeuf /*@C
2085412a4547SAlexis Marboeuf   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
2086412a4547SAlexis Marboeuf                   The fields are defined by DMCreateFieldIS().
2087435a35e8SMatthew G Knepley 
2088435a35e8SMatthew G Knepley   Not collective
2089435a35e8SMatthew G Knepley 
2090435a35e8SMatthew G Knepley   Input Parameters:
2091bb7acecfSBarry Smith + dm        - The `DM `object
2092bb7acecfSBarry Smith . numFields - The number of fields to select
20932adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2094435a35e8SMatthew G Knepley 
2095435a35e8SMatthew G Knepley   Output Parameters:
2096bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2097bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2098435a35e8SMatthew G Knepley 
2099bb7acecfSBarry Smith   Note:
2100bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21015d3b26e6SMatthew G. Knepley 
2102435a35e8SMatthew G Knepley   Level: intermediate
2103435a35e8SMatthew G Knepley 
2104bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2105435a35e8SMatthew G Knepley @*/
2106d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2107d71ae5a4SJacob Faibussowitsch {
2108435a35e8SMatthew G Knepley   PetscFunctionBegin;
2109435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2110dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
21118865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
21128865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2113dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
2114435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
2115435a35e8SMatthew G Knepley }
2116435a35e8SMatthew G Knepley 
21172adcc780SMatthew G. Knepley /*@C
2118bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
21192adcc780SMatthew G. Knepley 
21202adcc780SMatthew G. Knepley   Not collective
21212adcc780SMatthew G. Knepley 
2122d8d19677SJose E. Roman   Input Parameters:
2123bb7acecfSBarry Smith + dms - The `DM` objects
2124bb7acecfSBarry Smith - n - The number of `DM`s
21252adcc780SMatthew G. Knepley 
21262adcc780SMatthew G. Knepley   Output Parameters:
2127bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2128bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21292adcc780SMatthew G. Knepley 
2130bb7acecfSBarry Smith   Note:
2131bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21325d3b26e6SMatthew G. Knepley 
21332adcc780SMatthew G. Knepley   Level: intermediate
21342adcc780SMatthew G. Knepley 
2135bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21362adcc780SMatthew G. Knepley @*/
2137d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2138d71ae5a4SJacob Faibussowitsch {
21392adcc780SMatthew G. Knepley   PetscInt i;
21402adcc780SMatthew G. Knepley 
21412adcc780SMatthew G. Knepley   PetscFunctionBegin;
21422adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2143ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21442adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2145a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2146bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2147bb7acecfSBarry Smith   if (n) {
2148b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2149dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21502adcc780SMatthew G. Knepley   }
21512adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
21522adcc780SMatthew G. Knepley }
21532adcc780SMatthew G. Knepley 
215416621825SDmitry Karpeev /*@C
2155bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2156bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2157bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2158bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2159bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
216016621825SDmitry Karpeev 
216116621825SDmitry Karpeev   Not collective
216216621825SDmitry Karpeev 
216316621825SDmitry Karpeev   Input Parameter:
2164bb7acecfSBarry Smith . dm - the `DM` object
216516621825SDmitry Karpeev 
216616621825SDmitry Karpeev   Output Parameters:
2167bb7acecfSBarry Smith + n            - The number of subproblems in the domain decomposition (or NULL if not requested)
21680298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
21690298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21700298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2171bb7acecfSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no `DM`s are defined)
217216621825SDmitry Karpeev 
217316621825SDmitry Karpeev   Level: intermediate
217416621825SDmitry Karpeev 
2175bb7acecfSBarry Smith   Note:
217616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2177bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2178bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
217916621825SDmitry Karpeev 
2180bb7acecfSBarry Smith   Questions:
2181bb7acecfSBarry Smith   The dmlist is for the inner subdomains or the outer subdomains or all subdomains?
2182bb7acecfSBarry Smith 
2183bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
218416621825SDmitry Karpeev @*/
2185d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2186d71ae5a4SJacob Faibussowitsch {
2187be081cd6SPeter Brune   DMSubDomainHookLink link;
2188be081cd6SPeter Brune   PetscInt            i, l;
218916621825SDmitry Karpeev 
219016621825SDmitry Karpeev   PetscFunctionBegin;
219116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21929371c9d4SSatish Balay   if (n) {
21939371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21949371c9d4SSatish Balay     *n = 0;
21959371c9d4SSatish Balay   }
21969371c9d4SSatish Balay   if (namelist) {
21979371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21989371c9d4SSatish Balay     *namelist = NULL;
21999371c9d4SSatish Balay   }
22009371c9d4SSatish Balay   if (innerislist) {
22019371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
22029371c9d4SSatish Balay     *innerislist = NULL;
22039371c9d4SSatish Balay   }
22049371c9d4SSatish Balay   if (outerislist) {
22059371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
22069371c9d4SSatish Balay     *outerislist = NULL;
22079371c9d4SSatish Balay   }
22089371c9d4SSatish Balay   if (dmlist) {
22099371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
22109371c9d4SSatish Balay     *dmlist = NULL;
22119371c9d4SSatish Balay   }
2212f3f0edfdSDmitry Karpeev   /*
2213f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2214f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2215f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2216f3f0edfdSDmitry Karpeev    */
22177a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
221816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2219dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
222014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2221f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2222be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2223be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22249566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2225be081cd6SPeter Brune         }
2226648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2227e7c4fc90SDmitry Karpeev       }
222814a18fd3SPeter Brune     }
2229bb7acecfSBarry Smith     if (n) *n = l;
223014a18fd3SPeter Brune   }
2231e30e807fSPeter Brune   PetscFunctionReturn(0);
2232e30e807fSPeter Brune }
2233e30e807fSPeter Brune 
2234e30e807fSPeter Brune /*@C
2235e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2236e30e807fSPeter Brune 
2237e30e807fSPeter Brune   Not collective
2238e30e807fSPeter Brune 
2239e30e807fSPeter Brune   Input Parameters:
2240bb7acecfSBarry Smith + dm - the `DM` object
2241e30e807fSPeter Brune . n  - the number of subdomain scatters
2242e30e807fSPeter Brune - subdms - the local subdomains
2243e30e807fSPeter Brune 
2244e30e807fSPeter Brune   Output Parameters:
22456b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2246e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2247e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2248e30e807fSPeter Brune 
2249bb7acecfSBarry Smith   Note:
2250bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2251e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2252e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2253e30e807fSPeter Brune   solution and residual data.
2254e30e807fSPeter Brune 
2255bb7acecfSBarry Smith   Questions:
2256bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2257bb7acecfSBarry Smith 
2258e30e807fSPeter Brune   Level: developer
2259e30e807fSPeter Brune 
2260bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2261e30e807fSPeter Brune @*/
2262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2263d71ae5a4SJacob Faibussowitsch {
2264e30e807fSPeter Brune   PetscFunctionBegin;
2265e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2266e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2267dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
2268e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2269e7c4fc90SDmitry Karpeev }
2270e7c4fc90SDmitry Karpeev 
227147c6ae99SBarry Smith /*@
2272bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
227347c6ae99SBarry Smith 
2274d083f849SBarry Smith   Collective on dm
227547c6ae99SBarry Smith 
2276d8d19677SJose E. Roman   Input Parameters:
2277bb7acecfSBarry Smith + dm   - the `DM` object
2278bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
227947c6ae99SBarry Smith 
228047c6ae99SBarry Smith   Output Parameter:
2281bb7acecfSBarry Smith . dmf - the refined `D`M, or NULL
2282ae0a1c52SMatthew G Knepley 
2283f27dd7c6SMatthew G. Knepley   Options Database Keys:
2284412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2285412e9a14SMatthew G. Knepley 
2286bb7acecfSBarry Smith   Note:
2287bb7acecfSBarry Smith   If no refinement was done, the return value is NULL
228847c6ae99SBarry Smith 
228947c6ae99SBarry Smith   Level: developer
229047c6ae99SBarry Smith 
2291bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
229247c6ae99SBarry Smith @*/
2293d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2294d71ae5a4SJacob Faibussowitsch {
2295c833c3b5SJed Brown   DMRefineHookLink link;
229647c6ae99SBarry Smith 
229747c6ae99SBarry Smith   PetscFunctionBegin;
2298732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22999566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2300dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
23014057135bSMatthew G Knepley   if (*dmf) {
230243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
23038865f1eaSKarl Rupp 
23049566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
23058865f1eaSKarl Rupp 
2306644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
23070598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2308656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
23098865f1eaSKarl Rupp 
23109566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2311c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
23121baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2313c833c3b5SJed Brown     }
2314c833c3b5SJed Brown   }
23159566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
2316c833c3b5SJed Brown   PetscFunctionReturn(0);
2317c833c3b5SJed Brown }
2318c833c3b5SJed Brown 
2319bb9467b5SJed Brown /*@C
2320c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2321c833c3b5SJed Brown 
2322bb7acecfSBarry Smith    Logically Collective on coarse
2323c833c3b5SJed Brown 
23244165533cSJose E. Roman    Input Parameters:
2325bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2326bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2327bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
23280298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2329c833c3b5SJed Brown 
2330c833c3b5SJed Brown    Calling sequence of refinehook:
2331c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2332c833c3b5SJed Brown 
2333bb7acecfSBarry Smith +  coarse - coarse level `DM`
2334bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2335c833c3b5SJed Brown -  ctx - optional user-defined function context
2336c833c3b5SJed Brown 
2337c833c3b5SJed Brown    Calling sequence for interphook:
2338c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2339c833c3b5SJed Brown 
2340bb7acecfSBarry Smith +  coarse - coarse level `DM`
2341c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2342bb7acecfSBarry Smith .  fine - fine level `DM` to update
2343c833c3b5SJed Brown -  ctx - optional user-defined function context
2344c833c3b5SJed Brown 
2345c833c3b5SJed Brown    Level: advanced
2346c833c3b5SJed Brown 
2347c833c3b5SJed Brown    Notes:
2348bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2349bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2350bb7acecfSBarry Smith 
2351bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2352c833c3b5SJed Brown 
2353c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2354c833c3b5SJed Brown 
2355bb7acecfSBarry Smith    Fortran Note:
2356bb7acecfSBarry Smith    This function is not available from Fortran.
2357bb9467b5SJed Brown 
2358bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2359c833c3b5SJed Brown @*/
2360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2361d71ae5a4SJacob Faibussowitsch {
2362c833c3b5SJed Brown   DMRefineHookLink link, *p;
2363c833c3b5SJed Brown 
2364c833c3b5SJed Brown   PetscFunctionBegin;
2365c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23663d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23673d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
23683d8e3701SJed Brown   }
23699566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2370c833c3b5SJed Brown   link->refinehook = refinehook;
2371c833c3b5SJed Brown   link->interphook = interphook;
2372c833c3b5SJed Brown   link->ctx        = ctx;
23730298fd71SBarry Smith   link->next       = NULL;
2374c833c3b5SJed Brown   *p               = link;
2375c833c3b5SJed Brown   PetscFunctionReturn(0);
2376c833c3b5SJed Brown }
2377c833c3b5SJed Brown 
23783d8e3701SJed Brown /*@C
2379bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2380bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23813d8e3701SJed Brown 
2382bb7acecfSBarry Smith    Logically Collective on coarse
23833d8e3701SJed Brown 
23844165533cSJose E. Roman    Input Parameters:
2385bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2386bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2387bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
23883d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23893d8e3701SJed Brown 
23903d8e3701SJed Brown    Level: advanced
23913d8e3701SJed Brown 
2392bb7acecfSBarry Smith    Note:
23933d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23943d8e3701SJed Brown 
2395bb7acecfSBarry Smith    Fortran Note:
2396bb7acecfSBarry Smith    This function is not available from Fortran.
23973d8e3701SJed Brown 
2398bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23993d8e3701SJed Brown @*/
2400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2401d71ae5a4SJacob Faibussowitsch {
24023d8e3701SJed Brown   DMRefineHookLink link, *p;
24033d8e3701SJed Brown 
24043d8e3701SJed Brown   PetscFunctionBegin;
24053d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24063d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
24073d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
24083d8e3701SJed Brown       link = *p;
24093d8e3701SJed Brown       *p   = link->next;
24109566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
24113d8e3701SJed Brown       break;
24123d8e3701SJed Brown     }
24133d8e3701SJed Brown   }
24143d8e3701SJed Brown   PetscFunctionReturn(0);
24153d8e3701SJed Brown }
24163d8e3701SJed Brown 
2417c833c3b5SJed Brown /*@
2418bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2419c833c3b5SJed Brown 
2420c833c3b5SJed Brown    Collective if any hooks are
2421c833c3b5SJed Brown 
24224165533cSJose E. Roman    Input Parameters:
2423bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2424bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2425bb7acecfSBarry Smith -  fine - finer `DM` to update
2426c833c3b5SJed Brown 
2427c833c3b5SJed Brown    Level: developer
2428c833c3b5SJed Brown 
2429bb7acecfSBarry Smith    Developer Note:
2430bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2431bb7acecfSBarry Smith    an API with consistent terminology.
2432bb7acecfSBarry Smith 
2433bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2434c833c3b5SJed Brown @*/
2435d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2436d71ae5a4SJacob Faibussowitsch {
2437c833c3b5SJed Brown   DMRefineHookLink link;
2438c833c3b5SJed Brown 
2439c833c3b5SJed Brown   PetscFunctionBegin;
2440c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24411baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24424057135bSMatthew G Knepley   }
244347c6ae99SBarry Smith   PetscFunctionReturn(0);
244447c6ae99SBarry Smith }
244547c6ae99SBarry Smith 
2446eb3f98d2SBarry Smith /*@
24471f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24481f3379b2SToby Isaac 
2449bb7acecfSBarry Smith    Collective on dm
24501f3379b2SToby Isaac 
24514165533cSJose E. Roman    Input Parameters:
2452bb7acecfSBarry Smith +  coarse - coarse `DM`
2453bb7acecfSBarry Smith .  fine   - fine `DM`
2454bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2455bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2456bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24571f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24581f3379b2SToby Isaac 
24594165533cSJose E. Roman    Output Parameter:
24601f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24611f3379b2SToby Isaac 
24621f3379b2SToby Isaac    Level: developer
24631f3379b2SToby Isaac 
2464bb7acecfSBarry Smith    Note:
2465bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24661f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24671f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24681f3379b2SToby Isaac    slope-limiting reconstruction.
24691f3379b2SToby Isaac 
2470bb7acecfSBarry Smith    Developer Note:
2471bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2472bb7acecfSBarry Smith 
2473bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24741f3379b2SToby Isaac @*/
2475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2476d71ae5a4SJacob Faibussowitsch {
24771f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24781f3379b2SToby Isaac 
24791f3379b2SToby Isaac   PetscFunctionBegin;
24801f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24811f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24821f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24831f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24841f3379b2SToby Isaac 
24859566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24861f3379b2SToby Isaac   if (interpsol) {
24879566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24881f3379b2SToby Isaac   } else if (interp) {
24899566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
249098921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24911f3379b2SToby Isaac   PetscFunctionReturn(0);
24921f3379b2SToby Isaac }
24931f3379b2SToby Isaac 
24941f3379b2SToby Isaac /*@
2495bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2496eb3f98d2SBarry Smith 
2497eb3f98d2SBarry Smith     Not Collective
2498eb3f98d2SBarry Smith 
2499eb3f98d2SBarry Smith     Input Parameter:
2500bb7acecfSBarry Smith .   dm - the `DM` object
2501eb3f98d2SBarry Smith 
2502eb3f98d2SBarry Smith     Output Parameter:
2503eb3f98d2SBarry Smith .   level - number of refinements
2504eb3f98d2SBarry Smith 
2505eb3f98d2SBarry Smith     Level: developer
2506eb3f98d2SBarry Smith 
2507bb7acecfSBarry Smith     Note:
2508bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2509bb7acecfSBarry Smith 
2510bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2511eb3f98d2SBarry Smith @*/
2512d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2513d71ae5a4SJacob Faibussowitsch {
2514eb3f98d2SBarry Smith   PetscFunctionBegin;
2515eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2516eb3f98d2SBarry Smith   *level = dm->levelup;
2517eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2518eb3f98d2SBarry Smith }
2519eb3f98d2SBarry Smith 
2520fef3a512SBarry Smith /*@
2521bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2522fef3a512SBarry Smith 
2523fef3a512SBarry Smith     Not Collective
2524fef3a512SBarry Smith 
2525d8d19677SJose E. Roman     Input Parameters:
2526bb7acecfSBarry Smith +   dm - the `DM` object
2527fef3a512SBarry Smith -   level - number of refinements
2528fef3a512SBarry Smith 
2529fef3a512SBarry Smith     Level: advanced
2530fef3a512SBarry Smith 
253195452b02SPatrick Sanan     Notes:
2532bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2533fef3a512SBarry Smith 
2534bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2535bb7acecfSBarry Smith 
2536bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2537fef3a512SBarry Smith @*/
2538d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2539d71ae5a4SJacob Faibussowitsch {
2540fef3a512SBarry Smith   PetscFunctionBegin;
2541fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2542fef3a512SBarry Smith   dm->levelup = level;
2543fef3a512SBarry Smith   PetscFunctionReturn(0);
2544fef3a512SBarry Smith }
2545fef3a512SBarry Smith 
2546d410b0cfSMatthew G. Knepley /*@
2547bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2548d410b0cfSMatthew G. Knepley 
2549d410b0cfSMatthew G. Knepley   Collective on dm
2550d410b0cfSMatthew G. Knepley 
2551f1a722f8SMatthew G. Knepley   Input Parameters:
2552bb7acecfSBarry Smith + dm     - the `DM` object
2553d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2554d410b0cfSMatthew G. Knepley 
2555d410b0cfSMatthew G. Knepley   Output Parameter:
2556bb7acecfSBarry Smith . dme - the extruded `DM`, or NULL
2557d410b0cfSMatthew G. Knepley 
2558bb7acecfSBarry Smith   Note:
2559bb7acecfSBarry Smith   If no extrusion was done, the return value is NULL
2560d410b0cfSMatthew G. Knepley 
2561d410b0cfSMatthew G. Knepley   Level: developer
2562d410b0cfSMatthew G. Knepley 
2563bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2564d410b0cfSMatthew G. Knepley @*/
2565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2566d71ae5a4SJacob Faibussowitsch {
2567d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2568d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2569dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2570d410b0cfSMatthew G. Knepley   if (*dme) {
2571d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25729566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2573d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25749566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2575d410b0cfSMatthew G. Knepley   }
2576d410b0cfSMatthew G. Knepley   PetscFunctionReturn(0);
2577d410b0cfSMatthew G. Knepley }
2578d410b0cfSMatthew G. Knepley 
2579d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2580d71ae5a4SJacob Faibussowitsch {
2581ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2582ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2583ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2584ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2585ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2586ca3d3a14SMatthew G. Knepley }
2587ca3d3a14SMatthew G. Knepley 
2588d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2589d71ae5a4SJacob Faibussowitsch {
2590ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2591ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2592ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2593ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2594ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2595ca3d3a14SMatthew G. Knepley }
2596ca3d3a14SMatthew G. Knepley 
2597ca3d3a14SMatthew G. Knepley /*@
2598bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2599ca3d3a14SMatthew G. Knepley 
2600ca3d3a14SMatthew G. Knepley   Input Parameter:
2601ca3d3a14SMatthew G. Knepley . dm - The DM
2602ca3d3a14SMatthew G. Knepley 
2603ca3d3a14SMatthew G. Knepley   Output Parameter:
2604ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2605ca3d3a14SMatthew G. Knepley 
2606ca3d3a14SMatthew G. Knepley   Level: developer
2607ca3d3a14SMatthew G. Knepley 
2608bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2609ca3d3a14SMatthew G. Knepley @*/
2610d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2611d71ae5a4SJacob Faibussowitsch {
2612ca3d3a14SMatthew G. Knepley   Vec tv;
2613ca3d3a14SMatthew G. Knepley 
2614ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2615ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2616534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
26179566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2618ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2619ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2620ca3d3a14SMatthew G. Knepley }
2621ca3d3a14SMatthew G. Knepley 
2622d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2623d71ae5a4SJacob Faibussowitsch {
2624ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2625ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2626ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2627ca3d3a14SMatthew G. Knepley 
2628ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26299566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26309566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26319566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26339566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2637ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2639ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2640ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2641ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26429566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2643ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26449566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26459566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2646ca3d3a14SMatthew G. Knepley     }
2647ca3d3a14SMatthew G. Knepley   }
26489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26499566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26509566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2651ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2652ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26539566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2654ca3d3a14SMatthew G. Knepley       if (dof) {
2655ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2656ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2657ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2658ca3d3a14SMatthew G. Knepley 
2659ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26609566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26619566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26629566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2663ca3d3a14SMatthew G. Knepley       }
2664ca3d3a14SMatthew G. Knepley     }
2665ca3d3a14SMatthew G. Knepley   }
26669566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
2667ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2668ca3d3a14SMatthew G. Knepley }
2669ca3d3a14SMatthew G. Knepley 
2670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2671d71ae5a4SJacob Faibussowitsch {
2672ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2673ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2674ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2675ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2676ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2677ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2678ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26799566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
2680ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2681ca3d3a14SMatthew G. Knepley }
2682ca3d3a14SMatthew G. Knepley 
2683bb9467b5SJed Brown /*@C
2684bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2685baf369e7SPeter Brune 
2686bb7acecfSBarry Smith    Logically Collective on dm
2687baf369e7SPeter Brune 
26884165533cSJose E. Roman    Input Parameters:
2689bb7acecfSBarry Smith +  dm - the `DM`
2690bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2691bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
26920298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2693baf369e7SPeter Brune 
2694baf369e7SPeter Brune    Calling sequence for beginhook:
2695baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2696baf369e7SPeter Brune 
2697baf369e7SPeter Brune +  dm - global DM
2698baf369e7SPeter Brune .  g - global vector
2699baf369e7SPeter Brune .  mode - mode
2700baf369e7SPeter Brune .  l - local vector
2701baf369e7SPeter Brune -  ctx - optional user-defined function context
2702baf369e7SPeter Brune 
2703baf369e7SPeter Brune    Calling sequence for endhook:
2704ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2705baf369e7SPeter Brune 
2706baf369e7SPeter Brune +  global - global DM
2707baf369e7SPeter Brune -  ctx - optional user-defined function context
2708baf369e7SPeter Brune 
2709baf369e7SPeter Brune    Level: advanced
2710baf369e7SPeter Brune 
2711bb7acecfSBarry Smith    Note:
2712bb7acecfSBarry Smith    The hook may be used to provide, for example, values that represent boundary conditions in the local vectors that do not exist on the global vector.
2713bb7acecfSBarry Smith 
2714bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2715baf369e7SPeter Brune @*/
2716d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2717d71ae5a4SJacob Faibussowitsch {
2718baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2719baf369e7SPeter Brune 
2720baf369e7SPeter Brune   PetscFunctionBegin;
2721baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2722baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
27239566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2724baf369e7SPeter Brune   link->beginhook = beginhook;
2725baf369e7SPeter Brune   link->endhook   = endhook;
2726baf369e7SPeter Brune   link->ctx       = ctx;
27270298fd71SBarry Smith   link->next      = NULL;
2728baf369e7SPeter Brune   *p              = link;
2729baf369e7SPeter Brune   PetscFunctionReturn(0);
2730baf369e7SPeter Brune }
2731baf369e7SPeter Brune 
2732d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2733d71ae5a4SJacob Faibussowitsch {
27344c274da1SToby Isaac   Mat          cMat;
273579769bd5SJed Brown   Vec          cVec, cBias;
27364c274da1SToby Isaac   PetscSection section, cSec;
27374c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27384c274da1SToby Isaac 
27394c274da1SToby Isaac   PetscFunctionBegin;
27404c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27419566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27424c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27435db9a05bSToby Isaac     PetscInt nRows;
27445db9a05bSToby Isaac 
27459566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27465db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
27479566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27489566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27499566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27509566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27524c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27539566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27544c274da1SToby Isaac       if (dof) {
27554c274da1SToby Isaac         PetscScalar *vals;
27569566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27579566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27584c274da1SToby Isaac       }
27594c274da1SToby Isaac     }
27609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27614c274da1SToby Isaac   }
27624c274da1SToby Isaac   PetscFunctionReturn(0);
27634c274da1SToby Isaac }
27644c274da1SToby Isaac 
276547c6ae99SBarry Smith /*@
276601729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
276701729b5cSPatrick Sanan 
2768d083f849SBarry Smith     Neighbor-wise Collective on dm
276901729b5cSPatrick Sanan 
277001729b5cSPatrick Sanan     Input Parameters:
2771bb7acecfSBarry Smith +   dm - the `DM` object
277201729b5cSPatrick Sanan .   g - the global vector
2773bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
277401729b5cSPatrick Sanan -   l - the local vector
277501729b5cSPatrick Sanan 
277601729b5cSPatrick Sanan     Notes:
2777bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2778bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2779bb7acecfSBarry Smith 
2780bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
278101729b5cSPatrick Sanan 
278201729b5cSPatrick Sanan     Level: beginner
278301729b5cSPatrick Sanan 
2784bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2785bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2786bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
278701729b5cSPatrick Sanan @*/
2788d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2789d71ae5a4SJacob Faibussowitsch {
279001729b5cSPatrick Sanan   PetscFunctionBegin;
27919566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27929566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
279301729b5cSPatrick Sanan   PetscFunctionReturn(0);
279401729b5cSPatrick Sanan }
279501729b5cSPatrick Sanan 
279601729b5cSPatrick Sanan /*@
279747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
279847c6ae99SBarry Smith 
2799d083f849SBarry Smith     Neighbor-wise Collective on dm
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith     Input Parameters:
2802bb7acecfSBarry Smith +   dm - the `DM` object
280347c6ae99SBarry Smith .   g - the global vector
2804bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
280547c6ae99SBarry Smith -   l - the local vector
280647c6ae99SBarry Smith 
280701729b5cSPatrick Sanan     Level: intermediate
280847c6ae99SBarry Smith 
2809bb7acecfSBarry Smith     Notes:
2810bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2811bb7acecfSBarry Smith 
2812bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2813bb7acecfSBarry Smith 
2814bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2815bb7acecfSBarry Smith 
2816bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2817bb7acecfSBarry Smith 
2818bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
281947c6ae99SBarry Smith @*/
2820d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2821d71ae5a4SJacob Faibussowitsch {
28227128ae9fSMatthew G Knepley   PetscSF                 sf;
2823baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
282447c6ae99SBarry Smith 
282547c6ae99SBarry Smith   PetscFunctionBegin;
2826171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2827baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28281baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2829baf369e7SPeter Brune   }
28309566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28317128ae9fSMatthew G Knepley   if (sf) {
2832ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2833ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2834d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28357128ae9fSMatthew G Knepley 
28367a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28379566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28389566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28399566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28409566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28419566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28427128ae9fSMatthew G Knepley   } else {
28439566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28447128ae9fSMatthew G Knepley   }
284547c6ae99SBarry Smith   PetscFunctionReturn(0);
284647c6ae99SBarry Smith }
284747c6ae99SBarry Smith 
284847c6ae99SBarry Smith /*@
284947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
285047c6ae99SBarry Smith 
2851d083f849SBarry Smith     Neighbor-wise Collective on dm
285247c6ae99SBarry Smith 
285347c6ae99SBarry Smith     Input Parameters:
2854bb7acecfSBarry Smith +   dm - the `DM` object
285547c6ae99SBarry Smith .   g - the global vector
2856bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
285747c6ae99SBarry Smith -   l - the local vector
285847c6ae99SBarry Smith 
285901729b5cSPatrick Sanan     Level: intermediate
286047c6ae99SBarry Smith 
2861bb7acecfSBarry Smith     Note:
2862bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2863bb7acecfSBarry Smith 
2864bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
286547c6ae99SBarry Smith @*/
2866d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2867d71ae5a4SJacob Faibussowitsch {
28687128ae9fSMatthew G Knepley   PetscSF                 sf;
2869ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2870ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2871ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2872baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2873d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
287447c6ae99SBarry Smith 
287547c6ae99SBarry Smith   PetscFunctionBegin;
2876171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28779566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28789566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28797128ae9fSMatthew G Knepley   if (sf) {
28807a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28817128ae9fSMatthew G Knepley 
28829566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28839566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28849566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28859566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28869566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28879566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28887128ae9fSMatthew G Knepley   } else {
28899566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28907128ae9fSMatthew G Knepley   }
28919566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2892baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28939566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2894baf369e7SPeter Brune   }
289547c6ae99SBarry Smith   PetscFunctionReturn(0);
289647c6ae99SBarry Smith }
289747c6ae99SBarry Smith 
2898d4d07f1eSToby Isaac /*@C
2899d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2900d4d07f1eSToby Isaac 
2901bb7acecfSBarry Smith    Logically Collective on dm
2902d4d07f1eSToby Isaac 
29034165533cSJose E. Roman    Input Parameters:
2904bb7acecfSBarry Smith +  dm - the `DM`
2905bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2906bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2907d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2908d4d07f1eSToby Isaac 
2909d4d07f1eSToby Isaac    Calling sequence for beginhook:
2910d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2911d4d07f1eSToby Isaac 
2912bb7acecfSBarry Smith +  dm - global `DM`
2913d4d07f1eSToby Isaac .  l - local vector
2914d4d07f1eSToby Isaac .  mode - mode
2915d4d07f1eSToby Isaac .  g - global vector
2916d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2917d4d07f1eSToby Isaac 
2918d4d07f1eSToby Isaac    Calling sequence for endhook:
2919d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2920d4d07f1eSToby Isaac 
2921bb7acecfSBarry Smith +  global - global `DM`
2922d4d07f1eSToby Isaac .  l - local vector
2923d4d07f1eSToby Isaac .  mode - mode
2924d4d07f1eSToby Isaac .  g - global vector
2925d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2926d4d07f1eSToby Isaac 
2927d4d07f1eSToby Isaac    Level: advanced
2928d4d07f1eSToby Isaac 
2929bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2930d4d07f1eSToby Isaac @*/
2931d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2932d71ae5a4SJacob Faibussowitsch {
2933d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2934d4d07f1eSToby Isaac 
2935d4d07f1eSToby Isaac   PetscFunctionBegin;
2936d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2937d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29389566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2939d4d07f1eSToby Isaac   link->beginhook = beginhook;
2940d4d07f1eSToby Isaac   link->endhook   = endhook;
2941d4d07f1eSToby Isaac   link->ctx       = ctx;
2942d4d07f1eSToby Isaac   link->next      = NULL;
2943d4d07f1eSToby Isaac   *p              = link;
2944d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2945d4d07f1eSToby Isaac }
2946d4d07f1eSToby Isaac 
2947d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2948d71ae5a4SJacob Faibussowitsch {
29494c274da1SToby Isaac   Mat          cMat;
29504c274da1SToby Isaac   Vec          cVec;
29514c274da1SToby Isaac   PetscSection section, cSec;
29524c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29534c274da1SToby Isaac 
29544c274da1SToby Isaac   PetscFunctionBegin;
29554c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29569566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29574c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29585db9a05bSToby Isaac     PetscInt nRows;
29595db9a05bSToby Isaac 
29609566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29615db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
29629566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29639566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29649566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29654c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29674c274da1SToby Isaac       if (dof) {
29684c274da1SToby Isaac         PetscInt     d;
29694c274da1SToby Isaac         PetscScalar *vals;
29709566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29719566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29724c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29734c274da1SToby Isaac          * we just extracted */
2974ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29754c274da1SToby Isaac       }
29764c274da1SToby Isaac     }
29779566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29789566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29794c274da1SToby Isaac   }
29804c274da1SToby Isaac   PetscFunctionReturn(0);
29814c274da1SToby Isaac }
298201729b5cSPatrick Sanan /*@
298301729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
298401729b5cSPatrick Sanan 
2985d083f849SBarry Smith     Neighbor-wise Collective on dm
298601729b5cSPatrick Sanan 
298701729b5cSPatrick Sanan     Input Parameters:
2988bb7acecfSBarry Smith +   dm - the `DM` object
298901729b5cSPatrick Sanan .   l - the local vector
2990bb7acecfSBarry 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.
299101729b5cSPatrick Sanan -   g - the global vector
299201729b5cSPatrick Sanan 
299301729b5cSPatrick Sanan     Notes:
299401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2995bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
299601729b5cSPatrick Sanan 
2997bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2998bb7acecfSBarry Smith 
2999bb7acecfSBarry 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.
3000bb7acecfSBarry Smith 
3001bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
300201729b5cSPatrick Sanan 
300301729b5cSPatrick Sanan     Level: beginner
300401729b5cSPatrick Sanan 
3005bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`,  `DMGlobaToLocallHookAdd()`
300601729b5cSPatrick Sanan @*/
3007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
3008d71ae5a4SJacob Faibussowitsch {
300901729b5cSPatrick Sanan   PetscFunctionBegin;
30109566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
30119566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
301201729b5cSPatrick Sanan   PetscFunctionReturn(0);
301301729b5cSPatrick Sanan }
30144c274da1SToby Isaac 
301547c6ae99SBarry Smith /*@
301601729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
30179a42bb27SBarry Smith 
3018d083f849SBarry Smith     Neighbor-wise Collective on dm
30199a42bb27SBarry Smith 
30209a42bb27SBarry Smith     Input Parameters:
3021bb7acecfSBarry Smith +   dm - the `DM` object
3022f6813fd5SJed Brown .   l - the local vector
3023bb7acecfSBarry 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.
30241eb28f2eSBarry Smith -   g - the global vector
30259a42bb27SBarry Smith 
302695452b02SPatrick Sanan     Notes:
3027bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
3028bb7acecfSBarry Smith 
3029bb7acecfSBarry 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.
3030bb7acecfSBarry Smith 
3031bb7acecfSBarry Smith     Use `DMLocalToGlobalEnd()` to complete the communication process.
3032bb7acecfSBarry Smith 
3033bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3034bb7acecfSBarry Smith 
3035bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30369a42bb27SBarry Smith 
303701729b5cSPatrick Sanan     Level: intermediate
30389a42bb27SBarry Smith 
3039bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30409a42bb27SBarry Smith @*/
3041d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3042d71ae5a4SJacob Faibussowitsch {
30437128ae9fSMatthew G Knepley   PetscSF                 sf;
304484330215SMatthew G. Knepley   PetscSection            s, gs;
3045d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3046ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3047ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3048ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3049fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3050d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30519a42bb27SBarry Smith 
30529a42bb27SBarry Smith   PetscFunctionBegin;
3053171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3054d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30551baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3056d4d07f1eSToby Isaac   }
30579566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30589566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30599566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30607128ae9fSMatthew G Knepley   switch (mode) {
30617128ae9fSMatthew G Knepley   case INSERT_VALUES:
30627128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3063d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3064d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3065d71ae5a4SJacob Faibussowitsch     break;
30667128ae9fSMatthew G Knepley   case ADD_VALUES:
30677128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3068d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3069d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3070d71ae5a4SJacob Faibussowitsch     break;
3071d71ae5a4SJacob Faibussowitsch   default:
3072d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30737128ae9fSMatthew G Knepley   }
3074ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30759566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3076ca3d3a14SMatthew G. Knepley     if (transform) {
30779566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30789566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30799566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30809566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3081fa88e482SJed Brown     } else if (isInsert) {
30829566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3083fa88e482SJed Brown     } else {
30849566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3085fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3086ca3d3a14SMatthew G. Knepley     }
3087fa88e482SJed Brown     if (s && isInsert) {
30889566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3089fa88e482SJed Brown     } else {
30909566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3091fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3092fa88e482SJed Brown     }
3093ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30949566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
309584330215SMatthew G. Knepley     } else if (s && isInsert) {
309684330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
309784330215SMatthew G. Knepley 
30989566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30999566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
31009566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
310184330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3102b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
310384330215SMatthew G. Knepley 
31049566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
31059566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
31069566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
31079566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
31089566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
31099566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3110b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
311103442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
31127a8be351SBarry 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);
3113b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3114b3b16f48SMatthew G. Knepley         if (!gcdof) {
311584330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3116b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3117b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
311884330215SMatthew G. Knepley           const PetscInt *cdofs;
311984330215SMatthew G. Knepley           PetscInt        cind = 0;
312084330215SMatthew G. Knepley 
31219566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3122b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
31239371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
31249371c9d4SSatish Balay               ++cind;
31259371c9d4SSatish Balay               continue;
31269371c9d4SSatish Balay             }
3127b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
312884330215SMatthew G. Knepley           }
31297a8be351SBarry 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);
313084330215SMatthew G. Knepley       }
3131ca3d3a14SMatthew G. Knepley     }
3132fa88e482SJed Brown     if (g_inplace) {
31339566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3134fa88e482SJed Brown     } else {
31359566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3136fa88e482SJed Brown     }
3137ca3d3a14SMatthew G. Knepley     if (transform) {
31389566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31399566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3140fa88e482SJed Brown     } else if (l_inplace) {
31419566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3142ca3d3a14SMatthew G. Knepley     } else {
31439566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3144ca3d3a14SMatthew G. Knepley     }
31457128ae9fSMatthew G Knepley   } else {
31469566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31477128ae9fSMatthew G Knepley   }
31489a42bb27SBarry Smith   PetscFunctionReturn(0);
31499a42bb27SBarry Smith }
31509a42bb27SBarry Smith 
31519a42bb27SBarry Smith /*@
31529a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
315347c6ae99SBarry Smith 
3154d083f849SBarry Smith     Neighbor-wise Collective on dm
315547c6ae99SBarry Smith 
315647c6ae99SBarry Smith     Input Parameters:
3157bb7acecfSBarry Smith +   dm - the `DM` object
3158f6813fd5SJed Brown .   l - the local vector
3159bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3160f6813fd5SJed Brown -   g - the global vector
316147c6ae99SBarry Smith 
316201729b5cSPatrick Sanan     Level: intermediate
316347c6ae99SBarry Smith 
3164bb7acecfSBarry Smith     Note:
3165bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3166bb7acecfSBarry Smith 
3167bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
316847c6ae99SBarry Smith @*/
3169d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3170d71ae5a4SJacob Faibussowitsch {
31717128ae9fSMatthew G Knepley   PetscSF                 sf;
317284330215SMatthew G. Knepley   PetscSection            s;
3173d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3174ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
317547c6ae99SBarry Smith 
317647c6ae99SBarry Smith   PetscFunctionBegin;
3177171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31789566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31799566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31807128ae9fSMatthew G Knepley   switch (mode) {
31817128ae9fSMatthew G Knepley   case INSERT_VALUES:
3182d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3183d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3184d71ae5a4SJacob Faibussowitsch     break;
31857128ae9fSMatthew G Knepley   case ADD_VALUES:
3186d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3187d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3188d71ae5a4SJacob Faibussowitsch     break;
3189d71ae5a4SJacob Faibussowitsch   default:
3190d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31917128ae9fSMatthew G Knepley   }
319284330215SMatthew G. Knepley   if (sf && !isInsert) {
3193ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3194ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3195ca3d3a14SMatthew G. Knepley     Vec                tmpl;
319684330215SMatthew G. Knepley 
31979566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3198ca3d3a14SMatthew G. Knepley     if (transform) {
31999566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
32009566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3201ca3d3a14SMatthew G. Knepley     } else {
32029566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3203ca3d3a14SMatthew G. Knepley     }
32049566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
32059566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3206ca3d3a14SMatthew G. Knepley     if (transform) {
32079566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
32089566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3209ca3d3a14SMatthew G. Knepley     } else {
32109566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3211ca3d3a14SMatthew G. Knepley     }
32129566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
321384330215SMatthew G. Knepley   } else if (s && isInsert) {
32147128ae9fSMatthew G Knepley   } else {
32159566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
32167128ae9fSMatthew G Knepley   }
3217d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
32189566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3219d4d07f1eSToby Isaac   }
322047c6ae99SBarry Smith   PetscFunctionReturn(0);
322147c6ae99SBarry Smith }
322247c6ae99SBarry Smith 
3223f089877aSRichard Tran Mills /*@
3224bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3225bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3226bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3227f089877aSRichard Tran Mills 
3228d083f849SBarry Smith    Neighbor-wise Collective on dm
3229f089877aSRichard Tran Mills 
3230f089877aSRichard Tran Mills    Input Parameters:
3231bb7acecfSBarry Smith +  dm - the `DM` object
3232bc0a1609SRichard Tran Mills .  g - the original local vector
3233bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3234f089877aSRichard Tran Mills 
3235bc0a1609SRichard Tran Mills    Output Parameter:
3236bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3237f089877aSRichard Tran Mills 
3238f089877aSRichard Tran Mills    Level: intermediate
3239f089877aSRichard Tran Mills 
3240bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3241f089877aSRichard Tran Mills @*/
3242d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3243d71ae5a4SJacob Faibussowitsch {
3244f089877aSRichard Tran Mills   PetscFunctionBegin;
3245f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32469f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32479f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32489f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
3249f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3250f089877aSRichard Tran Mills }
3251f089877aSRichard Tran Mills 
3252f089877aSRichard Tran Mills /*@
3253bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3254bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3255f089877aSRichard Tran Mills 
3256d083f849SBarry Smith    Neighbor-wise Collective on dm
3257f089877aSRichard Tran Mills 
3258f089877aSRichard Tran Mills    Input Parameters:
3259bb7acecfSBarry Smith +  da - the `DM` object
3260bc0a1609SRichard Tran Mills .  g - the original local vector
3261bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3262f089877aSRichard Tran Mills 
3263bc0a1609SRichard Tran Mills    Output Parameter:
3264bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3265f089877aSRichard Tran Mills 
3266f089877aSRichard Tran Mills    Level: intermediate
3267f089877aSRichard Tran Mills 
3268bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3269f089877aSRichard Tran Mills @*/
3270d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3271d71ae5a4SJacob Faibussowitsch {
3272f089877aSRichard Tran Mills   PetscFunctionBegin;
3273f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32749f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32759f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32769f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
3277f089877aSRichard Tran Mills   PetscFunctionReturn(0);
3278f089877aSRichard Tran Mills }
3279f089877aSRichard Tran Mills 
328047c6ae99SBarry Smith /*@
3281bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
328247c6ae99SBarry Smith 
3283d083f849SBarry Smith     Collective on dm
328447c6ae99SBarry Smith 
3285d8d19677SJose E. Roman     Input Parameters:
3286bb7acecfSBarry Smith +   dm - the `DM` object
3287bb7acecfSBarry Smith -   comm - the communicator to contain the new `DM` object (or MPI_COMM_NULL)
328847c6ae99SBarry Smith 
328947c6ae99SBarry Smith     Output Parameter:
3290bb7acecfSBarry Smith .   dmc - the coarsened `DM`
329147c6ae99SBarry Smith 
329247c6ae99SBarry Smith     Level: developer
329347c6ae99SBarry Smith 
3294bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
329547c6ae99SBarry Smith @*/
3296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3297d71ae5a4SJacob Faibussowitsch {
3298b17ce1afSJed Brown   DMCoarsenHookLink link;
329947c6ae99SBarry Smith 
330047c6ae99SBarry Smith   PetscFunctionBegin;
3301171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3303dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3304b9d85ea2SLisandro Dalcin   if (*dmc) {
3305a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
33069566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
330743842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
33089566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3309644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
33100598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3311656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
33129566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3313b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
33149566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3315b17ce1afSJed Brown     }
3316b9d85ea2SLisandro Dalcin   }
33179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
33187a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3319b17ce1afSJed Brown   PetscFunctionReturn(0);
3320b17ce1afSJed Brown }
3321b17ce1afSJed Brown 
3322bb9467b5SJed Brown /*@C
3323b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3324b17ce1afSJed Brown 
3325bb7acecfSBarry Smith    Logically Collective on fine
3326b17ce1afSJed Brown 
33274165533cSJose E. Roman    Input Parameters:
3328bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3329b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3330bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
33310298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3332b17ce1afSJed Brown 
3333b17ce1afSJed Brown    Calling sequence of coarsenhook:
3334b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3335b17ce1afSJed Brown 
3336bb7acecfSBarry Smith +  fine - fine level `DM`
3337bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3338b17ce1afSJed Brown -  ctx - optional user-defined function context
3339b17ce1afSJed Brown 
3340b17ce1afSJed Brown    Calling sequence for restricthook:
3341c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3342bb7acecfSBarry Smith $
3343bb7acecfSBarry Smith +  fine - fine level `DM`
3344bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3345c833c3b5SJed Brown .  rscale - scaling vector for restriction
3346c833c3b5SJed Brown .  inject - matrix restricting by injection
3347b17ce1afSJed Brown .  coarse - coarse level DM to update
3348b17ce1afSJed Brown -  ctx - optional user-defined function context
3349b17ce1afSJed Brown 
3350b17ce1afSJed Brown    Level: advanced
3351b17ce1afSJed Brown 
3352b17ce1afSJed Brown    Notes:
3353bb7acecfSBarry 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`.
3354b17ce1afSJed Brown 
3355b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3356b17ce1afSJed Brown 
3357b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3358bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3359b17ce1afSJed Brown 
3360bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3361bb7acecfSBarry Smith 
3362bb7acecfSBarry Smith    Fortran Note:
3363bb7acecfSBarry Smith    This function is not available from Fortran.
3364bb9467b5SJed Brown 
3365db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3366b17ce1afSJed Brown @*/
3367d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3368d71ae5a4SJacob Faibussowitsch {
3369b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3370b17ce1afSJed Brown 
3371b17ce1afSJed Brown   PetscFunctionBegin;
3372b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33731e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33741e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
33751e3d8eccSJed Brown   }
33769566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3377b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3378b17ce1afSJed Brown   link->restricthook = restricthook;
3379b17ce1afSJed Brown   link->ctx          = ctx;
33800298fd71SBarry Smith   link->next         = NULL;
3381b17ce1afSJed Brown   *p                 = link;
3382b17ce1afSJed Brown   PetscFunctionReturn(0);
3383b17ce1afSJed Brown }
3384b17ce1afSJed Brown 
3385dc822a44SJed Brown /*@C
3386bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3387dc822a44SJed Brown 
3388bb7acecfSBarry Smith    Logically Collective on fine
3389dc822a44SJed Brown 
33904165533cSJose E. Roman    Input Parameters:
3391bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3392dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3393bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3394dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3395dc822a44SJed Brown 
3396dc822a44SJed Brown    Level: advanced
3397dc822a44SJed Brown 
3398bb7acecfSBarry Smith    Note:
3399dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3400dc822a44SJed Brown 
3401bb7acecfSBarry Smith    Fortran Note:
3402bb7acecfSBarry Smith    This function is not available from Fortran.
3403dc822a44SJed Brown 
3404db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3405dc822a44SJed Brown @*/
3406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3407d71ae5a4SJacob Faibussowitsch {
3408dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3409dc822a44SJed Brown 
3410dc822a44SJed Brown   PetscFunctionBegin;
3411dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3412dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3413dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3414dc822a44SJed Brown       link = *p;
3415dc822a44SJed Brown       *p   = link->next;
34169566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3417dc822a44SJed Brown       break;
3418dc822a44SJed Brown     }
3419dc822a44SJed Brown   }
3420dc822a44SJed Brown   PetscFunctionReturn(0);
3421dc822a44SJed Brown }
3422dc822a44SJed Brown 
3423b17ce1afSJed Brown /*@
3424bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3425b17ce1afSJed Brown 
3426b17ce1afSJed Brown    Collective if any hooks are
3427b17ce1afSJed Brown 
34284165533cSJose E. Roman    Input Parameters:
3429bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3430bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3431e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3432bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3433e91eccc2SStefano Zampini -  coarse - coarser DM to update
3434b17ce1afSJed Brown 
3435b17ce1afSJed Brown    Level: developer
3436b17ce1afSJed Brown 
3437bb7acecfSBarry Smith    Developer Note:
3438bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3439bb7acecfSBarry Smith 
3440bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3441b17ce1afSJed Brown @*/
3442d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3443d71ae5a4SJacob Faibussowitsch {
3444b17ce1afSJed Brown   DMCoarsenHookLink link;
3445b17ce1afSJed Brown 
3446b17ce1afSJed Brown   PetscFunctionBegin;
3447b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34481baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3449b17ce1afSJed Brown   }
345047c6ae99SBarry Smith   PetscFunctionReturn(0);
345147c6ae99SBarry Smith }
345247c6ae99SBarry Smith 
3453bb9467b5SJed Brown /*@C
3454be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34555dbd56e3SPeter Brune 
3456d083f849SBarry Smith    Logically Collective on global
34575dbd56e3SPeter Brune 
34584165533cSJose E. Roman    Input Parameters:
3459bb7acecfSBarry Smith +  global - global `DM`
3460bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34615dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
34620298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
34635dbd56e3SPeter Brune 
3464ec4806b8SPeter Brune    Calling sequence for ddhook:
3465ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3466ec4806b8SPeter Brune 
3467bb7acecfSBarry Smith +  global - global `DM`
3468bb7acecfSBarry Smith .  block  - block `DM`
3469ec4806b8SPeter Brune -  ctx - optional user-defined function context
3470ec4806b8SPeter Brune 
34715dbd56e3SPeter Brune    Calling sequence for restricthook:
3472ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
34735dbd56e3SPeter Brune 
3474bb7acecfSBarry Smith +  global - global `DM`
34755dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34765dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3477bb7acecfSBarry Smith .  block  - block `DM`
34785dbd56e3SPeter Brune -  ctx - optional user-defined function context
34795dbd56e3SPeter Brune 
34805dbd56e3SPeter Brune    Level: advanced
34815dbd56e3SPeter Brune 
34825dbd56e3SPeter Brune    Notes:
3483bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34845dbd56e3SPeter Brune 
34855dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34865dbd56e3SPeter Brune 
34875dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3488bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34895dbd56e3SPeter Brune 
3490bb7acecfSBarry Smith    Fortran Note:
3491bb7acecfSBarry Smith    This function is not available from Fortran.
3492bb9467b5SJed Brown 
3493bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34945dbd56e3SPeter Brune @*/
3495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3496d71ae5a4SJacob Faibussowitsch {
3497be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34985dbd56e3SPeter Brune 
34995dbd56e3SPeter Brune   PetscFunctionBegin;
35005dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3501b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
3502b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3503b3a6b972SJed Brown   }
35049566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
35055dbd56e3SPeter Brune   link->restricthook = restricthook;
3506be081cd6SPeter Brune   link->ddhook       = ddhook;
35075dbd56e3SPeter Brune   link->ctx          = ctx;
35080298fd71SBarry Smith   link->next         = NULL;
35095dbd56e3SPeter Brune   *p                 = link;
35105dbd56e3SPeter Brune   PetscFunctionReturn(0);
35115dbd56e3SPeter Brune }
35125dbd56e3SPeter Brune 
3513b3a6b972SJed Brown /*@C
3514b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3515b3a6b972SJed Brown 
3516bb7acecfSBarry Smith    Logically Collective on global
3517b3a6b972SJed Brown 
35184165533cSJose E. Roman    Input Parameters:
3519bb7acecfSBarry Smith +  global - global `DM`
3520bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3521b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3522b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3523b3a6b972SJed Brown 
3524b3a6b972SJed Brown    Level: advanced
3525b3a6b972SJed Brown 
3526bb7acecfSBarry Smith    Fortran Note:
3527bb7acecfSBarry Smith    This function is not available from Fortran.
3528b3a6b972SJed Brown 
3529db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3530b3a6b972SJed Brown @*/
3531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3532d71ae5a4SJacob Faibussowitsch {
3533b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3534b3a6b972SJed Brown 
3535b3a6b972SJed Brown   PetscFunctionBegin;
3536b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3537b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3538b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3539b3a6b972SJed Brown       link = *p;
3540b3a6b972SJed Brown       *p   = link->next;
35419566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3542b3a6b972SJed Brown       break;
3543b3a6b972SJed Brown     }
3544b3a6b972SJed Brown   }
3545b3a6b972SJed Brown   PetscFunctionReturn(0);
3546b3a6b972SJed Brown }
3547b3a6b972SJed Brown 
35485dbd56e3SPeter Brune /*@
3549bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35505dbd56e3SPeter Brune 
35515dbd56e3SPeter Brune    Collective if any hooks are
35525dbd56e3SPeter Brune 
35534165533cSJose E. Roman    Input Parameters:
3554bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3555be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3556be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3557bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35585dbd56e3SPeter Brune 
35595dbd56e3SPeter Brune    Level: developer
35605dbd56e3SPeter Brune 
3561db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35625dbd56e3SPeter Brune @*/
3563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3564d71ae5a4SJacob Faibussowitsch {
3565be081cd6SPeter Brune   DMSubDomainHookLink link;
35665dbd56e3SPeter Brune 
35675dbd56e3SPeter Brune   PetscFunctionBegin;
3568be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35691baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35705dbd56e3SPeter Brune   }
35715dbd56e3SPeter Brune   PetscFunctionReturn(0);
35725dbd56e3SPeter Brune }
35735dbd56e3SPeter Brune 
35745fe1f584SPeter Brune /*@
3575bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35765fe1f584SPeter Brune 
35775fe1f584SPeter Brune     Not Collective
35785fe1f584SPeter Brune 
35795fe1f584SPeter Brune     Input Parameter:
3580bb7acecfSBarry Smith .   dm - the `DM` object
35815fe1f584SPeter Brune 
35825fe1f584SPeter Brune     Output Parameter:
35836a7d9d85SPeter Brune .   level - number of coarsenings
35845fe1f584SPeter Brune 
35855fe1f584SPeter Brune     Level: developer
35865fe1f584SPeter Brune 
3587bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35885fe1f584SPeter Brune @*/
3589d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3590d71ae5a4SJacob Faibussowitsch {
35915fe1f584SPeter Brune   PetscFunctionBegin;
35925fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3593b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35945fe1f584SPeter Brune   *level = dm->leveldown;
35955fe1f584SPeter Brune   PetscFunctionReturn(0);
35965fe1f584SPeter Brune }
35975fe1f584SPeter Brune 
35989a64c4a8SMatthew G. Knepley /*@
3599bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
36009a64c4a8SMatthew G. Knepley 
3601bb7acecfSBarry Smith     Collective on dm
36029a64c4a8SMatthew G. Knepley 
36039a64c4a8SMatthew G. Knepley     Input Parameters:
3604bb7acecfSBarry Smith +   dm - the `DM` object
36059a64c4a8SMatthew G. Knepley -   level - number of coarsenings
36069a64c4a8SMatthew G. Knepley 
36079a64c4a8SMatthew G. Knepley     Level: developer
36089a64c4a8SMatthew G. Knepley 
3609bb7acecfSBarry Smith     Note:
3610bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3611bb7acecfSBarry Smith 
3612bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
36139a64c4a8SMatthew G. Knepley @*/
3614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3615d71ae5a4SJacob Faibussowitsch {
36169a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
36179a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36189a64c4a8SMatthew G. Knepley   dm->leveldown = level;
36199a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
36209a64c4a8SMatthew G. Knepley }
36219a64c4a8SMatthew G. Knepley 
362247c6ae99SBarry Smith /*@C
3623bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
362447c6ae99SBarry Smith 
3625d083f849SBarry Smith     Collective on dm
362647c6ae99SBarry Smith 
3627d8d19677SJose E. Roman     Input Parameters:
3628bb7acecfSBarry Smith +   dm - the `DM` object
362947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
363047c6ae99SBarry Smith 
363147c6ae99SBarry Smith     Output Parameter:
3632bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
363347c6ae99SBarry Smith 
363447c6ae99SBarry Smith     Level: developer
363547c6ae99SBarry Smith 
3636bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
363747c6ae99SBarry Smith @*/
3638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3639d71ae5a4SJacob Faibussowitsch {
364047c6ae99SBarry Smith   PetscFunctionBegin;
3641171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36427a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
364347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3644b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
364547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3646dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
364747c6ae99SBarry Smith   } else if (dm->ops->refine) {
364847c6ae99SBarry Smith     PetscInt i;
364947c6ae99SBarry Smith 
36509566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
365148a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3652ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
365347c6ae99SBarry Smith   PetscFunctionReturn(0);
365447c6ae99SBarry Smith }
365547c6ae99SBarry Smith 
365647c6ae99SBarry Smith /*@C
3657bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
365847c6ae99SBarry Smith 
3659d083f849SBarry Smith     Collective on dm
366047c6ae99SBarry Smith 
3661d8d19677SJose E. Roman     Input Parameters:
3662bb7acecfSBarry Smith +   dm - the `DM` object
366347c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
366447c6ae99SBarry Smith 
366547c6ae99SBarry Smith     Output Parameter:
3666bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
366747c6ae99SBarry Smith 
366847c6ae99SBarry Smith     Level: developer
366947c6ae99SBarry Smith 
3670bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
367147c6ae99SBarry Smith @*/
3672d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3673d71ae5a4SJacob Faibussowitsch {
367447c6ae99SBarry Smith   PetscFunctionBegin;
3675171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36767a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
367747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
367847c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
367947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3680dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
368147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
368247c6ae99SBarry Smith     PetscInt i;
368347c6ae99SBarry Smith 
36849566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
368548a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3686ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
368747c6ae99SBarry Smith   PetscFunctionReturn(0);
368847c6ae99SBarry Smith }
368947c6ae99SBarry Smith 
36901a266240SBarry Smith /*@C
3691bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36921a266240SBarry Smith 
3693bb7acecfSBarry Smith     Logically Collective if the function is collective
36941a266240SBarry Smith 
36951a266240SBarry Smith     Input Parameters:
3696bb7acecfSBarry Smith +   dm - the `DM` object
36971a266240SBarry Smith -   destroy - the destroy function
36981a266240SBarry Smith 
36991a266240SBarry Smith     Level: intermediate
37001a266240SBarry Smith 
3701bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3702f07f9ceaSJed Brown @*/
3703d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3704d71ae5a4SJacob Faibussowitsch {
37051a266240SBarry Smith   PetscFunctionBegin;
3706171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37071a266240SBarry Smith   dm->ctxdestroy = destroy;
37081a266240SBarry Smith   PetscFunctionReturn(0);
37091a266240SBarry Smith }
37101a266240SBarry Smith 
3711b07ff414SBarry Smith /*@
3712bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
371347c6ae99SBarry Smith 
371447c6ae99SBarry Smith     Not Collective
371547c6ae99SBarry Smith 
371647c6ae99SBarry Smith     Input Parameters:
3717bb7acecfSBarry Smith +   dm - the `DM` object
371847c6ae99SBarry Smith -   ctx - the user context
371947c6ae99SBarry Smith 
372047c6ae99SBarry Smith     Level: intermediate
372147c6ae99SBarry Smith 
3722bb7acecfSBarry Smith     Note:
372335cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3724bb7acecfSBarry Smith 
3725bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
372647c6ae99SBarry Smith @*/
3727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3728d71ae5a4SJacob Faibussowitsch {
372947c6ae99SBarry Smith   PetscFunctionBegin;
3730171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373147c6ae99SBarry Smith   dm->ctx = ctx;
373247c6ae99SBarry Smith   PetscFunctionReturn(0);
373347c6ae99SBarry Smith }
373447c6ae99SBarry Smith 
373547c6ae99SBarry Smith /*@
3736bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
373747c6ae99SBarry Smith 
373847c6ae99SBarry Smith     Not Collective
373947c6ae99SBarry Smith 
374047c6ae99SBarry Smith     Input Parameter:
3741bb7acecfSBarry Smith .   dm - the `DM` object
374247c6ae99SBarry Smith 
374347c6ae99SBarry Smith     Output Parameter:
374447c6ae99SBarry Smith .   ctx - the user context
374547c6ae99SBarry Smith 
374647c6ae99SBarry Smith     Level: intermediate
374747c6ae99SBarry Smith 
3748bb7acecfSBarry Smith     Note:
374935cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3750bb7acecfSBarry Smith 
3751bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
375247c6ae99SBarry Smith @*/
3753d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3754d71ae5a4SJacob Faibussowitsch {
375547c6ae99SBarry Smith   PetscFunctionBegin;
3756171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37571b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
375847c6ae99SBarry Smith   PetscFunctionReturn(0);
375947c6ae99SBarry Smith }
376047c6ae99SBarry Smith 
376108da532bSDmitry Karpeev /*@C
3762bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
376308da532bSDmitry Karpeev 
3764d083f849SBarry Smith     Logically Collective on dm
376508da532bSDmitry Karpeev 
3766d8d19677SJose E. Roman     Input Parameters:
376708da532bSDmitry Karpeev +   dm - the DM object
37680298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
376908da532bSDmitry Karpeev 
377008da532bSDmitry Karpeev     Level: intermediate
377108da532bSDmitry Karpeev 
3772bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3773db781477SPatrick Sanan          `DMSetJacobian()`
377408da532bSDmitry Karpeev @*/
3775d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3776d71ae5a4SJacob Faibussowitsch {
377708da532bSDmitry Karpeev   PetscFunctionBegin;
37785a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
378008da532bSDmitry Karpeev   PetscFunctionReturn(0);
378108da532bSDmitry Karpeev }
378208da532bSDmitry Karpeev 
378308da532bSDmitry Karpeev /*@
3784bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
378508da532bSDmitry Karpeev 
378608da532bSDmitry Karpeev     Not Collective
378708da532bSDmitry Karpeev 
378808da532bSDmitry Karpeev     Input Parameter:
3789bb7acecfSBarry Smith .   dm - the `DM` object to destroy
379008da532bSDmitry Karpeev 
379108da532bSDmitry Karpeev     Output Parameter:
3792bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
379308da532bSDmitry Karpeev 
379408da532bSDmitry Karpeev     Level: developer
379508da532bSDmitry Karpeev 
3796bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
379708da532bSDmitry Karpeev @*/
3798d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3799d71ae5a4SJacob Faibussowitsch {
380008da532bSDmitry Karpeev   PetscFunctionBegin;
38015a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3802534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
380308da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
380408da532bSDmitry Karpeev   PetscFunctionReturn(0);
380508da532bSDmitry Karpeev }
380608da532bSDmitry Karpeev 
380708da532bSDmitry Karpeev /*@C
3808bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
380908da532bSDmitry Karpeev 
3810d083f849SBarry Smith     Logically Collective on dm
381108da532bSDmitry Karpeev 
3812f899ff85SJose E. Roman     Input Parameter:
3813bb7acecfSBarry Smith .   dm - the `DM` object
381408da532bSDmitry Karpeev 
381508da532bSDmitry Karpeev     Output parameters:
381608da532bSDmitry Karpeev +   xl - lower bound
381708da532bSDmitry Karpeev -   xu - upper bound
381808da532bSDmitry Karpeev 
3819907376e6SBarry Smith     Level: advanced
3820907376e6SBarry Smith 
382195452b02SPatrick Sanan     Notes:
382295452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
382308da532bSDmitry Karpeev 
3824bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
382508da532bSDmitry Karpeev @*/
3826d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3827d71ae5a4SJacob Faibussowitsch {
382808da532bSDmitry Karpeev   PetscFunctionBegin;
38295a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
383008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
38315a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3832dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
383308da532bSDmitry Karpeev   PetscFunctionReturn(0);
383408da532bSDmitry Karpeev }
383508da532bSDmitry Karpeev 
3836b0ae01b7SPeter Brune /*@
3837bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3838b0ae01b7SPeter Brune 
3839b0ae01b7SPeter Brune     Not Collective
3840b0ae01b7SPeter Brune 
3841b0ae01b7SPeter Brune     Input Parameter:
3842b0ae01b7SPeter Brune .   dm - the DM object
3843b0ae01b7SPeter Brune 
3844b0ae01b7SPeter Brune     Output Parameter:
3845bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3846b0ae01b7SPeter Brune 
3847b0ae01b7SPeter Brune     Level: developer
3848b0ae01b7SPeter Brune 
3849bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3850b0ae01b7SPeter Brune @*/
3851d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3852d71ae5a4SJacob Faibussowitsch {
3853b0ae01b7SPeter Brune   PetscFunctionBegin;
38545a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3855534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3856b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3857b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3858b0ae01b7SPeter Brune }
3859b0ae01b7SPeter Brune 
38603ad4599aSBarry Smith /*@
3861bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38623ad4599aSBarry Smith 
38633ad4599aSBarry Smith     Not Collective
38643ad4599aSBarry Smith 
38653ad4599aSBarry Smith     Input Parameter:
3866bb7acecfSBarry Smith .   dm - the `DM` object
38673ad4599aSBarry Smith 
38683ad4599aSBarry Smith     Output Parameter:
3869bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38703ad4599aSBarry Smith 
38713ad4599aSBarry Smith     Level: developer
38723ad4599aSBarry Smith 
3873bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38743ad4599aSBarry Smith @*/
3875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3876d71ae5a4SJacob Faibussowitsch {
38773ad4599aSBarry Smith   PetscFunctionBegin;
38785a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3879534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38803ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38813ad4599aSBarry Smith   PetscFunctionReturn(0);
38823ad4599aSBarry Smith }
38833ad4599aSBarry Smith 
3884a7058e45SLawrence Mitchell /*@
3885bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3886a7058e45SLawrence Mitchell 
3887a7058e45SLawrence Mitchell     Not Collective
3888a7058e45SLawrence Mitchell 
3889a7058e45SLawrence Mitchell     Input Parameter:
3890bb7acecfSBarry Smith .   dm - the `DM` object
3891a7058e45SLawrence Mitchell 
3892a7058e45SLawrence Mitchell     Output Parameter:
3893bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3894a7058e45SLawrence Mitchell 
3895a7058e45SLawrence Mitchell     Level: developer
3896a7058e45SLawrence Mitchell 
3897bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3898a7058e45SLawrence Mitchell @*/
3899d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3900d71ae5a4SJacob Faibussowitsch {
3901a7058e45SLawrence Mitchell   PetscFunctionBegin;
39025a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3903534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3904dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3905ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
3906a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3907a7058e45SLawrence Mitchell }
3908a7058e45SLawrence Mitchell 
39090298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3910264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3911264ace61SBarry Smith 
3912264ace61SBarry Smith /*@C
3913bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3914264ace61SBarry Smith 
3915d083f849SBarry Smith   Collective on dm
3916264ace61SBarry Smith 
3917264ace61SBarry Smith   Input Parameters:
3918bb7acecfSBarry Smith + dm     - The `DM` object
3919bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3920264ace61SBarry Smith 
3921264ace61SBarry Smith   Options Database Key:
3922bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3923264ace61SBarry Smith 
3924264ace61SBarry Smith   Level: intermediate
3925264ace61SBarry Smith 
3926bb7acecfSBarry Smith   Note:
3927bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3928bb7acecfSBarry Smith 
3929bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3930264ace61SBarry Smith @*/
3931d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3932d71ae5a4SJacob Faibussowitsch {
3933264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3934264ace61SBarry Smith   PetscBool match;
3935264ace61SBarry Smith 
3936264ace61SBarry Smith   PetscFunctionBegin;
3937264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
3939264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3940264ace61SBarry Smith 
39419566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
39429566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
39437a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3944264ace61SBarry Smith 
3945dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
39469566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
39479566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39489566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
3949264ace61SBarry Smith   PetscFunctionReturn(0);
3950264ace61SBarry Smith }
3951264ace61SBarry Smith 
3952264ace61SBarry Smith /*@C
3953bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3954264ace61SBarry Smith 
3955264ace61SBarry Smith   Not Collective
3956264ace61SBarry Smith 
3957264ace61SBarry Smith   Input Parameter:
3958bb7acecfSBarry Smith . dm  - The `DM`
3959264ace61SBarry Smith 
3960264ace61SBarry Smith   Output Parameter:
3961bb7acecfSBarry Smith . type - The `DMType` name
3962264ace61SBarry Smith 
3963264ace61SBarry Smith   Level: intermediate
3964264ace61SBarry Smith 
3965bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3966264ace61SBarry Smith @*/
3967d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3968d71ae5a4SJacob Faibussowitsch {
3969264ace61SBarry Smith   PetscFunctionBegin;
3970264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3971c959eef4SJed Brown   PetscValidPointer(type, 2);
39729566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3973264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3974264ace61SBarry Smith   PetscFunctionReturn(0);
3975264ace61SBarry Smith }
3976264ace61SBarry Smith 
397767a56275SMatthew G Knepley /*@C
3978bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
397967a56275SMatthew G Knepley 
3980d083f849SBarry Smith   Collective on dm
398167a56275SMatthew G Knepley 
398267a56275SMatthew G Knepley   Input Parameters:
3983bb7acecfSBarry Smith + dm - the `DM`
3984bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
398567a56275SMatthew G Knepley 
398667a56275SMatthew G Knepley   Output Parameter:
3987bb7acecfSBarry Smith . M - pointer to new `DM`
398867a56275SMatthew G Knepley 
398967a56275SMatthew G Knepley   Notes:
3990bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3991bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3992bb7acecfSBarry Smith   of the input `DM`.
399367a56275SMatthew G Knepley 
399467a56275SMatthew G Knepley   Level: intermediate
399567a56275SMatthew G Knepley 
3996bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
399767a56275SMatthew G Knepley @*/
3998d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3999d71ae5a4SJacob Faibussowitsch {
400067a56275SMatthew G Knepley   DM        B;
400167a56275SMatthew G Knepley   char      convname[256];
4002c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
400367a56275SMatthew G Knepley 
400467a56275SMatthew G Knepley   PetscFunctionBegin;
400567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400667a56275SMatthew G Knepley   PetscValidType(dm, 1);
400767a56275SMatthew G Knepley   PetscValidPointer(M, 3);
40089566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
40099566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
4010c067b6caSMatthew G. Knepley   if (sametype) {
4011c067b6caSMatthew G. Knepley     *M = dm;
40129566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
4013c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
4014c067b6caSMatthew G. Knepley   } else {
40150298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
401667a56275SMatthew G Knepley 
401767a56275SMatthew G Knepley     /*
401867a56275SMatthew G Knepley        Order of precedence:
401967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
402067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
402167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
402267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
402367a56275SMatthew G Knepley        5) Use a really basic converter.
402467a56275SMatthew G Knepley     */
402567a56275SMatthew G Knepley 
402667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
40279566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40289566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40299566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40309566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40319566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40329566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
403367a56275SMatthew G Knepley     if (conv) goto foundconv;
403467a56275SMatthew G Knepley 
403567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
40369566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
40379566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
40389566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
40399566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
40409566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
40419566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
40429566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
40439566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
404467a56275SMatthew G Knepley     if (conv) {
40459566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
404667a56275SMatthew G Knepley       goto foundconv;
404767a56275SMatthew G Knepley     }
404867a56275SMatthew G Knepley 
404967a56275SMatthew G Knepley #if 0
405067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
405167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40529566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
405367a56275SMatthew G Knepley     if (conv) goto foundconv;
405467a56275SMatthew G Knepley 
405567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
405667a56275SMatthew G Knepley     if (dm->ops->convert) {
405767a56275SMatthew G Knepley       conv = dm->ops->convert;
405867a56275SMatthew G Knepley     }
405967a56275SMatthew G Knepley     if (conv) goto foundconv;
406067a56275SMatthew G Knepley #endif
406167a56275SMatthew G Knepley 
406267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
406398921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
406467a56275SMatthew G Knepley 
406567a56275SMatthew G Knepley   foundconv:
40669566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40679566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
406812fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
406990b157c4SStefano Zampini     {
40704fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40716858538eSMatthew G. Knepley 
40724fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40734fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4074c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40759566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40769566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40779566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40789566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
407912fa691eSMatthew G. Knepley     }
40809566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
408167a56275SMatthew G Knepley   }
40829566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
408367a56275SMatthew G Knepley   PetscFunctionReturn(0);
408467a56275SMatthew G Knepley }
4085264ace61SBarry Smith 
4086264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4087264ace61SBarry Smith 
4088264ace61SBarry Smith /*@C
4089bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40901c84c290SBarry Smith 
40911c84c290SBarry Smith   Not Collective
40921c84c290SBarry Smith 
40931c84c290SBarry Smith   Input Parameters:
40941c84c290SBarry Smith + name        - The name of a new user-defined creation routine
40951c84c290SBarry Smith - create_func - The creation routine itself
40961c84c290SBarry Smith 
40971c84c290SBarry Smith   Notes:
4098bb7acecfSBarry Smith   DMRegister() may be called multiple times to add several user-defined `DM`s
40991c84c290SBarry Smith 
41001c84c290SBarry Smith   Sample usage:
41011c84c290SBarry Smith .vb
4102bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
41031c84c290SBarry Smith .ve
41041c84c290SBarry Smith 
41051c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
41061c84c290SBarry Smith .vb
41071c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
41081c84c290SBarry Smith     DMSetType(DM,"my_da");
41091c84c290SBarry Smith .ve
41101c84c290SBarry Smith    or at runtime via the option
41111c84c290SBarry Smith .vb
41121c84c290SBarry Smith     -da_type my_da
41131c84c290SBarry Smith .ve
4114264ace61SBarry Smith 
4115264ace61SBarry Smith   Level: advanced
41161c84c290SBarry Smith 
4117bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4118264ace61SBarry Smith @*/
4119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4120d71ae5a4SJacob Faibussowitsch {
4121264ace61SBarry Smith   PetscFunctionBegin;
41229566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
41239566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
4124264ace61SBarry Smith   PetscFunctionReturn(0);
4125264ace61SBarry Smith }
4126264ace61SBarry Smith 
4127b859378eSBarry Smith /*@C
4128bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4129b859378eSBarry Smith 
4130d083f849SBarry Smith   Collective on viewer
4131b859378eSBarry Smith 
4132b859378eSBarry Smith   Input Parameters:
4133bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4134bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4135bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4136bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4137b859378eSBarry Smith 
4138b859378eSBarry Smith    Level: intermediate
4139b859378eSBarry Smith 
4140b859378eSBarry Smith   Notes:
414155849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4142b859378eSBarry Smith 
4143bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4144bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4145bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4146cd7e8a5eSksagiyam 
4147b859378eSBarry Smith   Notes for advanced users:
4148b859378eSBarry Smith   Most users should not need to know the details of the binary storage
4149bb7acecfSBarry Smith   format, since `DMLoad()` and `DMView()` completely hide these details.
4150b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
4151b859378eSBarry Smith   format is
4152b859378eSBarry Smith .vb
4153b859378eSBarry Smith      has not yet been determined
4154b859378eSBarry Smith .ve
4155b859378eSBarry Smith 
4156db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4157b859378eSBarry Smith @*/
4158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4159d71ae5a4SJacob Faibussowitsch {
41609331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4161b859378eSBarry Smith 
4162b859378eSBarry Smith   PetscFunctionBegin;
4163b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4164b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41659566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41669566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41679566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41689566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41699331c7a4SMatthew G. Knepley   if (isbinary) {
41709331c7a4SMatthew G. Knepley     PetscInt classid;
41719331c7a4SMatthew G. Knepley     char     type[256];
4172b859378eSBarry Smith 
41739566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41747a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41759566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41769566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4177dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41789331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4179dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41809331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41819566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
4182b859378eSBarry Smith   PetscFunctionReturn(0);
4183b859378eSBarry Smith }
4184b859378eSBarry Smith 
41857da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41867da65231SMatthew G Knepley 
4187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4188d71ae5a4SJacob Faibussowitsch {
41891d47ebbbSSatish Balay   PetscInt f;
41901b30c384SMatthew G Knepley 
41917da65231SMatthew G Knepley   PetscFunctionBegin;
419263a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
419348a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41947da65231SMatthew G Knepley   PetscFunctionReturn(0);
41957da65231SMatthew G Knepley }
41967da65231SMatthew G Knepley 
4197d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4198d71ae5a4SJacob Faibussowitsch {
41991b30c384SMatthew G Knepley   PetscInt f, g;
42007da65231SMatthew G Knepley 
42017da65231SMatthew G Knepley   PetscFunctionBegin;
420263a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
42031d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
42049566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
420548a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
42069566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
42077da65231SMatthew G Knepley   }
42087da65231SMatthew G Knepley   PetscFunctionReturn(0);
42097da65231SMatthew G Knepley }
4210e7c4fc90SDmitry Karpeev 
4211d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4212d71ae5a4SJacob Faibussowitsch {
42130c5b8624SToby Isaac   PetscInt           localSize, bs;
42140c5b8624SToby Isaac   PetscMPIInt        size;
42150c5b8624SToby Isaac   Vec                x, xglob;
42160c5b8624SToby Isaac   const PetscScalar *xarray;
4217e759306cSMatthew G. Knepley 
4218e759306cSMatthew G. Knepley   PetscFunctionBegin;
42199566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
42209566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
42219566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
42229566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
42239566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
42240c5b8624SToby Isaac   if (size > 1) {
42259566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
42269566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
42279566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
42289566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
42290c5b8624SToby Isaac   } else {
42300c5b8624SToby Isaac     xglob = x;
42310c5b8624SToby Isaac   }
42329566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
42330c5b8624SToby Isaac   if (size > 1) {
42349566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
42359566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
42360c5b8624SToby Isaac   }
42379566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
4238e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4239e759306cSMatthew G. Knepley }
4240e759306cSMatthew G. Knepley 
424188ed4aceSMatthew G Knepley /*@
4242bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4243061576a5SJed Brown 
4244061576a5SJed Brown   Input Parameter:
4245bb7acecfSBarry Smith . dm - The `DM`
4246061576a5SJed Brown 
4247061576a5SJed Brown   Output Parameter:
4248bb7acecfSBarry Smith . section - The `PetscSection`
4249061576a5SJed Brown 
4250061576a5SJed Brown   Options Database Keys:
4251bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4252061576a5SJed Brown 
4253061576a5SJed Brown   Level: advanced
4254061576a5SJed Brown 
4255061576a5SJed Brown   Notes:
4256bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4257061576a5SJed Brown 
4258bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4259061576a5SJed Brown 
4260db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4261061576a5SJed Brown @*/
4262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4263d71ae5a4SJacob Faibussowitsch {
4264061576a5SJed Brown   PetscFunctionBegin;
42659566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
4266061576a5SJed Brown   PetscFunctionReturn(0);
4267061576a5SJed Brown }
4268061576a5SJed Brown 
4269061576a5SJed Brown /*@
4270bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
427188ed4aceSMatthew G Knepley 
427288ed4aceSMatthew G Knepley   Input Parameter:
4273bb7acecfSBarry Smith . dm - The `DM`
427488ed4aceSMatthew G Knepley 
427588ed4aceSMatthew G Knepley   Output Parameter:
4276bb7acecfSBarry Smith . section - The `PetscSection`
427788ed4aceSMatthew G Knepley 
4278e5893cccSMatthew G. Knepley   Options Database Keys:
4279bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4280e5893cccSMatthew G. Knepley 
428188ed4aceSMatthew G Knepley   Level: intermediate
428288ed4aceSMatthew G Knepley 
4283bb7acecfSBarry Smith   Note:
4284bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
428588ed4aceSMatthew G Knepley 
4286db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
428788ed4aceSMatthew G Knepley @*/
4288d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4289d71ae5a4SJacob Faibussowitsch {
429088ed4aceSMatthew G Knepley   PetscFunctionBegin;
429188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42931bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4294e5e52638SMatthew G. Knepley     PetscInt d;
4295e5e52638SMatthew G. Knepley 
429645480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
429745480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
429845480ffeSMatthew G. Knepley       PetscViewer       viewer;
429945480ffeSMatthew G. Knepley       PetscViewerFormat format;
430045480ffeSMatthew G. Knepley       PetscBool         flg;
430145480ffeSMatthew G. Knepley 
43029566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
43039566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
430445480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
43059566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
43069566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
430745480ffeSMatthew G. Knepley       }
430845480ffeSMatthew G. Knepley       if (flg) {
43099566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
43109566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
43119566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
431245480ffeSMatthew G. Knepley       }
431345480ffeSMatthew G. Knepley     }
4314dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
43159566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
43162f0f8703SMatthew G. Knepley   }
43171bb6d2a8SBarry Smith   *section = dm->localSection;
431888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
431988ed4aceSMatthew G Knepley }
432088ed4aceSMatthew G Knepley 
432188ed4aceSMatthew G Knepley /*@
4322bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4323061576a5SJed Brown 
4324061576a5SJed Brown   Input Parameters:
4325bb7acecfSBarry Smith + dm - The `DM`
4326bb7acecfSBarry Smith - section - The `PetscSection`
4327061576a5SJed Brown 
4328061576a5SJed Brown   Level: advanced
4329061576a5SJed Brown 
4330061576a5SJed Brown   Notes:
4331bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4332061576a5SJed Brown 
4333bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4334061576a5SJed Brown 
4335db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4336061576a5SJed Brown @*/
4337d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4338d71ae5a4SJacob Faibussowitsch {
4339061576a5SJed Brown   PetscFunctionBegin;
43409566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
4341061576a5SJed Brown   PetscFunctionReturn(0);
4342061576a5SJed Brown }
4343061576a5SJed Brown 
4344061576a5SJed Brown /*@
4345bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
434688ed4aceSMatthew G Knepley 
434788ed4aceSMatthew G Knepley   Input Parameters:
4348bb7acecfSBarry Smith + dm - The `DM`
4349bb7acecfSBarry Smith - section - The `PetscSection`
435088ed4aceSMatthew G Knepley 
435188ed4aceSMatthew G Knepley   Level: intermediate
435288ed4aceSMatthew G Knepley 
4353bb7acecfSBarry Smith   Note:
4354bb7acecfSBarry Smith   Any existing Section will be destroyed
435588ed4aceSMatthew G Knepley 
4356bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
435788ed4aceSMatthew G Knepley @*/
4358d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4359d71ae5a4SJacob Faibussowitsch {
4360c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4361af122d2aSMatthew G Knepley   PetscInt f;
436288ed4aceSMatthew G Knepley 
436388ed4aceSMatthew G Knepley   PetscFunctionBegin;
436488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4365b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43679566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43681bb6d2a8SBarry Smith   dm->localSection = section;
43699566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4370af122d2aSMatthew G Knepley   if (numFields) {
43719566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4372af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43730f21e855SMatthew G. Knepley       PetscObject disc;
4374af122d2aSMatthew G Knepley       const char *name;
4375af122d2aSMatthew G Knepley 
43769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43779566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43789566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4379af122d2aSMatthew G Knepley     }
4380af122d2aSMatthew G Knepley   }
4381e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43829566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
438388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
438488ed4aceSMatthew G Knepley }
438588ed4aceSMatthew G Knepley 
43869435951eSToby Isaac /*@
4387bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43889435951eSToby Isaac 
4389e228b242SToby Isaac   not collective
4390e228b242SToby Isaac 
43919435951eSToby Isaac   Input Parameter:
4392bb7acecfSBarry Smith . dm - The `DM`
43939435951eSToby Isaac 
4394d8d19677SJose E. Roman   Output Parameters:
4395bb7acecfSBarry 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.
4396bb7acecfSBarry 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.
439779769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43989435951eSToby Isaac 
43999435951eSToby Isaac   Level: advanced
44009435951eSToby Isaac 
4401bb7acecfSBarry Smith   Note:
4402bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
44039435951eSToby Isaac 
4404db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
44059435951eSToby Isaac @*/
4406d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4407d71ae5a4SJacob Faibussowitsch {
44089435951eSToby Isaac   PetscFunctionBegin;
44099435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4410dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
44113b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
44123b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
441379769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
44149435951eSToby Isaac   PetscFunctionReturn(0);
44159435951eSToby Isaac }
44169435951eSToby Isaac 
44179435951eSToby Isaac /*@
4418bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
44199435951eSToby Isaac 
4420bb7acecfSBarry 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()`.
44219435951eSToby Isaac 
4422bb7acecfSBarry 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.
44239435951eSToby Isaac 
4424e228b242SToby Isaac   collective on dm
4425e228b242SToby Isaac 
44269435951eSToby Isaac   Input Parameters:
4427bb7acecfSBarry Smith + dm - The `DM`
4428bb7acecfSBarry 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).
4429bb7acecfSBarry 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).
4430bb7acecfSBarry 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).
44319435951eSToby Isaac 
44329435951eSToby Isaac   Level: advanced
44339435951eSToby Isaac 
4434bb7acecfSBarry Smith   Note:
4435bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
44369435951eSToby Isaac 
4437db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
44389435951eSToby Isaac @*/
4439d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4440d71ae5a4SJacob Faibussowitsch {
4441e228b242SToby Isaac   PetscMPIInt result;
44429435951eSToby Isaac 
44439435951eSToby Isaac   PetscFunctionBegin;
44449435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4445e228b242SToby Isaac   if (section) {
4446e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
44487a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4449e228b242SToby Isaac   }
4450e228b242SToby Isaac   if (mat) {
4451e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
44529566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
44537a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4454e228b242SToby Isaac   }
445579769bd5SJed Brown   if (bias) {
445679769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44579566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
445879769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
445979769bd5SJed Brown   }
44609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44619566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44623b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44649566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44653b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44669566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
446879769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44699435951eSToby Isaac   PetscFunctionReturn(0);
44709435951eSToby Isaac }
44719435951eSToby Isaac 
4472497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4473507e4973SMatthew G. Knepley /*
4474bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4475507e4973SMatthew G. Knepley 
4476507e4973SMatthew G. Knepley   Input Parameters:
4477bb7acecfSBarry Smith + dm - The `DM`
4478bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4479bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4480507e4973SMatthew G. Knepley 
4481507e4973SMatthew G. Knepley   Level: intermediate
4482507e4973SMatthew G. Knepley 
4483db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4484507e4973SMatthew G. Knepley */
4485d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4486d71ae5a4SJacob Faibussowitsch {
4487507e4973SMatthew G. Knepley   MPI_Comm        comm;
4488507e4973SMatthew G. Knepley   PetscLayout     layout;
4489507e4973SMatthew G. Knepley   const PetscInt *ranges;
4490507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4491507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4492507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4493507e4973SMatthew G. Knepley 
4494507e4973SMatthew G. Knepley   PetscFunctionBegin;
44959566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4496507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44999566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
45009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
45019566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
45029566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
45039566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
45049566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
45059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4506507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4507f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4508507e4973SMatthew G. Knepley 
45099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
45109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
45119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
45129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
45139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
45149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4515507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
45169371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
45179371c9d4SSatish 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));
45189371c9d4SSatish Balay       valid = PETSC_FALSE;
45199371c9d4SSatish Balay     }
45209371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
45219371c9d4SSatish 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));
45229371c9d4SSatish Balay       valid = PETSC_FALSE;
45239371c9d4SSatish Balay     }
4524507e4973SMatthew G. Knepley     if (gdof < 0) {
4525507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4526507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4527507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4528507e4973SMatthew G. Knepley 
45299566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4530507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
45319371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
45329371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
45339371c9d4SSatish Balay           valid = PETSC_FALSE;
45349371c9d4SSatish Balay           break;
45359371c9d4SSatish Balay         }
4536507e4973SMatthew G. Knepley       }
4537507e4973SMatthew G. Knepley     }
4538507e4973SMatthew G. Knepley   }
45399566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
45409566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
45411c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4542507e4973SMatthew G. Knepley   if (!gvalid) {
45439566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4544507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4545507e4973SMatthew G. Knepley   }
4546507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4547507e4973SMatthew G. Knepley }
4548f741bcd2SMatthew G. Knepley #endif
4549507e4973SMatthew G. Knepley 
45505f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
45516725e60dSJed Brown {
45526725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
45536725e60dSJed Brown   PetscFunctionBegin;
45546725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45556725e60dSJed Brown   PetscValidPointer(sf, 2);
45565f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45576725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45586725e60dSJed Brown   else *sf = dm->sf;
45596725e60dSJed Brown   PetscFunctionReturn(0);
45606725e60dSJed Brown }
45616725e60dSJed Brown 
456288ed4aceSMatthew G Knepley /*@
4563bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
456488ed4aceSMatthew G Knepley 
4565d083f849SBarry Smith   Collective on dm
45668b1ab98fSJed Brown 
456788ed4aceSMatthew G Knepley   Input Parameter:
4568bb7acecfSBarry Smith . dm - The `DM`
456988ed4aceSMatthew G Knepley 
457088ed4aceSMatthew G Knepley   Output Parameter:
4571bb7acecfSBarry Smith . section - The `PetscSection`
457288ed4aceSMatthew G Knepley 
457388ed4aceSMatthew G Knepley   Level: intermediate
457488ed4aceSMatthew G Knepley 
4575bb7acecfSBarry Smith   Note:
4576bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
457788ed4aceSMatthew G Knepley 
4578db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
457988ed4aceSMatthew G Knepley @*/
4580d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4581d71ae5a4SJacob Faibussowitsch {
458288ed4aceSMatthew G Knepley   PetscFunctionBegin;
458388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
458488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45851bb6d2a8SBarry Smith   if (!dm->globalSection) {
4586fd59a867SMatthew G. Knepley     PetscSection s;
45876725e60dSJed Brown     PetscSF      sf;
4588fd59a867SMatthew G. Knepley 
45899566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45907a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45917a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45925f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45936725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45949566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45959566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45969566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
459788ed4aceSMatthew G Knepley   }
45981bb6d2a8SBarry Smith   *section = dm->globalSection;
459988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
460088ed4aceSMatthew G Knepley }
460188ed4aceSMatthew G Knepley 
4602b21d0597SMatthew G Knepley /*@
4603bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4604b21d0597SMatthew G Knepley 
4605b21d0597SMatthew G Knepley   Input Parameters:
4606bb7acecfSBarry Smith + dm - The `DM`
46075080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4608b21d0597SMatthew G Knepley 
4609b21d0597SMatthew G Knepley   Level: intermediate
4610b21d0597SMatthew G Knepley 
4611bb7acecfSBarry Smith   Note:
4612bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4613b21d0597SMatthew G Knepley 
4614db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4615b21d0597SMatthew G Knepley @*/
4616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4617d71ae5a4SJacob Faibussowitsch {
4618b21d0597SMatthew G Knepley   PetscFunctionBegin;
4619b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46205080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
46219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
46229566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
46231bb6d2a8SBarry Smith   dm->globalSection = section;
4624497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
46259566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4626507e4973SMatthew G. Knepley #endif
4627b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4628b21d0597SMatthew G Knepley }
4629b21d0597SMatthew G Knepley 
463088ed4aceSMatthew G Knepley /*@
4631bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4632bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
463388ed4aceSMatthew G Knepley 
463488ed4aceSMatthew G Knepley   Input Parameter:
4635bb7acecfSBarry Smith . dm - The `DM`
463688ed4aceSMatthew G Knepley 
463788ed4aceSMatthew G Knepley   Output Parameter:
4638bb7acecfSBarry Smith . sf - The `PetscSF`
463988ed4aceSMatthew G Knepley 
464088ed4aceSMatthew G Knepley   Level: intermediate
464188ed4aceSMatthew G Knepley 
4642bb7acecfSBarry Smith   Note:
4643bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
464488ed4aceSMatthew G Knepley 
4645db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
464688ed4aceSMatthew G Knepley @*/
4647d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4648d71ae5a4SJacob Faibussowitsch {
464988ed4aceSMatthew G Knepley   PetscInt nroots;
465088ed4aceSMatthew G Knepley 
465188ed4aceSMatthew G Knepley   PetscFunctionBegin;
465288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
465388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
465448a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
46559566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
465688ed4aceSMatthew G Knepley   if (nroots < 0) {
465788ed4aceSMatthew G Knepley     PetscSection section, gSection;
465888ed4aceSMatthew G Knepley 
46599566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
466031ea6d37SMatthew G Knepley     if (section) {
46619566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46629566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
466331ea6d37SMatthew G Knepley     } else {
46640298fd71SBarry Smith       *sf = NULL;
466531ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
466631ea6d37SMatthew G Knepley     }
466788ed4aceSMatthew G Knepley   }
46681bb6d2a8SBarry Smith   *sf = dm->sectionSF;
466988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
467088ed4aceSMatthew G Knepley }
467188ed4aceSMatthew G Knepley 
467288ed4aceSMatthew G Knepley /*@
4673bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
467488ed4aceSMatthew G Knepley 
467588ed4aceSMatthew G Knepley   Input Parameters:
4676bb7acecfSBarry Smith + dm - The `DM`
4677bb7acecfSBarry Smith - sf - The `PetscSF`
467888ed4aceSMatthew G Knepley 
467988ed4aceSMatthew G Knepley   Level: intermediate
468088ed4aceSMatthew G Knepley 
4681bb7acecfSBarry Smith   Note:
4682bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
468388ed4aceSMatthew G Knepley 
4684db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
468588ed4aceSMatthew G Knepley @*/
4686d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4687d71ae5a4SJacob Faibussowitsch {
468888ed4aceSMatthew G Knepley   PetscFunctionBegin;
468988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4690b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46919566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46929566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46931bb6d2a8SBarry Smith   dm->sectionSF = sf;
469488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
469588ed4aceSMatthew G Knepley }
469688ed4aceSMatthew G Knepley 
469788ed4aceSMatthew G Knepley /*@C
4698bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
469988ed4aceSMatthew G Knepley   describing the data layout.
470088ed4aceSMatthew G Knepley 
470188ed4aceSMatthew G Knepley   Input Parameters:
4702bb7acecfSBarry Smith + dm - The `DM`
4703bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4704bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
470588ed4aceSMatthew G Knepley 
47061bb6d2a8SBarry Smith   Level: developer
47071bb6d2a8SBarry Smith 
4708bb7acecfSBarry Smith   Note:
4709bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4710bb7acecfSBarry Smith 
4711bb7acecfSBarry Smith   Developer Note:
4712bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4713bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4714bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
47151bb6d2a8SBarry Smith 
4716db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
471788ed4aceSMatthew G Knepley @*/
4718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4719d71ae5a4SJacob Faibussowitsch {
472088ed4aceSMatthew G Knepley   PetscFunctionBegin;
472188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47229566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
472388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
472488ed4aceSMatthew G Knepley }
4725af122d2aSMatthew G Knepley 
4726b21d0597SMatthew G Knepley /*@
4727bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4728bb7acecfSBarry Smith 
4729bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4730b21d0597SMatthew G Knepley 
4731b21d0597SMatthew G Knepley   Input Parameter:
4732bb7acecfSBarry Smith . dm - The `DM`
4733b21d0597SMatthew G Knepley 
4734b21d0597SMatthew G Knepley   Output Parameter:
4735bb7acecfSBarry Smith . sf - The `PetscSF`
4736b21d0597SMatthew G Knepley 
4737b21d0597SMatthew G Knepley   Level: intermediate
4738b21d0597SMatthew G Knepley 
4739bb7acecfSBarry Smith   Note:
4740bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4741b21d0597SMatthew G Knepley 
4742db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4743b21d0597SMatthew G Knepley @*/
4744d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4745d71ae5a4SJacob Faibussowitsch {
4746b21d0597SMatthew G Knepley   PetscFunctionBegin;
4747b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4748b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4749b21d0597SMatthew G Knepley   *sf = dm->sf;
4750b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4751b21d0597SMatthew G Knepley }
4752b21d0597SMatthew G Knepley 
4753057b4bcdSMatthew G Knepley /*@
4754bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4755bb7acecfSBarry Smith 
4756bb7acecfSBarry Smith   Collective on dm
4757057b4bcdSMatthew G Knepley 
4758057b4bcdSMatthew G Knepley   Input Parameters:
4759bb7acecfSBarry Smith + dm - The `DM`
4760bb7acecfSBarry Smith - sf - The` PetscSF`
4761057b4bcdSMatthew G Knepley 
4762057b4bcdSMatthew G Knepley   Level: intermediate
4763057b4bcdSMatthew G Knepley 
4764db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4765057b4bcdSMatthew G Knepley @*/
4766d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4767d71ae5a4SJacob Faibussowitsch {
4768057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4769057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4770b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47719566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47729566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4773057b4bcdSMatthew G Knepley   dm->sf = sf;
4774057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4775057b4bcdSMatthew G Knepley }
4776057b4bcdSMatthew G Knepley 
47774f37162bSMatthew G. Knepley /*@
4778bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47794f37162bSMatthew G. Knepley 
47804f37162bSMatthew G. Knepley   Input Parameter:
4781bb7acecfSBarry Smith . dm - The `DM`
47824f37162bSMatthew G. Knepley 
47834f37162bSMatthew G. Knepley   Output Parameter:
4784bb7acecfSBarry Smith . sf - The `PetscSF`
47854f37162bSMatthew G. Knepley 
47864f37162bSMatthew G. Knepley   Level: intermediate
47874f37162bSMatthew G. Knepley 
4788bb7acecfSBarry Smith   Note:
4789bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47904f37162bSMatthew G. Knepley 
4791db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47924f37162bSMatthew G. Knepley @*/
4793d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4794d71ae5a4SJacob Faibussowitsch {
47954f37162bSMatthew G. Knepley   PetscFunctionBegin;
47964f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47974f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47984f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47994f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
48004f37162bSMatthew G. Knepley }
48014f37162bSMatthew G. Knepley 
48024f37162bSMatthew G. Knepley /*@
48034f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
48044f37162bSMatthew G. Knepley 
48054f37162bSMatthew G. Knepley   Input Parameters:
48064f37162bSMatthew G. Knepley + dm - The DM
48074f37162bSMatthew G. Knepley - sf - The PetscSF
48084f37162bSMatthew G. Knepley 
48094f37162bSMatthew G. Knepley   Level: intermediate
48104f37162bSMatthew G. Knepley 
4811db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
48124f37162bSMatthew G. Knepley @*/
4813d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4814d71ae5a4SJacob Faibussowitsch {
48154f37162bSMatthew G. Knepley   PetscFunctionBegin;
48164f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48174f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
48189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
48199566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
48204f37162bSMatthew G. Knepley   dm->sfNatural = sf;
48214f37162bSMatthew G. Knepley   PetscFunctionReturn(0);
48224f37162bSMatthew G. Knepley }
48234f37162bSMatthew G. Knepley 
4824d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4825d71ae5a4SJacob Faibussowitsch {
482634aa8a36SMatthew G. Knepley   PetscClassId id;
482734aa8a36SMatthew G. Knepley 
482834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
48299566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
483034aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
48319566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483234aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
48339566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
483417c1d62eSMatthew G. Knepley   } else {
48359566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
483634aa8a36SMatthew G. Knepley   }
483734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
483834aa8a36SMatthew G. Knepley }
483934aa8a36SMatthew G. Knepley 
4840d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4841d71ae5a4SJacob Faibussowitsch {
484244a7f3ddSMatthew G. Knepley   RegionField *tmpr;
484344a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
484444a7f3ddSMatthew G. Knepley 
484544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
484644a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
48479566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
484844a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
48499371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
48509371c9d4SSatish Balay     tmpr[f].disc        = NULL;
48519371c9d4SSatish Balay     tmpr[f].label       = NULL;
48529371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
48539371c9d4SSatish Balay   }
48549566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
485544a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
485644a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
485744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
485844a7f3ddSMatthew G. Knepley }
485944a7f3ddSMatthew G. Knepley 
486044a7f3ddSMatthew G. Knepley /*@
486144a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
486244a7f3ddSMatthew G. Knepley 
4863d083f849SBarry Smith   Logically collective on dm
486444a7f3ddSMatthew G. Knepley 
486544a7f3ddSMatthew G. Knepley   Input Parameter:
486644a7f3ddSMatthew G. Knepley . dm - The DM
486744a7f3ddSMatthew G. Knepley 
486844a7f3ddSMatthew G. Knepley   Level: intermediate
486944a7f3ddSMatthew G. Knepley 
4870db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
487144a7f3ddSMatthew G. Knepley @*/
4872d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4873d71ae5a4SJacob Faibussowitsch {
487444a7f3ddSMatthew G. Knepley   PetscInt f;
487544a7f3ddSMatthew G. Knepley 
487644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
487744a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
487844a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48799566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48809566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
488144a7f3ddSMatthew G. Knepley   }
48829566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
488344a7f3ddSMatthew G. Knepley   dm->fields = NULL;
488444a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
488544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
488644a7f3ddSMatthew G. Knepley }
488744a7f3ddSMatthew G. Knepley 
4888689b5837SMatthew G. Knepley /*@
4889689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4890689b5837SMatthew G. Knepley 
4891689b5837SMatthew G. Knepley   Not collective
4892689b5837SMatthew G. Knepley 
4893689b5837SMatthew G. Knepley   Input Parameter:
4894689b5837SMatthew G. Knepley . dm - The DM
4895689b5837SMatthew G. Knepley 
4896689b5837SMatthew G. Knepley   Output Parameter:
4897689b5837SMatthew G. Knepley . Nf - The number of fields
4898689b5837SMatthew G. Knepley 
4899689b5837SMatthew G. Knepley   Level: intermediate
4900689b5837SMatthew G. Knepley 
4901db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4902689b5837SMatthew G. Knepley @*/
4903d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4904d71ae5a4SJacob Faibussowitsch {
49050f21e855SMatthew G. Knepley   PetscFunctionBegin;
49060f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4907534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
490844a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4909af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4910af122d2aSMatthew G Knepley }
4911af122d2aSMatthew G Knepley 
4912689b5837SMatthew G. Knepley /*@
4913689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4914689b5837SMatthew G. Knepley 
4915d083f849SBarry Smith   Logically collective on dm
4916689b5837SMatthew G. Knepley 
4917689b5837SMatthew G. Knepley   Input Parameters:
4918689b5837SMatthew G. Knepley + dm - The DM
4919689b5837SMatthew G. Knepley - Nf - The number of fields
4920689b5837SMatthew G. Knepley 
4921689b5837SMatthew G. Knepley   Level: intermediate
4922689b5837SMatthew G. Knepley 
4923db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4924689b5837SMatthew G. Knepley @*/
4925d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4926d71ae5a4SJacob Faibussowitsch {
49270f21e855SMatthew G. Knepley   PetscInt Nf, f;
4928af122d2aSMatthew G Knepley 
4929af122d2aSMatthew G Knepley   PetscFunctionBegin;
4930af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49319566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
49320f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
49330f21e855SMatthew G. Knepley     PetscContainer obj;
49340f21e855SMatthew G. Knepley 
49359566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
49369566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
49379566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4938af122d2aSMatthew G Knepley   }
4939af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4940af122d2aSMatthew G Knepley }
4941af122d2aSMatthew G Knepley 
4942c1929be8SMatthew G. Knepley /*@
4943bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4944c1929be8SMatthew G. Knepley 
4945c1929be8SMatthew G. Knepley   Not collective
4946c1929be8SMatthew G. Knepley 
4947c1929be8SMatthew G. Knepley   Input Parameters:
4948bb7acecfSBarry Smith + dm - The `DM`
4949c1929be8SMatthew G. Knepley - f  - The field number
4950c1929be8SMatthew G. Knepley 
495144a7f3ddSMatthew G. Knepley   Output Parameters:
4952bb7acecfSBarry Smith + label - The label indicating the support of the field, or NULL for the entire mesh (pass in NULL if not needed)
4953bb7acecfSBarry Smith - disc - The discretization object (pass in NULL if not needed)
4954c1929be8SMatthew G. Knepley 
495544a7f3ddSMatthew G. Knepley   Level: intermediate
4956c1929be8SMatthew G. Knepley 
4957db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4958c1929be8SMatthew G. Knepley @*/
4959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4960d71ae5a4SJacob Faibussowitsch {
4961af122d2aSMatthew G Knepley   PetscFunctionBegin;
4962af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4963bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49647a8be351SBarry 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);
496544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4966bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
4967decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4968decb47aaSMatthew G. Knepley }
4969decb47aaSMatthew G. Knepley 
4970083401c6SMatthew G. Knepley /* Does not clear the DS */
4971d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4972d71ae5a4SJacob Faibussowitsch {
4973083401c6SMatthew G. Knepley   PetscFunctionBegin;
49749566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49759566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49769566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4977083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4978bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49799566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4980bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4981083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4982083401c6SMatthew G. Knepley }
4983083401c6SMatthew G. Knepley 
498446560f82SMatthew G. Knepley /*@C
4985bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4986bb7acecfSBarry Smith   the field numbering.
4987c1929be8SMatthew G. Knepley 
4988d083f849SBarry Smith   Logically collective on dm
4989c1929be8SMatthew G. Knepley 
4990c1929be8SMatthew G. Knepley   Input Parameters:
4991bb7acecfSBarry Smith + dm    - The `DM`
4992c1929be8SMatthew G. Knepley . f     - The field number
499344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4994bb7acecfSBarry Smith - disc - The discretization object
4995c1929be8SMatthew G. Knepley 
499644a7f3ddSMatthew G. Knepley   Level: intermediate
4997c1929be8SMatthew G. Knepley 
4998db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4999c1929be8SMatthew G. Knepley @*/
5000d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
5001d71ae5a4SJacob Faibussowitsch {
5002decb47aaSMatthew G. Knepley   PetscFunctionBegin;
5003decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5004e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
5005bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
50067a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
5007bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
5008bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
50099566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
501044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
501144a7f3ddSMatthew G. Knepley }
501244a7f3ddSMatthew G. Knepley 
501346560f82SMatthew G. Knepley /*@C
5014bb7acecfSBarry 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)
5015bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
501644a7f3ddSMatthew G. Knepley 
5017d083f849SBarry Smith   Logically collective on dm
501844a7f3ddSMatthew G. Knepley 
501944a7f3ddSMatthew G. Knepley   Input Parameters:
5020bb7acecfSBarry Smith + dm    - The `DM`
502144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
5022bb7acecfSBarry Smith - disc - The discretization object
502344a7f3ddSMatthew G. Knepley 
502444a7f3ddSMatthew G. Knepley   Level: intermediate
502544a7f3ddSMatthew G. Knepley 
5026bb7acecfSBarry Smith   Notes:
5027bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
5028bb7acecfSBarry Smith 
5029bb7acecfSBarry Smith   For example, a piecewise continous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
5030bb7acecfSBarry 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
5031bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
5032bb7acecfSBarry Smith 
5033bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
503444a7f3ddSMatthew G. Knepley @*/
5035d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
5036d71ae5a4SJacob Faibussowitsch {
503744a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
503844a7f3ddSMatthew G. Knepley 
503944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
504044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5041064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5042bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
50439566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
504444a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
5045bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
50469566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
5047bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
5048bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
50499566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
5050af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
5051af122d2aSMatthew G Knepley }
50526636e97aSMatthew G Knepley 
5053e5e52638SMatthew G. Knepley /*@
5054e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
5055e0b68406SMatthew Knepley 
5056e0b68406SMatthew Knepley   Logically collective on dm
5057e0b68406SMatthew Knepley 
5058e0b68406SMatthew Knepley   Input Parameters:
5059bb7acecfSBarry Smith + dm          - The `DM`
5060e0b68406SMatthew Knepley . f           - The field index
5061bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5062e0b68406SMatthew Knepley 
5063e0b68406SMatthew Knepley   Level: intermediate
5064e0b68406SMatthew Knepley 
5065db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5066e0b68406SMatthew Knepley @*/
5067d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5068d71ae5a4SJacob Faibussowitsch {
5069e0b68406SMatthew Knepley   PetscFunctionBegin;
507063a3b9bcSJacob 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);
5071e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
5072e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5073e0b68406SMatthew Knepley }
5074e0b68406SMatthew Knepley 
5075e0b68406SMatthew Knepley /*@
5076e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5077e0b68406SMatthew Knepley 
5078bb7acecfSBarry Smith   Not collective
5079e0b68406SMatthew Knepley 
5080e0b68406SMatthew Knepley   Input Parameters:
5081bb7acecfSBarry Smith + dm          - The `DM`
5082e0b68406SMatthew Knepley - f           - The field index
5083e0b68406SMatthew Knepley 
5084e0b68406SMatthew Knepley   Output Parameter:
5085e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5086e0b68406SMatthew Knepley 
5087e0b68406SMatthew Knepley   Level: intermediate
5088e0b68406SMatthew Knepley 
5089bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5090e0b68406SMatthew Knepley @*/
5091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5092d71ae5a4SJacob Faibussowitsch {
5093e0b68406SMatthew Knepley   PetscFunctionBegin;
509463a3b9bcSJacob 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);
5095e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
5096e0b68406SMatthew Knepley   PetscFunctionReturn(0);
5097e0b68406SMatthew Knepley }
5098e0b68406SMatthew Knepley 
5099e0b68406SMatthew Knepley /*@
5100bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5101e5e52638SMatthew G. Knepley 
5102d083f849SBarry Smith   Collective on dm
5103e5e52638SMatthew G. Knepley 
5104e5e52638SMatthew G. Knepley   Input Parameter:
5105bb7acecfSBarry Smith . dm - The `DM`
5106e5e52638SMatthew G. Knepley 
5107e5e52638SMatthew G. Knepley   Output Parameter:
5108bb7acecfSBarry Smith . newdm - The `DM`
5109e5e52638SMatthew G. Knepley 
5110e5e52638SMatthew G. Knepley   Level: advanced
5111e5e52638SMatthew G. Knepley 
5112db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5113e5e52638SMatthew G. Knepley @*/
5114d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5115d71ae5a4SJacob Faibussowitsch {
5116e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5117e5e52638SMatthew G. Knepley 
5118e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5119e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
51209566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
51219566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5122e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5123e5e52638SMatthew G. Knepley     DMLabel     label;
5124e5e52638SMatthew G. Knepley     PetscObject field;
512534aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5126e5e52638SMatthew G. Knepley 
51279566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
51289566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
51299566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
51309566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
513134aa8a36SMatthew G. Knepley   }
513234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
513334aa8a36SMatthew G. Knepley }
513434aa8a36SMatthew G. Knepley 
513534aa8a36SMatthew G. Knepley /*@
513634aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
513734aa8a36SMatthew G. Knepley 
513834aa8a36SMatthew G. Knepley   Not collective
513934aa8a36SMatthew G. Knepley 
514034aa8a36SMatthew G. Knepley   Input Parameters:
514134aa8a36SMatthew G. Knepley + dm - The DM object
514234aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
514334aa8a36SMatthew G. Knepley 
5144d8d19677SJose E. Roman   Output Parameters:
514534aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
514634aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
514734aa8a36SMatthew G. Knepley 
514834aa8a36SMatthew G. Knepley   Notes:
514934aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
515034aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
515134aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5152979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
515334aa8a36SMatthew G. Knepley 
515434aa8a36SMatthew G. Knepley   Level: developer
515534aa8a36SMatthew G. Knepley 
5156db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
515734aa8a36SMatthew G. Knepley @*/
5158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5159d71ae5a4SJacob Faibussowitsch {
516034aa8a36SMatthew G. Knepley   PetscFunctionBegin;
516134aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5162534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5163534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
516434aa8a36SMatthew G. Knepley   if (f < 0) {
516534aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
516634aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
516734aa8a36SMatthew G. Knepley   } else {
516834aa8a36SMatthew G. Knepley     PetscInt Nf;
516934aa8a36SMatthew G. Knepley 
51709566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51717a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
517234aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
517334aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
517434aa8a36SMatthew G. Knepley   }
517534aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
517634aa8a36SMatthew G. Knepley }
517734aa8a36SMatthew G. Knepley 
517834aa8a36SMatthew G. Knepley /*@
517934aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
518034aa8a36SMatthew G. Knepley 
518134aa8a36SMatthew G. Knepley   Not collective
518234aa8a36SMatthew G. Knepley 
518334aa8a36SMatthew G. Knepley   Input Parameters:
518434aa8a36SMatthew G. Knepley + dm         - The DM object
518534aa8a36SMatthew G. Knepley . f          - The field number
518634aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
518734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
518834aa8a36SMatthew G. Knepley 
518934aa8a36SMatthew G. Knepley   Notes:
519034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
519134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
519234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5193979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
519434aa8a36SMatthew G. Knepley 
519534aa8a36SMatthew G. Knepley   Level: developer
519634aa8a36SMatthew G. Knepley 
5197db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
519834aa8a36SMatthew G. Knepley @*/
5199d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5200d71ae5a4SJacob Faibussowitsch {
520134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
520234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
520334aa8a36SMatthew G. Knepley   if (f < 0) {
520434aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
520534aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
520634aa8a36SMatthew G. Knepley   } else {
520734aa8a36SMatthew G. Knepley     PetscInt Nf;
520834aa8a36SMatthew G. Knepley 
52099566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
52107a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
521134aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
521234aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5213e5e52638SMatthew G. Knepley   }
5214e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5215e5e52638SMatthew G. Knepley }
5216e5e52638SMatthew G. Knepley 
5217b0441da4SMatthew G. Knepley /*@
5218b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5219b0441da4SMatthew G. Knepley 
5220b0441da4SMatthew G. Knepley   Not collective
5221b0441da4SMatthew G. Knepley 
5222f899ff85SJose E. Roman   Input Parameter:
5223b0441da4SMatthew G. Knepley . dm - The DM object
5224b0441da4SMatthew G. Knepley 
5225d8d19677SJose E. Roman   Output Parameters:
5226b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5227b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5228b0441da4SMatthew G. Knepley 
5229b0441da4SMatthew G. Knepley   Notes:
5230b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5231b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5232b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5233b0441da4SMatthew G. Knepley 
5234b0441da4SMatthew G. Knepley   Level: developer
5235b0441da4SMatthew G. Knepley 
5236db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5237b0441da4SMatthew G. Knepley @*/
5238d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5239d71ae5a4SJacob Faibussowitsch {
5240b0441da4SMatthew G. Knepley   PetscInt Nf;
5241b0441da4SMatthew G. Knepley 
5242b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5243b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5244064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5245064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
52469566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5247b0441da4SMatthew G. Knepley   if (!Nf) {
52489566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5249b0441da4SMatthew G. Knepley   } else {
52509566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5251b0441da4SMatthew G. Knepley   }
5252b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
5253b0441da4SMatthew G. Knepley }
5254b0441da4SMatthew G. Knepley 
5255b0441da4SMatthew G. Knepley /*@
5256b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5257b0441da4SMatthew G. Knepley 
5258b0441da4SMatthew G. Knepley   Not collective
5259b0441da4SMatthew G. Knepley 
5260b0441da4SMatthew G. Knepley   Input Parameters:
5261b0441da4SMatthew G. Knepley + dm         - The DM object
5262b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5263b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5264b0441da4SMatthew G. Knepley 
5265b0441da4SMatthew G. Knepley   Notes:
5266b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5267b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5268b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5269b0441da4SMatthew G. Knepley 
5270b0441da4SMatthew G. Knepley   Level: developer
5271b0441da4SMatthew G. Knepley 
5272db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5273b0441da4SMatthew G. Knepley @*/
5274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5275d71ae5a4SJacob Faibussowitsch {
5276b0441da4SMatthew G. Knepley   PetscInt Nf;
5277b0441da4SMatthew G. Knepley 
5278b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5279b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52809566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5281b0441da4SMatthew G. Knepley   if (!Nf) {
52829566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5283b0441da4SMatthew G. Knepley   } else {
52849566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5285e5e52638SMatthew G. Knepley   }
5286e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5287e5e52638SMatthew G. Knepley }
5288e5e52638SMatthew G. Knepley 
5289d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5290d71ae5a4SJacob Faibussowitsch {
5291799db056SMatthew G. Knepley   DM           plex;
5292799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5293799db056SMatthew G. Knepley   const char **names;
5294799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5295799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5296799db056SMatthew G. Knepley   size_t       len;
5297799db056SMatthew G. Knepley   MPI_Comm     comm;
5298799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5299783e2ec8SMatthew G. Knepley 
5300783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5301799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5302799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5303799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5304799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5305799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5306799db056SMatthew G. Knepley     PetscDS  dsBC;
5307799db056SMatthew G. Knepley     PetscInt numBd;
5308799db056SMatthew G. Knepley 
5309799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5310799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5311799db056SMatthew G. Knepley     maxLabels += numBd;
5312799db056SMatthew G. Knepley   }
5313799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5314799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5315799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5316799db056SMatthew G. Knepley     PetscDS  dsBC;
5317799db056SMatthew G. Knepley     PetscInt numBd, bd;
5318799db056SMatthew G. Knepley 
5319799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5320799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5321799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5322799db056SMatthew G. Knepley       DMLabel      label;
5323799db056SMatthew G. Knepley       PetscInt     field;
5324799db056SMatthew G. Knepley       PetscObject  obj;
5325799db056SMatthew G. Knepley       PetscClassId id;
5326799db056SMatthew G. Knepley 
5327799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
53289566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
53299566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5330799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
53319371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
53329371c9d4SSatish Balay         if (labels[l] == label) break;
5333799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5334783e2ec8SMatthew G. Knepley     }
5335799db056SMatthew G. Knepley   }
5336799db056SMatthew G. Knepley   /* Get label names */
5337799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5338799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
53399371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
53409371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
53419371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
53429371c9d4SSatish Balay   }
5343799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5344799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5345799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5346799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscStrcpy(&sendNames[gmaxLen * l], names[l]));
5347799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5348799db056SMatthew G. Knepley   /* Put all names on all processes */
5349799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5350799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5351799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5352799db056SMatthew G. Knepley   gNl = displs[size];
53539371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53549371c9d4SSatish Balay     counts[p] *= gmaxLen;
53559371c9d4SSatish Balay     displs[p] *= gmaxLen;
53569371c9d4SSatish Balay   }
5357799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5358799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5359799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5360799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5361799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5362799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5363799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53649371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53659371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53669566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5367799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53689566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5369799db056SMatthew G. Knepley     ++gl;
5370783e2ec8SMatthew G. Knepley   }
5371799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
5372783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5373783e2ec8SMatthew G. Knepley }
5374783e2ec8SMatthew G. Knepley 
5375d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5376d71ae5a4SJacob Faibussowitsch {
5377e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5378e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5379e5e52638SMatthew G. Knepley 
5380e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5381e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
53829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5383e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53849371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53859371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53869371c9d4SSatish Balay     tmpd[s].label  = NULL;
53879371c9d4SSatish Balay     tmpd[s].fields = NULL;
53889371c9d4SSatish Balay   }
53899566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5390e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5391e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5392e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5393e5e52638SMatthew G. Knepley }
5394e5e52638SMatthew G. Knepley 
5395e5e52638SMatthew G. Knepley /*@
5396e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5397e5e52638SMatthew G. Knepley 
5398e5e52638SMatthew G. Knepley   Not collective
5399e5e52638SMatthew G. Knepley 
5400e5e52638SMatthew G. Knepley   Input Parameter:
5401e5e52638SMatthew G. Knepley . dm - The DM
5402e5e52638SMatthew G. Knepley 
5403e5e52638SMatthew G. Knepley   Output Parameter:
5404e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5405e5e52638SMatthew G. Knepley 
5406e5e52638SMatthew G. Knepley   Level: intermediate
5407e5e52638SMatthew G. Knepley 
5408db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5409e5e52638SMatthew G. Knepley @*/
5410d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5411d71ae5a4SJacob Faibussowitsch {
5412e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5413e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5414534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5415e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5416e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5417e5e52638SMatthew G. Knepley }
5418e5e52638SMatthew G. Knepley 
5419e5e52638SMatthew G. Knepley /*@
5420e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5421e5e52638SMatthew G. Knepley 
5422d083f849SBarry Smith   Logically collective on dm
5423e5e52638SMatthew G. Knepley 
5424e5e52638SMatthew G. Knepley   Input Parameter:
5425e5e52638SMatthew G. Knepley . dm - The DM
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley   Level: intermediate
5428e5e52638SMatthew G. Knepley 
5429db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5430e5e52638SMatthew G. Knepley @*/
5431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5432d71ae5a4SJacob Faibussowitsch {
5433e5e52638SMatthew G. Knepley   PetscInt s;
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5436e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5437e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
54389566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
54399566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
54409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5441e5e52638SMatthew G. Knepley   }
54429566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5443e5e52638SMatthew G. Knepley   dm->probs = NULL;
5444e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5445e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5446e5e52638SMatthew G. Knepley }
5447e5e52638SMatthew G. Knepley 
5448e5e52638SMatthew G. Knepley /*@
5449e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley   Not collective
5452e5e52638SMatthew G. Knepley 
5453e5e52638SMatthew G. Knepley   Input Parameter:
5454e5e52638SMatthew G. Knepley . dm    - The DM
5455e5e52638SMatthew G. Knepley 
5456e5e52638SMatthew G. Knepley   Output Parameter:
5457e5e52638SMatthew G. Knepley . prob - The default PetscDS
5458e5e52638SMatthew G. Knepley 
5459e5e52638SMatthew G. Knepley   Level: intermediate
5460e5e52638SMatthew G. Knepley 
5461db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5462e5e52638SMatthew G. Knepley @*/
5463d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5464d71ae5a4SJacob Faibussowitsch {
5465e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5466e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5467e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5468b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5469b0143b4dSMatthew G. Knepley     PetscDS ds;
5470b0143b4dSMatthew G. Knepley 
54719566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54729566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54739566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5474b0143b4dSMatthew G. Knepley   }
5475b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5476e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5477e5e52638SMatthew G. Knepley }
5478e5e52638SMatthew G. Knepley 
5479e5e52638SMatthew G. Knepley /*@
5480e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5481e5e52638SMatthew G. Knepley 
5482e5e52638SMatthew G. Knepley   Not collective
5483e5e52638SMatthew G. Knepley 
5484e5e52638SMatthew G. Knepley   Input Parameters:
5485e5e52638SMatthew G. Knepley + dm    - The DM
5486e5e52638SMatthew G. Knepley - point - Cell for the DS
5487e5e52638SMatthew G. Knepley 
5488e5e52638SMatthew G. Knepley   Output Parameter:
5489e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5490e5e52638SMatthew G. Knepley 
5491e5e52638SMatthew G. Knepley   Level: developer
5492e5e52638SMatthew G. Knepley 
5493db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5494e5e52638SMatthew G. Knepley @*/
5495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5496d71ae5a4SJacob Faibussowitsch {
5497e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5498e5e52638SMatthew G. Knepley   PetscInt s;
5499e5e52638SMatthew G. Knepley 
5500e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5501e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5502e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
550363a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5504e5e52638SMatthew G. Knepley   *prob = NULL;
5505e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5506e5e52638SMatthew G. Knepley     PetscInt val;
5507e5e52638SMatthew G. Knepley 
55089371c9d4SSatish Balay     if (!dm->probs[s].label) {
55099371c9d4SSatish Balay       probDef = dm->probs[s].ds;
55109371c9d4SSatish Balay     } else {
55119566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
55129371c9d4SSatish Balay       if (val >= 0) {
55139371c9d4SSatish Balay         *prob = dm->probs[s].ds;
55149371c9d4SSatish Balay         break;
55159371c9d4SSatish Balay       }
5516e5e52638SMatthew G. Knepley     }
5517e5e52638SMatthew G. Knepley   }
5518e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5519e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5520e5e52638SMatthew G. Knepley }
5521e5e52638SMatthew G. Knepley 
5522e5e52638SMatthew G. Knepley /*@
5523e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5524e5e52638SMatthew G. Knepley 
5525e5e52638SMatthew G. Knepley   Not collective
5526e5e52638SMatthew G. Knepley 
5527e5e52638SMatthew G. Knepley   Input Parameters:
5528e5e52638SMatthew G. Knepley + dm    - The DM
5529e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5530e5e52638SMatthew G. Knepley 
5531b3cf3223SMatthew G. Knepley   Output Parameters:
5532b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5533b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5534e5e52638SMatthew G. Knepley 
5535154ca461SJed Brown   Note:
5536154ca461SJed Brown   If a non-NULL label is given, but there is no PetscDS on that specific label,
5537154ca461SJed Brown   the PetscDS for the full domain (if present) is returned. Returns with
5538154ca461SJed Brown   fields=NULL and prob=NULL if there is no PetscDS for the full domain.
5539e5e52638SMatthew G. Knepley 
5540e5e52638SMatthew G. Knepley   Level: advanced
5541e5e52638SMatthew G. Knepley 
5542db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5543e5e52638SMatthew G. Knepley @*/
5544d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5545d71ae5a4SJacob Faibussowitsch {
5546e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5547e5e52638SMatthew G. Knepley 
5548e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5549e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5550e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55519371c9d4SSatish Balay   if (fields) {
55529371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55539371c9d4SSatish Balay     *fields = NULL;
55549371c9d4SSatish Balay   }
55559371c9d4SSatish Balay   if (ds) {
55569371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55579371c9d4SSatish Balay     *ds = NULL;
55589371c9d4SSatish Balay   }
5559e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5560154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5561b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5562b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
5563154ca461SJed Brown       if (dm->probs[s].label) PetscFunctionReturn(0);
5564b3cf3223SMatthew G. Knepley     }
5565e5e52638SMatthew G. Knepley   }
55662df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5567e5e52638SMatthew G. Knepley }
5568e5e52638SMatthew G. Knepley 
5569e5e52638SMatthew G. Knepley /*@
5570bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5571083401c6SMatthew G. Knepley 
5572083401c6SMatthew G. Knepley   Collective on dm
5573083401c6SMatthew G. Knepley 
5574083401c6SMatthew G. Knepley   Input Parameters:
5575bb7acecfSBarry Smith + dm     - The `DM`
5576bb7acecfSBarry Smith . label  - The `DMLabel` defining the mesh region, or NULL for the entire mesh
5577bb7acecfSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or NULL for all fields
5578bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5579083401c6SMatthew G. Knepley 
5580bb7acecfSBarry Smith   Note:
5581bb7acecfSBarry 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,
5582083401c6SMatthew G. Knepley   the fields argument is ignored.
5583083401c6SMatthew G. Knepley 
5584083401c6SMatthew G. Knepley   Level: advanced
5585083401c6SMatthew G. Knepley 
5586db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5587083401c6SMatthew G. Knepley @*/
5588d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5589d71ae5a4SJacob Faibussowitsch {
5590083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5591083401c6SMatthew G. Knepley 
5592083401c6SMatthew G. Knepley   PetscFunctionBegin;
5593083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5594083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5595064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5596083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5597083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55989566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5599083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5600083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5601083401c6SMatthew G. Knepley     }
5602083401c6SMatthew G. Knepley   }
56039566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
56049566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
56069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5607083401c6SMatthew G. Knepley   if (!label) {
5608083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5609083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5610083401c6SMatthew G. Knepley     Nds = 0;
5611083401c6SMatthew G. Knepley   }
5612083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5613083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5614083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5615083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5616083401c6SMatthew G. Knepley }
5617083401c6SMatthew G. Knepley 
5618083401c6SMatthew G. Knepley /*@
5619e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5620e5e52638SMatthew G. Knepley 
5621e5e52638SMatthew G. Knepley   Not collective
5622e5e52638SMatthew G. Knepley 
5623e5e52638SMatthew G. Knepley   Input Parameters:
5624e5e52638SMatthew G. Knepley + dm  - The DM
5625e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5626e5e52638SMatthew G. Knepley 
5627e5e52638SMatthew G. Knepley   Output Parameters:
5628b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5629b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5630083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5631e5e52638SMatthew G. Knepley 
5632e5e52638SMatthew G. Knepley   Level: advanced
5633e5e52638SMatthew G. Knepley 
5634db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5635e5e52638SMatthew G. Knepley @*/
5636d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5637d71ae5a4SJacob Faibussowitsch {
5638e5e52638SMatthew G. Knepley   PetscInt Nds;
5639e5e52638SMatthew G. Knepley 
5640e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5641e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56429566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
564363a3b9bcSJacob 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);
5644e5e52638SMatthew G. Knepley   if (label) {
5645e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5646e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5647e5e52638SMatthew G. Knepley   }
5648b3cf3223SMatthew G. Knepley   if (fields) {
5649b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5650b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5651b3cf3223SMatthew G. Knepley   }
5652e5e52638SMatthew G. Knepley   if (ds) {
5653b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5654e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5655e5e52638SMatthew G. Knepley   }
5656e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5657e5e52638SMatthew G. Knepley }
5658e5e52638SMatthew G. Knepley 
5659e5e52638SMatthew G. Knepley /*@
5660083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5661e5e52638SMatthew G. Knepley 
5662083401c6SMatthew G. Knepley   Not collective
5663e5e52638SMatthew G. Knepley 
5664e5e52638SMatthew G. Knepley   Input Parameters:
5665e5e52638SMatthew G. Knepley + dm     - The DM
5666083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5667083401c6SMatthew G. Knepley . label  - The region label, or NULL
5668083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5669083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5670e5e52638SMatthew G. Knepley 
5671e5e52638SMatthew G. Knepley   Level: advanced
5672e5e52638SMatthew G. Knepley 
5673db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5674e5e52638SMatthew G. Knepley @*/
5675d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5676d71ae5a4SJacob Faibussowitsch {
5677083401c6SMatthew G. Knepley   PetscInt Nds;
5678e5e52638SMatthew G. Knepley 
5679e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5680e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5681ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56829566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
568363a3b9bcSJacob 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);
56849566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56859566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5686083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5687083401c6SMatthew G. Knepley   if (fields) {
5688083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56899566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56909566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5691083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5692e5e52638SMatthew G. Knepley   }
5693083401c6SMatthew G. Knepley   if (ds) {
5694083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56959566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56969566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5697083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5698083401c6SMatthew G. Knepley   }
5699e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5700e5e52638SMatthew G. Knepley }
5701e5e52638SMatthew G. Knepley 
5702e5e52638SMatthew G. Knepley /*@
57031d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
57041d3af9e0SMatthew G. Knepley 
57051d3af9e0SMatthew G. Knepley   Not collective
57061d3af9e0SMatthew G. Knepley 
57071d3af9e0SMatthew G. Knepley   Input Parameters:
57081d3af9e0SMatthew G. Knepley + dm  - The DM
57091d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
57101d3af9e0SMatthew G. Knepley 
57111d3af9e0SMatthew G. Knepley   Output Parameter:
57121d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
57131d3af9e0SMatthew G. Knepley 
57141d3af9e0SMatthew G. Knepley   Level: advanced
57151d3af9e0SMatthew G. Knepley 
5716db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
57171d3af9e0SMatthew G. Knepley @*/
5718d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5719d71ae5a4SJacob Faibussowitsch {
57201d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
57211d3af9e0SMatthew G. Knepley 
57221d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
57231d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57241d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5725dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
57269566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
57279371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
57289371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
57291d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
57301d3af9e0SMatthew G. Knepley   else *num = n;
57311d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
57321d3af9e0SMatthew G. Knepley }
57331d3af9e0SMatthew G. Knepley 
57342df84da0SMatthew G. Knepley /*@C
5735bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
57362df84da0SMatthew G. Knepley 
57372df84da0SMatthew G. Knepley   Not collective
57382df84da0SMatthew G. Knepley 
5739f1a722f8SMatthew G. Knepley   Input Parameters:
5740bb7acecfSBarry Smith + dm     - The `DM`
57412df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5742bb7acecfSBarry Smith . prefix - The options prefix for the output `PetscFE`, or NULL
5743bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
57442df84da0SMatthew G. Knepley 
57452df84da0SMatthew G. Knepley   Output Parameter:
5746bb7acecfSBarry Smith . fem - The `PetscFE`
57472df84da0SMatthew G. Knepley 
5748bb7acecfSBarry Smith   Note:
5749bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57502df84da0SMatthew G. Knepley 
57512df84da0SMatthew G. Knepley   Level: intermediate
57522df84da0SMatthew G. Knepley 
5753db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57542df84da0SMatthew G. Knepley @*/
5755d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5756d71ae5a4SJacob Faibussowitsch {
57572df84da0SMatthew G. Knepley   DMPolytopeType ct;
57582df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57592df84da0SMatthew G. Knepley 
57602df84da0SMatthew G. Knepley   PetscFunctionBegin;
57612df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57622df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57632df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57642df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57652df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57699566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57702df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
57712df84da0SMatthew G. Knepley }
57722df84da0SMatthew G. Knepley 
57731d3af9e0SMatthew G. Knepley /*@
5774bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5775e5e52638SMatthew G. Knepley 
5776d083f849SBarry Smith   Collective on dm
5777e5e52638SMatthew G. Knepley 
5778e5e52638SMatthew G. Knepley   Input Parameter:
5779bb7acecfSBarry Smith . dm - The `DM`
5780e5e52638SMatthew G. Knepley 
578145480ffeSMatthew G. Knepley   Options Database Keys:
5782bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
578345480ffeSMatthew G. Knepley 
5784bb7acecfSBarry Smith   Note:
5785bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5786e5e52638SMatthew G. Knepley 
5787e5e52638SMatthew G. Knepley   Level: intermediate
5788e5e52638SMatthew G. Knepley 
5789db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5790e5e52638SMatthew G. Knepley @*/
5791d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5792d71ae5a4SJacob Faibussowitsch {
5793e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5794083401c6SMatthew G. Knepley   PetscDS   dsDef;
5795083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5796f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5797f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5798e5e52638SMatthew G. Knepley 
5799e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5800e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5801e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
58029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
58039566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5804083401c6SMatthew G. Knepley   /* Determine how many regions we have */
58059566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5806083401c6SMatthew G. Knepley   Nl   = 0;
5807083401c6SMatthew G. Knepley   Ndef = 0;
5808083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5809083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5810083401c6SMatthew G. Knepley     PetscInt l;
5811083401c6SMatthew G. Knepley 
5812f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5813f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5814f918ec44SMatthew G. Knepley     {
5815f918ec44SMatthew G. Knepley       PetscClassId id;
5816f918ec44SMatthew G. Knepley 
58179566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5818f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5819f918ec44SMatthew G. Knepley         Ceed ceed;
5820f918ec44SMatthew G. Knepley 
58219566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
58229566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5823f918ec44SMatthew G. Knepley       }
5824f918ec44SMatthew G. Knepley     }
5825f918ec44SMatthew G. Knepley #endif
58269371c9d4SSatish Balay     if (!label) {
58279371c9d4SSatish Balay       ++Ndef;
58289371c9d4SSatish Balay       continue;
58299371c9d4SSatish Balay     }
58309371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
58319371c9d4SSatish Balay       if (label == labelSet[l]) break;
5832083401c6SMatthew G. Knepley     if (l < Nl) continue;
5833083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5834083401c6SMatthew G. Knepley   }
5835083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
58369566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5837083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5838b3cf3223SMatthew G. Knepley     IS        fields;
5839b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5840b3cf3223SMatthew G. Knepley 
58419371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58429371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
58437a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
58449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58459371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58469371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
58479566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58499566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58509566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
585188f0c812SMatthew G. Knepley 
58529566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58539566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
58549566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58559566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58562df9ee95SMatthew G. Knepley   }
58579566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
58589566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5859083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5860083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58610122748bSMatthew G. Knepley     DM              plex;
5862083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5863083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5864083401c6SMatthew G. Knepley     PetscInt       *fields;
5865083401c6SMatthew G. Knepley     const PetscInt *cells;
5866083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58670122748bSMatthew G. Knepley 
58689566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58709566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58719566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58725fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5873083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5874083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5875083401c6SMatthew G. Knepley       IS      pointIS;
5876083401c6SMatthew G. Knepley 
58779566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58789566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58799566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58809566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5881083401c6SMatthew G. Knepley     }
58829566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5883083401c6SMatthew G. Knepley 
58849566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58859566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58869566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58879566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58889566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58899566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58909566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5891083401c6SMatthew G. Knepley 
58929566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58939371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58949371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58959566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58979566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58989566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5899083401c6SMatthew G. Knepley 
59009566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
59019566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
59029566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
59039566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
59049566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
59059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
59069566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5907083401c6SMatthew G. Knepley   }
5908083401c6SMatthew G. Knepley   /* Create label DSes
5909083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5910083401c6SMatthew G. Knepley   */
5911083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5912083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5913083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5914083401c6SMatthew G. Knepley     PetscDS   ds;
5915083401c6SMatthew G. Knepley     IS        fields;
5916083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5917083401c6SMatthew G. Knepley 
59189566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
59199371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59209371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
59219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
59229371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
59239371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
59249566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
59259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
59269566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
59279566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
59289566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
59299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
59309566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5931083401c6SMatthew G. Knepley     {
5932083401c6SMatthew G. Knepley       DMPolytopeType ct;
5933083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
59345fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
59350122748bSMatthew G. Knepley 
59369566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5937665f567fSMatthew G. Knepley       if (lStart >= 0) {
59389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5939412e9a14SMatthew G. Knepley         switch (ct) {
5940412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5941412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5942412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5943d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5944d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5945d71ae5a4SJacob Faibussowitsch           break;
5946d71ae5a4SJacob Faibussowitsch         default:
5947d71ae5a4SJacob Faibussowitsch           break;
5948412e9a14SMatthew G. Knepley         }
5949665f567fSMatthew G. Knepley       }
59509566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
59515fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59525fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59535fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59549566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59559566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
59565fedec97SMatthew G. Knepley           }
59575fedec97SMatthew G. Knepley           ++nf;
59585fedec97SMatthew G. Knepley         }
59595fedec97SMatthew G. Knepley       }
5960e5e52638SMatthew G. Knepley     }
59619566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5962e5e52638SMatthew G. Knepley   }
59639566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5964e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5965083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5966083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5967083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5968083401c6SMatthew G. Knepley     const PetscInt *fld;
59695fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59705fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5971e5e52638SMatthew G. Knepley 
59729566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59739566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59749566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59759566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5976083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5977083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59785fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5979e5e52638SMatthew G. Knepley       PetscClassId id;
5980e5e52638SMatthew G. Knepley 
59815fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59829566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59835fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59849566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
59859566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5986083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59879566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5988e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5989e5e52638SMatthew G. Knepley     }
59909566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5991e5e52638SMatthew G. Knepley   }
5992f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5994f9244615SMatthew G. Knepley   if (flg) {
59953b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59963b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59973b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59983b4aee56SMatthew G. Knepley 
59999566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
60009566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
60013b4aee56SMatthew G. Knepley     }
6002f9244615SMatthew G. Knepley   }
6003e5e52638SMatthew G. Knepley   /* Setup DSes */
6004e5e52638SMatthew G. Knepley   if (doSetup) {
60059566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
6006e5e52638SMatthew G. Knepley   }
6007e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6008e5e52638SMatthew G. Knepley }
6009e5e52638SMatthew G. Knepley 
6010e5e52638SMatthew G. Knepley /*@
6011bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
60127f96f943SMatthew G. Knepley 
6013bb7acecfSBarry Smith   Collective on `DM`
6014f2cacb80SMatthew G. Knepley 
60157f96f943SMatthew G. Knepley   Input Parameters:
6016bb7acecfSBarry Smith + dm   - The `DM`
60177f96f943SMatthew G. Knepley - time - The time
60187f96f943SMatthew G. Knepley 
60197f96f943SMatthew G. Knepley   Output Parameters:
6020f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
6021f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
60227f96f943SMatthew G. Knepley 
6023bb7acecfSBarry Smith   Note:
6024bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
60257f96f943SMatthew G. Knepley 
60267f96f943SMatthew G. Knepley   Level: developer
60277f96f943SMatthew G. Knepley 
6028db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
60297f96f943SMatthew G. Knepley @*/
6030d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
6031d71ae5a4SJacob Faibussowitsch {
60327f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
60337f96f943SMatthew G. Knepley   void   **ectxs;
6034f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
60357f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
60367f96f943SMatthew G. Knepley 
60377f96f943SMatthew G. Knepley   PetscFunctionBegin;
6038f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6039f60fa741SMatthew G. Knepley   if (u) {
6040f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
6041f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
6042f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
6043f60fa741SMatthew G. Knepley   }
6044f60fa741SMatthew G. Knepley   if (u_t) {
6045f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
6046f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
6047f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
6048f60fa741SMatthew G. Knepley   }
60499566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60509566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60519566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60527f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60537f96f943SMatthew G. Knepley     PetscDS         ds;
60547f96f943SMatthew G. Knepley     DMLabel         label;
60557f96f943SMatthew G. Knepley     IS              fieldIS;
60567f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60577f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60587f96f943SMatthew G. Knepley 
60599566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
60609566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60619566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60629566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60639566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6064f2cacb80SMatthew G. Knepley     if (u) {
6065f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6066f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6067f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60687f96f943SMatthew G. Knepley     }
6069f2cacb80SMatthew G. Knepley     if (u_t) {
60709566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60719566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6072f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6073f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6074f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6075f2cacb80SMatthew G. Knepley     }
60769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6077f2cacb80SMatthew G. Knepley   }
6078f2cacb80SMatthew G. Knepley   if (u) {
60799566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6081f2cacb80SMatthew G. Knepley   }
6082f2cacb80SMatthew G. Knepley   if (u_t) {
60839566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6085f2cacb80SMatthew G. Knepley   }
60869566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6087f60fa741SMatthew G. Knepley   if (u) {
6088f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6089f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6090f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6091f60fa741SMatthew G. Knepley   }
6092f60fa741SMatthew G. Knepley   if (u_t) {
6093f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6094f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6095f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6096f60fa741SMatthew G. Knepley   }
60977f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
60987f96f943SMatthew G. Knepley }
60997f96f943SMatthew G. Knepley 
6100d71ae5a4SJacob Faibussowitsch PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
6101d71ae5a4SJacob Faibussowitsch {
610245480ffeSMatthew G. Knepley   PetscDS    dsNew;
610345480ffeSMatthew G. Knepley   DSBoundary b;
61046a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
61055fedec97SMatthew G. Knepley   PetscBool  isCohesive;
610645480ffeSMatthew G. Knepley   void      *ctx;
610745480ffeSMatthew G. Knepley 
610845480ffeSMatthew G. Knepley   PetscFunctionBegin;
61099566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
61109566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
61119566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
61129566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
61139566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
61149566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
611545480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
61169566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
61179566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
61189566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
61199566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
61206a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
61216a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
612245480ffeSMatthew G. Knepley   }
612345480ffeSMatthew G. Knepley   if (Nf) {
61249566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
61259566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
612645480ffeSMatthew G. Knepley   }
61279566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
612845480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
61299566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
613045480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
61317a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
613245480ffeSMatthew G. Knepley   }
613345480ffeSMatthew G. Knepley 
61349566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
61359566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
613645480ffeSMatthew G. Knepley   PetscFunctionReturn(0);
613745480ffeSMatthew G. Knepley }
613845480ffeSMatthew G. Knepley 
61397f96f943SMatthew G. Knepley /*@
6140bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6141e5e52638SMatthew G. Knepley 
6142d083f849SBarry Smith   Collective on dm
6143e5e52638SMatthew G. Knepley 
6144e5e52638SMatthew G. Knepley   Input Parameter:
6145bb7acecfSBarry Smith . dm - The `DM`
6146e5e52638SMatthew G. Knepley 
6147e5e52638SMatthew G. Knepley   Output Parameter:
6148bb7acecfSBarry Smith . newdm - The `DM`
6149e5e52638SMatthew G. Knepley 
6150e5e52638SMatthew G. Knepley   Level: advanced
6151e5e52638SMatthew G. Knepley 
6152db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6153e5e52638SMatthew G. Knepley @*/
6154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6155d71ae5a4SJacob Faibussowitsch {
6156e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6157e5e52638SMatthew G. Knepley 
6158e5e52638SMatthew G. Knepley   PetscFunctionBegin;
6159e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
61609566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61619566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6162e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6163e5e52638SMatthew G. Knepley     DMLabel  label;
6164b3cf3223SMatthew G. Knepley     IS       fields;
616545480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6166783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6167e5e52638SMatthew G. Knepley 
61689566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6169b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
61709566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
6171d5b43468SJose E. Roman     /* Complete new labels in the new DS */
61729566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61739566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6174783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6175b8025e53SMatthew G. Knepley       PetscWeakForm wf;
617645480ffeSMatthew G. Knepley       DMLabel       label;
6177783e2ec8SMatthew G. Knepley       PetscInt      field;
6178783e2ec8SMatthew G. Knepley 
61799566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61809566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6181783e2ec8SMatthew G. Knepley     }
6182e5e52638SMatthew G. Knepley   }
6183799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
6184e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6185e5e52638SMatthew G. Knepley }
6186e5e52638SMatthew G. Knepley 
6187e5e52638SMatthew G. Knepley /*@
6188bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6189e5e52638SMatthew G. Knepley 
6190d083f849SBarry Smith   Collective on dm
6191e5e52638SMatthew G. Knepley 
6192e5e52638SMatthew G. Knepley   Input Parameter:
6193bb7acecfSBarry Smith . dm - The `DM`
6194e5e52638SMatthew G. Knepley 
6195e5e52638SMatthew G. Knepley   Output Parameter:
6196bb7acecfSBarry Smith . newdm - The `DM`
6197e5e52638SMatthew G. Knepley 
6198e5e52638SMatthew G. Knepley   Level: advanced
6199e5e52638SMatthew G. Knepley 
6200bb7acecfSBarry Smith   Developer Note:
6201bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6202bb7acecfSBarry Smith 
6203db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6204e5e52638SMatthew G. Knepley @*/
6205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6206d71ae5a4SJacob Faibussowitsch {
6207e5e52638SMatthew G. Knepley   PetscFunctionBegin;
62089566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
62099566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
6210e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
6211e5e52638SMatthew G. Knepley }
6212e5e52638SMatthew G. Knepley 
6213c73cfb54SMatthew G. Knepley /*@
6214bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6215c73cfb54SMatthew G. Knepley 
6216c73cfb54SMatthew G. Knepley   Not collective
6217c73cfb54SMatthew G. Knepley 
6218c73cfb54SMatthew G. Knepley   Input Parameter:
6219bb7acecfSBarry Smith . dm - The `DM`
6220c73cfb54SMatthew G. Knepley 
6221c73cfb54SMatthew G. Knepley   Output Parameter:
6222c73cfb54SMatthew G. Knepley . dim - The topological dimension
6223c73cfb54SMatthew G. Knepley 
6224c73cfb54SMatthew G. Knepley   Level: beginner
6225c73cfb54SMatthew G. Knepley 
6226db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6227c73cfb54SMatthew G. Knepley @*/
6228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6229d71ae5a4SJacob Faibussowitsch {
6230c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6231c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6232534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6233c73cfb54SMatthew G. Knepley   *dim = dm->dim;
6234c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6235c73cfb54SMatthew G. Knepley }
6236c73cfb54SMatthew G. Knepley 
6237c73cfb54SMatthew G. Knepley /*@
6238bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6239c73cfb54SMatthew G. Knepley 
6240c73cfb54SMatthew G. Knepley   Collective on dm
6241c73cfb54SMatthew G. Knepley 
6242c73cfb54SMatthew G. Knepley   Input Parameters:
6243bb7acecfSBarry Smith + dm - The `DM`
6244c73cfb54SMatthew G. Knepley - dim - The topological dimension
6245c73cfb54SMatthew G. Knepley 
6246c73cfb54SMatthew G. Knepley   Level: beginner
6247c73cfb54SMatthew G. Knepley 
6248db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6249c73cfb54SMatthew G. Knepley @*/
6250d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6251d71ae5a4SJacob Faibussowitsch {
6252e5e52638SMatthew G. Knepley   PetscDS  ds;
625345480ffeSMatthew G. Knepley   PetscInt Nds, n;
6254f17e8794SMatthew G. Knepley 
6255c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6256c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6257c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6258c73cfb54SMatthew G. Knepley   dm->dim = dim;
6259d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62609566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
626145480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
62629566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
62639566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
626445480ffeSMatthew G. Knepley     }
6265d17bd122SMatthew G. Knepley   }
6266c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
6267c73cfb54SMatthew G. Knepley }
6268c73cfb54SMatthew G. Knepley 
6269793f3fe5SMatthew G. Knepley /*@
6270793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6271793f3fe5SMatthew G. Knepley 
6272d083f849SBarry Smith   Collective on dm
6273793f3fe5SMatthew G. Knepley 
6274793f3fe5SMatthew G. Knepley   Input Parameters:
6275bb7acecfSBarry Smith + dm - the `DM`
6276793f3fe5SMatthew G. Knepley - dim - the dimension
6277793f3fe5SMatthew G. Knepley 
6278793f3fe5SMatthew G. Knepley   Output Parameters:
6279793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6280aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6281793f3fe5SMatthew G. Knepley 
6282793f3fe5SMatthew G. Knepley   Note:
6283793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6284a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6285793f3fe5SMatthew G. Knepley   then the interval is empty.
6286793f3fe5SMatthew G. Knepley 
6287793f3fe5SMatthew G. Knepley   Level: intermediate
6288793f3fe5SMatthew G. Knepley 
6289db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6290793f3fe5SMatthew G. Knepley @*/
6291d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6292d71ae5a4SJacob Faibussowitsch {
6293793f3fe5SMatthew G. Knepley   PetscInt d;
6294793f3fe5SMatthew G. Knepley 
6295793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6296793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62979566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62987a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6299dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
6300793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
6301793f3fe5SMatthew G. Knepley }
6302793f3fe5SMatthew G. Knepley 
63036636e97aSMatthew G Knepley /*@
6304bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6305f4d763aaSMatthew G. Knepley 
63068f700142SStefano Zampini   Collective on dm
63078f700142SStefano Zampini 
6308f4d763aaSMatthew G. Knepley   Input Parameter:
6309bb7acecfSBarry Smith . dm - The original `DM`
6310f4d763aaSMatthew G. Knepley 
6311f4d763aaSMatthew G. Knepley   Output Parameter:
6312bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6313f4d763aaSMatthew G. Knepley 
6314f4d763aaSMatthew G. Knepley   Level: intermediate
6315f4d763aaSMatthew G. Knepley 
6316bb7acecfSBarry Smith   Note:
6317bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6318bb7acecfSBarry 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
6319bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6320bb7acecfSBarry Smith 
6321bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6322f4d763aaSMatthew G. Knepley @*/
6323d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6324d71ae5a4SJacob Faibussowitsch {
6325c26acbdeSMatthew G. Knepley   PetscSection section;
63262d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
632714f150ffSMatthew G. Knepley 
632814f150ffSMatthew G. Knepley   PetscFunctionBegin;
632914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
633014f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
63319566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
63329566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
63339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
63342d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6335c26acbdeSMatthew G. Knepley     *odm = dm;
6336c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6337c26acbdeSMatthew G. Knepley   }
633814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6339c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
634014f150ffSMatthew G. Knepley     PetscSF      sf;
634114f150ffSMatthew G. Knepley 
63429566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
63439566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
63449566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
63459566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
63469566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
63479566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
63489566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63499566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63509566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
635114f150ffSMatthew G. Knepley   }
635214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
635314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
635414f150ffSMatthew G. Knepley }
6355f4d763aaSMatthew G. Knepley 
6356f4d763aaSMatthew G. Knepley /*@
6357cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6358f4d763aaSMatthew G. Knepley 
6359f4d763aaSMatthew G. Knepley   Input Parameter:
6360bb7acecfSBarry Smith . dm - The original `DM`
6361f4d763aaSMatthew G. Knepley 
6362cdb7a50dSMatthew G. Knepley   Output Parameters:
6363cdb7a50dSMatthew G. Knepley + num - The output sequence number
6364cdb7a50dSMatthew G. Knepley - val - The output sequence value
6365f4d763aaSMatthew G. Knepley 
6366f4d763aaSMatthew G. Knepley   Level: intermediate
6367f4d763aaSMatthew G. Knepley 
6368bb7acecfSBarry Smith   Note:
6369bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6370bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6371bb7acecfSBarry Smith 
6372bb7acecfSBarry Smith   Developer Note:
6373bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6374bb7acecfSBarry Smith   not directly related to the `DM`.
6375f4d763aaSMatthew G. Knepley 
6376db781477SPatrick Sanan .seealso: `VecView()`
6377f4d763aaSMatthew G. Knepley @*/
6378d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6379d71ae5a4SJacob Faibussowitsch {
6380f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6381f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63829371c9d4SSatish Balay   if (num) {
63839371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63849371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63859371c9d4SSatish Balay   }
63869371c9d4SSatish Balay   if (val) {
63879371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63889371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63899371c9d4SSatish Balay   }
6390f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6391f4d763aaSMatthew G. Knepley }
6392f4d763aaSMatthew G. Knepley 
6393f4d763aaSMatthew G. Knepley /*@
6394cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6395f4d763aaSMatthew G. Knepley 
6396f4d763aaSMatthew G. Knepley   Input Parameters:
6397bb7acecfSBarry Smith + dm - The original `DM`
6398cdb7a50dSMatthew G. Knepley . num - The output sequence number
6399cdb7a50dSMatthew G. Knepley - val - The output sequence value
6400f4d763aaSMatthew G. Knepley 
6401f4d763aaSMatthew G. Knepley   Level: intermediate
6402f4d763aaSMatthew G. Knepley 
6403bb7acecfSBarry Smith   Note:
6404bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6405bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6406f4d763aaSMatthew G. Knepley 
6407db781477SPatrick Sanan .seealso: `VecView()`
6408f4d763aaSMatthew G. Knepley @*/
6409d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6410d71ae5a4SJacob Faibussowitsch {
6411f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6412f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6414cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6415cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6416cdb7a50dSMatthew G. Knepley }
6417cdb7a50dSMatthew G. Knepley 
6418cdb7a50dSMatthew G. Knepley /*@C
6419bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6420cdb7a50dSMatthew G. Knepley 
6421cdb7a50dSMatthew G. Knepley   Input Parameters:
6422bb7acecfSBarry Smith + dm   - The original `DM`
6423cdb7a50dSMatthew G. Knepley . name - The sequence name
6424cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6425cdb7a50dSMatthew G. Knepley 
6426cdb7a50dSMatthew G. Knepley   Output Parameter:
6427cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6428cdb7a50dSMatthew G. Knepley 
6429cdb7a50dSMatthew G. Knepley   Level: intermediate
6430cdb7a50dSMatthew G. Knepley 
6431bb7acecfSBarry Smith   Note:
6432bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6433bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6434bb7acecfSBarry Smith 
6435bb7acecfSBarry Smith   Developer Note:
6436bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6437cdb7a50dSMatthew G. Knepley 
6438db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6439cdb7a50dSMatthew G. Knepley @*/
6440d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6441d71ae5a4SJacob Faibussowitsch {
6442cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6443cdb7a50dSMatthew G. Knepley 
6444cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6445cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6446cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6447064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
64489566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6449cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6450cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6451cdb7a50dSMatthew G. Knepley     PetscScalar value;
6452cdb7a50dSMatthew G. Knepley 
64539566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64544aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6455cdb7a50dSMatthew G. Knepley #endif
6456cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6457f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6458f4d763aaSMatthew G. Knepley }
64598e4ac7eaSMatthew G. Knepley 
64608e4ac7eaSMatthew G. Knepley /*@
6461bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64628e4ac7eaSMatthew G. Knepley 
64638e4ac7eaSMatthew G. Knepley   Not collective
64648e4ac7eaSMatthew G. Knepley 
64658e4ac7eaSMatthew G. Knepley   Input Parameter:
6466bb7acecfSBarry Smith . dm - The `DM`
64678e4ac7eaSMatthew G. Knepley 
64688e4ac7eaSMatthew G. Knepley   Output Parameter:
6469bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64708e4ac7eaSMatthew G. Knepley 
64718e4ac7eaSMatthew G. Knepley   Level: beginner
64728e4ac7eaSMatthew G. Knepley 
6473db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
64748e4ac7eaSMatthew G. Knepley @*/
6475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6476d71ae5a4SJacob Faibussowitsch {
64778e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64788e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6479534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64808e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64818e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
64828e4ac7eaSMatthew G. Knepley }
64838e4ac7eaSMatthew G. Knepley 
64848e4ac7eaSMatthew G. Knepley /*@
6485bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64868e4ac7eaSMatthew G. Knepley 
64878e4ac7eaSMatthew G. Knepley   Collective on dm
64888e4ac7eaSMatthew G. Knepley 
64898e4ac7eaSMatthew G. Knepley   Input Parameters:
6490bb7acecfSBarry Smith  + dm - The `DM`
6491bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64928e4ac7eaSMatthew G. Knepley 
6493bb7acecfSBarry Smith   Note:
6494bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64955d3b26e6SMatthew G. Knepley 
64968e4ac7eaSMatthew G. Knepley   Level: beginner
64978e4ac7eaSMatthew G. Knepley 
6498db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64998e4ac7eaSMatthew G. Knepley @*/
6500d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6501d71ae5a4SJacob Faibussowitsch {
65028e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65038e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65048833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
65058e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
65068e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
65078e4ac7eaSMatthew G. Knepley }
6508c58f1c22SToby Isaac 
6509c58f1c22SToby Isaac /*@C
6510bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6511c58f1c22SToby Isaac 
6512c58f1c22SToby Isaac   Not Collective
6513c58f1c22SToby Isaac 
6514c58f1c22SToby Isaac   Input Parameters:
6515bb7acecfSBarry Smith + dm   - The `DM` object
6516c58f1c22SToby Isaac - name - The label name
6517c58f1c22SToby Isaac 
6518c58f1c22SToby Isaac   Level: intermediate
6519c58f1c22SToby Isaac 
6520db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6521c58f1c22SToby Isaac @*/
6522d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6523d71ae5a4SJacob Faibussowitsch {
65245d80c0bfSVaclav Hapla   PetscBool flg;
65255d80c0bfSVaclav Hapla   DMLabel   label;
6526c58f1c22SToby Isaac 
6527c58f1c22SToby Isaac   PetscFunctionBegin;
6528c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6529c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65309566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6531c58f1c22SToby Isaac   if (!flg) {
65329566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65339566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65349566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6535c58f1c22SToby Isaac   }
6536c58f1c22SToby Isaac   PetscFunctionReturn(0);
6537c58f1c22SToby Isaac }
6538c58f1c22SToby Isaac 
6539c58f1c22SToby Isaac /*@C
6540bb7acecfSBarry 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.
65410fdc7489SMatthew Knepley 
65420fdc7489SMatthew Knepley   Not Collective
65430fdc7489SMatthew Knepley 
65440fdc7489SMatthew Knepley   Input Parameters:
6545bb7acecfSBarry Smith + dm   - The `DM` object
65460fdc7489SMatthew Knepley . l    - The index for the label
65470fdc7489SMatthew Knepley - name - The label name
65480fdc7489SMatthew Knepley 
65490fdc7489SMatthew Knepley   Level: intermediate
65500fdc7489SMatthew Knepley 
6551db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65520fdc7489SMatthew Knepley @*/
6553d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6554d71ae5a4SJacob Faibussowitsch {
65550fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65560fdc7489SMatthew Knepley   DMLabel     label;
65570fdc7489SMatthew Knepley   PetscInt    Nl, m;
65580fdc7489SMatthew Knepley   PetscBool   flg, match;
65590fdc7489SMatthew Knepley   const char *lname;
65600fdc7489SMatthew Knepley 
65610fdc7489SMatthew Knepley   PetscFunctionBegin;
65620fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6563064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65649566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65650fdc7489SMatthew Knepley   if (!flg) {
65669566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65679566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65689566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65690fdc7489SMatthew Knepley   }
65709566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
657163a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65720fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65739566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65749566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65750fdc7489SMatthew Knepley     if (match) break;
65760fdc7489SMatthew Knepley   }
65770fdc7489SMatthew Knepley   if (m == l) PetscFunctionReturn(0);
65780fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65790fdc7489SMatthew Knepley   else prev->next = orig->next;
65800fdc7489SMatthew Knepley   if (!l) {
65810fdc7489SMatthew Knepley     orig->next = dm->labels;
65820fdc7489SMatthew Knepley     dm->labels = orig;
65830fdc7489SMatthew Knepley   } else {
65849371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65859371c9d4SSatish Balay       ;
65860fdc7489SMatthew Knepley     orig->next = prev->next;
65870fdc7489SMatthew Knepley     prev->next = orig;
65880fdc7489SMatthew Knepley   }
65890fdc7489SMatthew Knepley   PetscFunctionReturn(0);
65900fdc7489SMatthew Knepley }
65910fdc7489SMatthew Knepley 
65920fdc7489SMatthew Knepley /*@C
6593bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6594c58f1c22SToby Isaac 
6595c58f1c22SToby Isaac   Not Collective
6596c58f1c22SToby Isaac 
6597c58f1c22SToby Isaac   Input Parameters:
6598bb7acecfSBarry Smith + dm   - The `DM` object
6599c58f1c22SToby Isaac . name - The label name
6600c58f1c22SToby Isaac - point - The mesh point
6601c58f1c22SToby Isaac 
6602c58f1c22SToby Isaac   Output Parameter:
6603c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6604c58f1c22SToby Isaac 
6605c58f1c22SToby Isaac   Level: beginner
6606c58f1c22SToby Isaac 
6607db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6608c58f1c22SToby Isaac @*/
6609d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6610d71ae5a4SJacob Faibussowitsch {
6611c58f1c22SToby Isaac   DMLabel label;
6612c58f1c22SToby Isaac 
6613c58f1c22SToby Isaac   PetscFunctionBegin;
6614c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6615c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66169566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66177a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
66189566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
6619c58f1c22SToby Isaac   PetscFunctionReturn(0);
6620c58f1c22SToby Isaac }
6621c58f1c22SToby Isaac 
6622c58f1c22SToby Isaac /*@C
6623bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6624c58f1c22SToby Isaac 
6625c58f1c22SToby Isaac   Not Collective
6626c58f1c22SToby Isaac 
6627c58f1c22SToby Isaac   Input Parameters:
6628bb7acecfSBarry Smith + dm   - The `DM` object
6629c58f1c22SToby Isaac . name - The label name
6630c58f1c22SToby Isaac . point - The mesh point
6631c58f1c22SToby Isaac - value - The label value for this point
6632c58f1c22SToby Isaac 
6633c58f1c22SToby Isaac   Output Parameter:
6634c58f1c22SToby Isaac 
6635c58f1c22SToby Isaac   Level: beginner
6636c58f1c22SToby Isaac 
6637db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6638c58f1c22SToby Isaac @*/
6639d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6640d71ae5a4SJacob Faibussowitsch {
6641c58f1c22SToby Isaac   DMLabel label;
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   PetscFunctionBegin;
6644c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6645c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66469566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6647c58f1c22SToby Isaac   if (!label) {
66489566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66499566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6650c58f1c22SToby Isaac   }
66519566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
6652c58f1c22SToby Isaac   PetscFunctionReturn(0);
6653c58f1c22SToby Isaac }
6654c58f1c22SToby Isaac 
6655c58f1c22SToby Isaac /*@C
6656bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6657c58f1c22SToby Isaac 
6658c58f1c22SToby Isaac   Not Collective
6659c58f1c22SToby Isaac 
6660c58f1c22SToby Isaac   Input Parameters:
6661bb7acecfSBarry Smith + dm   - The `DM` object
6662c58f1c22SToby Isaac . name - The label name
6663c58f1c22SToby Isaac . point - The mesh point
6664c58f1c22SToby Isaac - value - The label value for this point
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac   Level: beginner
6667c58f1c22SToby Isaac 
6668db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6669c58f1c22SToby Isaac @*/
6670d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6671d71ae5a4SJacob Faibussowitsch {
6672c58f1c22SToby Isaac   DMLabel label;
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac   PetscFunctionBegin;
6675c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6676c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66779566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6678c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
66799566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
6680c58f1c22SToby Isaac   PetscFunctionReturn(0);
6681c58f1c22SToby Isaac }
6682c58f1c22SToby Isaac 
6683c58f1c22SToby Isaac /*@C
6684bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6685c58f1c22SToby Isaac 
6686c58f1c22SToby Isaac   Not Collective
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   Input Parameters:
6689bb7acecfSBarry Smith + dm   - The `DM` object
6690c58f1c22SToby Isaac - name - The label name
6691c58f1c22SToby Isaac 
6692c58f1c22SToby Isaac   Output Parameter:
6693c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6694c58f1c22SToby Isaac 
6695c58f1c22SToby Isaac   Level: beginner
6696c58f1c22SToby Isaac 
6697bb7acecfSBarry Smith   Developer Note:
6698bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6699bb7acecfSBarry Smith 
6700bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6701c58f1c22SToby Isaac @*/
6702d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6703d71ae5a4SJacob Faibussowitsch {
6704c58f1c22SToby Isaac   DMLabel label;
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   PetscFunctionBegin;
6707c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6708c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6709534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
67109566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6711c58f1c22SToby Isaac   *size = 0;
6712c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67139566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
6714c58f1c22SToby Isaac   PetscFunctionReturn(0);
6715c58f1c22SToby Isaac }
6716c58f1c22SToby Isaac 
6717c58f1c22SToby Isaac /*@C
6718bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6719c58f1c22SToby Isaac 
6720c58f1c22SToby Isaac   Not Collective
6721c58f1c22SToby Isaac 
6722c58f1c22SToby Isaac   Input Parameters:
6723bb7acecfSBarry Smith + mesh - The `DM` object
6724c58f1c22SToby Isaac - name - The label name
6725c58f1c22SToby Isaac 
6726c58f1c22SToby Isaac   Output Parameter:
6727c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6728c58f1c22SToby Isaac 
6729c58f1c22SToby Isaac   Level: beginner
6730c58f1c22SToby Isaac 
6731db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6732c58f1c22SToby Isaac @*/
6733d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6734d71ae5a4SJacob Faibussowitsch {
6735c58f1c22SToby Isaac   DMLabel label;
6736c58f1c22SToby Isaac 
6737c58f1c22SToby Isaac   PetscFunctionBegin;
6738c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6739c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6740c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
67419566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6742c58f1c22SToby Isaac   *ids = NULL;
6743dab2e251SBlaise Bourdin   if (label) {
67449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6745dab2e251SBlaise Bourdin   } else {
6746dab2e251SBlaise Bourdin     /* returning an empty IS */
67479566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6748dab2e251SBlaise Bourdin   }
6749c58f1c22SToby Isaac   PetscFunctionReturn(0);
6750c58f1c22SToby Isaac }
6751c58f1c22SToby Isaac 
6752c58f1c22SToby Isaac /*@C
6753c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6754c58f1c22SToby Isaac 
6755c58f1c22SToby Isaac   Not Collective
6756c58f1c22SToby Isaac 
6757c58f1c22SToby Isaac   Input Parameters:
6758bb7acecfSBarry Smith + dm - The `DM` object
6759c58f1c22SToby Isaac . name - The label name
6760c58f1c22SToby Isaac - value - The stratum value
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac   Output Parameter:
6763bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Level: beginner
6766c58f1c22SToby Isaac 
6767db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6768c58f1c22SToby Isaac @*/
6769d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6770d71ae5a4SJacob Faibussowitsch {
6771c58f1c22SToby Isaac   DMLabel label;
6772c58f1c22SToby Isaac 
6773c58f1c22SToby Isaac   PetscFunctionBegin;
6774c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6775c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6776534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67779566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6778c58f1c22SToby Isaac   *size = 0;
6779c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
67809566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
6781c58f1c22SToby Isaac   PetscFunctionReturn(0);
6782c58f1c22SToby Isaac }
6783c58f1c22SToby Isaac 
6784c58f1c22SToby Isaac /*@C
6785c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6786c58f1c22SToby Isaac 
6787c58f1c22SToby Isaac   Not Collective
6788c58f1c22SToby Isaac 
6789c58f1c22SToby Isaac   Input Parameters:
6790bb7acecfSBarry Smith + dm - The `DM` object
6791c58f1c22SToby Isaac . name - The label name
6792c58f1c22SToby Isaac - value - The stratum value
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac   Output Parameter:
6795c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Level: beginner
6798c58f1c22SToby Isaac 
6799db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6800c58f1c22SToby Isaac @*/
6801d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6802d71ae5a4SJacob Faibussowitsch {
6803c58f1c22SToby Isaac   DMLabel label;
6804c58f1c22SToby Isaac 
6805c58f1c22SToby Isaac   PetscFunctionBegin;
6806c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6807c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6808c58f1c22SToby Isaac   PetscValidPointer(points, 4);
68099566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6810c58f1c22SToby Isaac   *points = NULL;
6811c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
68129566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
6813c58f1c22SToby Isaac   PetscFunctionReturn(0);
6814c58f1c22SToby Isaac }
6815c58f1c22SToby Isaac 
68164de306b1SToby Isaac /*@C
68179044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68184de306b1SToby Isaac 
68194de306b1SToby Isaac   Not Collective
68204de306b1SToby Isaac 
68214de306b1SToby Isaac   Input Parameters:
6822bb7acecfSBarry Smith + dm - The `DM` object
68234de306b1SToby Isaac . name - The label name
68244de306b1SToby Isaac . value - The stratum value
68254de306b1SToby Isaac - points - The stratum points
68264de306b1SToby Isaac 
68274de306b1SToby Isaac   Level: beginner
68284de306b1SToby Isaac 
6829bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
68304de306b1SToby Isaac @*/
6831d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6832d71ae5a4SJacob Faibussowitsch {
68334de306b1SToby Isaac   DMLabel label;
68344de306b1SToby Isaac 
68354de306b1SToby Isaac   PetscFunctionBegin;
68364de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68374de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68384de306b1SToby Isaac   PetscValidPointer(points, 4);
68399566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68404de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
68419566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
68424de306b1SToby Isaac   PetscFunctionReturn(0);
68434de306b1SToby Isaac }
68444de306b1SToby Isaac 
6845c58f1c22SToby Isaac /*@C
6846bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6847c58f1c22SToby Isaac 
6848c58f1c22SToby Isaac   Not Collective
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Input Parameters:
6851bb7acecfSBarry Smith + dm   - The `DM` object
6852c58f1c22SToby Isaac . name - The label name
6853c58f1c22SToby Isaac - value - The label value for this point
6854c58f1c22SToby Isaac 
6855c58f1c22SToby Isaac   Output Parameter:
6856c58f1c22SToby Isaac 
6857c58f1c22SToby Isaac   Level: beginner
6858c58f1c22SToby Isaac 
6859bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6860c58f1c22SToby Isaac @*/
6861d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6862d71ae5a4SJacob Faibussowitsch {
6863c58f1c22SToby Isaac   DMLabel label;
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   PetscFunctionBegin;
6866c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6867c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68689566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6869c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
68709566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
6871c58f1c22SToby Isaac   PetscFunctionReturn(0);
6872c58f1c22SToby Isaac }
6873c58f1c22SToby Isaac 
6874c58f1c22SToby Isaac /*@
6875bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6876c58f1c22SToby Isaac 
6877c58f1c22SToby Isaac   Not Collective
6878c58f1c22SToby Isaac 
6879c58f1c22SToby Isaac   Input Parameter:
6880bb7acecfSBarry Smith . dm   - The `DM` object
6881c58f1c22SToby Isaac 
6882c58f1c22SToby Isaac   Output Parameter:
6883c58f1c22SToby Isaac . numLabels - the number of Labels
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac   Level: intermediate
6886c58f1c22SToby Isaac 
6887bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6888c58f1c22SToby Isaac @*/
6889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6890d71ae5a4SJacob Faibussowitsch {
68915d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6892c58f1c22SToby Isaac   PetscInt    n    = 0;
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac   PetscFunctionBegin;
6895c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6896534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68979371c9d4SSatish Balay   while (next) {
68989371c9d4SSatish Balay     ++n;
68999371c9d4SSatish Balay     next = next->next;
69009371c9d4SSatish Balay   }
6901c58f1c22SToby Isaac   *numLabels = n;
6902c58f1c22SToby Isaac   PetscFunctionReturn(0);
6903c58f1c22SToby Isaac }
6904c58f1c22SToby Isaac 
6905c58f1c22SToby Isaac /*@C
6906c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Not Collective
6909c58f1c22SToby Isaac 
6910c58f1c22SToby Isaac   Input Parameters:
6911bb7acecfSBarry Smith + dm - The `DM` object
6912c58f1c22SToby Isaac - n  - the label number
6913c58f1c22SToby Isaac 
6914c58f1c22SToby Isaac   Output Parameter:
6915c58f1c22SToby Isaac . name - the label name
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac   Level: intermediate
6918c58f1c22SToby Isaac 
6919bb7acecfSBarry Smith   Developer Note:
6920bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6921bb7acecfSBarry Smith 
6922bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6923c58f1c22SToby Isaac @*/
6924d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6925d71ae5a4SJacob Faibussowitsch {
69265d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6927c58f1c22SToby Isaac   PetscInt    l    = 0;
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac   PetscFunctionBegin;
6930c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6931c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6932c58f1c22SToby Isaac   while (next) {
6933c58f1c22SToby Isaac     if (l == n) {
69349566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
6935c58f1c22SToby Isaac       PetscFunctionReturn(0);
6936c58f1c22SToby Isaac     }
6937c58f1c22SToby Isaac     ++l;
6938c58f1c22SToby Isaac     next = next->next;
6939c58f1c22SToby Isaac   }
694063a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6941c58f1c22SToby Isaac }
6942c58f1c22SToby Isaac 
6943c58f1c22SToby Isaac /*@C
6944bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac   Not Collective
6947c58f1c22SToby Isaac 
6948c58f1c22SToby Isaac   Input Parameters:
6949bb7acecfSBarry Smith + dm   - The `DM` object
6950c58f1c22SToby Isaac - name - The label name
6951c58f1c22SToby Isaac 
6952c58f1c22SToby Isaac   Output Parameter:
6953bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6954c58f1c22SToby Isaac 
6955c58f1c22SToby Isaac   Level: intermediate
6956c58f1c22SToby Isaac 
6957bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6958c58f1c22SToby Isaac @*/
6959d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6960d71ae5a4SJacob Faibussowitsch {
69615d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6962d67d17b1SMatthew G. Knepley   const char *lname;
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   PetscFunctionBegin;
6965c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6966c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6967534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6968c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6969c58f1c22SToby Isaac   while (next) {
69709566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69719566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6972c58f1c22SToby Isaac     if (*hasLabel) break;
6973c58f1c22SToby Isaac     next = next->next;
6974c58f1c22SToby Isaac   }
6975c58f1c22SToby Isaac   PetscFunctionReturn(0);
6976c58f1c22SToby Isaac }
6977c58f1c22SToby Isaac 
6978c58f1c22SToby Isaac /*@C
6979bb7acecfSBarry Smith   DMGetLabel - Return the label of a given name, or NULL, from a `DM`
6980c58f1c22SToby Isaac 
6981c58f1c22SToby Isaac   Not Collective
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   Input Parameters:
6984bb7acecfSBarry Smith + dm   - The `DM` object
6985c58f1c22SToby Isaac - name - The label name
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   Output Parameter:
6988bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent
6989c58f1c22SToby Isaac 
6990bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6991bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6992bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6993bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6994bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6995bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6996bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69976d7c9049SMatthew G. Knepley 
6998c58f1c22SToby Isaac   Level: intermediate
6999c58f1c22SToby Isaac 
7000bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
7001c58f1c22SToby Isaac @*/
7002d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7003d71ae5a4SJacob Faibussowitsch {
70045d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7005c58f1c22SToby Isaac   PetscBool   hasLabel;
7006d67d17b1SMatthew G. Knepley   const char *lname;
7007c58f1c22SToby Isaac 
7008c58f1c22SToby Isaac   PetscFunctionBegin;
7009c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7010c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7011c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7012c58f1c22SToby Isaac   *label = NULL;
7013c58f1c22SToby Isaac   while (next) {
70149566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70159566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7016c58f1c22SToby Isaac     if (hasLabel) {
7017c58f1c22SToby Isaac       *label = next->label;
7018c58f1c22SToby Isaac       break;
7019c58f1c22SToby Isaac     }
7020c58f1c22SToby Isaac     next = next->next;
7021c58f1c22SToby Isaac   }
7022c58f1c22SToby Isaac   PetscFunctionReturn(0);
7023c58f1c22SToby Isaac }
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac /*@C
7026bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
7027c58f1c22SToby Isaac 
7028c58f1c22SToby Isaac   Not Collective
7029c58f1c22SToby Isaac 
7030c58f1c22SToby Isaac   Input Parameters:
7031bb7acecfSBarry Smith + dm - The `DM` object
7032c58f1c22SToby Isaac - n  - the label number
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac   Output Parameter:
7035c58f1c22SToby Isaac . label - the label
7036c58f1c22SToby Isaac 
7037c58f1c22SToby Isaac   Level: intermediate
7038c58f1c22SToby Isaac 
7039bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7040c58f1c22SToby Isaac @*/
7041d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7042d71ae5a4SJacob Faibussowitsch {
70435d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7044c58f1c22SToby Isaac   PetscInt    l    = 0;
7045c58f1c22SToby Isaac 
7046c58f1c22SToby Isaac   PetscFunctionBegin;
7047c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7048c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7049c58f1c22SToby Isaac   while (next) {
7050c58f1c22SToby Isaac     if (l == n) {
7051c58f1c22SToby Isaac       *label = next->label;
7052c58f1c22SToby Isaac       PetscFunctionReturn(0);
7053c58f1c22SToby Isaac     }
7054c58f1c22SToby Isaac     ++l;
7055c58f1c22SToby Isaac     next = next->next;
7056c58f1c22SToby Isaac   }
705763a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7058c58f1c22SToby Isaac }
7059c58f1c22SToby Isaac 
7060c58f1c22SToby Isaac /*@C
7061bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7062c58f1c22SToby Isaac 
7063c58f1c22SToby Isaac   Not Collective
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Input Parameters:
7066bb7acecfSBarry Smith + dm   - The `DM` object
7067bb7acecfSBarry Smith - label - The `DMLabel`
7068c58f1c22SToby Isaac 
7069c58f1c22SToby Isaac   Level: developer
7070c58f1c22SToby Isaac 
7071bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7072c58f1c22SToby Isaac @*/
7073d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7074d71ae5a4SJacob Faibussowitsch {
70755d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7076c58f1c22SToby Isaac   PetscBool   hasLabel;
7077d67d17b1SMatthew G. Knepley   const char *lname;
70785d80c0bfSVaclav Hapla   PetscBool   flg;
7079c58f1c22SToby Isaac 
7080c58f1c22SToby Isaac   PetscFunctionBegin;
7081c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70839566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70847a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70859566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7086c58f1c22SToby Isaac   tmpLabel->label  = label;
7087c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70885d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70895d80c0bfSVaclav Hapla   *p = tmpLabel;
70909566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70919566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70925d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70939566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7094ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7095c58f1c22SToby Isaac   PetscFunctionReturn(0);
7096c58f1c22SToby Isaac }
7097c58f1c22SToby Isaac 
7098c58f1c22SToby Isaac /*@C
70994a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
71004a7ee7d0SMatthew G. Knepley 
71014a7ee7d0SMatthew G. Knepley   Not Collective
71024a7ee7d0SMatthew G. Knepley 
71034a7ee7d0SMatthew G. Knepley   Input Parameters:
7104bb7acecfSBarry Smith + dm    - The `DM` object
7105bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
71064a7ee7d0SMatthew G. Knepley 
7107bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7108bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7109bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7110bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7111bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7112bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7113bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
71144a7ee7d0SMatthew G. Knepley 
71154a7ee7d0SMatthew G. Knepley   Level: intermediate
71164a7ee7d0SMatthew G. Knepley 
7117bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
71184a7ee7d0SMatthew G. Knepley @*/
7119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7120d71ae5a4SJacob Faibussowitsch {
71214a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
71224a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
71234a7ee7d0SMatthew G. Knepley   const char *name, *lname;
71244a7ee7d0SMatthew G. Knepley 
71254a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
71264a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71274a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
71289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
71294a7ee7d0SMatthew G. Knepley   while (next) {
71309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
71319566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
71324a7ee7d0SMatthew G. Knepley     if (hasLabel) {
71339566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
71349566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
71354a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
71369566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
71374a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
71389566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
71394a7ee7d0SMatthew G. Knepley       next->label = label;
71404a7ee7d0SMatthew G. Knepley       break;
71414a7ee7d0SMatthew G. Knepley     }
71424a7ee7d0SMatthew G. Knepley     next = next->next;
71434a7ee7d0SMatthew G. Knepley   }
71444a7ee7d0SMatthew G. Knepley   PetscFunctionReturn(0);
71454a7ee7d0SMatthew G. Knepley }
71464a7ee7d0SMatthew G. Knepley 
71474a7ee7d0SMatthew G. Knepley /*@C
7148bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Not Collective
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   Input Parameters:
7153bb7acecfSBarry Smith + dm   - The `DM` object
7154c58f1c22SToby Isaac - name - The label name
7155c58f1c22SToby Isaac 
7156c58f1c22SToby Isaac   Output Parameter:
7157bb7acecfSBarry Smith . label - The `DMLabel`, or NULL if the label is absent. Pass in NULL to call `DMLabelDestroy()` on the label, otherwise the
7158bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7159c58f1c22SToby Isaac 
7160c58f1c22SToby Isaac   Level: developer
7161c58f1c22SToby Isaac 
7162bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7163c58f1c22SToby Isaac @*/
7164d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7165d71ae5a4SJacob Faibussowitsch {
716695d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7167c58f1c22SToby Isaac   PetscBool   hasLabel;
7168d67d17b1SMatthew G. Knepley   const char *lname;
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac   PetscFunctionBegin;
7171c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7172e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7173e5472504SVaclav Hapla   if (label) {
7174e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7175c58f1c22SToby Isaac     *label = NULL;
7176e5472504SVaclav Hapla   }
71775d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71789566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71799566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7180c58f1c22SToby Isaac     if (hasLabel) {
718195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71829566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
718395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71849566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7185ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
718695d578d6SVaclav Hapla       if (label) *label = link->label;
71879566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71889566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7189c58f1c22SToby Isaac       break;
7190c58f1c22SToby Isaac     }
7191c58f1c22SToby Isaac   }
7192c58f1c22SToby Isaac   PetscFunctionReturn(0);
7193c58f1c22SToby Isaac }
7194c58f1c22SToby Isaac 
7195306894acSVaclav Hapla /*@
7196bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7197306894acSVaclav Hapla 
7198306894acSVaclav Hapla   Not Collective
7199306894acSVaclav Hapla 
7200306894acSVaclav Hapla   Input Parameters:
7201bb7acecfSBarry Smith + dm   - The `DM` object
7202bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7203306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7204306894acSVaclav Hapla 
7205306894acSVaclav Hapla   Level: developer
7206306894acSVaclav Hapla 
7207bb7acecfSBarry Smith   Note:
7208306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7209bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7210306894acSVaclav Hapla   *label nullified.
7211306894acSVaclav Hapla 
7212bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7213306894acSVaclav Hapla @*/
7214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7215d71ae5a4SJacob Faibussowitsch {
721643e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7217306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7218306894acSVaclav Hapla 
7219306894acSVaclav Hapla   PetscFunctionBegin;
7220306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7221306894acSVaclav Hapla   PetscValidPointer(label, 2);
7222f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7223306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7224306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
72255d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
722643e45a93SVaclav Hapla     if (*label == link->label) {
7227306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
722843e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7229306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7230ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
723143e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
72329566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
72339566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7234306894acSVaclav Hapla       break;
7235306894acSVaclav Hapla     }
7236306894acSVaclav Hapla   }
72377a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7238306894acSVaclav Hapla   PetscFunctionReturn(0);
7239306894acSVaclav Hapla }
7240306894acSVaclav Hapla 
7241c58f1c22SToby Isaac /*@C
7242c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7243c58f1c22SToby Isaac 
7244c58f1c22SToby Isaac   Not Collective
7245c58f1c22SToby Isaac 
7246c58f1c22SToby Isaac   Input Parameters:
7247bb7acecfSBarry Smith + dm   - The `DM` object
7248c58f1c22SToby Isaac - name - The label name
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   Output Parameter:
7251c58f1c22SToby Isaac . output - The flag for output
7252c58f1c22SToby Isaac 
7253c58f1c22SToby Isaac   Level: developer
7254c58f1c22SToby Isaac 
7255bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7256c58f1c22SToby Isaac @*/
7257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7258d71ae5a4SJacob Faibussowitsch {
72595d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7260d67d17b1SMatthew G. Knepley   const char *lname;
7261c58f1c22SToby Isaac 
7262c58f1c22SToby Isaac   PetscFunctionBegin;
7263c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7264dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7265dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7266c58f1c22SToby Isaac   while (next) {
7267c58f1c22SToby Isaac     PetscBool flg;
7268c58f1c22SToby Isaac 
72699566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72709566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72719371c9d4SSatish Balay     if (flg) {
72729371c9d4SSatish Balay       *output = next->output;
72739371c9d4SSatish Balay       PetscFunctionReturn(0);
72749371c9d4SSatish Balay     }
7275c58f1c22SToby Isaac     next = next->next;
7276c58f1c22SToby Isaac   }
727798921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7278c58f1c22SToby Isaac }
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac /*@C
7281bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7282c58f1c22SToby Isaac 
7283c58f1c22SToby Isaac   Not Collective
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Input Parameters:
7286bb7acecfSBarry Smith + dm     - The `DM` object
7287c58f1c22SToby Isaac . name   - The label name
7288bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac   Level: developer
7291c58f1c22SToby Isaac 
7292bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7293c58f1c22SToby Isaac @*/
7294d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7295d71ae5a4SJacob Faibussowitsch {
72965d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7297d67d17b1SMatthew G. Knepley   const char *lname;
7298c58f1c22SToby Isaac 
7299c58f1c22SToby Isaac   PetscFunctionBegin;
7300c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7301534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7302c58f1c22SToby Isaac   while (next) {
7303c58f1c22SToby Isaac     PetscBool flg;
7304c58f1c22SToby Isaac 
73059566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
73069566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
73079371c9d4SSatish Balay     if (flg) {
73089371c9d4SSatish Balay       next->output = output;
73099371c9d4SSatish Balay       PetscFunctionReturn(0);
73109371c9d4SSatish Balay     }
7311c58f1c22SToby Isaac     next = next->next;
7312c58f1c22SToby Isaac   }
731398921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7314c58f1c22SToby Isaac }
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac /*@
7317bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7318c58f1c22SToby Isaac 
7319d083f849SBarry Smith   Collective on dmA
7320c58f1c22SToby Isaac 
7321d8d19677SJose E. Roman   Input Parameters:
7322bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7323bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7324bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7325bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7326bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7327c58f1c22SToby Isaac 
7328c58f1c22SToby Isaac   Level: intermediate
7329c58f1c22SToby Isaac 
7330bb7acecfSBarry Smith   Note:
73312cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7332c58f1c22SToby Isaac 
7333bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7334c58f1c22SToby Isaac @*/
7335d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7336d71ae5a4SJacob Faibussowitsch {
73372cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7338c58f1c22SToby Isaac   const char *name;
7339c58f1c22SToby Isaac   PetscBool   flg;
73405d80c0bfSVaclav Hapla   DMLabelLink link;
7341c58f1c22SToby Isaac 
73425d80c0bfSVaclav Hapla   PetscFunctionBegin;
73435d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73445d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73455d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
73465d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73477a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73485d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
73495d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73505d80c0bfSVaclav Hapla     label = link->label;
73519566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73525d80c0bfSVaclav Hapla     if (!all) {
73539566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7354c58f1c22SToby Isaac       if (flg) continue;
73559566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73567d5acc75SStefano Zampini       if (flg) continue;
73579566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7358ba2698f1SMatthew G. Knepley       if (flg) continue;
73595d80c0bfSVaclav Hapla     }
73609566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73612cbb9b06SVaclav Hapla     if (labelOld) {
73622cbb9b06SVaclav Hapla       switch (emode) {
7363d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7364d71ae5a4SJacob Faibussowitsch         continue;
7365d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7366d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7367d71ae5a4SJacob Faibussowitsch         break;
7368d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7369d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7370d71ae5a4SJacob Faibussowitsch       default:
7371d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73722cbb9b06SVaclav Hapla       }
73732cbb9b06SVaclav Hapla     }
73745d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73759566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73765d80c0bfSVaclav Hapla     } else {
73775d80c0bfSVaclav Hapla       labelNew = label;
73785d80c0bfSVaclav Hapla     }
73799566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73809566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7381c58f1c22SToby Isaac   }
7382c58f1c22SToby Isaac   PetscFunctionReturn(0);
7383c58f1c22SToby Isaac }
7384461a15a0SLisandro Dalcin 
7385609dae6eSVaclav Hapla /*@C
7386bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7387609dae6eSVaclav Hapla 
73885efe38ccSVaclav Hapla   Collective
7389609dae6eSVaclav Hapla 
7390609dae6eSVaclav Hapla   Input Parameters:
7391bb7acecfSBarry Smith + dm0 - First `DM` object
7392bb7acecfSBarry Smith - dm1 - Second `DM` object
7393609dae6eSVaclav Hapla 
7394609dae6eSVaclav Hapla   Output Parameters
73955efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7396609dae6eSVaclav Hapla - message - (Optional) Message describing the difference, or NULL if there is no difference
7397609dae6eSVaclav Hapla 
7398609dae6eSVaclav Hapla   Level: intermediate
7399609dae6eSVaclav Hapla 
7400609dae6eSVaclav Hapla   Notes:
7401bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7402bb7acecfSBarry Smith 
7403bb7acecfSBarry Smith   If equal is passed as NULL and difference is found, an error is thrown on all processes.
7404bb7acecfSBarry Smith 
7405bb7acecfSBarry Smith   Make sure to pass equal is NULL on all processes or none of them.
7406609dae6eSVaclav Hapla 
74075efe38ccSVaclav Hapla   The output message is set independently on each rank.
7408bb7acecfSBarry Smith 
7409bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7410bb7acecfSBarry Smith 
7411bb7acecfSBarry Smith   If message is passed as NULL and a difference is found, the difference description is printed to stderr in synchronized manner.
7412bb7acecfSBarry Smith 
7413bb7acecfSBarry Smith   Make sure to pass message as NULL on all processes or no processes.
7414609dae6eSVaclav Hapla 
7415609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7416bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7417609dae6eSVaclav Hapla 
7418bb7acecfSBarry Smith   Fortran Note:
7419bb7acecfSBarry Smith   This function is not available from Fortran.
7420609dae6eSVaclav Hapla 
7421bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7422609dae6eSVaclav Hapla @*/
7423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7424d71ae5a4SJacob Faibussowitsch {
74255efe38ccSVaclav Hapla   PetscInt    n, i;
7426609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
74275efe38ccSVaclav Hapla   PetscBool   eq;
7428609dae6eSVaclav Hapla   MPI_Comm    comm;
74295efe38ccSVaclav Hapla   PetscMPIInt rank;
7430609dae6eSVaclav Hapla 
7431609dae6eSVaclav Hapla   PetscFunctionBegin;
7432609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7433609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7434609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
74355efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7436609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
74379566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
74389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
74395efe38ccSVaclav Hapla   {
74405efe38ccSVaclav Hapla     PetscInt n1;
74415efe38ccSVaclav Hapla 
74429566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
74439566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
74445efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
744548a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
74469566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
74475efe38ccSVaclav Hapla     if (!eq) goto finish;
74485efe38ccSVaclav Hapla   }
74495efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7450609dae6eSVaclav Hapla     DMLabel     l0, l1;
7451609dae6eSVaclav Hapla     const char *name;
7452609dae6eSVaclav Hapla     char       *msgInner;
7453609dae6eSVaclav Hapla 
7454609dae6eSVaclav Hapla     /* Ignore label order */
74559566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74569566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74579566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7458609dae6eSVaclav Hapla     if (!l1) {
745963a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74605efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74615efe38ccSVaclav Hapla       break;
7462609dae6eSVaclav Hapla     }
74639566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74649566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74659566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74665efe38ccSVaclav Hapla     if (!eq) break;
7467609dae6eSVaclav Hapla   }
74689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7469609dae6eSVaclav Hapla finish:
74705efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7471609dae6eSVaclav Hapla   if (message) {
7472609dae6eSVaclav Hapla     *message = NULL;
747348a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74745efe38ccSVaclav Hapla   } else {
747548a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74769566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74775efe38ccSVaclav Hapla   }
74785efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74795efe38ccSVaclav Hapla   if (equal) *equal = eq;
74807a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
7481609dae6eSVaclav Hapla   PetscFunctionReturn(0);
7482609dae6eSVaclav Hapla }
7483609dae6eSVaclav Hapla 
7484d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7485d71ae5a4SJacob Faibussowitsch {
7486461a15a0SLisandro Dalcin   PetscFunctionBegin;
7487461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7488461a15a0SLisandro Dalcin   if (!*label) {
74899566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74909566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7491461a15a0SLisandro Dalcin   }
74929566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
7493461a15a0SLisandro Dalcin   PetscFunctionReturn(0);
7494461a15a0SLisandro Dalcin }
7495461a15a0SLisandro Dalcin 
74960fdc7489SMatthew Knepley /*
74970fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74980fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74990fdc7489SMatthew Knepley   (label, id) pair in the DM.
75000fdc7489SMatthew Knepley 
75010fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
75020fdc7489SMatthew Knepley   each label.
75030fdc7489SMatthew Knepley */
7504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7505d71ae5a4SJacob Faibussowitsch {
75060fdc7489SMatthew Knepley   DMUniversalLabel ul;
75070fdc7489SMatthew Knepley   PetscBool       *active;
75080fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
75090fdc7489SMatthew Knepley 
75100fdc7489SMatthew Knepley   PetscFunctionBegin;
75119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
75129566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
75139566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
75149566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
75150fdc7489SMatthew Knepley   ul->Nl = 0;
75160fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75170fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
75180fdc7489SMatthew Knepley     const char *name;
75190fdc7489SMatthew Knepley 
75209566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75219566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
75229566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
75230fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
75240fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
75250fdc7489SMatthew Knepley   }
75269566063dSJacob 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));
75270fdc7489SMatthew Knepley   ul->Nv = 0;
75280fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75290fdc7489SMatthew Knepley     DMLabel     label;
75300fdc7489SMatthew Knepley     PetscInt    nv;
75310fdc7489SMatthew Knepley     const char *name;
75320fdc7489SMatthew Knepley 
75330fdc7489SMatthew Knepley     if (!active[l]) continue;
75349566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
75359566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75379566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
75380fdc7489SMatthew Knepley     ul->indices[m] = l;
75390fdc7489SMatthew Knepley     ul->Nv += nv;
75400fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
75410fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
75420fdc7489SMatthew Knepley     ++m;
75430fdc7489SMatthew Knepley   }
75440fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
75450fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
75460fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
75470fdc7489SMatthew Knepley   }
75480fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
75490fdc7489SMatthew Knepley     PetscInt b;
75500fdc7489SMatthew Knepley 
75510fdc7489SMatthew Knepley     ul->masks[l] = 0;
75520fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75530fdc7489SMatthew Knepley   }
75549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75550fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75560fdc7489SMatthew Knepley     DMLabel         label;
75570fdc7489SMatthew Knepley     IS              valueIS;
75580fdc7489SMatthew Knepley     const PetscInt *varr;
75590fdc7489SMatthew Knepley     PetscInt        nv, v;
75600fdc7489SMatthew Knepley 
75610fdc7489SMatthew Knepley     if (!active[l]) continue;
75629566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75639566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7566ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75679566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75689566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75699566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75700fdc7489SMatthew Knepley     ++m;
75710fdc7489SMatthew Knepley   }
75729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75730fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75740fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75750fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75760fdc7489SMatthew Knepley 
75770fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75780fdc7489SMatthew Knepley       DMLabel  label;
75790649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75800fdc7489SMatthew Knepley 
75810fdc7489SMatthew Knepley       if (!active[l]) continue;
75829566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75849566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75859371c9d4SSatish Balay       if (val == defval) {
75869371c9d4SSatish Balay         ++m;
75879371c9d4SSatish Balay         continue;
75889371c9d4SSatish Balay       }
75890649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75900fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75919566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
759263a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75930fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75940fdc7489SMatthew Knepley       ++m;
75950fdc7489SMatthew Knepley     }
75969566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75970fdc7489SMatthew Knepley   }
75989566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75990fdc7489SMatthew Knepley   *universal = ul;
76000fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76010fdc7489SMatthew Knepley }
76020fdc7489SMatthew Knepley 
7603d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7604d71ae5a4SJacob Faibussowitsch {
76050fdc7489SMatthew Knepley   PetscInt l;
76060fdc7489SMatthew Knepley 
76070fdc7489SMatthew Knepley   PetscFunctionBegin;
76089566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
76099566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
76109566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
76119566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
76129566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
76130fdc7489SMatthew Knepley   *universal = NULL;
76140fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76150fdc7489SMatthew Knepley }
76160fdc7489SMatthew Knepley 
7617d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7618d71ae5a4SJacob Faibussowitsch {
76190fdc7489SMatthew Knepley   PetscFunctionBegin;
76200fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
76210fdc7489SMatthew Knepley   *ulabel = ul->label;
76220fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76230fdc7489SMatthew Knepley }
76240fdc7489SMatthew Knepley 
7625d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7626d71ae5a4SJacob Faibussowitsch {
76270fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
76280fdc7489SMatthew Knepley 
76290fdc7489SMatthew Knepley   PetscFunctionBegin;
7630064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
76310fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
76329566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
76339566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
76340fdc7489SMatthew Knepley   }
76350fdc7489SMatthew Knepley   if (preserveOrder) {
76360fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
76370fdc7489SMatthew Knepley       const char *name;
76380fdc7489SMatthew Knepley       PetscBool   match;
76390fdc7489SMatthew Knepley 
76409566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
76419566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
764263a3b9bcSJacob 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]);
76430fdc7489SMatthew Knepley     }
76440fdc7489SMatthew Knepley   }
76450fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76460fdc7489SMatthew Knepley }
76470fdc7489SMatthew Knepley 
7648d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7649d71ae5a4SJacob Faibussowitsch {
76500fdc7489SMatthew Knepley   PetscInt l;
76510fdc7489SMatthew Knepley 
76520fdc7489SMatthew Knepley   PetscFunctionBegin;
76530fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76540fdc7489SMatthew Knepley     DMLabel  label;
76550fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76560fdc7489SMatthew Knepley 
76570fdc7489SMatthew Knepley     if (lval) {
76589566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76599566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76609566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76610fdc7489SMatthew Knepley     }
76620fdc7489SMatthew Knepley   }
76630fdc7489SMatthew Knepley   PetscFunctionReturn(0);
76640fdc7489SMatthew Knepley }
7665a8fb8f29SToby Isaac 
7666a8fb8f29SToby Isaac /*@
7667bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7668bb7acecfSBarry Smith 
7669bb7acecfSBarry Smith   Not collective
7670a8fb8f29SToby Isaac 
7671a8fb8f29SToby Isaac   Input Parameter:
7672bb7acecfSBarry Smith . dm - The `DM` object
7673a8fb8f29SToby Isaac 
7674a8fb8f29SToby Isaac   Output Parameter:
7675bb7acecfSBarry Smith . cdm - The coarse `DM`
7676a8fb8f29SToby Isaac 
7677a8fb8f29SToby Isaac   Level: intermediate
7678a8fb8f29SToby Isaac 
7679bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7680a8fb8f29SToby Isaac @*/
7681d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7682d71ae5a4SJacob Faibussowitsch {
7683a8fb8f29SToby Isaac   PetscFunctionBegin;
7684a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7685a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7686a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7687a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7688a8fb8f29SToby Isaac }
7689a8fb8f29SToby Isaac 
7690a8fb8f29SToby Isaac /*@
7691bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7692a8fb8f29SToby Isaac 
7693a8fb8f29SToby Isaac   Input Parameters:
7694bb7acecfSBarry Smith + dm - The `DM` object
7695bb7acecfSBarry Smith - cdm - The coarse `DM`
7696a8fb8f29SToby Isaac 
7697a8fb8f29SToby Isaac   Level: intermediate
7698a8fb8f29SToby Isaac 
7699bb7acecfSBarry Smith   Note:
7700bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7701bb7acecfSBarry Smith 
7702bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7703a8fb8f29SToby Isaac @*/
7704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7705d71ae5a4SJacob Faibussowitsch {
7706a8fb8f29SToby Isaac   PetscFunctionBegin;
7707a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7708a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
770989d734beSBarry Smith   if (dm == cdm) cdm = NULL;
77109566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
77119566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7712a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7713a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7714a8fb8f29SToby Isaac }
7715a8fb8f29SToby Isaac 
771688bdff64SToby Isaac /*@
7717bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
771888bdff64SToby Isaac 
771988bdff64SToby Isaac   Input Parameter:
7720bb7acecfSBarry Smith . dm - The `DM` object
772188bdff64SToby Isaac 
772288bdff64SToby Isaac   Output Parameter:
7723bb7acecfSBarry Smith . fdm - The fine `DM`
772488bdff64SToby Isaac 
772588bdff64SToby Isaac   Level: intermediate
772688bdff64SToby Isaac 
7727bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
772888bdff64SToby Isaac @*/
7729d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7730d71ae5a4SJacob Faibussowitsch {
773188bdff64SToby Isaac   PetscFunctionBegin;
773288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
773388bdff64SToby Isaac   PetscValidPointer(fdm, 2);
773488bdff64SToby Isaac   *fdm = dm->fineMesh;
773588bdff64SToby Isaac   PetscFunctionReturn(0);
773688bdff64SToby Isaac }
773788bdff64SToby Isaac 
773888bdff64SToby Isaac /*@
7739bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
774088bdff64SToby Isaac 
774188bdff64SToby Isaac   Input Parameters:
7742bb7acecfSBarry Smith + dm - The `DM` object
7743bb7acecfSBarry Smith - fdm - The fine `DM`
774488bdff64SToby Isaac 
7745bb7acecfSBarry Smith   Level: developer
774688bdff64SToby Isaac 
7747bb7acecfSBarry Smith   Note:
7748bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7749bb7acecfSBarry Smith 
7750bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
775188bdff64SToby Isaac @*/
7752d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7753d71ae5a4SJacob Faibussowitsch {
775488bdff64SToby Isaac   PetscFunctionBegin;
775588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
775688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
775789d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77589566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
776088bdff64SToby Isaac   dm->fineMesh = fdm;
776188bdff64SToby Isaac   PetscFunctionReturn(0);
776288bdff64SToby Isaac }
776388bdff64SToby Isaac 
7764a6ba4734SToby Isaac /*@C
7765bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7766a6ba4734SToby Isaac 
7767783e2ec8SMatthew G. Knepley   Collective on dm
7768783e2ec8SMatthew G. Knepley 
7769a6ba4734SToby Isaac   Input Parameters:
7770bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7771bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7772a6ba4734SToby Isaac . name     - The BC name
777345480ffeSMatthew G. Knepley . label    - The label defining constrained points
7774bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
777545480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7776a6ba4734SToby Isaac . field    - The field to constrain
777745480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7778a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7779a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
778056cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7781a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7782a6ba4734SToby Isaac 
778345480ffeSMatthew G. Knepley   Output Parameter:
778445480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
778545480ffeSMatthew G. Knepley 
7786a6ba4734SToby Isaac   Options Database Keys:
7787a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7788a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7789a6ba4734SToby Isaac 
7790bb7acecfSBarry Smith   Notes:
7791bb7acecfSBarry Smith   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if `DM_BC_ESSENTIAL`, then the calling sequence is:
779256cf3b9cSMatthew G. Knepley 
779356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
779456cf3b9cSMatthew G. Knepley 
7795bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
779656cf3b9cSMatthew G. Knepley 
779756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
779856cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
779956cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
780056cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
780156cf3b9cSMatthew G. Knepley 
780256cf3b9cSMatthew G. Knepley + dim - the spatial dimension
780356cf3b9cSMatthew G. Knepley . Nf - the number of fields
780456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
780556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
780656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
780756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
780856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
780956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
781056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
781156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
781256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
781356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
781456cf3b9cSMatthew G. Knepley . t - current time
781556cf3b9cSMatthew G. Knepley . x - coordinates of the current point
781656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
781756cf3b9cSMatthew G. Knepley . constants - constant parameters
781856cf3b9cSMatthew G. Knepley - bcval - output values at the current point
781956cf3b9cSMatthew G. Knepley 
7820ed808b8fSJed Brown   Level: intermediate
7821a6ba4734SToby Isaac 
7822db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7823a6ba4734SToby Isaac @*/
7824d71ae5a4SJacob 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)
7825d71ae5a4SJacob Faibussowitsch {
7826e5e52638SMatthew G. Knepley   PetscDS ds;
7827a6ba4734SToby Isaac 
7828a6ba4734SToby Isaac   PetscFunctionBegin;
7829a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7830783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
783145480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
783245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
783345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
783445480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
783501a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
78369566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7837799db056SMatthew G. Knepley   /* Complete label */
7838799db056SMatthew G. Knepley   if (label) {
7839799db056SMatthew G. Knepley     PetscObject  obj;
7840799db056SMatthew G. Knepley     PetscClassId id;
7841799db056SMatthew G. Knepley 
7842799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7843799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7844799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7845799db056SMatthew G. Knepley       DM plex;
7846799db056SMatthew G. Knepley 
7847799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7848799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7849799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7850799db056SMatthew G. Knepley     }
7851799db056SMatthew G. Knepley   }
78529566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
7853a6ba4734SToby Isaac   PetscFunctionReturn(0);
7854a6ba4734SToby Isaac }
7855a6ba4734SToby Isaac 
785645480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7857d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7858d71ae5a4SJacob Faibussowitsch {
7859e5e52638SMatthew G. Knepley   PetscDS     ds;
7860dff059c6SToby Isaac   DMBoundary *lastnext;
7861e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7862e6f8dbb6SToby Isaac 
7863e6f8dbb6SToby Isaac   PetscFunctionBegin;
78649566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7865e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
786647a1f5adSToby Isaac   if (dm->boundary) {
786747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
786847a1f5adSToby Isaac 
786947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
787047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
787147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
787247a1f5adSToby Isaac     while (next) {
787347a1f5adSToby Isaac       DMBoundary b = next;
787447a1f5adSToby Isaac 
787547a1f5adSToby Isaac       next = b->next;
78769566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7877a6ba4734SToby Isaac     }
787847a1f5adSToby Isaac     dm->boundary = NULL;
7879a6ba4734SToby Isaac   }
788047a1f5adSToby Isaac 
7881dff059c6SToby Isaac   lastnext = &(dm->boundary);
7882e6f8dbb6SToby Isaac   while (dsbound) {
7883e6f8dbb6SToby Isaac     DMBoundary dmbound;
7884e6f8dbb6SToby Isaac 
78859566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7886e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
788745480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
788847a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7889dff059c6SToby Isaac     *lastnext = dmbound;
7890dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7891dff059c6SToby Isaac     dsbound   = dsbound->next;
7892a6ba4734SToby Isaac   }
7893a6ba4734SToby Isaac   PetscFunctionReturn(0);
7894a6ba4734SToby Isaac }
7895a6ba4734SToby Isaac 
7896bb7acecfSBarry Smith /* TODO: missing manual page */
7897d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7898d71ae5a4SJacob Faibussowitsch {
7899b95f2879SToby Isaac   DMBoundary b;
7900a6ba4734SToby Isaac 
7901a6ba4734SToby Isaac   PetscFunctionBegin;
7902a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7903534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7904a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
79059566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7906b95f2879SToby Isaac   b = dm->boundary;
7907a6ba4734SToby Isaac   while (b && !(*isBd)) {
7908e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7909e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7910a6ba4734SToby Isaac     PetscInt   i;
7911a6ba4734SToby Isaac 
791245480ffeSMatthew G. Knepley     if (label) {
79139566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7914a6ba4734SToby Isaac     }
7915a6ba4734SToby Isaac     b = b->next;
7916a6ba4734SToby Isaac   }
7917a6ba4734SToby Isaac   PetscFunctionReturn(0);
7918a6ba4734SToby Isaac }
79194d6f44ffSToby Isaac 
79204d6f44ffSToby Isaac /*@C
7921bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7922a6e0b375SMatthew G. Knepley 
7923bb7acecfSBarry Smith   Collective on dm
79244d6f44ffSToby Isaac 
79254d6f44ffSToby Isaac   Input Parameters:
7926bb7acecfSBarry Smith + dm      - The `DM`
79270709b2feSToby Isaac . time    - The time
79284d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
79294d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
79304d6f44ffSToby Isaac - mode    - The insertion mode for values
79314d6f44ffSToby Isaac 
79324d6f44ffSToby Isaac   Output Parameter:
79334d6f44ffSToby Isaac . X - vector
79344d6f44ffSToby Isaac 
79354d6f44ffSToby Isaac    Calling sequence of func:
793677b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
79374d6f44ffSToby Isaac 
79384d6f44ffSToby Isaac +  dim - The spatial dimension
79398ec8862eSJed Brown .  time - The time at which to sample
79404d6f44ffSToby Isaac .  x   - The coordinates
794177b739a6SMatthew Knepley .  Nc  - The number of components
79424d6f44ffSToby Isaac .  u   - The output field values
79434d6f44ffSToby Isaac -  ctx - optional user-defined function context
79444d6f44ffSToby Isaac 
79454d6f44ffSToby Isaac   Level: developer
79464d6f44ffSToby Isaac 
7947bb7acecfSBarry Smith   Developer Notes:
7948bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7949bb7acecfSBarry Smith 
7950bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7951bb7acecfSBarry Smith 
7952db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79534d6f44ffSToby Isaac @*/
7954d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7955d71ae5a4SJacob Faibussowitsch {
79564d6f44ffSToby Isaac   Vec localX;
79574d6f44ffSToby Isaac 
79584d6f44ffSToby Isaac   PetscFunctionBegin;
79594d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79609566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7961f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79629566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79639566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79649566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79659566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79664d6f44ffSToby Isaac   PetscFunctionReturn(0);
79674d6f44ffSToby Isaac }
79684d6f44ffSToby Isaac 
7969a6e0b375SMatthew G. Knepley /*@C
7970bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7971a6e0b375SMatthew G. Knepley 
7972a6e0b375SMatthew G. Knepley   Not collective
7973a6e0b375SMatthew G. Knepley 
7974a6e0b375SMatthew G. Knepley   Input Parameters:
7975bb7acecfSBarry Smith + dm      - The `DM`
7976a6e0b375SMatthew G. Knepley . time    - The time
7977a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7978a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7979a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7980a6e0b375SMatthew G. Knepley 
7981a6e0b375SMatthew G. Knepley   Output Parameter:
7982a6e0b375SMatthew G. Knepley . localX - vector
7983a6e0b375SMatthew G. Knepley 
7984a6e0b375SMatthew G. Knepley    Calling sequence of func:
798577b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7986a6e0b375SMatthew G. Knepley 
7987a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7988a6e0b375SMatthew G. Knepley .  x   - The coordinates
798977b739a6SMatthew Knepley .  Nc  - The number of components
7990a6e0b375SMatthew G. Knepley .  u   - The output field values
7991a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7992a6e0b375SMatthew G. Knepley 
7993a6e0b375SMatthew G. Knepley   Level: developer
7994a6e0b375SMatthew G. Knepley 
7995bb7acecfSBarry Smith   Developer Notes:
7996bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7997bb7acecfSBarry Smith 
7998bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7999bb7acecfSBarry Smith 
8000db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8001a6e0b375SMatthew G. Knepley @*/
8002d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8003d71ae5a4SJacob Faibussowitsch {
80044d6f44ffSToby Isaac   PetscFunctionBegin;
80054d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8006064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
80079566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
80084d6f44ffSToby Isaac   PetscFunctionReturn(0);
80094d6f44ffSToby Isaac }
80104d6f44ffSToby Isaac 
8011a6e0b375SMatthew G. Knepley /*@C
8012bb7acecfSBarry 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.
8013a6e0b375SMatthew G. Knepley 
8014bb7acecfSBarry Smith   Collective on dm
8015a6e0b375SMatthew G. Knepley 
8016a6e0b375SMatthew G. Knepley   Input Parameters:
8017bb7acecfSBarry Smith + dm      - The `DM`
8018a6e0b375SMatthew G. Knepley . time    - The time
8019bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8020a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8021bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
8022a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8023a6e0b375SMatthew G. Knepley 
8024a6e0b375SMatthew G. Knepley   Output Parameter:
8025a6e0b375SMatthew G. Knepley . X - vector
8026a6e0b375SMatthew G. Knepley 
8027a6e0b375SMatthew G. Knepley    Calling sequence of func:
802877b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8029a6e0b375SMatthew G. Knepley 
8030a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8031a6e0b375SMatthew G. Knepley .  x   - The coordinates
803277b739a6SMatthew Knepley .  Nc  - The number of components
8033a6e0b375SMatthew G. Knepley .  u   - The output field values
8034a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8035a6e0b375SMatthew G. Knepley 
8036a6e0b375SMatthew G. Knepley   Level: developer
8037a6e0b375SMatthew G. Knepley 
8038bb7acecfSBarry Smith   Developer Notes:
8039bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8040bb7acecfSBarry Smith 
8041bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8042bb7acecfSBarry Smith 
8043db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
8044a6e0b375SMatthew G. Knepley @*/
8045d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
8046d71ae5a4SJacob Faibussowitsch {
80472c53366bSMatthew G. Knepley   Vec localX;
80482c53366bSMatthew G. Knepley 
80492c53366bSMatthew G. Knepley   PetscFunctionBegin;
80502c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
80519566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
8052f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
80539566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80549566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80559566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80569566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80572c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
80582c53366bSMatthew G. Knepley }
80592c53366bSMatthew G. Knepley 
8060a6e0b375SMatthew G. Knepley /*@C
8061bb7acecfSBarry 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.
8062a6e0b375SMatthew G. Knepley 
8063a6e0b375SMatthew G. Knepley   Not collective
8064a6e0b375SMatthew G. Knepley 
8065a6e0b375SMatthew G. Knepley   Input Parameters:
8066bb7acecfSBarry Smith + dm      - The `DM`
8067a6e0b375SMatthew G. Knepley . time    - The time
8068bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8069a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8070a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8071a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8072a6e0b375SMatthew G. Knepley 
8073a6e0b375SMatthew G. Knepley   Output Parameter:
8074a6e0b375SMatthew G. Knepley . localX - vector
8075a6e0b375SMatthew G. Knepley 
8076a6e0b375SMatthew G. Knepley    Calling sequence of func:
807777b739a6SMatthew Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8078a6e0b375SMatthew G. Knepley 
8079a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8080a6e0b375SMatthew G. Knepley .  x   - The coordinates
808177b739a6SMatthew Knepley .  Nc  - The number of components
8082a6e0b375SMatthew G. Knepley .  u   - The output field values
8083a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8084a6e0b375SMatthew G. Knepley 
8085a6e0b375SMatthew G. Knepley   Level: developer
8086a6e0b375SMatthew G. Knepley 
8087bb7acecfSBarry Smith   Developer Notes:
8088bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8089bb7acecfSBarry Smith 
8090bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8091bb7acecfSBarry Smith 
8092db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8093a6e0b375SMatthew G. Knepley @*/
8094d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
8095d71ae5a4SJacob Faibussowitsch {
80964d6f44ffSToby Isaac   PetscFunctionBegin;
80974d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8098064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80999566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
81004d6f44ffSToby Isaac   PetscFunctionReturn(0);
81014d6f44ffSToby Isaac }
81022716604bSToby Isaac 
8103a6e0b375SMatthew G. Knepley /*@C
8104bb7acecfSBarry 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.
8105a6e0b375SMatthew G. Knepley 
8106a6e0b375SMatthew G. Knepley   Not collective
8107a6e0b375SMatthew G. Knepley 
8108a6e0b375SMatthew G. Knepley   Input Parameters:
8109bb7acecfSBarry Smith + dm      - The `DM`
8110a6e0b375SMatthew G. Knepley . time    - The time
8111eb8f539aSJed Brown . localU  - The input field vector; may be NULL if projection is defined purely by coordinates
8112a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8113a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8114a6e0b375SMatthew G. Knepley 
8115a6e0b375SMatthew G. Knepley   Output Parameter:
8116a6e0b375SMatthew G. Knepley . localX  - The output vector
8117a6e0b375SMatthew G. Knepley 
8118a6e0b375SMatthew G. Knepley    Calling sequence of func:
8119a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8120a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8121a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8122a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8123a6e0b375SMatthew G. Knepley 
8124a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8125a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8126a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8127a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8128a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8129a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8130a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8131a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8132a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8133a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8134a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8135a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8136a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8137a6e0b375SMatthew G. Knepley .  t            - The current time
8138a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8139a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8140a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8141a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8142a6e0b375SMatthew G. Knepley 
8143bb7acecfSBarry Smith   Note:
8144bb7acecfSBarry 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.
8145bb7acecfSBarry 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
8146bb7acecfSBarry 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
8147a6e0b375SMatthew 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.
8148a6e0b375SMatthew G. Knepley 
8149a6e0b375SMatthew G. Knepley   Level: intermediate
8150a6e0b375SMatthew G. Knepley 
8151bb7acecfSBarry Smith   Developer Notes:
8152bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8153bb7acecfSBarry Smith 
8154bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8155bb7acecfSBarry Smith 
8156db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8157a6e0b375SMatthew G. Knepley @*/
8158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8159d71ae5a4SJacob Faibussowitsch {
81608c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81618c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8162eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81638c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81649566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81658c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
81668c6c5593SMatthew G. Knepley }
81678c6c5593SMatthew G. Knepley 
8168a6e0b375SMatthew G. Knepley /*@C
8169a6e0b375SMatthew 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.
8170a6e0b375SMatthew G. Knepley 
8171a6e0b375SMatthew G. Knepley   Not collective
8172a6e0b375SMatthew G. Knepley 
8173a6e0b375SMatthew G. Knepley   Input Parameters:
8174bb7acecfSBarry Smith + dm      - The `DM`
8175a6e0b375SMatthew G. Knepley . time    - The time
8176bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8177a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8178a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8179bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8180a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8181a6e0b375SMatthew G. Knepley . localU  - The input field vector
8182a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8183a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8184a6e0b375SMatthew G. Knepley 
8185a6e0b375SMatthew G. Knepley   Output Parameter:
8186a6e0b375SMatthew G. Knepley . localX  - The output vector
8187a6e0b375SMatthew G. Knepley 
8188a6e0b375SMatthew G. Knepley    Calling sequence of func:
8189a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8190a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8191a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8192a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8193a6e0b375SMatthew G. Knepley 
8194a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8195a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8196a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8197a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8198a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8199a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8200a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8201a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8202a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8203a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8204a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8205a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8206a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8207a6e0b375SMatthew G. Knepley .  t            - The current time
8208a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8209a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8210a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8211a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8212a6e0b375SMatthew G. Knepley 
8213bb7acecfSBarry Smith   Note:
8214bb7acecfSBarry 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.
8215bb7acecfSBarry 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
8216bb7acecfSBarry 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
8217a6e0b375SMatthew 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.
8218a6e0b375SMatthew G. Knepley 
8219a6e0b375SMatthew G. Knepley   Level: intermediate
8220a6e0b375SMatthew G. Knepley 
8221bb7acecfSBarry Smith   Developer Notes:
8222bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8223bb7acecfSBarry Smith 
8224bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8225bb7acecfSBarry Smith 
8226d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8227a6e0b375SMatthew G. Knepley @*/
8228d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8229d71ae5a4SJacob Faibussowitsch {
82308c6c5593SMatthew G. Knepley   PetscFunctionBegin;
82318c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8232064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8233064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
82349566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
82358c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
82368c6c5593SMatthew G. Knepley }
82378c6c5593SMatthew G. Knepley 
82382716604bSToby Isaac /*@C
8239d29d7c6eSMatthew 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.
8240d29d7c6eSMatthew G. Knepley 
8241d29d7c6eSMatthew G. Knepley   Not collective
8242d29d7c6eSMatthew G. Knepley 
8243d29d7c6eSMatthew G. Knepley   Input Parameters:
8244bb7acecfSBarry Smith + dm      - The `DM`
8245d29d7c6eSMatthew G. Knepley . time    - The time
8246bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8247d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8248d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8249bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8250d29d7c6eSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8251d29d7c6eSMatthew G. Knepley . U       - The input field vector
8252d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8253d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8254d29d7c6eSMatthew G. Knepley 
8255d29d7c6eSMatthew G. Knepley   Output Parameter:
8256d29d7c6eSMatthew G. Knepley . X       - The output vector
8257d29d7c6eSMatthew G. Knepley 
8258d29d7c6eSMatthew G. Knepley    Calling sequence of func:
8259d29d7c6eSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8260d29d7c6eSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8261d29d7c6eSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8262d29d7c6eSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8263d29d7c6eSMatthew G. Knepley 
8264d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8265d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8266d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8267d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8268d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8269d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8270d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8271d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8272d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8273d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8274d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8275d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8276d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8277d29d7c6eSMatthew G. Knepley .  t            - The current time
8278d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8279d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8280d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8281d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8282d29d7c6eSMatthew G. Knepley 
8283bb7acecfSBarry Smith   Note:
8284bb7acecfSBarry 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.
8285bb7acecfSBarry 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
8286bb7acecfSBarry 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
8287d29d7c6eSMatthew 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.
8288d29d7c6eSMatthew G. Knepley 
8289d29d7c6eSMatthew G. Knepley   Level: intermediate
8290d29d7c6eSMatthew G. Knepley 
8291bb7acecfSBarry Smith   Developer Notes:
8292bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8293bb7acecfSBarry Smith 
8294bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8295bb7acecfSBarry Smith 
8296d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8297d29d7c6eSMatthew G. Knepley @*/
8298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFieldLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec U, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec X)
8299d71ae5a4SJacob Faibussowitsch {
8300d29d7c6eSMatthew G. Knepley   DM  dmIn;
8301d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8302d29d7c6eSMatthew G. Knepley 
8303d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8304d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8305d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8306d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8307d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8308f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
830972fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
831072fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8311d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8312d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8313d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8314d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8315d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
8316d29d7c6eSMatthew G. Knepley   PetscFunctionReturn(0);
8317d29d7c6eSMatthew G. Knepley }
8318d29d7c6eSMatthew G. Knepley 
8319d29d7c6eSMatthew G. Knepley /*@C
8320ece3a9fcSMatthew 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.
8321ece3a9fcSMatthew G. Knepley 
8322ece3a9fcSMatthew G. Knepley   Not collective
8323ece3a9fcSMatthew G. Knepley 
8324ece3a9fcSMatthew G. Knepley   Input Parameters:
8325bb7acecfSBarry Smith + dm      - The `DM`
8326ece3a9fcSMatthew G. Knepley . time    - The time
8327bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8328ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8329ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8330bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8331ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8332ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8333ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8334ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8335ece3a9fcSMatthew G. Knepley 
8336ece3a9fcSMatthew G. Knepley   Output Parameter:
8337ece3a9fcSMatthew G. Knepley . localX  - The output vector
8338ece3a9fcSMatthew G. Knepley 
8339ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8340ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8341ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8342ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8343ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8344ece3a9fcSMatthew G. Knepley 
8345ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8346ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8347ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8348ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8349ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8350ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8351ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8352ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8353ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8354ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8355ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8356ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8357ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8358ece3a9fcSMatthew G. Knepley .  t            - The current time
8359ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8360ece3a9fcSMatthew G. Knepley .  n            - The face normal
8361ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8362ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8363ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8364ece3a9fcSMatthew G. Knepley 
8365ece3a9fcSMatthew G. Knepley   Note:
8366bb7acecfSBarry 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.
8367bb7acecfSBarry 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
8368bb7acecfSBarry 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
8369ece3a9fcSMatthew 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.
8370ece3a9fcSMatthew G. Knepley 
8371ece3a9fcSMatthew G. Knepley   Level: intermediate
8372ece3a9fcSMatthew G. Knepley 
8373bb7acecfSBarry Smith   Developer Notes:
8374bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8375bb7acecfSBarry Smith 
8376bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8377bb7acecfSBarry Smith 
8378db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8379ece3a9fcSMatthew G. Knepley @*/
8380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU, void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode mode, Vec localX)
8381d71ae5a4SJacob Faibussowitsch {
8382ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8383ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8384064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8385064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83869566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8387ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8388ece3a9fcSMatthew G. Knepley }
8389ece3a9fcSMatthew G. Knepley 
8390ece3a9fcSMatthew G. Knepley /*@C
83912716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83922716604bSToby Isaac 
8393bb7acecfSBarry Smith   Collective on dm
8394bb7acecfSBarry Smith 
83952716604bSToby Isaac   Input Parameters:
8396bb7acecfSBarry Smith + dm    - The `DM`
83970709b2feSToby Isaac . time  - The time
83982716604bSToby Isaac . funcs - The functions to evaluate for each field component
83992716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8400574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84012716604bSToby Isaac 
84022716604bSToby Isaac   Output Parameter:
84032716604bSToby Isaac . diff - The diff ||u - u_h||_2
84042716604bSToby Isaac 
84052716604bSToby Isaac   Level: developer
84062716604bSToby Isaac 
8407bb7acecfSBarry Smith   Developer Notes:
8408bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8409bb7acecfSBarry Smith 
8410bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8411bb7acecfSBarry Smith 
8412db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84132716604bSToby Isaac @*/
8414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8415d71ae5a4SJacob Faibussowitsch {
84162716604bSToby Isaac   PetscFunctionBegin;
84172716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8418b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84199566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
84202716604bSToby Isaac   PetscFunctionReturn(0);
84212716604bSToby Isaac }
8422b698f381SToby Isaac 
8423b698f381SToby Isaac /*@C
8424b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8425b698f381SToby Isaac 
8426d083f849SBarry Smith   Collective on dm
8427d083f849SBarry Smith 
8428b698f381SToby Isaac   Input Parameters:
8429bb7acecfSBarry Smith + dm    - The `DM`
8430b698f381SToby Isaac , time  - The time
8431b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8432b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8433574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8434b698f381SToby Isaac - n     - The vector to project along
8435b698f381SToby Isaac 
8436b698f381SToby Isaac   Output Parameter:
8437b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8438b698f381SToby Isaac 
8439b698f381SToby Isaac   Level: developer
8440b698f381SToby Isaac 
8441bb7acecfSBarry Smith   Developer Notes:
8442bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8443bb7acecfSBarry Smith 
8444bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8445bb7acecfSBarry Smith 
8446bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8447b698f381SToby Isaac @*/
8448d71ae5a4SJacob 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)
8449d71ae5a4SJacob Faibussowitsch {
8450b698f381SToby Isaac   PetscFunctionBegin;
8451b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8452b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84539566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
8454b698f381SToby Isaac   PetscFunctionReturn(0);
8455b698f381SToby Isaac }
8456b698f381SToby Isaac 
84572a16baeaSToby Isaac /*@C
84582a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84592a16baeaSToby Isaac 
8460d083f849SBarry Smith   Collective on dm
8461d083f849SBarry Smith 
84622a16baeaSToby Isaac   Input Parameters:
8463bb7acecfSBarry Smith + dm    - The `DM`
84642a16baeaSToby Isaac . time  - The time
84652a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84662a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8467574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84682a16baeaSToby Isaac 
84692a16baeaSToby Isaac   Output Parameter:
84702a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84712a16baeaSToby Isaac 
84722a16baeaSToby Isaac   Level: developer
84732a16baeaSToby Isaac 
8474bb7acecfSBarry Smith   Developer Notes:
8475bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8476bb7acecfSBarry Smith 
8477bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8478bb7acecfSBarry Smith 
8479db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84802a16baeaSToby Isaac @*/
8481d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8482d71ae5a4SJacob Faibussowitsch {
84832a16baeaSToby Isaac   PetscFunctionBegin;
84842a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84852a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84869566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84872a16baeaSToby Isaac   PetscFunctionReturn(0);
84882a16baeaSToby Isaac }
84892a16baeaSToby Isaac 
8490df0b854cSToby Isaac /*@C
8491bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8492502a2867SDave May 
8493502a2867SDave May  Not Collective
8494502a2867SDave May 
8495502a2867SDave May  Input Parameter:
8496bb7acecfSBarry Smith .  dm    - The `DM`
8497502a2867SDave May 
84980a19bb7dSprj-  Output Parameters:
84990a19bb7dSprj- +  nranks - the number of neighbours
85000a19bb7dSprj- -  ranks - the neighbors ranks
8501502a2867SDave May 
8502bb7acecfSBarry Smith  Note:
8503bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8504502a2867SDave May 
8505502a2867SDave May  Level: beginner
8506502a2867SDave May 
8507db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8508502a2867SDave May @*/
8509d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8510d71ae5a4SJacob Faibussowitsch {
8511502a2867SDave May   PetscFunctionBegin;
8512502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
85139566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
8514502a2867SDave May   PetscFunctionReturn(0);
8515502a2867SDave May }
8516502a2867SDave May 
8517531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8518531c7667SBarry Smith 
8519531c7667SBarry Smith /*
8520531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8521531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8522531c7667SBarry Smith */
8523d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8524d71ae5a4SJacob Faibussowitsch {
8525531c7667SBarry Smith   PetscFunctionBegin;
8526531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8527531c7667SBarry Smith     Vec x1local;
8528531c7667SBarry Smith     DM  dm;
85299566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85307a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
85319566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
85329566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
85339566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8534531c7667SBarry Smith     x1 = x1local;
8535531c7667SBarry Smith   }
85369566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8537531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8538531c7667SBarry Smith     DM dm;
85399566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
85409566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8541531c7667SBarry Smith   }
8542531c7667SBarry Smith   PetscFunctionReturn(0);
8543531c7667SBarry Smith }
8544531c7667SBarry Smith 
8545531c7667SBarry Smith /*@
8546bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8547531c7667SBarry Smith 
8548531c7667SBarry Smith     Input Parameter:
8549bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8550531c7667SBarry Smith 
8551bb7acecfSBarry Smith     Developer Note:
8552bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8553531c7667SBarry Smith 
85541b266c99SBarry Smith     Level: advanced
85551b266c99SBarry Smith 
8556db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8557531c7667SBarry Smith @*/
8558d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8559d71ae5a4SJacob Faibussowitsch {
8560531c7667SBarry Smith   PetscFunctionBegin;
8561531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8562531c7667SBarry Smith   PetscFunctionReturn(0);
8563531c7667SBarry Smith }
85648320bc6fSPatrick Sanan 
85658320bc6fSPatrick Sanan /*@
8566bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85678320bc6fSPatrick Sanan 
85688320bc6fSPatrick Sanan     Collective
85698320bc6fSPatrick Sanan 
85708320bc6fSPatrick Sanan     Input Parameters:
8571bb7acecfSBarry Smith +    dm1 - the first `DM`
8572bb7acecfSBarry Smith -    dm2 - the second `DM`
85738320bc6fSPatrick Sanan 
85748320bc6fSPatrick Sanan     Output Parameters:
8575bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8576bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85778320bc6fSPatrick Sanan 
85788320bc6fSPatrick Sanan     Notes:
8579bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85803d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85818320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8582bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85833d862458SPatrick Sanan     decomposition, but hold different data.
85848320bc6fSPatrick Sanan 
85858320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8586bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85878320bc6fSPatrick Sanan 
8588bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85898320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85908320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8591bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85928320bc6fSPatrick Sanan 
8593bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8594bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85958320bc6fSPatrick Sanan .vb
85968320bc6fSPatrick Sanan   ...
85979566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85988320bc6fSPatrick Sanan   if (set && compatible)  {
85999566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
86009566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
86019566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
86028320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
86038320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
86048320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
86058320bc6fSPatrick Sanan       }
86068320bc6fSPatrick Sanan     }
86079566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
86089566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
86098320bc6fSPatrick Sanan   } else {
86108320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
86118320bc6fSPatrick Sanan   }
86128320bc6fSPatrick Sanan   ...
86138320bc6fSPatrick Sanan .ve
86148320bc6fSPatrick Sanan 
8615bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
86168320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
86178320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
86188320bc6fSPatrick Sanan     always check the "set" output parameter.
86198320bc6fSPatrick Sanan 
8620bb7acecfSBarry Smith     A `DM` is always compatible with itself.
86218320bc6fSPatrick Sanan 
8622bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
86238320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
86248320bc6fSPatrick Sanan     incompatible.
86258320bc6fSPatrick Sanan 
86268320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8627bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
86288320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
86298320bc6fSPatrick Sanan 
8630bb7acecfSBarry Smith     Developer Note:
8631bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
86323d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8633a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8634bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
86358320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
86363d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8637bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
86388320bc6fSPatrick Sanan 
86398320bc6fSPatrick Sanan     Level: advanced
86408320bc6fSPatrick Sanan 
8641db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
86428320bc6fSPatrick Sanan @*/
8643d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8644d71ae5a4SJacob Faibussowitsch {
86458320bc6fSPatrick Sanan   PetscMPIInt compareResult;
86468320bc6fSPatrick Sanan   DMType      type, type2;
86478320bc6fSPatrick Sanan   PetscBool   sameType;
86488320bc6fSPatrick Sanan 
86498320bc6fSPatrick Sanan   PetscFunctionBegin;
8650a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86518320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86528320bc6fSPatrick Sanan 
86538320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8654a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86558320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86568320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86578320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86588320bc6fSPatrick Sanan   }
86598320bc6fSPatrick Sanan 
86608320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86618320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86628320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86638320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86658320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86668320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86678320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86688320bc6fSPatrick Sanan     PetscFunctionReturn(0);
86698320bc6fSPatrick Sanan   }
86708320bc6fSPatrick Sanan 
86718320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8672a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8673dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
8674b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
86758320bc6fSPatrick Sanan   }
86768320bc6fSPatrick Sanan 
8677a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86788320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86799566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86809566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86819566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86828320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8683dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86848320bc6fSPatrick Sanan   } else {
86858320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86868320bc6fSPatrick Sanan   }
86878320bc6fSPatrick Sanan   PetscFunctionReturn(0);
86888320bc6fSPatrick Sanan }
8689c0f0dcc3SMatthew G. Knepley 
8690c0f0dcc3SMatthew G. Knepley /*@C
8691bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8692c0f0dcc3SMatthew G. Knepley 
8693bb7acecfSBarry Smith   Logically Collective on dm
8694c0f0dcc3SMatthew G. Knepley 
8695c0f0dcc3SMatthew G. Knepley   Input Parameters:
8696bb7acecfSBarry Smith + DM - the `DM`
8697c0f0dcc3SMatthew G. Knepley . f - the monitor function
8698c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8699c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8700c0f0dcc3SMatthew G. Knepley 
8701c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8702bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8703c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8704c0f0dcc3SMatthew G. Knepley 
8705bb7acecfSBarry Smith   Note:
8706c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8707bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8708c0f0dcc3SMatthew G. Knepley   order in which they were set.
8709c0f0dcc3SMatthew G. Knepley 
8710bb7acecfSBarry Smith   Fortran Note:
8711bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8712bb7acecfSBarry Smith 
8713bb7acecfSBarry Smith   Developer Note:
8714bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8715c0f0dcc3SMatthew G. Knepley 
8716c0f0dcc3SMatthew G. Knepley   Level: intermediate
8717c0f0dcc3SMatthew G. Knepley 
8718bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8719c0f0dcc3SMatthew G. Knepley @*/
8720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8721d71ae5a4SJacob Faibussowitsch {
8722c0f0dcc3SMatthew G. Knepley   PetscInt m;
8723c0f0dcc3SMatthew G. Knepley 
8724c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8725c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8726c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8727c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8728c0f0dcc3SMatthew G. Knepley 
87299566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
8730c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8731c0f0dcc3SMatthew G. Knepley   }
87327a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8733c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8734c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8735c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
8736c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8737c0f0dcc3SMatthew G. Knepley }
8738c0f0dcc3SMatthew G. Knepley 
8739c0f0dcc3SMatthew G. Knepley /*@
8740bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8741c0f0dcc3SMatthew G. Knepley 
8742bb7acecfSBarry Smith   Logically Collective on dm
8743c0f0dcc3SMatthew G. Knepley 
8744c0f0dcc3SMatthew G. Knepley   Input Parameter:
8745c0f0dcc3SMatthew G. Knepley . dm - the DM
8746c0f0dcc3SMatthew G. Knepley 
8747c0f0dcc3SMatthew G. Knepley   Options Database Key:
8748c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8749bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8750c0f0dcc3SMatthew G. Knepley   set via the options database
8751c0f0dcc3SMatthew G. Knepley 
8752bb7acecfSBarry Smith   Note:
8753bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8754c0f0dcc3SMatthew G. Knepley 
8755c0f0dcc3SMatthew G. Knepley   Level: intermediate
8756c0f0dcc3SMatthew G. Knepley 
8757bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8758c0f0dcc3SMatthew G. Knepley @*/
8759d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8760d71ae5a4SJacob Faibussowitsch {
8761c0f0dcc3SMatthew G. Knepley   PetscInt m;
8762c0f0dcc3SMatthew G. Knepley 
8763c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8764c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8765c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87669566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8767c0f0dcc3SMatthew G. Knepley   }
8768c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
8769c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8770c0f0dcc3SMatthew G. Knepley }
8771c0f0dcc3SMatthew G. Knepley 
8772c0f0dcc3SMatthew G. Knepley /*@C
8773c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8774c0f0dcc3SMatthew G. Knepley 
8775bb7acecfSBarry Smith   Collective on dm
8776c0f0dcc3SMatthew G. Knepley 
8777c0f0dcc3SMatthew G. Knepley   Input Parameters:
8778bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8779c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8780c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8781c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8782c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8783bb7acecfSBarry 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
8784c0f0dcc3SMatthew G. Knepley 
8785c0f0dcc3SMatthew G. Knepley   Output Parameter:
8786c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8787c0f0dcc3SMatthew G. Knepley 
8788c0f0dcc3SMatthew G. Knepley   Level: developer
8789c0f0dcc3SMatthew G. Knepley 
8790db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8791db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8792db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8793db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8794c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8795db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8796bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8797c0f0dcc3SMatthew G. Knepley @*/
8798d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8799d71ae5a4SJacob Faibussowitsch {
8800c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8801c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8802c0f0dcc3SMatthew G. Knepley 
8803c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8804c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
88059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8806c0f0dcc3SMatthew G. Knepley   if (*flg) {
8807c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8808c0f0dcc3SMatthew G. Knepley 
88099566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
88109566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
88119566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
88129566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8813c0f0dcc3SMatthew G. Knepley   }
8814c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8815c0f0dcc3SMatthew G. Knepley }
8816c0f0dcc3SMatthew G. Knepley 
8817c0f0dcc3SMatthew G. Knepley /*@
8818c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8819c0f0dcc3SMatthew G. Knepley 
8820bb7acecfSBarry Smith    Collective on dm
8821c0f0dcc3SMatthew G. Knepley 
8822c0f0dcc3SMatthew G. Knepley    Input Parameters:
8823bb7acecfSBarry Smith .  dm - The `DM`
8824c0f0dcc3SMatthew G. Knepley 
8825c0f0dcc3SMatthew G. Knepley    Level: developer
8826c0f0dcc3SMatthew G. Knepley 
8827bb7acecfSBarry Smith    Question:
8828bb7acecfSBarry Smith    Note should indicate when during the life of the `DM` the monitor is run. It appears to be related to the discretization process seems rather specialized
8829bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8830bb7acecfSBarry Smith 
8831bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8832c0f0dcc3SMatthew G. Knepley @*/
8833d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8834d71ae5a4SJacob Faibussowitsch {
8835c0f0dcc3SMatthew G. Knepley   PetscInt m;
8836c0f0dcc3SMatthew G. Knepley 
8837c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8838c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
8839c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
884048a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
8841c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8842c0f0dcc3SMatthew G. Knepley }
88432e4af2aeSMatthew G. Knepley 
88442e4af2aeSMatthew G. Knepley /*@
8845bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
88462e4af2aeSMatthew G. Knepley 
8847bb7acecfSBarry Smith   Collective on dm
88482e4af2aeSMatthew G. Knepley 
88492e4af2aeSMatthew G. Knepley   Input Parameters:
8850bb7acecfSBarry Smith + dm     - The `DM`
88516b867d5aSJose E. Roman - sol    - The solution vector
88522e4af2aeSMatthew G. Knepley 
88536b867d5aSJose E. Roman   Input/Output Parameter:
88546b867d5aSJose E. Roman . errors - An array of length Nf, the number of fields, or NULL for no output; on output
88556b867d5aSJose E. Roman            contains the error in each field
88566b867d5aSJose E. Roman 
88576b867d5aSJose E. Roman   Output Parameter:
88586b867d5aSJose E. Roman . errorVec - A vector to hold the cellwise error (may be NULL)
88592e4af2aeSMatthew G. Knepley 
8860bb7acecfSBarry Smith   Note:
8861bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88622e4af2aeSMatthew G. Knepley 
88632e4af2aeSMatthew G. Knepley   Level: developer
88642e4af2aeSMatthew G. Knepley 
8865db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88662e4af2aeSMatthew G. Knepley @*/
8867d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8868d71ae5a4SJacob Faibussowitsch {
88692e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88702e4af2aeSMatthew G. Knepley   void    **ctxs;
88712e4af2aeSMatthew G. Knepley   PetscReal time;
88722e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88732e4af2aeSMatthew G. Knepley 
88742e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88759566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88769566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88779566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88782e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88792e4af2aeSMatthew G. Knepley     PetscDS         ds;
88802e4af2aeSMatthew G. Knepley     DMLabel         label;
88812e4af2aeSMatthew G. Knepley     IS              fieldIS;
88822e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88832e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88842e4af2aeSMatthew G. Knepley 
88859566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88869566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88879566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88882e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88892e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88909566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88912e4af2aeSMatthew G. Knepley     }
88929566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88932e4af2aeSMatthew G. Knepley   }
8894ad540459SPierre 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);
88959566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88969566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88972e4af2aeSMatthew G. Knepley   if (errorVec) {
88982e4af2aeSMatthew G. Knepley     DM             edm;
88992e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
89002e4af2aeSMatthew G. Knepley     PetscBool      simplex;
89012e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
89022e4af2aeSMatthew G. Knepley 
89039566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
89049566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
89059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
89072e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
89089566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
89092e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
89102e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
89112e4af2aeSMatthew G. Knepley       PetscQuadrature q;
89122e4af2aeSMatthew G. Knepley       const char     *name;
89132e4af2aeSMatthew G. Knepley 
89149566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
89159566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
89169566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
89179566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
89189566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
89199566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
89209566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
89219566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
89222e4af2aeSMatthew G. Knepley     }
89239566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
89242e4af2aeSMatthew G. Knepley 
89259566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
89269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
89279566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
89289566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
89292e4af2aeSMatthew G. Knepley   }
89309566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
89312e4af2aeSMatthew G. Knepley   PetscFunctionReturn(0);
89322e4af2aeSMatthew G. Knepley }
89339a2a23afSMatthew G. Knepley 
89349a2a23afSMatthew G. Knepley /*@
8935bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
89369a2a23afSMatthew G. Knepley 
89379a2a23afSMatthew G. Knepley   Not collective
89389a2a23afSMatthew G. Knepley 
89399a2a23afSMatthew G. Knepley   Input Parameter:
8940bb7acecfSBarry Smith . dm     - The `DM`
89419a2a23afSMatthew G. Knepley 
89429a2a23afSMatthew G. Knepley   Output Parameter:
8943a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
89449a2a23afSMatthew G. Knepley 
89459a2a23afSMatthew G. Knepley   Level: advanced
89469a2a23afSMatthew G. Knepley 
8947bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
89489a2a23afSMatthew G. Knepley @*/
8949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8950d71ae5a4SJacob Faibussowitsch {
89519a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89529a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89539566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89549a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89559a2a23afSMatthew G. Knepley }
89569a2a23afSMatthew G. Knepley 
89579a2a23afSMatthew G. Knepley /*@
8958ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89599a2a23afSMatthew G. Knepley 
89609a2a23afSMatthew G. Knepley   Not collective
89619a2a23afSMatthew G. Knepley 
89629a2a23afSMatthew G. Knepley   Input Parameters:
8963bb7acecfSBarry Smith + dm     - The `DM`
8964bb7acecfSBarry Smith . label  - The `DMLabel`
8965ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8966ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89679a2a23afSMatthew G. Knepley 
89689a2a23afSMatthew G. Knepley   Output Parameter:
8969bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89709a2a23afSMatthew G. Knepley 
8971bb7acecfSBarry Smith   Note:
8972bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
897304c51a94SMatthew G. Knepley 
89749a2a23afSMatthew G. Knepley   Level: advanced
89759a2a23afSMatthew G. Knepley 
8976bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89779a2a23afSMatthew G. Knepley @*/
8978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8979d71ae5a4SJacob Faibussowitsch {
8980ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
898104c51a94SMatthew G. Knepley   PetscBool       has;
89829a2a23afSMatthew G. Knepley 
89839a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89849a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89859a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89869a2a23afSMatthew G. Knepley   key.label = label;
89879a2a23afSMatthew G. Knepley   key.value = value;
8988ac17215fSMatthew G. Knepley   key.part  = part;
89899566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89909566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89919566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89929a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
89939a2a23afSMatthew G. Knepley }
89949a2a23afSMatthew G. Knepley 
89959a2a23afSMatthew G. Knepley /*@
8996bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89979a2a23afSMatthew G. Knepley 
8998bb7acecfSBarry Smith   Not collective because auxilary vectors are not parallel
89999a2a23afSMatthew G. Knepley 
90009a2a23afSMatthew G. Knepley   Input Parameters:
9001bb7acecfSBarry Smith + dm     - The `DM`
9002bb7acecfSBarry Smith . label  - The `DMLabel`
90039a2a23afSMatthew G. Knepley . value  - The label value indicating the region
9004ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
9005bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
90069a2a23afSMatthew G. Knepley 
90079a2a23afSMatthew G. Knepley   Level: advanced
90089a2a23afSMatthew G. Knepley 
9009bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
90109a2a23afSMatthew G. Knepley @*/
9011d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
9012d71ae5a4SJacob Faibussowitsch {
90139a2a23afSMatthew G. Knepley   Vec             old;
90149a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
90159a2a23afSMatthew G. Knepley 
90169a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90179a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90189a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
90199a2a23afSMatthew G. Knepley   key.label = label;
90209a2a23afSMatthew G. Knepley   key.value = value;
9021ac17215fSMatthew G. Knepley   key.part  = part;
90229566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
90239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
90249566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
90259566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
90269566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
90279a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90289a2a23afSMatthew G. Knepley }
90299a2a23afSMatthew G. Knepley 
90309a2a23afSMatthew G. Knepley /*@C
9031bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
90329a2a23afSMatthew G. Knepley 
90339a2a23afSMatthew G. Knepley   Not collective
90349a2a23afSMatthew G. Knepley 
90359a2a23afSMatthew G. Knepley   Input Parameter:
9036bb7acecfSBarry Smith . dm      - The `DM`
90379a2a23afSMatthew G. Knepley 
90389a2a23afSMatthew G. Knepley   Output Parameters:
9039bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
9040bb7acecfSBarry Smith . values  - The label values for each `Vec`
9041bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
90429a2a23afSMatthew G. Knepley 
9043bb7acecfSBarry Smith   Note:
9044bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
90459a2a23afSMatthew G. Knepley 
90469a2a23afSMatthew G. Knepley   Level: advanced
90479a2a23afSMatthew G. Knepley 
9048bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
90499a2a23afSMatthew G. Knepley @*/
9050d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9051d71ae5a4SJacob Faibussowitsch {
90529a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90539a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90549a2a23afSMatthew G. Knepley 
90559a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90569a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90579a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9058dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9059dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90609566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90629566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90639371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90649371c9d4SSatish Balay     labels[i] = keys[i].label;
90659371c9d4SSatish Balay     values[i] = keys[i].value;
90669371c9d4SSatish Balay     parts[i]  = keys[i].part;
90679371c9d4SSatish Balay   }
90689566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90699a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90709a2a23afSMatthew G. Knepley }
90719a2a23afSMatthew G. Knepley 
90729a2a23afSMatthew G. Knepley /*@
9073bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90749a2a23afSMatthew G. Knepley 
90759a2a23afSMatthew G. Knepley   Not collective
90769a2a23afSMatthew G. Knepley 
90779a2a23afSMatthew G. Knepley   Input Parameter:
9078bb7acecfSBarry Smith . dm    - The `DM`
90799a2a23afSMatthew G. Knepley 
90809a2a23afSMatthew G. Knepley   Output Parameter:
9081bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90829a2a23afSMatthew G. Knepley 
90839a2a23afSMatthew G. Knepley   Level: advanced
90849a2a23afSMatthew G. Knepley 
9085bb7acecfSBarry Smith   Note:
9086bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9087bb7acecfSBarry Smith 
9088db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90899a2a23afSMatthew G. Knepley @*/
9090d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9091d71ae5a4SJacob Faibussowitsch {
90929a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90939a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90949566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90959566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90969a2a23afSMatthew G. Knepley   PetscFunctionReturn(0);
90979a2a23afSMatthew G. Knepley }
9098b5a892a1SMatthew G. Knepley 
9099b5a892a1SMatthew G. Knepley /*@C
9100bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9101b5a892a1SMatthew G. Knepley 
9102b5a892a1SMatthew G. Knepley   Not collective
9103b5a892a1SMatthew G. Knepley 
9104b5a892a1SMatthew G. Knepley   Input Parameters:
9105bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9106b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9107b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9108b5a892a1SMatthew G. Knepley 
9109b5a892a1SMatthew G. Knepley   Output Parameters:
9110bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9111b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9112b5a892a1SMatthew G. Knepley 
9113b5a892a1SMatthew G. Knepley   Level: advanced
9114b5a892a1SMatthew G. Knepley 
9115bb7acecfSBarry Smith   Note:
9116bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9117bb7acecfSBarry Smith 
9118bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9119bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9120bb7acecfSBarry Smith 
9121bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9122bb7acecfSBarry Smith 
9123bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9124b5a892a1SMatthew G. Knepley @*/
9125d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9126d71ae5a4SJacob Faibussowitsch {
9127b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9128b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9129b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9130b5a892a1SMatthew G. Knepley 
9131b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91329371c9d4SSatish Balay   if (!nO) {
91339371c9d4SSatish Balay     *ornt  = 0;
91349371c9d4SSatish Balay     *found = PETSC_TRUE;
91359371c9d4SSatish Balay     PetscFunctionReturn(0);
91369371c9d4SSatish Balay   }
9137b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9138b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9139b5a892a1SMatthew G. Knepley 
91409371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91419371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
91429371c9d4SSatish Balay     if (c == cS) {
91439371c9d4SSatish Balay       *ornt = o;
91449371c9d4SSatish Balay       break;
91459371c9d4SSatish Balay     }
9146b5a892a1SMatthew G. Knepley   }
9147b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9148b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9149b5a892a1SMatthew G. Knepley }
9150b5a892a1SMatthew G. Knepley 
9151b5a892a1SMatthew G. Knepley /*@C
9152bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9153b5a892a1SMatthew G. Knepley 
9154b5a892a1SMatthew G. Knepley   Not collective
9155b5a892a1SMatthew G. Knepley 
9156b5a892a1SMatthew G. Knepley   Input Parameters:
9157bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9158b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9159b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9160b5a892a1SMatthew G. Knepley 
9161b5a892a1SMatthew G. Knepley   Output Parameters:
9162bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9163b5a892a1SMatthew G. Knepley 
9164b5a892a1SMatthew G. Knepley   Level: advanced
9165b5a892a1SMatthew G. Knepley 
9166bb7acecfSBarry Smith   Note:
9167bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9168bb7acecfSBarry Smith 
9169bb7acecfSBarry Smith   Developer Note:
9170bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9171bb7acecfSBarry Smith 
9172bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9173b5a892a1SMatthew G. Knepley @*/
9174d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9175d71ae5a4SJacob Faibussowitsch {
9176b5a892a1SMatthew G. Knepley   PetscBool found;
9177b5a892a1SMatthew G. Knepley 
9178b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91799566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91807a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9181b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9182b5a892a1SMatthew G. Knepley }
9183b5a892a1SMatthew G. Knepley 
9184b5a892a1SMatthew G. Knepley /*@C
9185bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9186b5a892a1SMatthew G. Knepley 
9187b5a892a1SMatthew G. Knepley   Not collective
9188b5a892a1SMatthew G. Knepley 
9189b5a892a1SMatthew G. Knepley   Input Parameters:
9190bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9191b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9192b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9193b5a892a1SMatthew G. Knepley 
9194b5a892a1SMatthew G. Knepley   Output Parameters:
9195bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9196b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9197b5a892a1SMatthew G. Knepley 
9198b5a892a1SMatthew G. Knepley   Level: advanced
9199b5a892a1SMatthew G. Knepley 
9200bb7acecfSBarry Smith   Note:
9201bb7acecfSBarry Smith   An arrangement is a vertex order
9202bb7acecfSBarry Smith 
9203bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9204bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9205bb7acecfSBarry Smith 
9206bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9207bb7acecfSBarry Smith 
9208bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9209b5a892a1SMatthew G. Knepley @*/
9210d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9211d71ae5a4SJacob Faibussowitsch {
9212b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9213b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9214b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9215b5a892a1SMatthew G. Knepley 
9216b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92179371c9d4SSatish Balay   if (!nO) {
92189371c9d4SSatish Balay     *ornt  = 0;
92199371c9d4SSatish Balay     *found = PETSC_TRUE;
92209371c9d4SSatish Balay     PetscFunctionReturn(0);
92219371c9d4SSatish Balay   }
9222b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9223b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9224b5a892a1SMatthew G. Knepley 
92259371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
92269371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
92279371c9d4SSatish Balay     if (c == cS) {
92289371c9d4SSatish Balay       *ornt = o;
92299371c9d4SSatish Balay       break;
92309371c9d4SSatish Balay     }
9231b5a892a1SMatthew G. Knepley   }
9232b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
9233b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9234b5a892a1SMatthew G. Knepley }
9235b5a892a1SMatthew G. Knepley 
9236b5a892a1SMatthew G. Knepley /*@C
9237bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9238b5a892a1SMatthew G. Knepley 
9239b5a892a1SMatthew G. Knepley   Not collective
9240b5a892a1SMatthew G. Knepley 
9241b5a892a1SMatthew G. Knepley   Input Parameters:
9242bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9243b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9244b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9245b5a892a1SMatthew G. Knepley 
9246b5a892a1SMatthew G. Knepley   Output Parameters:
9247bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9248b5a892a1SMatthew G. Knepley 
9249b5a892a1SMatthew G. Knepley   Level: advanced
9250b5a892a1SMatthew G. Knepley 
9251bb7acecfSBarry Smith   Note:
9252bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9253bb7acecfSBarry Smith 
9254bb7acecfSBarry Smith   Developer Note:
9255bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9256bb7acecfSBarry Smith 
9257bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9258b5a892a1SMatthew G. Knepley @*/
9259d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9260d71ae5a4SJacob Faibussowitsch {
9261b5a892a1SMatthew G. Knepley   PetscBool found;
9262b5a892a1SMatthew G. Knepley 
9263b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92649566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92657a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
9266b5a892a1SMatthew G. Knepley   PetscFunctionReturn(0);
9267b5a892a1SMatthew G. Knepley }
9268012bc364SMatthew G. Knepley 
9269012bc364SMatthew G. Knepley /*@C
9270012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9271012bc364SMatthew G. Knepley 
9272012bc364SMatthew G. Knepley   Not collective
9273012bc364SMatthew G. Knepley 
9274012bc364SMatthew G. Knepley   Input Parameters:
9275bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9276012bc364SMatthew G. Knepley - point - Coordinates of the point
9277012bc364SMatthew G. Knepley 
9278012bc364SMatthew G. Knepley   Output Parameters:
9279012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9280012bc364SMatthew G. Knepley 
9281012bc364SMatthew G. Knepley   Level: advanced
9282012bc364SMatthew G. Knepley 
9283bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9284012bc364SMatthew G. Knepley @*/
9285d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9286d71ae5a4SJacob Faibussowitsch {
9287012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9288012bc364SMatthew G. Knepley   PetscInt  d;
9289012bc364SMatthew G. Knepley 
9290012bc364SMatthew G. Knepley   PetscFunctionBegin;
9291012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9292012bc364SMatthew G. Knepley   switch (ct) {
9293012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9294012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9295012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92969371c9d4SSatish Balay       if (point[d] < -1.0) {
92979371c9d4SSatish Balay         *inside = PETSC_FALSE;
92989371c9d4SSatish Balay         break;
92999371c9d4SSatish Balay       }
9300012bc364SMatthew G. Knepley       sum += point[d];
9301012bc364SMatthew G. Knepley     }
93029371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
93039371c9d4SSatish Balay       *inside = PETSC_FALSE;
93049371c9d4SSatish Balay       break;
93059371c9d4SSatish Balay     }
9306012bc364SMatthew G. Knepley     break;
9307012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9308012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9309012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
93109371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
93119371c9d4SSatish Balay         *inside = PETSC_FALSE;
9312012bc364SMatthew G. Knepley         break;
93139371c9d4SSatish Balay       }
93149371c9d4SSatish Balay     break;
9315d71ae5a4SJacob Faibussowitsch   default:
9316d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9317012bc364SMatthew G. Knepley   }
9318012bc364SMatthew G. Knepley   PetscFunctionReturn(0);
9319012bc364SMatthew G. Knepley }
9320