xref: /petsc/src/dm/interface/dm.c (revision 20f4b53cbb5e9bd9ef12b76a8697d60d197cda17)
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};
240e762ea3SJed Brown const char *const DMBlockingTypes[]          = {"TOPOLOGICAL_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;
1063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
2073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20838221697SMatthew G. Knepley }
20938221697SMatthew G. Knepley 
2109a42bb27SBarry Smith /*@C
211bb7acecfSBarry Smith        DMSetVecType - Sets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
2129a42bb27SBarry Smith 
213*20f4b53cSBarry Smith    Logically Collective
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 
219*20f4b53cSBarry Smith    Options Database Key:
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));
2333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2349a42bb27SBarry Smith }
2359a42bb27SBarry Smith 
236c0dedaeaSBarry Smith /*@C
237bb7acecfSBarry Smith        DMGetVecType - Gets the type of vector created with `DMCreateLocalVector()` and `DMCreateGlobalVector()`
238c0dedaeaSBarry Smith 
239*20f4b53cSBarry Smith    Logically Collective
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;
2563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
257c0dedaeaSBarry Smith }
258c0dedaeaSBarry Smith 
2595f1ad066SMatthew G Knepley /*@
260bb7acecfSBarry Smith   VecGetDM - Gets the `DM` defining the data layout of the vector
2615f1ad066SMatthew G Knepley 
262*20f4b53cSBarry Smith   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));
2833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
289*20f4b53cSBarry Smith   Not Collective
2905f1ad066SMatthew G Knepley 
2915f1ad066SMatthew G Knepley   Input Parameters:
292bb7acecfSBarry Smith + v - The `Vec`
293bb7acecfSBarry Smith - dm - The `DM`
2945f1ad066SMatthew G Knepley 
295*20f4b53cSBarry Smith   Level: developer
296*20f4b53cSBarry Smith 
297bb7acecfSBarry Smith   Note:
298bb7acecfSBarry Smith   This is rarely used, generally one uses `DMGetLocalVector()` or  `DMGetGlobalVector()` to create a vector associated with a given `DM`
299d9805387SMatthew G. Knepley 
300bb7acecfSBarry 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.
301bb7acecfSBarry Smith 
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));
3103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
316*20f4b53cSBarry Smith    Logically Collective
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Input Parameters:
319bb7acecfSBarry Smith +  dm - the `DM` context
3208f1509bcSBarry Smith -  ctype - the matrix type
3218f1509bcSBarry Smith 
322*20f4b53cSBarry Smith    Options Database Key:
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;
3353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
341*20f4b53cSBarry Smith    Logically Collective
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 
349*20f4b53cSBarry Smith    Options Database Key:
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;
3623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3638f1509bcSBarry Smith }
3648f1509bcSBarry Smith 
3658f1509bcSBarry Smith /*@C
366bb7acecfSBarry Smith        DMSetMatType - Sets the type of matrix created with `DMCreateMatrix()`
3678f1509bcSBarry Smith 
368*20f4b53cSBarry Smith    Logically Collective
3698f1509bcSBarry Smith 
3708f1509bcSBarry Smith    Input Parameters:
371bb7acecfSBarry Smith +  dm - the `DM` context
372bb7acecfSBarry Smith -  ctype - the matrix type, for example `MATMPIAIJ`
373521d9a4cSLisandro Dalcin 
374*20f4b53cSBarry Smith    Options Database Key:
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));
3873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
388521d9a4cSLisandro Dalcin }
389521d9a4cSLisandro Dalcin 
390c0dedaeaSBarry Smith /*@C
391*20f4b53cSBarry Smith        DMGetMatType - Gets the type of matrix that would be created with `DMCreateMatrix()`
392c0dedaeaSBarry Smith 
393*20f4b53cSBarry Smith    Logically Collective
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;
4103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
411c0dedaeaSBarry Smith }
412c0dedaeaSBarry Smith 
413c688c046SMatthew G Knepley /*@
414bb7acecfSBarry Smith   MatGetDM - Gets the `DM` defining the data layout of the matrix
415c688c046SMatthew G Knepley 
416*20f4b53cSBarry Smith   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));
4403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
446*20f4b53cSBarry Smith   Not Collective
447c688c046SMatthew G Knepley 
448c688c046SMatthew G Knepley   Input Parameters:
449*20f4b53cSBarry Smith + A - The `Mat`
450*20f4b53cSBarry Smith - 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));
4693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
475*20f4b53cSBarry Smith    Logically Collective
4769a42bb27SBarry Smith 
477d8d19677SJose E. Roman    Input Parameters:
478bb7acecfSBarry Smith +  da - the `DM` context
479bb7acecfSBarry Smith -  prefix - the prefix to prepend
4809a42bb27SBarry Smith 
481*20f4b53cSBarry Smith    Level: advanced
482*20f4b53cSBarry Smith 
483*20f4b53cSBarry Smith    Note:
4849a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4859a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4869a42bb27SBarry Smith 
487*20f4b53cSBarry Smith .seealso: `DM`, `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));
4963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4979a42bb27SBarry Smith }
4989a42bb27SBarry Smith 
49931697293SDave May /*@C
500da81f932SPierre Jolivet    DMAppendOptionsPrefix - Appends an additional string to an already existing prefix used for searching for
501bb7acecfSBarry Smith    `DM` options in the options database.
50231697293SDave May 
503*20f4b53cSBarry Smith    Logically Collective
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 
509*20f4b53cSBarry Smith    Level: advanced
510*20f4b53cSBarry Smith 
511*20f4b53cSBarry Smith    Note:
512bb7acecfSBarry 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.
51331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
51431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
51531697293SDave May 
516*20f4b53cSBarry Smith .seealso: `DM`, `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));
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
53831697293SDave May    Level: advanced
53931697293SDave May 
540*20f4b53cSBarry Smith    Fortran Note:
541*20f4b53cSBarry Smith    Pass in a string 'prefix' of
542*20f4b53cSBarry Smith    sufficient length to hold the prefix.
543*20f4b53cSBarry Smith 
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));
5513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
5793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
5873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
6063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
6173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6186858538eSMatthew G. Knepley }
6196858538eSMatthew G. Knepley 
6201fb7b255SJunchao Zhang /*@C
621bb7acecfSBarry Smith     DMDestroy - Destroys a `DM`.
62247c6ae99SBarry Smith 
623*20f4b53cSBarry Smith     Collective
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Input Parameter:
626bb7acecfSBarry Smith .   dm - the `DM` object to destroy
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith     Level: developer
62947c6ae99SBarry Smith 
630*20f4b53cSBarry Smith .seealso: `DM`, `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;
6383ba16761SJacob Faibussowitsch   if (!*dm) PetscFunctionReturn(PETSC_SUCCESS);
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;
6463ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6479371c9d4SSatish Balay   }
6483ba16761SJacob Faibussowitsch   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(PETSC_SUCCESS);
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));
8013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80247c6ae99SBarry Smith }
80347c6ae99SBarry Smith 
804d7bf68aeSBarry Smith /*@
805bb7acecfSBarry Smith     DMSetUp - sets up the data structures inside a `DM` object
806d7bf68aeSBarry Smith 
807*20f4b53cSBarry Smith     Collective
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);
8233ba16761SJacob Faibussowitsch   if (dm->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
824dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setup);
8258387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
8263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
827d7bf68aeSBarry Smith }
828d7bf68aeSBarry Smith 
829d7bf68aeSBarry Smith /*@
830bb7acecfSBarry Smith     DMSetFromOptions - sets parameters in a `DM` from the options database
831d7bf68aeSBarry Smith 
832*20f4b53cSBarry Smith     Collective
833d7bf68aeSBarry Smith 
834d7bf68aeSBarry Smith     Input Parameter:
835bb7acecfSBarry Smith .   dm - the `DM` object to set options for
836d7bf68aeSBarry Smith 
837*20f4b53cSBarry Smith     Options Database Keys:
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>
842*20f4b53cSBarry 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`
843*20f4b53cSBarry Smith . -dm_plex_filename <str>           - File containing a mesh
8449318fe57SMatthew G. Knepley . -dm_plex_boundary_filename <str>  - File containing a mesh boundary
845cd7e8a5eSksagiyam . -dm_plex_name <str>               - Name of the mesh in the file
8465dca41c3SJed Brown . -dm_plex_shape <shape>            - The domain shape, such as `BOX`, `SPHERE`, etc.
8479318fe57SMatthew G. Knepley . -dm_plex_cell <ct>                - Cell shape
8489318fe57SMatthew G. Knepley . -dm_plex_reference_cell_domain <bool> - Use a reference cell domain
8499318fe57SMatthew G. Knepley . -dm_plex_dim <dim>                - Set the topological dimension
850bb7acecfSBarry Smith . -dm_plex_simplex <bool>           - `PETSC_TRUE` for simplex elements, `PETSC_FALSE` for tensor elements
851bb7acecfSBarry Smith . -dm_plex_interpolate <bool>       - `PETSC_TRUE` turns on topological interpolation (creating edges and faces)
8529318fe57SMatthew G. Knepley . -dm_plex_scale <sc>               - Scale factor for mesh coordinates
8539318fe57SMatthew G. Knepley . -dm_plex_box_faces <m,n,p>        - Number of faces along each dimension
8549318fe57SMatthew G. Knepley . -dm_plex_box_lower <x,y,z>        - Specify lower-left-bottom coordinates for the box
8559318fe57SMatthew G. Knepley . -dm_plex_box_upper <x,y,z>        - Specify upper-right-top coordinates for the box
856bb7acecfSBarry Smith . -dm_plex_box_bd <bx,by,bz>        - Specify the `DMBoundaryType` for each direction
8579318fe57SMatthew G. Knepley . -dm_plex_sphere_radius <r>        - The sphere radius
8589318fe57SMatthew G. Knepley . -dm_plex_ball_radius <r>          - Radius of the ball
8599318fe57SMatthew G. Knepley . -dm_plex_cylinder_bd <bz>         - Boundary type in the z direction
8609318fe57SMatthew G. Knepley . -dm_plex_cylinder_num_wedges <n>  - Number of wedges around the cylinder
861bdf63967SMatthew G. Knepley . -dm_plex_reorder <order>          - Reorder the mesh using the specified algorithm
8629318fe57SMatthew G. Knepley . -dm_refine_pre <n>                - The number of refinements before distribution
8639318fe57SMatthew G. Knepley . -dm_refine_uniform_pre <bool>     - Flag for uniform refinement before distribution
8649318fe57SMatthew G. Knepley . -dm_refine_volume_limit_pre <v>   - The maximum cell volume after refinement before distribution
8659318fe57SMatthew G. Knepley . -dm_refine <n>                    - The number of refinements after distribution
866bdf63967SMatthew G. Knepley . -dm_extrude <l>                   - Activate extrusion and specify the number of layers to extrude
867d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thickness <t>           - The total thickness of extruded layers
868d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_use_tensor <bool>       - Use tensor cells when extruding
869d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_symmetric <bool>        - Extrude layers symmetrically about the surface
870d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_normal <n0,...,nd>      - Specify the extrusion direction
871d410b0cfSMatthew G. Knepley . -dm_plex_transform_extrude_thicknesses <t0,...,tl> - Specify thickness of each layer
872909dfd52SMatthew G. Knepley . -dm_plex_create_fv_ghost_cells    - Flag to create finite volume ghost cells on the boundary
873909dfd52SMatthew G. Knepley . -dm_plex_fv_ghost_cells_label <name> - Label name for ghost cells boundary
8749318fe57SMatthew G. Knepley . -dm_distribute <bool>             - Flag to redistribute a mesh among processes
8759318fe57SMatthew G. Knepley . -dm_distribute_overlap <n>        - The size of the overlap halo
8769318fe57SMatthew G. Knepley . -dm_plex_adj_cone <bool>          - Set adjacency direction
877*20f4b53cSBarry Smith .  -dm_plex_adj_closure <bool>       - Set adjacency size
878*20f4b53cSBarry Smith .   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - `DMPlexCheckSymmetry()`
879bb7acecfSBarry Smith .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - `DMPlexCheckSkeleton()`
880bb7acecfSBarry 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()`
881bb7acecfSBarry Smith .   -dm_plex_check_geometry        - Check that cells have positive volume - `DMPlexCheckGeometry()`
882bb7acecfSBarry Smith .   -dm_plex_check_pointsf         - Check some necessary conditions for `PointSF` - `DMPlexCheckPointSF()`
883bb7acecfSBarry Smith .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - `DMPlexCheckInterfaceCones()`
884384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
885d7bf68aeSBarry Smith 
88695eb5ee5SVaclav Hapla     Level: intermediate
88795eb5ee5SVaclav Hapla 
888bb7acecfSBarry Smith .seealso: `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
889bb7acecfSBarry Smith          `DMPlexCheckSymmetry()`, `DMPlexCheckSkeleton()`, `DMPlexCheckFaces()`, `DMPlexCheckGeometry()`, `DMPlexCheckPointSF()`, `DMPlexCheckInterfaceCones()`,
890bb7acecfSBarry Smith          `DMSetOptionsPrefix()`, `DM`, `DMType`, `DMPLEX`, `DMDA`
891d7bf68aeSBarry Smith @*/
892d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions(DM dm)
893d71ae5a4SJacob Faibussowitsch {
8947781c08eSBarry Smith   char      typeName[256];
895ca266f36SBarry Smith   PetscBool flg;
896d7bf68aeSBarry Smith 
897d7bf68aeSBarry Smith   PetscFunctionBegin;
898171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
9009566063dSJacob Faibussowitsch   if (dm->sf) PetscCall(PetscSFSetFromOptions(dm->sf));
9019566063dSJacob Faibussowitsch   if (dm->sectionSF) PetscCall(PetscSFSetFromOptions(dm->sectionSF));
902dd4c3f67SMatthew G. Knepley   if (dm->coordinates[0].dm) PetscCall(DMSetFromOptions(dm->coordinates[0].dm));
903d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)dm);
9049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_preallocate_only", "only preallocate matrix, but do not set column indices", "DMSetMatrixPreallocateOnly", dm->prealloc_only, &dm->prealloc_only, NULL));
9059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_vec_type", "Vector type used for created vectors", "DMSetVecType", VecList, dm->vectype, typeName, 256, &flg));
9061baa6e33SBarry Smith   if (flg) PetscCall(DMSetVecType(dm, typeName));
9079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_mat_type", "Matrix type used for created matrices", "DMSetMatType", MatList, dm->mattype ? dm->mattype : typeName, typeName, sizeof(typeName), &flg));
9081baa6e33SBarry Smith   if (flg) PetscCall(DMSetMatType(dm, typeName));
909863027abSJed Brown   PetscCall(PetscOptionsEnum("-dm_blocking_type", "Topological point or field node blocking", "DMSetBlockingType", DMBlockingTypes, (PetscEnum)dm->blocking_type, (PetscEnum *)&dm->blocking_type, NULL));
9109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_is_coloring_type", "Global or local coloring of Jacobian", "DMSetISColoringType", ISColoringTypes, (PetscEnum)dm->coloringtype, (PetscEnum *)&dm->coloringtype, NULL));
9119566063dSJacob 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));
912dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, setfromoptions, PetscOptionsObject);
913f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
914dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)dm, PetscOptionsObject));
915d0609cedSBarry Smith   PetscOptionsEnd();
9163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
917d7bf68aeSBarry Smith }
918d7bf68aeSBarry Smith 
919fc9bc008SSatish Balay /*@C
920bb7acecfSBarry Smith    DMViewFromOptions - View a `DM` in a particular way based on a request in the options database
921fe2efc57SMark 
922*20f4b53cSBarry Smith    Collective
923fe2efc57SMark 
924fe2efc57SMark    Input Parameters:
925bb7acecfSBarry Smith +  dm - the `DM` object
926*20f4b53cSBarry Smith .  obj - optional object that provides the prefix for the options database (if `NULL` then the prefix in obj is used)
927bb7acecfSBarry Smith -  optionname - option string that is used to activate viewing
928fe2efc57SMark 
929fe2efc57SMark    Level: intermediate
930bb7acecfSBarry Smith 
931bb7acecfSBarry Smith    Note:
932bb7acecfSBarry Smith    See `PetscObjectViewFromOptions()` for a list of values that can be provided in the options database to determine how the `DM` is viewed
933bb7acecfSBarry Smith 
934bb7acecfSBarry Smith .seealso: `DM`, `DMView()`, `PetscObjectViewFromOptions()`, `DMCreate()`, `PetscObjectViewFromOptions()`
935fe2efc57SMark @*/
936d71ae5a4SJacob Faibussowitsch PetscErrorCode DMViewFromOptions(DM dm, PetscObject obj, const char name[])
937d71ae5a4SJacob Faibussowitsch {
938fe2efc57SMark   PetscFunctionBegin;
939fe2efc57SMark   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9409566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)dm, obj, name));
9413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
942fe2efc57SMark }
943fe2efc57SMark 
944fe2efc57SMark /*@C
945bb7acecfSBarry 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
946bb7acecfSBarry Smith     save the `DM` in a binary file to be loaded later or create a visualization of the `DM`
94747c6ae99SBarry Smith 
948*20f4b53cSBarry Smith     Collective
94947c6ae99SBarry Smith 
950d8d19677SJose E. Roman     Input Parameters:
951bb7acecfSBarry Smith +   dm - the `DM` object to view
95247c6ae99SBarry Smith -   v - the viewer
95347c6ae99SBarry Smith 
954*20f4b53cSBarry Smith     Level: beginner
955*20f4b53cSBarry Smith 
956cd7e8a5eSksagiyam     Notes:
957bb7acecfSBarry Smith     Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` as the `PetscViewerFormat` one can save multiple `DMPLEX`
958bb7acecfSBarry Smith     meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
959bb7acecfSBarry Smith     before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
960cd7e8a5eSksagiyam 
961bb7acecfSBarry Smith .seealso: `PetscViewer`, `PetscViewerFormat`, `PetscViewerSetFormat`(), `DMDestroy()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMLoad()`, `PetscObjectSetName()`
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith @*/
964d71ae5a4SJacob Faibussowitsch PetscErrorCode DMView(DM dm, PetscViewer v)
965d71ae5a4SJacob Faibussowitsch {
96632c0f0efSBarry Smith   PetscBool         isbinary;
96776a8abe0SBarry Smith   PetscMPIInt       size;
96876a8abe0SBarry Smith   PetscViewerFormat format;
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith   PetscFunctionBegin;
971171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97248a46eb9SPierre Jolivet   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm), &v));
973b1b135c8SBarry Smith   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
97474903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
97574903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
97674903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
97774903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
97874903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
97974903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
98074903a4fSStefano Zampini      in an error here */
98174903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9829566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckWritable(v));
983b1b135c8SBarry Smith 
9849566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
9859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
9863ba16761SJacob Faibussowitsch   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(PETSC_SUCCESS);
9879566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)dm, v));
9889566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERBINARY, &isbinary));
98932c0f0efSBarry Smith   if (isbinary) {
99055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
99132c0f0efSBarry Smith     char     type[256];
99232c0f0efSBarry Smith 
9939566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, &classid, 1, PETSC_INT));
994c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(type, ((PetscObject)dm)->type_name, sizeof(type)));
9959566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(v, type, 256, PETSC_CHAR));
99632c0f0efSBarry Smith   }
997dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, view, v);
9983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
99947c6ae99SBarry Smith }
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith /*@
1002bb7acecfSBarry 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,
1003bb7acecfSBarry Smith     that is it has no ghost locations.
100447c6ae99SBarry Smith 
1005*20f4b53cSBarry Smith     Collective
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Input Parameter:
1008bb7acecfSBarry Smith .   dm - the `DM` object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Output Parameter:
101147c6ae99SBarry Smith .   vec - the global vector
101247c6ae99SBarry Smith 
1013073dac72SJed Brown     Level: beginner
101447c6ae99SBarry Smith 
1015bb7acecfSBarry Smith .seealso: `Vec`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1016bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
101747c6ae99SBarry Smith @*/
1018d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateGlobalVector(DM dm, Vec *vec)
1019d71ae5a4SJacob Faibussowitsch {
102047c6ae99SBarry Smith   PetscFunctionBegin;
1021171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1022b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1023dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createglobalvector, vec);
102476bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1025c6b011d8SStefano Zampini     DM vdm;
1026c6b011d8SStefano Zampini 
10279566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10287a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1029c6b011d8SStefano Zampini   }
10303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
103147c6ae99SBarry Smith }
103247c6ae99SBarry Smith 
103347c6ae99SBarry Smith /*@
1034bb7acecfSBarry Smith     DMCreateLocalVector - Creates a local vector from a `DM` object.
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith     Not Collective
103747c6ae99SBarry Smith 
103847c6ae99SBarry Smith     Input Parameter:
1039bb7acecfSBarry Smith .   dm - the `DM` object
104047c6ae99SBarry Smith 
104147c6ae99SBarry Smith     Output Parameter:
104247c6ae99SBarry Smith .   vec - the local vector
104347c6ae99SBarry Smith 
1044073dac72SJed Brown     Level: beginner
104547c6ae99SBarry Smith 
1046*20f4b53cSBarry Smith     Note:
1047bb7acecfSBarry 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.
1048bb7acecfSBarry Smith 
1049bb7acecfSBarry Smith  .seealso: `Vec`, `DMCreateGlobalVector()`, `DMGetLocalVector()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`
1050bb7acecfSBarry Smith          `DMGlobalToLocalBegin()`, `DMGlobalToLocalEnd()`
105147c6ae99SBarry Smith @*/
1052d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLocalVector(DM dm, Vec *vec)
1053d71ae5a4SJacob Faibussowitsch {
105447c6ae99SBarry Smith   PetscFunctionBegin;
1055171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1056b9d85ea2SLisandro Dalcin   PetscValidPointer(vec, 2);
1057dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createlocalvector, vec);
105876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1059c6b011d8SStefano Zampini     DM vdm;
1060c6b011d8SStefano Zampini 
10619566063dSJacob Faibussowitsch     PetscCall(VecGetDM(*vec, &vdm));
10627a8be351SBarry Smith     PetscCheck(vdm, PETSC_COMM_SELF, PETSC_ERR_LIB, "DM type '%s' did not attach the DM to the vector", ((PetscObject)dm)->type_name);
1063c6b011d8SStefano Zampini   }
10643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106547c6ae99SBarry Smith }
106647c6ae99SBarry Smith 
10671411c6eeSJed Brown /*@
1068bb7acecfSBarry Smith    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a `DM`.
10691411c6eeSJed Brown 
1070*20f4b53cSBarry Smith    Collective
10711411c6eeSJed Brown 
10721411c6eeSJed Brown    Input Parameter:
1073bb7acecfSBarry Smith .  dm - the `DM` that provides the mapping
10741411c6eeSJed Brown 
10751411c6eeSJed Brown    Output Parameter:
10761411c6eeSJed Brown .  ltog - the mapping
10771411c6eeSJed Brown 
1078bb7acecfSBarry Smith    Level: advanced
10791411c6eeSJed Brown 
10801411c6eeSJed Brown    Notes:
1081bb7acecfSBarry Smith    The global to local mapping allows one to set values into the global vector or matrix using `VecSetValuesLocal()` and `MatSetValuesLocal()`
10821411c6eeSJed Brown 
1083bb7acecfSBarry Smith    Vectors obtained with  `DMCreateGlobalVector()` and matrices obtained with `DMCreateMatrix()` already contain the global mapping so you do
1084bb7acecfSBarry Smith    need to use this function with those objects.
1085bb7acecfSBarry Smith 
1086bb7acecfSBarry Smith    This mapping can then be used by `VecSetLocalToGlobalMapping()` or `MatSetLocalToGlobalMapping()`.
1087bb7acecfSBarry Smith 
1088bb7acecfSBarry Smith .seealso: `DMCreateLocalVector()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `VecSetLocalToGlobalMapping()`, `MatSetLocalToGlobalMapping()`,
1089bb7acecfSBarry Smith           `DMCreateMatrix()`
10901411c6eeSJed Brown @*/
1091d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalToGlobalMapping(DM dm, ISLocalToGlobalMapping *ltog)
1092d71ae5a4SJacob Faibussowitsch {
10930be3e97aSMatthew G. Knepley   PetscInt bs = -1, bsLocal[2], bsMinMax[2];
10941411c6eeSJed Brown 
10951411c6eeSJed Brown   PetscFunctionBegin;
10961411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10971411c6eeSJed Brown   PetscValidPointer(ltog, 2);
10981411c6eeSJed Brown   if (!dm->ltogmap) {
109937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
110037d0c07bSMatthew G Knepley 
11019566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
110237d0c07bSMatthew G Knepley     if (section) {
1103a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
110437d0c07bSMatthew G Knepley       PetscInt       *ltog;
1105ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
110637d0c07bSMatthew G Knepley 
11079566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
11089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(section, &pStart, &pEnd));
11099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetStorageSize(section, &n));
11109566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(n, &ltog)); /* We want the local+overlap size */
111137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1112e6befd46SJed Brown         PetscInt bdof, cdof, dof, off, c, cind;
111337d0c07bSMatthew G Knepley 
111437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
11159566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(section, p, &dof));
11169566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(section, p, &cdof));
11179566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintIndices(section, p, &cdofs));
11189566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &off));
11191a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
11201a7dc684SMatthew G. Knepley         bdof = cdof && (dof - cdof) ? 1 : dof;
1121ad540459SPierre Jolivet         if (dof) bs = bs < 0 ? bdof : PetscGCD(bs, bdof);
11225227eafbSStefano Zampini 
1123e6befd46SJed Brown         for (c = 0, cind = 0; c < dof; ++c, ++l) {
11245227eafbSStefano Zampini           if (cind < cdof && c == cdofs[cind]) {
1125e6befd46SJed Brown             ltog[l] = off < 0 ? off - c : -(off + c + 1);
1126e6befd46SJed Brown             cind++;
1127e6befd46SJed Brown           } else {
11285227eafbSStefano Zampini             ltog[l] = (off < 0 ? -(off + 1) : off) + c - cind;
1129e6befd46SJed Brown           }
113037d0c07bSMatthew G Knepley         }
113137d0c07bSMatthew G Knepley       }
1132bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
11339371c9d4SSatish Balay       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs;
11349371c9d4SSatish Balay       bsLocal[1] = bs;
11359566063dSJacob Faibussowitsch       PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)dm), bsLocal, bsMinMax));
11369371c9d4SSatish Balay       if (bsMinMax[0] != bsMinMax[1]) {
11379371c9d4SSatish Balay         bs = 1;
11389371c9d4SSatish Balay       } else {
11399371c9d4SSatish Balay         bs = bsMinMax[0];
11409371c9d4SSatish Balay       }
11417591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
11427591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1143ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1144ca469d19SJed Brown         for (l = 0, k = 0; l < n; l += bs, ++k) {
1145ca469d19SJed Brown           // Integer division of negative values truncates toward zero(!), not toward negative infinity
1146ca469d19SJed Brown           ltog[k] = ltog[l] >= 0 ? ltog[l] / bs : -(-(ltog[l] + 1) / bs + 1);
1147ca469d19SJed Brown         }
1148ccf3bd66SMatthew G. Knepley         n /= bs;
1149ccf3bd66SMatthew G. Knepley       }
11509566063dSJacob Faibussowitsch       PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap));
1151dbbe0bcdSBarry Smith     } else PetscUseTypeMethod(dm, getlocaltoglobalmapping);
115237d0c07bSMatthew G Knepley   }
11531411c6eeSJed Brown   *ltog = dm->ltogmap;
11543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11551411c6eeSJed Brown }
11561411c6eeSJed Brown 
11571411c6eeSJed Brown /*@
1158bb7acecfSBarry Smith    DMGetBlockSize - Gets the inherent block size associated with a `DM`
11591411c6eeSJed Brown 
11601411c6eeSJed Brown    Not Collective
11611411c6eeSJed Brown 
11621411c6eeSJed Brown    Input Parameter:
1163bb7acecfSBarry Smith .  dm - the `DM` with block structure
11641411c6eeSJed Brown 
11651411c6eeSJed Brown    Output Parameter:
11661411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
11671411c6eeSJed Brown 
11681411c6eeSJed Brown    Level: intermediate
11691411c6eeSJed Brown 
1170bb7acecfSBarry Smith    Note:
1171bb7acecfSBarry Smith    This might be the number of degrees of freedom at each grid point for a structured grid.
1172bb7acecfSBarry Smith 
1173bb7acecfSBarry Smith    Complex `DM` that represent multiphysics or staggered grids or mixed-methods do not generally have a single inherent block size, but
1174bb7acecfSBarry Smith    rather different locations in the vectors may have a different block size.
1175bb7acecfSBarry Smith 
1176db781477SPatrick Sanan .seealso: `ISCreateBlock()`, `VecSetBlockSize()`, `MatSetBlockSize()`, `DMGetLocalToGlobalMapping()`
11771411c6eeSJed Brown @*/
1178d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBlockSize(DM dm, PetscInt *bs)
1179d71ae5a4SJacob Faibussowitsch {
11801411c6eeSJed Brown   PetscFunctionBegin;
11811411c6eeSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1182534a8f05SLisandro Dalcin   PetscValidIntPointer(bs, 2);
11837a8be351SBarry Smith   PetscCheck(dm->bs >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM does not have enough information to provide a block size yet");
11841411c6eeSJed Brown   *bs = dm->bs;
11853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11861411c6eeSJed Brown }
11871411c6eeSJed Brown 
118848eeb7c8SBarry Smith /*@C
1189bb7acecfSBarry Smith     DMCreateInterpolation - Gets the interpolation matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1190bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the coarse `DM` to similar vectors on the fine grid `DM`.
119147c6ae99SBarry Smith 
1192*20f4b53cSBarry Smith     Collective
119347c6ae99SBarry Smith 
1194d8d19677SJose E. Roman     Input Parameters:
1195bb7acecfSBarry Smith +   dmc - the `DM` object
1196bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
119747c6ae99SBarry Smith 
1198d8d19677SJose E. Roman     Output Parameters:
119947c6ae99SBarry Smith +  mat - the interpolation
1200bb7acecfSBarry Smith -  vec - the scaling (optional), see `DMCreateInterpolationScale()`
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith     Level: developer
120347c6ae99SBarry Smith 
120495452b02SPatrick Sanan     Notes:
1205bb7acecfSBarry 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
1206bb7acecfSBarry Smith     DMCoarsen(). The coordinates set into the `DMDA` are completely ignored in computing the interpolation.
1207d52bd9f3SBarry Smith 
1208bb7acecfSBarry Smith     For `DMDA` objects you can use this interpolation (more precisely the interpolation from the `DMGetCoordinateDM()`) to interpolate the mesh coordinate
1209bb7acecfSBarry Smith     vectors EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
121085afcc9aSBarry Smith 
1211bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolationScale()`
121247c6ae99SBarry Smith @*/
1213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolation(DM dmc, DM dmf, Mat *mat, Vec *vec)
1214d71ae5a4SJacob Faibussowitsch {
121547c6ae99SBarry Smith   PetscFunctionBegin;
1216a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1217a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
1218c7d20fa0SStefano Zampini   PetscValidPointer(mat, 3);
12199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInterpolation, dmc, dmf, 0, 0));
1220dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createinterpolation, dmf, mat, vec);
12219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInterpolation, dmc, dmf, 0, 0));
12223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
122347c6ae99SBarry Smith }
122447c6ae99SBarry Smith 
12253ad4599aSBarry Smith /*@
1226bb7acecfSBarry 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`.
1227bb7acecfSBarry Smith     xcoarse = diag(L)*R*xfine preserves scale and is thus suitable for state (versus residual) restriction. In other words xcoarse is the coarse
1228bb7acecfSBarry Smith     representation of xfine.
12292ed6491fSPatrick Sanan 
12302ed6491fSPatrick Sanan   Input Parameters:
1231bb7acecfSBarry Smith +      dac - `DM` that defines a coarse mesh
1232bb7acecfSBarry Smith .      daf - `DM` that defines a fine mesh
12332ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
12342ed6491fSPatrick Sanan 
12352ed6491fSPatrick Sanan   Output Parameter:
12362ed6491fSPatrick Sanan .    scale - the scaled vector
12372ed6491fSPatrick Sanan 
1238bb7acecfSBarry Smith   Level: advanced
12392ed6491fSPatrick Sanan 
1240*20f4b53cSBarry Smith   Developer Note:
1241bb7acecfSBarry Smith   If the fine-scale `DMDA` has the -dm_bind_below option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
1242e9c74fd6SRichard Tran Mills   on the restriction/interpolation operator to set the bindingpropagates flag to true.
1243e9c74fd6SRichard Tran Mills 
1244bb7acecfSBarry Smith .seealso: `MatRestrict()`, `MatInterpolate()`, `DMCreateInterpolation()`, DMCreateRestriction()`, `DMCreateGlobalVector()`
12452ed6491fSPatrick Sanan @*/
1246d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInterpolationScale(DM dac, DM daf, Mat mat, Vec *scale)
1247d71ae5a4SJacob Faibussowitsch {
12482ed6491fSPatrick Sanan   Vec         fine;
12492ed6491fSPatrick Sanan   PetscScalar one = 1.0;
12509704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
1251e9c74fd6SRichard Tran Mills   PetscBool bindingpropagates, isbound;
12529704db99SRichard Tran Mills #endif
12532ed6491fSPatrick Sanan 
12542ed6491fSPatrick Sanan   PetscFunctionBegin;
12559566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(daf, &fine));
12569566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(dac, scale));
12579566063dSJacob Faibussowitsch   PetscCall(VecSet(fine, one));
12589704db99SRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
12599704db99SRichard Tran Mills   /* If the 'fine' Vec is bound to the CPU, it makes sense to bind 'mat' as well.
12609704db99SRichard Tran Mills    * Note that we only do this for the CUDA case, right now, but if we add support for MatMultTranspose() via ViennaCL,
12619704db99SRichard Tran Mills    * we'll need to do it for that case, too.*/
12629566063dSJacob Faibussowitsch   PetscCall(VecGetBindingPropagates(fine, &bindingpropagates));
1263e9c74fd6SRichard Tran Mills   if (bindingpropagates) {
12649566063dSJacob Faibussowitsch     PetscCall(MatSetBindingPropagates(mat, PETSC_TRUE));
12659566063dSJacob Faibussowitsch     PetscCall(VecBoundToCPU(fine, &isbound));
12669566063dSJacob Faibussowitsch     PetscCall(MatBindToCPU(mat, isbound));
126783aa49f4SRichard Tran Mills   }
12689704db99SRichard Tran Mills #endif
12699566063dSJacob Faibussowitsch   PetscCall(MatRestrict(mat, fine, *scale));
12709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&fine));
12719566063dSJacob Faibussowitsch   PetscCall(VecReciprocal(*scale));
12723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12732ed6491fSPatrick Sanan }
12742ed6491fSPatrick Sanan 
12752ed6491fSPatrick Sanan /*@
1276bb7acecfSBarry Smith     DMCreateRestriction - Gets restriction matrix between two `DM` objects. The resulting matrix map degrees of freedom in the vector obtained by
1277bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine `DM` to similar vectors on the coarse grid `DM`.
12783ad4599aSBarry Smith 
1279*20f4b53cSBarry Smith     Collective
12803ad4599aSBarry Smith 
1281d8d19677SJose E. Roman     Input Parameters:
1282bb7acecfSBarry Smith +   dmc - the `DM` object
1283bb7acecfSBarry Smith -   dmf - the second, finer `DM` object
12843ad4599aSBarry Smith 
12853ad4599aSBarry Smith     Output Parameter:
12863ad4599aSBarry Smith .  mat - the restriction
12873ad4599aSBarry Smith 
12883ad4599aSBarry Smith     Level: developer
12893ad4599aSBarry Smith 
1290bb7acecfSBarry Smith     Note:
1291bb7acecfSBarry Smith     This only works for `DMSTAG`. For many situations either the transpose of the operator obtained with `DMCreateInterpolation()` or that
1292bb7acecfSBarry Smith     matrix multiplied by the vector obtained with `DMCreateInterpolationScale()` provides the desired object.
12933ad4599aSBarry Smith 
1294bb7acecfSBarry Smith .seealso: `DMRestrict()`, `DMInterpolate()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateInterpolation()`
12953ad4599aSBarry Smith @*/
1296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateRestriction(DM dmc, DM dmf, Mat *mat)
1297d71ae5a4SJacob Faibussowitsch {
12983ad4599aSBarry Smith   PetscFunctionBegin;
1299a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1300a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13015a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateRestriction, dmc, dmf, 0, 0));
1303dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createrestriction, dmf, mat);
13049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateRestriction, dmc, dmf, 0, 0));
13053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13063ad4599aSBarry Smith }
13073ad4599aSBarry Smith 
130847c6ae99SBarry Smith /*@
1309bb7acecfSBarry Smith     DMCreateInjection - Gets injection matrix between two `DM` objects. This is an operator that applied to a vector obtained with
1310bb7acecfSBarry Smith     `DMCreateGlobalVector()` on the fine grid maps the values to a vector on the vector on the coarse `DM` by simply selecting the values
1311bb7acecfSBarry Smith     on the coarse grid points. This compares to the operator obtained by `DMCreateRestriction()` or the transpose of the operator obtained
1312bb7acecfSBarry Smith     by `DMCreateInterpolation()` that uses a "local weighted average" of the values around the coarse grid point as the coarse grid value.
131347c6ae99SBarry Smith 
1314*20f4b53cSBarry Smith     Collective
131547c6ae99SBarry Smith 
1316d8d19677SJose E. Roman     Input Parameters:
1317bb7acecfSBarry Smith +   dac - the `DM` object
1318bb7acecfSBarry Smith -   daf - the second, finer `DM` object
131947c6ae99SBarry Smith 
132047c6ae99SBarry Smith     Output Parameter:
13216dbf9973SLawrence Mitchell .   mat - the injection
132247c6ae99SBarry Smith 
132347c6ae99SBarry Smith     Level: developer
132447c6ae99SBarry Smith 
1325bb7acecfSBarry Smith    Note:
1326bb7acecfSBarry 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
1327bb7acecfSBarry Smith         `DMCoarsen()`. The coordinates set into the `DMDA` are completely ignored in computing the injection.
132885afcc9aSBarry Smith 
1329bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateInterpolation()`,
1330bb7acecfSBarry Smith           `DMCreateRestriction()`, `MatRestrict()`, `MatInterpolate()`
133147c6ae99SBarry Smith @*/
1332d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateInjection(DM dac, DM daf, Mat *mat)
1333d71ae5a4SJacob Faibussowitsch {
133447c6ae99SBarry Smith   PetscFunctionBegin;
1335a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1336a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
13375a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13389566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateInjection, dac, daf, 0, 0));
1339dbbe0bcdSBarry Smith   PetscUseTypeMethod(dac, createinjection, daf, mat);
13409566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateInjection, dac, daf, 0, 0));
13413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
134247c6ae99SBarry Smith }
134347c6ae99SBarry Smith 
1344b412c318SBarry Smith /*@
1345bb7acecfSBarry 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
1346bb7acecfSBarry Smith   a Galerkin finite element model on the `DM`
1347bd041c0cSMatthew G. Knepley 
1348*20f4b53cSBarry Smith   Collective
1349bd041c0cSMatthew G. Knepley 
1350d8d19677SJose E. Roman   Input Parameters:
1351bb7acecfSBarry Smith + dmc - the target `DM` object
1352bb7acecfSBarry Smith - dmf - the source `DM` object
1353bd041c0cSMatthew G. Knepley 
1354bd041c0cSMatthew G. Knepley   Output Parameter:
1355b4937a87SMatthew G. Knepley . mat - the mass matrix
1356bd041c0cSMatthew G. Knepley 
1357bd041c0cSMatthew G. Knepley   Level: developer
1358bd041c0cSMatthew G. Knepley 
1359bb7acecfSBarry Smith   Notes:
1360bb7acecfSBarry Smith   For `DMPLEX` the finite element model for the `DM` must have been already provided.
1361bb7acecfSBarry Smith 
1362*20f4b53cSBarry 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()`
1363bb7acecfSBarry Smith 
1364bb7acecfSBarry Smith .seealso: `DMCreateMassMatrixLumped()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1365bd041c0cSMatthew G. Knepley @*/
1366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrix(DM dmc, DM dmf, Mat *mat)
1367d71ae5a4SJacob Faibussowitsch {
1368bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1369b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 1);
1370b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 2);
13715a84ad33SLisandro Dalcin   PetscValidPointer(mat, 3);
13725b8ffe73SMark Adams   PetscCall(PetscLogEventBegin(DM_CreateMassMatrix, 0, 0, 0, 0));
1373dbbe0bcdSBarry Smith   PetscUseTypeMethod(dmc, createmassmatrix, dmf, mat);
13745b8ffe73SMark Adams   PetscCall(PetscLogEventEnd(DM_CreateMassMatrix, 0, 0, 0, 0));
13753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1376b4937a87SMatthew G. Knepley }
1377b4937a87SMatthew G. Knepley 
1378b4937a87SMatthew G. Knepley /*@
1379bb7acecfSBarry Smith   DMCreateMassMatrixLumped - Gets the lumped mass matrix for a given `DM`
1380b4937a87SMatthew G. Knepley 
1381*20f4b53cSBarry Smith   Collective
1382b4937a87SMatthew G. Knepley 
1383b4937a87SMatthew G. Knepley   Input Parameter:
1384bb7acecfSBarry Smith . dm - the `DM` object
1385b4937a87SMatthew G. Knepley 
1386b4937a87SMatthew G. Knepley   Output Parameter:
1387bb7acecfSBarry Smith . lm - the lumped mass matrix, which is a diagonal matrix, represented as a vector
1388b4937a87SMatthew G. Knepley 
1389b4937a87SMatthew G. Knepley   Level: developer
1390b4937a87SMatthew G. Knepley 
1391bb7acecfSBarry Smith   Note:
1392bb7acecfSBarry Smith   See `DMCreateMassMatrix()` for how to create the non-lumped version of the mass matrix.
1393bb7acecfSBarry Smith 
1394bb7acecfSBarry Smith .seealso: `DMCreateMassMatrix()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMRefine()`, `DMCoarsen()`, `DMCreateRestriction()`, `DMCreateInterpolation()`, `DMCreateInjection()`
1395b4937a87SMatthew G. Knepley @*/
1396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMassMatrixLumped(DM dm, Vec *lm)
1397d71ae5a4SJacob Faibussowitsch {
1398b4937a87SMatthew G. Knepley   PetscFunctionBegin;
1399b4937a87SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1400b4937a87SMatthew G. Knepley   PetscValidPointer(lm, 2);
1401dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createmassmatrixlumped, lm);
14023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1403bd041c0cSMatthew G. Knepley }
1404bd041c0cSMatthew G. Knepley 
1405bd041c0cSMatthew G. Knepley /*@
1406bb7acecfSBarry Smith     DMCreateColoring - Gets coloring of a graph associated with the `DM`. Often the graph represents the operator matrix associated with the discretization
1407bb7acecfSBarry Smith     of a PDE on the `DM`.
140847c6ae99SBarry Smith 
1409*20f4b53cSBarry Smith     Collective
141047c6ae99SBarry Smith 
1411d8d19677SJose E. Roman     Input Parameters:
1412bb7acecfSBarry Smith +   dm - the `DM` object
1413bb7acecfSBarry Smith -   ctype - `IS_COLORING_LOCAL` or `IS_COLORING_GLOBAL`
141447c6ae99SBarry Smith 
141547c6ae99SBarry Smith     Output Parameter:
141647c6ae99SBarry Smith .   coloring - the coloring
141747c6ae99SBarry Smith 
14181bf8429eSBarry Smith     Level: developer
14191bf8429eSBarry Smith 
1420ec5066bdSBarry Smith     Notes:
1421bb7acecfSBarry 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
1422bb7acecfSBarry Smith     matrix comes from (what this function provides). In general using the mesh produces a more optimal coloring (fewer colors).
1423ec5066bdSBarry Smith 
1424bb7acecfSBarry Smith     This produces a coloring with the distance of 2, see `MatSetColoringDistance()` which can be used for efficiently computing Jacobians with `MatFDColoringCreate()`
14251bf8429eSBarry Smith     For `DMDA` in three dimensions with periodic boundary conditions the number of grid points in each dimension must be divisible by 2*stencil_width + 1,
14261bf8429eSBarry Smith     otherwise an error will be generated.
1427ec5066bdSBarry Smith 
14281bf8429eSBarry Smith .seealso: `DM`, `ISColoring`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatType()`, `MatColoring`, `MatFDColoringCreate()`
1429aab9d709SJed Brown @*/
1430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateColoring(DM dm, ISColoringType ctype, ISColoring *coloring)
1431d71ae5a4SJacob Faibussowitsch {
143247c6ae99SBarry Smith   PetscFunctionBegin;
1433171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14345a84ad33SLisandro Dalcin   PetscValidPointer(coloring, 3);
1435dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getcoloring, ctype, coloring);
14363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
143747c6ae99SBarry Smith }
143847c6ae99SBarry Smith 
1439b412c318SBarry Smith /*@
1440bb7acecfSBarry Smith     DMCreateMatrix - Gets an empty matrix for a `DM` that is most commonly used to store the Jacobian of a discrete PDE operator.
144147c6ae99SBarry Smith 
1442*20f4b53cSBarry Smith     Collective
144347c6ae99SBarry Smith 
144447c6ae99SBarry Smith     Input Parameter:
1445bb7acecfSBarry Smith .   dm - the `DM` object
144647c6ae99SBarry Smith 
144747c6ae99SBarry Smith     Output Parameter:
144847c6ae99SBarry Smith .   mat - the empty Jacobian
144947c6ae99SBarry Smith 
1450*20f4b53cSBarry Smith     Options Database Key:
1451bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()` and `DMCreateMassMatrix()`, but do not fill it with zeros
1452f27dd7c6SMatthew G. Knepley 
1453*20f4b53cSBarry Smith     Level: beginner
1454*20f4b53cSBarry Smith 
145595452b02SPatrick Sanan     Notes:
145695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
145794013140SBarry Smith     do not need to do it yourself.
145894013140SBarry Smith 
145994013140SBarry Smith     By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1460bb7acecfSBarry Smith     the nonzero pattern call `DMSetMatrixPreallocateOnly()`
146194013140SBarry Smith 
1462bb7acecfSBarry Smith     For `DMDA`, when you call `MatView()` on this matrix it is displayed using the global natural ordering, NOT in the ordering used
146394013140SBarry Smith     internally by PETSc.
146494013140SBarry Smith 
1465bb7acecfSBarry Smith     For `DMDA`, in general it is easiest to use `MatSetValuesStencil()` or `MatSetValuesLocal()` to put values into the matrix because
1466bb7acecfSBarry Smith     `MatSetValues()` requires the indices for the global numbering for the `DMDA` which is complic`ated to compute
146794013140SBarry Smith 
1468bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMSetMatType()`, `DMCreateMassMatrix()`
1469aab9d709SJed Brown @*/
1470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateMatrix(DM dm, Mat *mat)
1471d71ae5a4SJacob Faibussowitsch {
147247c6ae99SBarry Smith   PetscFunctionBegin;
1473171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1474064a246eSJacob Faibussowitsch   PetscValidPointer(mat, 2);
14759566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
14769566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_CreateMatrix, 0, 0, 0, 0));
1477dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, creatematrix, mat);
147876bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1479c6b011d8SStefano Zampini     DM mdm;
1480c6b011d8SStefano Zampini 
14819566063dSJacob Faibussowitsch     PetscCall(MatGetDM(*mat, &mdm));
14827a8be351SBarry Smith     PetscCheck(mdm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "DM type '%s' did not attach the DM to the matrix", ((PetscObject)dm)->type_name);
1483c6b011d8SStefano Zampini   }
1484e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1485e5e52638SMatthew G. Knepley   if (dm->Nf) {
1486e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1487649ef022SMatthew Knepley     PetscInt     Nf, f;
1488e571a35bSMatthew G. Knepley 
14899566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
1490649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1491649ef022SMatthew Knepley       if (dm->nullspaceConstructors[f]) {
14929566063dSJacob Faibussowitsch         PetscCall((*dm->nullspaceConstructors[f])(dm, f, f, &nullSpace));
14939566063dSJacob Faibussowitsch         PetscCall(MatSetNullSpace(*mat, nullSpace));
14949566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1495649ef022SMatthew Knepley         break;
1496e571a35bSMatthew G. Knepley       }
1497649ef022SMatthew Knepley     }
1498649ef022SMatthew Knepley     for (f = 0; f < Nf; ++f) {
1499649ef022SMatthew Knepley       if (dm->nearnullspaceConstructors[f]) {
15009566063dSJacob Faibussowitsch         PetscCall((*dm->nearnullspaceConstructors[f])(dm, f, f, &nullSpace));
15019566063dSJacob Faibussowitsch         PetscCall(MatSetNearNullSpace(*mat, nullSpace));
15029566063dSJacob Faibussowitsch         PetscCall(MatNullSpaceDestroy(&nullSpace));
1503e571a35bSMatthew G. Knepley       }
1504e571a35bSMatthew G. Knepley     }
1505e571a35bSMatthew G. Knepley   }
15069566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_CreateMatrix, 0, 0, 0, 0));
15073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
150847c6ae99SBarry Smith }
150947c6ae99SBarry Smith 
1510732e2eb9SMatthew G Knepley /*@
1511bb7acecfSBarry Smith   DMSetMatrixPreallocateSkip - When `DMCreateMatrix()` is called the matrix sizes and `ISLocalToGlobalMapping` will be
1512bb7acecfSBarry 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
1513bb7acecfSBarry Smith   `MatSetPreallocationCOO()` and `MatSetValuesCOO()` will be used.
1514aa0f6e3cSJed Brown 
1515*20f4b53cSBarry Smith   Logically Collective
1516aa0f6e3cSJed Brown 
1517aa0f6e3cSJed Brown   Input Parameters:
1518bb7acecfSBarry Smith + dm - the `DM`
1519bb7acecfSBarry Smith - skip - `PETSC_TRUE` to skip preallocation
1520aa0f6e3cSJed Brown 
1521aa0f6e3cSJed Brown   Level: developer
1522aa0f6e3cSJed Brown 
1523bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateOnly()`
1524aa0f6e3cSJed Brown @*/
1525d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateSkip(DM dm, PetscBool skip)
1526d71ae5a4SJacob Faibussowitsch {
1527aa0f6e3cSJed Brown   PetscFunctionBegin;
1528aa0f6e3cSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1529aa0f6e3cSJed Brown   dm->prealloc_skip = skip;
15303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1531aa0f6e3cSJed Brown }
1532aa0f6e3cSJed Brown 
1533aa0f6e3cSJed Brown /*@
1534bb7acecfSBarry Smith   DMSetMatrixPreallocateOnly - When `DMCreateMatrix()` is called the matrix will be properly
1535732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1536732e2eb9SMatthew G Knepley 
1537*20f4b53cSBarry Smith   Logically Collective
1538732e2eb9SMatthew G Knepley 
1539d8d19677SJose E. Roman   Input Parameters:
1540bb7acecfSBarry Smith + dm - the `DM`
1541bb7acecfSBarry Smith - only - `PETSC_TRUE` if only want preallocation
1542732e2eb9SMatthew G Knepley 
1543*20f4b53cSBarry Smith   Options Database Key:
1544bb7acecfSBarry Smith . -dm_preallocate_only - Only preallocate the matrix for `DMCreateMatrix()`, `DMCreateMassMatrix()`, but do not fill it with zeros
1545f27dd7c6SMatthew G. Knepley 
1546*20f4b53cSBarry Smith   Level: developer
1547*20f4b53cSBarry Smith 
1548bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMSetMatrixStructureOnly()`, `DMSetMatrixPreallocateSkip()`
1549732e2eb9SMatthew G Knepley @*/
1550d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1551d71ae5a4SJacob Faibussowitsch {
1552732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1553732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1554732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
15553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1556732e2eb9SMatthew G Knepley }
1557732e2eb9SMatthew G Knepley 
1558b06ff27eSHong Zhang /*@
1559bb7acecfSBarry Smith   DMSetMatrixStructureOnly - When `DMCreateMatrix()` is called, the matrix structure will be created
1560bb7acecfSBarry Smith     but the array for numerical values will not be allocated.
1561b06ff27eSHong Zhang 
1562*20f4b53cSBarry Smith   Logically Collective
1563b06ff27eSHong Zhang 
1564d8d19677SJose E. Roman   Input Parameters:
1565bb7acecfSBarry Smith + dm - the `DM`
1566da81f932SPierre Jolivet - only - `PETSC_TRUE` if you only want matrix structure
1567b06ff27eSHong Zhang 
1568b06ff27eSHong Zhang   Level: developer
1569bb7acecfSBarry Smith 
1570bb7acecfSBarry Smith .seealso: `DMCreateMatrix()`, `DMSetMatrixPreallocateOnly()`, `DMSetMatrixPreallocateSkip()`
1571b06ff27eSHong Zhang @*/
1572d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1573d71ae5a4SJacob Faibussowitsch {
1574b06ff27eSHong Zhang   PetscFunctionBegin;
1575b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1576b06ff27eSHong Zhang   dm->structure_only = only;
15773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1578b06ff27eSHong Zhang }
1579b06ff27eSHong Zhang 
1580863027abSJed Brown /*@
1581863027abSJed Brown   DMSetBlockingType - set the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1582863027abSJed Brown 
1583*20f4b53cSBarry Smith   Logically Collective
1584863027abSJed Brown 
1585863027abSJed Brown   Input Parameters:
1586863027abSJed Brown + dm - the `DM`
1587863027abSJed Brown - btype - block by topological point or field node
1588863027abSJed Brown 
1589*20f4b53cSBarry Smith   Options Database Key:
15900e762ea3SJed Brown . -dm_blocking_type [topological_point, field_node] - use topological point blocking or field node blocking
1591863027abSJed Brown 
1592*20f4b53cSBarry Smith   Level: advanced
1593*20f4b53cSBarry Smith 
1594863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1595863027abSJed Brown @*/
1596863027abSJed Brown PetscErrorCode DMSetBlockingType(DM dm, DMBlockingType btype)
1597863027abSJed Brown {
1598863027abSJed Brown   PetscFunctionBegin;
1599863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1600863027abSJed Brown   dm->blocking_type = btype;
16013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1602863027abSJed Brown }
1603863027abSJed Brown 
1604863027abSJed Brown /*@
1605863027abSJed Brown   DMGetBlockingType - get the blocking granularity to be used for variable block size `DMCreateMatrix()` is called
1606863027abSJed Brown 
1607863027abSJed Brown   Not Collective
1608863027abSJed Brown 
1609863027abSJed Brown   Input Parameters:
1610863027abSJed Brown . dm - the `DM`
1611863027abSJed Brown 
1612863027abSJed Brown   Output Parameters:
1613863027abSJed Brown . btype - block by topological point or field node
1614863027abSJed Brown 
1615863027abSJed Brown   Level: advanced
1616863027abSJed Brown 
1617863027abSJed Brown .seealso: `DMCreateMatrix()`, `MatSetVariableBlockSizes()`
1618863027abSJed Brown @*/
1619863027abSJed Brown PetscErrorCode DMGetBlockingType(DM dm, DMBlockingType *btype)
1620863027abSJed Brown {
1621863027abSJed Brown   PetscFunctionBegin;
1622863027abSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1623863027abSJed Brown   PetscValidPointer(btype, 2);
1624863027abSJed Brown   *btype = dm->blocking_type;
16253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1626863027abSJed Brown }
1627863027abSJed Brown 
1628a89ea682SMatthew G Knepley /*@C
1629bb7acecfSBarry Smith   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with `DMRestoreWorkArray()`
1630a89ea682SMatthew G Knepley 
1631a89ea682SMatthew G Knepley   Not Collective
1632a89ea682SMatthew G Knepley 
1633a89ea682SMatthew G Knepley   Input Parameters:
1634bb7acecfSBarry Smith + dm - the `DM` object
1635a5b23f4aSJose E. Roman . count - The minimum size
1636*20f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, or `MPIU_INT`)
1637a89ea682SMatthew G Knepley 
1638a89ea682SMatthew G Knepley   Output Parameter:
1639a89ea682SMatthew G Knepley . array - the work array
1640a89ea682SMatthew G Knepley 
1641a89ea682SMatthew G Knepley   Level: developer
1642a89ea682SMatthew G Knepley 
1643bb7acecfSBarry Smith   Note:
1644da81f932SPierre Jolivet   A `DM` may stash the array between instantiations so using this routine may be more efficient than calling `PetscMalloc()`
1645bb7acecfSBarry Smith 
1646bb7acecfSBarry Smith   The array may contain nonzero values
1647bb7acecfSBarry Smith 
1648bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMRestoreWorkArray()`, `PetscMalloc()`
1649a89ea682SMatthew G Knepley @*/
1650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1651d71ae5a4SJacob Faibussowitsch {
1652aa1993deSMatthew G Knepley   DMWorkLink  link;
165369291d52SBarry Smith   PetscMPIInt dsize;
1654a89ea682SMatthew G Knepley 
1655a89ea682SMatthew G Knepley   PetscFunctionBegin;
1656a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1657aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1658aa1993deSMatthew G Knepley   if (dm->workin) {
1659aa1993deSMatthew G Knepley     link       = dm->workin;
1660aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1661aa1993deSMatthew G Knepley   } else {
16624dfa11a4SJacob Faibussowitsch     PetscCall(PetscNew(&link));
1663a89ea682SMatthew G Knepley   }
16649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(dtype, &dsize));
16655056fcd2SBarry Smith   if (((size_t)dsize * count) > link->bytes) {
16669566063dSJacob Faibussowitsch     PetscCall(PetscFree(link->mem));
16679566063dSJacob Faibussowitsch     PetscCall(PetscMalloc(dsize * count, &link->mem));
1668854ce69bSBarry Smith     link->bytes = dsize * count;
1669aa1993deSMatthew G Knepley   }
1670aa1993deSMatthew G Knepley   link->next  = dm->workout;
1671aa1993deSMatthew G Knepley   dm->workout = link;
1672cea3dcb8SSatish Balay #if defined(__MEMCHECK_H) && (defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) || defined(PLAT_amd64_darwin))
167300d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char *)link->mem + (size_t)dsize * count, link->bytes - (size_t)dsize * count);
167400d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize * count);
167500d952a4SJed Brown #endif
1676aa1993deSMatthew G Knepley   *(void **)mem = link->mem;
16773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1678a89ea682SMatthew G Knepley }
1679a89ea682SMatthew G Knepley 
1680aa1993deSMatthew G Knepley /*@C
1681bb7acecfSBarry Smith   DMRestoreWorkArray - Restores a work array obtained with `DMCreateWorkArray()`
1682aa1993deSMatthew G Knepley 
1683aa1993deSMatthew G Knepley   Not Collective
1684aa1993deSMatthew G Knepley 
1685aa1993deSMatthew G Knepley   Input Parameters:
1686bb7acecfSBarry Smith + dm - the `DM` object
1687a5b23f4aSJose E. Roman . count - The minimum size
1688*20f4b53cSBarry Smith - dtype - MPI data type, often `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_INT`
1689aa1993deSMatthew G Knepley 
1690aa1993deSMatthew G Knepley   Output Parameter:
1691aa1993deSMatthew G Knepley . array - the work array
1692aa1993deSMatthew G Knepley 
1693aa1993deSMatthew G Knepley   Level: developer
1694aa1993deSMatthew G Knepley 
1695*20f4b53cSBarry Smith   Developer Note:
1696bb7acecfSBarry Smith   count and dtype are ignored, they are only needed for `DMGetWorkArray()`
1697147403d9SBarry Smith 
1698bb7acecfSBarry Smith .seealso: `DMDestroy()`, `DMCreate()`, `DMGetWorkArray()`
1699aa1993deSMatthew G Knepley @*/
1700d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestoreWorkArray(DM dm, PetscInt count, MPI_Datatype dtype, void *mem)
1701d71ae5a4SJacob Faibussowitsch {
1702aa1993deSMatthew G Knepley   DMWorkLink *p, link;
1703aa1993deSMatthew G Knepley 
1704aa1993deSMatthew G Knepley   PetscFunctionBegin;
1705aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1706aa1993deSMatthew G Knepley   PetscValidPointer(mem, 4);
1707aa1993deSMatthew G Knepley   for (p = &dm->workout; (link = *p); p = &link->next) {
1708aa1993deSMatthew G Knepley     if (link->mem == *(void **)mem) {
1709aa1993deSMatthew G Knepley       *p            = link->next;
1710aa1993deSMatthew G Knepley       link->next    = dm->workin;
1711aa1993deSMatthew G Knepley       dm->workin    = link;
17120298fd71SBarry Smith       *(void **)mem = NULL;
17133ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
1714aa1993deSMatthew G Knepley     }
1715aa1993deSMatthew G Knepley   }
1716aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Array was not checked out");
1717aa1993deSMatthew G Knepley }
1718e7c4fc90SDmitry Karpeev 
17198cda7954SMatthew G. Knepley /*@C
1720bb7acecfSBarry Smith   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field, defined with `DMAddField()`, when function spaces
1721bb7acecfSBarry Smith   are joined or split, such as in `DMCreateSubDM()`
17228cda7954SMatthew G. Knepley 
1723*20f4b53cSBarry Smith   Logically Collective; No Fortran Support
17248cda7954SMatthew G. Knepley 
17258cda7954SMatthew G. Knepley   Input Parameters:
1726bb7acecfSBarry Smith + dm     - The `DM`
17278cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17288cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
17298cda7954SMatthew G. Knepley 
1730*20f4b53cSBarry Smith   Calling sequence of `nullsp`:
1731147403d9SBarry Smith .vb
1732147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1733147403d9SBarry Smith .ve
1734bb7acecfSBarry Smith +  dm        - The present `DM`
1735bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1736147403d9SBarry Smith .  field     - The field number in dm
1737147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17388cda7954SMatthew G. Knepley 
173949762cbcSSatish Balay   Level: intermediate
174049762cbcSSatish Balay 
1741bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1742147403d9SBarry Smith @*/
1743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1744d71ae5a4SJacob Faibussowitsch {
1745435a35e8SMatthew G Knepley   PetscFunctionBegin;
1746435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17477a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1748435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
17493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1750435a35e8SMatthew G Knepley }
1751435a35e8SMatthew G Knepley 
17528cda7954SMatthew G. Knepley /*@C
1753bb7acecfSBarry Smith   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, defined with `DMAddField()`
17548cda7954SMatthew G. Knepley 
1755*20f4b53cSBarry Smith   Not Collective; No Fortran Support
17568cda7954SMatthew G. Knepley 
17578cda7954SMatthew G. Knepley   Input Parameters:
1758bb7acecfSBarry Smith + dm     - The `DM`
17598cda7954SMatthew G. Knepley - field  - The field number for the nullspace
17608cda7954SMatthew G. Knepley 
17618cda7954SMatthew G. Knepley   Output Parameter:
17628cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
17638cda7954SMatthew G. Knepley 
1764*20f4b53cSBarry Smith   Calling sequence of `nullsp`:
1765147403d9SBarry Smith .vb
1766147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1767147403d9SBarry Smith .ve
1768147403d9SBarry Smith +  dm        - The present DM
1769147403d9SBarry Smith .  origField - The field number given above, in the original DM
1770147403d9SBarry Smith .  field     - The field number in dm
1771147403d9SBarry Smith -  nullSpace - The nullspace for the given field
17728cda7954SMatthew G. Knepley 
177349762cbcSSatish Balay    Level: intermediate
177449762cbcSSatish Balay 
1775bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNullSpaceConstructor()`, `DMSetNearNullSpaceConstructor()`, `DMGetNearNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
1776147403d9SBarry Smith @*/
1777d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1778d71ae5a4SJacob Faibussowitsch {
17790a50eb56SMatthew G. Knepley   PetscFunctionBegin;
17800a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1781f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
17827a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
17830a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
17843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17850a50eb56SMatthew G. Knepley }
17860a50eb56SMatthew G. Knepley 
17878cda7954SMatthew G. Knepley /*@C
1788bb7acecfSBarry Smith   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
17898cda7954SMatthew G. Knepley 
1790*20f4b53cSBarry Smith   Logically Collective; No Fortran Support
17918cda7954SMatthew G. Knepley 
17928cda7954SMatthew G. Knepley   Input Parameters:
1793bb7acecfSBarry Smith + dm     - The `DM`
17948cda7954SMatthew G. Knepley . field  - The field number for the nullspace
17958cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
17968cda7954SMatthew G. Knepley 
1797*20f4b53cSBarry Smith   Calling sequence of `nullsp`:
1798147403d9SBarry Smith .vb
1799147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1800147403d9SBarry Smith .ve
1801bb7acecfSBarry Smith +  dm        - The present `DM`
1802bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1803147403d9SBarry Smith .  field     - The field number in dm
1804147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18058cda7954SMatthew G. Knepley 
180649762cbcSSatish Balay    Level: intermediate
180749762cbcSSatish Balay 
1808bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`, `DMCreateSuperDM()`,
1809bb7acecfSBarry Smith           `MatNullSpace`
1810147403d9SBarry Smith @*/
1811d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1812d71ae5a4SJacob Faibussowitsch {
1813f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1814f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18157a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1816f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
18173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1818f9d4088aSMatthew G. Knepley }
1819f9d4088aSMatthew G. Knepley 
18208cda7954SMatthew G. Knepley /*@C
1821bb7acecfSBarry Smith   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, defined with `DMAddField()`
18228cda7954SMatthew G. Knepley 
1823*20f4b53cSBarry Smith   Not Collective; No Fortran Support
18248cda7954SMatthew G. Knepley 
18258cda7954SMatthew G. Knepley   Input Parameters:
1826bb7acecfSBarry Smith + dm     - The `DM`
18278cda7954SMatthew G. Knepley - field  - The field number for the nullspace
18288cda7954SMatthew G. Knepley 
18298cda7954SMatthew G. Knepley   Output Parameter:
18308cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
18318cda7954SMatthew G. Knepley 
1832*20f4b53cSBarry Smith   Calling sequence of `nullsp`:
1833147403d9SBarry Smith .vb
1834147403d9SBarry Smith     PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
1835147403d9SBarry Smith .ve
1836bb7acecfSBarry Smith +  dm        - The present `DM`
1837bb7acecfSBarry Smith .  origField - The field number given above, in the original `DM`
1838147403d9SBarry Smith .  field     - The field number in dm
1839147403d9SBarry Smith -  nullSpace - The nullspace for the given field
18408cda7954SMatthew G. Knepley 
184149762cbcSSatish Balay    Level: intermediate
184249762cbcSSatish Balay 
1843bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMSetNearNullSpaceConstructor()`, `DMSetNullSpaceConstructor()`, `DMGetNullSpaceConstructor()`, `DMCreateSubDM()`,
1844bb7acecfSBarry Smith           `MatNullSpace`, `DMCreateSuperDM()`
1845147403d9SBarry Smith @*/
1846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM, PetscInt, PetscInt, MatNullSpace *))
1847d71ae5a4SJacob Faibussowitsch {
1848f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1849f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1850f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
18517a8be351SBarry Smith   PetscCheck(field < 10, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %" PetscInt_FMT " >= 10 fields", field);
1852f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
18533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1854f9d4088aSMatthew G. Knepley }
1855f9d4088aSMatthew G. Knepley 
18564f3b5142SJed Brown /*@C
1857bb7acecfSBarry Smith   DMCreateFieldIS - Creates a set of `IS` objects with the global indices of dofs for each field defined with `DMAddField()`
18584d343eeaSMatthew G Knepley 
1859*20f4b53cSBarry Smith   Not Collective; No Fortran Support
18604d343eeaSMatthew G Knepley 
18614d343eeaSMatthew G Knepley   Input Parameter:
1862bb7acecfSBarry Smith . dm - the `DM` object
18634d343eeaSMatthew G Knepley 
18644d343eeaSMatthew G Knepley   Output Parameters:
1865*20f4b53cSBarry Smith + numFields  - The number of fields (or `NULL` if not requested)
1866*20f4b53cSBarry Smith . fieldNames - The number of each field (or `NULL` if not requested)
1867*20f4b53cSBarry Smith - fields     - The global indices for each field (or `NULL` if not requested)
18684d343eeaSMatthew G Knepley 
18694d343eeaSMatthew G Knepley   Level: intermediate
18704d343eeaSMatthew G Knepley 
1871bb7acecfSBarry Smith   Note:
187221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1873bb7acecfSBarry Smith   `PetscFree()`, every entry of fields should be destroyed with `ISDestroy()`, and both arrays should be freed with
1874bb7acecfSBarry Smith   `PetscFree()`.
187521c9b008SJed Brown 
1876bb7acecfSBarry Smith   Developer Note:
1877bb7acecfSBarry Smith   It is not clear why both this function and `DMCreateFieldDecomposition()` exist. Having two seems redundant and confusing. This function should
1878bb7acecfSBarry Smith   likely be removed.
1879bb7acecfSBarry Smith 
1880bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMGetField()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`,
1881bb7acecfSBarry Smith           `DMCreateFieldDecomposition()`
18824d343eeaSMatthew G Knepley @*/
1883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1884d71ae5a4SJacob Faibussowitsch {
188537d0c07bSMatthew G Knepley   PetscSection section, sectionGlobal;
18864d343eeaSMatthew G Knepley 
18874d343eeaSMatthew G Knepley   PetscFunctionBegin;
18884d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
188969ca1f37SDmitry Karpeev   if (numFields) {
1890534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields, 2);
189169ca1f37SDmitry Karpeev     *numFields = 0;
189269ca1f37SDmitry Karpeev   }
189337d0c07bSMatthew G Knepley   if (fieldNames) {
189437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames, 3);
18950298fd71SBarry Smith     *fieldNames = NULL;
189669ca1f37SDmitry Karpeev   }
189769ca1f37SDmitry Karpeev   if (fields) {
189869ca1f37SDmitry Karpeev     PetscValidPointer(fields, 4);
18990298fd71SBarry Smith     *fields = NULL;
190069ca1f37SDmitry Karpeev   }
19019566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
190237d0c07bSMatthew G Knepley   if (section) {
19033a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
190437d0c07bSMatthew G Knepley     PetscInt  nF, f, pStart, pEnd, p;
190537d0c07bSMatthew G Knepley 
19069566063dSJacob Faibussowitsch     PetscCall(DMGetGlobalSection(dm, &sectionGlobal));
19079566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetNumFields(section, &nF));
19089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc3(nF, &fieldSizes, nF, &fieldNc, nF, &fieldIndices));
19099566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(sectionGlobal, &pStart, &pEnd));
191037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
191137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
19129566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldComponents(section, f, &fieldNc[f]));
191337d0c07bSMatthew G Knepley     }
191437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
191537d0c07bSMatthew G Knepley       PetscInt gdof;
191637d0c07bSMatthew G Knepley 
19179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
191837d0c07bSMatthew G Knepley       if (gdof > 0) {
191937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
19203a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
192137d0c07bSMatthew G Knepley 
19229566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19239566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
19243a544194SStefano Zampini           fpdof = fdof - fcdof;
19253a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
19263a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
19273a544194SStefano Zampini             fieldNc[f] = 1;
19283a544194SStefano Zampini           }
19293a544194SStefano Zampini           fieldSizes[f] += fpdof;
193037d0c07bSMatthew G Knepley         }
193137d0c07bSMatthew G Knepley       }
193237d0c07bSMatthew G Knepley     }
193337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
19349566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(fieldSizes[f], &fieldIndices[f]));
193537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
193637d0c07bSMatthew G Knepley     }
193737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
193837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
193937d0c07bSMatthew G Knepley 
19409566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(sectionGlobal, p, &gdof));
194137d0c07bSMatthew G Knepley       if (gdof > 0) {
19429566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(sectionGlobal, p, &goff));
194337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
194437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
194537d0c07bSMatthew G Knepley 
19469566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldDof(section, p, f, &fdof));
19479566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldConstraintDof(section, p, f, &fcdof));
1948ad540459SPierre Jolivet           for (fc = 0; fc < fdof - fcdof; ++fc, ++fieldSizes[f]) fieldIndices[f][fieldSizes[f]] = goff++;
194937d0c07bSMatthew G Knepley         }
195037d0c07bSMatthew G Knepley       }
195137d0c07bSMatthew G Knepley     }
19528865f1eaSKarl Rupp     if (numFields) *numFields = nF;
195337d0c07bSMatthew G Knepley     if (fieldNames) {
19549566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fieldNames));
195537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
195637d0c07bSMatthew G Knepley         const char *fieldName;
195737d0c07bSMatthew G Knepley 
19589566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
19599566063dSJacob Faibussowitsch         PetscCall(PetscStrallocpy(fieldName, (char **)&(*fieldNames)[f]));
196037d0c07bSMatthew G Knepley       }
196137d0c07bSMatthew G Knepley     }
196237d0c07bSMatthew G Knepley     if (fields) {
19639566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(nF, fields));
196437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
19653a544194SStefano Zampini         PetscInt bs, in[2], out[2];
19663a544194SStefano Zampini 
19679566063dSJacob Faibussowitsch         PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]));
19683a544194SStefano Zampini         in[0] = -fieldNc[f];
19693a544194SStefano Zampini         in[1] = fieldNc[f];
19701c2dc1cbSBarry Smith         PetscCall(MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
19713a544194SStefano Zampini         bs = (-out[0] == out[1]) ? out[1] : 1;
19729566063dSJacob Faibussowitsch         PetscCall(ISSetBlockSize((*fields)[f], bs));
197337d0c07bSMatthew G Knepley       }
197437d0c07bSMatthew G Knepley     }
19759566063dSJacob Faibussowitsch     PetscCall(PetscFree3(fieldSizes, fieldNc, fieldIndices));
1976dbbe0bcdSBarry Smith   } else PetscTryTypeMethod(dm, createfieldis, numFields, fieldNames, fields);
19773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19784d343eeaSMatthew G Knepley }
19794d343eeaSMatthew G Knepley 
198016621825SDmitry Karpeev /*@C
1981bb7acecfSBarry Smith   DMCreateFieldDecomposition - Returns a list of `IS` objects defining a decomposition of a problem into subproblems
1982bb7acecfSBarry Smith                           corresponding to different fields: each `IS` contains the global indices of the dofs of the
1983bb7acecfSBarry Smith                           corresponding field, defined by `DMAddField()`. The optional list of `DM`s define the `DM` for each subproblem.
1984bb7acecfSBarry Smith                           The same as `DMCreateFieldIS()` but also returns a `DM` for each field.
1985e7c4fc90SDmitry Karpeev 
1986*20f4b53cSBarry Smith   Not Collective; No Fortran Support
1987e7c4fc90SDmitry Karpeev 
1988e7c4fc90SDmitry Karpeev   Input Parameter:
1989bb7acecfSBarry Smith . dm - the `DM` object
1990e7c4fc90SDmitry Karpeev 
1991e7c4fc90SDmitry Karpeev   Output Parameters:
1992*20f4b53cSBarry Smith + len       - The number of fields (or `NULL` if not requested)
1993*20f4b53cSBarry Smith . namelist  - The name for each field (or `NULL` if not requested)
1994*20f4b53cSBarry Smith . islist    - The global indices for each field (or `NULL` if not requested)
1995*20f4b53cSBarry Smith - dmlist    - The `DM`s for each field subproblem (or `NULL`, if not requested; if `NULL` is returned, no `DM`s are defined)
1996e7c4fc90SDmitry Karpeev 
1997e7c4fc90SDmitry Karpeev   Level: intermediate
1998e7c4fc90SDmitry Karpeev 
1999bb7acecfSBarry Smith   Note:
2000e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2001bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2002bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
2003e7c4fc90SDmitry Karpeev 
2004bb7acecfSBarry Smith   Developer Note:
2005bb7acecfSBarry Smith   It is not clear why this function and `DMCreateFieldIS()` exist. Having two seems redundant and confusing.
2006bb7acecfSBarry Smith 
2007bb7acecfSBarry Smith .seealso: `DMAddField()`, `DMCreateFieldIS()`, `DMCreateSubDM()`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2008e7c4fc90SDmitry Karpeev @*/
2009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
2010d71ae5a4SJacob Faibussowitsch {
2011e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
2012e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20138865f1eaSKarl Rupp   if (len) {
2014534a8f05SLisandro Dalcin     PetscValidIntPointer(len, 2);
20158865f1eaSKarl Rupp     *len = 0;
20168865f1eaSKarl Rupp   }
20178865f1eaSKarl Rupp   if (namelist) {
20188865f1eaSKarl Rupp     PetscValidPointer(namelist, 3);
2019ea78f98cSLisandro Dalcin     *namelist = NULL;
20208865f1eaSKarl Rupp   }
20218865f1eaSKarl Rupp   if (islist) {
20228865f1eaSKarl Rupp     PetscValidPointer(islist, 4);
2023ea78f98cSLisandro Dalcin     *islist = NULL;
20248865f1eaSKarl Rupp   }
20258865f1eaSKarl Rupp   if (dmlist) {
20268865f1eaSKarl Rupp     PetscValidPointer(dmlist, 5);
2027ea78f98cSLisandro Dalcin     *dmlist = NULL;
20288865f1eaSKarl Rupp   }
2029f3f0edfdSDmitry Karpeev   /*
2030f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2031f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2032f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2033f3f0edfdSDmitry Karpeev    */
20347a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
203516621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
2036435a35e8SMatthew G Knepley     PetscSection section;
2037435a35e8SMatthew G Knepley     PetscInt     numFields, f;
2038435a35e8SMatthew G Knepley 
20399566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
20409566063dSJacob Faibussowitsch     if (section) PetscCall(PetscSectionGetNumFields(section, &numFields));
2041435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
2042f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
20439566063dSJacob Faibussowitsch       if (namelist) PetscCall(PetscMalloc1(numFields, namelist));
20449566063dSJacob Faibussowitsch       if (islist) PetscCall(PetscMalloc1(numFields, islist));
20459566063dSJacob Faibussowitsch       if (dmlist) PetscCall(PetscMalloc1(numFields, dmlist));
2046435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
2047435a35e8SMatthew G Knepley         const char *fieldName;
2048435a35e8SMatthew G Knepley 
20499566063dSJacob Faibussowitsch         PetscCall(DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL));
205003dc3394SMatthew G. Knepley         if (namelist) {
20519566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetFieldName(section, f, &fieldName));
20529566063dSJacob Faibussowitsch           PetscCall(PetscStrallocpy(fieldName, (char **)&(*namelist)[f]));
2053435a35e8SMatthew G Knepley         }
205403dc3394SMatthew G. Knepley       }
2055435a35e8SMatthew G Knepley     } else {
20569566063dSJacob Faibussowitsch       PetscCall(DMCreateFieldIS(dm, len, namelist, islist));
2057e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
20580298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
2059e7c4fc90SDmitry Karpeev     }
2060dbbe0bcdSBarry Smith   } else PetscUseTypeMethod(dm, createfielddecomposition, len, namelist, islist, dmlist);
20613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
206216621825SDmitry Karpeev }
206316621825SDmitry Karpeev 
2064412a4547SAlexis Marboeuf /*@C
2065*20f4b53cSBarry Smith   DMCreateSubDM - Returns an `IS` and `DM` encapsulating a subproblem defined by the fields passed in.
2066*20f4b53cSBarry Smith                   The fields are defined by `DMCreateFieldIS()`.
2067435a35e8SMatthew G Knepley 
2068435a35e8SMatthew G Knepley   Not collective
2069435a35e8SMatthew G Knepley 
2070435a35e8SMatthew G Knepley   Input Parameters:
2071bb7acecfSBarry Smith + dm        - The `DM` object
2072bb7acecfSBarry Smith . numFields - The number of fields to select
20732adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
2074435a35e8SMatthew G Knepley 
2075435a35e8SMatthew G Knepley   Output Parameters:
2076bb7acecfSBarry Smith + is - The global indices for all the degrees of freedom in the new sub `DM`
2077bb7acecfSBarry Smith - subdm - The `DM` for the subproblem
2078435a35e8SMatthew G Knepley 
2079*20f4b53cSBarry Smith   Level: intermediate
2080*20f4b53cSBarry Smith 
2081bb7acecfSBarry Smith   Note:
2082bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
20835d3b26e6SMatthew G. Knepley 
2084bb7acecfSBarry Smith .seealso: `DMCreateFieldIS()`, `DMCreateFieldDecomposition()`, `DMAddField()`, `DMCreateSuperDM()`, `DM`, `IS`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2085435a35e8SMatthew G Knepley @*/
2086d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2087d71ae5a4SJacob Faibussowitsch {
2088435a35e8SMatthew G Knepley   PetscFunctionBegin;
2089435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2090dadcf809SJacob Faibussowitsch   PetscValidIntPointer(fields, 3);
20918865f1eaSKarl Rupp   if (is) PetscValidPointer(is, 4);
20928865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm, 5);
2093dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createsubdm, numFields, fields, is, subdm);
20943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2095435a35e8SMatthew G Knepley }
2096435a35e8SMatthew G Knepley 
20972adcc780SMatthew G. Knepley /*@C
2098bb7acecfSBarry Smith   DMCreateSuperDM - Returns an arrays of `IS` and `DM` encapsulating a superproblem defined by multiple `DM`s passed in.
20992adcc780SMatthew G. Knepley 
21002adcc780SMatthew G. Knepley   Not collective
21012adcc780SMatthew G. Knepley 
2102d8d19677SJose E. Roman   Input Parameters:
2103bb7acecfSBarry Smith + dms - The `DM` objects
2104bb7acecfSBarry Smith - n - The number of `DM`s
21052adcc780SMatthew G. Knepley 
21062adcc780SMatthew G. Knepley   Output Parameters:
2107bb7acecfSBarry Smith + is - The global indices for each of subproblem within the super `DM`, or NULL
2108bb7acecfSBarry Smith - superdm - The `DM` for the superproblem
21092adcc780SMatthew G. Knepley 
2110*20f4b53cSBarry Smith   Level: intermediate
2111*20f4b53cSBarry Smith 
2112bb7acecfSBarry Smith   Note:
2113bb7acecfSBarry Smith   You need to call `DMPlexSetMigrationSF()` on the original `DM` if you want the Global-To-Natural map to be automatically constructed
21145d3b26e6SMatthew G. Knepley 
2115bb7acecfSBarry Smith .seealso: `DM`, `DMCreateSubDM()`, `DMPlexSetMigrationSF()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
21162adcc780SMatthew G. Knepley @*/
2117d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt n, IS **is, DM *superdm)
2118d71ae5a4SJacob Faibussowitsch {
21192adcc780SMatthew G. Knepley   PetscInt i;
21202adcc780SMatthew G. Knepley 
21212adcc780SMatthew G. Knepley   PetscFunctionBegin;
21222adcc780SMatthew G. Knepley   PetscValidPointer(dms, 1);
2123ad540459SPierre Jolivet   for (i = 0; i < n; ++i) PetscValidHeaderSpecific(dms[i], DM_CLASSID, 1);
21242adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is, 3);
2125a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm, 4);
2126bb7acecfSBarry Smith   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %" PetscInt_FMT, n);
2127bb7acecfSBarry Smith   if (n) {
2128b9d85ea2SLisandro Dalcin     DM dm = dms[0];
2129dbbe0bcdSBarry Smith     PetscCall((*dm->ops->createsuperdm)(dms, n, is, superdm));
21302adcc780SMatthew G. Knepley   }
21313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21322adcc780SMatthew G. Knepley }
21332adcc780SMatthew G. Knepley 
213416621825SDmitry Karpeev /*@C
2135bb7acecfSBarry Smith   DMCreateDomainDecomposition - Returns lists of `IS` objects defining a decomposition of a problem into subproblems
2136bb7acecfSBarry Smith                           corresponding to restrictions to pairs of nested subdomains: each `IS` contains the global
2137bb7acecfSBarry Smith                           indices of the dofs of the corresponding subdomains with in the dofs of the original `DM`.
2138bb7acecfSBarry Smith                           The inner subdomains conceptually define a nonoverlapping covering, while outer subdomains can overlap.
2139bb7acecfSBarry Smith                           The optional list of `DM`s define a `DM` for each subproblem.
214016621825SDmitry Karpeev 
2141*20f4b53cSBarry Smith   Not Collective
214216621825SDmitry Karpeev 
214316621825SDmitry Karpeev   Input Parameter:
2144bb7acecfSBarry Smith . dm - the `DM` object
214516621825SDmitry Karpeev 
214616621825SDmitry Karpeev   Output Parameters:
2147*20f4b53cSBarry Smith + n           - The number of subproblems in the domain decomposition (or `NULL` if not requested)
2148*20f4b53cSBarry Smith . namelist    - The name for each subdomain (or `NULL` if not requested)
21490298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
21500298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
2151*20f4b53cSBarry Smith - dmlist      - The `DM`s for each subdomain subproblem (or NULL, if not requested; if `NULL` is returned, no `DM`s are defined)
215216621825SDmitry Karpeev 
215316621825SDmitry Karpeev   Level: intermediate
215416621825SDmitry Karpeev 
2155bb7acecfSBarry Smith   Note:
215616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
2157bb7acecfSBarry Smith   `PetscFree()`, every entry of is should be destroyed with `ISDestroy()`, every entry of dm should be destroyed with `DMDestroy()`,
2158bb7acecfSBarry Smith   and all of the arrays should be freed with `PetscFree()`.
215916621825SDmitry Karpeev 
2160bb7acecfSBarry Smith   Questions:
2161*20f4b53cSBarry Smith   The `dmlist` is for the inner subdomains or the outer subdomains or all subdomains?
2162bb7acecfSBarry Smith 
2163bb7acecfSBarry Smith .seealso: `DMCreateFieldDecomposition()`, `DMDestroy()`, `DMCreateDomainDecompositionScatters()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldDecomposition()`
216416621825SDmitry Karpeev @*/
2165d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *n, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
2166d71ae5a4SJacob Faibussowitsch {
2167be081cd6SPeter Brune   DMSubDomainHookLink link;
2168be081cd6SPeter Brune   PetscInt            i, l;
216916621825SDmitry Karpeev 
217016621825SDmitry Karpeev   PetscFunctionBegin;
217116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21729371c9d4SSatish Balay   if (n) {
21739371c9d4SSatish Balay     PetscValidIntPointer(n, 2);
21749371c9d4SSatish Balay     *n = 0;
21759371c9d4SSatish Balay   }
21769371c9d4SSatish Balay   if (namelist) {
21779371c9d4SSatish Balay     PetscValidPointer(namelist, 3);
21789371c9d4SSatish Balay     *namelist = NULL;
21799371c9d4SSatish Balay   }
21809371c9d4SSatish Balay   if (innerislist) {
21819371c9d4SSatish Balay     PetscValidPointer(innerislist, 4);
21829371c9d4SSatish Balay     *innerislist = NULL;
21839371c9d4SSatish Balay   }
21849371c9d4SSatish Balay   if (outerislist) {
21859371c9d4SSatish Balay     PetscValidPointer(outerislist, 5);
21869371c9d4SSatish Balay     *outerislist = NULL;
21879371c9d4SSatish Balay   }
21889371c9d4SSatish Balay   if (dmlist) {
21899371c9d4SSatish Balay     PetscValidPointer(dmlist, 6);
21909371c9d4SSatish Balay     *dmlist = NULL;
21919371c9d4SSatish Balay   }
2192f3f0edfdSDmitry Karpeev   /*
2193f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
2194f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
2195f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
2196f3f0edfdSDmitry Karpeev    */
21977a8be351SBarry Smith   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
219816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
2199dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createdomaindecomposition, &l, namelist, innerislist, outerislist, dmlist);
220014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
2201f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
2202be081cd6SPeter Brune       for (i = 0; i < l; i++) {
2203be081cd6SPeter Brune         for (link = dm->subdomainhook; link; link = link->next) {
22049566063dSJacob Faibussowitsch           if (link->ddhook) PetscCall((*link->ddhook)(dm, (*dmlist)[i], link->ctx));
2205be081cd6SPeter Brune         }
2206648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
2207e7c4fc90SDmitry Karpeev       }
220814a18fd3SPeter Brune     }
2209bb7acecfSBarry Smith     if (n) *n = l;
221014a18fd3SPeter Brune   }
22113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2212e30e807fSPeter Brune }
2213e30e807fSPeter Brune 
2214e30e807fSPeter Brune /*@C
2215e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
2216e30e807fSPeter Brune 
2217*20f4b53cSBarry Smith   Not Collective
2218e30e807fSPeter Brune 
2219e30e807fSPeter Brune   Input Parameters:
2220bb7acecfSBarry Smith + dm - the `DM` object
2221e30e807fSPeter Brune . n  - the number of subdomain scatters
2222e30e807fSPeter Brune - subdms - the local subdomains
2223e30e807fSPeter Brune 
2224e30e807fSPeter Brune   Output Parameters:
22256b867d5aSJose E. Roman + iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2226e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2227e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2228e30e807fSPeter Brune 
2229*20f4b53cSBarry Smith   Level: developer
2230*20f4b53cSBarry Smith 
2231bb7acecfSBarry Smith   Note:
2232bb7acecfSBarry Smith     This is an alternative to the iis and ois arguments in `DMCreateDomainDecomposition()` that allow for the solution
2233e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2234e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2235e30e807fSPeter Brune   solution and residual data.
2236e30e807fSPeter Brune 
2237bb7acecfSBarry Smith   Questions:
2238bb7acecfSBarry Smith   Can the subdms input be anything or are they exactly the `DM` obtained from `DMCreateDomainDecomposition()`?
2239bb7acecfSBarry Smith 
2240bb7acecfSBarry Smith .seealso: `DM`, `DMCreateDomainDecomposition()`, `DMDestroy()`, `DMView()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMCreateFieldIS()`
2241e30e807fSPeter Brune @*/
2242d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDomainDecompositionScatters(DM dm, PetscInt n, DM *subdms, VecScatter **iscat, VecScatter **oscat, VecScatter **gscat)
2243d71ae5a4SJacob Faibussowitsch {
2244e30e807fSPeter Brune   PetscFunctionBegin;
2245e30e807fSPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2246e30e807fSPeter Brune   PetscValidPointer(subdms, 3);
2247dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, createddscatters, n, subdms, iscat, oscat, gscat);
22483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2249e7c4fc90SDmitry Karpeev }
2250e7c4fc90SDmitry Karpeev 
225147c6ae99SBarry Smith /*@
2252bb7acecfSBarry Smith   DMRefine - Refines a `DM` object using a standard nonadaptive refinement of the underlying mesh
225347c6ae99SBarry Smith 
2254*20f4b53cSBarry Smith   Collective
225547c6ae99SBarry Smith 
2256d8d19677SJose E. Roman   Input Parameters:
2257bb7acecfSBarry Smith + dm   - the `DM` object
2258bb7acecfSBarry Smith - comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
225947c6ae99SBarry Smith 
226047c6ae99SBarry Smith   Output Parameter:
2261*20f4b53cSBarry Smith . dmf - the refined `DM`, or `NULL`
2262ae0a1c52SMatthew G Knepley 
2263*20f4b53cSBarry Smith   Options Database Key:
2264412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2265412e9a14SMatthew G. Knepley 
226647c6ae99SBarry Smith   Level: developer
226747c6ae99SBarry Smith 
2268*20f4b53cSBarry Smith   Note:
2269*20f4b53cSBarry Smith   If no refinement was done, the return value is `NULL`
2270*20f4b53cSBarry Smith 
2271bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
227247c6ae99SBarry Smith @*/
2273d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefine(DM dm, MPI_Comm comm, DM *dmf)
2274d71ae5a4SJacob Faibussowitsch {
2275c833c3b5SJed Brown   DMRefineHookLink link;
227647c6ae99SBarry Smith 
227747c6ae99SBarry Smith   PetscFunctionBegin;
2278732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22799566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Refine, dm, 0, 0, 0));
2280dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, refine, comm, dmf);
22814057135bSMatthew G Knepley   if (*dmf) {
228243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
22838865f1eaSKarl Rupp 
22849566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmf));
22858865f1eaSKarl Rupp 
2286644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
22870598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2288656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
22898865f1eaSKarl Rupp 
22909566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmf, dm->mattype));
2291c833c3b5SJed Brown     for (link = dm->refinehook; link; link = link->next) {
22921baa6e33SBarry Smith       if (link->refinehook) PetscCall((*link->refinehook)(dm, *dmf, link->ctx));
2293c833c3b5SJed Brown     }
2294c833c3b5SJed Brown   }
22959566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Refine, dm, 0, 0, 0));
22963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2297c833c3b5SJed Brown }
2298c833c3b5SJed Brown 
2299bb9467b5SJed Brown /*@C
2300c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2301c833c3b5SJed Brown 
2302*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
2303c833c3b5SJed Brown 
23044165533cSJose E. Roman    Input Parameters:
2305bb7acecfSBarry Smith +  coarse - `DM` on which to run a hook when interpolating to a finer level
2306bb7acecfSBarry Smith .  refinehook - function to run when setting up the finer level
2307bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels (once per `SNESSolve`())
2308*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2309c833c3b5SJed Brown 
2310*20f4b53cSBarry Smith    Calling sequence of `refinehook`:
2311*20f4b53cSBarry Smith $  PetscErrorCode refinehook(DM coarse, DM fine, void *ctx);
2312bb7acecfSBarry Smith +  coarse - coarse level `DM`
2313bb7acecfSBarry Smith .  fine - fine level `DM` to interpolate problem to
2314c833c3b5SJed Brown -  ctx - optional user-defined function context
2315c833c3b5SJed Brown 
2316*20f4b53cSBarry Smith    Calling sequence of `interphook`:
2317*20f4b53cSBarry Smith $  PetscErrorCode interphook(DM coarse, Mat interp, DM fine,void *ctx)
2318bb7acecfSBarry Smith +  coarse - coarse level `DM`
2319c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2320bb7acecfSBarry Smith .  fine - fine level `DM` to update
2321c833c3b5SJed Brown -  ctx - optional user-defined function context
2322c833c3b5SJed Brown 
2323c833c3b5SJed Brown    Level: advanced
2324c833c3b5SJed Brown 
2325c833c3b5SJed Brown    Notes:
2326bb7acecfSBarry Smith    This function is only needed if auxiliary data that is attached to the `DM`s via, for example, `PetscObjectCompose()`, needs to be
2327bb7acecfSBarry Smith    passed to fine grids while grid sequencing.
2328bb7acecfSBarry Smith 
2329bb7acecfSBarry Smith    The actual interpolation is done when `DMInterpolate()` is called.
2330c833c3b5SJed Brown 
2331c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2332c833c3b5SJed Brown 
2333bb7acecfSBarry Smith .seealso: `DM`, `DMCoarsenHookAdd()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2334c833c3b5SJed Brown @*/
2335d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookAdd(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2336d71ae5a4SJacob Faibussowitsch {
2337c833c3b5SJed Brown   DMRefineHookLink link, *p;
2338c833c3b5SJed Brown 
2339c833c3b5SJed Brown   PetscFunctionBegin;
2340c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23413d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
23423ba16761SJacob Faibussowitsch     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
23433d8e3701SJed Brown   }
23449566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2345c833c3b5SJed Brown   link->refinehook = refinehook;
2346c833c3b5SJed Brown   link->interphook = interphook;
2347c833c3b5SJed Brown   link->ctx        = ctx;
23480298fd71SBarry Smith   link->next       = NULL;
2349c833c3b5SJed Brown   *p               = link;
23503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2351c833c3b5SJed Brown }
2352c833c3b5SJed Brown 
23533d8e3701SJed Brown /*@C
2354bb7acecfSBarry Smith    DMRefineHookRemove - remove a callback from the list of hooks, that have been set with `DMRefineHookAdd()`, to be run when interpolating
2355bb7acecfSBarry Smith     a nonlinear problem to a finer grid
23563d8e3701SJed Brown 
2357*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
23583d8e3701SJed Brown 
23594165533cSJose E. Roman    Input Parameters:
2360bb7acecfSBarry Smith +  coarse - the `DM` on which to run a hook when restricting to a coarser level
2361bb7acecfSBarry Smith .  refinehook - function to run when setting up a finer level
2362bb7acecfSBarry Smith .  interphook - function to run to update data on finer levels
2363*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
23643d8e3701SJed Brown 
23653d8e3701SJed Brown    Level: advanced
23663d8e3701SJed Brown 
2367bb7acecfSBarry Smith    Note:
23683d8e3701SJed Brown    This function does nothing if the hook is not in the list.
23693d8e3701SJed Brown 
2370bb7acecfSBarry Smith .seealso: `DMRefineHookAdd()`, `DMCoarsenHookRemove()`, `DMInterpolate()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
23713d8e3701SJed Brown @*/
2372d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHookRemove(DM coarse, PetscErrorCode (*refinehook)(DM, DM, void *), PetscErrorCode (*interphook)(DM, Mat, DM, void *), void *ctx)
2373d71ae5a4SJacob Faibussowitsch {
23743d8e3701SJed Brown   DMRefineHookLink link, *p;
23753d8e3701SJed Brown 
23763d8e3701SJed Brown   PetscFunctionBegin;
23773d8e3701SJed Brown   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
23783d8e3701SJed Brown   for (p = &coarse->refinehook; *p; p = &(*p)->next) { /* Search the list of current hooks */
23793d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
23803d8e3701SJed Brown       link = *p;
23813d8e3701SJed Brown       *p   = link->next;
23829566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
23833d8e3701SJed Brown       break;
23843d8e3701SJed Brown     }
23853d8e3701SJed Brown   }
23863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23873d8e3701SJed Brown }
23883d8e3701SJed Brown 
2389c833c3b5SJed Brown /*@
2390bb7acecfSBarry Smith    DMInterpolate - interpolates user-defined problem data attached to a `DM` to a finer `DM` by running hooks registered by `DMRefineHookAdd()`
2391c833c3b5SJed Brown 
2392c833c3b5SJed Brown    Collective if any hooks are
2393c833c3b5SJed Brown 
23944165533cSJose E. Roman    Input Parameters:
2395bb7acecfSBarry Smith +  coarse - coarser `DM` to use as a base
2396bb7acecfSBarry Smith .  interp - interpolation matrix, apply using `MatInterpolate()`
2397bb7acecfSBarry Smith -  fine - finer `DM` to update
2398c833c3b5SJed Brown 
2399c833c3b5SJed Brown    Level: developer
2400c833c3b5SJed Brown 
2401bb7acecfSBarry Smith    Developer Note:
2402bb7acecfSBarry Smith    This routine is called `DMInterpolate()` while the hook is called `DMRefineHookAdd()`. It would be better to have an
2403bb7acecfSBarry Smith    an API with consistent terminology.
2404bb7acecfSBarry Smith 
2405bb7acecfSBarry Smith .seealso: `DM`, `DMRefineHookAdd()`, `MatInterpolate()`
2406c833c3b5SJed Brown @*/
2407d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolate(DM coarse, Mat interp, DM fine)
2408d71ae5a4SJacob Faibussowitsch {
2409c833c3b5SJed Brown   DMRefineHookLink link;
2410c833c3b5SJed Brown 
2411c833c3b5SJed Brown   PetscFunctionBegin;
2412c833c3b5SJed Brown   for (link = fine->refinehook; link; link = link->next) {
24131baa6e33SBarry Smith     if (link->interphook) PetscCall((*link->interphook)(coarse, interp, fine, link->ctx));
24144057135bSMatthew G Knepley   }
24153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
241647c6ae99SBarry Smith }
241747c6ae99SBarry Smith 
2418eb3f98d2SBarry Smith /*@
24191f3379b2SToby Isaac    DMInterpolateSolution - Interpolates a solution from a coarse mesh to a fine mesh.
24201f3379b2SToby Isaac 
2421*20f4b53cSBarry Smith    Collective
24221f3379b2SToby Isaac 
24234165533cSJose E. Roman    Input Parameters:
2424bb7acecfSBarry Smith +  coarse - coarse `DM`
2425bb7acecfSBarry Smith .  fine   - fine `DM`
2426bb7acecfSBarry Smith .  interp - (optional) the matrix computed by `DMCreateInterpolation()`.  Implementations may not need this, but if it
2427bb7acecfSBarry Smith             is available it can avoid some recomputation.  If it is provided, `MatInterpolate()` will be used if
2428bb7acecfSBarry Smith             the coarse `DM` does not have a specialized implementation.
24291f3379b2SToby Isaac -  coarseSol - solution on the coarse mesh
24301f3379b2SToby Isaac 
24314165533cSJose E. Roman    Output Parameter:
24321f3379b2SToby Isaac .  fineSol - the interpolation of coarseSol to the fine mesh
24331f3379b2SToby Isaac 
24341f3379b2SToby Isaac    Level: developer
24351f3379b2SToby Isaac 
2436bb7acecfSBarry Smith    Note:
2437bb7acecfSBarry Smith    This function exists because the interpolation of a solution vector between meshes is not always a linear
24381f3379b2SToby Isaac    map.  For example, if a boundary value problem has an inhomogeneous Dirichlet boundary condition that is compressed
24391f3379b2SToby Isaac    out of the solution vector.  Or if interpolation is inherently a nonlinear operation, such as a method using
24401f3379b2SToby Isaac    slope-limiting reconstruction.
24411f3379b2SToby Isaac 
2442bb7acecfSBarry Smith    Developer Note:
2443bb7acecfSBarry Smith    This doesn't just interpolate "solutions" so its API name is questionable.
2444bb7acecfSBarry Smith 
2445bb7acecfSBarry Smith .seealso: `DMInterpolate()`, `DMCreateInterpolation()`
24461f3379b2SToby Isaac @*/
2447d71ae5a4SJacob Faibussowitsch PetscErrorCode DMInterpolateSolution(DM coarse, DM fine, Mat interp, Vec coarseSol, Vec fineSol)
2448d71ae5a4SJacob Faibussowitsch {
24491f3379b2SToby Isaac   PetscErrorCode (*interpsol)(DM, DM, Mat, Vec, Vec) = NULL;
24501f3379b2SToby Isaac 
24511f3379b2SToby Isaac   PetscFunctionBegin;
24521f3379b2SToby Isaac   PetscValidHeaderSpecific(coarse, DM_CLASSID, 1);
24531f3379b2SToby Isaac   if (interp) PetscValidHeaderSpecific(interp, MAT_CLASSID, 3);
24541f3379b2SToby Isaac   PetscValidHeaderSpecific(coarseSol, VEC_CLASSID, 4);
24551f3379b2SToby Isaac   PetscValidHeaderSpecific(fineSol, VEC_CLASSID, 5);
24561f3379b2SToby Isaac 
24579566063dSJacob Faibussowitsch   PetscCall(PetscObjectQueryFunction((PetscObject)coarse, "DMInterpolateSolution_C", &interpsol));
24581f3379b2SToby Isaac   if (interpsol) {
24599566063dSJacob Faibussowitsch     PetscCall((*interpsol)(coarse, fine, interp, coarseSol, fineSol));
24601f3379b2SToby Isaac   } else if (interp) {
24619566063dSJacob Faibussowitsch     PetscCall(MatInterpolate(interp, coarseSol, fineSol));
246298921bdaSJacob Faibussowitsch   } else SETERRQ(PetscObjectComm((PetscObject)coarse), PETSC_ERR_SUP, "DM %s does not implement DMInterpolateSolution()", ((PetscObject)coarse)->type_name);
24633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24641f3379b2SToby Isaac }
24651f3379b2SToby Isaac 
24661f3379b2SToby Isaac /*@
2467bb7acecfSBarry Smith     DMGetRefineLevel - Gets the number of refinements that have generated this `DM` from some initial `DM`.
2468eb3f98d2SBarry Smith 
2469eb3f98d2SBarry Smith     Not Collective
2470eb3f98d2SBarry Smith 
2471eb3f98d2SBarry Smith     Input Parameter:
2472bb7acecfSBarry Smith .   dm - the `DM` object
2473eb3f98d2SBarry Smith 
2474eb3f98d2SBarry Smith     Output Parameter:
2475eb3f98d2SBarry Smith .   level - number of refinements
2476eb3f98d2SBarry Smith 
2477eb3f98d2SBarry Smith     Level: developer
2478eb3f98d2SBarry Smith 
2479bb7acecfSBarry Smith     Note:
2480bb7acecfSBarry Smith     This can be used, by example, to set the number of coarser levels associated with this `DM` for a multigrid solver.
2481bb7acecfSBarry Smith 
2482bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2483eb3f98d2SBarry Smith @*/
2484d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRefineLevel(DM dm, PetscInt *level)
2485d71ae5a4SJacob Faibussowitsch {
2486eb3f98d2SBarry Smith   PetscFunctionBegin;
2487eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2488eb3f98d2SBarry Smith   *level = dm->levelup;
24893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2490eb3f98d2SBarry Smith }
2491eb3f98d2SBarry Smith 
2492fef3a512SBarry Smith /*@
2493bb7acecfSBarry Smith     DMSetRefineLevel - Sets the number of refinements that have generated this `DM`.
2494fef3a512SBarry Smith 
2495fef3a512SBarry Smith     Not Collective
2496fef3a512SBarry Smith 
2497d8d19677SJose E. Roman     Input Parameters:
2498bb7acecfSBarry Smith +   dm - the `DM` object
2499fef3a512SBarry Smith -   level - number of refinements
2500fef3a512SBarry Smith 
2501fef3a512SBarry Smith     Level: advanced
2502fef3a512SBarry Smith 
250395452b02SPatrick Sanan     Notes:
2504bb7acecfSBarry Smith     This value is used by `PCMG` to determine how many multigrid levels to use
2505fef3a512SBarry Smith 
2506bb7acecfSBarry Smith     The values are usually set automatically by the process that is causing the refinements of an initial `DM` by calling this routine.
2507bb7acecfSBarry Smith 
2508bb7acecfSBarry Smith .seealso: `DMGetRefineLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
2509fef3a512SBarry Smith @*/
2510d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRefineLevel(DM dm, PetscInt level)
2511d71ae5a4SJacob Faibussowitsch {
2512fef3a512SBarry Smith   PetscFunctionBegin;
2513fef3a512SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2514fef3a512SBarry Smith   dm->levelup = level;
25153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2516fef3a512SBarry Smith }
2517fef3a512SBarry Smith 
2518d410b0cfSMatthew G. Knepley /*@
2519bb7acecfSBarry Smith   DMExtrude - Extrude a `DM` object from a surface
2520d410b0cfSMatthew G. Knepley 
2521*20f4b53cSBarry Smith   Collective
2522d410b0cfSMatthew G. Knepley 
2523f1a722f8SMatthew G. Knepley   Input Parameters:
2524bb7acecfSBarry Smith + dm     - the `DM` object
2525d410b0cfSMatthew G. Knepley - layers - the number of extruded cell layers
2526d410b0cfSMatthew G. Knepley 
2527d410b0cfSMatthew G. Knepley   Output Parameter:
2528*20f4b53cSBarry Smith . dme - the extruded `DM`, or `NULL`
2529d410b0cfSMatthew G. Knepley 
2530d410b0cfSMatthew G. Knepley   Level: developer
2531d410b0cfSMatthew G. Knepley 
2532*20f4b53cSBarry Smith   Note:
2533*20f4b53cSBarry Smith   If no extrusion was done, the return value is `NULL`
2534*20f4b53cSBarry Smith 
2535bb7acecfSBarry Smith .seealso: `DMRefine()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`
2536d410b0cfSMatthew G. Knepley @*/
2537d71ae5a4SJacob Faibussowitsch PetscErrorCode DMExtrude(DM dm, PetscInt layers, DM *dme)
2538d71ae5a4SJacob Faibussowitsch {
2539d410b0cfSMatthew G. Knepley   PetscFunctionBegin;
2540d410b0cfSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2541dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, extrude, layers, dme);
2542d410b0cfSMatthew G. Knepley   if (*dme) {
2543d410b0cfSMatthew G. Knepley     (*dme)->ops->creatematrix = dm->ops->creatematrix;
25449566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dme));
2545d410b0cfSMatthew G. Knepley     (*dme)->ctx = dm->ctx;
25469566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dme, dm->mattype));
2547d410b0cfSMatthew G. Knepley   }
25483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2549d410b0cfSMatthew G. Knepley }
2550d410b0cfSMatthew G. Knepley 
2551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2552d71ae5a4SJacob Faibussowitsch {
2553ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2554ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2555ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2556ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
25573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2558ca3d3a14SMatthew G. Knepley }
2559ca3d3a14SMatthew G. Knepley 
2560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2561d71ae5a4SJacob Faibussowitsch {
2562ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2563ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2564ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2565ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
25663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2567ca3d3a14SMatthew G. Knepley }
2568ca3d3a14SMatthew G. Knepley 
2569ca3d3a14SMatthew G. Knepley /*@
2570bb7acecfSBarry Smith   DMHasBasisTransform - Whether the `DM` employs a basis transformation from functions in global vectors to functions in local vectors
2571ca3d3a14SMatthew G. Knepley 
2572ca3d3a14SMatthew G. Knepley   Input Parameter:
2573*20f4b53cSBarry Smith . dm - The `DM`
2574ca3d3a14SMatthew G. Knepley 
2575ca3d3a14SMatthew G. Knepley   Output Parameter:
2576*20f4b53cSBarry Smith . flg - `PETSC_TRUE` if a basis transformation should be done
2577ca3d3a14SMatthew G. Knepley 
2578ca3d3a14SMatthew G. Knepley   Level: developer
2579ca3d3a14SMatthew G. Knepley 
2580bb7acecfSBarry Smith .seealso: `DM`, `DMPlexGlobalToLocalBasis()`, `DMPlexLocalToGlobalBasis()`, `DMPlexCreateBasisRotation()`
2581ca3d3a14SMatthew G. Knepley @*/
2582d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2583d71ae5a4SJacob Faibussowitsch {
2584ca3d3a14SMatthew G. Knepley   Vec tv;
2585ca3d3a14SMatthew G. Knepley 
2586ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2587ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2588534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
25899566063dSJacob Faibussowitsch   PetscCall(DMGetBasisTransformVec_Internal(dm, &tv));
2590ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
25913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2592ca3d3a14SMatthew G. Knepley }
2593ca3d3a14SMatthew G. Knepley 
2594d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2595d71ae5a4SJacob Faibussowitsch {
2596ca3d3a14SMatthew G. Knepley   PetscSection s, ts;
2597ca3d3a14SMatthew G. Knepley   PetscScalar *ta;
2598ca3d3a14SMatthew G. Knepley   PetscInt     cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2599ca3d3a14SMatthew G. Knepley 
2600ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
26019566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
26029566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
26039566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
26049566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetNumFields(s, &Nf));
26059566063dSJacob Faibussowitsch   PetscCall(DMClone(dm, &dm->transformDM));
26069566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm->transformDM, &ts));
26079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(ts, Nf));
26089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(ts, pStart, pEnd));
2609ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
26109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(s, f, &Nc));
2611ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2612ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2613ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
26149566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(s, p, f, &dof));
2615ca3d3a14SMatthew G. Knepley       if (!dof) continue;
26169566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim)));
26179566063dSJacob Faibussowitsch       PetscCall(PetscSectionAddDof(ts, p, PetscSqr(cdim)));
2618ca3d3a14SMatthew G. Knepley     }
2619ca3d3a14SMatthew G. Knepley   }
26209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(ts));
26219566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(dm->transformDM, &dm->transform));
26229566063dSJacob Faibussowitsch   PetscCall(VecGetArray(dm->transform, &ta));
2623ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2624ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
26259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldDof(ts, p, f, &dof));
2626ca3d3a14SMatthew G. Knepley       if (dof) {
2627ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2628ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2629ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2630ca3d3a14SMatthew G. Knepley 
2631ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
26329566063dSJacob Faibussowitsch         PetscCall((*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx));
26339566063dSJacob Faibussowitsch         PetscCall(DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *)&tva));
26349566063dSJacob Faibussowitsch         PetscCall(PetscArraycpy(tva, A, PetscSqr(cdim)));
2635ca3d3a14SMatthew G. Knepley       }
2636ca3d3a14SMatthew G. Knepley     }
2637ca3d3a14SMatthew G. Knepley   }
26389566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(dm->transform, &ta));
26393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2640ca3d3a14SMatthew G. Knepley }
2641ca3d3a14SMatthew G. Knepley 
2642d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2643d71ae5a4SJacob Faibussowitsch {
2644ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2645ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2646ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2647ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2648ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2649ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2650ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
26519566063dSJacob Faibussowitsch   if (newdm->transformSetUp) PetscCall(DMConstructBasisTransform_Internal(newdm));
26523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2653ca3d3a14SMatthew G. Knepley }
2654ca3d3a14SMatthew G. Knepley 
2655bb9467b5SJed Brown /*@C
2656bb7acecfSBarry Smith    DMGlobalToLocalHookAdd - adds a callback to be run when `DMGlobalToLocal()` is called
2657baf369e7SPeter Brune 
2658*20f4b53cSBarry Smith    Logically Collective
2659baf369e7SPeter Brune 
26604165533cSJose E. Roman    Input Parameters:
2661bb7acecfSBarry Smith +  dm - the `DM`
2662bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMGlobalToLocalBegin()`
2663bb7acecfSBarry Smith .  endhook - function to run after `DMGlobalToLocalEnd()` has completed
2664*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2665baf369e7SPeter Brune 
2666*20f4b53cSBarry Smith    Calling sequence of `beginhook`:
2667*20f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2668baf369e7SPeter Brune +  dm - global DM
2669baf369e7SPeter Brune .  g - global vector
2670baf369e7SPeter Brune .  mode - mode
2671baf369e7SPeter Brune .  l - local vector
2672baf369e7SPeter Brune -  ctx - optional user-defined function context
2673baf369e7SPeter Brune 
2674*20f4b53cSBarry Smith    Calling sequence of `endhook`:
2675*20f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, VecScatter out, VecScatter in, DM coarse, void *ctx)
2676baf369e7SPeter Brune +  global - global DM
2677baf369e7SPeter Brune -  ctx - optional user-defined function context
2678baf369e7SPeter Brune 
2679baf369e7SPeter Brune    Level: advanced
2680baf369e7SPeter Brune 
2681bb7acecfSBarry Smith    Note:
2682bb7acecfSBarry 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.
2683bb7acecfSBarry Smith 
2684bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocal()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2685baf369e7SPeter Brune @*/
2686d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2687d71ae5a4SJacob Faibussowitsch {
2688baf369e7SPeter Brune   DMGlobalToLocalHookLink link, *p;
2689baf369e7SPeter Brune 
2690baf369e7SPeter Brune   PetscFunctionBegin;
2691baf369e7SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2692baf369e7SPeter Brune   for (p = &dm->gtolhook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
26939566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2694baf369e7SPeter Brune   link->beginhook = beginhook;
2695baf369e7SPeter Brune   link->endhook   = endhook;
2696baf369e7SPeter Brune   link->ctx       = ctx;
26970298fd71SBarry Smith   link->next      = NULL;
2698baf369e7SPeter Brune   *p              = link;
26993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2700baf369e7SPeter Brune }
2701baf369e7SPeter Brune 
2702d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2703d71ae5a4SJacob Faibussowitsch {
27044c274da1SToby Isaac   Mat          cMat;
270579769bd5SJed Brown   Vec          cVec, cBias;
27064c274da1SToby Isaac   PetscSection section, cSec;
27074c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
27084c274da1SToby Isaac 
27094c274da1SToby Isaac   PetscFunctionBegin;
27104c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
27119566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, &cBias));
27124c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
27135db9a05bSToby Isaac     PetscInt nRows;
27145db9a05bSToby Isaac 
27159566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
27163ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
27179566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
27189566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
27199566063dSJacob Faibussowitsch     PetscCall(MatMult(cMat, l, cVec));
27209566063dSJacob Faibussowitsch     if (cBias) PetscCall(VecAXPY(cVec, 1., cBias));
27219566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
27224c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
27239566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
27244c274da1SToby Isaac       if (dof) {
27254c274da1SToby Isaac         PetscScalar *vals;
27269566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(cVec, cSec, p, &vals));
27279566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(l, section, p, vals, INSERT_ALL_VALUES));
27284c274da1SToby Isaac       }
27294c274da1SToby Isaac     }
27309566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
27314c274da1SToby Isaac   }
27323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27334c274da1SToby Isaac }
27344c274da1SToby Isaac 
273547c6ae99SBarry Smith /*@
273601729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
273701729b5cSPatrick Sanan 
2738*20f4b53cSBarry Smith     Neighbor-wise Collective
273901729b5cSPatrick Sanan 
274001729b5cSPatrick Sanan     Input Parameters:
2741bb7acecfSBarry Smith +   dm - the `DM` object
274201729b5cSPatrick Sanan .   g - the global vector
2743bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
274401729b5cSPatrick Sanan -   l - the local vector
274501729b5cSPatrick Sanan 
2746*20f4b53cSBarry Smith     Level: beginner
2747*20f4b53cSBarry Smith 
274801729b5cSPatrick Sanan     Notes:
2749bb7acecfSBarry Smith     The communication involved in this update can be overlapped with computation by instead using
2750bb7acecfSBarry Smith     `DMGlobalToLocalBegin()` and `DMGlobalToLocalEnd()`.
2751bb7acecfSBarry Smith 
2752bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
275301729b5cSPatrick Sanan 
2754bb7acecfSBarry Smith .seealso: `DM`, `DMGlobalToLocalHookAdd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`,
2755bb7acecfSBarry Smith           `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`,
2756bb7acecfSBarry Smith           `DMGlobalToLocalBegin()` `DMGlobalToLocalEnd()`
275701729b5cSPatrick Sanan @*/
2758d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocal(DM dm, Vec g, InsertMode mode, Vec l)
2759d71ae5a4SJacob Faibussowitsch {
276001729b5cSPatrick Sanan   PetscFunctionBegin;
27619566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(dm, g, mode, l));
27629566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(dm, g, mode, l));
27633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
276401729b5cSPatrick Sanan }
276501729b5cSPatrick Sanan 
276601729b5cSPatrick Sanan /*@
276747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
276847c6ae99SBarry Smith 
2769*20f4b53cSBarry Smith     Neighbor-wise Collective
277047c6ae99SBarry Smith 
277147c6ae99SBarry Smith     Input Parameters:
2772bb7acecfSBarry Smith +   dm - the `DM` object
277347c6ae99SBarry Smith .   g - the global vector
2774bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
277547c6ae99SBarry Smith -   l - the local vector
277647c6ae99SBarry Smith 
277701729b5cSPatrick Sanan     Level: intermediate
277847c6ae99SBarry Smith 
2779bb7acecfSBarry Smith     Notes:
2780bb7acecfSBarry Smith     The operation is completed with `DMGlobalToLocalEnd()`
2781bb7acecfSBarry Smith 
2782bb7acecfSBarry Smith     One can perform local computations between the `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()` to overlap communication and computation
2783bb7acecfSBarry Smith 
2784bb7acecfSBarry Smith     `DMGlobalToLocal()` is a short form of  `DMGlobalToLocalBegin()` and  `DMGlobalToLocalEnd()`
2785bb7acecfSBarry Smith 
2786bb7acecfSBarry Smith     `DMGlobalToLocalHookAdd()` may be used to provide additional operations that are performed during the update process.
2787bb7acecfSBarry Smith 
2788bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
278947c6ae99SBarry Smith @*/
2790d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
2791d71ae5a4SJacob Faibussowitsch {
27927128ae9fSMatthew G Knepley   PetscSF                 sf;
2793baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
279447c6ae99SBarry Smith 
279547c6ae99SBarry Smith   PetscFunctionBegin;
2796171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2797baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
27981baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, g, mode, l, link->ctx));
2799baf369e7SPeter Brune   }
28009566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28017128ae9fSMatthew G Knepley   if (sf) {
2802ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2803ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2804d0295fc0SJunchao Zhang     PetscMemType       lmtype, gmtype;
28057128ae9fSMatthew G Knepley 
28067a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28079566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28089566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28099566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray, MPI_REPLACE));
28109566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28119566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28127128ae9fSMatthew G Knepley   } else {
28139566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalbegin)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28147128ae9fSMatthew G Knepley   }
28153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
281647c6ae99SBarry Smith }
281747c6ae99SBarry Smith 
281847c6ae99SBarry Smith /*@
281947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
282047c6ae99SBarry Smith 
2821*20f4b53cSBarry Smith     Neighbor-wise Collective
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith     Input Parameters:
2824bb7acecfSBarry Smith +   dm - the `DM` object
282547c6ae99SBarry Smith .   g - the global vector
2826bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
282747c6ae99SBarry Smith -   l - the local vector
282847c6ae99SBarry Smith 
282901729b5cSPatrick Sanan     Level: intermediate
283047c6ae99SBarry Smith 
2831bb7acecfSBarry Smith     Note:
2832bb7acecfSBarry Smith     See `DMGlobalToLocalBegin()` for details.
2833bb7acecfSBarry Smith 
2834bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobal()`, `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`
283547c6ae99SBarry Smith @*/
2836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGlobalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
2837d71ae5a4SJacob Faibussowitsch {
28387128ae9fSMatthew G Knepley   PetscSF                 sf;
2839ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2840ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2841ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2842baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2843d0295fc0SJunchao Zhang   PetscMemType            lmtype, gmtype;
284447c6ae99SBarry Smith 
284547c6ae99SBarry Smith   PetscFunctionBegin;
2846171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28479566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
28489566063dSJacob Faibussowitsch   PetscCall(DMHasBasisTransform(dm, &transform));
28497128ae9fSMatthew G Knepley   if (sf) {
28507a8be351SBarry Smith     PetscCheck(mode != ADD_VALUES, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", (int)mode);
28517128ae9fSMatthew G Knepley 
28529566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(l, &lArray, &lmtype));
28539566063dSJacob Faibussowitsch     PetscCall(VecGetArrayReadAndMemType(g, &gArray, &gmtype));
28549566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray, MPI_REPLACE));
28559566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(l, &lArray));
28569566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayReadAndMemType(g, &gArray));
28579566063dSJacob Faibussowitsch     if (transform) PetscCall(DMPlexGlobalToLocalBasis(dm, l));
28587128ae9fSMatthew G Knepley   } else {
28599566063dSJacob Faibussowitsch     PetscCall((*dm->ops->globaltolocalend)(dm, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l));
28607128ae9fSMatthew G Knepley   }
28619566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalHook_Constraints(dm, g, mode, l, NULL));
2862baf369e7SPeter Brune   for (link = dm->gtolhook; link; link = link->next) {
28639566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
2864baf369e7SPeter Brune   }
28653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
286647c6ae99SBarry Smith }
286747c6ae99SBarry Smith 
2868d4d07f1eSToby Isaac /*@C
2869d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2870d4d07f1eSToby Isaac 
2871*20f4b53cSBarry Smith    Logically Collective
2872d4d07f1eSToby Isaac 
28734165533cSJose E. Roman    Input Parameters:
2874bb7acecfSBarry Smith +  dm - the `DM`
2875bb7acecfSBarry Smith .  beginhook - function to run at the beginning of `DMLocalToGlobalBegin()`
2876bb7acecfSBarry Smith .  endhook - function to run after `DMLocalToGlobalEnd()` has completed
2877*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
2878d4d07f1eSToby Isaac 
2879*20f4b53cSBarry Smith    Calling sequence of `beginhook`:
2880*20f4b53cSBarry Smith $  PetscErrorCode  beginhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2881bb7acecfSBarry Smith +  dm - global `DM`
2882d4d07f1eSToby Isaac .  l - local vector
2883d4d07f1eSToby Isaac .  mode - mode
2884d4d07f1eSToby Isaac .  g - global vector
2885d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2886d4d07f1eSToby Isaac 
2887*20f4b53cSBarry Smith    Calling sequence of `endhook`:
2888*20f4b53cSBarry Smith $  PetscErrorCode  endhook(DM fine, Vec l, InsertMode mode, Vec g, void *ctx)
2889bb7acecfSBarry Smith +  global - global `DM`
2890d4d07f1eSToby Isaac .  l - local vector
2891d4d07f1eSToby Isaac .  mode - mode
2892d4d07f1eSToby Isaac .  g - global vector
2893d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2894d4d07f1eSToby Isaac 
2895d4d07f1eSToby Isaac    Level: advanced
2896d4d07f1eSToby Isaac 
2897bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMRefineHookAdd()`, `DMGlobalToLocalHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
2898d4d07f1eSToby Isaac @*/
2899d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalHookAdd(DM dm, PetscErrorCode (*beginhook)(DM, Vec, InsertMode, Vec, void *), PetscErrorCode (*endhook)(DM, Vec, InsertMode, Vec, void *), void *ctx)
2900d71ae5a4SJacob Faibussowitsch {
2901d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link, *p;
2902d4d07f1eSToby Isaac 
2903d4d07f1eSToby Isaac   PetscFunctionBegin;
2904d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2905d4d07f1eSToby Isaac   for (p = &dm->ltoghook; *p; p = &(*p)->next) { } /* Scan to the end of the current list of hooks */
29069566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
2907d4d07f1eSToby Isaac   link->beginhook = beginhook;
2908d4d07f1eSToby Isaac   link->endhook   = endhook;
2909d4d07f1eSToby Isaac   link->ctx       = ctx;
2910d4d07f1eSToby Isaac   link->next      = NULL;
2911d4d07f1eSToby Isaac   *p              = link;
29123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2913d4d07f1eSToby Isaac }
2914d4d07f1eSToby Isaac 
2915d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2916d71ae5a4SJacob Faibussowitsch {
29174c274da1SToby Isaac   Mat          cMat;
29184c274da1SToby Isaac   Vec          cVec;
29194c274da1SToby Isaac   PetscSection section, cSec;
29204c274da1SToby Isaac   PetscInt     pStart, pEnd, p, dof;
29214c274da1SToby Isaac 
29224c274da1SToby Isaac   PetscFunctionBegin;
29234c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29249566063dSJacob Faibussowitsch   PetscCall(DMGetDefaultConstraints(dm, &cSec, &cMat, NULL));
29254c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
29265db9a05bSToby Isaac     PetscInt nRows;
29275db9a05bSToby Isaac 
29289566063dSJacob Faibussowitsch     PetscCall(MatGetSize(cMat, &nRows, NULL));
29293ba16761SJacob Faibussowitsch     if (nRows <= 0) PetscFunctionReturn(PETSC_SUCCESS);
29309566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
29319566063dSJacob Faibussowitsch     PetscCall(MatCreateVecs(cMat, NULL, &cVec));
29329566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(cSec, &pStart, &pEnd));
29334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
29349566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(cSec, p, &dof));
29354c274da1SToby Isaac       if (dof) {
29364c274da1SToby Isaac         PetscInt     d;
29374c274da1SToby Isaac         PetscScalar *vals;
29389566063dSJacob Faibussowitsch         PetscCall(VecGetValuesSection(l, section, p, &vals));
29399566063dSJacob Faibussowitsch         PetscCall(VecSetValuesSection(cVec, cSec, p, vals, mode));
29404c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
29414c274da1SToby Isaac          * we just extracted */
2942ad540459SPierre Jolivet         for (d = 0; d < dof; d++) vals[d] = 0.;
29434c274da1SToby Isaac       }
29444c274da1SToby Isaac     }
29459566063dSJacob Faibussowitsch     PetscCall(MatMultTransposeAdd(cMat, cVec, l, l));
29469566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&cVec));
29474c274da1SToby Isaac   }
29483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29494c274da1SToby Isaac }
295001729b5cSPatrick Sanan /*@
295101729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
295201729b5cSPatrick Sanan 
2953*20f4b53cSBarry Smith     Neighbor-wise Collective
295401729b5cSPatrick Sanan 
295501729b5cSPatrick Sanan     Input Parameters:
2956bb7acecfSBarry Smith +   dm - the `DM` object
295701729b5cSPatrick Sanan .   l - the local vector
2958bb7acecfSBarry 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.
295901729b5cSPatrick Sanan -   g - the global vector
296001729b5cSPatrick Sanan 
2961*20f4b53cSBarry Smith     Level: beginner
2962*20f4b53cSBarry Smith 
296301729b5cSPatrick Sanan     Notes:
296401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
2965bb7acecfSBarry Smith     `DMLocalToGlobalBegin()` and `DMLocalToGlobalEnd()`.
296601729b5cSPatrick Sanan 
2967bb7acecfSBarry Smith     In the `ADD_VALUES` case you normally would zero the receiving vector before beginning this operation.
2968bb7acecfSBarry Smith 
2969bb7acecfSBarry 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.
2970bb7acecfSBarry Smith 
2971bb7acecfSBarry Smith     Use `DMLocalToGlobalHookAdd()` to add additional operations that are performed on the data during the update process
297201729b5cSPatrick Sanan 
2973bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`, `DMLocalToGlobalHookAdd()`, `DMGlobaToLocallHookAdd()`
297401729b5cSPatrick Sanan @*/
2975d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobal(DM dm, Vec l, InsertMode mode, Vec g)
2976d71ae5a4SJacob Faibussowitsch {
297701729b5cSPatrick Sanan   PetscFunctionBegin;
29789566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, l, mode, g));
29799566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, l, mode, g));
29803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
298101729b5cSPatrick Sanan }
29824c274da1SToby Isaac 
298347c6ae99SBarry Smith /*@
298401729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
29859a42bb27SBarry Smith 
2986*20f4b53cSBarry Smith     Neighbor-wise Collective
29879a42bb27SBarry Smith 
29889a42bb27SBarry Smith     Input Parameters:
2989bb7acecfSBarry Smith +   dm - the `DM` object
2990f6813fd5SJed Brown .   l - the local vector
2991aa624791SPierre Jolivet .   mode - if `INSERT_VALUES` then no parallel communication is used, if `ADD_VALUES` then all ghost points from the same base point accumulate into that base point.
29921eb28f2eSBarry Smith -   g - the global vector
29939a42bb27SBarry Smith 
2994*20f4b53cSBarry Smith     Level: intermediate
2995*20f4b53cSBarry Smith 
299695452b02SPatrick Sanan     Notes:
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 `DMLocalToGlobalEnd()` to complete the communication process.
3002bb7acecfSBarry Smith 
3003bb7acecfSBarry Smith     `DMLocalToGlobal()` is a short form of  `DMLocalToGlobalBegin()` and  `DMLocalToGlobalEnd()`
3004bb7acecfSBarry Smith 
3005bb7acecfSBarry Smith     `DMLocalToGlobalHookAdd()` may be used to provide additional operations that are performed during the update process.
30069a42bb27SBarry Smith 
3007bb7acecfSBarry Smith .seealso: `DMLocalToGlobal()`, `DMLocalToGlobalEnd()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocal()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalBegin()`
30089a42bb27SBarry Smith @*/
3009d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalBegin(DM dm, Vec l, InsertMode mode, Vec g)
3010d71ae5a4SJacob Faibussowitsch {
30117128ae9fSMatthew G Knepley   PetscSF                 sf;
301284330215SMatthew G. Knepley   PetscSection            s, gs;
3013d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3014ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
3015ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
3016ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
3017fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
3018d0295fc0SJunchao Zhang   PetscMemType            lmtype = PETSC_MEMTYPE_HOST, gmtype = PETSC_MEMTYPE_HOST;
30199a42bb27SBarry Smith 
30209a42bb27SBarry Smith   PetscFunctionBegin;
3021171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3022d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
30231baa6e33SBarry Smith     if (link->beginhook) PetscCall((*link->beginhook)(dm, l, mode, g, link->ctx));
3024d4d07f1eSToby Isaac   }
30259566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalHook_Constraints(dm, l, mode, g, NULL));
30269566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
30279566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
30287128ae9fSMatthew G Knepley   switch (mode) {
30297128ae9fSMatthew G Knepley   case INSERT_VALUES:
30307128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
3031d71ae5a4SJacob Faibussowitsch   case INSERT_BC_VALUES:
3032d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3033d71ae5a4SJacob Faibussowitsch     break;
30347128ae9fSMatthew G Knepley   case ADD_VALUES:
30357128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
3036d71ae5a4SJacob Faibussowitsch   case ADD_BC_VALUES:
3037d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3038d71ae5a4SJacob Faibussowitsch     break;
3039d71ae5a4SJacob Faibussowitsch   default:
3040d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
30417128ae9fSMatthew G Knepley   }
3042ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
30439566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3044ca3d3a14SMatthew G. Knepley     if (transform) {
30459566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
30469566063dSJacob Faibussowitsch       PetscCall(VecCopy(l, tmpl));
30479566063dSJacob Faibussowitsch       PetscCall(DMPlexLocalToGlobalBasis(dm, tmpl));
30489566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3049fa88e482SJed Brown     } else if (isInsert) {
30509566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(l, &lArray));
3051fa88e482SJed Brown     } else {
30529566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, &lmtype));
3053fa88e482SJed Brown       l_inplace = PETSC_TRUE;
3054ca3d3a14SMatthew G. Knepley     }
3055fa88e482SJed Brown     if (s && isInsert) {
30569566063dSJacob Faibussowitsch       PetscCall(VecGetArray(g, &gArray));
3057fa88e482SJed Brown     } else {
30589566063dSJacob Faibussowitsch       PetscCall(VecGetArrayAndMemType(g, &gArray, &gmtype));
3059fa88e482SJed Brown       g_inplace = PETSC_TRUE;
3060fa88e482SJed Brown     }
3061ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
30629566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM));
306384330215SMatthew G. Knepley     } else if (s && isInsert) {
306484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
306584330215SMatthew G. Knepley 
30669566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gs));
30679566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetChart(s, &pStart, &pEnd));
30689566063dSJacob Faibussowitsch       PetscCall(VecGetOwnershipRange(g, &gStart, NULL));
306984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
3070b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
307184330215SMatthew G. Knepley 
30729566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(s, p, &dof));
30739566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(gs, p, &gdof));
30749566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(s, p, &cdof));
30759566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetConstraintDof(gs, p, &gcdof));
30769566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(s, p, &off));
30779566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(gs, p, &goff));
3078b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
307903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
30807a8be351SBarry 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);
3081b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
3082b3b16f48SMatthew G. Knepley         if (!gcdof) {
308384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff - gStart + d] = lArray[off + d];
3084b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
3085b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
308684330215SMatthew G. Knepley           const PetscInt *cdofs;
308784330215SMatthew G. Knepley           PetscInt        cind = 0;
308884330215SMatthew G. Knepley 
30899566063dSJacob Faibussowitsch           PetscCall(PetscSectionGetConstraintIndices(s, p, &cdofs));
3090b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
30919371c9d4SSatish Balay             if ((cind < cdof) && (d == cdofs[cind])) {
30929371c9d4SSatish Balay               ++cind;
30939371c9d4SSatish Balay               continue;
30949371c9d4SSatish Balay             }
3095b3b16f48SMatthew G. Knepley             gArray[goff - gStart + e++] = lArray[off + d];
309684330215SMatthew G. Knepley           }
30977a8be351SBarry 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);
309884330215SMatthew G. Knepley       }
3099ca3d3a14SMatthew G. Knepley     }
3100fa88e482SJed Brown     if (g_inplace) {
31019566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayAndMemType(g, &gArray));
3102fa88e482SJed Brown     } else {
31039566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(g, &gArray));
3104fa88e482SJed Brown     }
3105ca3d3a14SMatthew G. Knepley     if (transform) {
31069566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31079566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3108fa88e482SJed Brown     } else if (l_inplace) {
31099566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3110ca3d3a14SMatthew G. Knepley     } else {
31119566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(l, &lArray));
3112ca3d3a14SMatthew G. Knepley     }
31137128ae9fSMatthew G Knepley   } else {
31149566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalbegin)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31157128ae9fSMatthew G Knepley   }
31163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31179a42bb27SBarry Smith }
31189a42bb27SBarry Smith 
31199a42bb27SBarry Smith /*@
31209a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
312147c6ae99SBarry Smith 
3122*20f4b53cSBarry Smith     Neighbor-wise Collective
312347c6ae99SBarry Smith 
312447c6ae99SBarry Smith     Input Parameters:
3125bb7acecfSBarry Smith +   dm - the `DM` object
3126f6813fd5SJed Brown .   l - the local vector
3127bb7acecfSBarry Smith .   mode - `INSERT_VALUES` or `ADD_VALUES`
3128f6813fd5SJed Brown -   g - the global vector
312947c6ae99SBarry Smith 
313001729b5cSPatrick Sanan     Level: intermediate
313147c6ae99SBarry Smith 
3132bb7acecfSBarry Smith     Note:
3133bb7acecfSBarry Smith     See `DMLocalToGlobalBegin()` for full details
3134bb7acecfSBarry Smith 
3135bb7acecfSBarry Smith .seealso: `DMLocalToGlobalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMGlobalToLocalEnd()`, `DMGlobalToLocalEnd()`
313647c6ae99SBarry Smith @*/
3137d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToGlobalEnd(DM dm, Vec l, InsertMode mode, Vec g)
3138d71ae5a4SJacob Faibussowitsch {
31397128ae9fSMatthew G Knepley   PetscSF                 sf;
314084330215SMatthew G. Knepley   PetscSection            s;
3141d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
3142ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
314347c6ae99SBarry Smith 
314447c6ae99SBarry Smith   PetscFunctionBegin;
3145171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31469566063dSJacob Faibussowitsch   PetscCall(DMGetSectionSF(dm, &sf));
31479566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &s));
31487128ae9fSMatthew G Knepley   switch (mode) {
31497128ae9fSMatthew G Knepley   case INSERT_VALUES:
3150d71ae5a4SJacob Faibussowitsch   case INSERT_ALL_VALUES:
3151d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_TRUE;
3152d71ae5a4SJacob Faibussowitsch     break;
31537128ae9fSMatthew G Knepley   case ADD_VALUES:
3154d71ae5a4SJacob Faibussowitsch   case ADD_ALL_VALUES:
3155d71ae5a4SJacob Faibussowitsch     isInsert = PETSC_FALSE;
3156d71ae5a4SJacob Faibussowitsch     break;
3157d71ae5a4SJacob Faibussowitsch   default:
3158d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %d", mode);
31597128ae9fSMatthew G Knepley   }
316084330215SMatthew G. Knepley   if (sf && !isInsert) {
3161ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
3162ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
3163ca3d3a14SMatthew G. Knepley     Vec                tmpl;
316484330215SMatthew G. Knepley 
31659566063dSJacob Faibussowitsch     PetscCall(DMHasBasisTransform(dm, &transform));
3166ca3d3a14SMatthew G. Knepley     if (transform) {
31679566063dSJacob Faibussowitsch       PetscCall(DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
31689566063dSJacob Faibussowitsch       PetscCall(VecGetArrayRead(tmpl, &lArray));
3169ca3d3a14SMatthew G. Knepley     } else {
31709566063dSJacob Faibussowitsch       PetscCall(VecGetArrayReadAndMemType(l, &lArray, NULL));
3171ca3d3a14SMatthew G. Knepley     }
31729566063dSJacob Faibussowitsch     PetscCall(VecGetArrayAndMemType(g, &gArray, NULL));
31739566063dSJacob Faibussowitsch     PetscCall(PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM));
3174ca3d3a14SMatthew G. Knepley     if (transform) {
31759566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayRead(tmpl, &lArray));
31769566063dSJacob Faibussowitsch       PetscCall(DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl));
3177ca3d3a14SMatthew G. Knepley     } else {
31789566063dSJacob Faibussowitsch       PetscCall(VecRestoreArrayReadAndMemType(l, &lArray));
3179ca3d3a14SMatthew G. Knepley     }
31809566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayAndMemType(g, &gArray));
318184330215SMatthew G. Knepley   } else if (s && isInsert) {
31827128ae9fSMatthew G Knepley   } else {
31839566063dSJacob Faibussowitsch     PetscCall((*dm->ops->localtoglobalend)(dm, l, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), g));
31847128ae9fSMatthew G Knepley   }
3185d4d07f1eSToby Isaac   for (link = dm->ltoghook; link; link = link->next) {
31869566063dSJacob Faibussowitsch     if (link->endhook) PetscCall((*link->endhook)(dm, g, mode, l, link->ctx));
3187d4d07f1eSToby Isaac   }
31883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
318947c6ae99SBarry Smith }
319047c6ae99SBarry Smith 
3191f089877aSRichard Tran Mills /*@
3192bb7acecfSBarry Smith    DMLocalToLocalBegin - Begins the process of mapping values from a local vector (that include ghost points
3193bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
3194bb7acecfSBarry Smith    points in the second are set correctly from values on other MPI ranks. Must be followed by `DMLocalToLocalEnd()`.
3195f089877aSRichard Tran Mills 
3196*20f4b53cSBarry Smith    Neighbor-wise Collective
3197f089877aSRichard Tran Mills 
3198f089877aSRichard Tran Mills    Input Parameters:
3199bb7acecfSBarry Smith +  dm - the `DM` object
3200bc0a1609SRichard Tran Mills .  g - the original local vector
3201bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3202f089877aSRichard Tran Mills 
3203bc0a1609SRichard Tran Mills    Output Parameter:
3204bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3205f089877aSRichard Tran Mills 
3206f089877aSRichard Tran Mills    Level: intermediate
3207f089877aSRichard Tran Mills 
3208bb7acecfSBarry Smith .seealso: `DMLocalToLocalEnd(), `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalEnd()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3209f089877aSRichard Tran Mills @*/
3210d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalBegin(DM dm, Vec g, InsertMode mode, Vec l)
3211d71ae5a4SJacob Faibussowitsch {
3212f089877aSRichard Tran Mills   PetscFunctionBegin;
3213f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32149f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32159f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32169f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalbegin, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3218f089877aSRichard Tran Mills }
3219f089877aSRichard Tran Mills 
3220f089877aSRichard Tran Mills /*@
3221bb7acecfSBarry Smith    DMLocalToLocalEnd - Maps from a local vector to another local vector where the ghost
3222bb7acecfSBarry Smith    points in the second are set correctly. Must be preceded by `DMLocalToLocalBegin()`.
3223f089877aSRichard Tran Mills 
3224*20f4b53cSBarry Smith    Neighbor-wise Collective
3225f089877aSRichard Tran Mills 
3226f089877aSRichard Tran Mills    Input Parameters:
3227bb7acecfSBarry Smith +  da - the `DM` object
3228bc0a1609SRichard Tran Mills .  g - the original local vector
3229bb7acecfSBarry Smith -  mode - one of `INSERT_VALUES` or `ADD_VALUES`
3230f089877aSRichard Tran Mills 
3231bc0a1609SRichard Tran Mills    Output Parameter:
3232bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
3233f089877aSRichard Tran Mills 
3234f089877aSRichard Tran Mills    Level: intermediate
3235f089877aSRichard Tran Mills 
3236bb7acecfSBarry Smith .seealso: `DMLocalToLocalBegin()`, `DMCoarsen()`, `DMDestroy()`, `DMView()`, `DMCreateLocalVector()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMLocalToLocalBegin()`, `DMGlobalToLocalEnd()`, `DMLocalToGlobalBegin()`
3237f089877aSRichard Tran Mills @*/
3238d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLocalToLocalEnd(DM dm, Vec g, InsertMode mode, Vec l)
3239d71ae5a4SJacob Faibussowitsch {
3240f089877aSRichard Tran Mills   PetscFunctionBegin;
3241f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32429f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(g, VEC_CLASSID, 2);
32439f4ada15SMatthew G. Knepley   PetscValidHeaderSpecific(l, VEC_CLASSID, 4);
32449f4ada15SMatthew G. Knepley   PetscUseTypeMethod(dm, localtolocalend, g, mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode), l);
32453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3246f089877aSRichard Tran Mills }
3247f089877aSRichard Tran Mills 
324847c6ae99SBarry Smith /*@
3249bb7acecfSBarry Smith     DMCoarsen - Coarsens a `DM` object using a standard, non-adaptive coarsening of the underlying mesh
325047c6ae99SBarry Smith 
3251*20f4b53cSBarry Smith     Collective
325247c6ae99SBarry Smith 
3253d8d19677SJose E. Roman     Input Parameters:
3254bb7acecfSBarry Smith +   dm - the `DM` object
3255*20f4b53cSBarry Smith -   comm - the communicator to contain the new `DM` object (or `MPI_COMM_NULL`)
325647c6ae99SBarry Smith 
325747c6ae99SBarry Smith     Output Parameter:
3258bb7acecfSBarry Smith .   dmc - the coarsened `DM`
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith     Level: developer
326147c6ae99SBarry Smith 
3262bb7acecfSBarry Smith .seealso: `DM`, `DMRefine()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
326347c6ae99SBarry Smith @*/
3264d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
3265d71ae5a4SJacob Faibussowitsch {
3266b17ce1afSJed Brown   DMCoarsenHookLink link;
326747c6ae99SBarry Smith 
326847c6ae99SBarry Smith   PetscFunctionBegin;
3269171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Coarsen, dm, 0, 0, 0));
3271dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, coarsen, comm, dmc);
3272b9d85ea2SLisandro Dalcin   if (*dmc) {
3273a3574896SRichard Tran Mills     (*dmc)->bind_below = dm->bind_below; /* Propagate this from parent DM; otherwise -dm_bind_below will be useless for multigrid cases. */
32749566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dm, *dmc));
327543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
32769566063dSJacob Faibussowitsch     PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)dm, (PetscObject)*dmc));
3277644e2e5bSBarry Smith     (*dmc)->ctx       = dm->ctx;
32780598a293SJed Brown     (*dmc)->levelup   = dm->levelup;
3279656b349aSBarry Smith     (*dmc)->leveldown = dm->leveldown + 1;
32809566063dSJacob Faibussowitsch     PetscCall(DMSetMatType(*dmc, dm->mattype));
3281b17ce1afSJed Brown     for (link = dm->coarsenhook; link; link = link->next) {
32829566063dSJacob Faibussowitsch       if (link->coarsenhook) PetscCall((*link->coarsenhook)(dm, *dmc, link->ctx));
3283b17ce1afSJed Brown     }
3284b9d85ea2SLisandro Dalcin   }
32859566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Coarsen, dm, 0, 0, 0));
32867a8be351SBarry Smith   PetscCheck(*dmc, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
32873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3288b17ce1afSJed Brown }
3289b17ce1afSJed Brown 
3290bb9467b5SJed Brown /*@C
3291b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3292b17ce1afSJed Brown 
3293*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
3294b17ce1afSJed Brown 
32954165533cSJose E. Roman    Input Parameters:
3296bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3297b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3298bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels (called once per `SNESSolve()`)
3299*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3300b17ce1afSJed Brown 
3301*20f4b53cSBarry Smith    Calling sequence of `coarsenhook`:
3302*20f4b53cSBarry Smith $  PetscErrorCode  coarsenhook(DM fine, DM coarse, void *ctx);
3303bb7acecfSBarry Smith +  fine - fine level `DM`
3304bb7acecfSBarry Smith .  coarse - coarse level `DM` to restrict problem to
3305b17ce1afSJed Brown -  ctx - optional user-defined function context
3306b17ce1afSJed Brown 
3307*20f4b53cSBarry Smith    Calling sequence of `restricthook`:
3308*20f4b53cSBarry Smith $  PetscErrorCode  restricthook(DM fine, Mat mrestrict, Vec rscale, Mat inject, DM coarse, void *ctx)
3309bb7acecfSBarry Smith +  fine - fine level `DM`
3310bb7acecfSBarry Smith .  mrestrict - matrix restricting a fine-level solution to the coarse grid, usually the transpose of the interpolation
3311c833c3b5SJed Brown .  rscale - scaling vector for restriction
3312c833c3b5SJed Brown .  inject - matrix restricting by injection
3313b17ce1afSJed Brown .  coarse - coarse level DM to update
3314b17ce1afSJed Brown -  ctx - optional user-defined function context
3315b17ce1afSJed Brown 
3316b17ce1afSJed Brown    Level: advanced
3317b17ce1afSJed Brown 
3318b17ce1afSJed Brown    Notes:
3319bb7acecfSBarry 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`.
3320b17ce1afSJed Brown 
3321b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3322b17ce1afSJed Brown 
3323b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3324bb7acecfSBarry Smith    extract the finest level information from its context (instead of from the `SNES`).
3325b17ce1afSJed Brown 
3326bb7acecfSBarry Smith    The hooks are automatically called by `DMRestrict()`
3327bb7acecfSBarry Smith 
3328db781477SPatrick Sanan .seealso: `DMCoarsenHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3329b17ce1afSJed Brown @*/
3330d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookAdd(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3331d71ae5a4SJacob Faibussowitsch {
3332b17ce1afSJed Brown   DMCoarsenHookLink link, *p;
3333b17ce1afSJed Brown 
3334b17ce1afSJed Brown   PetscFunctionBegin;
3335b17ce1afSJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
33361e3d8eccSJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
33373ba16761SJacob Faibussowitsch     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
33381e3d8eccSJed Brown   }
33399566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
3340b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3341b17ce1afSJed Brown   link->restricthook = restricthook;
3342b17ce1afSJed Brown   link->ctx          = ctx;
33430298fd71SBarry Smith   link->next         = NULL;
3344b17ce1afSJed Brown   *p                 = link;
33453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3346b17ce1afSJed Brown }
3347b17ce1afSJed Brown 
3348dc822a44SJed Brown /*@C
3349bb7acecfSBarry Smith    DMCoarsenHookRemove - remove a callback set with `DMCoarsenHookAdd()`
3350dc822a44SJed Brown 
3351*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
3352dc822a44SJed Brown 
33534165533cSJose E. Roman    Input Parameters:
3354bb7acecfSBarry Smith +  fine - `DM` on which to run a hook when restricting to a coarser level
3355dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3356bb7acecfSBarry Smith .  restricthook - function to run to update data on coarser levels
3357*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3358dc822a44SJed Brown 
3359dc822a44SJed Brown    Level: advanced
3360dc822a44SJed Brown 
3361bb7acecfSBarry Smith    Note:
3362dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3363dc822a44SJed Brown 
3364db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3365dc822a44SJed Brown @*/
3366d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHookRemove(DM fine, PetscErrorCode (*coarsenhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, Mat, Vec, Mat, DM, void *), void *ctx)
3367d71ae5a4SJacob Faibussowitsch {
3368dc822a44SJed Brown   DMCoarsenHookLink link, *p;
3369dc822a44SJed Brown 
3370dc822a44SJed Brown   PetscFunctionBegin;
3371dc822a44SJed Brown   PetscValidHeaderSpecific(fine, DM_CLASSID, 1);
3372dc822a44SJed Brown   for (p = &fine->coarsenhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3373dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3374dc822a44SJed Brown       link = *p;
3375dc822a44SJed Brown       *p   = link->next;
33769566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3377dc822a44SJed Brown       break;
3378dc822a44SJed Brown     }
3379dc822a44SJed Brown   }
33803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3381dc822a44SJed Brown }
3382dc822a44SJed Brown 
3383b17ce1afSJed Brown /*@
3384bb7acecfSBarry Smith    DMRestrict - restricts user-defined problem data to a coarser `DM` by running hooks registered by `DMCoarsenHookAdd()`
3385b17ce1afSJed Brown 
3386b17ce1afSJed Brown    Collective if any hooks are
3387b17ce1afSJed Brown 
33884165533cSJose E. Roman    Input Parameters:
3389bb7acecfSBarry Smith +  fine - finer `DM` from which the data is obtained
3390bb7acecfSBarry Smith .  restrct - restriction matrix, apply using `MatRestrict()`, usually the transpose of the interpolation
3391e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3392bb7acecfSBarry Smith .  inject - injection matrix, also use `MatRestrict()`
3393*20f4b53cSBarry Smith -  coarse - coarser `DM` to update
3394b17ce1afSJed Brown 
3395b17ce1afSJed Brown    Level: developer
3396b17ce1afSJed Brown 
3397bb7acecfSBarry Smith    Developer Note:
3398bb7acecfSBarry Smith    Though this routine is called `DMRestrict()` the hooks are added with `DMCoarsenHookAdd()`, a consistent terminology would be better
3399bb7acecfSBarry Smith 
3400bb7acecfSBarry Smith .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`, `DMInterpolate()`, `DMRefineHookAdd()`
3401b17ce1afSJed Brown @*/
3402d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRestrict(DM fine, Mat restrct, Vec rscale, Mat inject, DM coarse)
3403d71ae5a4SJacob Faibussowitsch {
3404b17ce1afSJed Brown   DMCoarsenHookLink link;
3405b17ce1afSJed Brown 
3406b17ce1afSJed Brown   PetscFunctionBegin;
3407b17ce1afSJed Brown   for (link = fine->coarsenhook; link; link = link->next) {
34081baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(fine, restrct, rscale, inject, coarse, link->ctx));
3409b17ce1afSJed Brown   }
34103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
341147c6ae99SBarry Smith }
341247c6ae99SBarry Smith 
3413bb9467b5SJed Brown /*@C
3414be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
34155dbd56e3SPeter Brune 
3416*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
34175dbd56e3SPeter Brune 
34184165533cSJose E. Roman    Input Parameters:
3419bb7acecfSBarry Smith +  global - global `DM`
3420bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
34215dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3422*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
34235dbd56e3SPeter Brune 
3424*20f4b53cSBarry Smith    Calling sequence of `ddhook`:
3425*20f4b53cSBarry Smith $  PetscErrorCode ddhook(DM global, DM block, void *ctx)
3426bb7acecfSBarry Smith +  global - global `DM`
3427bb7acecfSBarry Smith .  block  - block `DM`
3428ec4806b8SPeter Brune -  ctx - optional user-defined function context
3429ec4806b8SPeter Brune 
3430*20f4b53cSBarry Smith    Calling sequence of `restricthook`:
3431*20f4b53cSBarry Smith $  PetscErrorCode restricthook(DM global, VecScatter out, VecScatter in, DM block, void *ctx)
3432bb7acecfSBarry Smith +  global - global `DM`
34335dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
34345dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3435bb7acecfSBarry Smith .  block  - block `DM`
34365dbd56e3SPeter Brune -  ctx - optional user-defined function context
34375dbd56e3SPeter Brune 
34385dbd56e3SPeter Brune    Level: advanced
34395dbd56e3SPeter Brune 
34405dbd56e3SPeter Brune    Notes:
3441bb7acecfSBarry Smith    This function is only needed if auxiliary data needs to be set up on subdomain `DM`s.
34425dbd56e3SPeter Brune 
34435dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
34445dbd56e3SPeter Brune 
34455dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3446bb7acecfSBarry Smith    extract the global information from its context (instead of from the `SNES`).
34475dbd56e3SPeter Brune 
3448bb7acecfSBarry Smith .seealso: `DMSubDomainHookRemove()`, `DMRefineHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
34495dbd56e3SPeter Brune @*/
3450d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookAdd(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3451d71ae5a4SJacob Faibussowitsch {
3452be081cd6SPeter Brune   DMSubDomainHookLink link, *p;
34535dbd56e3SPeter Brune 
34545dbd56e3SPeter Brune   PetscFunctionBegin;
34555dbd56e3SPeter Brune   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3456b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Scan to the end of the current list of hooks */
34573ba16761SJacob Faibussowitsch     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(PETSC_SUCCESS);
3458b3a6b972SJed Brown   }
34599566063dSJacob Faibussowitsch   PetscCall(PetscNew(&link));
34605dbd56e3SPeter Brune   link->restricthook = restricthook;
3461be081cd6SPeter Brune   link->ddhook       = ddhook;
34625dbd56e3SPeter Brune   link->ctx          = ctx;
34630298fd71SBarry Smith   link->next         = NULL;
34645dbd56e3SPeter Brune   *p                 = link;
34653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
34665dbd56e3SPeter Brune }
34675dbd56e3SPeter Brune 
3468b3a6b972SJed Brown /*@C
3469b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3470b3a6b972SJed Brown 
3471*20f4b53cSBarry Smith    Logically Collective; No Fortran Support
3472b3a6b972SJed Brown 
34734165533cSJose E. Roman    Input Parameters:
3474bb7acecfSBarry Smith +  global - global `DM`
3475bb7acecfSBarry Smith .  ddhook - function to run to pass data to the decomposition `DM` upon its creation
3476b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3477*20f4b53cSBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be `NULL`)
3478b3a6b972SJed Brown 
3479b3a6b972SJed Brown    Level: advanced
3480b3a6b972SJed Brown 
3481db781477SPatrick Sanan .seealso: `DMSubDomainHookAdd()`, `SNESFASGetInterpolation()`, `SNESFASGetInjection()`, `PetscObjectCompose()`, `PetscContainerCreate()`
3482b3a6b972SJed Brown @*/
3483d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainHookRemove(DM global, PetscErrorCode (*ddhook)(DM, DM, void *), PetscErrorCode (*restricthook)(DM, VecScatter, VecScatter, DM, void *), void *ctx)
3484d71ae5a4SJacob Faibussowitsch {
3485b3a6b972SJed Brown   DMSubDomainHookLink link, *p;
3486b3a6b972SJed Brown 
3487b3a6b972SJed Brown   PetscFunctionBegin;
3488b3a6b972SJed Brown   PetscValidHeaderSpecific(global, DM_CLASSID, 1);
3489b3a6b972SJed Brown   for (p = &global->subdomainhook; *p; p = &(*p)->next) { /* Search the list of current hooks */
3490b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3491b3a6b972SJed Brown       link = *p;
3492b3a6b972SJed Brown       *p   = link->next;
34939566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
3494b3a6b972SJed Brown       break;
3495b3a6b972SJed Brown     }
3496b3a6b972SJed Brown   }
34973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3498b3a6b972SJed Brown }
3499b3a6b972SJed Brown 
35005dbd56e3SPeter Brune /*@
3501bb7acecfSBarry Smith    DMSubDomainRestrict - restricts user-defined problem data to a block `DM` by running hooks registered by `DMSubDomainHookAdd()`
35025dbd56e3SPeter Brune 
35035dbd56e3SPeter Brune    Collective if any hooks are
35045dbd56e3SPeter Brune 
35054165533cSJose E. Roman    Input Parameters:
3506bb7acecfSBarry Smith +  fine - finer `DM` to use as a base
3507be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3508be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
3509bb7acecfSBarry Smith -  coarse - coarser `DM` to update
35105dbd56e3SPeter Brune 
35115dbd56e3SPeter Brune    Level: developer
35125dbd56e3SPeter Brune 
3513db781477SPatrick Sanan .seealso: `DMCoarsenHookAdd()`, `MatRestrict()`
35145dbd56e3SPeter Brune @*/
3515d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSubDomainRestrict(DM global, VecScatter oscatter, VecScatter gscatter, DM subdm)
3516d71ae5a4SJacob Faibussowitsch {
3517be081cd6SPeter Brune   DMSubDomainHookLink link;
35185dbd56e3SPeter Brune 
35195dbd56e3SPeter Brune   PetscFunctionBegin;
3520be081cd6SPeter Brune   for (link = global->subdomainhook; link; link = link->next) {
35211baa6e33SBarry Smith     if (link->restricthook) PetscCall((*link->restricthook)(global, oscatter, gscatter, subdm, link->ctx));
35225dbd56e3SPeter Brune   }
35233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35245dbd56e3SPeter Brune }
35255dbd56e3SPeter Brune 
35265fe1f584SPeter Brune /*@
3527bb7acecfSBarry Smith     DMGetCoarsenLevel - Gets the number of coarsenings that have generated this `DM`.
35285fe1f584SPeter Brune 
35295fe1f584SPeter Brune     Not Collective
35305fe1f584SPeter Brune 
35315fe1f584SPeter Brune     Input Parameter:
3532bb7acecfSBarry Smith .   dm - the `DM` object
35335fe1f584SPeter Brune 
35345fe1f584SPeter Brune     Output Parameter:
35356a7d9d85SPeter Brune .   level - number of coarsenings
35365fe1f584SPeter Brune 
35375fe1f584SPeter Brune     Level: developer
35385fe1f584SPeter Brune 
3539bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMSetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35405fe1f584SPeter Brune @*/
3541d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarsenLevel(DM dm, PetscInt *level)
3542d71ae5a4SJacob Faibussowitsch {
35435fe1f584SPeter Brune   PetscFunctionBegin;
35445fe1f584SPeter Brune   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3545b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level, 2);
35465fe1f584SPeter Brune   *level = dm->leveldown;
35473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35485fe1f584SPeter Brune }
35495fe1f584SPeter Brune 
35509a64c4a8SMatthew G. Knepley /*@
3551bb7acecfSBarry Smith     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this `DM`.
35529a64c4a8SMatthew G. Knepley 
3553*20f4b53cSBarry Smith     Collective
35549a64c4a8SMatthew G. Knepley 
35559a64c4a8SMatthew G. Knepley     Input Parameters:
3556bb7acecfSBarry Smith +   dm - the `DM` object
35579a64c4a8SMatthew G. Knepley -   level - number of coarsenings
35589a64c4a8SMatthew G. Knepley 
35599a64c4a8SMatthew G. Knepley     Level: developer
35609a64c4a8SMatthew G. Knepley 
3561bb7acecfSBarry Smith     Note:
3562bb7acecfSBarry Smith     This is rarely used directly, the information is automatically set when a `DM` is created with `DMCoarsen()`
3563bb7acecfSBarry Smith 
3564bb7acecfSBarry Smith .seealso: `DMSetCoarsenLevel()`, `DMCoarsen()`, `DMGetCoarsenLevel()`, `DMGetRefineLevel()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
35659a64c4a8SMatthew G. Knepley @*/
3566d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarsenLevel(DM dm, PetscInt level)
3567d71ae5a4SJacob Faibussowitsch {
35689a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
35699a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35709a64c4a8SMatthew G. Knepley   dm->leveldown = level;
35713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35729a64c4a8SMatthew G. Knepley }
35739a64c4a8SMatthew G. Knepley 
357447c6ae99SBarry Smith /*@C
3575bb7acecfSBarry Smith     DMRefineHierarchy - Refines a `DM` object, all levels at once
357647c6ae99SBarry Smith 
3577*20f4b53cSBarry Smith     Collective
357847c6ae99SBarry Smith 
3579d8d19677SJose E. Roman     Input Parameters:
3580bb7acecfSBarry Smith +   dm - the `DM` object
358147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
358247c6ae99SBarry Smith 
358347c6ae99SBarry Smith     Output Parameter:
3584bb7acecfSBarry Smith .   dmf - the refined `DM` hierarchy
358547c6ae99SBarry Smith 
358647c6ae99SBarry Smith     Level: developer
358747c6ae99SBarry Smith 
3588bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMCoarsenHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
358947c6ae99SBarry Smith @*/
3590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRefineHierarchy(DM dm, PetscInt nlevels, DM dmf[])
3591d71ae5a4SJacob Faibussowitsch {
359247c6ae99SBarry Smith   PetscFunctionBegin;
3593171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35947a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
35953ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
3596b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf, 3);
359747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
3598dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, refinehierarchy, nlevels, dmf);
359947c6ae99SBarry Smith   } else if (dm->ops->refine) {
360047c6ae99SBarry Smith     PetscInt i;
360147c6ae99SBarry Smith 
36029566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &dmf[0]));
360348a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMRefine(dmf[i - 1], PetscObjectComm((PetscObject)dm), &dmf[i]));
3604ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No RefineHierarchy for this DM yet");
36053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
360647c6ae99SBarry Smith }
360747c6ae99SBarry Smith 
360847c6ae99SBarry Smith /*@C
3609bb7acecfSBarry Smith     DMCoarsenHierarchy - Coarsens a `DM` object, all levels at once
361047c6ae99SBarry Smith 
3611*20f4b53cSBarry Smith     Collective
361247c6ae99SBarry Smith 
3613d8d19677SJose E. Roman     Input Parameters:
3614bb7acecfSBarry Smith +   dm - the `DM` object
361547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
361647c6ae99SBarry Smith 
361747c6ae99SBarry Smith     Output Parameter:
3618bb7acecfSBarry Smith .   dmc - the coarsened `DM` hierarchy
361947c6ae99SBarry Smith 
362047c6ae99SBarry Smith     Level: developer
362147c6ae99SBarry Smith 
3622bb7acecfSBarry Smith .seealso: `DMCoarsen()`, `DMRefineHierarchy()`, `DMDestroy()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`
362347c6ae99SBarry Smith @*/
3624d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3625d71ae5a4SJacob Faibussowitsch {
362647c6ae99SBarry Smith   PetscFunctionBegin;
3627171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36287a8be351SBarry Smith   PetscCheck(nlevels >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "nlevels cannot be negative");
36293ba16761SJacob Faibussowitsch   if (nlevels == 0) PetscFunctionReturn(PETSC_SUCCESS);
363047c6ae99SBarry Smith   PetscValidPointer(dmc, 3);
363147c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
3632dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, coarsenhierarchy, nlevels, dmc);
363347c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
363447c6ae99SBarry Smith     PetscInt i;
363547c6ae99SBarry Smith 
36369566063dSJacob Faibussowitsch     PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &dmc[0]));
363748a46eb9SPierre Jolivet     for (i = 1; i < nlevels; i++) PetscCall(DMCoarsen(dmc[i - 1], PetscObjectComm((PetscObject)dm), &dmc[i]));
3638ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No CoarsenHierarchy for this DM yet");
36393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
364047c6ae99SBarry Smith }
364147c6ae99SBarry Smith 
36421a266240SBarry Smith /*@C
3643bb7acecfSBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the `DM` is destroyed
36441a266240SBarry Smith 
3645bb7acecfSBarry Smith     Logically Collective if the function is collective
36461a266240SBarry Smith 
36471a266240SBarry Smith     Input Parameters:
3648bb7acecfSBarry Smith +   dm - the `DM` object
36491a266240SBarry Smith -   destroy - the destroy function
36501a266240SBarry Smith 
36511a266240SBarry Smith     Level: intermediate
36521a266240SBarry Smith 
3653bb7acecfSBarry Smith .seealso: `DMSetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
3654f07f9ceaSJed Brown @*/
3655d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContextDestroy(DM dm, PetscErrorCode (*destroy)(void **))
3656d71ae5a4SJacob Faibussowitsch {
36571a266240SBarry Smith   PetscFunctionBegin;
3658171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36591a266240SBarry Smith   dm->ctxdestroy = destroy;
36603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36611a266240SBarry Smith }
36621a266240SBarry Smith 
3663b07ff414SBarry Smith /*@
3664bb7acecfSBarry Smith     DMSetApplicationContext - Set a user context into a `DM` object
366547c6ae99SBarry Smith 
366647c6ae99SBarry Smith     Not Collective
366747c6ae99SBarry Smith 
366847c6ae99SBarry Smith     Input Parameters:
3669bb7acecfSBarry Smith +   dm - the `DM` object
367047c6ae99SBarry Smith -   ctx - the user context
367147c6ae99SBarry Smith 
367247c6ae99SBarry Smith     Level: intermediate
367347c6ae99SBarry Smith 
3674bb7acecfSBarry Smith     Note:
367535cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3676bb7acecfSBarry Smith 
3677bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
367847c6ae99SBarry Smith @*/
3679d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetApplicationContext(DM dm, void *ctx)
3680d71ae5a4SJacob Faibussowitsch {
368147c6ae99SBarry Smith   PetscFunctionBegin;
3682171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368347c6ae99SBarry Smith   dm->ctx = ctx;
36843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368547c6ae99SBarry Smith }
368647c6ae99SBarry Smith 
368747c6ae99SBarry Smith /*@
3688bb7acecfSBarry Smith     DMGetApplicationContext - Gets a user context from a `DM` object
368947c6ae99SBarry Smith 
369047c6ae99SBarry Smith     Not Collective
369147c6ae99SBarry Smith 
369247c6ae99SBarry Smith     Input Parameter:
3693bb7acecfSBarry Smith .   dm - the `DM` object
369447c6ae99SBarry Smith 
369547c6ae99SBarry Smith     Output Parameter:
369647c6ae99SBarry Smith .   ctx - the user context
369747c6ae99SBarry Smith 
369847c6ae99SBarry Smith     Level: intermediate
369947c6ae99SBarry Smith 
3700bb7acecfSBarry Smith     Note:
370135cb6cd3SPierre Jolivet     A user context is a way to pass problem specific information that is accessible whenever the `DM` is available
3702bb7acecfSBarry Smith 
3703bb7acecfSBarry Smith .seealso: `DMGetApplicationContext()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
370447c6ae99SBarry Smith @*/
3705d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetApplicationContext(DM dm, void *ctx)
3706d71ae5a4SJacob Faibussowitsch {
370747c6ae99SBarry Smith   PetscFunctionBegin;
3708171400e9SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37091b2093e4SBarry Smith   *(void **)ctx = dm->ctx;
37103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
371147c6ae99SBarry Smith }
371247c6ae99SBarry Smith 
371308da532bSDmitry Karpeev /*@C
3714bb7acecfSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for `SNESVI`.
371508da532bSDmitry Karpeev 
3716*20f4b53cSBarry Smith     Logically Collective
371708da532bSDmitry Karpeev 
3718d8d19677SJose E. Roman     Input Parameters:
371908da532bSDmitry Karpeev +   dm - the DM object
3720*20f4b53cSBarry Smith -   f - the function that computes variable bounds used by SNESVI (use `NULL` to cancel a previous function that was set)
372108da532bSDmitry Karpeev 
372208da532bSDmitry Karpeev     Level: intermediate
372308da532bSDmitry Karpeev 
3724bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`,
3725db781477SPatrick Sanan          `DMSetJacobian()`
372608da532bSDmitry Karpeev @*/
3727d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetVariableBounds(DM dm, PetscErrorCode (*f)(DM, Vec, Vec))
3728d71ae5a4SJacob Faibussowitsch {
372908da532bSDmitry Karpeev   PetscFunctionBegin;
37305a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373108da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
37323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
373308da532bSDmitry Karpeev }
373408da532bSDmitry Karpeev 
373508da532bSDmitry Karpeev /*@
3736bb7acecfSBarry Smith     DMHasVariableBounds - does the `DM` object have a variable bounds function?
373708da532bSDmitry Karpeev 
373808da532bSDmitry Karpeev     Not Collective
373908da532bSDmitry Karpeev 
374008da532bSDmitry Karpeev     Input Parameter:
3741bb7acecfSBarry Smith .   dm - the `DM` object to destroy
374208da532bSDmitry Karpeev 
374308da532bSDmitry Karpeev     Output Parameter:
3744bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the variable bounds function exists
374508da532bSDmitry Karpeev 
374608da532bSDmitry Karpeev     Level: developer
374708da532bSDmitry Karpeev 
3748bb7acecfSBarry Smith .seealso: `DMComputeVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
374908da532bSDmitry Karpeev @*/
3750d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasVariableBounds(DM dm, PetscBool *flg)
3751d71ae5a4SJacob Faibussowitsch {
375208da532bSDmitry Karpeev   PetscFunctionBegin;
37535a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3754534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
375508da532bSDmitry Karpeev   *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
37563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
375708da532bSDmitry Karpeev }
375808da532bSDmitry Karpeev 
375908da532bSDmitry Karpeev /*@C
3760bb7acecfSBarry Smith     DMComputeVariableBounds - compute variable bounds used by `SNESVI`.
376108da532bSDmitry Karpeev 
3762*20f4b53cSBarry Smith     Logically Collective
376308da532bSDmitry Karpeev 
3764f899ff85SJose E. Roman     Input Parameter:
3765bb7acecfSBarry Smith .   dm - the `DM` object
376608da532bSDmitry Karpeev 
376708da532bSDmitry Karpeev     Output parameters:
376808da532bSDmitry Karpeev +   xl - lower bound
376908da532bSDmitry Karpeev -   xu - upper bound
377008da532bSDmitry Karpeev 
3771907376e6SBarry Smith     Level: advanced
3772907376e6SBarry Smith 
3773*20f4b53cSBarry Smith     Note:
377495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
377508da532bSDmitry Karpeev 
3776bb7acecfSBarry Smith .seealso: `DMHasVariableBounds()`, `DMView()`, `DMCreateGlobalVector()`, `DMCreateInterpolation()`, `DMCreateColoring()`, `DMCreateMatrix()`, `DMCreateMassMatrix()`, `DMGetApplicationContext()`
377708da532bSDmitry Karpeev @*/
3778d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3779d71ae5a4SJacob Faibussowitsch {
378008da532bSDmitry Karpeev   PetscFunctionBegin;
37815a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl, VEC_CLASSID, 2);
37835a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu, VEC_CLASSID, 3);
3784dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, computevariablebounds, xl, xu);
37853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
378608da532bSDmitry Karpeev }
378708da532bSDmitry Karpeev 
3788b0ae01b7SPeter Brune /*@
3789bb7acecfSBarry Smith     DMHasColoring - does the `DM` object have a method of providing a coloring?
3790b0ae01b7SPeter Brune 
3791b0ae01b7SPeter Brune     Not Collective
3792b0ae01b7SPeter Brune 
3793b0ae01b7SPeter Brune     Input Parameter:
3794b0ae01b7SPeter Brune .   dm - the DM object
3795b0ae01b7SPeter Brune 
3796b0ae01b7SPeter Brune     Output Parameter:
3797bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateColoring()`.
3798b0ae01b7SPeter Brune 
3799b0ae01b7SPeter Brune     Level: developer
3800b0ae01b7SPeter Brune 
3801bb7acecfSBarry Smith .seealso: `DMCreateColoring()`
3802b0ae01b7SPeter Brune @*/
3803d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasColoring(DM dm, PetscBool *flg)
3804d71ae5a4SJacob Faibussowitsch {
3805b0ae01b7SPeter Brune   PetscFunctionBegin;
38065a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3807534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3808b0ae01b7SPeter Brune   *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
38093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3810b0ae01b7SPeter Brune }
3811b0ae01b7SPeter Brune 
38123ad4599aSBarry Smith /*@
3813bb7acecfSBarry Smith     DMHasCreateRestriction - does the `DM` object have a method of providing a restriction?
38143ad4599aSBarry Smith 
38153ad4599aSBarry Smith     Not Collective
38163ad4599aSBarry Smith 
38173ad4599aSBarry Smith     Input Parameter:
3818bb7acecfSBarry Smith .   dm - the `DM` object
38193ad4599aSBarry Smith 
38203ad4599aSBarry Smith     Output Parameter:
3821bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateRestriction()`.
38223ad4599aSBarry Smith 
38233ad4599aSBarry Smith     Level: developer
38243ad4599aSBarry Smith 
3825bb7acecfSBarry Smith .seealso: `DMCreateRestriction()`, `DMHasCreateInterpolation()`, `DMHasCreateInjection()`
38263ad4599aSBarry Smith @*/
3827d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateRestriction(DM dm, PetscBool *flg)
3828d71ae5a4SJacob Faibussowitsch {
38293ad4599aSBarry Smith   PetscFunctionBegin;
38305a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3831534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
38323ad4599aSBarry Smith   *flg = (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
38333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38343ad4599aSBarry Smith }
38353ad4599aSBarry Smith 
3836a7058e45SLawrence Mitchell /*@
3837bb7acecfSBarry Smith     DMHasCreateInjection - does the `DM` object have a method of providing an injection?
3838a7058e45SLawrence Mitchell 
3839a7058e45SLawrence Mitchell     Not Collective
3840a7058e45SLawrence Mitchell 
3841a7058e45SLawrence Mitchell     Input Parameter:
3842bb7acecfSBarry Smith .   dm - the `DM` object
3843a7058e45SLawrence Mitchell 
3844a7058e45SLawrence Mitchell     Output Parameter:
3845bb7acecfSBarry Smith .   flg - `PETSC_TRUE` if the `DM` has facilities for `DMCreateInjection()`.
3846a7058e45SLawrence Mitchell 
3847a7058e45SLawrence Mitchell     Level: developer
3848a7058e45SLawrence Mitchell 
3849bb7acecfSBarry Smith .seealso: `DMCreateInjection()`, `DMHasCreateRestriction()`, `DMHasCreateInterpolation()`
3850a7058e45SLawrence Mitchell @*/
3851d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasCreateInjection(DM dm, PetscBool *flg)
3852d71ae5a4SJacob Faibussowitsch {
3853a7058e45SLawrence Mitchell   PetscFunctionBegin;
38545a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3855534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
3856dbbe0bcdSBarry Smith   if (dm->ops->hascreateinjection) PetscUseTypeMethod(dm, hascreateinjection, flg);
3857ad540459SPierre Jolivet   else *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
38583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3859a7058e45SLawrence Mitchell }
3860a7058e45SLawrence Mitchell 
38610298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3862264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3863264ace61SBarry Smith 
3864264ace61SBarry Smith /*@C
3865bb7acecfSBarry Smith   DMSetType - Builds a `DM`, for a particular `DM` implementation.
3866264ace61SBarry Smith 
3867*20f4b53cSBarry Smith   Collective
3868264ace61SBarry Smith 
3869264ace61SBarry Smith   Input Parameters:
3870bb7acecfSBarry Smith + dm     - The `DM` object
3871bb7acecfSBarry Smith - method - The name of the `DMType`, for example `DMDA`, `DMPLEX`
3872264ace61SBarry Smith 
3873264ace61SBarry Smith   Options Database Key:
3874bb7acecfSBarry Smith . -dm_type <type> - Sets the `DM` type; use -help for a list of available types
3875264ace61SBarry Smith 
3876264ace61SBarry Smith   Level: intermediate
3877264ace61SBarry Smith 
3878bb7acecfSBarry Smith   Note:
3879bb7acecfSBarry Smith   Of the `DM` is constructed by directly calling a function to construct a particular `DM`, for example, `DMDACreate2d()` or `DMPLEXCreateBoxMesh()`
3880bb7acecfSBarry Smith 
3881bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMGetType()`, `DMCreate()`, `DMDACreate2d()`
3882264ace61SBarry Smith @*/
3883d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetType(DM dm, DMType method)
3884d71ae5a4SJacob Faibussowitsch {
3885264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3886264ace61SBarry Smith   PetscBool match;
3887264ace61SBarry Smith 
3888264ace61SBarry Smith   PetscFunctionBegin;
3889264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38909566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, method, &match));
38913ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
3892264ace61SBarry Smith 
38939566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
38949566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(DMList, method, &r));
38957a8be351SBarry Smith   PetscCheck(r, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3896264ace61SBarry Smith 
3897dbbe0bcdSBarry Smith   PetscTryTypeMethod(dm, destroy);
38989566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(dm->ops, sizeof(*dm->ops)));
38999566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)dm, method));
39009566063dSJacob Faibussowitsch   PetscCall((*r)(dm));
39013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3902264ace61SBarry Smith }
3903264ace61SBarry Smith 
3904264ace61SBarry Smith /*@C
3905bb7acecfSBarry Smith   DMGetType - Gets the `DM` type name (as a string) from the `DM`.
3906264ace61SBarry Smith 
3907264ace61SBarry Smith   Not Collective
3908264ace61SBarry Smith 
3909264ace61SBarry Smith   Input Parameter:
3910bb7acecfSBarry Smith . dm  - The `DM`
3911264ace61SBarry Smith 
3912264ace61SBarry Smith   Output Parameter:
3913bb7acecfSBarry Smith . type - The `DMType` name
3914264ace61SBarry Smith 
3915264ace61SBarry Smith   Level: intermediate
3916264ace61SBarry Smith 
3917bb7acecfSBarry Smith .seealso: `DMType`, `DMDA`, `DMPLEX`, `DMSetType()`, `DMCreate()`
3918264ace61SBarry Smith @*/
3919d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetType(DM dm, DMType *type)
3920d71ae5a4SJacob Faibussowitsch {
3921264ace61SBarry Smith   PetscFunctionBegin;
3922264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3923c959eef4SJed Brown   PetscValidPointer(type, 2);
39249566063dSJacob Faibussowitsch   PetscCall(DMRegisterAll());
3925264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
39263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3927264ace61SBarry Smith }
3928264ace61SBarry Smith 
392967a56275SMatthew G Knepley /*@C
3930bb7acecfSBarry Smith   DMConvert - Converts a `DM` to another `DM`, either of the same or different type.
393167a56275SMatthew G Knepley 
3932*20f4b53cSBarry Smith   Collective
393367a56275SMatthew G Knepley 
393467a56275SMatthew G Knepley   Input Parameters:
3935bb7acecfSBarry Smith + dm - the `DM`
3936bb7acecfSBarry Smith - newtype - new `DM` type (use "same" for the same type)
393767a56275SMatthew G Knepley 
393867a56275SMatthew G Knepley   Output Parameter:
3939bb7acecfSBarry Smith . M - pointer to new `DM`
394067a56275SMatthew G Knepley 
3941*20f4b53cSBarry Smith   Level: intermediate
3942*20f4b53cSBarry Smith 
394367a56275SMatthew G Knepley   Notes:
3944bb7acecfSBarry Smith   Cannot be used to convert a sequential `DM` to a parallel or a parallel to sequential,
3945bb7acecfSBarry Smith   the MPI communicator of the generated `DM` is always the same as the communicator
3946bb7acecfSBarry Smith   of the input `DM`.
394767a56275SMatthew G Knepley 
3948bb7acecfSBarry Smith .seealso: `DM`, `DMSetType()`, `DMCreate()`, `DMClone()`
394967a56275SMatthew G Knepley @*/
3950d71ae5a4SJacob Faibussowitsch PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3951d71ae5a4SJacob Faibussowitsch {
395267a56275SMatthew G Knepley   DM        B;
395367a56275SMatthew G Knepley   char      convname[256];
3954c067b6caSMatthew G. Knepley   PetscBool sametype /*, issame */;
395567a56275SMatthew G Knepley 
395667a56275SMatthew G Knepley   PetscFunctionBegin;
395767a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395867a56275SMatthew G Knepley   PetscValidType(dm, 1);
395967a56275SMatthew G Knepley   PetscValidPointer(M, 3);
39609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)dm, newtype, &sametype));
39619566063dSJacob Faibussowitsch   /* PetscCall(PetscStrcmp(newtype, "same", &issame)); */
3962c067b6caSMatthew G. Knepley   if (sametype) {
3963c067b6caSMatthew G. Knepley     *M = dm;
39649566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)dm));
39653ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3966c067b6caSMatthew G. Knepley   } else {
39670298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM *) = NULL;
396867a56275SMatthew G Knepley 
396967a56275SMatthew G Knepley     /*
397067a56275SMatthew G Knepley        Order of precedence:
397167a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
397267a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
397367a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
397467a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
397567a56275SMatthew G Knepley        5) Use a really basic converter.
397667a56275SMatthew G Knepley     */
397767a56275SMatthew G Knepley 
397867a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
39799566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39809566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39819566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39829566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39839566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39849566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)dm, convname, &conv));
398567a56275SMatthew G Knepley     if (conv) goto foundconv;
398667a56275SMatthew G Knepley 
398767a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
39889566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &B));
39899566063dSJacob Faibussowitsch     PetscCall(DMSetType(B, newtype));
39909566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(convname, "DMConvert_", sizeof(convname)));
39919566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, ((PetscObject)dm)->type_name, sizeof(convname)));
39929566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_", sizeof(convname)));
39939566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, newtype, sizeof(convname)));
39949566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(convname, "_C", sizeof(convname)));
39959566063dSJacob Faibussowitsch     PetscCall(PetscObjectQueryFunction((PetscObject)B, convname, &conv));
399667a56275SMatthew G Knepley     if (conv) {
39979566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&B));
399867a56275SMatthew G Knepley       goto foundconv;
399967a56275SMatthew G Knepley     }
400067a56275SMatthew G Knepley 
400167a56275SMatthew G Knepley #if 0
400267a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
400367a56275SMatthew G Knepley     conv = B->ops->convertfrom;
40049566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&B));
400567a56275SMatthew G Knepley     if (conv) goto foundconv;
400667a56275SMatthew G Knepley 
400767a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
400867a56275SMatthew G Knepley     if (dm->ops->convert) {
400967a56275SMatthew G Knepley       conv = dm->ops->convert;
401067a56275SMatthew G Knepley     }
401167a56275SMatthew G Knepley     if (conv) goto foundconv;
401267a56275SMatthew G Knepley #endif
401367a56275SMatthew G Knepley 
401467a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
401598921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject)dm)->type_name, newtype);
401667a56275SMatthew G Knepley 
401767a56275SMatthew G Knepley   foundconv:
40189566063dSJacob Faibussowitsch     PetscCall(PetscLogEventBegin(DM_Convert, dm, 0, 0, 0));
40199566063dSJacob Faibussowitsch     PetscCall((*conv)(dm, newtype, M));
402012fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
402190b157c4SStefano Zampini     {
40224fb89dddSMatthew G. Knepley       const PetscReal *maxCell, *Lstart, *L;
40236858538eSMatthew G. Knepley 
40244fb89dddSMatthew G. Knepley       PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
40254fb89dddSMatthew G. Knepley       PetscCall(DMSetPeriodicity(*M, maxCell, Lstart, L));
4026c8a6034eSMark       (*M)->prealloc_only = dm->prealloc_only;
40279566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->vectype));
40289566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*M)->vectype));
40299566063dSJacob Faibussowitsch       PetscCall(PetscFree((*M)->mattype));
40309566063dSJacob Faibussowitsch       PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*M)->mattype));
403112fa691eSMatthew G. Knepley     }
40329566063dSJacob Faibussowitsch     PetscCall(PetscLogEventEnd(DM_Convert, dm, 0, 0, 0));
403367a56275SMatthew G Knepley   }
40349566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateIncrease((PetscObject)*M));
40353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
403667a56275SMatthew G Knepley }
4037264ace61SBarry Smith 
4038264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
4039264ace61SBarry Smith 
4040264ace61SBarry Smith /*@C
4041bb7acecfSBarry Smith   DMRegister -  Adds a new `DM` type implementation
40421c84c290SBarry Smith 
40431c84c290SBarry Smith   Not Collective
40441c84c290SBarry Smith 
40451c84c290SBarry Smith   Input Parameters:
4046*20f4b53cSBarry Smith + sname    - The name of a new user-defined creation routine
4047*20f4b53cSBarry Smith - function - The creation routine itself
4048*20f4b53cSBarry Smith 
4049*20f4b53cSBarry Smith   Level: advanced
40501c84c290SBarry Smith 
40511c84c290SBarry Smith   Notes:
4052*20f4b53cSBarry Smith   `DMRegister()` may be called multiple times to add several user-defined `DM`s
40531c84c290SBarry Smith 
40541c84c290SBarry Smith   Sample usage:
40551c84c290SBarry Smith .vb
4056bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
40571c84c290SBarry Smith .ve
40581c84c290SBarry Smith 
4059*20f4b53cSBarry Smith   Then, your `DM` type can be chosen with the procedural interface via
40601c84c290SBarry Smith .vb
40611c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
40621c84c290SBarry Smith     DMSetType(DM,"my_da");
40631c84c290SBarry Smith .ve
40641c84c290SBarry Smith    or at runtime via the option
40651c84c290SBarry Smith .vb
40661c84c290SBarry Smith     -da_type my_da
40671c84c290SBarry Smith .ve
4068264ace61SBarry Smith 
4069bb7acecfSBarry Smith .seealso: `DM`, `DMType`, `DMSetType()`, `DMRegisterAll()`, `DMRegisterDestroy()`
4070264ace61SBarry Smith @*/
4071d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRegister(const char sname[], PetscErrorCode (*function)(DM))
4072d71ae5a4SJacob Faibussowitsch {
4073264ace61SBarry Smith   PetscFunctionBegin;
40749566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
40759566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&DMList, sname, function));
40763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4077264ace61SBarry Smith }
4078264ace61SBarry Smith 
4079b859378eSBarry Smith /*@C
4080bb7acecfSBarry Smith   DMLoad - Loads a DM that has been stored in binary  with `DMView()`.
4081b859378eSBarry Smith 
4082*20f4b53cSBarry Smith   Collective
4083b859378eSBarry Smith 
4084b859378eSBarry Smith   Input Parameters:
4085bb7acecfSBarry Smith + newdm - the newly loaded `DM`, this needs to have been created with `DMCreate()` or
4086bb7acecfSBarry Smith            some related function before a call to `DMLoad()`.
4087bb7acecfSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or
4088bb7acecfSBarry Smith            `PETSCVIEWERHDF5` file viewer, obtained from `PetscViewerHDF5Open()`
4089b859378eSBarry Smith 
4090b859378eSBarry Smith    Level: intermediate
4091b859378eSBarry Smith 
4092b859378eSBarry Smith   Notes:
409355849f57SBarry Smith   The type is determined by the data in the file, any type set into the DM before this call is ignored.
4094b859378eSBarry Smith 
4095bb7acecfSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
4096bb7acecfSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
4097bb7acecfSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
4098cd7e8a5eSksagiyam 
4099db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `DMView()`, `MatLoad()`, `VecLoad()`
4100b859378eSBarry Smith @*/
4101d71ae5a4SJacob Faibussowitsch PetscErrorCode DMLoad(DM newdm, PetscViewer viewer)
4102d71ae5a4SJacob Faibussowitsch {
41039331c7a4SMatthew G. Knepley   PetscBool isbinary, ishdf5;
4104b859378eSBarry Smith 
4105b859378eSBarry Smith   PetscFunctionBegin;
4106b859378eSBarry Smith   PetscValidHeaderSpecific(newdm, DM_CLASSID, 1);
4107b859378eSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
41089566063dSJacob Faibussowitsch   PetscCall(PetscViewerCheckReadable(viewer));
41099566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
41109566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
41119566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DM_Load, viewer, 0, 0, 0));
41129331c7a4SMatthew G. Knepley   if (isbinary) {
41139331c7a4SMatthew G. Knepley     PetscInt classid;
41149331c7a4SMatthew G. Knepley     char     type[256];
4115b859378eSBarry Smith 
41169566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT));
41177a8be351SBarry Smith     PetscCheck(classid == DM_FILE_CLASSID, PetscObjectComm((PetscObject)newdm), PETSC_ERR_ARG_WRONG, "Not DM next in file, classid found %d", (int)classid);
41189566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR));
41199566063dSJacob Faibussowitsch     PetscCall(DMSetType(newdm, type));
4120dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41219331c7a4SMatthew G. Knepley   } else if (ishdf5) {
4122dbbe0bcdSBarry Smith     PetscTryTypeMethod(newdm, load, viewer);
41239331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
41249566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DM_Load, viewer, 0, 0, 0));
41253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4126b859378eSBarry Smith }
4127b859378eSBarry Smith 
41287da65231SMatthew G Knepley /******************************** FEM Support **********************************/
41297da65231SMatthew G Knepley 
4130d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
4131d71ae5a4SJacob Faibussowitsch {
41321d47ebbbSSatish Balay   PetscInt f;
41331b30c384SMatthew G Knepley 
41347da65231SMatthew G Knepley   PetscFunctionBegin;
413563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
413648a46eb9SPierre Jolivet   for (f = 0; f < len; ++f) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f])));
41373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41387da65231SMatthew G Knepley }
41397da65231SMatthew G Knepley 
4140d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4141d71ae5a4SJacob Faibussowitsch {
41421b30c384SMatthew G Knepley   PetscInt f, g;
41437da65231SMatthew G Knepley 
41447da65231SMatthew G Knepley   PetscFunctionBegin;
414563a3b9bcSJacob Faibussowitsch   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " Element %s\n", c, name));
41461d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
41479566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "  |"));
414848a46eb9SPierre Jolivet     for (g = 0; g < cols; ++g) PetscCall(PetscPrintf(PETSC_COMM_SELF, " % 9.5g", (double)PetscRealPart(A[f * cols + g])));
41499566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, " |\n"));
41507da65231SMatthew G Knepley   }
41513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41527da65231SMatthew G Knepley }
4153e7c4fc90SDmitry Karpeev 
4154d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4155d71ae5a4SJacob Faibussowitsch {
41560c5b8624SToby Isaac   PetscInt           localSize, bs;
41570c5b8624SToby Isaac   PetscMPIInt        size;
41580c5b8624SToby Isaac   Vec                x, xglob;
41590c5b8624SToby Isaac   const PetscScalar *xarray;
4160e759306cSMatthew G. Knepley 
4161e759306cSMatthew G. Knepley   PetscFunctionBegin;
41629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
41639566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(X, &x));
41649566063dSJacob Faibussowitsch   PetscCall(VecCopy(X, x));
41659566063dSJacob Faibussowitsch   PetscCall(VecChop(x, tol));
41669566063dSJacob Faibussowitsch   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "%s:\n", name));
41670c5b8624SToby Isaac   if (size > 1) {
41689566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(x, &localSize));
41699566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(x, &xarray));
41709566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(x, &bs));
41719566063dSJacob Faibussowitsch     PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm), bs, localSize, PETSC_DETERMINE, xarray, &xglob));
41720c5b8624SToby Isaac   } else {
41730c5b8624SToby Isaac     xglob = x;
41740c5b8624SToby Isaac   }
41759566063dSJacob Faibussowitsch   PetscCall(VecView(xglob, PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)dm))));
41760c5b8624SToby Isaac   if (size > 1) {
41779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&xglob));
41789566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(x, &xarray));
41790c5b8624SToby Isaac   }
41809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&x));
41813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4182e759306cSMatthew G. Knepley }
4183e759306cSMatthew G. Knepley 
418488ed4aceSMatthew G Knepley /*@
4185bb7acecfSBarry Smith   DMGetSection - Get the `PetscSection` encoding the local data layout for the `DM`.   This is equivalent to `DMGetLocalSection()`. Deprecated in v3.12
4186061576a5SJed Brown 
4187061576a5SJed Brown   Input Parameter:
4188bb7acecfSBarry Smith . dm - The `DM`
4189061576a5SJed Brown 
4190061576a5SJed Brown   Output Parameter:
4191bb7acecfSBarry Smith . section - The `PetscSection`
4192061576a5SJed Brown 
4193*20f4b53cSBarry Smith   Options Database Key:
4194bb7acecfSBarry Smith . -dm_petscsection_view - View the `PetscSection` created by the `DM`
4195061576a5SJed Brown 
4196061576a5SJed Brown   Level: advanced
4197061576a5SJed Brown 
4198061576a5SJed Brown   Notes:
4199bb7acecfSBarry Smith   Use `DMGetLocalSection()` in new code.
4200061576a5SJed Brown 
4201bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
4202061576a5SJed Brown 
4203db781477SPatrick Sanan .seealso: `DMGetLocalSection()`, `DMSetLocalSection()`, `DMGetGlobalSection()`
4204061576a5SJed Brown @*/
4205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4206d71ae5a4SJacob Faibussowitsch {
4207061576a5SJed Brown   PetscFunctionBegin;
42089566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, section));
42093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4210061576a5SJed Brown }
4211061576a5SJed Brown 
4212061576a5SJed Brown /*@
4213bb7acecfSBarry Smith   DMGetLocalSection - Get the `PetscSection` encoding the local data layout for the `DM`.
421488ed4aceSMatthew G Knepley 
421588ed4aceSMatthew G Knepley   Input Parameter:
4216bb7acecfSBarry Smith . dm - The `DM`
421788ed4aceSMatthew G Knepley 
421888ed4aceSMatthew G Knepley   Output Parameter:
4219bb7acecfSBarry Smith . section - The `PetscSection`
422088ed4aceSMatthew G Knepley 
4221*20f4b53cSBarry Smith   Options Database Key:
4222bb7acecfSBarry Smith . -dm_petscsection_view - View the section created by the `DM`
4223e5893cccSMatthew G. Knepley 
422488ed4aceSMatthew G Knepley   Level: intermediate
422588ed4aceSMatthew G Knepley 
4226bb7acecfSBarry Smith   Note:
4227bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
422888ed4aceSMatthew G Knepley 
4229db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetGlobalSection()`
423088ed4aceSMatthew G Knepley @*/
4231d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
4232d71ae5a4SJacob Faibussowitsch {
423388ed4aceSMatthew G Knepley   PetscFunctionBegin;
423488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42361bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4237e5e52638SMatthew G. Knepley     PetscInt d;
4238e5e52638SMatthew G. Knepley 
423945480ffeSMatthew G. Knepley     if (dm->setfromoptionscalled) {
424045480ffeSMatthew G. Knepley       PetscObject       obj = (PetscObject)dm;
424145480ffeSMatthew G. Knepley       PetscViewer       viewer;
424245480ffeSMatthew G. Knepley       PetscViewerFormat format;
424345480ffeSMatthew G. Knepley       PetscBool         flg;
424445480ffeSMatthew G. Knepley 
42459566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetViewer(PetscObjectComm(obj), obj->options, obj->prefix, "-dm_petscds_view", &viewer, &format, &flg));
42469566063dSJacob Faibussowitsch       if (flg) PetscCall(PetscViewerPushFormat(viewer, format));
424745480ffeSMatthew G. Knepley       for (d = 0; d < dm->Nds; ++d) {
42489566063dSJacob Faibussowitsch         PetscCall(PetscDSSetFromOptions(dm->probs[d].ds));
42499566063dSJacob Faibussowitsch         if (flg) PetscCall(PetscDSView(dm->probs[d].ds, viewer));
425045480ffeSMatthew G. Knepley       }
425145480ffeSMatthew G. Knepley       if (flg) {
42529566063dSJacob Faibussowitsch         PetscCall(PetscViewerFlush(viewer));
42539566063dSJacob Faibussowitsch         PetscCall(PetscViewerPopFormat(viewer));
42549566063dSJacob Faibussowitsch         PetscCall(PetscViewerDestroy(&viewer));
425545480ffeSMatthew G. Knepley       }
425645480ffeSMatthew G. Knepley     }
4257dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm, createlocalsection);
42589566063dSJacob Faibussowitsch     if (dm->localSection) PetscCall(PetscObjectViewFromOptions((PetscObject)dm->localSection, NULL, "-dm_petscsection_view"));
42592f0f8703SMatthew G. Knepley   }
42601bb6d2a8SBarry Smith   *section = dm->localSection;
42613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
426288ed4aceSMatthew G Knepley }
426388ed4aceSMatthew G Knepley 
426488ed4aceSMatthew G Knepley /*@
4265bb7acecfSBarry Smith   DMSetSection - Set the `PetscSection` encoding the local data layout for the `DM`.  This is equivalent to `DMSetLocalSection()`. Deprecated in v3.12
4266061576a5SJed Brown 
4267061576a5SJed Brown   Input Parameters:
4268bb7acecfSBarry Smith + dm - The `DM`
4269bb7acecfSBarry Smith - section - The `PetscSection`
4270061576a5SJed Brown 
4271061576a5SJed Brown   Level: advanced
4272061576a5SJed Brown 
4273061576a5SJed Brown   Notes:
4274bb7acecfSBarry Smith   Use `DMSetLocalSection()` in new code.
4275061576a5SJed Brown 
4276bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4277061576a5SJed Brown 
4278db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`, `DMSetGlobalSection()`
4279061576a5SJed Brown @*/
4280d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSection(DM dm, PetscSection section)
4281d71ae5a4SJacob Faibussowitsch {
4282061576a5SJed Brown   PetscFunctionBegin;
42839566063dSJacob Faibussowitsch   PetscCall(DMSetLocalSection(dm, section));
42843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4285061576a5SJed Brown }
4286061576a5SJed Brown 
4287061576a5SJed Brown /*@
4288bb7acecfSBarry Smith   DMSetLocalSection - Set the `PetscSection` encoding the local data layout for the `DM`.
428988ed4aceSMatthew G Knepley 
429088ed4aceSMatthew G Knepley   Input Parameters:
4291bb7acecfSBarry Smith + dm - The `DM`
4292bb7acecfSBarry Smith - section - The `PetscSection`
429388ed4aceSMatthew G Knepley 
429488ed4aceSMatthew G Knepley   Level: intermediate
429588ed4aceSMatthew G Knepley 
4296bb7acecfSBarry Smith   Note:
4297bb7acecfSBarry Smith   Any existing Section will be destroyed
429888ed4aceSMatthew G Knepley 
4299bb7acecfSBarry Smith .seealso: `PetscSection`, `DMGetLocalSection()`, `DMSetGlobalSection()`
430088ed4aceSMatthew G Knepley @*/
4301d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
4302d71ae5a4SJacob Faibussowitsch {
4303c473ab19SMatthew G. Knepley   PetscInt numFields = 0;
4304af122d2aSMatthew G Knepley   PetscInt f;
430588ed4aceSMatthew G Knepley 
430688ed4aceSMatthew G Knepley   PetscFunctionBegin;
430788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4308b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
43109566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->localSection));
43111bb6d2a8SBarry Smith   dm->localSection = section;
43129566063dSJacob Faibussowitsch   if (section) PetscCall(PetscSectionGetNumFields(dm->localSection, &numFields));
4313af122d2aSMatthew G Knepley   if (numFields) {
43149566063dSJacob Faibussowitsch     PetscCall(DMSetNumFields(dm, numFields));
4315af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
43160f21e855SMatthew G. Knepley       PetscObject disc;
4317af122d2aSMatthew G Knepley       const char *name;
4318af122d2aSMatthew G Knepley 
43199566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetFieldName(dm->localSection, f, &name));
43209566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, &disc));
43219566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName(disc, name));
4322af122d2aSMatthew G Knepley     }
4323af122d2aSMatthew G Knepley   }
4324e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
43259566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
43263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432788ed4aceSMatthew G Knepley }
432888ed4aceSMatthew G Knepley 
43299435951eSToby Isaac /*@
4330bb7acecfSBarry Smith   DMGetDefaultConstraints - Get the `PetscSection` and `Mat` that specify the local constraint interpolation. See `DMSetDefaultConstraints()` for a description of the purpose of constraint interpolation.
43319435951eSToby Isaac 
4332*20f4b53cSBarry Smith   not Collective
4333e228b242SToby Isaac 
43349435951eSToby Isaac   Input Parameter:
4335bb7acecfSBarry Smith . dm - The `DM`
43369435951eSToby Isaac 
4337d8d19677SJose E. Roman   Output Parameters:
4338*20f4b53cSBarry 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.
4339*20f4b53cSBarry 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.
434079769bd5SJed Brown - bias - Vector containing bias to be added to constrained dofs
43419435951eSToby Isaac 
43429435951eSToby Isaac   Level: advanced
43439435951eSToby Isaac 
4344bb7acecfSBarry Smith   Note:
4345bb7acecfSBarry Smith   This gets borrowed references, so the user should not destroy the `PetscSection`, `Mat`, or `Vec`.
43469435951eSToby Isaac 
4347db781477SPatrick Sanan .seealso: `DMSetDefaultConstraints()`
43489435951eSToby Isaac @*/
4349d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat, Vec *bias)
4350d71ae5a4SJacob Faibussowitsch {
43519435951eSToby Isaac   PetscFunctionBegin;
43529435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4353dbbe0bcdSBarry Smith   if (!dm->defaultConstraint.section && !dm->defaultConstraint.mat && dm->ops->createdefaultconstraints) PetscUseTypeMethod(dm, createdefaultconstraints);
43543b8ba7d1SJed Brown   if (section) *section = dm->defaultConstraint.section;
43553b8ba7d1SJed Brown   if (mat) *mat = dm->defaultConstraint.mat;
435679769bd5SJed Brown   if (bias) *bias = dm->defaultConstraint.bias;
43573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43589435951eSToby Isaac }
43599435951eSToby Isaac 
43609435951eSToby Isaac /*@
4361bb7acecfSBarry Smith   DMSetDefaultConstraints - Set the `PetscSection` and `Mat` that specify the local constraint interpolation.
43629435951eSToby Isaac 
4363*20f4b53cSBarry Smith   Collective
4364e228b242SToby Isaac 
43659435951eSToby Isaac   Input Parameters:
4366bb7acecfSBarry Smith + dm - The `DM`
4367bb7acecfSBarry 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).
4368*20f4b53cSBarry 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).
4369*20f4b53cSBarry 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).
43709435951eSToby Isaac 
43719435951eSToby Isaac   Level: advanced
43729435951eSToby Isaac 
4373*20f4b53cSBarry Smith   Notes:
4374*20f4b53cSBarry 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()`.
4375*20f4b53cSBarry Smith 
4376*20f4b53cSBarry 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.
4377*20f4b53cSBarry Smith 
4378bb7acecfSBarry Smith   This increments the references of the `PetscSection`, `Mat`, and `Vec`, so they user can destroy them.
43799435951eSToby Isaac 
4380db781477SPatrick Sanan .seealso: `DMGetDefaultConstraints()`
43819435951eSToby Isaac @*/
4382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat, Vec bias)
4383d71ae5a4SJacob Faibussowitsch {
4384e228b242SToby Isaac   PetscMPIInt result;
43859435951eSToby Isaac 
43869435951eSToby Isaac   PetscFunctionBegin;
43879435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4388e228b242SToby Isaac   if (section) {
4389e228b242SToby Isaac     PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43909566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)section), &result));
43917a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint section must have local communicator");
4392e228b242SToby Isaac   }
4393e228b242SToby Isaac   if (mat) {
4394e228b242SToby Isaac     PetscValidHeaderSpecific(mat, MAT_CLASSID, 3);
43959566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)mat), &result));
43967a8be351SBarry Smith     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint matrix must have local communicator");
4397e228b242SToby Isaac   }
439879769bd5SJed Brown   if (bias) {
439979769bd5SJed Brown     PetscValidHeaderSpecific(bias, VEC_CLASSID, 4);
44009566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_compare(PETSC_COMM_SELF, PetscObjectComm((PetscObject)bias), &result));
440179769bd5SJed Brown     PetscCheck(result == MPI_CONGRUENT || result == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "constraint bias must have local communicator");
440279769bd5SJed Brown   }
44039566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
44049566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->defaultConstraint.section));
44053b8ba7d1SJed Brown   dm->defaultConstraint.section = section;
44069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mat));
44079566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&dm->defaultConstraint.mat));
44083b8ba7d1SJed Brown   dm->defaultConstraint.mat = mat;
44099566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)bias));
44109566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&dm->defaultConstraint.bias));
441179769bd5SJed Brown   dm->defaultConstraint.bias = bias;
44123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44139435951eSToby Isaac }
44149435951eSToby Isaac 
4415497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4416507e4973SMatthew G. Knepley /*
4417bb7acecfSBarry Smith   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections. Generates and error if they are not consistent.
4418507e4973SMatthew G. Knepley 
4419507e4973SMatthew G. Knepley   Input Parameters:
4420bb7acecfSBarry Smith + dm - The `DM`
4421bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4422bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
4423507e4973SMatthew G. Knepley 
4424507e4973SMatthew G. Knepley   Level: intermediate
4425507e4973SMatthew G. Knepley 
4426db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`
4427507e4973SMatthew G. Knepley */
4428d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4429d71ae5a4SJacob Faibussowitsch {
4430507e4973SMatthew G. Knepley   MPI_Comm        comm;
4431507e4973SMatthew G. Knepley   PetscLayout     layout;
4432507e4973SMatthew G. Knepley   const PetscInt *ranges;
4433507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4434507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4435507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4436507e4973SMatthew G. Knepley 
4437507e4973SMatthew G. Knepley   PetscFunctionBegin;
44389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4439507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
44419566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(globalSection, &pStart, &pEnd));
44439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetConstrainedStorageSize(globalSection, &nroots));
44449566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(comm, &layout));
44459566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
44469566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, nroots));
44479566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetUp(layout));
44489566063dSJacob Faibussowitsch   PetscCall(PetscLayoutGetRanges(layout, &ranges));
4449507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4450f741bcd2SMatthew G. Knepley     PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d;
4451507e4973SMatthew G. Knepley 
44529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(localSection, p, &dof));
44539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(localSection, p, &off));
44549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(localSection, p, &cdof));
44559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(globalSection, p, &gdof));
44569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetConstraintDof(globalSection, p, &gcdof));
44579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(globalSection, p, &goff));
4458507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
44599371c9d4SSatish Balay     if ((gdof < 0 ? -(gdof + 1) : gdof) != dof) {
44609371c9d4SSatish 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));
44619371c9d4SSatish Balay       valid = PETSC_FALSE;
44629371c9d4SSatish Balay     }
44639371c9d4SSatish Balay     if (gcdof && (gcdof != cdof)) {
44649371c9d4SSatish 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));
44659371c9d4SSatish Balay       valid = PETSC_FALSE;
44669371c9d4SSatish Balay     }
4467507e4973SMatthew G. Knepley     if (gdof < 0) {
4468507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof + 1) - gcdof : gdof - gcdof;
4469507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4470507e4973SMatthew G. Knepley         PetscInt offset = -(goff + 1) + d, r;
4471507e4973SMatthew G. Knepley 
44729566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(offset, size + 1, ranges, &r));
4473507e4973SMatthew G. Knepley         if (r < 0) r = -(r + 2);
44749371c9d4SSatish Balay         if ((r < 0) || (r >= size)) {
44759371c9d4SSatish Balay           PetscCall(PetscSynchronizedPrintf(comm, "[%d]Point %" PetscInt_FMT " mapped to invalid process %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", rank, p, r, gdof, goff));
44769371c9d4SSatish Balay           valid = PETSC_FALSE;
44779371c9d4SSatish Balay           break;
44789371c9d4SSatish Balay         }
4479507e4973SMatthew G. Knepley       }
4480507e4973SMatthew G. Knepley     }
4481507e4973SMatthew G. Knepley   }
44829566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
44839566063dSJacob Faibussowitsch   PetscCall(PetscSynchronizedFlush(comm, NULL));
44841c2dc1cbSBarry Smith   PetscCall(MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm));
4485507e4973SMatthew G. Knepley   if (!gvalid) {
44869566063dSJacob Faibussowitsch     PetscCall(DMView(dm, NULL));
4487507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4488507e4973SMatthew G. Knepley   }
44893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4490507e4973SMatthew G. Knepley }
4491f741bcd2SMatthew G. Knepley #endif
4492507e4973SMatthew G. Knepley 
44935f06a3ddSJed Brown static PetscErrorCode DMGetIsoperiodicPointSF_Internal(DM dm, PetscSF *sf)
44946725e60dSJed Brown {
44956725e60dSJed Brown   PetscErrorCode (*f)(DM, PetscSF *);
44966725e60dSJed Brown   PetscFunctionBegin;
44976725e60dSJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44986725e60dSJed Brown   PetscValidPointer(sf, 2);
44995f06a3ddSJed Brown   PetscCall(PetscObjectQueryFunction((PetscObject)dm, "DMGetIsoperiodicPointSF_C", &f));
45006725e60dSJed Brown   if (f) PetscCall(f(dm, sf));
45016725e60dSJed Brown   else *sf = dm->sf;
45023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45036725e60dSJed Brown }
45046725e60dSJed Brown 
450588ed4aceSMatthew G Knepley /*@
4506bb7acecfSBarry Smith   DMGetGlobalSection - Get the `PetscSection` encoding the global data layout for the `DM`.
450788ed4aceSMatthew G Knepley 
4508*20f4b53cSBarry Smith   Collective
45098b1ab98fSJed Brown 
451088ed4aceSMatthew G Knepley   Input Parameter:
4511bb7acecfSBarry Smith . dm - The `DM`
451288ed4aceSMatthew G Knepley 
451388ed4aceSMatthew G Knepley   Output Parameter:
4514bb7acecfSBarry Smith . section - The `PetscSection`
451588ed4aceSMatthew G Knepley 
451688ed4aceSMatthew G Knepley   Level: intermediate
451788ed4aceSMatthew G Knepley 
4518bb7acecfSBarry Smith   Note:
4519bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSection`.
452088ed4aceSMatthew G Knepley 
4521db781477SPatrick Sanan .seealso: `DMSetLocalSection()`, `DMGetLocalSection()`
452288ed4aceSMatthew G Knepley @*/
4523d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
4524d71ae5a4SJacob Faibussowitsch {
452588ed4aceSMatthew G Knepley   PetscFunctionBegin;
452688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
45281bb6d2a8SBarry Smith   if (!dm->globalSection) {
4529fd59a867SMatthew G. Knepley     PetscSection s;
45306725e60dSJed Brown     PetscSF      sf;
4531fd59a867SMatthew G. Knepley 
45329566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &s));
45337a8be351SBarry Smith     PetscCheck(s, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
45347a8be351SBarry Smith     PetscCheck(dm->sf, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
45355f06a3ddSJed Brown     PetscCall(DMGetIsoperiodicPointSF_Internal(dm, &sf));
45366725e60dSJed Brown     PetscCall(PetscSectionCreateGlobalSection(s, sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection));
45379566063dSJacob Faibussowitsch     PetscCall(PetscLayoutDestroy(&dm->map));
45389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map));
45399566063dSJacob Faibussowitsch     PetscCall(PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view"));
454088ed4aceSMatthew G Knepley   }
45411bb6d2a8SBarry Smith   *section = dm->globalSection;
45423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
454388ed4aceSMatthew G Knepley }
454488ed4aceSMatthew G Knepley 
4545b21d0597SMatthew G Knepley /*@
4546bb7acecfSBarry Smith   DMSetGlobalSection - Set the `PetscSection` encoding the global data layout for the `DM`.
4547b21d0597SMatthew G Knepley 
4548b21d0597SMatthew G Knepley   Input Parameters:
4549bb7acecfSBarry Smith + dm - The `DM`
45505080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4551b21d0597SMatthew G Knepley 
4552b21d0597SMatthew G Knepley   Level: intermediate
4553b21d0597SMatthew G Knepley 
4554bb7acecfSBarry Smith   Note:
4555bb7acecfSBarry Smith   Any existing `PetscSection` will be destroyed
4556b21d0597SMatthew G Knepley 
4557db781477SPatrick Sanan .seealso: `DMGetGlobalSection()`, `DMSetLocalSection()`
4558b21d0597SMatthew G Knepley @*/
4559d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
4560d71ae5a4SJacob Faibussowitsch {
4561b21d0597SMatthew G Knepley   PetscFunctionBegin;
4562b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45635080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
45649566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)section));
45659566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&dm->globalSection));
45661bb6d2a8SBarry Smith   dm->globalSection = section;
4567497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
45689566063dSJacob Faibussowitsch   if (section) PetscCall(DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section));
4569507e4973SMatthew G. Knepley #endif
45703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4571b21d0597SMatthew G Knepley }
4572b21d0597SMatthew G Knepley 
457388ed4aceSMatthew G Knepley /*@
4574bb7acecfSBarry Smith   DMGetSectionSF - Get the `PetscSF` encoding the parallel dof overlap for the `DM`. If it has not been set,
4575bb7acecfSBarry Smith   it is created from the default `PetscSection` layouts in the `DM`.
457688ed4aceSMatthew G Knepley 
457788ed4aceSMatthew G Knepley   Input Parameter:
4578bb7acecfSBarry Smith . dm - The `DM`
457988ed4aceSMatthew G Knepley 
458088ed4aceSMatthew G Knepley   Output Parameter:
4581bb7acecfSBarry Smith . sf - The `PetscSF`
458288ed4aceSMatthew G Knepley 
458388ed4aceSMatthew G Knepley   Level: intermediate
458488ed4aceSMatthew G Knepley 
4585bb7acecfSBarry Smith   Note:
4586bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
458788ed4aceSMatthew G Knepley 
4588db781477SPatrick Sanan .seealso: `DMSetSectionSF()`, `DMCreateSectionSF()`
458988ed4aceSMatthew G Knepley @*/
4590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
4591d71ae5a4SJacob Faibussowitsch {
459288ed4aceSMatthew G Knepley   PetscInt nroots;
459388ed4aceSMatthew G Knepley 
459488ed4aceSMatthew G Knepley   PetscFunctionBegin;
459588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
459688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
459748a46eb9SPierre Jolivet   if (!dm->sectionSF) PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &dm->sectionSF));
45989566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL));
459988ed4aceSMatthew G Knepley   if (nroots < 0) {
460088ed4aceSMatthew G Knepley     PetscSection section, gSection;
460188ed4aceSMatthew G Knepley 
46029566063dSJacob Faibussowitsch     PetscCall(DMGetLocalSection(dm, &section));
460331ea6d37SMatthew G Knepley     if (section) {
46049566063dSJacob Faibussowitsch       PetscCall(DMGetGlobalSection(dm, &gSection));
46059566063dSJacob Faibussowitsch       PetscCall(DMCreateSectionSF(dm, section, gSection));
460631ea6d37SMatthew G Knepley     } else {
46070298fd71SBarry Smith       *sf = NULL;
46083ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
460931ea6d37SMatthew G Knepley     }
461088ed4aceSMatthew G Knepley   }
46111bb6d2a8SBarry Smith   *sf = dm->sectionSF;
46123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
461388ed4aceSMatthew G Knepley }
461488ed4aceSMatthew G Knepley 
461588ed4aceSMatthew G Knepley /*@
4616bb7acecfSBarry Smith   DMSetSectionSF - Set the `PetscSF` encoding the parallel dof overlap for the `DM`
461788ed4aceSMatthew G Knepley 
461888ed4aceSMatthew G Knepley   Input Parameters:
4619bb7acecfSBarry Smith + dm - The `DM`
4620bb7acecfSBarry Smith - sf - The `PetscSF`
462188ed4aceSMatthew G Knepley 
462288ed4aceSMatthew G Knepley   Level: intermediate
462388ed4aceSMatthew G Knepley 
4624bb7acecfSBarry Smith   Note:
4625bb7acecfSBarry Smith   Any previous `PetscSF` is destroyed
462688ed4aceSMatthew G Knepley 
4627db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMCreateSectionSF()`
462888ed4aceSMatthew G Knepley @*/
4629d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
4630d71ae5a4SJacob Faibussowitsch {
463188ed4aceSMatthew G Knepley   PetscFunctionBegin;
463288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4633b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
46349566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
46359566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sectionSF));
46361bb6d2a8SBarry Smith   dm->sectionSF = sf;
46373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
463888ed4aceSMatthew G Knepley }
463988ed4aceSMatthew G Knepley 
464088ed4aceSMatthew G Knepley /*@C
4641bb7acecfSBarry Smith   DMCreateSectionSF - Create the `PetscSF` encoding the parallel dof overlap for the `DM` based upon the `PetscSection`s
464288ed4aceSMatthew G Knepley   describing the data layout.
464388ed4aceSMatthew G Knepley 
464488ed4aceSMatthew G Knepley   Input Parameters:
4645bb7acecfSBarry Smith + dm - The `DM`
4646bb7acecfSBarry Smith . localSection - `PetscSection` describing the local data layout
4647bb7acecfSBarry Smith - globalSection - `PetscSection` describing the global data layout
464888ed4aceSMatthew G Knepley 
46491bb6d2a8SBarry Smith   Level: developer
46501bb6d2a8SBarry Smith 
4651bb7acecfSBarry Smith   Note:
4652bb7acecfSBarry Smith   One usually uses `DMGetSectionSF()` to obtain the `PetscSF`
4653bb7acecfSBarry Smith 
4654bb7acecfSBarry Smith   Developer Note:
4655bb7acecfSBarry Smith   Since this routine has for arguments the two sections from the `DM` and puts the resulting `PetscSF`
4656bb7acecfSBarry Smith   directly into the `DM`, perhaps this function should not take the local and global sections as
4657bb7acecfSBarry Smith   input and should just obtain them from the `DM`?
46581bb6d2a8SBarry Smith 
4659db781477SPatrick Sanan .seealso: `DMGetSectionSF()`, `DMSetSectionSF()`, `DMGetLocalSection()`, `DMGetGlobalSection()`
466088ed4aceSMatthew G Knepley @*/
4661d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
4662d71ae5a4SJacob Faibussowitsch {
466388ed4aceSMatthew G Knepley   PetscFunctionBegin;
466488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46659566063dSJacob Faibussowitsch   PetscCall(PetscSFSetGraphSection(dm->sectionSF, localSection, globalSection));
46663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
466788ed4aceSMatthew G Knepley }
4668af122d2aSMatthew G Knepley 
4669b21d0597SMatthew G Knepley /*@
4670bb7acecfSBarry Smith   DMGetPointSF - Get the `PetscSF` encoding the parallel section point overlap for the `DM`.
4671bb7acecfSBarry Smith 
4672bb7acecfSBarry Smith   Not collective but the resulting `PetscSF` is collective
4673b21d0597SMatthew G Knepley 
4674b21d0597SMatthew G Knepley   Input Parameter:
4675bb7acecfSBarry Smith . dm - The `DM`
4676b21d0597SMatthew G Knepley 
4677b21d0597SMatthew G Knepley   Output Parameter:
4678bb7acecfSBarry Smith . sf - The `PetscSF`
4679b21d0597SMatthew G Knepley 
4680b21d0597SMatthew G Knepley   Level: intermediate
4681b21d0597SMatthew G Knepley 
4682bb7acecfSBarry Smith   Note:
4683bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
4684b21d0597SMatthew G Knepley 
4685db781477SPatrick Sanan .seealso: `DMSetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4686b21d0597SMatthew G Knepley @*/
4687d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4688d71ae5a4SJacob Faibussowitsch {
4689b21d0597SMatthew G Knepley   PetscFunctionBegin;
4690b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4691b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4692b21d0597SMatthew G Knepley   *sf = dm->sf;
46933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4694b21d0597SMatthew G Knepley }
4695b21d0597SMatthew G Knepley 
4696057b4bcdSMatthew G Knepley /*@
4697bb7acecfSBarry Smith   DMSetPointSF - Set the `PetscSF` encoding the parallel section point overlap for the `DM`.
4698bb7acecfSBarry Smith 
4699*20f4b53cSBarry Smith   Collective
4700057b4bcdSMatthew G Knepley 
4701057b4bcdSMatthew G Knepley   Input Parameters:
4702bb7acecfSBarry Smith + dm - The `DM`
4703bb7acecfSBarry Smith - sf - The `PetscSF`
4704057b4bcdSMatthew G Knepley 
4705057b4bcdSMatthew G Knepley   Level: intermediate
4706057b4bcdSMatthew G Knepley 
4707db781477SPatrick Sanan .seealso: `DMGetPointSF()`, `DMGetSectionSF()`, `DMSetSectionSF()`, `DMCreateSectionSF()`
4708057b4bcdSMatthew G Knepley @*/
4709d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4710d71ae5a4SJacob Faibussowitsch {
4711057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4712057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4713b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47159566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sf));
4716057b4bcdSMatthew G Knepley   dm->sf = sf;
47173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4718057b4bcdSMatthew G Knepley }
4719057b4bcdSMatthew G Knepley 
47204f37162bSMatthew G. Knepley /*@
4721bb7acecfSBarry Smith   DMGetNaturalSF - Get the `PetscSF` encoding the map back to the original mesh ordering
47224f37162bSMatthew G. Knepley 
47234f37162bSMatthew G. Knepley   Input Parameter:
4724bb7acecfSBarry Smith . dm - The `DM`
47254f37162bSMatthew G. Knepley 
47264f37162bSMatthew G. Knepley   Output Parameter:
4727bb7acecfSBarry Smith . sf - The `PetscSF`
47284f37162bSMatthew G. Knepley 
47294f37162bSMatthew G. Knepley   Level: intermediate
47304f37162bSMatthew G. Knepley 
4731bb7acecfSBarry Smith   Note:
4732bb7acecfSBarry Smith   This gets a borrowed reference, so the user should not destroy this `PetscSF`.
47334f37162bSMatthew G. Knepley 
4734db781477SPatrick Sanan .seealso: `DMSetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47354f37162bSMatthew G. Knepley @*/
4736d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNaturalSF(DM dm, PetscSF *sf)
4737d71ae5a4SJacob Faibussowitsch {
47384f37162bSMatthew G. Knepley   PetscFunctionBegin;
47394f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47404f37162bSMatthew G. Knepley   PetscValidPointer(sf, 2);
47414f37162bSMatthew G. Knepley   *sf = dm->sfNatural;
47423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47434f37162bSMatthew G. Knepley }
47444f37162bSMatthew G. Knepley 
47454f37162bSMatthew G. Knepley /*@
47464f37162bSMatthew G. Knepley   DMSetNaturalSF - Set the PetscSF encoding the map back to the original mesh ordering
47474f37162bSMatthew G. Knepley 
47484f37162bSMatthew G. Knepley   Input Parameters:
47494f37162bSMatthew G. Knepley + dm - The DM
47504f37162bSMatthew G. Knepley - sf - The PetscSF
47514f37162bSMatthew G. Knepley 
47524f37162bSMatthew G. Knepley   Level: intermediate
47534f37162bSMatthew G. Knepley 
4754db781477SPatrick Sanan .seealso: `DMGetNaturalSF()`, `DMSetUseNatural()`, `DMGetUseNatural()`, `DMPlexCreateGlobalToNaturalSF()`, `DMPlexDistribute()`
47554f37162bSMatthew G. Knepley @*/
4756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNaturalSF(DM dm, PetscSF sf)
4757d71ae5a4SJacob Faibussowitsch {
47584f37162bSMatthew G. Knepley   PetscFunctionBegin;
47594f37162bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47604f37162bSMatthew G. Knepley   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
47619566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sf));
47629566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&dm->sfNatural));
47634f37162bSMatthew G. Knepley   dm->sfNatural = sf;
47643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47654f37162bSMatthew G. Knepley }
47664f37162bSMatthew G. Knepley 
4767d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
4768d71ae5a4SJacob Faibussowitsch {
476934aa8a36SMatthew G. Knepley   PetscClassId id;
477034aa8a36SMatthew G. Knepley 
477134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
47729566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId(disc, &id));
477334aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
47749566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
477534aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
47769566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE));
477717c1d62eSMatthew G. Knepley   } else {
47789566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE));
477934aa8a36SMatthew G. Knepley   }
47803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
478134aa8a36SMatthew G. Knepley }
478234aa8a36SMatthew G. Knepley 
4783d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4784d71ae5a4SJacob Faibussowitsch {
478544a7f3ddSMatthew G. Knepley   RegionField *tmpr;
478644a7f3ddSMatthew G. Knepley   PetscInt     Nf = dm->Nf, f;
478744a7f3ddSMatthew G. Knepley 
478844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
47893ba16761SJacob Faibussowitsch   if (Nf >= NfNew) PetscFunctionReturn(PETSC_SUCCESS);
47909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NfNew, &tmpr));
479144a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
47929371c9d4SSatish Balay   for (f = Nf; f < NfNew; ++f) {
47939371c9d4SSatish Balay     tmpr[f].disc        = NULL;
47949371c9d4SSatish Balay     tmpr[f].label       = NULL;
47959371c9d4SSatish Balay     tmpr[f].avoidTensor = PETSC_FALSE;
47969371c9d4SSatish Balay   }
47979566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
479844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
479944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
48003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
480144a7f3ddSMatthew G. Knepley }
480244a7f3ddSMatthew G. Knepley 
480344a7f3ddSMatthew G. Knepley /*@
4804*20f4b53cSBarry Smith   DMClearFields - Remove all fields from the `DM`
480544a7f3ddSMatthew G. Knepley 
4806*20f4b53cSBarry Smith   Logically Collective
480744a7f3ddSMatthew G. Knepley 
480844a7f3ddSMatthew G. Knepley   Input Parameter:
4809*20f4b53cSBarry Smith . dm - The `DM`
481044a7f3ddSMatthew G. Knepley 
481144a7f3ddSMatthew G. Knepley   Level: intermediate
481244a7f3ddSMatthew G. Knepley 
4813db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetNumFields()`, `DMSetField()`
481444a7f3ddSMatthew G. Knepley @*/
4815d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearFields(DM dm)
4816d71ae5a4SJacob Faibussowitsch {
481744a7f3ddSMatthew G. Knepley   PetscInt f;
481844a7f3ddSMatthew G. Knepley 
481944a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
482044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482144a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
48229566063dSJacob Faibussowitsch     PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
48239566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->fields[f].label));
482444a7f3ddSMatthew G. Knepley   }
48259566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->fields));
482644a7f3ddSMatthew G. Knepley   dm->fields = NULL;
482744a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
48283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
482944a7f3ddSMatthew G. Knepley }
483044a7f3ddSMatthew G. Knepley 
4831689b5837SMatthew G. Knepley /*@
4832*20f4b53cSBarry Smith   DMGetNumFields - Get the number of fields in the `DM`
4833689b5837SMatthew G. Knepley 
4834*20f4b53cSBarry Smith   Not Collective
4835689b5837SMatthew G. Knepley 
4836689b5837SMatthew G. Knepley   Input Parameter:
4837*20f4b53cSBarry Smith . dm - The `DM`
4838689b5837SMatthew G. Knepley 
4839689b5837SMatthew G. Knepley   Output Parameter:
4840689b5837SMatthew G. Knepley . Nf - The number of fields
4841689b5837SMatthew G. Knepley 
4842689b5837SMatthew G. Knepley   Level: intermediate
4843689b5837SMatthew G. Knepley 
4844db781477SPatrick Sanan .seealso: `DMSetNumFields()`, `DMSetField()`
4845689b5837SMatthew G. Knepley @*/
4846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4847d71ae5a4SJacob Faibussowitsch {
48480f21e855SMatthew G. Knepley   PetscFunctionBegin;
48490f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4850534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
485144a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
48523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4853af122d2aSMatthew G Knepley }
4854af122d2aSMatthew G Knepley 
4855689b5837SMatthew G. Knepley /*@
4856*20f4b53cSBarry Smith   DMSetNumFields - Set the number of fields in the `DM`
4857689b5837SMatthew G. Knepley 
4858*20f4b53cSBarry Smith   Logically Collective
4859689b5837SMatthew G. Knepley 
4860689b5837SMatthew G. Knepley   Input Parameters:
4861*20f4b53cSBarry Smith + dm - The `DM`
4862689b5837SMatthew G. Knepley - Nf - The number of fields
4863689b5837SMatthew G. Knepley 
4864689b5837SMatthew G. Knepley   Level: intermediate
4865689b5837SMatthew G. Knepley 
4866db781477SPatrick Sanan .seealso: `DMGetNumFields()`, `DMSetField()`
4867689b5837SMatthew G. Knepley @*/
4868d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4869d71ae5a4SJacob Faibussowitsch {
48700f21e855SMatthew G. Knepley   PetscInt Nf, f;
4871af122d2aSMatthew G Knepley 
4872af122d2aSMatthew G Knepley   PetscFunctionBegin;
4873af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
48749566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
48750f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
48760f21e855SMatthew G. Knepley     PetscContainer obj;
48770f21e855SMatthew G. Knepley 
48789566063dSJacob Faibussowitsch     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)dm), &obj));
48799566063dSJacob Faibussowitsch     PetscCall(DMAddField(dm, NULL, (PetscObject)obj));
48809566063dSJacob Faibussowitsch     PetscCall(PetscContainerDestroy(&obj));
4881af122d2aSMatthew G Knepley   }
48823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4883af122d2aSMatthew G Knepley }
4884af122d2aSMatthew G Knepley 
4885c1929be8SMatthew G. Knepley /*@
4886bb7acecfSBarry Smith   DMGetField - Return the `DMLabel` and discretization object for a given `DM` field
4887c1929be8SMatthew G. Knepley 
4888*20f4b53cSBarry Smith   Not Collective
4889c1929be8SMatthew G. Knepley 
4890c1929be8SMatthew G. Knepley   Input Parameters:
4891bb7acecfSBarry Smith + dm - The `DM`
4892c1929be8SMatthew G. Knepley - f  - The field number
4893c1929be8SMatthew G. Knepley 
489444a7f3ddSMatthew G. Knepley   Output Parameters:
4895*20f4b53cSBarry Smith + label - The label indicating the support of the field, or `NULL` for the entire mesh (pass in `NULL` if not needed)
4896*20f4b53cSBarry Smith - disc - The discretization object (pass in `NULL` if not needed)
4897c1929be8SMatthew G. Knepley 
489844a7f3ddSMatthew G. Knepley   Level: intermediate
4899c1929be8SMatthew G. Knepley 
4900db781477SPatrick Sanan .seealso: `DMAddField()`, `DMSetField()`
4901c1929be8SMatthew G. Knepley @*/
4902d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *disc)
4903d71ae5a4SJacob Faibussowitsch {
4904af122d2aSMatthew G Knepley   PetscFunctionBegin;
4905af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4906bb7acecfSBarry Smith   PetscValidPointer(disc, 4);
49077a8be351SBarry 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);
490844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
4909bb7acecfSBarry Smith   if (disc) *disc = dm->fields[f].disc;
49103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4911decb47aaSMatthew G. Knepley }
4912decb47aaSMatthew G. Knepley 
4913083401c6SMatthew G. Knepley /* Does not clear the DS */
4914d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4915d71ae5a4SJacob Faibussowitsch {
4916083401c6SMatthew G. Knepley   PetscFunctionBegin;
49179566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, f + 1));
49189566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->fields[f].label));
49199566063dSJacob Faibussowitsch   PetscCall(PetscObjectDestroy(&dm->fields[f].disc));
4920083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4921bb7acecfSBarry Smith   dm->fields[f].disc  = disc;
49229566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4923bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
49243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4925083401c6SMatthew G. Knepley }
4926083401c6SMatthew G. Knepley 
492746560f82SMatthew G. Knepley /*@C
4928bb7acecfSBarry Smith   DMSetField - Set the discretization object for a given `DM` field. Usually one would call `DMAddField()` which automatically handles
4929bb7acecfSBarry Smith   the field numbering.
4930c1929be8SMatthew G. Knepley 
4931*20f4b53cSBarry Smith   Logically Collective
4932c1929be8SMatthew G. Knepley 
4933c1929be8SMatthew G. Knepley   Input Parameters:
4934bb7acecfSBarry Smith + dm    - The `DM`
4935c1929be8SMatthew G. Knepley . f     - The field number
4936*20f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4937bb7acecfSBarry Smith - disc - The discretization object
4938c1929be8SMatthew G. Knepley 
493944a7f3ddSMatthew G. Knepley   Level: intermediate
4940c1929be8SMatthew G. Knepley 
4941db781477SPatrick Sanan .seealso: `DMAddField()`, `DMGetField()`
4942c1929be8SMatthew G. Knepley @*/
4943d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject disc)
4944d71ae5a4SJacob Faibussowitsch {
4945decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4946decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4947e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4948bb7acecfSBarry Smith   PetscValidHeader(disc, 4);
49497a8be351SBarry Smith   PetscCheck(f >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be non-negative", f);
4950bb7acecfSBarry Smith   PetscCall(DMSetField_Internal(dm, f, label, disc));
4951bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, f, disc));
49529566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
495444a7f3ddSMatthew G. Knepley }
495544a7f3ddSMatthew G. Knepley 
495646560f82SMatthew G. Knepley /*@C
4957bb7acecfSBarry 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)
4958bb7acecfSBarry Smith   and a discretization object that defines the function space associated with those points.
495944a7f3ddSMatthew G. Knepley 
4960*20f4b53cSBarry Smith   Logically Collective
496144a7f3ddSMatthew G. Knepley 
496244a7f3ddSMatthew G. Knepley   Input Parameters:
4963bb7acecfSBarry Smith + dm    - The `DM`
4964*20f4b53cSBarry Smith . label - The label indicating the support of the field, or `NULL` for the entire mesh
4965bb7acecfSBarry Smith - disc - The discretization object
496644a7f3ddSMatthew G. Knepley 
496744a7f3ddSMatthew G. Knepley   Level: intermediate
496844a7f3ddSMatthew G. Knepley 
4969bb7acecfSBarry Smith   Notes:
4970bb7acecfSBarry Smith   The label already exists or will be added to the `DM` with `DMSetLabel()`.
4971bb7acecfSBarry Smith 
4972da81f932SPierre Jolivet   For example, a piecewise continuous pressure field can be defined by coefficients at the cell centers of a mesh and piecewise constant functions
4973bb7acecfSBarry 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
4974bb7acecfSBarry Smith   geometry entities, a `DMLabel` indicating a subset of those geometric entities, and a discretization object, such as a `PetscFE`.
4975bb7acecfSBarry Smith 
4976bb7acecfSBarry Smith .seealso: `DMSetLabel()`, `DMSetField()`, `DMGetField()`, `PetscFE`
497744a7f3ddSMatthew G. Knepley @*/
4978d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject disc)
4979d71ae5a4SJacob Faibussowitsch {
498044a7f3ddSMatthew G. Knepley   PetscInt Nf = dm->Nf;
498144a7f3ddSMatthew G. Knepley 
498244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
498344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4984064a246eSJacob Faibussowitsch   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4985bb7acecfSBarry Smith   PetscValidHeader(disc, 3);
49869566063dSJacob Faibussowitsch   PetscCall(DMFieldEnlarge_Static(dm, Nf + 1));
498744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
4988bb7acecfSBarry Smith   dm->fields[Nf].disc  = disc;
49899566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
4990bb7acecfSBarry Smith   PetscCall(PetscObjectReference((PetscObject)disc));
4991bb7acecfSBarry Smith   PetscCall(DMSetDefaultAdjacency_Private(dm, Nf, disc));
49929566063dSJacob Faibussowitsch   PetscCall(DMClearDS(dm));
49933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4994af122d2aSMatthew G Knepley }
49956636e97aSMatthew G Knepley 
4996e5e52638SMatthew G. Knepley /*@
4997e0b68406SMatthew Knepley   DMSetFieldAvoidTensor - Set flag to avoid defining the field on tensor cells
4998e0b68406SMatthew Knepley 
4999*20f4b53cSBarry Smith   Logically Collective
5000e0b68406SMatthew Knepley 
5001e0b68406SMatthew Knepley   Input Parameters:
5002bb7acecfSBarry Smith + dm          - The `DM`
5003e0b68406SMatthew Knepley . f           - The field index
5004bb7acecfSBarry Smith - avoidTensor - `PETSC_TRUE` to skip defining the field on tensor cells
5005e0b68406SMatthew Knepley 
5006e0b68406SMatthew Knepley   Level: intermediate
5007e0b68406SMatthew Knepley 
5008db781477SPatrick Sanan .seealso: `DMGetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5009e0b68406SMatthew Knepley @*/
5010d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFieldAvoidTensor(DM dm, PetscInt f, PetscBool avoidTensor)
5011d71ae5a4SJacob Faibussowitsch {
5012e0b68406SMatthew Knepley   PetscFunctionBegin;
501363a3b9bcSJacob 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);
5014e0b68406SMatthew Knepley   dm->fields[f].avoidTensor = avoidTensor;
50153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5016e0b68406SMatthew Knepley }
5017e0b68406SMatthew Knepley 
5018e0b68406SMatthew Knepley /*@
5019e0b68406SMatthew Knepley   DMGetFieldAvoidTensor - Get flag to avoid defining the field on tensor cells
5020e0b68406SMatthew Knepley 
5021*20f4b53cSBarry Smith   Not Collective
5022e0b68406SMatthew Knepley 
5023e0b68406SMatthew Knepley   Input Parameters:
5024bb7acecfSBarry Smith + dm          - The `DM`
5025e0b68406SMatthew Knepley - f           - The field index
5026e0b68406SMatthew Knepley 
5027e0b68406SMatthew Knepley   Output Parameter:
5028e0b68406SMatthew Knepley . avoidTensor - The flag to avoid defining the field on tensor cells
5029e0b68406SMatthew Knepley 
5030e0b68406SMatthew Knepley   Level: intermediate
5031e0b68406SMatthew Knepley 
5032bb7acecfSBarry Smith  .seealso: `DMAddField()`, `DMSetField()`, `DMGetField()`, `DMSetFieldAvoidTensor()`, `DMSetField()`, `DMGetField()`
5033e0b68406SMatthew Knepley @*/
5034d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFieldAvoidTensor(DM dm, PetscInt f, PetscBool *avoidTensor)
5035d71ae5a4SJacob Faibussowitsch {
5036e0b68406SMatthew Knepley   PetscFunctionBegin;
503763a3b9bcSJacob 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);
5038e0b68406SMatthew Knepley   *avoidTensor = dm->fields[f].avoidTensor;
50393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5040e0b68406SMatthew Knepley }
5041e0b68406SMatthew Knepley 
5042e0b68406SMatthew Knepley /*@
5043bb7acecfSBarry Smith   DMCopyFields - Copy the discretizations for the `DM` into another `DM`
5044e5e52638SMatthew G. Knepley 
5045*20f4b53cSBarry Smith   Collective
5046e5e52638SMatthew G. Knepley 
5047e5e52638SMatthew G. Knepley   Input Parameter:
5048bb7acecfSBarry Smith . dm - The `DM`
5049e5e52638SMatthew G. Knepley 
5050e5e52638SMatthew G. Knepley   Output Parameter:
5051bb7acecfSBarry Smith . newdm - The `DM`
5052e5e52638SMatthew G. Knepley 
5053e5e52638SMatthew G. Knepley   Level: advanced
5054e5e52638SMatthew G. Knepley 
5055db781477SPatrick Sanan .seealso: `DMGetField()`, `DMSetField()`, `DMAddField()`, `DMCopyDS()`, `DMGetDS()`, `DMGetCellDS()`
5056e5e52638SMatthew G. Knepley @*/
5057d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyFields(DM dm, DM newdm)
5058d71ae5a4SJacob Faibussowitsch {
5059e5e52638SMatthew G. Knepley   PetscInt Nf, f;
5060e5e52638SMatthew G. Knepley 
5061e5e52638SMatthew G. Knepley   PetscFunctionBegin;
50623ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
50639566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
50649566063dSJacob Faibussowitsch   PetscCall(DMClearFields(newdm));
5065e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5066e5e52638SMatthew G. Knepley     DMLabel     label;
5067e5e52638SMatthew G. Knepley     PetscObject field;
506834aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
5069e5e52638SMatthew G. Knepley 
50709566063dSJacob Faibussowitsch     PetscCall(DMGetField(dm, f, &label, &field));
50719566063dSJacob Faibussowitsch     PetscCall(DMSetField(newdm, f, label, field));
50729566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, f, &useCone, &useClosure));
50739566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(newdm, f, useCone, useClosure));
507434aa8a36SMatthew G. Knepley   }
50753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
507634aa8a36SMatthew G. Knepley }
507734aa8a36SMatthew G. Knepley 
507834aa8a36SMatthew G. Knepley /*@
507934aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
508034aa8a36SMatthew G. Knepley 
5081*20f4b53cSBarry Smith   Not Collective
508234aa8a36SMatthew G. Knepley 
508334aa8a36SMatthew G. Knepley   Input Parameters:
5084*20f4b53cSBarry Smith + dm - The `DM` object
5085*20f4b53cSBarry Smith - f  - The field number, or `PETSC_DEFAULT` for the default adjacency
508634aa8a36SMatthew G. Knepley 
5087d8d19677SJose E. Roman   Output Parameters:
508834aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
508934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
509034aa8a36SMatthew G. Knepley 
509134aa8a36SMatthew G. Knepley   Level: developer
509234aa8a36SMatthew G. Knepley 
5093*20f4b53cSBarry Smith   Notes:
5094*20f4b53cSBarry Smith .vb
5095*20f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5096*20f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5097*20f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5098*20f4b53cSBarry Smith .ve
5099*20f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
5100*20f4b53cSBarry Smith 
5101db781477SPatrick Sanan .seealso: `DMSetAdjacency()`, `DMGetField()`, `DMSetField()`
510234aa8a36SMatthew G. Knepley @*/
5103d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
5104d71ae5a4SJacob Faibussowitsch {
510534aa8a36SMatthew G. Knepley   PetscFunctionBegin;
510634aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5107534a8f05SLisandro Dalcin   if (useCone) PetscValidBoolPointer(useCone, 3);
5108534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
510934aa8a36SMatthew G. Knepley   if (f < 0) {
511034aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->adjacency[0];
511134aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
511234aa8a36SMatthew G. Knepley   } else {
511334aa8a36SMatthew G. Knepley     PetscInt Nf;
511434aa8a36SMatthew G. Knepley 
51159566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51167a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
511734aa8a36SMatthew G. Knepley     if (useCone) *useCone = dm->fields[f].adjacency[0];
511834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
511934aa8a36SMatthew G. Knepley   }
51203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
512134aa8a36SMatthew G. Knepley }
512234aa8a36SMatthew G. Knepley 
512334aa8a36SMatthew G. Knepley /*@
512434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
512534aa8a36SMatthew G. Knepley 
5126*20f4b53cSBarry Smith   Not Collective
512734aa8a36SMatthew G. Knepley 
512834aa8a36SMatthew G. Knepley   Input Parameters:
5129*20f4b53cSBarry Smith + dm         - The `DM` object
513034aa8a36SMatthew G. Knepley . f          - The field number
513134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
513234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
513334aa8a36SMatthew G. Knepley 
513434aa8a36SMatthew G. Knepley   Level: developer
513534aa8a36SMatthew G. Knepley 
5136*20f4b53cSBarry Smith   Notes:
5137*20f4b53cSBarry Smith .vb
5138*20f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5139*20f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5140*20f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5141*20f4b53cSBarry Smith .ve
5142*20f4b53cSBarry Smith   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
5143*20f4b53cSBarry Smith 
5144db781477SPatrick Sanan .seealso: `DMGetAdjacency()`, `DMGetField()`, `DMSetField()`
514534aa8a36SMatthew G. Knepley @*/
5146d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
5147d71ae5a4SJacob Faibussowitsch {
514834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
514934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515034aa8a36SMatthew G. Knepley   if (f < 0) {
515134aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
515234aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
515334aa8a36SMatthew G. Knepley   } else {
515434aa8a36SMatthew G. Knepley     PetscInt Nf;
515534aa8a36SMatthew G. Knepley 
51569566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
51577a8be351SBarry Smith     PetscCheck(f < Nf, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", f, Nf);
515834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
515934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
5160e5e52638SMatthew G. Knepley   }
51613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5162e5e52638SMatthew G. Knepley }
5163e5e52638SMatthew G. Knepley 
5164b0441da4SMatthew G. Knepley /*@
5165b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
5166b0441da4SMatthew G. Knepley 
5167b0441da4SMatthew G. Knepley   Not collective
5168b0441da4SMatthew G. Knepley 
5169f899ff85SJose E. Roman   Input Parameter:
5170*20f4b53cSBarry Smith . dm - The `DM` object
5171b0441da4SMatthew G. Knepley 
5172d8d19677SJose E. Roman   Output Parameters:
5173b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
5174b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5175b0441da4SMatthew G. Knepley 
5176b0441da4SMatthew G. Knepley   Level: developer
5177b0441da4SMatthew G. Knepley 
5178*20f4b53cSBarry Smith   Notes:
5179*20f4b53cSBarry Smith .vb
5180*20f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5181*20f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5182*20f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5183*20f4b53cSBarry Smith .ve
5184*20f4b53cSBarry Smith 
5185db781477SPatrick Sanan .seealso: `DMSetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5186b0441da4SMatthew G. Knepley @*/
5187d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
5188d71ae5a4SJacob Faibussowitsch {
5189b0441da4SMatthew G. Knepley   PetscInt Nf;
5190b0441da4SMatthew G. Knepley 
5191b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5192b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5193064a246eSJacob Faibussowitsch   if (useCone) PetscValidBoolPointer(useCone, 2);
5194064a246eSJacob Faibussowitsch   if (useClosure) PetscValidBoolPointer(useClosure, 3);
51959566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5196b0441da4SMatthew G. Knepley   if (!Nf) {
51979566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5198b0441da4SMatthew G. Knepley   } else {
51999566063dSJacob Faibussowitsch     PetscCall(DMGetAdjacency(dm, 0, useCone, useClosure));
5200b0441da4SMatthew G. Knepley   }
52013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5202b0441da4SMatthew G. Knepley }
5203b0441da4SMatthew G. Knepley 
5204b0441da4SMatthew G. Knepley /*@
5205b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5206b0441da4SMatthew G. Knepley 
5207*20f4b53cSBarry Smith   Not Collective
5208b0441da4SMatthew G. Knepley 
5209b0441da4SMatthew G. Knepley   Input Parameters:
5210*20f4b53cSBarry Smith + dm         - The `DM` object
5211b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5212b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5213b0441da4SMatthew G. Knepley 
5214b0441da4SMatthew G. Knepley   Level: developer
5215b0441da4SMatthew G. Knepley 
5216*20f4b53cSBarry Smith   Notes:
5217*20f4b53cSBarry Smith .vb
5218*20f4b53cSBarry Smith      FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5219*20f4b53cSBarry Smith      FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5220*20f4b53cSBarry Smith      FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5221*20f4b53cSBarry Smith .ve
5222*20f4b53cSBarry Smith 
5223db781477SPatrick Sanan .seealso: `DMGetBasicAdjacency()`, `DMGetField()`, `DMSetField()`
5224b0441da4SMatthew G. Knepley @*/
5225d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5226d71ae5a4SJacob Faibussowitsch {
5227b0441da4SMatthew G. Knepley   PetscInt Nf;
5228b0441da4SMatthew G. Knepley 
5229b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5230b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52319566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
5232b0441da4SMatthew G. Knepley   if (!Nf) {
52339566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure));
5234b0441da4SMatthew G. Knepley   } else {
52359566063dSJacob Faibussowitsch     PetscCall(DMSetAdjacency(dm, 0, useCone, useClosure));
5236e5e52638SMatthew G. Knepley   }
52373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5238e5e52638SMatthew G. Knepley }
5239e5e52638SMatthew G. Knepley 
5240d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompleteBCLabels_Internal(DM dm)
5241d71ae5a4SJacob Faibussowitsch {
5242799db056SMatthew G. Knepley   DM           plex;
5243799db056SMatthew G. Knepley   DMLabel     *labels, *glabels;
5244799db056SMatthew G. Knepley   const char **names;
5245799db056SMatthew G. Knepley   char        *sendNames, *recvNames;
5246799db056SMatthew G. Knepley   PetscInt     Nds, s, maxLabels = 0, maxLen = 0, gmaxLen, Nl = 0, gNl, l, gl, m;
5247799db056SMatthew G. Knepley   size_t       len;
5248799db056SMatthew G. Knepley   MPI_Comm     comm;
5249799db056SMatthew G. Knepley   PetscMPIInt  rank, size, p, *counts, *displs;
5250783e2ec8SMatthew G. Knepley 
5251783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5252799db056SMatthew G. Knepley   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5253799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_size(comm, &size));
5254799db056SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5255799db056SMatthew G. Knepley   PetscCall(DMGetNumDS(dm, &Nds));
5256799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5257799db056SMatthew G. Knepley     PetscDS  dsBC;
5258799db056SMatthew G. Knepley     PetscInt numBd;
5259799db056SMatthew G. Knepley 
5260799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5261799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5262799db056SMatthew G. Knepley     maxLabels += numBd;
5263799db056SMatthew G. Knepley   }
5264799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(maxLabels, &labels));
5265799db056SMatthew G. Knepley   /* Get list of labels to be completed */
5266799db056SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5267799db056SMatthew G. Knepley     PetscDS  dsBC;
5268799db056SMatthew G. Knepley     PetscInt numBd, bd;
5269799db056SMatthew G. Knepley 
5270799db056SMatthew G. Knepley     PetscCall(DMGetRegionNumDS(dm, s, NULL, NULL, &dsBC));
5271799db056SMatthew G. Knepley     PetscCall(PetscDSGetNumBoundary(dsBC, &numBd));
5272799db056SMatthew G. Knepley     for (bd = 0; bd < numBd; ++bd) {
5273799db056SMatthew G. Knepley       DMLabel      label;
5274799db056SMatthew G. Knepley       PetscInt     field;
5275799db056SMatthew G. Knepley       PetscObject  obj;
5276799db056SMatthew G. Knepley       PetscClassId id;
5277799db056SMatthew G. Knepley 
5278799db056SMatthew G. Knepley       PetscCall(PetscDSGetBoundary(dsBC, bd, NULL, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
52799566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, field, NULL, &obj));
52809566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
5281799db056SMatthew G. Knepley       if (!(id == PETSCFE_CLASSID) || !label) continue;
52829371c9d4SSatish Balay       for (l = 0; l < Nl; ++l)
52839371c9d4SSatish Balay         if (labels[l] == label) break;
5284799db056SMatthew G. Knepley       if (l == Nl) labels[Nl++] = label;
5285783e2ec8SMatthew G. Knepley     }
5286799db056SMatthew G. Knepley   }
5287799db056SMatthew G. Knepley   /* Get label names */
5288799db056SMatthew G. Knepley   PetscCall(PetscMalloc1(Nl, &names));
5289799db056SMatthew G. Knepley   for (l = 0; l < Nl; ++l) PetscCall(PetscObjectGetName((PetscObject)labels[l], &names[l]));
52909371c9d4SSatish Balay   for (l = 0; l < Nl; ++l) {
52919371c9d4SSatish Balay     PetscCall(PetscStrlen(names[l], &len));
52929371c9d4SSatish Balay     maxLen = PetscMax(maxLen, (PetscInt)len + 2);
52939371c9d4SSatish Balay   }
5294799db056SMatthew G. Knepley   PetscCall(PetscFree(labels));
5295799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allreduce(&maxLen, &gmaxLen, 1, MPIU_INT, MPI_MAX, comm));
5296799db056SMatthew G. Knepley   PetscCall(PetscCalloc1(Nl * gmaxLen, &sendNames));
5297c6a7a370SJeremy L Thompson   for (l = 0; l < Nl; ++l) PetscCall(PetscStrncpy(&sendNames[gmaxLen * l], names[l], gmaxLen));
5298799db056SMatthew G. Knepley   PetscCall(PetscFree(names));
5299799db056SMatthew G. Knepley   /* Put all names on all processes */
5300799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(size, &counts, size + 1, &displs));
5301799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgather(&Nl, 1, MPI_INT, counts, 1, MPI_INT, comm));
5302799db056SMatthew G. Knepley   for (p = 0; p < size; ++p) displs[p + 1] = displs[p] + counts[p];
5303799db056SMatthew G. Knepley   gNl = displs[size];
53049371c9d4SSatish Balay   for (p = 0; p < size; ++p) {
53059371c9d4SSatish Balay     counts[p] *= gmaxLen;
53069371c9d4SSatish Balay     displs[p] *= gmaxLen;
53079371c9d4SSatish Balay   }
5308799db056SMatthew G. Knepley   PetscCall(PetscCalloc2(gNl * gmaxLen, &recvNames, gNl, &glabels));
5309799db056SMatthew G. Knepley   PetscCallMPI(MPI_Allgatherv(sendNames, counts[rank], MPI_CHAR, recvNames, counts, displs, MPI_CHAR, comm));
5310799db056SMatthew G. Knepley   PetscCall(PetscFree2(counts, displs));
5311799db056SMatthew G. Knepley   PetscCall(PetscFree(sendNames));
5312799db056SMatthew G. Knepley   for (l = 0, gl = 0; l < gNl; ++l) {
5313799db056SMatthew G. Knepley     PetscCall(DMGetLabel(dm, &recvNames[l * gmaxLen], &glabels[gl]));
5314799db056SMatthew G. Knepley     PetscCheck(glabels[gl], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Label %s missing on rank %d", &recvNames[l * gmaxLen], rank);
53159371c9d4SSatish Balay     for (m = 0; m < gl; ++m)
53169371c9d4SSatish Balay       if (glabels[m] == glabels[gl]) continue;
53179566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
5318799db056SMatthew G. Knepley     PetscCall(DMPlexLabelComplete(plex, glabels[gl]));
53199566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5320799db056SMatthew G. Knepley     ++gl;
5321783e2ec8SMatthew G. Knepley   }
5322799db056SMatthew G. Knepley   PetscCall(PetscFree2(recvNames, glabels));
53233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5324783e2ec8SMatthew G. Knepley }
5325783e2ec8SMatthew G. Knepley 
5326d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5327d71ae5a4SJacob Faibussowitsch {
5328e5e52638SMatthew G. Knepley   DMSpace *tmpd;
5329e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5330e5e52638SMatthew G. Knepley 
5331e5e52638SMatthew G. Knepley   PetscFunctionBegin;
53323ba16761SJacob Faibussowitsch   if (Nds >= NdsNew) PetscFunctionReturn(PETSC_SUCCESS);
53339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(NdsNew, &tmpd));
5334e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
53359371c9d4SSatish Balay   for (s = Nds; s < NdsNew; ++s) {
53369371c9d4SSatish Balay     tmpd[s].ds     = NULL;
53379371c9d4SSatish Balay     tmpd[s].label  = NULL;
53389371c9d4SSatish Balay     tmpd[s].fields = NULL;
53399371c9d4SSatish Balay   }
53409566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5341e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5342e5e52638SMatthew G. Knepley   dm->probs = tmpd;
53433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5344e5e52638SMatthew G. Knepley }
5345e5e52638SMatthew G. Knepley 
5346e5e52638SMatthew G. Knepley /*@
5347*20f4b53cSBarry Smith   DMGetNumDS - Get the number of discrete systems in the `DM`
5348e5e52638SMatthew G. Knepley 
5349*20f4b53cSBarry Smith   Not Collective
5350e5e52638SMatthew G. Knepley 
5351e5e52638SMatthew G. Knepley   Input Parameter:
5352*20f4b53cSBarry Smith . dm - The `DM`
5353e5e52638SMatthew G. Knepley 
5354e5e52638SMatthew G. Knepley   Output Parameter:
5355*20f4b53cSBarry Smith . Nds - The number of `PetscDS` objects
5356e5e52638SMatthew G. Knepley 
5357e5e52638SMatthew G. Knepley   Level: intermediate
5358e5e52638SMatthew G. Knepley 
5359db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMGetCellDS()`
5360e5e52638SMatthew G. Knepley @*/
5361d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5362d71ae5a4SJacob Faibussowitsch {
5363e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5364e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5365534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5366e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
53673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5368e5e52638SMatthew G. Knepley }
5369e5e52638SMatthew G. Knepley 
5370e5e52638SMatthew G. Knepley /*@
5371*20f4b53cSBarry Smith   DMClearDS - Remove all discrete systems from the `DM`
5372e5e52638SMatthew G. Knepley 
5373*20f4b53cSBarry Smith   Logically Collective
5374e5e52638SMatthew G. Knepley 
5375e5e52638SMatthew G. Knepley   Input Parameter:
5376*20f4b53cSBarry Smith . dm - The `DM`
5377e5e52638SMatthew G. Knepley 
5378e5e52638SMatthew G. Knepley   Level: intermediate
5379e5e52638SMatthew G. Knepley 
5380db781477SPatrick Sanan .seealso: `DMGetNumDS()`, `DMGetDS()`, `DMSetField()`
5381e5e52638SMatthew G. Knepley @*/
5382d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearDS(DM dm)
5383d71ae5a4SJacob Faibussowitsch {
5384e5e52638SMatthew G. Knepley   PetscInt s;
5385e5e52638SMatthew G. Knepley 
5386e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5387e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5388e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
53899566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[s].ds));
53909566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&dm->probs[s].label));
53919566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[s].fields));
5392e5e52638SMatthew G. Knepley   }
53939566063dSJacob Faibussowitsch   PetscCall(PetscFree(dm->probs));
5394e5e52638SMatthew G. Knepley   dm->probs = NULL;
5395e5e52638SMatthew G. Knepley   dm->Nds   = 0;
53963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5397e5e52638SMatthew G. Knepley }
5398e5e52638SMatthew G. Knepley 
5399e5e52638SMatthew G. Knepley /*@
5400*20f4b53cSBarry Smith   DMGetDS - Get the default `PetscDS`
5401e5e52638SMatthew G. Knepley 
5402*20f4b53cSBarry Smith   Not Collective
5403e5e52638SMatthew G. Knepley 
5404e5e52638SMatthew G. Knepley   Input Parameter:
5405*20f4b53cSBarry Smith . dm    - The `DM`
5406e5e52638SMatthew G. Knepley 
5407e5e52638SMatthew G. Knepley   Output Parameter:
5408*20f4b53cSBarry Smith . prob - The default `PetscDS`
5409e5e52638SMatthew G. Knepley 
5410e5e52638SMatthew G. Knepley   Level: intermediate
5411e5e52638SMatthew G. Knepley 
5412db781477SPatrick Sanan .seealso: `DMGetCellDS()`, `DMGetRegionDS()`
5413e5e52638SMatthew G. Knepley @*/
5414d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5415d71ae5a4SJacob Faibussowitsch {
5416e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5417e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5418e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5419b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5420b0143b4dSMatthew G. Knepley     PetscDS ds;
5421b0143b4dSMatthew G. Knepley 
54229566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
54239566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, NULL, ds));
54249566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5425b0143b4dSMatthew G. Knepley   }
5426b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
54273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5428e5e52638SMatthew G. Knepley }
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley /*@
5431*20f4b53cSBarry Smith   DMGetCellDS - Get the `PetscDS` defined on a given cell
5432e5e52638SMatthew G. Knepley 
5433*20f4b53cSBarry Smith   Not Collective
5434e5e52638SMatthew G. Knepley 
5435e5e52638SMatthew G. Knepley   Input Parameters:
5436*20f4b53cSBarry Smith + dm    - The `DM`
5437*20f4b53cSBarry Smith - point - Cell for the `PetscDS`
5438e5e52638SMatthew G. Knepley 
5439e5e52638SMatthew G. Knepley   Output Parameter:
5440*20f4b53cSBarry Smith . prob - The `PetscDS` defined on the given cell
5441e5e52638SMatthew G. Knepley 
5442e5e52638SMatthew G. Knepley   Level: developer
5443e5e52638SMatthew G. Knepley 
5444db781477SPatrick Sanan .seealso: `DMGetDS()`, `DMSetRegionDS()`
5445e5e52638SMatthew G. Knepley @*/
5446d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5447d71ae5a4SJacob Faibussowitsch {
5448e5e52638SMatthew G. Knepley   PetscDS  probDef = NULL;
5449e5e52638SMatthew G. Knepley   PetscInt s;
5450e5e52638SMatthew G. Knepley 
5451e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5452e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5453e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
545463a3b9bcSJacob Faibussowitsch   PetscCheck(point >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %" PetscInt_FMT, point);
5455e5e52638SMatthew G. Knepley   *prob = NULL;
5456e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5457e5e52638SMatthew G. Knepley     PetscInt val;
5458e5e52638SMatthew G. Knepley 
54599371c9d4SSatish Balay     if (!dm->probs[s].label) {
54609371c9d4SSatish Balay       probDef = dm->probs[s].ds;
54619371c9d4SSatish Balay     } else {
54629566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(dm->probs[s].label, point, &val));
54639371c9d4SSatish Balay       if (val >= 0) {
54649371c9d4SSatish Balay         *prob = dm->probs[s].ds;
54659371c9d4SSatish Balay         break;
54669371c9d4SSatish Balay       }
5467e5e52638SMatthew G. Knepley     }
5468e5e52638SMatthew G. Knepley   }
5469e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
54703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5471e5e52638SMatthew G. Knepley }
5472e5e52638SMatthew G. Knepley 
5473e5e52638SMatthew G. Knepley /*@
5474*20f4b53cSBarry Smith   DMGetRegionDS - Get the `PetscDS` for a given mesh region, defined by a `DMLabel`
5475e5e52638SMatthew G. Knepley 
5476*20f4b53cSBarry Smith   Not Collective
5477e5e52638SMatthew G. Knepley 
5478e5e52638SMatthew G. Knepley   Input Parameters:
5479*20f4b53cSBarry Smith + dm    - The `DM`
5480*20f4b53cSBarry Smith - label - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5481e5e52638SMatthew G. Knepley 
5482b3cf3223SMatthew G. Knepley   Output Parameters:
5483*20f4b53cSBarry Smith + fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
5484*20f4b53cSBarry Smith - prob - The `PetscDS` defined on the given region, or `NULL`
5485e5e52638SMatthew G. Knepley 
5486e5e52638SMatthew G. Knepley   Level: advanced
5487e5e52638SMatthew G. Knepley 
5488*20f4b53cSBarry Smith   Note:
5489*20f4b53cSBarry Smith   If a non-`NULL` label is given, but there is no `PetscDS` on that specific label,
5490*20f4b53cSBarry Smith   the `PetscDS` for the full domain (if present) is returned. Returns with
5491*20f4b53cSBarry Smith   fields = `NULL` and prob = `NULL` if there is no `PetscDS` for the full domain.
5492*20f4b53cSBarry Smith 
5493db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5494e5e52638SMatthew G. Knepley @*/
5495d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5496d71ae5a4SJacob Faibussowitsch {
5497e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5498e5e52638SMatthew G. Knepley 
5499e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5500e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5501e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
55029371c9d4SSatish Balay   if (fields) {
55039371c9d4SSatish Balay     PetscValidPointer(fields, 3);
55049371c9d4SSatish Balay     *fields = NULL;
55059371c9d4SSatish Balay   }
55069371c9d4SSatish Balay   if (ds) {
55079371c9d4SSatish Balay     PetscValidPointer(ds, 4);
55089371c9d4SSatish Balay     *ds = NULL;
55099371c9d4SSatish Balay   }
5510e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5511154ca461SJed Brown     if (dm->probs[s].label == label || !dm->probs[s].label) {
5512b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5513b3cf3223SMatthew G. Knepley       if (ds) *ds = dm->probs[s].ds;
55143ba16761SJacob Faibussowitsch       if (dm->probs[s].label) PetscFunctionReturn(PETSC_SUCCESS);
5515b3cf3223SMatthew G. Knepley     }
5516e5e52638SMatthew G. Knepley   }
55173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5518e5e52638SMatthew G. Knepley }
5519e5e52638SMatthew G. Knepley 
5520e5e52638SMatthew G. Knepley /*@
5521bb7acecfSBarry Smith   DMSetRegionDS - Set the `PetscDS` for a given mesh region, defined by a `DMLabel`
5522083401c6SMatthew G. Knepley 
5523*20f4b53cSBarry Smith   Collective
5524083401c6SMatthew G. Knepley 
5525083401c6SMatthew G. Knepley   Input Parameters:
5526bb7acecfSBarry Smith + dm     - The `DM`
5527*20f4b53cSBarry Smith . label  - The `DMLabel` defining the mesh region, or `NULL` for the entire mesh
5528*20f4b53cSBarry Smith . fields - The IS containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL` for all fields
5529bb7acecfSBarry Smith - prob   - The `PetscDS` defined on the given region
5530083401c6SMatthew G. Knepley 
5531*20f4b53cSBarry Smith   Level: advanced
5532*20f4b53cSBarry Smith 
5533bb7acecfSBarry Smith   Note:
5534bb7acecfSBarry 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,
5535083401c6SMatthew G. Knepley   the fields argument is ignored.
5536083401c6SMatthew G. Knepley 
5537db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionNumDS()`, `DMGetDS()`, `DMGetCellDS()`
5538083401c6SMatthew G. Knepley @*/
5539d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5540d71ae5a4SJacob Faibussowitsch {
5541083401c6SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5542083401c6SMatthew G. Knepley 
5543083401c6SMatthew G. Knepley   PetscFunctionBegin;
5544083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5545083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5546064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 4);
5547083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5548083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
55499566063dSJacob Faibussowitsch       PetscCall(PetscDSDestroy(&dm->probs[s].ds));
5550083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
55513ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
5552083401c6SMatthew G. Knepley     }
5553083401c6SMatthew G. Knepley   }
55549566063dSJacob Faibussowitsch   PetscCall(DMDSEnlarge_Static(dm, Nds + 1));
55559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
55569566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fields));
55579566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)ds));
5558083401c6SMatthew G. Knepley   if (!label) {
5559083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5560083401c6SMatthew G. Knepley     for (s = Nds - 1; s >= 0; --s) dm->probs[s + 1] = dm->probs[s];
5561083401c6SMatthew G. Knepley     Nds = 0;
5562083401c6SMatthew G. Knepley   }
5563083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5564083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5565083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
55663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5567083401c6SMatthew G. Knepley }
5568083401c6SMatthew G. Knepley 
5569083401c6SMatthew G. Knepley /*@
5570*20f4b53cSBarry Smith   DMGetRegionNumDS - Get the `PetscDS` for a given mesh region, defined by the region number
5571e5e52638SMatthew G. Knepley 
5572*20f4b53cSBarry Smith   Not Collective
5573e5e52638SMatthew G. Knepley 
5574e5e52638SMatthew G. Knepley   Input Parameters:
5575*20f4b53cSBarry Smith + dm  - The `DM`
5576e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5577e5e52638SMatthew G. Knepley 
5578e5e52638SMatthew G. Knepley   Output Parameters:
5579*20f4b53cSBarry Smith + label  - The region label, or `NULL`
5580*20f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this `PetscDS`, or `NULL`
5581*20f4b53cSBarry Smith - ds     - The `PetscDS` defined on the given region, or `NULL`
5582e5e52638SMatthew G. Knepley 
5583e5e52638SMatthew G. Knepley   Level: advanced
5584e5e52638SMatthew G. Knepley 
5585db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5586e5e52638SMatthew G. Knepley @*/
5587d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5588d71ae5a4SJacob Faibussowitsch {
5589e5e52638SMatthew G. Knepley   PetscInt Nds;
5590e5e52638SMatthew G. Knepley 
5591e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5592e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55939566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
559463a3b9bcSJacob 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);
5595e5e52638SMatthew G. Knepley   if (label) {
5596e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5597e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5598e5e52638SMatthew G. Knepley   }
5599b3cf3223SMatthew G. Knepley   if (fields) {
5600b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5601b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5602b3cf3223SMatthew G. Knepley   }
5603e5e52638SMatthew G. Knepley   if (ds) {
5604b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5605e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5606e5e52638SMatthew G. Knepley   }
56073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5608e5e52638SMatthew G. Knepley }
5609e5e52638SMatthew G. Knepley 
5610e5e52638SMatthew G. Knepley /*@
5611*20f4b53cSBarry Smith   DMSetRegionNumDS - Set the `PetscDS` for a given mesh region, defined by the region number
5612e5e52638SMatthew G. Knepley 
5613*20f4b53cSBarry Smith   Not Collective
5614e5e52638SMatthew G. Knepley 
5615e5e52638SMatthew G. Knepley   Input Parameters:
5616*20f4b53cSBarry Smith + dm     - The `DM`
5617083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5618*20f4b53cSBarry Smith . label  - The region label, or `NULL`
5619*20f4b53cSBarry Smith . fields - The `IS` containing the `DM` field numbers for the fields in this DS, or `NULL` to prevent setting
5620*20f4b53cSBarry Smith - ds     - The `PetscDS` defined on the given region, or `NULL` to prevent setting
5621e5e52638SMatthew G. Knepley 
5622e5e52638SMatthew G. Knepley   Level: advanced
5623e5e52638SMatthew G. Knepley 
5624db781477SPatrick Sanan .seealso: `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
5625e5e52638SMatthew G. Knepley @*/
5626d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5627d71ae5a4SJacob Faibussowitsch {
5628083401c6SMatthew G. Knepley   PetscInt Nds;
5629e5e52638SMatthew G. Knepley 
5630e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5631e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5632ad540459SPierre Jolivet   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
56339566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
563463a3b9bcSJacob 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);
56359566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
56369566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&dm->probs[num].label));
5637083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5638083401c6SMatthew G. Knepley   if (fields) {
5639083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
56409566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)fields));
56419566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dm->probs[num].fields));
5642083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5643e5e52638SMatthew G. Knepley   }
5644083401c6SMatthew G. Knepley   if (ds) {
5645083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
56469566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)ds));
56479566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dm->probs[num].ds));
5648083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5649083401c6SMatthew G. Knepley   }
56503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5651e5e52638SMatthew G. Knepley }
5652e5e52638SMatthew G. Knepley 
5653e5e52638SMatthew G. Knepley /*@
5654*20f4b53cSBarry Smith   DMFindRegionNum - Find the region number for a given `PetscDS`, or -1 if it is not found.
56551d3af9e0SMatthew G. Knepley 
5656*20f4b53cSBarry Smith   Not Collective
56571d3af9e0SMatthew G. Knepley 
56581d3af9e0SMatthew G. Knepley   Input Parameters:
5659*20f4b53cSBarry Smith + dm  - The `DM`
5660*20f4b53cSBarry Smith - ds  - The `PetscDS` defined on the given region
56611d3af9e0SMatthew G. Knepley 
56621d3af9e0SMatthew G. Knepley   Output Parameter:
56631d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
56641d3af9e0SMatthew G. Knepley 
56651d3af9e0SMatthew G. Knepley   Level: advanced
56661d3af9e0SMatthew G. Knepley 
5667db781477SPatrick Sanan .seealso: `DMGetRegionNumDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`, `DMGetDS()`, `DMGetCellDS()`
56681d3af9e0SMatthew G. Knepley @*/
5669d71ae5a4SJacob Faibussowitsch PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
5670d71ae5a4SJacob Faibussowitsch {
56711d3af9e0SMatthew G. Knepley   PetscInt Nds, n;
56721d3af9e0SMatthew G. Knepley 
56731d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
56741d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56751d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
5676dadcf809SJacob Faibussowitsch   PetscValidIntPointer(num, 3);
56779566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
56789371c9d4SSatish Balay   for (n = 0; n < Nds; ++n)
56799371c9d4SSatish Balay     if (ds == dm->probs[n].ds) break;
56801d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
56811d3af9e0SMatthew G. Knepley   else *num = n;
56823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56831d3af9e0SMatthew G. Knepley }
56841d3af9e0SMatthew G. Knepley 
56852df84da0SMatthew G. Knepley /*@C
5686bb7acecfSBarry Smith   DMCreateFEDefault - Create a `PetscFE` based on the celltype for the mesh
56872df84da0SMatthew G. Knepley 
5688*20f4b53cSBarry Smith   Not Collective
56892df84da0SMatthew G. Knepley 
5690f1a722f8SMatthew G. Knepley   Input Parameters:
5691bb7acecfSBarry Smith + dm     - The `DM`
56922df84da0SMatthew G. Knepley . Nc     - The number of components for the field
5693*20f4b53cSBarry Smith . prefix - The options prefix for the output `PetscFE`, or `NULL`
5694bb7acecfSBarry Smith - qorder - The quadrature order or `PETSC_DETERMINE` to use `PetscSpace` polynomial degree
56952df84da0SMatthew G. Knepley 
56962df84da0SMatthew G. Knepley   Output Parameter:
5697bb7acecfSBarry Smith . fem - The `PetscFE`
56982df84da0SMatthew G. Knepley 
5699*20f4b53cSBarry Smith   Level: intermediate
5700*20f4b53cSBarry Smith 
5701bb7acecfSBarry Smith   Note:
5702bb7acecfSBarry Smith   This is a convenience method that just calls `PetscFECreateByCell()` underneath.
57032df84da0SMatthew G. Knepley 
5704db781477SPatrick Sanan .seealso: `PetscFECreateByCell()`, `DMAddField()`, `DMCreateDS()`, `DMGetCellDS()`, `DMGetRegionDS()`
57052df84da0SMatthew G. Knepley @*/
5706d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateFEDefault(DM dm, PetscInt Nc, const char prefix[], PetscInt qorder, PetscFE *fem)
5707d71ae5a4SJacob Faibussowitsch {
57082df84da0SMatthew G. Knepley   DMPolytopeType ct;
57092df84da0SMatthew G. Knepley   PetscInt       dim, cStart;
57102df84da0SMatthew G. Knepley 
57112df84da0SMatthew G. Knepley   PetscFunctionBegin;
57122df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57132df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 2);
57142df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 3);
57152df84da0SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, qorder, 4);
57162df84da0SMatthew G. Knepley   PetscValidPointer(fem, 5);
57179566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
57189566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
57199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
57209566063dSJacob Faibussowitsch   PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, Nc, ct, prefix, qorder, fem));
57213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57222df84da0SMatthew G. Knepley }
57232df84da0SMatthew G. Knepley 
57241d3af9e0SMatthew G. Knepley /*@
5725bb7acecfSBarry Smith   DMCreateDS - Create the discrete systems for the `DM` based upon the fields added to the `DM`
5726e5e52638SMatthew G. Knepley 
5727*20f4b53cSBarry Smith   Collective
5728e5e52638SMatthew G. Knepley 
5729e5e52638SMatthew G. Knepley   Input Parameter:
5730bb7acecfSBarry Smith . dm - The `DM`
5731e5e52638SMatthew G. Knepley 
5732*20f4b53cSBarry Smith   Options Database Key:
5733bb7acecfSBarry Smith . -dm_petscds_view - View all the `PetscDS` objects in this `DM`
573445480ffeSMatthew G. Knepley 
5735*20f4b53cSBarry Smith   Level: intermediate
5736*20f4b53cSBarry Smith 
5737bb7acecfSBarry Smith   Note:
5738bb7acecfSBarry Smith   If the label has a `PetscDS` defined, it will be replaced. Otherwise, it will be added to the `DM`.
5739e5e52638SMatthew G. Knepley 
5740db781477SPatrick Sanan .seealso: `DMSetField`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
5741e5e52638SMatthew G. Knepley @*/
5742d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateDS(DM dm)
5743d71ae5a4SJacob Faibussowitsch {
5744e5e52638SMatthew G. Knepley   MPI_Comm  comm;
5745083401c6SMatthew G. Knepley   PetscDS   dsDef;
5746083401c6SMatthew G. Knepley   DMLabel  *labelSet;
5747f9244615SMatthew G. Knepley   PetscInt  dE, Nf = dm->Nf, f, s, Nl, l, Ndef, k;
5748f9244615SMatthew G. Knepley   PetscBool doSetup = PETSC_TRUE, flg;
5749e5e52638SMatthew G. Knepley 
5750e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5751e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57523ba16761SJacob Faibussowitsch   if (!dm->fields) PetscFunctionReturn(PETSC_SUCCESS);
57539566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
57549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
5755083401c6SMatthew G. Knepley   /* Determine how many regions we have */
57569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf, &labelSet));
5757083401c6SMatthew G. Knepley   Nl   = 0;
5758083401c6SMatthew G. Knepley   Ndef = 0;
5759083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5760083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5761083401c6SMatthew G. Knepley     PetscInt l;
5762083401c6SMatthew G. Knepley 
5763f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
5764f918ec44SMatthew G. Knepley     /* Move CEED context to discretizations */
5765f918ec44SMatthew G. Knepley     {
5766f918ec44SMatthew G. Knepley       PetscClassId id;
5767f918ec44SMatthew G. Knepley 
57689566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(dm->fields[f].disc, &id));
5769f918ec44SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
5770f918ec44SMatthew G. Knepley         Ceed ceed;
5771f918ec44SMatthew G. Knepley 
57729566063dSJacob Faibussowitsch         PetscCall(DMGetCeed(dm, &ceed));
57739566063dSJacob Faibussowitsch         PetscCall(PetscFESetCeed((PetscFE)dm->fields[f].disc, ceed));
5774f918ec44SMatthew G. Knepley       }
5775f918ec44SMatthew G. Knepley     }
5776f918ec44SMatthew G. Knepley #endif
57779371c9d4SSatish Balay     if (!label) {
57789371c9d4SSatish Balay       ++Ndef;
57799371c9d4SSatish Balay       continue;
57809371c9d4SSatish Balay     }
57819371c9d4SSatish Balay     for (l = 0; l < Nl; ++l)
57829371c9d4SSatish Balay       if (label == labelSet[l]) break;
5783083401c6SMatthew G. Knepley     if (l < Nl) continue;
5784083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5785083401c6SMatthew G. Knepley   }
5786083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
57879566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
5788083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5789b3cf3223SMatthew G. Knepley     IS        fields;
5790b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5791b3cf3223SMatthew G. Knepley 
57929371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57939371c9d4SSatish Balay       if (!dm->fields[f].label) ++nf;
57947a8be351SBarry Smith     PetscCheck(nf, comm, PETSC_ERR_PLIB, "All fields have labels, but we are trying to create a default DS");
57959566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
57969371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
57979371c9d4SSatish Balay       if (!dm->fields[f].label) fld[nf++] = f;
57989566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
57999566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58009566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58019566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
580288f0c812SMatthew G. Knepley 
58039566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58049566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, NULL, fields, dsDef));
58059566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58069566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58072df9ee95SMatthew G. Knepley   }
58089566063dSJacob Faibussowitsch   PetscCall(DMGetRegionDS(dm, NULL, NULL, &dsDef));
58099566063dSJacob Faibussowitsch   if (dsDef) PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
5810083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5811083401c6SMatthew G. Knepley   if (Ndef && Nl) {
58120122748bSMatthew G. Knepley     DM              plex;
5813083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5814083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5815083401c6SMatthew G. Knepley     PetscInt       *fields;
5816083401c6SMatthew G. Knepley     const PetscInt *cells;
5817083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
58180122748bSMatthew G. Knepley 
58199566063dSJacob Faibussowitsch     PetscCall(DMConvert(dm, DMPLEX, &plex));
58209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(plex, &depth));
58219566063dSJacob Faibussowitsch     PetscCall(DMGetStratumIS(plex, "dim", depth, &allcellIS));
58229566063dSJacob Faibussowitsch     if (!allcellIS) PetscCall(DMGetStratumIS(plex, "depth", depth, &allcellIS));
58235fedec97SMatthew G. Knepley     /* TODO This looks like it only works for one label */
5824083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5825083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5826083401c6SMatthew G. Knepley       IS      pointIS;
5827083401c6SMatthew G. Knepley 
58289566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&defcellIS));
58299566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, 1, &pointIS));
58309566063dSJacob Faibussowitsch       PetscCall(ISDifference(allcellIS, pointIS, &defcellIS));
58319566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
5832083401c6SMatthew G. Knepley     }
58339566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&allcellIS));
5834083401c6SMatthew G. Knepley 
58359566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel));
58369566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(defcellIS, &n));
58379566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(defcellIS, &cells));
58389566063dSJacob Faibussowitsch     for (c = 0; c < n; ++c) PetscCall(DMLabelSetValue(cellLabel, cells[c], 1));
58399566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(defcellIS, &cells));
58409566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&defcellIS));
58419566063dSJacob Faibussowitsch     PetscCall(DMPlexLabelComplete(plex, cellLabel));
5842083401c6SMatthew G. Knepley 
58439566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Ndef, &fields));
58449371c9d4SSatish Balay     for (f = 0; f < Nf; ++f)
58459371c9d4SSatish Balay       if (!dm->fields[f].label) fields[nf++] = f;
58469566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fieldIS));
58479566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fieldIS, "dm_fields_"));
58489566063dSJacob Faibussowitsch     PetscCall(ISSetType(fieldIS, ISGENERAL));
58499566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER));
5850083401c6SMatthew G. Knepley 
58519566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &dsDef));
58529566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, cellLabel, fieldIS, dsDef));
58539566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsDef, dE));
58549566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&cellLabel));
58559566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&dsDef));
58569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fieldIS));
58579566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&plex));
5858083401c6SMatthew G. Knepley   }
5859083401c6SMatthew G. Knepley   /* Create label DSes
5860083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5861083401c6SMatthew G. Knepley   */
5862083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5863083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5864083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5865083401c6SMatthew G. Knepley     PetscDS   ds;
5866083401c6SMatthew G. Knepley     IS        fields;
5867083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5868083401c6SMatthew G. Knepley 
58699566063dSJacob Faibussowitsch     PetscCall(PetscDSCreate(PETSC_COMM_SELF, &ds));
58709371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58719371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
58729566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(nf, &fld));
58739371c9d4SSatish Balay     for (f = 0, nf = 0; f < Nf; ++f)
58749371c9d4SSatish Balay       if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
58759566063dSJacob Faibussowitsch     PetscCall(ISCreate(PETSC_COMM_SELF, &fields));
58769566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fields, "dm_fields_"));
58779566063dSJacob Faibussowitsch     PetscCall(ISSetType(fields, ISGENERAL));
58789566063dSJacob Faibussowitsch     PetscCall(ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER));
58799566063dSJacob Faibussowitsch     PetscCall(DMSetRegionDS(dm, label, fields, ds));
58809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&fields));
58819566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(ds, dE));
5882083401c6SMatthew G. Knepley     {
5883083401c6SMatthew G. Knepley       DMPolytopeType ct;
5884083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
58855fedec97SMatthew G. Knepley       PetscBool      isCohesiveLocal = PETSC_FALSE, isCohesive;
58860122748bSMatthew G. Knepley 
58879566063dSJacob Faibussowitsch       PetscCall(DMLabelGetBounds(label, &lStart, &lEnd));
5888665f567fSMatthew G. Knepley       if (lStart >= 0) {
58899566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCellType(dm, lStart, &ct));
5890412e9a14SMatthew G. Knepley         switch (ct) {
5891412e9a14SMatthew G. Knepley         case DM_POLYTOPE_POINT_PRISM_TENSOR:
5892412e9a14SMatthew G. Knepley         case DM_POLYTOPE_SEG_PRISM_TENSOR:
5893412e9a14SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
5894d71ae5a4SJacob Faibussowitsch         case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5895d71ae5a4SJacob Faibussowitsch           isCohesiveLocal = PETSC_TRUE;
5896d71ae5a4SJacob Faibussowitsch           break;
5897d71ae5a4SJacob Faibussowitsch         default:
5898d71ae5a4SJacob Faibussowitsch           break;
5899412e9a14SMatthew G. Knepley         }
5900665f567fSMatthew G. Knepley       }
59019566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Allreduce(&isCohesiveLocal, &isCohesive, 1, MPIU_BOOL, MPI_LOR, comm));
59025fedec97SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) {
59035fedec97SMatthew G. Knepley         if (label == dm->fields[f].label || !dm->fields[f].label) {
59045fedec97SMatthew G. Knepley           if (label == dm->fields[f].label) {
59059566063dSJacob Faibussowitsch             PetscCall(PetscDSSetDiscretization(ds, nf, NULL));
59069566063dSJacob Faibussowitsch             PetscCall(PetscDSSetCohesive(ds, nf, isCohesive));
59075fedec97SMatthew G. Knepley           }
59085fedec97SMatthew G. Knepley           ++nf;
59095fedec97SMatthew G. Knepley         }
59105fedec97SMatthew G. Knepley       }
5911e5e52638SMatthew G. Knepley     }
59129566063dSJacob Faibussowitsch     PetscCall(PetscDSDestroy(&ds));
5913e5e52638SMatthew G. Knepley   }
59149566063dSJacob Faibussowitsch   PetscCall(PetscFree(labelSet));
5915e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5916083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5917083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5918083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5919083401c6SMatthew G. Knepley     const PetscInt *fld;
59205fedec97SMatthew G. Knepley     PetscInt        nf, dsnf;
59215fedec97SMatthew G. Knepley     PetscBool       isCohesive;
5922e5e52638SMatthew G. Knepley 
59239566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsnf));
59249566063dSJacob Faibussowitsch     PetscCall(PetscDSIsCohesive(ds, &isCohesive));
59259566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(fields, &nf));
59269566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fields, &fld));
5927083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5928083401c6SMatthew G. Knepley       PetscObject  disc = dm->fields[fld[f]].disc;
59295fedec97SMatthew G. Knepley       PetscBool    isCohesiveField;
5930e5e52638SMatthew G. Knepley       PetscClassId id;
5931e5e52638SMatthew G. Knepley 
59325fedec97SMatthew G. Knepley       /* Handle DS with no fields */
59339566063dSJacob Faibussowitsch       if (dsnf) PetscCall(PetscDSGetCohesive(ds, f, &isCohesiveField));
59345fedec97SMatthew G. Knepley       /* If this is a cohesive cell, then regular fields need the lower dimensional discretization */
59359566063dSJacob Faibussowitsch       if (isCohesive && !isCohesiveField) PetscCall(PetscFEGetHeightSubspace((PetscFE)disc, 1, (PetscFE *)&disc));
59369566063dSJacob Faibussowitsch       PetscCall(PetscDSSetDiscretization(ds, f, disc));
5937083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
59389566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(disc, &id));
5939e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5940e5e52638SMatthew G. Knepley     }
59419566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fields, &fld));
5942e5e52638SMatthew G. Knepley   }
5943f9244615SMatthew G. Knepley   /* Allow k-jet tabulation */
59449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)dm)->prefix, "-dm_ds_jet_degree", &k, &flg));
5945f9244615SMatthew G. Knepley   if (flg) {
59463b4aee56SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {
59473b4aee56SMatthew G. Knepley       PetscDS  ds = dm->probs[s].ds;
59483b4aee56SMatthew G. Knepley       PetscInt Nf, f;
59493b4aee56SMatthew G. Knepley 
59509566063dSJacob Faibussowitsch       PetscCall(PetscDSGetNumFields(ds, &Nf));
59519566063dSJacob Faibussowitsch       for (f = 0; f < Nf; ++f) PetscCall(PetscDSSetJetDegree(ds, f, k));
59523b4aee56SMatthew G. Knepley     }
5953f9244615SMatthew G. Knepley   }
5954e5e52638SMatthew G. Knepley   /* Setup DSes */
5955e5e52638SMatthew G. Knepley   if (doSetup) {
59569566063dSJacob Faibussowitsch     for (s = 0; s < dm->Nds; ++s) PetscCall(PetscDSSetUp(dm->probs[s].ds));
5957e5e52638SMatthew G. Knepley   }
59583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5959e5e52638SMatthew G. Knepley }
5960e5e52638SMatthew G. Knepley 
5961e5e52638SMatthew G. Knepley /*@
5962bb7acecfSBarry Smith   DMComputeExactSolution - Compute the exact solution for a given `DM`, using the `PetscDS` information.
59637f96f943SMatthew G. Knepley 
5964*20f4b53cSBarry Smith   Collective
5965f2cacb80SMatthew G. Knepley 
59667f96f943SMatthew G. Knepley   Input Parameters:
5967bb7acecfSBarry Smith + dm   - The `DM`
59687f96f943SMatthew G. Knepley - time - The time
59697f96f943SMatthew G. Knepley 
59707f96f943SMatthew G. Knepley   Output Parameters:
5971*20f4b53cSBarry Smith + u    - The vector will be filled with exact solution values, or `NULL`
5972*20f4b53cSBarry Smith - u_t  - The vector will be filled with the time derivative of exact solution values, or `NULL`
5973*20f4b53cSBarry Smith 
5974*20f4b53cSBarry Smith   Level: developer
59757f96f943SMatthew G. Knepley 
5976bb7acecfSBarry Smith   Note:
5977bb7acecfSBarry Smith   The user must call `PetscDSSetExactSolution()` before using this routine
59787f96f943SMatthew G. Knepley 
5979db781477SPatrick Sanan .seealso: `PetscDSSetExactSolution()`
59807f96f943SMatthew G. Knepley @*/
5981d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
5982d71ae5a4SJacob Faibussowitsch {
59837f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
59847f96f943SMatthew G. Knepley   void   **ectxs;
5985f60fa741SMatthew G. Knepley   Vec      locu, locu_t;
59867f96f943SMatthew G. Knepley   PetscInt Nf, Nds, s;
59877f96f943SMatthew G. Knepley 
59887f96f943SMatthew G. Knepley   PetscFunctionBegin;
5989f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5990f60fa741SMatthew G. Knepley   if (u) {
5991f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5992f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu));
5993f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu, 0.));
5994f60fa741SMatthew G. Knepley   }
5995f60fa741SMatthew G. Knepley   if (u_t) {
5996f60fa741SMatthew G. Knepley     PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
5997f60fa741SMatthew G. Knepley     PetscCall(DMGetLocalVector(dm, &locu_t));
5998f60fa741SMatthew G. Knepley     PetscCall(VecSet(locu_t, 0.));
5999f60fa741SMatthew G. Knepley   }
60009566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
60019566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(Nf, &exacts, Nf, &ectxs));
60029566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
60037f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
60047f96f943SMatthew G. Knepley     PetscDS         ds;
60057f96f943SMatthew G. Knepley     DMLabel         label;
60067f96f943SMatthew G. Knepley     IS              fieldIS;
60077f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
60087f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
60097f96f943SMatthew G. Knepley 
60109566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
60119566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
60129566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(fieldIS, &fields));
60139566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(exacts, Nf));
60149566063dSJacob Faibussowitsch     PetscCall(PetscArrayzero(ectxs, Nf));
6015f2cacb80SMatthew G. Knepley     if (u) {
6016f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolution(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6017f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu));
6018f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu));
60197f96f943SMatthew G. Knepley     }
6020f2cacb80SMatthew G. Knepley     if (u_t) {
60219566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(exacts, Nf));
60229566063dSJacob Faibussowitsch       PetscCall(PetscArrayzero(ectxs, Nf));
6023f60fa741SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) PetscCall(PetscDSGetExactSolutionTimeDerivative(ds, fields[f], &exacts[fields[f]], &ectxs[fields[f]]));
6024f60fa741SMatthew G. Knepley       if (label) PetscCall(DMProjectFunctionLabelLocal(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6025f60fa741SMatthew G. Knepley       else PetscCall(DMProjectFunctionLocal(dm, time, exacts, ectxs, INSERT_ALL_VALUES, locu_t));
6026f2cacb80SMatthew G. Knepley     }
60279566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(fieldIS, &fields));
6028f2cacb80SMatthew G. Knepley   }
6029f2cacb80SMatthew G. Knepley   if (u) {
60309566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution"));
60319566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u, "exact_"));
6032f2cacb80SMatthew G. Knepley   }
6033f2cacb80SMatthew G. Knepley   if (u_t) {
60349566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)u, "Exact Solution Time Derivative"));
60359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)u_t, "exact_t_"));
6036f2cacb80SMatthew G. Knepley   }
60379566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exacts, ectxs));
6038f60fa741SMatthew G. Knepley   if (u) {
6039f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu, INSERT_ALL_VALUES, u));
6040f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu, INSERT_ALL_VALUES, u));
6041f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu));
6042f60fa741SMatthew G. Knepley   }
6043f60fa741SMatthew G. Knepley   if (u_t) {
6044f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalBegin(dm, locu_t, INSERT_ALL_VALUES, u_t));
6045f60fa741SMatthew G. Knepley     PetscCall(DMLocalToGlobalEnd(dm, locu_t, INSERT_ALL_VALUES, u_t));
6046f60fa741SMatthew G. Knepley     PetscCall(DMRestoreLocalVector(dm, &locu_t));
6047f60fa741SMatthew G. Knepley   }
60483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60497f96f943SMatthew G. Knepley }
60507f96f943SMatthew G. Knepley 
6051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMTransferDS_Internal(DM dm, DMLabel label, IS fields, PetscDS ds)
6052d71ae5a4SJacob Faibussowitsch {
605345480ffeSMatthew G. Knepley   PetscDS    dsNew;
605445480ffeSMatthew G. Knepley   DSBoundary b;
60556a02485aSMatthew G. Knepley   PetscInt   cdim, Nf, f, d;
60565fedec97SMatthew G. Knepley   PetscBool  isCohesive;
605745480ffeSMatthew G. Knepley   void      *ctx;
605845480ffeSMatthew G. Knepley 
605945480ffeSMatthew G. Knepley   PetscFunctionBegin;
60609566063dSJacob Faibussowitsch   PetscCall(PetscDSCreate(PetscObjectComm((PetscObject)ds), &dsNew));
60619566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyConstants(ds, dsNew));
60629566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyExactSolutions(ds, dsNew));
60639566063dSJacob Faibussowitsch   PetscCall(PetscDSSelectDiscretizations(ds, PETSC_DETERMINE, NULL, dsNew));
60649566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyEquations(ds, dsNew));
60659566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
606645480ffeSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
60679566063dSJacob Faibussowitsch     PetscCall(PetscDSGetContext(ds, f, &ctx));
60689566063dSJacob Faibussowitsch     PetscCall(PetscDSSetContext(dsNew, f, ctx));
60699566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
60709566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCohesive(dsNew, f, isCohesive));
60716a02485aSMatthew G. Knepley     PetscCall(PetscDSGetJetDegree(ds, f, &d));
60726a02485aSMatthew G. Knepley     PetscCall(PetscDSSetJetDegree(dsNew, f, d));
607345480ffeSMatthew G. Knepley   }
607445480ffeSMatthew G. Knepley   if (Nf) {
60759566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCoordinateDimension(ds, &cdim));
60769566063dSJacob Faibussowitsch     PetscCall(PetscDSSetCoordinateDimension(dsNew, cdim));
607745480ffeSMatthew G. Knepley   }
60789566063dSJacob Faibussowitsch   PetscCall(PetscDSCopyBoundary(ds, PETSC_DETERMINE, NULL, dsNew));
607945480ffeSMatthew G. Knepley   for (b = dsNew->boundary; b; b = b->next) {
60809566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, b->lname, &b->label));
608145480ffeSMatthew G. Knepley     /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
60827a8be351SBarry Smith     //PetscCheck(b->label,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Label %s missing in new DM", name);
608345480ffeSMatthew G. Knepley   }
608445480ffeSMatthew G. Knepley 
60859566063dSJacob Faibussowitsch   PetscCall(DMSetRegionDS(dm, label, fields, dsNew));
60869566063dSJacob Faibussowitsch   PetscCall(PetscDSDestroy(&dsNew));
60873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
608845480ffeSMatthew G. Knepley }
608945480ffeSMatthew G. Knepley 
60907f96f943SMatthew G. Knepley /*@
6091bb7acecfSBarry Smith   DMCopyDS - Copy the discrete systems for the `DM` into another `DM`
6092e5e52638SMatthew G. Knepley 
6093*20f4b53cSBarry Smith   Collective
6094e5e52638SMatthew G. Knepley 
6095e5e52638SMatthew G. Knepley   Input Parameter:
6096bb7acecfSBarry Smith . dm - The `DM`
6097e5e52638SMatthew G. Knepley 
6098e5e52638SMatthew G. Knepley   Output Parameter:
6099bb7acecfSBarry Smith . newdm - The `DM`
6100e5e52638SMatthew G. Knepley 
6101e5e52638SMatthew G. Knepley   Level: advanced
6102e5e52638SMatthew G. Knepley 
6103db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMAddField()`, `DMGetDS()`, `DMGetCellDS()`, `DMGetRegionDS()`, `DMSetRegionDS()`
6104e5e52638SMatthew G. Knepley @*/
6105d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDS(DM dm, DM newdm)
6106d71ae5a4SJacob Faibussowitsch {
6107e5e52638SMatthew G. Knepley   PetscInt Nds, s;
6108e5e52638SMatthew G. Knepley 
6109e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61103ba16761SJacob Faibussowitsch   if (dm == newdm) PetscFunctionReturn(PETSC_SUCCESS);
61119566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
61129566063dSJacob Faibussowitsch   PetscCall(DMClearDS(newdm));
6113e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
6114e5e52638SMatthew G. Knepley     DMLabel  label;
6115b3cf3223SMatthew G. Knepley     IS       fields;
611645480ffeSMatthew G. Knepley     PetscDS  ds, newds;
6117783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
6118e5e52638SMatthew G. Knepley 
61199566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fields, &ds));
6120b8025e53SMatthew G. Knepley     /* TODO: We need to change all keys from labels in the old DM to labels in the new DM */
61219566063dSJacob Faibussowitsch     PetscCall(DMTransferDS_Internal(newdm, label, fields, ds));
6122d5b43468SJose E. Roman     /* Complete new labels in the new DS */
61239566063dSJacob Faibussowitsch     PetscCall(DMGetRegionDS(newdm, label, NULL, &newds));
61249566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumBoundary(newds, &Nbd));
6125783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
6126b8025e53SMatthew G. Knepley       PetscWeakForm wf;
612745480ffeSMatthew G. Knepley       DMLabel       label;
6128783e2ec8SMatthew G. Knepley       PetscInt      field;
6129783e2ec8SMatthew G. Knepley 
61309566063dSJacob Faibussowitsch       PetscCall(PetscDSGetBoundary(newds, bd, &wf, NULL, NULL, &label, NULL, NULL, &field, NULL, NULL, NULL, NULL, NULL));
61319566063dSJacob Faibussowitsch       PetscCall(PetscWeakFormReplaceLabel(wf, label));
6132783e2ec8SMatthew G. Knepley     }
6133e5e52638SMatthew G. Knepley   }
6134799db056SMatthew G. Knepley   PetscCall(DMCompleteBCLabels_Internal(newdm));
61353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6136e5e52638SMatthew G. Knepley }
6137e5e52638SMatthew G. Knepley 
6138e5e52638SMatthew G. Knepley /*@
6139bb7acecfSBarry Smith   DMCopyDisc - Copy the fields and discrete systems for the `DM` into another `DM`
6140e5e52638SMatthew G. Knepley 
6141*20f4b53cSBarry Smith   Collective
6142e5e52638SMatthew G. Knepley 
6143e5e52638SMatthew G. Knepley   Input Parameter:
6144bb7acecfSBarry Smith . dm - The `DM`
6145e5e52638SMatthew G. Knepley 
6146e5e52638SMatthew G. Knepley   Output Parameter:
6147bb7acecfSBarry Smith . newdm - The `DM`
6148e5e52638SMatthew G. Knepley 
6149e5e52638SMatthew G. Knepley   Level: advanced
6150e5e52638SMatthew G. Knepley 
6151bb7acecfSBarry Smith   Developer Note:
6152bb7acecfSBarry Smith   Really ugly name, nothing in PETSc is called a `Disc` plus it is an ugly abbreviation
6153bb7acecfSBarry Smith 
6154db781477SPatrick Sanan .seealso: `DMCopyFields()`, `DMCopyDS()`
6155e5e52638SMatthew G. Knepley @*/
6156d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyDisc(DM dm, DM newdm)
6157d71ae5a4SJacob Faibussowitsch {
6158e5e52638SMatthew G. Knepley   PetscFunctionBegin;
61599566063dSJacob Faibussowitsch   PetscCall(DMCopyFields(dm, newdm));
61609566063dSJacob Faibussowitsch   PetscCall(DMCopyDS(dm, newdm));
61613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6162e5e52638SMatthew G. Knepley }
6163e5e52638SMatthew G. Knepley 
6164c73cfb54SMatthew G. Knepley /*@
6165bb7acecfSBarry Smith   DMGetDimension - Return the topological dimension of the `DM`
6166c73cfb54SMatthew G. Knepley 
6167*20f4b53cSBarry Smith   Not Collective
6168c73cfb54SMatthew G. Knepley 
6169c73cfb54SMatthew G. Knepley   Input Parameter:
6170bb7acecfSBarry Smith . dm - The `DM`
6171c73cfb54SMatthew G. Knepley 
6172c73cfb54SMatthew G. Knepley   Output Parameter:
6173c73cfb54SMatthew G. Knepley . dim - The topological dimension
6174c73cfb54SMatthew G. Knepley 
6175c73cfb54SMatthew G. Knepley   Level: beginner
6176c73cfb54SMatthew G. Knepley 
6177db781477SPatrick Sanan .seealso: `DMSetDimension()`, `DMCreate()`
6178c73cfb54SMatthew G. Knepley @*/
6179d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
6180d71ae5a4SJacob Faibussowitsch {
6181c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6182c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6183534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
6184c73cfb54SMatthew G. Knepley   *dim = dm->dim;
61853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6186c73cfb54SMatthew G. Knepley }
6187c73cfb54SMatthew G. Knepley 
6188c73cfb54SMatthew G. Knepley /*@
6189bb7acecfSBarry Smith   DMSetDimension - Set the topological dimension of the `DM`
6190c73cfb54SMatthew G. Knepley 
6191*20f4b53cSBarry Smith   Collective
6192c73cfb54SMatthew G. Knepley 
6193c73cfb54SMatthew G. Knepley   Input Parameters:
6194bb7acecfSBarry Smith + dm - The `DM`
6195c73cfb54SMatthew G. Knepley - dim - The topological dimension
6196c73cfb54SMatthew G. Knepley 
6197c73cfb54SMatthew G. Knepley   Level: beginner
6198c73cfb54SMatthew G. Knepley 
6199db781477SPatrick Sanan .seealso: `DMGetDimension()`, `DMCreate()`
6200c73cfb54SMatthew G. Knepley @*/
6201d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
6202d71ae5a4SJacob Faibussowitsch {
6203e5e52638SMatthew G. Knepley   PetscDS  ds;
620445480ffeSMatthew G. Knepley   PetscInt Nds, n;
6205f17e8794SMatthew G. Knepley 
6206c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
6207c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6208c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6209c73cfb54SMatthew G. Knepley   dm->dim = dim;
6210d17bd122SMatthew G. Knepley   if (dm->dim >= 0) {
62119566063dSJacob Faibussowitsch     PetscCall(DMGetNumDS(dm, &Nds));
621245480ffeSMatthew G. Knepley     for (n = 0; n < Nds; ++n) {
62139566063dSJacob Faibussowitsch       PetscCall(DMGetRegionNumDS(dm, n, NULL, NULL, &ds));
62149566063dSJacob Faibussowitsch       if (ds->dimEmbed < 0) PetscCall(PetscDSSetCoordinateDimension(ds, dim));
621545480ffeSMatthew G. Knepley     }
6216d17bd122SMatthew G. Knepley   }
62173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6218c73cfb54SMatthew G. Knepley }
6219c73cfb54SMatthew G. Knepley 
6220793f3fe5SMatthew G. Knepley /*@
6221793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
6222793f3fe5SMatthew G. Knepley 
6223*20f4b53cSBarry Smith   Collective
6224793f3fe5SMatthew G. Knepley 
6225793f3fe5SMatthew G. Knepley   Input Parameters:
6226bb7acecfSBarry Smith + dm - the `DM`
6227793f3fe5SMatthew G. Knepley - dim - the dimension
6228793f3fe5SMatthew G. Knepley 
6229793f3fe5SMatthew G. Knepley   Output Parameters:
6230793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
6231aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
6232793f3fe5SMatthew G. Knepley 
6233*20f4b53cSBarry Smith   Level: intermediate
6234*20f4b53cSBarry Smith 
6235793f3fe5SMatthew G. Knepley   Note:
6236793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
6237a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
6238793f3fe5SMatthew G. Knepley   then the interval is empty.
6239793f3fe5SMatthew G. Knepley 
6240db781477SPatrick Sanan .seealso: `DMPLEX`, `DMPlexGetDepthStratum()`, `DMPlexGetHeightStratum()`
6241793f3fe5SMatthew G. Knepley @*/
6242d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
6243d71ae5a4SJacob Faibussowitsch {
6244793f3fe5SMatthew G. Knepley   PetscInt d;
6245793f3fe5SMatthew G. Knepley 
6246793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
6247793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &d));
62497a8be351SBarry Smith   PetscCheck((dim >= 0) && (dim <= d), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %" PetscInt_FMT, dim);
6250dbbe0bcdSBarry Smith   PetscUseTypeMethod(dm, getdimpoints, dim, pStart, pEnd);
62513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6252793f3fe5SMatthew G. Knepley }
6253793f3fe5SMatthew G. Knepley 
62546636e97aSMatthew G Knepley /*@
6255bb7acecfSBarry Smith   DMGetOutputDM - Retrieve the `DM` associated with the layout for output
6256f4d763aaSMatthew G. Knepley 
6257*20f4b53cSBarry Smith   Collective
62588f700142SStefano Zampini 
6259f4d763aaSMatthew G. Knepley   Input Parameter:
6260bb7acecfSBarry Smith . dm - The original `DM`
6261f4d763aaSMatthew G. Knepley 
6262f4d763aaSMatthew G. Knepley   Output Parameter:
6263bb7acecfSBarry Smith . odm - The `DM` which provides the layout for output
6264f4d763aaSMatthew G. Knepley 
6265f4d763aaSMatthew G. Knepley   Level: intermediate
6266f4d763aaSMatthew G. Knepley 
6267bb7acecfSBarry Smith   Note:
6268bb7acecfSBarry Smith   In some situations the vector obtained with `DMCreateGlobalVector()` excludes points for degrees of freedom that are associated with fixed (Dirichelet) boundary
6269bb7acecfSBarry 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
6270bb7acecfSBarry Smith   locations for those dof so that they can be output to a file or other viewer along with the unconstrained dof.
6271bb7acecfSBarry Smith 
6272bb7acecfSBarry Smith .seealso: `VecView()`, `DMGetGlobalSection()`, `DMCreateGlobalVector()`, `PetscSectionHasConstraints()`, `DMSetGlobalSection()`
6273f4d763aaSMatthew G. Knepley @*/
6274d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
6275d71ae5a4SJacob Faibussowitsch {
6276c26acbdeSMatthew G. Knepley   PetscSection section;
62772d4e4a49SMatthew G. Knepley   PetscBool    hasConstraints, ghasConstraints;
627814f150ffSMatthew G. Knepley 
627914f150ffSMatthew G. Knepley   PetscFunctionBegin;
628014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
628114f150ffSMatthew G. Knepley   PetscValidPointer(odm, 2);
62829566063dSJacob Faibussowitsch   PetscCall(DMGetLocalSection(dm, &section));
62839566063dSJacob Faibussowitsch   PetscCall(PetscSectionHasConstraints(section, &hasConstraints));
62849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm)));
62852d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6286c26acbdeSMatthew G. Knepley     *odm = dm;
62873ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
6288c26acbdeSMatthew G. Knepley   }
628914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6290c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
629114f150ffSMatthew G. Knepley     PetscSF      sf;
629214f150ffSMatthew G. Knepley 
62939566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &dm->dmBC));
62949566063dSJacob Faibussowitsch     PetscCall(DMCopyDisc(dm, dm->dmBC));
62959566063dSJacob Faibussowitsch     PetscCall(PetscSectionClone(section, &newSection));
62969566063dSJacob Faibussowitsch     PetscCall(DMSetLocalSection(dm->dmBC, newSection));
62979566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&newSection));
62989566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm->dmBC, &sf));
62999566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection));
63009566063dSJacob Faibussowitsch     PetscCall(DMSetGlobalSection(dm->dmBC, gsection));
63019566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&gsection));
630214f150ffSMatthew G. Knepley   }
630314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
63043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
630514f150ffSMatthew G. Knepley }
6306f4d763aaSMatthew G. Knepley 
6307f4d763aaSMatthew G. Knepley /*@
6308cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6309f4d763aaSMatthew G. Knepley 
6310f4d763aaSMatthew G. Knepley   Input Parameter:
6311bb7acecfSBarry Smith . dm - The original `DM`
6312f4d763aaSMatthew G. Knepley 
6313cdb7a50dSMatthew G. Knepley   Output Parameters:
6314cdb7a50dSMatthew G. Knepley + num - The output sequence number
6315cdb7a50dSMatthew G. Knepley - val - The output sequence value
6316f4d763aaSMatthew G. Knepley 
6317f4d763aaSMatthew G. Knepley   Level: intermediate
6318f4d763aaSMatthew G. Knepley 
6319bb7acecfSBarry Smith   Note:
6320bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6321bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6322bb7acecfSBarry Smith 
6323bb7acecfSBarry Smith   Developer Note:
6324bb7acecfSBarry Smith   The `DM` serves as a convenient place to store the current iteration value. The iteration is not
6325bb7acecfSBarry Smith   not directly related to the `DM`.
6326f4d763aaSMatthew G. Knepley 
6327db781477SPatrick Sanan .seealso: `VecView()`
6328f4d763aaSMatthew G. Knepley @*/
6329d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6330d71ae5a4SJacob Faibussowitsch {
6331f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6332f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63339371c9d4SSatish Balay   if (num) {
63349371c9d4SSatish Balay     PetscValidIntPointer(num, 2);
63359371c9d4SSatish Balay     *num = dm->outputSequenceNum;
63369371c9d4SSatish Balay   }
63379371c9d4SSatish Balay   if (val) {
63389371c9d4SSatish Balay     PetscValidRealPointer(val, 3);
63399371c9d4SSatish Balay     *val = dm->outputSequenceVal;
63409371c9d4SSatish Balay   }
63413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6342f4d763aaSMatthew G. Knepley }
6343f4d763aaSMatthew G. Knepley 
6344f4d763aaSMatthew G. Knepley /*@
6345cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6346f4d763aaSMatthew G. Knepley 
6347f4d763aaSMatthew G. Knepley   Input Parameters:
6348bb7acecfSBarry Smith + dm - The original `DM`
6349cdb7a50dSMatthew G. Knepley . num - The output sequence number
6350cdb7a50dSMatthew G. Knepley - val - The output sequence value
6351f4d763aaSMatthew G. Knepley 
6352f4d763aaSMatthew G. Knepley   Level: intermediate
6353f4d763aaSMatthew G. Knepley 
6354bb7acecfSBarry Smith   Note:
6355bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6356bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6357f4d763aaSMatthew G. Knepley 
6358db781477SPatrick Sanan .seealso: `VecView()`
6359f4d763aaSMatthew G. Knepley @*/
6360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6361d71ae5a4SJacob Faibussowitsch {
6362f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6363f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6364f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6365cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
63663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6367cdb7a50dSMatthew G. Knepley }
6368cdb7a50dSMatthew G. Knepley 
6369cdb7a50dSMatthew G. Knepley /*@C
6370bb7acecfSBarry Smith  DMOutputSequenceLoad - Retrieve the sequence value from a `PetscViewer`
6371cdb7a50dSMatthew G. Knepley 
6372cdb7a50dSMatthew G. Knepley   Input Parameters:
6373bb7acecfSBarry Smith + dm   - The original `DM`
6374cdb7a50dSMatthew G. Knepley . name - The sequence name
6375cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6376cdb7a50dSMatthew G. Knepley 
6377cdb7a50dSMatthew G. Knepley   Output Parameter:
6378cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6379cdb7a50dSMatthew G. Knepley 
6380cdb7a50dSMatthew G. Knepley   Level: intermediate
6381cdb7a50dSMatthew G. Knepley 
6382bb7acecfSBarry Smith   Note:
6383bb7acecfSBarry Smith   This is intended for output that should appear in sequence, for instance
6384bb7acecfSBarry Smith   a set of timesteps in an `PETSCVIEWERHDF5` file, or a set of realizations of a stochastic system.
6385bb7acecfSBarry Smith 
6386bb7acecfSBarry Smith   Developer Note:
6387bb7acecfSBarry Smith   It is unclear at the user API level why a `DM` is needed as input
6388cdb7a50dSMatthew G. Knepley 
6389db781477SPatrick Sanan .seealso: `DMGetOutputSequenceNumber()`, `DMSetOutputSequenceNumber()`, `VecView()`
6390cdb7a50dSMatthew G. Knepley @*/
6391d71ae5a4SJacob Faibussowitsch PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6392d71ae5a4SJacob Faibussowitsch {
6393cdb7a50dSMatthew G. Knepley   PetscBool ishdf5;
6394cdb7a50dSMatthew G. Knepley 
6395cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6396cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6397cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6398064a246eSJacob Faibussowitsch   PetscValidRealPointer(val, 5);
63999566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
6400cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6401cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6402cdb7a50dSMatthew G. Knepley     PetscScalar value;
6403cdb7a50dSMatthew G. Knepley 
64049566063dSJacob Faibussowitsch     PetscCall(DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer));
64054aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6406cdb7a50dSMatthew G. Knepley #endif
6407cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
64083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6409f4d763aaSMatthew G. Knepley }
64108e4ac7eaSMatthew G. Knepley 
64118e4ac7eaSMatthew G. Knepley /*@
6412bb7acecfSBarry Smith   DMGetUseNatural - Get the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64138e4ac7eaSMatthew G. Knepley 
6414*20f4b53cSBarry Smith   Not Collective
64158e4ac7eaSMatthew G. Knepley 
64168e4ac7eaSMatthew G. Knepley   Input Parameter:
6417bb7acecfSBarry Smith . dm - The `DM`
64188e4ac7eaSMatthew G. Knepley 
64198e4ac7eaSMatthew G. Knepley   Output Parameter:
6420bb7acecfSBarry Smith . useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64218e4ac7eaSMatthew G. Knepley 
64228e4ac7eaSMatthew G. Knepley   Level: beginner
64238e4ac7eaSMatthew G. Knepley 
6424db781477SPatrick Sanan .seealso: `DMSetUseNatural()`, `DMCreate()`
64258e4ac7eaSMatthew G. Knepley @*/
6426d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6427d71ae5a4SJacob Faibussowitsch {
64288e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64298e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6430534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
64318e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
64323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64338e4ac7eaSMatthew G. Knepley }
64348e4ac7eaSMatthew G. Knepley 
64358e4ac7eaSMatthew G. Knepley /*@
6436bb7acecfSBarry Smith   DMSetUseNatural - Set the flag for creating a mapping to the natural order when a `DM` is (re)distributed in parallel
64378e4ac7eaSMatthew G. Knepley 
6438*20f4b53cSBarry Smith   Collective
64398e4ac7eaSMatthew G. Knepley 
64408e4ac7eaSMatthew G. Knepley   Input Parameters:
6441bb7acecfSBarry Smith  + dm - The `DM`
6442bb7acecfSBarry Smith - useNatural - `PETSC_TRUE` to build the mapping to a natural order during distribution
64438e4ac7eaSMatthew G. Knepley 
6444bb7acecfSBarry Smith   Note:
6445bb7acecfSBarry Smith   This also causes the map to be build after `DMCreateSubDM()` and `DMCreateSuperDM()`
64465d3b26e6SMatthew G. Knepley 
64478e4ac7eaSMatthew G. Knepley   Level: beginner
64488e4ac7eaSMatthew G. Knepley 
6449db781477SPatrick Sanan .seealso: `DMGetUseNatural()`, `DMCreate()`, `DMPlexDistribute()`, `DMCreateSubDM()`, `DMCreateSuperDM()`
64508e4ac7eaSMatthew G. Knepley @*/
6451d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6452d71ae5a4SJacob Faibussowitsch {
64538e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
64548e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64558833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
64568e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
64573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64588e4ac7eaSMatthew G. Knepley }
6459c58f1c22SToby Isaac 
6460c58f1c22SToby Isaac /*@C
6461bb7acecfSBarry Smith   DMCreateLabel - Create a label of the given name if it does not already exist in the `DM`
6462c58f1c22SToby Isaac 
6463c58f1c22SToby Isaac   Not Collective
6464c58f1c22SToby Isaac 
6465c58f1c22SToby Isaac   Input Parameters:
6466bb7acecfSBarry Smith + dm   - The `DM` object
6467c58f1c22SToby Isaac - name - The label name
6468c58f1c22SToby Isaac 
6469c58f1c22SToby Isaac   Level: intermediate
6470c58f1c22SToby Isaac 
6471db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6472c58f1c22SToby Isaac @*/
6473d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabel(DM dm, const char name[])
6474d71ae5a4SJacob Faibussowitsch {
64755d80c0bfSVaclav Hapla   PetscBool flg;
64765d80c0bfSVaclav Hapla   DMLabel   label;
6477c58f1c22SToby Isaac 
6478c58f1c22SToby Isaac   PetscFunctionBegin;
6479c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6480c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
64819566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
6482c58f1c22SToby Isaac   if (!flg) {
64839566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
64849566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
64859566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
6486c58f1c22SToby Isaac   }
64873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6488c58f1c22SToby Isaac }
6489c58f1c22SToby Isaac 
6490c58f1c22SToby Isaac /*@C
6491bb7acecfSBarry 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.
64920fdc7489SMatthew Knepley 
64930fdc7489SMatthew Knepley   Not Collective
64940fdc7489SMatthew Knepley 
64950fdc7489SMatthew Knepley   Input Parameters:
6496bb7acecfSBarry Smith + dm   - The `DM` object
64970fdc7489SMatthew Knepley . l    - The index for the label
64980fdc7489SMatthew Knepley - name - The label name
64990fdc7489SMatthew Knepley 
65000fdc7489SMatthew Knepley   Level: intermediate
65010fdc7489SMatthew Knepley 
6502db781477SPatrick Sanan .seealso: `DMCreateLabel()`, `DMLabelCreate()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
65030fdc7489SMatthew Knepley @*/
6504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCreateLabelAtIndex(DM dm, PetscInt l, const char name[])
6505d71ae5a4SJacob Faibussowitsch {
65060fdc7489SMatthew Knepley   DMLabelLink orig, prev = NULL;
65070fdc7489SMatthew Knepley   DMLabel     label;
65080fdc7489SMatthew Knepley   PetscInt    Nl, m;
65090fdc7489SMatthew Knepley   PetscBool   flg, match;
65100fdc7489SMatthew Knepley   const char *lname;
65110fdc7489SMatthew Knepley 
65120fdc7489SMatthew Knepley   PetscFunctionBegin;
65130fdc7489SMatthew Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6514064a246eSJacob Faibussowitsch   PetscValidCharPointer(name, 3);
65159566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &flg));
65160fdc7489SMatthew Knepley   if (!flg) {
65179566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, name, &label));
65189566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dm, label));
65199566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&label));
65200fdc7489SMatthew Knepley   }
65219566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
652263a3b9bcSJacob Faibussowitsch   PetscCheck(l < Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label index %" PetscInt_FMT " must be in [0, %" PetscInt_FMT ")", l, Nl);
65230fdc7489SMatthew Knepley   for (m = 0, orig = dm->labels; m < Nl; ++m, prev = orig, orig = orig->next) {
65249566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)orig->label, &lname));
65259566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &match));
65260fdc7489SMatthew Knepley     if (match) break;
65270fdc7489SMatthew Knepley   }
65283ba16761SJacob Faibussowitsch   if (m == l) PetscFunctionReturn(PETSC_SUCCESS);
65290fdc7489SMatthew Knepley   if (!m) dm->labels = orig->next;
65300fdc7489SMatthew Knepley   else prev->next = orig->next;
65310fdc7489SMatthew Knepley   if (!l) {
65320fdc7489SMatthew Knepley     orig->next = dm->labels;
65330fdc7489SMatthew Knepley     dm->labels = orig;
65340fdc7489SMatthew Knepley   } else {
65359371c9d4SSatish Balay     for (m = 0, prev = dm->labels; m < l - 1; ++m, prev = prev->next)
65369371c9d4SSatish Balay       ;
65370fdc7489SMatthew Knepley     orig->next = prev->next;
65380fdc7489SMatthew Knepley     prev->next = orig;
65390fdc7489SMatthew Knepley   }
65403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65410fdc7489SMatthew Knepley }
65420fdc7489SMatthew Knepley 
65430fdc7489SMatthew Knepley /*@C
6544bb7acecfSBarry Smith   DMGetLabelValue - Get the value in a `DMLabel` for the given point, with -1 as the default
6545c58f1c22SToby Isaac 
6546c58f1c22SToby Isaac   Not Collective
6547c58f1c22SToby Isaac 
6548c58f1c22SToby Isaac   Input Parameters:
6549bb7acecfSBarry Smith + dm   - The `DM` object
6550c58f1c22SToby Isaac . name - The label name
6551c58f1c22SToby Isaac - point - The mesh point
6552c58f1c22SToby Isaac 
6553c58f1c22SToby Isaac   Output Parameter:
6554c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6555c58f1c22SToby Isaac 
6556c58f1c22SToby Isaac   Level: beginner
6557c58f1c22SToby Isaac 
6558db781477SPatrick Sanan .seealso: `DMLabelGetValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6559c58f1c22SToby Isaac @*/
6560d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6561d71ae5a4SJacob Faibussowitsch {
6562c58f1c22SToby Isaac   DMLabel label;
6563c58f1c22SToby Isaac 
6564c58f1c22SToby Isaac   PetscFunctionBegin;
6565c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6566c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65679566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
65687a8be351SBarry Smith   PetscCheck(label, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
65699566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, point, value));
65703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6571c58f1c22SToby Isaac }
6572c58f1c22SToby Isaac 
6573c58f1c22SToby Isaac /*@C
6574bb7acecfSBarry Smith   DMSetLabelValue - Add a point to a `DMLabel` with given value
6575c58f1c22SToby Isaac 
6576c58f1c22SToby Isaac   Not Collective
6577c58f1c22SToby Isaac 
6578c58f1c22SToby Isaac   Input Parameters:
6579bb7acecfSBarry Smith + dm   - The `DM` object
6580c58f1c22SToby Isaac . name - The label name
6581c58f1c22SToby Isaac . point - The mesh point
6582c58f1c22SToby Isaac - value - The label value for this point
6583c58f1c22SToby Isaac 
6584c58f1c22SToby Isaac   Output Parameter:
6585c58f1c22SToby Isaac 
6586c58f1c22SToby Isaac   Level: beginner
6587c58f1c22SToby Isaac 
6588db781477SPatrick Sanan .seealso: `DMLabelSetValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6589c58f1c22SToby Isaac @*/
6590d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6591d71ae5a4SJacob Faibussowitsch {
6592c58f1c22SToby Isaac   DMLabel label;
6593c58f1c22SToby Isaac 
6594c58f1c22SToby Isaac   PetscFunctionBegin;
6595c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6596c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
65979566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6598c58f1c22SToby Isaac   if (!label) {
65999566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
66009566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
6601c58f1c22SToby Isaac   }
66029566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(label, point, value));
66033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6604c58f1c22SToby Isaac }
6605c58f1c22SToby Isaac 
6606c58f1c22SToby Isaac /*@C
6607bb7acecfSBarry Smith   DMClearLabelValue - Remove a point from a `DMLabel` with given value
6608c58f1c22SToby Isaac 
6609c58f1c22SToby Isaac   Not Collective
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac   Input Parameters:
6612bb7acecfSBarry Smith + dm   - The `DM` object
6613c58f1c22SToby Isaac . name - The label name
6614c58f1c22SToby Isaac . point - The mesh point
6615c58f1c22SToby Isaac - value - The label value for this point
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac   Level: beginner
6618c58f1c22SToby Isaac 
6619db781477SPatrick Sanan .seealso: `DMLabelClearValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6620c58f1c22SToby Isaac @*/
6621d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6622d71ae5a4SJacob Faibussowitsch {
6623c58f1c22SToby Isaac   DMLabel label;
6624c58f1c22SToby Isaac 
6625c58f1c22SToby Isaac   PetscFunctionBegin;
6626c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6627c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66289566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
66293ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66309566063dSJacob Faibussowitsch   PetscCall(DMLabelClearValue(label, point, value));
66313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6632c58f1c22SToby Isaac }
6633c58f1c22SToby Isaac 
6634c58f1c22SToby Isaac /*@C
6635bb7acecfSBarry Smith   DMGetLabelSize - Get the value of `DMLabelGetNumValues()` of a `DMLabel` in the `DM`
6636c58f1c22SToby Isaac 
6637c58f1c22SToby Isaac   Not Collective
6638c58f1c22SToby Isaac 
6639c58f1c22SToby Isaac   Input Parameters:
6640bb7acecfSBarry Smith + dm   - The `DM` object
6641c58f1c22SToby Isaac - name - The label name
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac   Output Parameter:
6644c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac   Level: beginner
6647c58f1c22SToby Isaac 
6648bb7acecfSBarry Smith   Developer Note:
6649bb7acecfSBarry Smith   This should be renamed to something like `DMGetLabelNumValues()` or removed.
6650bb7acecfSBarry Smith 
6651bb7acecfSBarry Smith .seealso: `DMLabelGetNumValues()`, `DMSetLabelValue()`, `DMGetLabel()`
6652c58f1c22SToby Isaac @*/
6653d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6654d71ae5a4SJacob Faibussowitsch {
6655c58f1c22SToby Isaac   DMLabel label;
6656c58f1c22SToby Isaac 
6657c58f1c22SToby Isaac   PetscFunctionBegin;
6658c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6659c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6660534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
66619566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6662c58f1c22SToby Isaac   *size = 0;
66633ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
66649566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, size));
66653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6666c58f1c22SToby Isaac }
6667c58f1c22SToby Isaac 
6668c58f1c22SToby Isaac /*@C
6669bb7acecfSBarry Smith   DMGetLabelIdIS - Get the `DMLabelGetValueIS()` from a `DMLabel` in the `DM`
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Not Collective
6672c58f1c22SToby Isaac 
6673c58f1c22SToby Isaac   Input Parameters:
6674bb7acecfSBarry Smith + mesh - The `DM` object
6675c58f1c22SToby Isaac - name - The label name
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac   Output Parameter:
6678*20f4b53cSBarry Smith . ids - The integer ids, or `NULL` if the label does not exist
6679c58f1c22SToby Isaac 
6680c58f1c22SToby Isaac   Level: beginner
6681c58f1c22SToby Isaac 
6682db781477SPatrick Sanan .seealso: `DMLabelGetValueIS()`, `DMGetLabelSize()`
6683c58f1c22SToby Isaac @*/
6684d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6685d71ae5a4SJacob Faibussowitsch {
6686c58f1c22SToby Isaac   DMLabel label;
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   PetscFunctionBegin;
6689c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6690c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6691c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
66929566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6693c58f1c22SToby Isaac   *ids = NULL;
6694dab2e251SBlaise Bourdin   if (label) {
66959566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, ids));
6696dab2e251SBlaise Bourdin   } else {
6697dab2e251SBlaise Bourdin     /* returning an empty IS */
66989566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_USE_POINTER, ids));
6699dab2e251SBlaise Bourdin   }
67003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6701c58f1c22SToby Isaac }
6702c58f1c22SToby Isaac 
6703c58f1c22SToby Isaac /*@C
6704c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6705c58f1c22SToby Isaac 
6706c58f1c22SToby Isaac   Not Collective
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   Input Parameters:
6709bb7acecfSBarry Smith + dm - The `DM` object
6710c58f1c22SToby Isaac . name - The label name
6711c58f1c22SToby Isaac - value - The stratum value
6712c58f1c22SToby Isaac 
6713c58f1c22SToby Isaac   Output Parameter:
6714bb7acecfSBarry Smith . size - The number of points, also called the stratum size
6715c58f1c22SToby Isaac 
6716c58f1c22SToby Isaac   Level: beginner
6717c58f1c22SToby Isaac 
6718db781477SPatrick Sanan .seealso: `DMLabelGetStratumSize()`, `DMGetLabelSize()`, `DMGetLabelIds()`
6719c58f1c22SToby Isaac @*/
6720d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6721d71ae5a4SJacob Faibussowitsch {
6722c58f1c22SToby Isaac   DMLabel label;
6723c58f1c22SToby Isaac 
6724c58f1c22SToby Isaac   PetscFunctionBegin;
6725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6726c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6727534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
67289566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6729c58f1c22SToby Isaac   *size = 0;
67303ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67319566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(label, value, size));
67323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6733c58f1c22SToby Isaac }
6734c58f1c22SToby Isaac 
6735c58f1c22SToby Isaac /*@C
6736c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6737c58f1c22SToby Isaac 
6738c58f1c22SToby Isaac   Not Collective
6739c58f1c22SToby Isaac 
6740c58f1c22SToby Isaac   Input Parameters:
6741bb7acecfSBarry Smith + dm - The `DM` object
6742c58f1c22SToby Isaac . name - The label name
6743c58f1c22SToby Isaac - value - The stratum value
6744c58f1c22SToby Isaac 
6745c58f1c22SToby Isaac   Output Parameter:
6746*20f4b53cSBarry Smith . points - The stratum points, or `NULL` if the label does not exist or does not have that value
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Level: beginner
6749c58f1c22SToby Isaac 
6750db781477SPatrick Sanan .seealso: `DMLabelGetStratumIS()`, `DMGetStratumSize()`
6751c58f1c22SToby Isaac @*/
6752d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6753d71ae5a4SJacob Faibussowitsch {
6754c58f1c22SToby Isaac   DMLabel label;
6755c58f1c22SToby Isaac 
6756c58f1c22SToby Isaac   PetscFunctionBegin;
6757c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6758c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6759c58f1c22SToby Isaac   PetscValidPointer(points, 4);
67609566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
6761c58f1c22SToby Isaac   *points = NULL;
67623ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67639566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, value, points));
67643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6765c58f1c22SToby Isaac }
6766c58f1c22SToby Isaac 
67674de306b1SToby Isaac /*@C
67689044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
67694de306b1SToby Isaac 
67704de306b1SToby Isaac   Not Collective
67714de306b1SToby Isaac 
67724de306b1SToby Isaac   Input Parameters:
6773bb7acecfSBarry Smith + dm - The `DM` object
67744de306b1SToby Isaac . name - The label name
67754de306b1SToby Isaac . value - The stratum value
67764de306b1SToby Isaac - points - The stratum points
67774de306b1SToby Isaac 
67784de306b1SToby Isaac   Level: beginner
67794de306b1SToby Isaac 
6780bb7acecfSBarry Smith .seealso: `DMLabel`, `DMClearLabelStratum()`, `DMLabelClearStratum()`, `DMLabelSetStratumIS()`, `DMGetStratumSize()`
67814de306b1SToby Isaac @*/
6782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6783d71ae5a4SJacob Faibussowitsch {
67844de306b1SToby Isaac   DMLabel label;
67854de306b1SToby Isaac 
67864de306b1SToby Isaac   PetscFunctionBegin;
67874de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67884de306b1SToby Isaac   PetscValidCharPointer(name, 2);
67894de306b1SToby Isaac   PetscValidPointer(points, 4);
67909566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
67913ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
67929566063dSJacob Faibussowitsch   PetscCall(DMLabelSetStratumIS(label, value, points));
67933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
67944de306b1SToby Isaac }
67954de306b1SToby Isaac 
6796c58f1c22SToby Isaac /*@C
6797bb7acecfSBarry Smith   DMClearLabelStratum - Remove all points from a stratum from a `DMLabel`
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Not Collective
6800c58f1c22SToby Isaac 
6801c58f1c22SToby Isaac   Input Parameters:
6802bb7acecfSBarry Smith + dm   - The `DM` object
6803c58f1c22SToby Isaac . name - The label name
6804c58f1c22SToby Isaac - value - The label value for this point
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Output Parameter:
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac   Level: beginner
6809c58f1c22SToby Isaac 
6810bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabelClearStratum()`, `DMSetLabelValue()`, `DMGetStratumIS()`, `DMClearLabelValue()`
6811c58f1c22SToby Isaac @*/
6812d71ae5a4SJacob Faibussowitsch PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6813d71ae5a4SJacob Faibussowitsch {
6814c58f1c22SToby Isaac   DMLabel label;
6815c58f1c22SToby Isaac 
6816c58f1c22SToby Isaac   PetscFunctionBegin;
6817c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6818c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
68199566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
68203ba16761SJacob Faibussowitsch   if (!label) PetscFunctionReturn(PETSC_SUCCESS);
68219566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(label, value));
68223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6823c58f1c22SToby Isaac }
6824c58f1c22SToby Isaac 
6825c58f1c22SToby Isaac /*@
6826bb7acecfSBarry Smith   DMGetNumLabels - Return the number of labels defined by on the `DM`
6827c58f1c22SToby Isaac 
6828c58f1c22SToby Isaac   Not Collective
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac   Input Parameter:
6831bb7acecfSBarry Smith . dm   - The `DM` object
6832c58f1c22SToby Isaac 
6833c58f1c22SToby Isaac   Output Parameter:
6834c58f1c22SToby Isaac . numLabels - the number of Labels
6835c58f1c22SToby Isaac 
6836c58f1c22SToby Isaac   Level: intermediate
6837c58f1c22SToby Isaac 
6838bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabelName()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6839c58f1c22SToby Isaac @*/
6840d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6841d71ae5a4SJacob Faibussowitsch {
68425d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6843c58f1c22SToby Isaac   PetscInt    n    = 0;
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   PetscFunctionBegin;
6846c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6847534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
68489371c9d4SSatish Balay   while (next) {
68499371c9d4SSatish Balay     ++n;
68509371c9d4SSatish Balay     next = next->next;
68519371c9d4SSatish Balay   }
6852c58f1c22SToby Isaac   *numLabels = n;
68533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6854c58f1c22SToby Isaac }
6855c58f1c22SToby Isaac 
6856c58f1c22SToby Isaac /*@C
6857c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6858c58f1c22SToby Isaac 
6859c58f1c22SToby Isaac   Not Collective
6860c58f1c22SToby Isaac 
6861c58f1c22SToby Isaac   Input Parameters:
6862bb7acecfSBarry Smith + dm - The `DM` object
6863c58f1c22SToby Isaac - n  - the label number
6864c58f1c22SToby Isaac 
6865c58f1c22SToby Isaac   Output Parameter:
6866c58f1c22SToby Isaac . name - the label name
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Level: intermediate
6869c58f1c22SToby Isaac 
6870bb7acecfSBarry Smith   Developer Note:
6871bb7acecfSBarry Smith   Some of the functions that appropriate on labels using their number have the suffix ByNum, others do not.
6872bb7acecfSBarry Smith 
6873bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabelByNum()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6874c58f1c22SToby Isaac @*/
6875d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6876d71ae5a4SJacob Faibussowitsch {
68775d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6878c58f1c22SToby Isaac   PetscInt    l    = 0;
6879c58f1c22SToby Isaac 
6880c58f1c22SToby Isaac   PetscFunctionBegin;
6881c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6882c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6883c58f1c22SToby Isaac   while (next) {
6884c58f1c22SToby Isaac     if (l == n) {
68859566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)next->label, name));
68863ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
6887c58f1c22SToby Isaac     }
6888c58f1c22SToby Isaac     ++l;
6889c58f1c22SToby Isaac     next = next->next;
6890c58f1c22SToby Isaac   }
689163a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
6892c58f1c22SToby Isaac }
6893c58f1c22SToby Isaac 
6894c58f1c22SToby Isaac /*@C
6895bb7acecfSBarry Smith   DMHasLabel - Determine whether the `DM` has a label of a given name
6896c58f1c22SToby Isaac 
6897c58f1c22SToby Isaac   Not Collective
6898c58f1c22SToby Isaac 
6899c58f1c22SToby Isaac   Input Parameters:
6900bb7acecfSBarry Smith + dm   - The `DM` object
6901c58f1c22SToby Isaac - name - The label name
6902c58f1c22SToby Isaac 
6903c58f1c22SToby Isaac   Output Parameter:
6904bb7acecfSBarry Smith . hasLabel - `PETSC_TRUE` if the label is present
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   Level: intermediate
6907c58f1c22SToby Isaac 
6908bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetLabel()`, `DMGetLabelByNum()`, `DMCreateLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6909c58f1c22SToby Isaac @*/
6910d71ae5a4SJacob Faibussowitsch PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6911d71ae5a4SJacob Faibussowitsch {
69125d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6913d67d17b1SMatthew G. Knepley   const char *lname;
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   PetscFunctionBegin;
6916c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6917c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6918534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
6919c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6920c58f1c22SToby Isaac   while (next) {
69219566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69229566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, hasLabel));
6923c58f1c22SToby Isaac     if (*hasLabel) break;
6924c58f1c22SToby Isaac     next = next->next;
6925c58f1c22SToby Isaac   }
69263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6927c58f1c22SToby Isaac }
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac /*@C
6930*20f4b53cSBarry Smith   DMGetLabel - Return the label of a given name, or `NULL`, from a `DM`
6931c58f1c22SToby Isaac 
6932c58f1c22SToby Isaac   Not Collective
6933c58f1c22SToby Isaac 
6934c58f1c22SToby Isaac   Input Parameters:
6935bb7acecfSBarry Smith + dm   - The `DM` object
6936c58f1c22SToby Isaac - name - The label name
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Output Parameter:
6939*20f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent
6940c58f1c22SToby Isaac 
6941bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
6942bb7acecfSBarry Smith +   "depth"       - Holds the depth (co-dimension) of each mesh point
6943bb7acecfSBarry Smith .   "celltype"    - Holds the topological type of each cell
6944bb7acecfSBarry Smith .   "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
6945bb7acecfSBarry Smith .   "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
6946bb7acecfSBarry Smith .   "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
6947bb7acecfSBarry Smith -  "Vertex Sets" - Mirrors the vertex sets defined by GMsh
69486d7c9049SMatthew G. Knepley 
6949c58f1c22SToby Isaac   Level: intermediate
6950c58f1c22SToby Isaac 
6951bb7acecfSBarry Smith .seealso: `DMLabel`, `DMHasLabel()`, `DMGetLabelByNum()`, `DMAddLabel()`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
6952c58f1c22SToby Isaac @*/
6953d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6954d71ae5a4SJacob Faibussowitsch {
69555d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6956c58f1c22SToby Isaac   PetscBool   hasLabel;
6957d67d17b1SMatthew G. Knepley   const char *lname;
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   PetscFunctionBegin;
6960c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6961c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6962c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6963c58f1c22SToby Isaac   *label = NULL;
6964c58f1c22SToby Isaac   while (next) {
69659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
69669566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
6967c58f1c22SToby Isaac     if (hasLabel) {
6968c58f1c22SToby Isaac       *label = next->label;
6969c58f1c22SToby Isaac       break;
6970c58f1c22SToby Isaac     }
6971c58f1c22SToby Isaac     next = next->next;
6972c58f1c22SToby Isaac   }
69733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6974c58f1c22SToby Isaac }
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac /*@C
6977bb7acecfSBarry Smith   DMGetLabelByNum - Return the nth label on a `DM`
6978c58f1c22SToby Isaac 
6979c58f1c22SToby Isaac   Not Collective
6980c58f1c22SToby Isaac 
6981c58f1c22SToby Isaac   Input Parameters:
6982bb7acecfSBarry Smith + dm - The `DM` object
6983c58f1c22SToby Isaac - n  - the label number
6984c58f1c22SToby Isaac 
6985c58f1c22SToby Isaac   Output Parameter:
6986c58f1c22SToby Isaac . label - the label
6987c58f1c22SToby Isaac 
6988c58f1c22SToby Isaac   Level: intermediate
6989c58f1c22SToby Isaac 
6990bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
6991c58f1c22SToby Isaac @*/
6992d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6993d71ae5a4SJacob Faibussowitsch {
69945d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6995c58f1c22SToby Isaac   PetscInt    l    = 0;
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac   PetscFunctionBegin;
6998c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6999c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7000c58f1c22SToby Isaac   while (next) {
7001c58f1c22SToby Isaac     if (l == n) {
7002c58f1c22SToby Isaac       *label = next->label;
70033ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
7004c58f1c22SToby Isaac     }
7005c58f1c22SToby Isaac     ++l;
7006c58f1c22SToby Isaac     next = next->next;
7007c58f1c22SToby Isaac   }
700863a3b9bcSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %" PetscInt_FMT " does not exist in this DM", n);
7009c58f1c22SToby Isaac }
7010c58f1c22SToby Isaac 
7011c58f1c22SToby Isaac /*@C
7012bb7acecfSBarry Smith   DMAddLabel - Add the label to this `DM`
7013c58f1c22SToby Isaac 
7014c58f1c22SToby Isaac   Not Collective
7015c58f1c22SToby Isaac 
7016c58f1c22SToby Isaac   Input Parameters:
7017bb7acecfSBarry Smith + dm   - The `DM` object
7018bb7acecfSBarry Smith - label - The `DMLabel`
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac   Level: developer
7021c58f1c22SToby Isaac 
7022bb7acecfSBarry Smith .seealso: `DMLabel`, `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7023c58f1c22SToby Isaac @*/
7024d71ae5a4SJacob Faibussowitsch PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7025d71ae5a4SJacob Faibussowitsch {
70265d80c0bfSVaclav Hapla   DMLabelLink l, *p, tmpLabel;
7027c58f1c22SToby Isaac   PetscBool   hasLabel;
7028d67d17b1SMatthew G. Knepley   const char *lname;
70295d80c0bfSVaclav Hapla   PetscBool   flg;
7030c58f1c22SToby Isaac 
7031c58f1c22SToby Isaac   PetscFunctionBegin;
7032c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70339566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &lname));
70349566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, lname, &hasLabel));
70357a8be351SBarry Smith   PetscCheck(!hasLabel, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
70369566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(1, &tmpLabel));
7037c58f1c22SToby Isaac   tmpLabel->label  = label;
7038c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
70395d80c0bfSVaclav Hapla   for (p = &dm->labels; (l = *p); p = &l->next) { }
70405d80c0bfSVaclav Hapla   *p = tmpLabel;
70419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)label));
70429566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "depth", &flg));
70435d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
70449566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(lname, "celltype", &flg));
7045ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
70463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7047c58f1c22SToby Isaac }
7048c58f1c22SToby Isaac 
7049c58f1c22SToby Isaac /*@C
70504a7ee7d0SMatthew G. Knepley   DMSetLabel - Replaces the label of a given name, or ignores it if the name is not present
70514a7ee7d0SMatthew G. Knepley 
70524a7ee7d0SMatthew G. Knepley   Not Collective
70534a7ee7d0SMatthew G. Knepley 
70544a7ee7d0SMatthew G. Knepley   Input Parameters:
7055bb7acecfSBarry Smith + dm    - The `DM` object
7056bb7acecfSBarry Smith - label - The `DMLabel`, having the same name, to substitute
70574a7ee7d0SMatthew G. Knepley 
7058bb7acecfSBarry Smith   Default labels in a `DMPLEX`:
7059bb7acecfSBarry Smith +  "depth"       - Holds the depth (co-dimension) of each mesh point
7060bb7acecfSBarry Smith .  "celltype"    - Holds the topological type of each cell
7061bb7acecfSBarry Smith .  "ghost"       - If the DM is distributed with overlap, this marks the cells and faces in the overlap
7062bb7acecfSBarry Smith .  "Cell Sets"   - Mirrors the cell sets defined by GMsh and ExodusII
7063bb7acecfSBarry Smith .  "Face Sets"   - Mirrors the face sets defined by GMsh and ExodusII
7064bb7acecfSBarry Smith - "Vertex Sets" - Mirrors the vertex sets defined by GMsh
70654a7ee7d0SMatthew G. Knepley 
70664a7ee7d0SMatthew G. Knepley   Level: intermediate
70674a7ee7d0SMatthew G. Knepley 
7068bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMPlexGetDepthLabel()`, `DMPlexGetCellType()`
70694a7ee7d0SMatthew G. Knepley @*/
7070d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabel(DM dm, DMLabel label)
7071d71ae5a4SJacob Faibussowitsch {
70724a7ee7d0SMatthew G. Knepley   DMLabelLink next = dm->labels;
70734a7ee7d0SMatthew G. Knepley   PetscBool   hasLabel, flg;
70744a7ee7d0SMatthew G. Knepley   const char *name, *lname;
70754a7ee7d0SMatthew G. Knepley 
70764a7ee7d0SMatthew G. Knepley   PetscFunctionBegin;
70774a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
70784a7ee7d0SMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
70799566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)label, &name));
70804a7ee7d0SMatthew G. Knepley   while (next) {
70819566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
70829566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
70834a7ee7d0SMatthew G. Knepley     if (hasLabel) {
70849566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)label));
70859566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "depth", &flg));
70864a7ee7d0SMatthew G. Knepley       if (flg) dm->depthLabel = label;
70879566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(lname, "celltype", &flg));
70884a7ee7d0SMatthew G. Knepley       if (flg) dm->celltypeLabel = label;
70899566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&next->label));
70904a7ee7d0SMatthew G. Knepley       next->label = label;
70914a7ee7d0SMatthew G. Knepley       break;
70924a7ee7d0SMatthew G. Knepley     }
70934a7ee7d0SMatthew G. Knepley     next = next->next;
70944a7ee7d0SMatthew G. Knepley   }
70953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70964a7ee7d0SMatthew G. Knepley }
70974a7ee7d0SMatthew G. Knepley 
70984a7ee7d0SMatthew G. Knepley /*@C
7099bb7acecfSBarry Smith   DMRemoveLabel - Remove the label given by name from this `DM`
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac   Not Collective
7102c58f1c22SToby Isaac 
7103c58f1c22SToby Isaac   Input Parameters:
7104bb7acecfSBarry Smith + dm   - The `DM` object
7105c58f1c22SToby Isaac - name - The label name
7106c58f1c22SToby Isaac 
7107c58f1c22SToby Isaac   Output Parameter:
7108*20f4b53cSBarry Smith . label - The `DMLabel`, or `NULL` if the label is absent. Pass in `NULL` to call `DMLabelDestroy()` on the label, otherwise the
7109bb7acecfSBarry Smith           caller is responsible for calling `DMLabelDestroy()`.
7110c58f1c22SToby Isaac 
7111c58f1c22SToby Isaac   Level: developer
7112c58f1c22SToby Isaac 
7113bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabelBySelf()`
7114c58f1c22SToby Isaac @*/
7115d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7116d71ae5a4SJacob Faibussowitsch {
711795d578d6SVaclav Hapla   DMLabelLink link, *pnext;
7118c58f1c22SToby Isaac   PetscBool   hasLabel;
7119d67d17b1SMatthew G. Knepley   const char *lname;
7120c58f1c22SToby Isaac 
7121c58f1c22SToby Isaac   PetscFunctionBegin;
7122c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7123e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7124e5472504SVaclav Hapla   if (label) {
7125e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7126c58f1c22SToby Isaac     *label = NULL;
7127e5472504SVaclav Hapla   }
71285d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
71299566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)link->label, &lname));
71309566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &hasLabel));
7131c58f1c22SToby Isaac     if (hasLabel) {
713295d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
71339566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &hasLabel));
713495d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
71359566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &hasLabel));
7136ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
713795d578d6SVaclav Hapla       if (label) *label = link->label;
71389566063dSJacob Faibussowitsch       else PetscCall(DMLabelDestroy(&link->label));
71399566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7140c58f1c22SToby Isaac       break;
7141c58f1c22SToby Isaac     }
7142c58f1c22SToby Isaac   }
71433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7144c58f1c22SToby Isaac }
7145c58f1c22SToby Isaac 
7146306894acSVaclav Hapla /*@
7147bb7acecfSBarry Smith   DMRemoveLabelBySelf - Remove the label from this `DM`
7148306894acSVaclav Hapla 
7149306894acSVaclav Hapla   Not Collective
7150306894acSVaclav Hapla 
7151306894acSVaclav Hapla   Input Parameters:
7152bb7acecfSBarry Smith + dm   - The `DM` object
7153bb7acecfSBarry Smith . label - The `DMLabel` to be removed from the `DM`
7154*20f4b53cSBarry Smith - failNotFound - Should it fail if the label is not found in the `DM`?
7155306894acSVaclav Hapla 
7156306894acSVaclav Hapla   Level: developer
7157306894acSVaclav Hapla 
7158bb7acecfSBarry Smith   Note:
7159306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7160bb7acecfSBarry Smith   If the `DM` has an exclusive reference to the label, the label gets destroyed and
7161306894acSVaclav Hapla   *label nullified.
7162306894acSVaclav Hapla 
7163bb7acecfSBarry Smith .seealso: `DMLabel`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabel()` `DMGetLabelValue()`, `DMSetLabelValue()`, `DMLabelDestroy()`, `DMRemoveLabel()`
7164306894acSVaclav Hapla @*/
7165d71ae5a4SJacob Faibussowitsch PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7166d71ae5a4SJacob Faibussowitsch {
716743e45a93SVaclav Hapla   DMLabelLink link, *pnext;
7168306894acSVaclav Hapla   PetscBool   hasLabel = PETSC_FALSE;
7169306894acSVaclav Hapla 
7170306894acSVaclav Hapla   PetscFunctionBegin;
7171306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7172306894acSVaclav Hapla   PetscValidPointer(label, 2);
71733ba16761SJacob Faibussowitsch   if (!*label && !failNotFound) PetscFunctionReturn(PETSC_SUCCESS);
7174306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7175306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm, failNotFound, 3);
71765d80c0bfSVaclav Hapla   for (pnext = &dm->labels; (link = *pnext); pnext = &link->next) {
717743e45a93SVaclav Hapla     if (*label == link->label) {
7178306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
717943e45a93SVaclav Hapla       *pnext   = link->next; /* Remove from list */
7180306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7181ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
718243e45a93SVaclav Hapla       if (((PetscObject)link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
71839566063dSJacob Faibussowitsch       PetscCall(DMLabelDestroy(&link->label));
71849566063dSJacob Faibussowitsch       PetscCall(PetscFree(link));
7185306894acSVaclav Hapla       break;
7186306894acSVaclav Hapla     }
7187306894acSVaclav Hapla   }
71887a8be351SBarry Smith   PetscCheck(hasLabel || !failNotFound, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
71893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7190306894acSVaclav Hapla }
7191306894acSVaclav Hapla 
7192c58f1c22SToby Isaac /*@C
7193c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7194c58f1c22SToby Isaac 
7195c58f1c22SToby Isaac   Not Collective
7196c58f1c22SToby Isaac 
7197c58f1c22SToby Isaac   Input Parameters:
7198bb7acecfSBarry Smith + dm   - The `DM` object
7199c58f1c22SToby Isaac - name - The label name
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   Output Parameter:
7202c58f1c22SToby Isaac . output - The flag for output
7203c58f1c22SToby Isaac 
7204c58f1c22SToby Isaac   Level: developer
7205c58f1c22SToby Isaac 
7206bb7acecfSBarry Smith .seealso: `DMLabel`, `DMSetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7207c58f1c22SToby Isaac @*/
7208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7209d71ae5a4SJacob Faibussowitsch {
72105d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7211d67d17b1SMatthew G. Knepley   const char *lname;
7212c58f1c22SToby Isaac 
7213c58f1c22SToby Isaac   PetscFunctionBegin;
7214c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7215dadcf809SJacob Faibussowitsch   PetscValidCharPointer(name, 2);
7216dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(output, 3);
7217c58f1c22SToby Isaac   while (next) {
7218c58f1c22SToby Isaac     PetscBool flg;
7219c58f1c22SToby Isaac 
72209566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72219566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72229371c9d4SSatish Balay     if (flg) {
72239371c9d4SSatish Balay       *output = next->output;
72243ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72259371c9d4SSatish Balay     }
7226c58f1c22SToby Isaac     next = next->next;
7227c58f1c22SToby Isaac   }
722898921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7229c58f1c22SToby Isaac }
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac /*@C
7232bb7acecfSBarry Smith   DMSetLabelOutput - Set if a given label should be saved to a `PetscViewer` in calls to `DMView()`
7233c58f1c22SToby Isaac 
7234c58f1c22SToby Isaac   Not Collective
7235c58f1c22SToby Isaac 
7236c58f1c22SToby Isaac   Input Parameters:
7237bb7acecfSBarry Smith + dm     - The `DM` object
7238c58f1c22SToby Isaac . name   - The label name
7239bb7acecfSBarry Smith - output - `PETSC_TRUE` to save the label to the viewer
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Level: developer
7242c58f1c22SToby Isaac 
7243bb7acecfSBarry Smith .seealso: `DMLabel`, `DMGetOutputFlag()`, `DMGetLabelOutput()`, `DMCreateLabel()`, `DMHasLabel()`, `DMGetLabelValue()`, `DMSetLabelValue()`, `DMGetStratumIS()`
7244c58f1c22SToby Isaac @*/
7245d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7246d71ae5a4SJacob Faibussowitsch {
72475d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7248d67d17b1SMatthew G. Knepley   const char *lname;
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   PetscFunctionBegin;
7251c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7252534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7253c58f1c22SToby Isaac   while (next) {
7254c58f1c22SToby Isaac     PetscBool flg;
7255c58f1c22SToby Isaac 
72569566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)next->label, &lname));
72579566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, lname, &flg));
72589371c9d4SSatish Balay     if (flg) {
72599371c9d4SSatish Balay       next->output = output;
72603ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
72619371c9d4SSatish Balay     }
7262c58f1c22SToby Isaac     next = next->next;
7263c58f1c22SToby Isaac   }
726498921bdaSJacob Faibussowitsch   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7265c58f1c22SToby Isaac }
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac /*@
7268bb7acecfSBarry Smith   DMCopyLabels - Copy labels from one `DM` mesh to another `DM` with a superset of the points
7269c58f1c22SToby Isaac 
7270*20f4b53cSBarry Smith   Collective
7271c58f1c22SToby Isaac 
7272d8d19677SJose E. Roman   Input Parameters:
7273bb7acecfSBarry Smith + dmA - The `DM` object with initial labels
7274bb7acecfSBarry Smith . dmB - The `DM` object to which labels are copied
7275bb7acecfSBarry Smith . mode - Copy labels by pointers (`PETSC_OWN_POINTER`) or duplicate them (`PETSC_COPY_VALUES`)
7276bb7acecfSBarry Smith . all  - Copy all labels including "depth", "dim", and "celltype" (`PETSC_TRUE`) which are otherwise ignored (`PETSC_FALSE`)
7277bb7acecfSBarry Smith - emode - How to behave when a `DMLabel` in the source and destination `DM`s with the same name is encountered (see `DMCopyLabelsMode`)
7278c58f1c22SToby Isaac 
7279c58f1c22SToby Isaac   Level: intermediate
7280c58f1c22SToby Isaac 
7281bb7acecfSBarry Smith   Note:
72822cbb9b06SVaclav Hapla   This is typically used when interpolating or otherwise adding to a mesh, or testing.
7283c58f1c22SToby Isaac 
7284bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`
7285c58f1c22SToby Isaac @*/
7286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all, DMCopyLabelsMode emode)
7287d71ae5a4SJacob Faibussowitsch {
72882cbb9b06SVaclav Hapla   DMLabel     label, labelNew, labelOld;
7289c58f1c22SToby Isaac   const char *name;
7290c58f1c22SToby Isaac   PetscBool   flg;
72915d80c0bfSVaclav Hapla   DMLabelLink link;
7292c58f1c22SToby Isaac 
72935d80c0bfSVaclav Hapla   PetscFunctionBegin;
72945d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
72955d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
72965d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode, 3);
72975d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
72987a8be351SBarry Smith   PetscCheck(mode != PETSC_USE_POINTER, PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
72993ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
73005d80c0bfSVaclav Hapla   for (link = dmA->labels; link; link = link->next) {
73015d80c0bfSVaclav Hapla     label = link->label;
73029566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)label, &name));
73035d80c0bfSVaclav Hapla     if (!all) {
73049566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "depth", &flg));
7305c58f1c22SToby Isaac       if (flg) continue;
73069566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "dim", &flg));
73077d5acc75SStefano Zampini       if (flg) continue;
73089566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, "celltype", &flg));
7309ba2698f1SMatthew G. Knepley       if (flg) continue;
73105d80c0bfSVaclav Hapla     }
73119566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmB, name, &labelOld));
73122cbb9b06SVaclav Hapla     if (labelOld) {
73132cbb9b06SVaclav Hapla       switch (emode) {
7314d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_KEEP:
7315d71ae5a4SJacob Faibussowitsch         continue;
7316d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_REPLACE:
7317d71ae5a4SJacob Faibussowitsch         PetscCall(DMRemoveLabelBySelf(dmB, &labelOld, PETSC_TRUE));
7318d71ae5a4SJacob Faibussowitsch         break;
7319d71ae5a4SJacob Faibussowitsch       case DM_COPY_LABELS_FAIL:
7320d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in destination DM", name);
7321d71ae5a4SJacob Faibussowitsch       default:
7322d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Unhandled DMCopyLabelsMode %d", (int)emode);
73232cbb9b06SVaclav Hapla       }
73242cbb9b06SVaclav Hapla     }
73255d80c0bfSVaclav Hapla     if (mode == PETSC_COPY_VALUES) {
73269566063dSJacob Faibussowitsch       PetscCall(DMLabelDuplicate(label, &labelNew));
73275d80c0bfSVaclav Hapla     } else {
73285d80c0bfSVaclav Hapla       labelNew = label;
73295d80c0bfSVaclav Hapla     }
73309566063dSJacob Faibussowitsch     PetscCall(DMAddLabel(dmB, labelNew));
73319566063dSJacob Faibussowitsch     if (mode == PETSC_COPY_VALUES) PetscCall(DMLabelDestroy(&labelNew));
7332c58f1c22SToby Isaac   }
73333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7334c58f1c22SToby Isaac }
7335461a15a0SLisandro Dalcin 
7336609dae6eSVaclav Hapla /*@C
7337bb7acecfSBarry Smith   DMCompareLabels - Compare labels of two `DMPLEX` meshes
7338609dae6eSVaclav Hapla 
7339*20f4b53cSBarry Smith   Collective; No Fortran Support
7340609dae6eSVaclav Hapla 
7341609dae6eSVaclav Hapla   Input Parameters:
7342bb7acecfSBarry Smith + dm0 - First `DM` object
7343bb7acecfSBarry Smith - dm1 - Second `DM` object
7344609dae6eSVaclav Hapla 
7345609dae6eSVaclav Hapla   Output Parameters
73465efe38ccSVaclav Hapla + equal   - (Optional) Flag whether labels of dm0 and dm1 are the same
7347*20f4b53cSBarry Smith - message - (Optional) Message describing the difference, or `NULL` if there is no difference
7348609dae6eSVaclav Hapla 
7349609dae6eSVaclav Hapla   Level: intermediate
7350609dae6eSVaclav Hapla 
7351609dae6eSVaclav Hapla   Notes:
7352bb7acecfSBarry Smith   The output flag equal will be the same on all processes.
7353bb7acecfSBarry Smith 
7354*20f4b53cSBarry Smith   If equal is passed as `NULL` and difference is found, an error is thrown on all processes.
7355bb7acecfSBarry Smith 
7356*20f4b53cSBarry Smith   Make sure to pass equal is `NULL` on all processes or none of them.
7357609dae6eSVaclav Hapla 
73585efe38ccSVaclav Hapla   The output message is set independently on each rank.
7359bb7acecfSBarry Smith 
7360bb7acecfSBarry Smith   message must be freed with `PetscFree()`
7361bb7acecfSBarry Smith 
7362*20f4b53cSBarry Smith   If message is passed as `NULL` and a difference is found, the difference description is printed to stderr in synchronized manner.
7363bb7acecfSBarry Smith 
7364*20f4b53cSBarry Smith   Make sure to pass message as `NULL` on all processes or no processes.
7365609dae6eSVaclav Hapla 
7366609dae6eSVaclav Hapla   Labels are matched by name. If the number of labels and their names are equal,
7367bb7acecfSBarry Smith   `DMLabelCompare()` is used to compare each pair of labels with the same name.
7368609dae6eSVaclav Hapla 
7369bb7acecfSBarry Smith .seealso: `DMLabel`, `DMAddLabel()`, `DMCopyLabelsMode`, `DMLabelCompare()`
7370609dae6eSVaclav Hapla @*/
7371d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCompareLabels(DM dm0, DM dm1, PetscBool *equal, char **message)
7372d71ae5a4SJacob Faibussowitsch {
73735efe38ccSVaclav Hapla   PetscInt    n, i;
7374609dae6eSVaclav Hapla   char        msg[PETSC_MAX_PATH_LEN] = "";
73755efe38ccSVaclav Hapla   PetscBool   eq;
7376609dae6eSVaclav Hapla   MPI_Comm    comm;
73775efe38ccSVaclav Hapla   PetscMPIInt rank;
7378609dae6eSVaclav Hapla 
7379609dae6eSVaclav Hapla   PetscFunctionBegin;
7380609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm0, DM_CLASSID, 1);
7381609dae6eSVaclav Hapla   PetscValidHeaderSpecific(dm1, DM_CLASSID, 2);
7382609dae6eSVaclav Hapla   PetscCheckSameComm(dm0, 1, dm1, 2);
73835efe38ccSVaclav Hapla   if (equal) PetscValidBoolPointer(equal, 3);
7384609dae6eSVaclav Hapla   if (message) PetscValidPointer(message, 4);
73859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm0, &comm));
73869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
73875efe38ccSVaclav Hapla   {
73885efe38ccSVaclav Hapla     PetscInt n1;
73895efe38ccSVaclav Hapla 
73909566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm0, &n));
73919566063dSJacob Faibussowitsch     PetscCall(DMGetNumLabels(dm1, &n1));
73925efe38ccSVaclav Hapla     eq = (PetscBool)(n == n1);
739348a46eb9SPierre Jolivet     if (!eq) PetscCall(PetscSNPrintf(msg, sizeof(msg), "Number of labels in dm0 = %" PetscInt_FMT " != %" PetscInt_FMT " = Number of labels in dm1", n, n1));
73949566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
73955efe38ccSVaclav Hapla     if (!eq) goto finish;
73965efe38ccSVaclav Hapla   }
73975efe38ccSVaclav Hapla   for (i = 0; i < n; i++) {
7398609dae6eSVaclav Hapla     DMLabel     l0, l1;
7399609dae6eSVaclav Hapla     const char *name;
7400609dae6eSVaclav Hapla     char       *msgInner;
7401609dae6eSVaclav Hapla 
7402609dae6eSVaclav Hapla     /* Ignore label order */
74039566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm0, i, &l0));
74049566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)l0, &name));
74059566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm1, name, &l1));
7406609dae6eSVaclav Hapla     if (!l1) {
740763a3b9bcSJacob Faibussowitsch       PetscCall(PetscSNPrintf(msg, sizeof(msg), "Label \"%s\" (#%" PetscInt_FMT " in dm0) not found in dm1", name, i));
74085efe38ccSVaclav Hapla       eq = PETSC_FALSE;
74095efe38ccSVaclav Hapla       break;
7410609dae6eSVaclav Hapla     }
74119566063dSJacob Faibussowitsch     PetscCall(DMLabelCompare(comm, l0, l1, &eq, &msgInner));
74129566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(msg, msgInner, sizeof(msg)));
74139566063dSJacob Faibussowitsch     PetscCall(PetscFree(msgInner));
74145efe38ccSVaclav Hapla     if (!eq) break;
7415609dae6eSVaclav Hapla   }
74169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPIU_BOOL, MPI_LAND, comm));
7417609dae6eSVaclav Hapla finish:
74185efe38ccSVaclav Hapla   /* If message output arg not set, print to stderr */
7419609dae6eSVaclav Hapla   if (message) {
7420609dae6eSVaclav Hapla     *message = NULL;
742148a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscStrallocpy(msg, message));
74225efe38ccSVaclav Hapla   } else {
742348a46eb9SPierre Jolivet     if (msg[0]) PetscCall(PetscSynchronizedFPrintf(comm, PETSC_STDERR, "[%d] %s\n", rank, msg));
74249566063dSJacob Faibussowitsch     PetscCall(PetscSynchronizedFlush(comm, PETSC_STDERR));
74255efe38ccSVaclav Hapla   }
74265efe38ccSVaclav Hapla   /* If same output arg not ser and labels are not equal, throw error */
74275efe38ccSVaclav Hapla   if (equal) *equal = eq;
74287a8be351SBarry Smith   else PetscCheck(eq, comm, PETSC_ERR_ARG_INCOMP, "DMLabels are not the same in dm0 and dm1");
74293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7430609dae6eSVaclav Hapla }
7431609dae6eSVaclav Hapla 
7432d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetLabelValue_Fast(DM dm, DMLabel *label, const char name[], PetscInt point, PetscInt value)
7433d71ae5a4SJacob Faibussowitsch {
7434461a15a0SLisandro Dalcin   PetscFunctionBegin;
7435461a15a0SLisandro Dalcin   PetscValidPointer(label, 2);
7436461a15a0SLisandro Dalcin   if (!*label) {
74379566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
74389566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, label));
7439461a15a0SLisandro Dalcin   }
74409566063dSJacob Faibussowitsch   PetscCall(DMLabelSetValue(*label, point, value));
74413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7442461a15a0SLisandro Dalcin }
7443461a15a0SLisandro Dalcin 
74440fdc7489SMatthew Knepley /*
74450fdc7489SMatthew Knepley   Many mesh programs, such as Triangle and TetGen, allow only a single label for each mesh point. Therefore, we would
74460fdc7489SMatthew Knepley   like to encode all label IDs using a single, universal label. We can do this by assigning an integer to every
74470fdc7489SMatthew Knepley   (label, id) pair in the DM.
74480fdc7489SMatthew Knepley 
74490fdc7489SMatthew Knepley   However, a mesh point can have multiple labels, so we must separate all these values. We will assign a bit range to
74500fdc7489SMatthew Knepley   each label.
74510fdc7489SMatthew Knepley */
7452d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreate(DM dm, DMUniversalLabel *universal)
7453d71ae5a4SJacob Faibussowitsch {
74540fdc7489SMatthew Knepley   DMUniversalLabel ul;
74550fdc7489SMatthew Knepley   PetscBool       *active;
74560fdc7489SMatthew Knepley   PetscInt         pStart, pEnd, p, Nl, l, m;
74570fdc7489SMatthew Knepley 
74580fdc7489SMatthew Knepley   PetscFunctionBegin;
74599566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, &ul));
74609566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "universal", &ul->label));
74619566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
74629566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(Nl, &active));
74630fdc7489SMatthew Knepley   ul->Nl = 0;
74640fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
74650fdc7489SMatthew Knepley     PetscBool   isdepth, iscelltype;
74660fdc7489SMatthew Knepley     const char *name;
74670fdc7489SMatthew Knepley 
74689566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74699566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "depth", 6, &isdepth));
74709566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(name, "celltype", 9, &iscelltype));
74710fdc7489SMatthew Knepley     active[l] = !(isdepth || iscelltype) ? PETSC_TRUE : PETSC_FALSE;
74720fdc7489SMatthew Knepley     if (active[l]) ++ul->Nl;
74730fdc7489SMatthew Knepley   }
74749566063dSJacob 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));
74750fdc7489SMatthew Knepley   ul->Nv = 0;
74760fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
74770fdc7489SMatthew Knepley     DMLabel     label;
74780fdc7489SMatthew Knepley     PetscInt    nv;
74790fdc7489SMatthew Knepley     const char *name;
74800fdc7489SMatthew Knepley 
74810fdc7489SMatthew Knepley     if (!active[l]) continue;
74829566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &name));
74839566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
74849566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
74859566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &ul->names[m]));
74860fdc7489SMatthew Knepley     ul->indices[m] = l;
74870fdc7489SMatthew Knepley     ul->Nv += nv;
74880fdc7489SMatthew Knepley     ul->offsets[m + 1] = nv;
74890fdc7489SMatthew Knepley     ul->bits[m + 1]    = PetscCeilReal(PetscLog2Real(nv + 1));
74900fdc7489SMatthew Knepley     ++m;
74910fdc7489SMatthew Knepley   }
74920fdc7489SMatthew Knepley   for (l = 1; l <= ul->Nl; ++l) {
74930fdc7489SMatthew Knepley     ul->offsets[l] = ul->offsets[l - 1] + ul->offsets[l];
74940fdc7489SMatthew Knepley     ul->bits[l]    = ul->bits[l - 1] + ul->bits[l];
74950fdc7489SMatthew Knepley   }
74960fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
74970fdc7489SMatthew Knepley     PetscInt b;
74980fdc7489SMatthew Knepley 
74990fdc7489SMatthew Knepley     ul->masks[l] = 0;
75000fdc7489SMatthew Knepley     for (b = ul->bits[l]; b < ul->bits[l + 1]; ++b) ul->masks[l] |= 1 << b;
75010fdc7489SMatthew Knepley   }
75029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(ul->Nv, &ul->values));
75030fdc7489SMatthew Knepley   for (l = 0, m = 0; l < Nl; ++l) {
75040fdc7489SMatthew Knepley     DMLabel         label;
75050fdc7489SMatthew Knepley     IS              valueIS;
75060fdc7489SMatthew Knepley     const PetscInt *varr;
75070fdc7489SMatthew Knepley     PetscInt        nv, v;
75080fdc7489SMatthew Knepley 
75090fdc7489SMatthew Knepley     if (!active[l]) continue;
75109566063dSJacob Faibussowitsch     PetscCall(DMGetLabelByNum(dm, l, &label));
75119566063dSJacob Faibussowitsch     PetscCall(DMLabelGetNumValues(label, &nv));
75129566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
75139566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &varr));
7514ad540459SPierre Jolivet     for (v = 0; v < nv; ++v) ul->values[ul->offsets[m] + v] = varr[v];
75159566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &varr));
75169566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
75179566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(nv, &ul->values[ul->offsets[m]]));
75180fdc7489SMatthew Knepley     ++m;
75190fdc7489SMatthew Knepley   }
75209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
75210fdc7489SMatthew Knepley   for (p = pStart; p < pEnd; ++p) {
75220fdc7489SMatthew Knepley     PetscInt  uval   = 0;
75230fdc7489SMatthew Knepley     PetscBool marked = PETSC_FALSE;
75240fdc7489SMatthew Knepley 
75250fdc7489SMatthew Knepley     for (l = 0, m = 0; l < Nl; ++l) {
75260fdc7489SMatthew Knepley       DMLabel  label;
75270649b39aSStefano Zampini       PetscInt val, defval, loc, nv;
75280fdc7489SMatthew Knepley 
75290fdc7489SMatthew Knepley       if (!active[l]) continue;
75309566063dSJacob Faibussowitsch       PetscCall(DMGetLabelByNum(dm, l, &label));
75319566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, p, &val));
75329566063dSJacob Faibussowitsch       PetscCall(DMLabelGetDefaultValue(label, &defval));
75339371c9d4SSatish Balay       if (val == defval) {
75349371c9d4SSatish Balay         ++m;
75359371c9d4SSatish Balay         continue;
75369371c9d4SSatish Balay       }
75370649b39aSStefano Zampini       nv     = ul->offsets[m + 1] - ul->offsets[m];
75380fdc7489SMatthew Knepley       marked = PETSC_TRUE;
75399566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(val, nv, &ul->values[ul->offsets[m]], &loc));
754063a3b9bcSJacob Faibussowitsch       PetscCheck(loc >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " not found in compression array", val);
75410fdc7489SMatthew Knepley       uval += (loc + 1) << ul->bits[m];
75420fdc7489SMatthew Knepley       ++m;
75430fdc7489SMatthew Knepley     }
75449566063dSJacob Faibussowitsch     if (marked) PetscCall(DMLabelSetValue(ul->label, p, uval));
75450fdc7489SMatthew Knepley   }
75469566063dSJacob Faibussowitsch   PetscCall(PetscFree(active));
75470fdc7489SMatthew Knepley   *universal = ul;
75483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75490fdc7489SMatthew Knepley }
75500fdc7489SMatthew Knepley 
7551d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelDestroy(DMUniversalLabel *universal)
7552d71ae5a4SJacob Faibussowitsch {
75530fdc7489SMatthew Knepley   PetscInt l;
75540fdc7489SMatthew Knepley 
75550fdc7489SMatthew Knepley   PetscFunctionBegin;
75569566063dSJacob Faibussowitsch   for (l = 0; l < (*universal)->Nl; ++l) PetscCall(PetscFree((*universal)->names[l]));
75579566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&(*universal)->label));
75589566063dSJacob Faibussowitsch   PetscCall(PetscFree5((*universal)->names, (*universal)->indices, (*universal)->offsets, (*universal)->bits, (*universal)->masks));
75599566063dSJacob Faibussowitsch   PetscCall(PetscFree((*universal)->values));
75609566063dSJacob Faibussowitsch   PetscCall(PetscFree(*universal));
75610fdc7489SMatthew Knepley   *universal = NULL;
75623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75630fdc7489SMatthew Knepley }
75640fdc7489SMatthew Knepley 
7565d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelGetLabel(DMUniversalLabel ul, DMLabel *ulabel)
7566d71ae5a4SJacob Faibussowitsch {
75670fdc7489SMatthew Knepley   PetscFunctionBegin;
75680fdc7489SMatthew Knepley   PetscValidPointer(ulabel, 2);
75690fdc7489SMatthew Knepley   *ulabel = ul->label;
75703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75710fdc7489SMatthew Knepley }
75720fdc7489SMatthew Knepley 
7573d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelCreateLabels(DMUniversalLabel ul, PetscBool preserveOrder, DM dm)
7574d71ae5a4SJacob Faibussowitsch {
75750fdc7489SMatthew Knepley   PetscInt Nl = ul->Nl, l;
75760fdc7489SMatthew Knepley 
75770fdc7489SMatthew Knepley   PetscFunctionBegin;
7578064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 3);
75790fdc7489SMatthew Knepley   for (l = 0; l < Nl; ++l) {
75809566063dSJacob Faibussowitsch     if (preserveOrder) PetscCall(DMCreateLabelAtIndex(dm, ul->indices[l], ul->names[l]));
75819566063dSJacob Faibussowitsch     else PetscCall(DMCreateLabel(dm, ul->names[l]));
75820fdc7489SMatthew Knepley   }
75830fdc7489SMatthew Knepley   if (preserveOrder) {
75840fdc7489SMatthew Knepley     for (l = 0; l < ul->Nl; ++l) {
75850fdc7489SMatthew Knepley       const char *name;
75860fdc7489SMatthew Knepley       PetscBool   match;
75870fdc7489SMatthew Knepley 
75889566063dSJacob Faibussowitsch       PetscCall(DMGetLabelName(dm, ul->indices[l], &name));
75899566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ul->names[l], &match));
759063a3b9bcSJacob 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]);
75910fdc7489SMatthew Knepley     }
75920fdc7489SMatthew Knepley   }
75933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
75940fdc7489SMatthew Knepley }
75950fdc7489SMatthew Knepley 
7596d71ae5a4SJacob Faibussowitsch PetscErrorCode DMUniversalLabelSetLabelValue(DMUniversalLabel ul, DM dm, PetscBool useIndex, PetscInt p, PetscInt value)
7597d71ae5a4SJacob Faibussowitsch {
75980fdc7489SMatthew Knepley   PetscInt l;
75990fdc7489SMatthew Knepley 
76000fdc7489SMatthew Knepley   PetscFunctionBegin;
76010fdc7489SMatthew Knepley   for (l = 0; l < ul->Nl; ++l) {
76020fdc7489SMatthew Knepley     DMLabel  label;
76030fdc7489SMatthew Knepley     PetscInt lval = (value & ul->masks[l]) >> ul->bits[l];
76040fdc7489SMatthew Knepley 
76050fdc7489SMatthew Knepley     if (lval) {
76069566063dSJacob Faibussowitsch       if (useIndex) PetscCall(DMGetLabelByNum(dm, ul->indices[l], &label));
76079566063dSJacob Faibussowitsch       else PetscCall(DMGetLabel(dm, ul->names[l], &label));
76089566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, p, ul->values[ul->offsets[l] + lval - 1]));
76090fdc7489SMatthew Knepley     }
76100fdc7489SMatthew Knepley   }
76113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76120fdc7489SMatthew Knepley }
7613a8fb8f29SToby Isaac 
7614a8fb8f29SToby Isaac /*@
7615bb7acecfSBarry Smith   DMGetCoarseDM - Get the coarse `DM`from which this `DM` was obtained by refinement
7616bb7acecfSBarry Smith 
7617*20f4b53cSBarry Smith   Not Collective
7618a8fb8f29SToby Isaac 
7619a8fb8f29SToby Isaac   Input Parameter:
7620bb7acecfSBarry Smith . dm - The `DM` object
7621a8fb8f29SToby Isaac 
7622a8fb8f29SToby Isaac   Output Parameter:
7623bb7acecfSBarry Smith . cdm - The coarse `DM`
7624a8fb8f29SToby Isaac 
7625a8fb8f29SToby Isaac   Level: intermediate
7626a8fb8f29SToby Isaac 
7627bb7acecfSBarry Smith .seealso: `DMSetCoarseDM()`, `DMCoarsen()`
7628a8fb8f29SToby Isaac @*/
7629d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7630d71ae5a4SJacob Faibussowitsch {
7631a8fb8f29SToby Isaac   PetscFunctionBegin;
7632a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7633a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7634a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
76353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7636a8fb8f29SToby Isaac }
7637a8fb8f29SToby Isaac 
7638a8fb8f29SToby Isaac /*@
7639bb7acecfSBarry Smith   DMSetCoarseDM - Set the coarse `DM` from which this `DM` was obtained by refinement
7640a8fb8f29SToby Isaac 
7641a8fb8f29SToby Isaac   Input Parameters:
7642bb7acecfSBarry Smith + dm - The `DM` object
7643bb7acecfSBarry Smith - cdm - The coarse `DM`
7644a8fb8f29SToby Isaac 
7645a8fb8f29SToby Isaac   Level: intermediate
7646a8fb8f29SToby Isaac 
7647bb7acecfSBarry Smith   Note:
7648bb7acecfSBarry Smith   Normally this is set automatically by `DMRefine()`
7649bb7acecfSBarry Smith 
7650bb7acecfSBarry Smith .seealso: `DMGetCoarseDM()`, `DMCoarsen()`, `DMSetRefine()`, `DMSetFineDM()`
7651a8fb8f29SToby Isaac @*/
7652d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7653d71ae5a4SJacob Faibussowitsch {
7654a8fb8f29SToby Isaac   PetscFunctionBegin;
7655a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7656a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
765789d734beSBarry Smith   if (dm == cdm) cdm = NULL;
76589566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)cdm));
76599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->coarseMesh));
7660a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
76613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7662a8fb8f29SToby Isaac }
7663a8fb8f29SToby Isaac 
766488bdff64SToby Isaac /*@
7665bb7acecfSBarry Smith   DMGetFineDM - Get the fine mesh from which this `DM` was obtained by coarsening
766688bdff64SToby Isaac 
766788bdff64SToby Isaac   Input Parameter:
7668bb7acecfSBarry Smith . dm - The `DM` object
766988bdff64SToby Isaac 
767088bdff64SToby Isaac   Output Parameter:
7671bb7acecfSBarry Smith . fdm - The fine `DM`
767288bdff64SToby Isaac 
767388bdff64SToby Isaac   Level: intermediate
767488bdff64SToby Isaac 
7675bb7acecfSBarry Smith .seealso: `DMSetFineDM()`, `DMCoarsen()`, `DMRefine()`
767688bdff64SToby Isaac @*/
7677d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
7678d71ae5a4SJacob Faibussowitsch {
767988bdff64SToby Isaac   PetscFunctionBegin;
768088bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
768188bdff64SToby Isaac   PetscValidPointer(fdm, 2);
768288bdff64SToby Isaac   *fdm = dm->fineMesh;
76833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
768488bdff64SToby Isaac }
768588bdff64SToby Isaac 
768688bdff64SToby Isaac /*@
7687bb7acecfSBarry Smith   DMSetFineDM - Set the fine mesh from which this was obtained by coarsening
768888bdff64SToby Isaac 
768988bdff64SToby Isaac   Input Parameters:
7690bb7acecfSBarry Smith + dm - The `DM` object
7691bb7acecfSBarry Smith - fdm - The fine `DM`
769288bdff64SToby Isaac 
7693bb7acecfSBarry Smith   Level: developer
769488bdff64SToby Isaac 
7695bb7acecfSBarry Smith   Note:
7696bb7acecfSBarry Smith   Normally this is set automatically by `DMCoarsen()`
7697bb7acecfSBarry Smith 
7698bb7acecfSBarry Smith .seealso: `DMGetFineDM()`, `DMCoarsen()`, `DMRefine()`
769988bdff64SToby Isaac @*/
7700d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFineDM(DM dm, DM fdm)
7701d71ae5a4SJacob Faibussowitsch {
770288bdff64SToby Isaac   PetscFunctionBegin;
770388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
770488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
770589d734beSBarry Smith   if (dm == fdm) fdm = NULL;
77069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fdm));
77079566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&dm->fineMesh));
770888bdff64SToby Isaac   dm->fineMesh = fdm;
77093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
771088bdff64SToby Isaac }
771188bdff64SToby Isaac 
7712a6ba4734SToby Isaac /*@C
7713bb7acecfSBarry Smith   DMAddBoundary - Add a boundary condition to a model represented by a `DM`
7714a6ba4734SToby Isaac 
7715*20f4b53cSBarry Smith   Collective
7716783e2ec8SMatthew G. Knepley 
7717a6ba4734SToby Isaac   Input Parameters:
7718bb7acecfSBarry Smith + dm       - The `DM`, with a `PetscDS` that matches the problem being constrained
7719bb7acecfSBarry Smith . type     - The type of condition, e.g. `DM_BC_ESSENTIAL_ANALYTIC`, `DM_BC_ESSENTIAL_FIELD` (Dirichlet), or `DM_BC_NATURAL` (Neumann)
7720a6ba4734SToby Isaac . name     - The BC name
772145480ffeSMatthew G. Knepley . label    - The label defining constrained points
7722bb7acecfSBarry Smith . Nv       - The number of `DMLabel` values for constrained points
772345480ffeSMatthew G. Knepley . values   - An array of values for constrained points
7724a6ba4734SToby Isaac . field    - The field to constrain
772545480ffeSMatthew G. Knepley . Nc       - The number of constrained field components (0 will constrain all fields)
7726a6ba4734SToby Isaac . comps    - An array of constrained component numbers
7727a6ba4734SToby Isaac . bcFunc   - A pointwise function giving boundary values
772856cf3b9cSMatthew G. Knepley . bcFunc_t - A pointwise function giving the time deriative of the boundary values, or NULL
7729a6ba4734SToby Isaac - ctx      - An optional user context for bcFunc
7730a6ba4734SToby Isaac 
773145480ffeSMatthew G. Knepley   Output Parameter:
773245480ffeSMatthew G. Knepley . bd          - (Optional) Boundary number
773345480ffeSMatthew G. Knepley 
7734a6ba4734SToby Isaac   Options Database Keys:
7735a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7736a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7737a6ba4734SToby Isaac 
7738*20f4b53cSBarry Smith   Level: intermediate
7739*20f4b53cSBarry Smith 
7740bb7acecfSBarry Smith   Notes:
7741bb7acecfSBarry 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:
774256cf3b9cSMatthew G. Knepley 
7743*20f4b53cSBarry Smith $ void bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
774456cf3b9cSMatthew G. Knepley 
7745bb7acecfSBarry Smith   If the type is `DM_BC_ESSENTIAL_FIELD` or other _FIELD value, then the calling sequence is:
774656cf3b9cSMatthew G. Knepley 
7747*20f4b53cSBarry Smith .vb
7748*20f4b53cSBarry Smith   void bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
7749*20f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
7750*20f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
7751*20f4b53cSBarry Smith               PetscReal time, const PetscReal x[], PetscScalar bcval[])
7752*20f4b53cSBarry Smith .ve
775356cf3b9cSMatthew G. Knepley + dim - the spatial dimension
775456cf3b9cSMatthew G. Knepley . Nf - the number of fields
775556cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
775656cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
775756cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
775856cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
775956cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
776056cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
776156cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
776256cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
776356cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
776456cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
776556cf3b9cSMatthew G. Knepley . t - current time
776656cf3b9cSMatthew G. Knepley . x - coordinates of the current point
776756cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
776856cf3b9cSMatthew G. Knepley . constants - constant parameters
776956cf3b9cSMatthew G. Knepley - bcval - output values at the current point
777056cf3b9cSMatthew G. Knepley 
7771db781477SPatrick Sanan .seealso: `DSGetBoundary()`, `PetscDSAddBoundary()`
7772a6ba4734SToby Isaac @*/
7773d71ae5a4SJacob 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)
7774d71ae5a4SJacob Faibussowitsch {
7775e5e52638SMatthew G. Knepley   PetscDS ds;
7776a6ba4734SToby Isaac 
7777a6ba4734SToby Isaac   PetscFunctionBegin;
7778a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7779783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
778045480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 4);
778145480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nv, 5);
778245480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 7);
778345480ffeSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, Nc, 8);
778401a5d20dSJed Brown   PetscCheck(!dm->localSection, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot add boundary to DM after creating local section");
77859566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7786799db056SMatthew G. Knepley   /* Complete label */
7787799db056SMatthew G. Knepley   if (label) {
7788799db056SMatthew G. Knepley     PetscObject  obj;
7789799db056SMatthew G. Knepley     PetscClassId id;
7790799db056SMatthew G. Knepley 
7791799db056SMatthew G. Knepley     PetscCall(DMGetField(dm, field, NULL, &obj));
7792799db056SMatthew G. Knepley     PetscCall(PetscObjectGetClassId(obj, &id));
7793799db056SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
7794799db056SMatthew G. Knepley       DM plex;
7795799db056SMatthew G. Knepley 
7796799db056SMatthew G. Knepley       PetscCall(DMConvert(dm, DMPLEX, &plex));
7797799db056SMatthew G. Knepley       if (plex) PetscCall(DMPlexLabelComplete(plex, label));
7798799db056SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
7799799db056SMatthew G. Knepley     }
7800799db056SMatthew G. Knepley   }
78019566063dSJacob Faibussowitsch   PetscCall(PetscDSAddBoundary(ds, type, name, label, Nv, values, field, Nc, comps, bcFunc, bcFunc_t, ctx, bd));
78023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7803a6ba4734SToby Isaac }
7804a6ba4734SToby Isaac 
780545480ffeSMatthew G. Knepley /* TODO Remove this since now the structures are the same */
7806d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPopulateBoundary(DM dm)
7807d71ae5a4SJacob Faibussowitsch {
7808e5e52638SMatthew G. Knepley   PetscDS     ds;
7809dff059c6SToby Isaac   DMBoundary *lastnext;
7810e6f8dbb6SToby Isaac   DSBoundary  dsbound;
7811e6f8dbb6SToby Isaac 
7812e6f8dbb6SToby Isaac   PetscFunctionBegin;
78139566063dSJacob Faibussowitsch   PetscCall(DMGetDS(dm, &ds));
7814e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
781547a1f5adSToby Isaac   if (dm->boundary) {
781647a1f5adSToby Isaac     DMBoundary next = dm->boundary;
781747a1f5adSToby Isaac 
781847a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
78193ba16761SJacob Faibussowitsch     if (next->dsboundary == dsbound) PetscFunctionReturn(PETSC_SUCCESS);
782047a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
782147a1f5adSToby Isaac     while (next) {
782247a1f5adSToby Isaac       DMBoundary b = next;
782347a1f5adSToby Isaac 
782447a1f5adSToby Isaac       next = b->next;
78259566063dSJacob Faibussowitsch       PetscCall(PetscFree(b));
7826a6ba4734SToby Isaac     }
782747a1f5adSToby Isaac     dm->boundary = NULL;
7828a6ba4734SToby Isaac   }
782947a1f5adSToby Isaac 
7830dff059c6SToby Isaac   lastnext = &(dm->boundary);
7831e6f8dbb6SToby Isaac   while (dsbound) {
7832e6f8dbb6SToby Isaac     DMBoundary dmbound;
7833e6f8dbb6SToby Isaac 
78349566063dSJacob Faibussowitsch     PetscCall(PetscNew(&dmbound));
7835e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
783645480ffeSMatthew G. Knepley     dmbound->label      = dsbound->label;
783747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7838dff059c6SToby Isaac     *lastnext = dmbound;
7839dff059c6SToby Isaac     lastnext  = &(dmbound->next);
7840dff059c6SToby Isaac     dsbound   = dsbound->next;
7841a6ba4734SToby Isaac   }
78423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7843a6ba4734SToby Isaac }
7844a6ba4734SToby Isaac 
7845bb7acecfSBarry Smith /* TODO: missing manual page */
7846d71ae5a4SJacob Faibussowitsch PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7847d71ae5a4SJacob Faibussowitsch {
7848b95f2879SToby Isaac   DMBoundary b;
7849a6ba4734SToby Isaac 
7850a6ba4734SToby Isaac   PetscFunctionBegin;
7851a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7852534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7853a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
78549566063dSJacob Faibussowitsch   PetscCall(DMPopulateBoundary(dm));
7855b95f2879SToby Isaac   b = dm->boundary;
7856a6ba4734SToby Isaac   while (b && !(*isBd)) {
7857e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7858e6f8dbb6SToby Isaac     DSBoundary dsb   = b->dsboundary;
7859a6ba4734SToby Isaac     PetscInt   i;
7860a6ba4734SToby Isaac 
786145480ffeSMatthew G. Knepley     if (label) {
78629566063dSJacob Faibussowitsch       for (i = 0; i < dsb->Nv && !(*isBd); ++i) PetscCall(DMLabelStratumHasPoint(label, dsb->values[i], point, isBd));
7863a6ba4734SToby Isaac     }
7864a6ba4734SToby Isaac     b = b->next;
7865a6ba4734SToby Isaac   }
78663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7867a6ba4734SToby Isaac }
78684d6f44ffSToby Isaac 
78694d6f44ffSToby Isaac /*@C
7870bb7acecfSBarry Smith   DMProjectFunction - This projects the given function into the function space provided by a `DM`, putting the coefficients in a global vector.
7871a6e0b375SMatthew G. Knepley 
7872*20f4b53cSBarry Smith   Collective
78734d6f44ffSToby Isaac 
78744d6f44ffSToby Isaac   Input Parameters:
7875bb7acecfSBarry Smith + dm      - The `DM`
78760709b2feSToby Isaac . time    - The time
78774d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
78784d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
78794d6f44ffSToby Isaac - mode    - The insertion mode for values
78804d6f44ffSToby Isaac 
78814d6f44ffSToby Isaac   Output Parameter:
78824d6f44ffSToby Isaac . X - vector
78834d6f44ffSToby Isaac 
7884*20f4b53cSBarry Smith    Calling sequence of `funcs`:
7885*20f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
78864d6f44ffSToby Isaac +  dim - The spatial dimension
78878ec8862eSJed Brown .  time - The time at which to sample
78884d6f44ffSToby Isaac .  x   - The coordinates
788977b739a6SMatthew Knepley .  Nc  - The number of components
78904d6f44ffSToby Isaac .  u   - The output field values
78914d6f44ffSToby Isaac -  ctx - optional user-defined function context
78924d6f44ffSToby Isaac 
78934d6f44ffSToby Isaac   Level: developer
78944d6f44ffSToby Isaac 
7895bb7acecfSBarry Smith   Developer Notes:
7896bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7897bb7acecfSBarry Smith 
7898bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7899bb7acecfSBarry Smith 
7900db781477SPatrick Sanan .seealso: `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
79014d6f44ffSToby Isaac @*/
7902d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7903d71ae5a4SJacob Faibussowitsch {
79044d6f44ffSToby Isaac   Vec localX;
79054d6f44ffSToby Isaac 
79064d6f44ffSToby Isaac   PetscFunctionBegin;
79074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79089566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7909f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79109566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX));
79119566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
79129566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
79139566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
79143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79154d6f44ffSToby Isaac }
79164d6f44ffSToby Isaac 
7917a6e0b375SMatthew G. Knepley /*@C
7918bb7acecfSBarry Smith   DMProjectFunctionLocal - This projects the given function into the function space provided by a `DM`, putting the coefficients in a local vector.
7919a6e0b375SMatthew G. Knepley 
7920*20f4b53cSBarry Smith   Not Collective
7921a6e0b375SMatthew G. Knepley 
7922a6e0b375SMatthew G. Knepley   Input Parameters:
7923bb7acecfSBarry Smith + dm      - The `DM`
7924a6e0b375SMatthew G. Knepley . time    - The time
7925a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7926a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7927a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7928a6e0b375SMatthew G. Knepley 
7929a6e0b375SMatthew G. Knepley   Output Parameter:
7930a6e0b375SMatthew G. Knepley . localX - vector
7931a6e0b375SMatthew G. Knepley 
7932*20f4b53cSBarry Smith    Calling sequence of `funcs`:
7933*20f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7934a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7935a6e0b375SMatthew G. Knepley .  x   - The coordinates
793677b739a6SMatthew Knepley .  Nc  - The number of components
7937a6e0b375SMatthew G. Knepley .  u   - The output field values
7938a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7939a6e0b375SMatthew G. Knepley 
7940a6e0b375SMatthew G. Knepley   Level: developer
7941a6e0b375SMatthew G. Knepley 
7942bb7acecfSBarry Smith   Developer Notes:
7943bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7944bb7acecfSBarry Smith 
7945bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7946bb7acecfSBarry Smith 
7947db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
7948a6e0b375SMatthew G. Knepley @*/
7949d71ae5a4SJacob Faibussowitsch PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7950d71ae5a4SJacob Faibussowitsch {
79514d6f44ffSToby Isaac   PetscFunctionBegin;
79524d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7953064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
79549566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlocal)(dm, time, funcs, ctxs, mode, localX));
79553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79564d6f44ffSToby Isaac }
79574d6f44ffSToby Isaac 
7958a6e0b375SMatthew G. Knepley /*@C
7959bb7acecfSBarry 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.
7960a6e0b375SMatthew G. Knepley 
7961*20f4b53cSBarry Smith   Collective
7962a6e0b375SMatthew G. Knepley 
7963a6e0b375SMatthew G. Knepley   Input Parameters:
7964bb7acecfSBarry Smith + dm      - The `DM`
7965a6e0b375SMatthew G. Knepley . time    - The time
7966bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
7967a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
7968bb7acecfSBarry Smith . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs may be null.
7969a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
7970a6e0b375SMatthew G. Knepley 
7971a6e0b375SMatthew G. Knepley   Output Parameter:
7972a6e0b375SMatthew G. Knepley . X - vector
7973a6e0b375SMatthew G. Knepley 
7974*20f4b53cSBarry Smith    Calling sequence of `funcs`:
7975*20f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
7976a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
7977a6e0b375SMatthew G. Knepley .  x   - The coordinates
797877b739a6SMatthew Knepley .  Nc  - The number of components
7979a6e0b375SMatthew G. Knepley .  u   - The output field values
7980a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
7981a6e0b375SMatthew G. Knepley 
7982a6e0b375SMatthew G. Knepley   Level: developer
7983a6e0b375SMatthew G. Knepley 
7984bb7acecfSBarry Smith   Developer Notes:
7985bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
7986bb7acecfSBarry Smith 
7987bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
7988bb7acecfSBarry Smith 
7989db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabelLocal()`, `DMComputeL2Diff()`
7990a6e0b375SMatthew G. Knepley @*/
7991d71ae5a4SJacob 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)
7992d71ae5a4SJacob Faibussowitsch {
79932c53366bSMatthew G. Knepley   Vec localX;
79942c53366bSMatthew G. Knepley 
79952c53366bSMatthew G. Knepley   PetscFunctionBegin;
79962c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79979566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(dm, &localX));
7998f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
79999566063dSJacob Faibussowitsch   PetscCall(DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80009566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
80019566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
80029566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(dm, &localX));
80033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80042c53366bSMatthew G. Knepley }
80052c53366bSMatthew G. Knepley 
8006a6e0b375SMatthew G. Knepley /*@C
8007bb7acecfSBarry 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.
8008a6e0b375SMatthew G. Knepley 
8009*20f4b53cSBarry Smith   Not Collective
8010a6e0b375SMatthew G. Knepley 
8011a6e0b375SMatthew G. Knepley   Input Parameters:
8012bb7acecfSBarry Smith + dm      - The `DM`
8013a6e0b375SMatthew G. Knepley . time    - The time
8014bb7acecfSBarry Smith . label   - The `DMLabel` selecting the portion of the mesh for projection
8015a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8016a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8017a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8018a6e0b375SMatthew G. Knepley 
8019a6e0b375SMatthew G. Knepley   Output Parameter:
8020a6e0b375SMatthew G. Knepley . localX - vector
8021a6e0b375SMatthew G. Knepley 
8022*20f4b53cSBarry Smith    Calling sequence of `funcs`:
8023*20f4b53cSBarry Smith $  PetscErrorCode funcs(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar u[], void *ctx);
8024a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8025a6e0b375SMatthew G. Knepley .  x   - The coordinates
802677b739a6SMatthew Knepley .  Nc  - The number of components
8027a6e0b375SMatthew G. Knepley .  u   - The output field values
8028a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8029a6e0b375SMatthew G. Knepley 
8030a6e0b375SMatthew G. Knepley   Level: developer
8031a6e0b375SMatthew G. Knepley 
8032bb7acecfSBarry Smith   Developer Notes:
8033bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8034bb7acecfSBarry Smith 
8035bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8036bb7acecfSBarry Smith 
8037db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMProjectFunctionLocal()`, `DMProjectFunctionLabel()`, `DMComputeL2Diff()`
8038a6e0b375SMatthew G. Knepley @*/
8039d71ae5a4SJacob 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)
8040d71ae5a4SJacob Faibussowitsch {
80414d6f44ffSToby Isaac   PetscFunctionBegin;
80424d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8043064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
80449566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfunctionlabellocal)(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX));
80453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
80464d6f44ffSToby Isaac }
80472716604bSToby Isaac 
8048a6e0b375SMatthew G. Knepley /*@C
8049bb7acecfSBarry 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.
8050a6e0b375SMatthew G. Knepley 
8051*20f4b53cSBarry Smith   Not Collective
8052a6e0b375SMatthew G. Knepley 
8053a6e0b375SMatthew G. Knepley   Input Parameters:
8054bb7acecfSBarry Smith + dm      - The `DM`
8055a6e0b375SMatthew G. Knepley . time    - The time
8056*20f4b53cSBarry Smith . localU  - The input field vector; may be `NULL` if projection is defined purely by coordinates
8057a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8058a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8059a6e0b375SMatthew G. Knepley 
8060a6e0b375SMatthew G. Knepley   Output Parameter:
8061a6e0b375SMatthew G. Knepley . localX  - The output vector
8062a6e0b375SMatthew G. Knepley 
8063*20f4b53cSBarry Smith    Calling sequence of `funcs`:
8064*20f4b53cSBarry Smith .vb
8065*20f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8066*20f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8067*20f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8068*20f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8069*20f4b53cSBarry Smith .ve
8070a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8071a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8072a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8073a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8074a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8075a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8076a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8077a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8078a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8079a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8080a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8081a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8082a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8083a6e0b375SMatthew G. Knepley .  t            - The current time
8084a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8085a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8086a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8087a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8088a6e0b375SMatthew G. Knepley 
8089bb7acecfSBarry Smith   Note:
8090bb7acecfSBarry 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.
8091bb7acecfSBarry 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
8092bb7acecfSBarry 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
8093a6e0b375SMatthew 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.
8094a6e0b375SMatthew G. Knepley 
8095a6e0b375SMatthew G. Knepley   Level: intermediate
8096a6e0b375SMatthew G. Knepley 
8097bb7acecfSBarry Smith   Developer Notes:
8098bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8099bb7acecfSBarry Smith 
8100bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8101bb7acecfSBarry Smith 
8102db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8103a6e0b375SMatthew G. Knepley @*/
8104d71ae5a4SJacob 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)
8105d71ae5a4SJacob Faibussowitsch {
81068c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81078c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8108eb8f539aSJed Brown   if (localU) PetscValidHeaderSpecific(localU, VEC_CLASSID, 3);
81098c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX, VEC_CLASSID, 6);
81109566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlocal)(dm, time, localU, funcs, mode, localX));
81113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81128c6c5593SMatthew G. Knepley }
81138c6c5593SMatthew G. Knepley 
8114a6e0b375SMatthew G. Knepley /*@C
8115a6e0b375SMatthew 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.
8116a6e0b375SMatthew G. Knepley 
8117*20f4b53cSBarry Smith   Not Collective
8118a6e0b375SMatthew G. Knepley 
8119a6e0b375SMatthew G. Knepley   Input Parameters:
8120bb7acecfSBarry Smith + dm      - The `DM`
8121a6e0b375SMatthew G. Knepley . time    - The time
8122bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8123a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8124a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8125bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8126*20f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8127a6e0b375SMatthew G. Knepley . localU  - The input field vector
8128a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8129a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8130a6e0b375SMatthew G. Knepley 
8131a6e0b375SMatthew G. Knepley   Output Parameter:
8132a6e0b375SMatthew G. Knepley . localX  - The output vector
8133a6e0b375SMatthew G. Knepley 
8134*20f4b53cSBarry Smith    Calling sequence of `funcs`:
8135*20f4b53cSBarry Smith .vb
8136*20f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8137*20f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8138*20f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8139*20f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8140*20f4b53cSBarry Smith .ve
8141a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8142a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8143a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8144a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8145a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8146a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8147a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8148a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8149a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8150a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8151a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8152a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8153a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8154a6e0b375SMatthew G. Knepley .  t            - The current time
8155a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8156a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8157a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8158a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8159a6e0b375SMatthew G. Knepley 
8160bb7acecfSBarry Smith   Note:
8161bb7acecfSBarry 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.
8162bb7acecfSBarry 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
8163bb7acecfSBarry 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
8164a6e0b375SMatthew 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.
8165a6e0b375SMatthew G. Knepley 
8166a6e0b375SMatthew G. Knepley   Level: intermediate
8167a6e0b375SMatthew G. Knepley 
8168bb7acecfSBarry Smith   Developer Notes:
8169bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8170bb7acecfSBarry Smith 
8171bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8172bb7acecfSBarry Smith 
8173d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabel()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8174a6e0b375SMatthew G. Knepley @*/
8175d71ae5a4SJacob 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)
8176d71ae5a4SJacob Faibussowitsch {
81778c6c5593SMatthew G. Knepley   PetscFunctionBegin;
81788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8179064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8180064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
81819566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
81823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
81838c6c5593SMatthew G. Knepley }
81848c6c5593SMatthew G. Knepley 
81852716604bSToby Isaac /*@C
8186d29d7c6eSMatthew 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.
8187d29d7c6eSMatthew G. Knepley 
8188*20f4b53cSBarry Smith   Not Collective
8189d29d7c6eSMatthew G. Knepley 
8190d29d7c6eSMatthew G. Knepley   Input Parameters:
8191bb7acecfSBarry Smith + dm      - The `DM`
8192d29d7c6eSMatthew G. Knepley . time    - The time
8193bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain to output
8194d29d7c6eSMatthew G. Knepley . numIds  - The number of label ids to use
8195d29d7c6eSMatthew G. Knepley . ids     - The label ids to use for marking
8196bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8197*20f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8198d29d7c6eSMatthew G. Knepley . U       - The input field vector
8199d29d7c6eSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8200d29d7c6eSMatthew G. Knepley - mode    - The insertion mode for values
8201d29d7c6eSMatthew G. Knepley 
8202d29d7c6eSMatthew G. Knepley   Output Parameter:
8203d29d7c6eSMatthew G. Knepley . X       - The output vector
8204d29d7c6eSMatthew G. Knepley 
8205*20f4b53cSBarry Smith    Calling sequence of `funcs`:
8206*20f4b53cSBarry Smith .vb
8207*20f4b53cSBarry Smith   void func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8208*20f4b53cSBarry Smith             const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8209*20f4b53cSBarry Smith             const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8210*20f4b53cSBarry Smith             PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8211*20f4b53cSBarry Smith .ve
8212d29d7c6eSMatthew G. Knepley +  dim          - The spatial dimension
8213d29d7c6eSMatthew G. Knepley .  Nf           - The number of input fields
8214d29d7c6eSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8215d29d7c6eSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8216d29d7c6eSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8217d29d7c6eSMatthew G. Knepley .  u            - The field values at this point in space
8218d29d7c6eSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8219d29d7c6eSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8220d29d7c6eSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8221d29d7c6eSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8222d29d7c6eSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8223d29d7c6eSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8224d29d7c6eSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8225d29d7c6eSMatthew G. Knepley .  t            - The current time
8226d29d7c6eSMatthew G. Knepley .  x            - The coordinates of this point
8227d29d7c6eSMatthew G. Knepley .  numConstants - The number of constants
8228d29d7c6eSMatthew G. Knepley .  constants    - The value of each constant
8229d29d7c6eSMatthew G. Knepley -  f            - The value of the function at this point in space
8230d29d7c6eSMatthew G. Knepley 
8231bb7acecfSBarry Smith   Note:
8232bb7acecfSBarry 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.
8233bb7acecfSBarry 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
8234bb7acecfSBarry 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
8235d29d7c6eSMatthew 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.
8236d29d7c6eSMatthew G. Knepley 
8237d29d7c6eSMatthew G. Knepley   Level: intermediate
8238d29d7c6eSMatthew G. Knepley 
8239bb7acecfSBarry Smith   Developer Notes:
8240bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8241bb7acecfSBarry Smith 
8242bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8243bb7acecfSBarry Smith 
8244d29d7c6eSMatthew G. Knepley .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8245d29d7c6eSMatthew G. Knepley @*/
8246d71ae5a4SJacob 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)
8247d71ae5a4SJacob Faibussowitsch {
8248d29d7c6eSMatthew G. Knepley   DM  dmIn;
8249d29d7c6eSMatthew G. Knepley   Vec localU, localX;
8250d29d7c6eSMatthew G. Knepley 
8251d29d7c6eSMatthew G. Knepley   PetscFunctionBegin;
8252d29d7c6eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8253d29d7c6eSMatthew G. Knepley   PetscCall(VecGetDM(U, &dmIn));
8254d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dmIn, &localU));
8255d29d7c6eSMatthew G. Knepley   PetscCall(DMGetLocalVector(dm, &localX));
8256f60fa741SMatthew G. Knepley   PetscCall(VecSet(localX, 0.));
825772fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalBegin(dmIn, U, mode, localU));
825872fc1ce5SMatthew G. Knepley   PetscCall(DMGlobalToLocalEnd(dmIn, U, mode, localU));
8259d29d7c6eSMatthew G. Knepley   PetscCall(DMProjectFieldLabelLocal(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
8260d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalBegin(dm, localX, mode, X));
8261d29d7c6eSMatthew G. Knepley   PetscCall(DMLocalToGlobalEnd(dm, localX, mode, X));
8262d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dm, &localX));
8263d29d7c6eSMatthew G. Knepley   PetscCall(DMRestoreLocalVector(dmIn, &localU));
82643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8265d29d7c6eSMatthew G. Knepley }
8266d29d7c6eSMatthew G. Knepley 
8267d29d7c6eSMatthew G. Knepley /*@C
8268ece3a9fcSMatthew 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.
8269ece3a9fcSMatthew G. Knepley 
8270*20f4b53cSBarry Smith   Not Collective
8271ece3a9fcSMatthew G. Knepley 
8272ece3a9fcSMatthew G. Knepley   Input Parameters:
8273bb7acecfSBarry Smith + dm      - The `DM`
8274ece3a9fcSMatthew G. Knepley . time    - The time
8275bb7acecfSBarry Smith . label   - The `DMLabel` marking the portion of the domain boundary to output
8276ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8277ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8278bb7acecfSBarry Smith . Nc      - The number of components to set in the output, or `PETSC_DETERMINE` for all components
8279*20f4b53cSBarry Smith . comps   - The components to set in the output, or `NULL` for all components
8280ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8281ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8282ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8283ece3a9fcSMatthew G. Knepley 
8284ece3a9fcSMatthew G. Knepley   Output Parameter:
8285ece3a9fcSMatthew G. Knepley . localX  - The output vector
8286ece3a9fcSMatthew G. Knepley 
8287*20f4b53cSBarry Smith    Calling sequence of `funcs`:
8288*20f4b53cSBarry Smith .vb
8289*20f4b53cSBarry Smith    void funcs(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8290*20f4b53cSBarry Smith               const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8291*20f4b53cSBarry Smith               const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8292*20f4b53cSBarry Smith               PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8293*20f4b53cSBarry Smith .ve
8294ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8295ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8296ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8297ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8298ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8299ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8300ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8301ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8302ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8303ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8304ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8305ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8306ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8307ece3a9fcSMatthew G. Knepley .  t            - The current time
8308ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8309ece3a9fcSMatthew G. Knepley .  n            - The face normal
8310ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8311ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8312ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8313ece3a9fcSMatthew G. Knepley 
8314ece3a9fcSMatthew G. Knepley   Note:
8315bb7acecfSBarry 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.
8316bb7acecfSBarry 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
8317bb7acecfSBarry 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
8318ece3a9fcSMatthew 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.
8319ece3a9fcSMatthew G. Knepley 
8320ece3a9fcSMatthew G. Knepley   Level: intermediate
8321ece3a9fcSMatthew G. Knepley 
8322bb7acecfSBarry Smith   Developer Notes:
8323bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8324bb7acecfSBarry Smith 
8325bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8326bb7acecfSBarry Smith 
8327db781477SPatrick Sanan .seealso: `DMProjectField()`, `DMProjectFieldLabelLocal()`, `DMProjectFunction()`, `DMComputeL2Diff()`
8328ece3a9fcSMatthew G. Knepley @*/
8329d71ae5a4SJacob 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)
8330d71ae5a4SJacob Faibussowitsch {
8331ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8332ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8333064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localU, VEC_CLASSID, 8);
8334064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(localX, VEC_CLASSID, 11);
83359566063dSJacob Faibussowitsch   PetscCall((dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX));
83363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8337ece3a9fcSMatthew G. Knepley }
8338ece3a9fcSMatthew G. Knepley 
8339ece3a9fcSMatthew G. Knepley /*@C
83402716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
83412716604bSToby Isaac 
8342*20f4b53cSBarry Smith   Collective
8343bb7acecfSBarry Smith 
83442716604bSToby Isaac   Input Parameters:
8345bb7acecfSBarry Smith + dm    - The `DM`
83460709b2feSToby Isaac . time  - The time
83472716604bSToby Isaac . funcs - The functions to evaluate for each field component
83482716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8349574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
83502716604bSToby Isaac 
83512716604bSToby Isaac   Output Parameter:
83522716604bSToby Isaac . diff - The diff ||u - u_h||_2
83532716604bSToby Isaac 
83542716604bSToby Isaac   Level: developer
83552716604bSToby Isaac 
8356bb7acecfSBarry Smith   Developer Notes:
8357bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8358bb7acecfSBarry Smith 
8359bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8360bb7acecfSBarry Smith 
8361db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
83622716604bSToby Isaac @*/
8363d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
8364d71ae5a4SJacob Faibussowitsch {
83652716604bSToby Isaac   PetscFunctionBegin;
83662716604bSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8367b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
83689566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2diff)(dm, time, funcs, ctxs, X, diff));
83693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83702716604bSToby Isaac }
8371b698f381SToby Isaac 
8372b698f381SToby Isaac /*@C
8373b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8374b698f381SToby Isaac 
8375*20f4b53cSBarry Smith   Collective
8376d083f849SBarry Smith 
8377b698f381SToby Isaac   Input Parameters:
8378bb7acecfSBarry Smith + dm    - The `DM`
8379b698f381SToby Isaac , time  - The time
8380b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8381b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8382574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8383b698f381SToby Isaac - n     - The vector to project along
8384b698f381SToby Isaac 
8385b698f381SToby Isaac   Output Parameter:
8386b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8387b698f381SToby Isaac 
8388b698f381SToby Isaac   Level: developer
8389b698f381SToby Isaac 
8390bb7acecfSBarry Smith   Developer Notes:
8391bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8392bb7acecfSBarry Smith 
8393bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8394bb7acecfSBarry Smith 
8395bb7acecfSBarry Smith .seealso: `DMProjectFunction()`, `DMComputeL2Diff()`, `DMComputeL2FieldDiff()`
8396b698f381SToby Isaac @*/
8397d71ae5a4SJacob 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)
8398d71ae5a4SJacob Faibussowitsch {
8399b698f381SToby Isaac   PetscFunctionBegin;
8400b698f381SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8401b698f381SToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84029566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2gradientdiff)(dm, time, funcs, ctxs, X, n, diff));
84033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8404b698f381SToby Isaac }
8405b698f381SToby Isaac 
84062a16baeaSToby Isaac /*@C
84072a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
84082a16baeaSToby Isaac 
8409*20f4b53cSBarry Smith   Collective
8410d083f849SBarry Smith 
84112a16baeaSToby Isaac   Input Parameters:
8412bb7acecfSBarry Smith + dm    - The `DM`
84132a16baeaSToby Isaac . time  - The time
84142a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
84152a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8416574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
84172a16baeaSToby Isaac 
84182a16baeaSToby Isaac   Output Parameter:
84192a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
84202a16baeaSToby Isaac 
84212a16baeaSToby Isaac   Level: developer
84222a16baeaSToby Isaac 
8423bb7acecfSBarry Smith   Developer Notes:
8424bb7acecfSBarry Smith   This API is specific to only particular usage of `DM`
8425bb7acecfSBarry Smith 
8426bb7acecfSBarry Smith   The notes need to provide some information about what has to be provided to the `DM` to be able to perform the computation.
8427bb7acecfSBarry Smith 
8428db781477SPatrick Sanan .seealso: `DMProjectFunction()`, `DMComputeL2FieldDiff()`, `DMComputeL2GradientDiff()`
84292a16baeaSToby Isaac @*/
8430d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
8431d71ae5a4SJacob Faibussowitsch {
84322a16baeaSToby Isaac   PetscFunctionBegin;
84332a16baeaSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84342a16baeaSToby Isaac   PetscValidHeaderSpecific(X, VEC_CLASSID, 5);
84359566063dSJacob Faibussowitsch   PetscCall((dm->ops->computel2fielddiff)(dm, time, funcs, ctxs, X, diff));
84363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
84372a16baeaSToby Isaac }
84382a16baeaSToby Isaac 
8439df0b854cSToby Isaac /*@C
8440bb7acecfSBarry Smith  DMGetNeighbors - Gets an array containing the MPI ranks of all the processes neighbors
8441502a2867SDave May 
8442502a2867SDave May  Not Collective
8443502a2867SDave May 
8444502a2867SDave May  Input Parameter:
8445bb7acecfSBarry Smith .  dm    - The `DM`
8446502a2867SDave May 
84470a19bb7dSprj-  Output Parameters:
84480a19bb7dSprj- +  nranks - the number of neighbours
84490a19bb7dSprj- -  ranks - the neighbors ranks
8450502a2867SDave May 
8451bb7acecfSBarry Smith  Note:
8452bb7acecfSBarry Smith  Do not free the array, it is freed when the `DM` is destroyed.
8453502a2867SDave May 
8454502a2867SDave May  Level: beginner
8455502a2867SDave May 
8456db781477SPatrick Sanan  .seealso: `DMDAGetNeighbors()`, `PetscSFGetRootRanks()`
8457502a2867SDave May @*/
8458d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNeighbors(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
8459d71ae5a4SJacob Faibussowitsch {
8460502a2867SDave May   PetscFunctionBegin;
8461502a2867SDave May   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
84629566063dSJacob Faibussowitsch   PetscCall((dm->ops->getneighbors)(dm, nranks, ranks));
84633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8464502a2867SDave May }
8465502a2867SDave May 
8466531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8467531c7667SBarry Smith 
8468531c7667SBarry Smith /*
8469531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8470531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8471531c7667SBarry Smith */
8472d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringApply_AIJDM(Mat J, MatFDColoring coloring, Vec x1, void *sctx)
8473d71ae5a4SJacob Faibussowitsch {
8474531c7667SBarry Smith   PetscFunctionBegin;
8475531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8476531c7667SBarry Smith     Vec x1local;
8477531c7667SBarry Smith     DM  dm;
84789566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84797a8be351SBarry Smith     PetscCheck(dm, PetscObjectComm((PetscObject)J), PETSC_ERR_ARG_INCOMP, "IS_COLORING_LOCAL requires a DM");
84809566063dSJacob Faibussowitsch     PetscCall(DMGetLocalVector(dm, &x1local));
84819566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalBegin(dm, x1, INSERT_VALUES, x1local));
84829566063dSJacob Faibussowitsch     PetscCall(DMGlobalToLocalEnd(dm, x1, INSERT_VALUES, x1local));
8483531c7667SBarry Smith     x1 = x1local;
8484531c7667SBarry Smith   }
84859566063dSJacob Faibussowitsch   PetscCall(MatFDColoringApply_AIJ(J, coloring, x1, sctx));
8486531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8487531c7667SBarry Smith     DM dm;
84889566063dSJacob Faibussowitsch     PetscCall(MatGetDM(J, &dm));
84899566063dSJacob Faibussowitsch     PetscCall(DMRestoreLocalVector(dm, &x1));
8490531c7667SBarry Smith   }
84913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8492531c7667SBarry Smith }
8493531c7667SBarry Smith 
8494531c7667SBarry Smith /*@
8495bb7acecfSBarry Smith     MatFDColoringUseDM - allows a `MatFDColoring` object to use the `DM` associated with the matrix to compute a `IS_COLORING_LOCAL` coloring
8496531c7667SBarry Smith 
8497531c7667SBarry Smith     Input Parameter:
8498bb7acecfSBarry Smith .    coloring - the `MatFDColoring` object
8499531c7667SBarry Smith 
8500bb7acecfSBarry Smith     Developer Note:
8501bb7acecfSBarry Smith     this routine exists because the PETSc `Mat` library does not know about the `DM` objects
8502531c7667SBarry Smith 
85031b266c99SBarry Smith     Level: advanced
85041b266c99SBarry Smith 
8505db781477SPatrick Sanan .seealso: `MatFDColoring`, `MatFDColoringCreate()`, `ISColoringType`
8506531c7667SBarry Smith @*/
8507d71ae5a4SJacob Faibussowitsch PetscErrorCode MatFDColoringUseDM(Mat coloring, MatFDColoring fdcoloring)
8508d71ae5a4SJacob Faibussowitsch {
8509531c7667SBarry Smith   PetscFunctionBegin;
8510531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
85113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8512531c7667SBarry Smith }
85138320bc6fSPatrick Sanan 
85148320bc6fSPatrick Sanan /*@
8515bb7acecfSBarry Smith     DMGetCompatibility - determine if two `DM`s are compatible
85168320bc6fSPatrick Sanan 
85178320bc6fSPatrick Sanan     Collective
85188320bc6fSPatrick Sanan 
85198320bc6fSPatrick Sanan     Input Parameters:
8520bb7acecfSBarry Smith +    dm1 - the first `DM`
8521bb7acecfSBarry Smith -    dm2 - the second `DM`
85228320bc6fSPatrick Sanan 
85238320bc6fSPatrick Sanan     Output Parameters:
8524bb7acecfSBarry Smith +    compatible - whether or not the two `DM`s are compatible
8525bb7acecfSBarry Smith -    set - whether or not the compatible value was actually determined and set
85268320bc6fSPatrick Sanan 
8527*20f4b53cSBarry Smith     Level: advanced
8528*20f4b53cSBarry Smith 
85298320bc6fSPatrick Sanan     Notes:
8530bb7acecfSBarry Smith     Two `DM`s are deemed compatible if they represent the same parallel decomposition
85313d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
85328320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
8533bb7acecfSBarry Smith     Loosely speaking, compatible `DM`s represent the same domain and parallel
85343d862458SPatrick Sanan     decomposition, but hold different data.
85358320bc6fSPatrick Sanan 
85368320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
8537bb7acecfSBarry Smith     over a pair of vectors obtained from different `DM`s.
85388320bc6fSPatrick Sanan 
8539bb7acecfSBarry Smith     For example, two `DMDA` objects are compatible if they have the same local
85408320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
85418320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
8542bb7acecfSBarry Smith     either `DM` in bounds for a loop over vectors derived from either `DM`.
85438320bc6fSPatrick Sanan 
8544bb7acecfSBarry Smith     Consider the operation of summing data living on a 2-dof `DMDA` to data living
8545bb7acecfSBarry Smith     on a 1-dof `DMDA`, which should be compatible, as in the following snippet.
85468320bc6fSPatrick Sanan .vb
85478320bc6fSPatrick Sanan   ...
85489566063dSJacob Faibussowitsch   PetscCall(DMGetCompatibility(da1,da2,&compatible,&set));
85498320bc6fSPatrick Sanan   if (set && compatible)  {
85509566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da1,vec1,&arr1));
85519566063dSJacob Faibussowitsch     PetscCall(DMDAVecGetArrayDOF(da2,vec2,&arr2));
85529566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL));
85538320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
85548320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
85558320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
85568320bc6fSPatrick Sanan       }
85578320bc6fSPatrick Sanan     }
85589566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da1,vec1,&arr1));
85599566063dSJacob Faibussowitsch     PetscCall(DMDAVecRestoreArrayDOF(da2,vec2,&arr2));
85608320bc6fSPatrick Sanan   } else {
85618320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
85628320bc6fSPatrick Sanan   }
85638320bc6fSPatrick Sanan   ...
85648320bc6fSPatrick Sanan .ve
85658320bc6fSPatrick Sanan 
8566bb7acecfSBarry Smith     Checking compatibility might be expensive for a given implementation of `DM`,
85678320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
85688320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
85698320bc6fSPatrick Sanan     always check the "set" output parameter.
85708320bc6fSPatrick Sanan 
8571bb7acecfSBarry Smith     A `DM` is always compatible with itself.
85728320bc6fSPatrick Sanan 
8573bb7acecfSBarry Smith     In the current implementation, `DM`s which live on "unequal" communicators
85748320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
85758320bc6fSPatrick Sanan     incompatible.
85768320bc6fSPatrick Sanan 
85778320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
8578bb7acecfSBarry Smith     is required on each rank. However, in `DM` implementations which store all this
85798320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
85808320bc6fSPatrick Sanan 
8581bb7acecfSBarry Smith     Developer Note:
8582bb7acecfSBarry Smith     Compatibility is assumed to be a symmetric concept; `DM` A is compatible with `DM` B
85833d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8584a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
8585bb7acecfSBarry Smith     compatibility. It is left to `DM` implementers to ensure that symmetry is
85868320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
85873d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
8588bb7acecfSBarry Smith     of other `DM` types and let *set = PETSC_FALSE if found.
85898320bc6fSPatrick Sanan 
8590db781477SPatrick Sanan .seealso: `DM`, `DMDACreateCompatibleDMDA()`, `DMStagCreateCompatibleDMStag()`
85918320bc6fSPatrick Sanan @*/
8592d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetCompatibility(DM dm1, DM dm2, PetscBool *compatible, PetscBool *set)
8593d71ae5a4SJacob Faibussowitsch {
85948320bc6fSPatrick Sanan   PetscMPIInt compareResult;
85958320bc6fSPatrick Sanan   DMType      type, type2;
85968320bc6fSPatrick Sanan   PetscBool   sameType;
85978320bc6fSPatrick Sanan 
85988320bc6fSPatrick Sanan   PetscFunctionBegin;
8599a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
86008320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
86018320bc6fSPatrick Sanan 
86028320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8603a5bc1bf3SBarry Smith   if (dm1 == dm2) {
86048320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86058320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
86063ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86078320bc6fSPatrick Sanan   }
86088320bc6fSPatrick Sanan 
86098320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
86108320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
86118320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
86128320bc6fSPatrick Sanan      determined by the implementation-specific logic */
86139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)dm1), PetscObjectComm((PetscObject)dm2), &compareResult));
86148320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
86158320bc6fSPatrick Sanan     *set        = PETSC_TRUE;
86168320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
86173ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
86188320bc6fSPatrick Sanan   }
86198320bc6fSPatrick Sanan 
86208320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8621a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8622dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm1, getcompatibility, dm2, compatible, set);
86233ba16761SJacob Faibussowitsch     if (*set) PetscFunctionReturn(PETSC_SUCCESS);
86248320bc6fSPatrick Sanan   }
86258320bc6fSPatrick Sanan 
8626a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
86278320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
86289566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm1, &type));
86299566063dSJacob Faibussowitsch   PetscCall(DMGetType(dm2, &type2));
86309566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp(type, type2, &sameType));
86318320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8632dbbe0bcdSBarry Smith     PetscUseTypeMethod(dm2, getcompatibility, dm1, compatible, set); /* Note argument order */
86338320bc6fSPatrick Sanan   } else {
86348320bc6fSPatrick Sanan     *set = PETSC_FALSE;
86358320bc6fSPatrick Sanan   }
86363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
86378320bc6fSPatrick Sanan }
8638c0f0dcc3SMatthew G. Knepley 
8639c0f0dcc3SMatthew G. Knepley /*@C
8640bb7acecfSBarry Smith   DMMonitorSet - Sets an additional monitor function that is to be used after a solve to monitor discretization performance.
8641c0f0dcc3SMatthew G. Knepley 
8642*20f4b53cSBarry Smith   Logically Collective
8643c0f0dcc3SMatthew G. Knepley 
8644c0f0dcc3SMatthew G. Knepley   Input Parameters:
8645bb7acecfSBarry Smith + DM - the `DM`
8646c0f0dcc3SMatthew G. Knepley . f - the monitor function
8647*20f4b53cSBarry Smith . mctx - [optional] user-defined context for private data for the monitor routine (use `NULL` if no context is desired)
8648*20f4b53cSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`)
8649c0f0dcc3SMatthew G. Knepley 
8650*20f4b53cSBarry Smith   Options Database Key:
8651bb7acecfSBarry Smith - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `DMMonitorSet()`, but
8652c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8653c0f0dcc3SMatthew G. Knepley 
8654bb7acecfSBarry Smith   Note:
8655c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8656bb7acecfSBarry Smith   `DMMonitorSet()` multiple times or with `DMMonitorSetFromOptions()`; all will be called in the
8657c0f0dcc3SMatthew G. Knepley   order in which they were set.
8658c0f0dcc3SMatthew G. Knepley 
8659bb7acecfSBarry Smith   Fortran Note:
8660bb7acecfSBarry Smith   Only a single monitor function can be set for each `DM` object
8661bb7acecfSBarry Smith 
8662bb7acecfSBarry Smith   Developer Note:
8663bb7acecfSBarry Smith   This API has a generic name but seems specific to a very particular aspect of the use of `DM`
8664c0f0dcc3SMatthew G. Knepley 
8665c0f0dcc3SMatthew G. Knepley   Level: intermediate
8666c0f0dcc3SMatthew G. Knepley 
8667bb7acecfSBarry Smith .seealso: `DMMonitorCancel()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8668c0f0dcc3SMatthew G. Knepley @*/
8669d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **))
8670d71ae5a4SJacob Faibussowitsch {
8671c0f0dcc3SMatthew G. Knepley   PetscInt m;
8672c0f0dcc3SMatthew G. Knepley 
8673c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8674c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8675c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8676c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8677c0f0dcc3SMatthew G. Knepley 
86789566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical));
86793ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
8680c0f0dcc3SMatthew G. Knepley   }
86817a8be351SBarry Smith   PetscCheck(dm->numbermonitors < MAXDMMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8682c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8683c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8684c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *)mctx;
86853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8686c0f0dcc3SMatthew G. Knepley }
8687c0f0dcc3SMatthew G. Knepley 
8688c0f0dcc3SMatthew G. Knepley /*@
8689bb7acecfSBarry Smith   DMMonitorCancel - Clears all the monitor functions for a `DM` object.
8690c0f0dcc3SMatthew G. Knepley 
8691*20f4b53cSBarry Smith   Logically Collective
8692c0f0dcc3SMatthew G. Knepley 
8693c0f0dcc3SMatthew G. Knepley   Input Parameter:
8694c0f0dcc3SMatthew G. Knepley . dm - the DM
8695c0f0dcc3SMatthew G. Knepley 
8696c0f0dcc3SMatthew G. Knepley   Options Database Key:
8697c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
8698bb7acecfSBarry Smith   into a code by calls to `DMonitorSet()`, but does not cancel those
8699c0f0dcc3SMatthew G. Knepley   set via the options database
8700c0f0dcc3SMatthew G. Knepley 
8701bb7acecfSBarry Smith   Note:
8702bb7acecfSBarry Smith   There is no way to clear one specific monitor from a `DM` object.
8703c0f0dcc3SMatthew G. Knepley 
8704c0f0dcc3SMatthew G. Knepley   Level: intermediate
8705c0f0dcc3SMatthew G. Knepley 
8706bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`, `DMMonitor()`
8707c0f0dcc3SMatthew G. Knepley @*/
8708d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorCancel(DM dm)
8709d71ae5a4SJacob Faibussowitsch {
8710c0f0dcc3SMatthew G. Knepley   PetscInt m;
8711c0f0dcc3SMatthew G. Knepley 
8712c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8713c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8714c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
87159566063dSJacob Faibussowitsch     if (dm->monitordestroy[m]) PetscCall((*dm->monitordestroy[m])(&dm->monitorcontext[m]));
8716c0f0dcc3SMatthew G. Knepley   }
8717c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
87183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8719c0f0dcc3SMatthew G. Knepley }
8720c0f0dcc3SMatthew G. Knepley 
8721c0f0dcc3SMatthew G. Knepley /*@C
8722c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
8723c0f0dcc3SMatthew G. Knepley 
8724*20f4b53cSBarry Smith   Collective
8725c0f0dcc3SMatthew G. Knepley 
8726c0f0dcc3SMatthew G. Knepley   Input Parameters:
8727bb7acecfSBarry Smith + dm   - `DM` object you wish to monitor
8728c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
8729c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
8730c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
8731c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
8732bb7acecfSBarry 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
8733c0f0dcc3SMatthew G. Knepley 
8734c0f0dcc3SMatthew G. Knepley   Output Parameter:
8735c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
8736c0f0dcc3SMatthew G. Knepley 
8737c0f0dcc3SMatthew G. Knepley   Level: developer
8738c0f0dcc3SMatthew G. Knepley 
8739db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
8740db781477SPatrick Sanan           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
8741db781477SPatrick Sanan           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`,
8742db781477SPatrick Sanan           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
8743c2e3fba1SPatrick Sanan           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
8744db781477SPatrick Sanan           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
8745bb7acecfSBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `DMMonitor()`, `DMMonitorSet()`
8746c0f0dcc3SMatthew G. Knepley @*/
8747d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
8748d71ae5a4SJacob Faibussowitsch {
8749c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
8750c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
8751c0f0dcc3SMatthew G. Knepley 
8752c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8753c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm), ((PetscObject)dm)->options, ((PetscObject)dm)->prefix, name, &viewer, &format, flg));
8755c0f0dcc3SMatthew G. Knepley   if (*flg) {
8756c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
8757c0f0dcc3SMatthew G. Knepley 
87589566063dSJacob Faibussowitsch     PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
87599566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)viewer));
87609566063dSJacob Faibussowitsch     if (monitorsetup) PetscCall((*monitorsetup)(dm, vf));
87619566063dSJacob Faibussowitsch     PetscCall(DMMonitorSet(dm, (PetscErrorCode(*)(DM, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy));
8762c0f0dcc3SMatthew G. Knepley   }
87633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8764c0f0dcc3SMatthew G. Knepley }
8765c0f0dcc3SMatthew G. Knepley 
8766c0f0dcc3SMatthew G. Knepley /*@
8767c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
8768c0f0dcc3SMatthew G. Knepley 
8769*20f4b53cSBarry Smith    Collective
8770c0f0dcc3SMatthew G. Knepley 
8771c0f0dcc3SMatthew G. Knepley    Input Parameters:
8772bb7acecfSBarry Smith .  dm - The `DM`
8773c0f0dcc3SMatthew G. Knepley 
8774c0f0dcc3SMatthew G. Knepley    Level: developer
8775c0f0dcc3SMatthew G. Knepley 
8776bb7acecfSBarry Smith    Question:
8777bb7acecfSBarry 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
8778bb7acecfSBarry Smith    since some `DM` have no concept of discretization
8779bb7acecfSBarry Smith 
8780bb7acecfSBarry Smith .seealso: `DMMonitorSet()`, `DMMonitorSetFromOptions()`
8781c0f0dcc3SMatthew G. Knepley @*/
8782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMMonitor(DM dm)
8783d71ae5a4SJacob Faibussowitsch {
8784c0f0dcc3SMatthew G. Knepley   PetscInt m;
8785c0f0dcc3SMatthew G. Knepley 
8786c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
87873ba16761SJacob Faibussowitsch   if (!dm) PetscFunctionReturn(PETSC_SUCCESS);
8788c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
878948a46eb9SPierre Jolivet   for (m = 0; m < dm->numbermonitors; ++m) PetscCall((*dm->monitor[m])(dm, dm->monitorcontext[m]));
87903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8791c0f0dcc3SMatthew G. Knepley }
87922e4af2aeSMatthew G. Knepley 
87932e4af2aeSMatthew G. Knepley /*@
8794bb7acecfSBarry Smith   DMComputeError - Computes the error assuming the user has provided the exact solution functions
87952e4af2aeSMatthew G. Knepley 
8796*20f4b53cSBarry Smith   Collective
87972e4af2aeSMatthew G. Knepley 
87982e4af2aeSMatthew G. Knepley   Input Parameters:
8799bb7acecfSBarry Smith + dm     - The `DM`
88006b867d5aSJose E. Roman - sol    - The solution vector
88012e4af2aeSMatthew G. Knepley 
88026b867d5aSJose E. Roman   Input/Output Parameter:
8803*20f4b53cSBarry Smith . errors - An array of length Nf, the number of fields, or `NULL` for no output; on output
88046b867d5aSJose E. Roman            contains the error in each field
88056b867d5aSJose E. Roman 
88066b867d5aSJose E. Roman   Output Parameter:
8807*20f4b53cSBarry Smith . errorVec - A vector to hold the cellwise error (may be `NULL`)
8808*20f4b53cSBarry Smith 
8809*20f4b53cSBarry Smith   Level: developer
88102e4af2aeSMatthew G. Knepley 
8811bb7acecfSBarry Smith   Note:
8812bb7acecfSBarry Smith   The exact solutions come from the `PetscDS` object, and the time comes from `DMGetOutputSequenceNumber()`.
88132e4af2aeSMatthew G. Knepley 
8814db781477SPatrick Sanan .seealso: `DMMonitorSet()`, `DMGetRegionNumDS()`, `PetscDSGetExactSolution()`, `DMGetOutputSequenceNumber()`
88152e4af2aeSMatthew G. Knepley @*/
8816d71ae5a4SJacob Faibussowitsch PetscErrorCode DMComputeError(DM dm, Vec sol, PetscReal errors[], Vec *errorVec)
8817d71ae5a4SJacob Faibussowitsch {
88182e4af2aeSMatthew G. Knepley   PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *);
88192e4af2aeSMatthew G. Knepley   void    **ctxs;
88202e4af2aeSMatthew G. Knepley   PetscReal time;
88212e4af2aeSMatthew G. Knepley   PetscInt  Nf, f, Nds, s;
88222e4af2aeSMatthew G. Knepley 
88232e4af2aeSMatthew G. Knepley   PetscFunctionBegin;
88249566063dSJacob Faibussowitsch   PetscCall(DMGetNumFields(dm, &Nf));
88259566063dSJacob Faibussowitsch   PetscCall(PetscCalloc2(Nf, &exactSol, Nf, &ctxs));
88269566063dSJacob Faibussowitsch   PetscCall(DMGetNumDS(dm, &Nds));
88272e4af2aeSMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
88282e4af2aeSMatthew G. Knepley     PetscDS         ds;
88292e4af2aeSMatthew G. Knepley     DMLabel         label;
88302e4af2aeSMatthew G. Knepley     IS              fieldIS;
88312e4af2aeSMatthew G. Knepley     const PetscInt *fields;
88322e4af2aeSMatthew G. Knepley     PetscInt        dsNf;
88332e4af2aeSMatthew G. Knepley 
88349566063dSJacob Faibussowitsch     PetscCall(DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds));
88359566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(ds, &dsNf));
88369566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISGetIndices(fieldIS, &fields));
88372e4af2aeSMatthew G. Knepley     for (f = 0; f < dsNf; ++f) {
88382e4af2aeSMatthew G. Knepley       const PetscInt field = fields[f];
88399566063dSJacob Faibussowitsch       PetscCall(PetscDSGetExactSolution(ds, field, &exactSol[field], &ctxs[field]));
88402e4af2aeSMatthew G. Knepley     }
88419566063dSJacob Faibussowitsch     if (fieldIS) PetscCall(ISRestoreIndices(fieldIS, &fields));
88422e4af2aeSMatthew G. Knepley   }
8843ad540459SPierre 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);
88449566063dSJacob Faibussowitsch   PetscCall(DMGetOutputSequenceNumber(dm, NULL, &time));
88459566063dSJacob Faibussowitsch   if (errors) PetscCall(DMComputeL2FieldDiff(dm, time, exactSol, ctxs, sol, errors));
88462e4af2aeSMatthew G. Knepley   if (errorVec) {
88472e4af2aeSMatthew G. Knepley     DM             edm;
88482e4af2aeSMatthew G. Knepley     DMPolytopeType ct;
88492e4af2aeSMatthew G. Knepley     PetscBool      simplex;
88502e4af2aeSMatthew G. Knepley     PetscInt       dim, cStart, Nf;
88512e4af2aeSMatthew G. Knepley 
88529566063dSJacob Faibussowitsch     PetscCall(DMClone(dm, &edm));
88539566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(edm, &dim));
88549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
88559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellType(dm, cStart, &ct));
88562e4af2aeSMatthew G. Knepley     simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
88579566063dSJacob Faibussowitsch     PetscCall(DMGetNumFields(dm, &Nf));
88582e4af2aeSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
88592e4af2aeSMatthew G. Knepley       PetscFE         fe, efe;
88602e4af2aeSMatthew G. Knepley       PetscQuadrature q;
88612e4af2aeSMatthew G. Knepley       const char     *name;
88622e4af2aeSMatthew G. Knepley 
88639566063dSJacob Faibussowitsch       PetscCall(DMGetField(dm, f, NULL, (PetscObject *)&fe));
88649566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, Nf, simplex, 0, PETSC_DETERMINE, &efe));
88659566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetName((PetscObject)fe, &name));
88669566063dSJacob Faibussowitsch       PetscCall(PetscObjectSetName((PetscObject)efe, name));
88679566063dSJacob Faibussowitsch       PetscCall(PetscFEGetQuadrature(fe, &q));
88689566063dSJacob Faibussowitsch       PetscCall(PetscFESetQuadrature(efe, q));
88699566063dSJacob Faibussowitsch       PetscCall(DMSetField(edm, f, NULL, (PetscObject)efe));
88709566063dSJacob Faibussowitsch       PetscCall(PetscFEDestroy(&efe));
88712e4af2aeSMatthew G. Knepley     }
88729566063dSJacob Faibussowitsch     PetscCall(DMCreateDS(edm));
88732e4af2aeSMatthew G. Knepley 
88749566063dSJacob Faibussowitsch     PetscCall(DMCreateGlobalVector(edm, errorVec));
88759566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)*errorVec, "Error"));
88769566063dSJacob Faibussowitsch     PetscCall(DMPlexComputeL2DiffVec(dm, time, exactSol, ctxs, sol, *errorVec));
88779566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&edm));
88782e4af2aeSMatthew G. Knepley   }
88799566063dSJacob Faibussowitsch   PetscCall(PetscFree2(exactSol, ctxs));
88803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
88812e4af2aeSMatthew G. Knepley }
88829a2a23afSMatthew G. Knepley 
88839a2a23afSMatthew G. Knepley /*@
8884bb7acecfSBarry Smith   DMGetNumAuxiliaryVec - Get the number of auxiliary vectors associated with this `DM`
88859a2a23afSMatthew G. Knepley 
8886*20f4b53cSBarry Smith   Not Collective
88879a2a23afSMatthew G. Knepley 
88889a2a23afSMatthew G. Knepley   Input Parameter:
8889bb7acecfSBarry Smith . dm     - The `DM`
88909a2a23afSMatthew G. Knepley 
88919a2a23afSMatthew G. Knepley   Output Parameter:
8892a5b23f4aSJose E. Roman . numAux - The number of auxiliary data vectors
88939a2a23afSMatthew G. Knepley 
88949a2a23afSMatthew G. Knepley   Level: advanced
88959a2a23afSMatthew G. Knepley 
8896bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
88979a2a23afSMatthew G. Knepley @*/
8898d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetNumAuxiliaryVec(DM dm, PetscInt *numAux)
8899d71ae5a4SJacob Faibussowitsch {
89009a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89019a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89029566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetSize(dm->auxData, numAux));
89033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89049a2a23afSMatthew G. Knepley }
89059a2a23afSMatthew G. Knepley 
89069a2a23afSMatthew G. Knepley /*@
8907ac17215fSMatthew G. Knepley   DMGetAuxiliaryVec - Get the auxiliary vector for region specified by the given label and value, and equation part
89089a2a23afSMatthew G. Knepley 
8909*20f4b53cSBarry Smith   Not Collective
89109a2a23afSMatthew G. Knepley 
89119a2a23afSMatthew G. Knepley   Input Parameters:
8912bb7acecfSBarry Smith + dm     - The `DM`
8913bb7acecfSBarry Smith . label  - The `DMLabel`
8914ac17215fSMatthew G. Knepley . value  - The label value indicating the region
8915ac17215fSMatthew G. Knepley - part   - The equation part, or 0 if unused
89169a2a23afSMatthew G. Knepley 
89179a2a23afSMatthew G. Knepley   Output Parameter:
8918bb7acecfSBarry Smith . aux    - The `Vec` holding auxiliary field data
89199a2a23afSMatthew G. Knepley 
8920bb7acecfSBarry Smith   Note:
8921bb7acecfSBarry Smith   If no auxiliary vector is found for this (label, value), (NULL, 0, 0) is checked as well.
892204c51a94SMatthew G. Knepley 
89239a2a23afSMatthew G. Knepley   Level: advanced
89249a2a23afSMatthew G. Knepley 
8925bb7acecfSBarry Smith .seealso: `DMSetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryLabels()`
89269a2a23afSMatthew G. Knepley @*/
8927d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec *aux)
8928d71ae5a4SJacob Faibussowitsch {
8929ac17215fSMatthew G. Knepley   PetscHashAuxKey key, wild = {NULL, 0, 0};
893004c51a94SMatthew G. Knepley   PetscBool       has;
89319a2a23afSMatthew G. Knepley 
89329a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89339a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89349a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89359a2a23afSMatthew G. Knepley   key.label = label;
89369a2a23afSMatthew G. Knepley   key.value = value;
8937ac17215fSMatthew G. Knepley   key.part  = part;
89389566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxHas(dm->auxData, key, &has));
89399566063dSJacob Faibussowitsch   if (has) PetscCall(PetscHMapAuxGet(dm->auxData, key, aux));
89409566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxGet(dm->auxData, wild, aux));
89413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89429a2a23afSMatthew G. Knepley }
89439a2a23afSMatthew G. Knepley 
89449a2a23afSMatthew G. Knepley /*@
8945bb7acecfSBarry Smith   DMSetAuxiliaryVec - Set an auxiliary vector for region specified by the given label and value, and equation part
89469a2a23afSMatthew G. Knepley 
8947*20f4b53cSBarry Smith   Not Collective because auxiliary vectors are not parallel
89489a2a23afSMatthew G. Knepley 
89499a2a23afSMatthew G. Knepley   Input Parameters:
8950bb7acecfSBarry Smith + dm     - The `DM`
8951bb7acecfSBarry Smith . label  - The `DMLabel`
89529a2a23afSMatthew G. Knepley . value  - The label value indicating the region
8953ac17215fSMatthew G. Knepley . part   - The equation part, or 0 if unused
8954bb7acecfSBarry Smith - aux    - The `Vec` holding auxiliary field data
89559a2a23afSMatthew G. Knepley 
89569a2a23afSMatthew G. Knepley   Level: advanced
89579a2a23afSMatthew G. Knepley 
8958bb7acecfSBarry Smith .seealso: `DMGetAuxiliaryVec()`, `DMGetAuxiliaryLabels()`, `DMCopyAuxiliaryVec()`
89599a2a23afSMatthew G. Knepley @*/
8960d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetAuxiliaryVec(DM dm, DMLabel label, PetscInt value, PetscInt part, Vec aux)
8961d71ae5a4SJacob Faibussowitsch {
89629a2a23afSMatthew G. Knepley   Vec             old;
89639a2a23afSMatthew G. Knepley   PetscHashAuxKey key;
89649a2a23afSMatthew G. Knepley 
89659a2a23afSMatthew G. Knepley   PetscFunctionBegin;
89669a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
89679a2a23afSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
89689a2a23afSMatthew G. Knepley   key.label = label;
89699a2a23afSMatthew G. Knepley   key.value = value;
8970ac17215fSMatthew G. Knepley   key.part  = part;
89719566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGet(dm->auxData, key, &old));
89729566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)aux));
89739566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)old));
89749566063dSJacob Faibussowitsch   if (!aux) PetscCall(PetscHMapAuxDel(dm->auxData, key));
89759566063dSJacob Faibussowitsch   else PetscCall(PetscHMapAuxSet(dm->auxData, key, aux));
89763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
89779a2a23afSMatthew G. Knepley }
89789a2a23afSMatthew G. Knepley 
89799a2a23afSMatthew G. Knepley /*@C
8980bb7acecfSBarry Smith   DMGetAuxiliaryLabels - Get the labels, values, and parts for all auxiliary vectors in this `DM`
89819a2a23afSMatthew G. Knepley 
8982*20f4b53cSBarry Smith   Not Collective
89839a2a23afSMatthew G. Knepley 
89849a2a23afSMatthew G. Knepley   Input Parameter:
8985bb7acecfSBarry Smith . dm      - The `DM`
89869a2a23afSMatthew G. Knepley 
89879a2a23afSMatthew G. Knepley   Output Parameters:
8988bb7acecfSBarry Smith + labels  - The `DMLabel`s for each `Vec`
8989bb7acecfSBarry Smith . values  - The label values for each `Vec`
8990bb7acecfSBarry Smith - parts   - The equation parts for each `Vec`
89919a2a23afSMatthew G. Knepley 
8992bb7acecfSBarry Smith   Note:
8993bb7acecfSBarry Smith   The arrays passed in must be at least as large as `DMGetNumAuxiliaryVec()`.
89949a2a23afSMatthew G. Knepley 
89959a2a23afSMatthew G. Knepley   Level: advanced
89969a2a23afSMatthew G. Knepley 
8997bb7acecfSBarry Smith .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMGetNumAuxiliaryVec()`, `DMSetAuxiliaryVec()`, DMCopyAuxiliaryVec()`
89989a2a23afSMatthew G. Knepley @*/
8999d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetAuxiliaryLabels(DM dm, DMLabel labels[], PetscInt values[], PetscInt parts[])
9000d71ae5a4SJacob Faibussowitsch {
90019a2a23afSMatthew G. Knepley   PetscHashAuxKey *keys;
90029a2a23afSMatthew G. Knepley   PetscInt         n, i, off = 0;
90039a2a23afSMatthew G. Knepley 
90049a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90059a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90069a2a23afSMatthew G. Knepley   PetscValidPointer(labels, 2);
9007dadcf809SJacob Faibussowitsch   PetscValidIntPointer(values, 3);
9008dadcf809SJacob Faibussowitsch   PetscValidIntPointer(parts, 4);
90099566063dSJacob Faibussowitsch   PetscCall(DMGetNumAuxiliaryVec(dm, &n));
90109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &keys));
90119566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxGetKeys(dm->auxData, &off, keys));
90129371c9d4SSatish Balay   for (i = 0; i < n; ++i) {
90139371c9d4SSatish Balay     labels[i] = keys[i].label;
90149371c9d4SSatish Balay     values[i] = keys[i].value;
90159371c9d4SSatish Balay     parts[i]  = keys[i].part;
90169371c9d4SSatish Balay   }
90179566063dSJacob Faibussowitsch   PetscCall(PetscFree(keys));
90183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90199a2a23afSMatthew G. Knepley }
90209a2a23afSMatthew G. Knepley 
90219a2a23afSMatthew G. Knepley /*@
9022bb7acecfSBarry Smith   DMCopyAuxiliaryVec - Copy the auxiliary vector data on a `DM` to a new `DM`
90239a2a23afSMatthew G. Knepley 
9024*20f4b53cSBarry Smith   Not Collective
90259a2a23afSMatthew G. Knepley 
90269a2a23afSMatthew G. Knepley   Input Parameter:
9027bb7acecfSBarry Smith . dm    - The `DM`
90289a2a23afSMatthew G. Knepley 
90299a2a23afSMatthew G. Knepley   Output Parameter:
9030bb7acecfSBarry Smith . dmNew - The new `DM`, now with the same auxiliary data
90319a2a23afSMatthew G. Knepley 
90329a2a23afSMatthew G. Knepley   Level: advanced
90339a2a23afSMatthew G. Knepley 
9034bb7acecfSBarry Smith   Note:
9035bb7acecfSBarry Smith   This is a shallow copy of the auxiliary vectors
9036bb7acecfSBarry Smith 
9037db781477SPatrick Sanan .seealso: `DMGetNumAuxiliaryVec()`, `DMGetAuxiliaryVec()`, `DMSetAuxiliaryVec()`
90389a2a23afSMatthew G. Knepley @*/
9039d71ae5a4SJacob Faibussowitsch PetscErrorCode DMCopyAuxiliaryVec(DM dm, DM dmNew)
9040d71ae5a4SJacob Faibussowitsch {
90419a2a23afSMatthew G. Knepley   PetscFunctionBegin;
90429a2a23afSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90439566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDestroy(&dmNew->auxData));
90449566063dSJacob Faibussowitsch   PetscCall(PetscHMapAuxDuplicate(dm->auxData, &dmNew->auxData));
90453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
90469a2a23afSMatthew G. Knepley }
9047b5a892a1SMatthew G. Knepley 
9048b5a892a1SMatthew G. Knepley /*@C
9049bb7acecfSBarry Smith   DMPolytopeMatchOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9050b5a892a1SMatthew G. Knepley 
9051*20f4b53cSBarry Smith   Not Collective
9052b5a892a1SMatthew G. Knepley 
9053b5a892a1SMatthew G. Knepley   Input Parameters:
9054bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9055b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9056b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9057b5a892a1SMatthew G. Knepley 
9058b5a892a1SMatthew G. Knepley   Output Parameters:
9059bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9060b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9061b5a892a1SMatthew G. Knepley 
9062b5a892a1SMatthew G. Knepley   Level: advanced
9063b5a892a1SMatthew G. Knepley 
9064bb7acecfSBarry Smith   Note:
9065bb7acecfSBarry Smith   An arrangement is a face order combined with an orientation for each face
9066bb7acecfSBarry Smith 
9067bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9068bb7acecfSBarry Smith   that labels each arrangement (face ordering plus orientation for each face).
9069bb7acecfSBarry Smith 
9070bb7acecfSBarry Smith   See `DMPolytopeMatchVertexOrientation()` to find a new vertex orientation that takes the source vertex arrangement to the target vertex arrangement
9071bb7acecfSBarry Smith 
9072bb7acecfSBarry Smith .seealso: `DMPolytopeGetOrientation()`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetVertexOrientation()`
9073b5a892a1SMatthew G. Knepley @*/
9074d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt, PetscBool *found)
9075d71ae5a4SJacob Faibussowitsch {
9076b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetConeSize(ct);
9077b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9078b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9079b5a892a1SMatthew G. Knepley 
9080b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
90819371c9d4SSatish Balay   if (!nO) {
90829371c9d4SSatish Balay     *ornt  = 0;
90839371c9d4SSatish Balay     *found = PETSC_TRUE;
90843ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
90859371c9d4SSatish Balay   }
9086b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9087b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, o);
9088b5a892a1SMatthew G. Knepley 
90899371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
90909371c9d4SSatish Balay       if (sourceCone[arr[c * 2]] != targetCone[c]) break;
90919371c9d4SSatish Balay     if (c == cS) {
90929371c9d4SSatish Balay       *ornt = o;
90939371c9d4SSatish Balay       break;
90949371c9d4SSatish Balay     }
9095b5a892a1SMatthew G. Knepley   }
9096b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
90973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9098b5a892a1SMatthew G. Knepley }
9099b5a892a1SMatthew G. Knepley 
9100b5a892a1SMatthew G. Knepley /*@C
9101bb7acecfSBarry Smith   DMPolytopeGetOrientation - Determine an orientation (transformation) that takes the source face arrangement to the target face arrangement
9102b5a892a1SMatthew G. Knepley 
9103*20f4b53cSBarry Smith   Not Collective
9104b5a892a1SMatthew G. Knepley 
9105b5a892a1SMatthew G. Knepley   Input Parameters:
9106bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9107b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of faces
9108b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of faces
9109b5a892a1SMatthew G. Knepley 
9110b5a892a1SMatthew G. Knepley   Output Parameters:
9111bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9112b5a892a1SMatthew G. Knepley 
9113b5a892a1SMatthew G. Knepley   Level: advanced
9114b5a892a1SMatthew G. Knepley 
9115bb7acecfSBarry Smith   Note:
9116bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchOrientation()` except it will generate an error if no suitable orientation can be found.
9117bb7acecfSBarry Smith 
9118bb7acecfSBarry Smith   Developer Note:
9119bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchOrientation()` and error if none is found
9120bb7acecfSBarry Smith 
9121bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchOrientation()`, `DMPolytopeGetVertexOrientation()`, `DMPolytopeMatchVertexOrientation()`
9122b5a892a1SMatthew G. Knepley @*/
9123d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9124d71ae5a4SJacob Faibussowitsch {
9125b5a892a1SMatthew G. Knepley   PetscBool found;
9126b5a892a1SMatthew G. Knepley 
9127b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91289566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchOrientation(ct, sourceCone, targetCone, ornt, &found));
91297a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
91303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9131b5a892a1SMatthew G. Knepley }
9132b5a892a1SMatthew G. Knepley 
9133b5a892a1SMatthew G. Knepley /*@C
9134bb7acecfSBarry Smith   DMPolytopeMatchVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9135b5a892a1SMatthew G. Knepley 
9136*20f4b53cSBarry Smith   Not Collective
9137b5a892a1SMatthew G. Knepley 
9138b5a892a1SMatthew G. Knepley   Input Parameters:
9139bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9140b5a892a1SMatthew G. Knepley . sourceVert - The source arrangement of vertices
9141b5a892a1SMatthew G. Knepley - targetVert - The target arrangement of vertices
9142b5a892a1SMatthew G. Knepley 
9143b5a892a1SMatthew G. Knepley   Output Parameters:
9144bb7acecfSBarry Smith + ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9145b5a892a1SMatthew G. Knepley - found - Flag indicating that a suitable orientation was found
9146b5a892a1SMatthew G. Knepley 
9147b5a892a1SMatthew G. Knepley   Level: advanced
9148b5a892a1SMatthew G. Knepley 
9149bb7acecfSBarry Smith   Note:
9150bb7acecfSBarry Smith   An arrangement is a vertex order
9151bb7acecfSBarry Smith 
9152bb7acecfSBarry Smith   Each orientation (transformation) is labeled with an integer from negative `DMPolytopeTypeGetNumArrangments(ct)`/2 to `DMPolytopeTypeGetNumArrangments(ct)`/2
9153bb7acecfSBarry Smith   that labels each arrangement (vertex ordering).
9154bb7acecfSBarry Smith 
9155bb7acecfSBarry Smith   See `DMPolytopeMatchOrientation()` to find a new face orientation that takes the source face arrangement to the target face arrangement
9156bb7acecfSBarry Smith 
9157bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeGetOrientation()`, `DMPolytopeMatchOrientation()`, `DMPolytopeTypeGetNumVertices()`, `DMPolytopeTypeGetVertexArrangment()`
9158b5a892a1SMatthew G. Knepley @*/
9159d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeMatchVertexOrientation(DMPolytopeType ct, const PetscInt sourceVert[], const PetscInt targetVert[], PetscInt *ornt, PetscBool *found)
9160d71ae5a4SJacob Faibussowitsch {
9161b5a892a1SMatthew G. Knepley   const PetscInt cS = DMPolytopeTypeGetNumVertices(ct);
9162b5a892a1SMatthew G. Knepley   const PetscInt nO = DMPolytopeTypeGetNumArrangments(ct) / 2;
9163b5a892a1SMatthew G. Knepley   PetscInt       o, c;
9164b5a892a1SMatthew G. Knepley 
9165b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
91669371c9d4SSatish Balay   if (!nO) {
91679371c9d4SSatish Balay     *ornt  = 0;
91689371c9d4SSatish Balay     *found = PETSC_TRUE;
91693ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
91709371c9d4SSatish Balay   }
9171b5a892a1SMatthew G. Knepley   for (o = -nO; o < nO; ++o) {
9172b5a892a1SMatthew G. Knepley     const PetscInt *arr = DMPolytopeTypeGetVertexArrangment(ct, o);
9173b5a892a1SMatthew G. Knepley 
91749371c9d4SSatish Balay     for (c = 0; c < cS; ++c)
91759371c9d4SSatish Balay       if (sourceVert[arr[c]] != targetVert[c]) break;
91769371c9d4SSatish Balay     if (c == cS) {
91779371c9d4SSatish Balay       *ornt = o;
91789371c9d4SSatish Balay       break;
91799371c9d4SSatish Balay     }
9180b5a892a1SMatthew G. Knepley   }
9181b5a892a1SMatthew G. Knepley   *found = o == nO ? PETSC_FALSE : PETSC_TRUE;
91823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9183b5a892a1SMatthew G. Knepley }
9184b5a892a1SMatthew G. Knepley 
9185b5a892a1SMatthew G. Knepley /*@C
9186bb7acecfSBarry Smith   DMPolytopeGetVertexOrientation - Determine an orientation (transformation) that takes the source vertex arrangement to the target vertex arrangement
9187b5a892a1SMatthew G. Knepley 
9188*20f4b53cSBarry Smith   Not Collective
9189b5a892a1SMatthew G. Knepley 
9190b5a892a1SMatthew G. Knepley   Input Parameters:
9191bb7acecfSBarry Smith + ct         - The `DMPolytopeType`
9192b5a892a1SMatthew G. Knepley . sourceCone - The source arrangement of vertices
9193b5a892a1SMatthew G. Knepley - targetCone - The target arrangement of vertices
9194b5a892a1SMatthew G. Knepley 
9195b5a892a1SMatthew G. Knepley   Output Parameters:
9196bb7acecfSBarry Smith . ornt  - The orientation (transformation) which will take the source arrangement to the target arrangement
9197b5a892a1SMatthew G. Knepley 
9198b5a892a1SMatthew G. Knepley   Level: advanced
9199b5a892a1SMatthew G. Knepley 
9200bb7acecfSBarry Smith   Note:
9201bb7acecfSBarry Smith   This function is the same as `DMPolytopeMatchVertexOrientation()` except it errors if not orientation is possible.
9202bb7acecfSBarry Smith 
9203bb7acecfSBarry Smith   Developer Note:
9204bb7acecfSBarry Smith   It is unclear why this function needs to exist since one can simply call `DMPolytopeMatchVertexOrientation()` and error if none is found
9205bb7acecfSBarry Smith 
9206bb7acecfSBarry Smith .seealso: `DMPolytopeType`, `DMPolytopeMatchVertexOrientation()`, `DMPolytopeGetOrientation()`
9207b5a892a1SMatthew G. Knepley @*/
9208d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeGetVertexOrientation(DMPolytopeType ct, const PetscInt sourceCone[], const PetscInt targetCone[], PetscInt *ornt)
9209d71ae5a4SJacob Faibussowitsch {
9210b5a892a1SMatthew G. Knepley   PetscBool found;
9211b5a892a1SMatthew G. Knepley 
9212b5a892a1SMatthew G. Knepley   PetscFunctionBegin;
92139566063dSJacob Faibussowitsch   PetscCall(DMPolytopeMatchVertexOrientation(ct, sourceCone, targetCone, ornt, &found));
92147a8be351SBarry Smith   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find orientation for %s", DMPolytopeTypes[ct]);
92153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9216b5a892a1SMatthew G. Knepley }
9217012bc364SMatthew G. Knepley 
9218012bc364SMatthew G. Knepley /*@C
9219012bc364SMatthew G. Knepley   DMPolytopeInCellTest - Check whether a point lies inside the reference cell of given type
9220012bc364SMatthew G. Knepley 
9221*20f4b53cSBarry Smith   Not Collective
9222012bc364SMatthew G. Knepley 
9223012bc364SMatthew G. Knepley   Input Parameters:
9224bb7acecfSBarry Smith + ct    - The `DMPolytopeType`
9225012bc364SMatthew G. Knepley - point - Coordinates of the point
9226012bc364SMatthew G. Knepley 
9227012bc364SMatthew G. Knepley   Output Parameters:
9228012bc364SMatthew G. Knepley . inside  - Flag indicating whether the point is inside the reference cell of given type
9229012bc364SMatthew G. Knepley 
9230012bc364SMatthew G. Knepley   Level: advanced
9231012bc364SMatthew G. Knepley 
9232bb7acecfSBarry Smith .seealso: `DM`, `DMPolytopeType`, `DMLocatePoints()`
9233012bc364SMatthew G. Knepley @*/
9234d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPolytopeInCellTest(DMPolytopeType ct, const PetscReal point[], PetscBool *inside)
9235d71ae5a4SJacob Faibussowitsch {
9236012bc364SMatthew G. Knepley   PetscReal sum = 0.0;
9237012bc364SMatthew G. Knepley   PetscInt  d;
9238012bc364SMatthew G. Knepley 
9239012bc364SMatthew G. Knepley   PetscFunctionBegin;
9240012bc364SMatthew G. Knepley   *inside = PETSC_TRUE;
9241012bc364SMatthew G. Knepley   switch (ct) {
9242012bc364SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
9243012bc364SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
9244012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d) {
92459371c9d4SSatish Balay       if (point[d] < -1.0) {
92469371c9d4SSatish Balay         *inside = PETSC_FALSE;
92479371c9d4SSatish Balay         break;
92489371c9d4SSatish Balay       }
9249012bc364SMatthew G. Knepley       sum += point[d];
9250012bc364SMatthew G. Knepley     }
92519371c9d4SSatish Balay     if (sum > PETSC_SMALL) {
92529371c9d4SSatish Balay       *inside = PETSC_FALSE;
92539371c9d4SSatish Balay       break;
92549371c9d4SSatish Balay     }
9255012bc364SMatthew G. Knepley     break;
9256012bc364SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
9257012bc364SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
9258012bc364SMatthew G. Knepley     for (d = 0; d < DMPolytopeTypeGetDim(ct); ++d)
92599371c9d4SSatish Balay       if (PetscAbsReal(point[d]) > 1. + PETSC_SMALL) {
92609371c9d4SSatish Balay         *inside = PETSC_FALSE;
9261012bc364SMatthew G. Knepley         break;
92629371c9d4SSatish Balay       }
92639371c9d4SSatish Balay     break;
9264d71ae5a4SJacob Faibussowitsch   default:
9265d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unsupported polytope type %s", DMPolytopeTypes[ct]);
9266012bc364SMatthew G. Knepley   }
92673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9268012bc364SMatthew G. Knepley }
9269